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