|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2016 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a network reply delegate allowing to check redirects. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSignal, QObject |
|
13 from PyQt5.QtNetwork import QNetworkRequest |
|
14 |
|
15 |
|
16 class FollowRedirectReply(QObject): |
|
17 """ |
|
18 Class implementing a network reply delegate allowing to check redirects. |
|
19 """ |
|
20 finished = pyqtSignal() |
|
21 |
|
22 def __init__(self, url, manager, maxRedirects=5): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param url URL to get (QUrl) |
|
27 @param manager reference to the network access manager |
|
28 (QNetworkAccessManager) |
|
29 @keyparam maxRedirects maximum allowed redirects (integer) |
|
30 """ |
|
31 super(FollowRedirectReply, self).__init__() |
|
32 |
|
33 self.__manager = manager |
|
34 self.__maxRedirects = maxRedirects |
|
35 self.__redirectCount = 0 |
|
36 |
|
37 self.__reply = self.__manager.get(QNetworkRequest(url)) |
|
38 self.__reply.finished.connect(self.__replyFinished) |
|
39 |
|
40 def reply(self): |
|
41 """ |
|
42 Public method to get the reply object. |
|
43 |
|
44 @return reference to the reply object (QNetworkReply) |
|
45 """ |
|
46 return self.__reply |
|
47 |
|
48 def originalUrl(self): |
|
49 """ |
|
50 Public method to get the original URL. |
|
51 |
|
52 @return original URL (QUrl) |
|
53 """ |
|
54 return self.__reply.request().url() |
|
55 |
|
56 def url(self): |
|
57 """ |
|
58 Public method to get the final URL (after redirects). |
|
59 |
|
60 @return final URL (QUrl) |
|
61 """ |
|
62 return self.__reply.url() |
|
63 |
|
64 def error(self): |
|
65 """ |
|
66 Public method to get the error information. |
|
67 |
|
68 @return error code (QNetworkReply.NetworkError) |
|
69 """ |
|
70 return self.__reply.error() |
|
71 |
|
72 def errorString(self): |
|
73 """ |
|
74 Public method to get the error message. |
|
75 |
|
76 @return error message (string) |
|
77 """ |
|
78 return self.__reply.errorString() |
|
79 |
|
80 def readAll(self): |
|
81 """ |
|
82 Public method to read all received data. |
|
83 |
|
84 @return received raw data (QByteArray) |
|
85 """ |
|
86 return self.__reply.readAll() |
|
87 |
|
88 def close(self): |
|
89 """ |
|
90 Public method to close the data stream. |
|
91 """ |
|
92 self.__reply.close() |
|
93 |
|
94 def __replyFinished(self): |
|
95 """ |
|
96 Private slot handling the receipt of the requested data. |
|
97 """ |
|
98 replyStatus = self.__reply.attribute( |
|
99 QNetworkRequest.HttpStatusCodeAttribute) |
|
100 if (replyStatus != 301 and replyStatus != 302) or \ |
|
101 self.__redirectCount == self.__maxRedirects: |
|
102 self.finished.emit() |
|
103 return |
|
104 |
|
105 self.__redirectCount += 1 |
|
106 |
|
107 redirectUrl = self.__reply.attribute( |
|
108 QNetworkRequest.RedirectionTargetAttribute) |
|
109 self.__reply.close() |
|
110 self.__reply.deleteLater() |
|
111 self.__reply = None |
|
112 |
|
113 self.__reply = self.__manager.get(QNetworkRequest(redirectUrl)) |
|
114 self.__reply.finished.connect(self.__replyFinished) |