43 |
41 |
44 |
42 |
45 def proxyAuthenticationRequired(proxy, auth): |
43 def proxyAuthenticationRequired(proxy, auth): |
46 """ |
44 """ |
47 Module slot to handle a proxy authentication request. |
45 Module slot to handle a proxy authentication request. |
48 |
46 |
49 @param proxy reference to the proxy object (QNetworkProxy) |
47 @param proxy reference to the proxy object (QNetworkProxy) |
50 @param auth reference to the authenticator object (QAuthenticator) |
48 @param auth reference to the authenticator object (QAuthenticator) |
51 """ |
49 """ |
52 info = QCoreApplication.translate( |
50 info = QCoreApplication.translate( |
53 "EricNetworkProxyFactory", |
51 "EricNetworkProxyFactory", "<b>Connect to proxy '{0}' using:</b>" |
54 "<b>Connect to proxy '{0}' using:</b>" |
|
55 ).format(Utilities.html_encode(proxy.hostName())) |
52 ).format(Utilities.html_encode(proxy.hostName())) |
56 |
53 |
57 from UI.AuthenticationDialog import AuthenticationDialog |
54 from UI.AuthenticationDialog import AuthenticationDialog |
|
55 |
58 dlg = AuthenticationDialog(info, proxy.user(), True) |
56 dlg = AuthenticationDialog(info, proxy.user(), True) |
59 dlg.setData(proxy.user(), proxy.password()) |
57 dlg.setData(proxy.user(), proxy.password()) |
60 if dlg.exec() == QDialog.DialogCode.Accepted: |
58 if dlg.exec() == QDialog.DialogCode.Accepted: |
61 username, password = dlg.getData() |
59 username, password = dlg.getData() |
62 auth.setUser(username) |
60 auth.setUser(username) |
72 |
70 |
73 class HostnameMatcher: |
71 class HostnameMatcher: |
74 """ |
72 """ |
75 Class implementing a matcher for host names. |
73 Class implementing a matcher for host names. |
76 """ |
74 """ |
|
75 |
77 def __init__(self, pattern): |
76 def __init__(self, pattern): |
78 """ |
77 """ |
79 Constructor |
78 Constructor |
80 |
79 |
81 @param pattern pattern to be matched against |
80 @param pattern pattern to be matched against |
82 @type str |
81 @type str |
83 """ |
82 """ |
84 self.__regExp = None |
83 self.__regExp = None |
85 self.setPattern(pattern) |
84 self.setPattern(pattern) |
86 |
85 |
87 def setPattern(self, pattern): |
86 def setPattern(self, pattern): |
88 """ |
87 """ |
89 Public method to set the match pattern. |
88 Public method to set the match pattern. |
90 |
89 |
91 @param pattern pattern to be matched against |
90 @param pattern pattern to be matched against |
92 """ |
91 """ |
93 self.__pattern = pattern |
92 self.__pattern = pattern |
94 |
93 |
95 if "?" in pattern or "*" in pattern: |
94 if "?" in pattern or "*" in pattern: |
96 regexp = "^.*{0}.*$".format( |
95 regexp = "^.*{0}.*$".format( |
97 pattern |
96 pattern.replace(".", "\\.").replace("*", ".*").replace("?", ".") |
98 .replace(".", "\\.") |
|
99 .replace("*", ".*") |
|
100 .replace("?", ".") |
|
101 ) |
97 ) |
102 self.__regExp = re.compile(regexp, re.IGNORECASE) |
98 self.__regExp = re.compile(regexp, re.IGNORECASE) |
103 |
99 |
104 def pattern(self): |
100 def pattern(self): |
105 """ |
101 """ |
106 Public method to get the match pattern. |
102 Public method to get the match pattern. |
107 |
103 |
108 @return match pattern |
104 @return match pattern |
109 @rtype str |
105 @rtype str |
110 """ |
106 """ |
111 return self.__pattern |
107 return self.__pattern |
112 |
108 |
113 def match(self, host): |
109 def match(self, host): |
114 """ |
110 """ |
115 Public method to test the given string. |
111 Public method to test the given string. |
116 |
112 |
117 @param host host name to be matched |
113 @param host host name to be matched |
118 @type str |
114 @type str |
119 @return flag indicating a successful match |
115 @return flag indicating a successful match |
120 @rtype bool |
116 @rtype bool |
121 """ |
117 """ |
122 if self.__regExp is None: |
118 if self.__regExp is None: |
123 return self.__pattern in host |
119 return self.__pattern in host |
124 |
120 |
125 return self.__regExp.search(host) is not None |
121 return self.__regExp.search(host) is not None |
126 |
122 |
127 |
123 |
128 class EricNetworkProxyFactory(QNetworkProxyFactory): |
124 class EricNetworkProxyFactory(QNetworkProxyFactory): |
129 """ |
125 """ |
130 Class implementing a network proxy factory. |
126 Class implementing a network proxy factory. |
131 """ |
127 """ |
|
128 |
132 def __init__(self): |
129 def __init__(self): |
133 """ |
130 """ |
134 Constructor |
131 Constructor |
135 """ |
132 """ |
136 super().__init__() |
133 super().__init__() |
137 |
134 |
138 self.__hostnameMatchers = [] |
135 self.__hostnameMatchers = [] |
139 self.__exceptions = "" |
136 self.__exceptions = "" |
140 |
137 |
141 def __setExceptions(self, exceptions): |
138 def __setExceptions(self, exceptions): |
142 """ |
139 """ |
143 Private method to set the host name exceptions. |
140 Private method to set the host name exceptions. |
144 |
141 |
145 @param exceptions list of exceptions separated by ',' |
142 @param exceptions list of exceptions separated by ',' |
146 @type str |
143 @type str |
147 """ |
144 """ |
148 self.__hostnameMatchers = [] |
145 self.__hostnameMatchers = [] |
149 self.__exceptions = exceptions |
146 self.__exceptions = exceptions |
150 for exception in self.__exceptions.split(","): |
147 for exception in self.__exceptions.split(","): |
151 self.__hostnameMatchers.append(HostnameMatcher(exception.strip())) |
148 self.__hostnameMatchers.append(HostnameMatcher(exception.strip())) |
152 |
149 |
153 def queryProxy(self, query): |
150 def queryProxy(self, query): |
154 """ |
151 """ |
155 Public method to determine a proxy for a given query. |
152 Public method to determine a proxy for a given query. |
156 |
153 |
157 @param query reference to the query object (QNetworkProxyQuery) |
154 @param query reference to the query object (QNetworkProxyQuery) |
158 @return list of proxies in order of preference (list of QNetworkProxy) |
155 @return list of proxies in order of preference (list of QNetworkProxy) |
159 """ |
156 """ |
160 if ( |
157 if ( |
161 query.queryType() == QNetworkProxyQuery.QueryType.UrlRequest and |
158 query.queryType() == QNetworkProxyQuery.QueryType.UrlRequest |
162 query.protocolTag() in ["http", "https", "ftp"] |
159 and query.protocolTag() in ["http", "https", "ftp"] |
163 ): |
160 ): |
164 # use proxy at all ? |
161 # use proxy at all ? |
165 if not Preferences.getUI("UseProxy"): |
162 if not Preferences.getUI("UseProxy"): |
166 return [QNetworkProxy(QNetworkProxy.ProxyType.NoProxy)] |
163 return [QNetworkProxy(QNetworkProxy.ProxyType.NoProxy)] |
167 |
164 |
168 # test for exceptions |
165 # test for exceptions |
169 exceptions = Preferences.getUI("ProxyExceptions") |
166 exceptions = Preferences.getUI("ProxyExceptions") |
170 if exceptions != self.__exceptions: |
167 if exceptions != self.__exceptions: |
171 self.__setExceptions(exceptions) |
168 self.__setExceptions(exceptions) |
172 urlHost = query.url().host() |
169 urlHost = query.url().host() |
173 for matcher in self.__hostnameMatchers: |
170 for matcher in self.__hostnameMatchers: |
174 if matcher.match(urlHost): |
171 if matcher.match(urlHost): |
175 return [QNetworkProxy(QNetworkProxy.ProxyType.NoProxy)] |
172 return [QNetworkProxy(QNetworkProxy.ProxyType.NoProxy)] |
176 |
173 |
177 # determine proxy |
174 # determine proxy |
178 if Preferences.getUI("UseSystemProxy"): |
175 if Preferences.getUI("UseSystemProxy"): |
179 proxyList = QNetworkProxyFactory.systemProxyForQuery(query) |
176 proxyList = QNetworkProxyFactory.systemProxyForQuery(query) |
180 if ( |
177 if ( |
181 not Globals.isWindowsPlatform() and |
178 not Globals.isWindowsPlatform() |
182 len(proxyList) == 1 and |
179 and len(proxyList) == 1 |
183 proxyList[0].type() == QNetworkProxy.ProxyType.NoProxy |
180 and proxyList[0].type() == QNetworkProxy.ProxyType.NoProxy |
184 ): |
181 ): |
185 # try it the Python way |
182 # try it the Python way |
186 # scan the environment for variables named <scheme>_proxy |
183 # scan the environment for variables named <scheme>_proxy |
187 # scan over whole environment to make this case insensitive |
184 # scan over whole environment to make this case insensitive |
188 for name, value in os.environ.items(): |
185 for name, value in os.environ.items(): |
189 name = name.lower() |
186 name = name.lower() |
190 if ( |
187 if ( |
191 value and |
188 value |
192 name[-6:] == '_proxy' and |
189 and name[-6:] == "_proxy" |
193 name[:-6] == query.protocolTag().lower() |
190 and name[:-6] == query.protocolTag().lower() |
194 ): |
191 ): |
195 url = QUrl(value) |
192 url = QUrl(value) |
196 if url.scheme() in ["http", "https"]: |
193 if url.scheme() in ["http", "https"]: |
197 proxyType = QNetworkProxy.ProxyType.HttpProxy |
194 proxyType = QNetworkProxy.ProxyType.HttpProxy |
198 elif url.scheme() == "ftp": |
195 elif url.scheme() == "ftp": |
199 proxyType = ( |
196 proxyType = QNetworkProxy.ProxyType.FtpCachingProxy |
200 QNetworkProxy.ProxyType.FtpCachingProxy |
|
201 ) |
|
202 else: |
197 else: |
203 proxyType = QNetworkProxy.ProxyType.HttpProxy |
198 proxyType = QNetworkProxy.ProxyType.HttpProxy |
204 proxy = QNetworkProxy( |
199 proxy = QNetworkProxy( |
205 proxyType, url.host(), url.port(), |
200 proxyType, |
206 url.userName(), url.password()) |
201 url.host(), |
|
202 url.port(), |
|
203 url.userName(), |
|
204 url.password(), |
|
205 ) |
207 proxyList = [proxy] |
206 proxyList = [proxy] |
208 break |
207 break |
209 if proxyList: |
208 if proxyList: |
210 scheme = schemeFromProxyType(proxyList[0].type()) |
209 scheme = schemeFromProxyType(proxyList[0].type()) |
211 if scheme == "": |
210 if scheme == "": |
212 scheme = "Http" |
211 scheme = "Http" |
213 if scheme != "NoProxy": |
212 if scheme != "NoProxy": |
214 proxyList[0].setUser( |
213 proxyList[0].setUser( |
215 Preferences.getUI("ProxyUser/{0}".format(scheme))) |
214 Preferences.getUI("ProxyUser/{0}".format(scheme)) |
|
215 ) |
216 proxyList[0].setPassword( |
216 proxyList[0].setPassword( |
217 Preferences.getUI( |
217 Preferences.getUI("ProxyPassword/{0}".format(scheme)) |
218 "ProxyPassword/{0}".format(scheme))) |
218 ) |
219 return proxyList |
219 return proxyList |
220 else: |
220 else: |
221 return [QNetworkProxy(QNetworkProxy.ProxyType.NoProxy)] |
221 return [QNetworkProxy(QNetworkProxy.ProxyType.NoProxy)] |
222 else: |
222 else: |
223 if Preferences.getUI("UseHttpProxyForAll"): |
223 if Preferences.getUI("UseHttpProxyForAll"): |
227 host = Preferences.getUI("ProxyHost/{0}".format(protocolKey)) |
227 host = Preferences.getUI("ProxyHost/{0}".format(protocolKey)) |
228 if not host: |
228 if not host: |
229 EricMessageBox.critical( |
229 EricMessageBox.critical( |
230 None, |
230 None, |
231 QCoreApplication.translate( |
231 QCoreApplication.translate( |
232 "EricNetworkProxyFactory", |
232 "EricNetworkProxyFactory", "Proxy Configuration Error" |
233 "Proxy Configuration Error"), |
233 ), |
234 QCoreApplication.translate( |
234 QCoreApplication.translate( |
235 "EricNetworkProxyFactory", |
235 "EricNetworkProxyFactory", |
236 """Proxy usage was activated""" |
236 """Proxy usage was activated""" |
237 """ but no proxy host for protocol""" |
237 """ but no proxy host for protocol""" |
238 """ '{0}' configured.""").format(protocolKey)) |
238 """ '{0}' configured.""", |
239 return [ |
239 ).format(protocolKey), |
240 QNetworkProxy(QNetworkProxy.ProxyType.DefaultProxy) |
240 ) |
241 ] |
241 return [QNetworkProxy(QNetworkProxy.ProxyType.DefaultProxy)] |
242 else: |
242 else: |
243 if protocolKey in ["Http", "Https", "Ftp"]: |
243 if protocolKey in ["Http", "Https", "Ftp"]: |
244 if query.protocolTag() == "ftp": |
244 if query.protocolTag() == "ftp": |
245 proxyType = QNetworkProxy.ProxyType.FtpCachingProxy |
245 proxyType = QNetworkProxy.ProxyType.FtpCachingProxy |
246 else: |
246 else: |
247 proxyType = QNetworkProxy.ProxyType.HttpProxy |
247 proxyType = QNetworkProxy.ProxyType.HttpProxy |
248 proxy = QNetworkProxy( |
248 proxy = QNetworkProxy( |
249 proxyType, host, |
249 proxyType, |
|
250 host, |
250 Preferences.getUI("ProxyPort/" + protocolKey), |
251 Preferences.getUI("ProxyPort/" + protocolKey), |
251 Preferences.getUI("ProxyUser/" + protocolKey), |
252 Preferences.getUI("ProxyUser/" + protocolKey), |
252 Preferences.getUI("ProxyPassword/" + protocolKey)) |
253 Preferences.getUI("ProxyPassword/" + protocolKey), |
|
254 ) |
253 else: |
255 else: |
254 proxy = QNetworkProxy( |
256 proxy = QNetworkProxy(QNetworkProxy.ProxyType.DefaultProxy) |
255 QNetworkProxy.ProxyType.DefaultProxy) |
257 return [proxy, QNetworkProxy(QNetworkProxy.ProxyType.DefaultProxy)] |
256 return [ |
|
257 proxy, |
|
258 QNetworkProxy(QNetworkProxy.ProxyType.DefaultProxy) |
|
259 ] |
|
260 else: |
258 else: |
261 return [QNetworkProxy(QNetworkProxy.ProxyType.NoProxy)] |
259 return [QNetworkProxy(QNetworkProxy.ProxyType.NoProxy)] |