eric6/E5Network/E5NetworkProxyFactory.py

Tue, 10 Sep 2019 19:30:07 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 10 Sep 2019 19:30:07 +0200
changeset 7229
53054eb5b15a
parent 6942
2602857055c5
child 7253
50dbe65a1334
permissions
-rw-r--r--

Removed obsolete "from __future__ import ..." statements.

286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
1 # -*- coding: utf-8 -*-
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
2
6645
ad476851d7e0 Updated copyright for 2019.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6554
diff changeset
3 # Copyright (c) 2010 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
4 #
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
5
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
6 """
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
7 Module implementing a network proxy factory.
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
8 """
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
9
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2401
diff changeset
10
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
11 import os
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
12
4351
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
13 from PyQt5.QtCore import Qt, QUrl, QCoreApplication, QRegExp
3656
441956d8fce5 Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3178
diff changeset
14 from PyQt5.QtWidgets import QDialog
441956d8fce5 Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3178
diff changeset
15 from PyQt5.QtNetwork import QNetworkProxyFactory, QNetworkProxy, \
2990
583beaf0b4b8 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2401
diff changeset
16 QNetworkProxyQuery
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
17
537
72b32daeb8d6 Continued replacing QMessageBox methods with own methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 319
diff changeset
18 from E5Gui import E5MessageBox
72b32daeb8d6 Continued replacing QMessageBox methods with own methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 319
diff changeset
19
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
20 import Preferences
882
34b86be88bf0 Redid the native file dialog code to be future proof.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 791
diff changeset
21 import Globals
2080
4b33165dd55b Made the third set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1936
diff changeset
22 import Utilities
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
23
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 882
diff changeset
24
289
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
25 def schemeFromProxyType(proxyType):
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
26 """
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
27 Module function to determine the scheme name from the proxy type.
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
28
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
29 @param proxyType type of the proxy (QNetworkProxy.ProxyType)
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
30 @return scheme (string, one of Http, Https, Ftp)
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
31 """
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
32 scheme = ""
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
33 if proxyType == QNetworkProxy.HttpProxy:
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
34 scheme = "Http"
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
35 elif proxyType == QNetworkProxy.HttpCachingProxy:
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
36 scheme = "Https"
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
37 elif proxyType == QNetworkProxy.FtpCachingProxy:
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
38 scheme = "Ftp"
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
39 elif proxyType == QNetworkProxy.NoProxy:
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
40 scheme = "NoProxy"
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
41 return scheme
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
42
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 882
diff changeset
43
289
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
44 def proxyAuthenticationRequired(proxy, auth):
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
45 """
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
46 Module slot to handle a proxy authentication request.
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
47
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
48 @param proxy reference to the proxy object (QNetworkProxy)
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
49 @param auth reference to the authenticator object (QAuthenticator)
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
50 """
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3020
diff changeset
51 info = QCoreApplication.translate(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3020
diff changeset
52 "E5NetworkProxyFactory",
289
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
53 "<b>Connect to proxy '{0}' using:</b>")\
2080
4b33165dd55b Made the third set of Qt5 compatibility changes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1936
diff changeset
54 .format(Utilities.html_encode(proxy.hostName()))
289
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
55
2401
4f428de32b69 Continued implementing the delayed import.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
56 from UI.AuthenticationDialog import AuthenticationDialog
289
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
57 dlg = AuthenticationDialog(info, proxy.user(), True)
1936
536b88d73d53 Little enhancement to the network proxy factory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1932
diff changeset
58 dlg.setData(proxy.user(), proxy.password())
289
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
59 if dlg.exec_() == QDialog.Accepted:
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
60 username, password = dlg.getData()
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
61 auth.setUser(username)
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
62 auth.setPassword(password)
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
63 if dlg.shallSave():
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
64 scheme = schemeFromProxyType(proxy.type())
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
65 if scheme and scheme != "NoProxy":
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
66 Preferences.setUI("ProxyUser/{0}".format(scheme), username)
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
67 Preferences.setUI("ProxyPassword/{0}".format(scheme), password)
1936
536b88d73d53 Little enhancement to the network proxy factory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1932
diff changeset
68 proxy.setUser(username)
536b88d73d53 Little enhancement to the network proxy factory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1932
diff changeset
69 proxy.setPassword(password)
289
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
70
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 882
diff changeset
71
4351
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
72 class HostnameMatcher(object):
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
73 """
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
74 Class implementing a matcher for host names.
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
75 """
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
76 def __init__(self, pattern):
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
77 """
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
78 Constructor
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
79
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
80 @param pattern pattern to be matched against
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
81 @type str
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
82 """
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
83 self.__regExp = None
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
84 self.setPattern(pattern)
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
85
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
86 def setPattern(self, pattern):
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
87 """
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
88 Public method to set the match pattern.
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
89
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
90 @param pattern pattern to be matched against
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
91 """
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
92 self.__pattern = pattern
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
93
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
94 if "?" in pattern or "*" in pattern:
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
95 regexp = "^.*{0}.*$".format(
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
96 pattern
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
97 .replace(".", "\\.")
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
98 .replace("*", ".*")
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
99 .replace("?", ".")
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
100 )
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
101 self.__regExp = QRegExp(regexp, Qt.CaseInsensitive)
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
102
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
103 def pattern(self):
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
104 """
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
105 Public method to get the match pattern.
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
106
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
107 @return match pattern
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
108 @rtype str
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
109 """
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
110 return self.__pattern
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
111
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
112 def match(self, host):
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
113 """
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
114 Public method to test the given string.
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
115
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
116 @param host host name to be matched
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
117 @type str
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
118 @return flag indicating a successful match
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
119 @rtype bool
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
120 """
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
121 if self.__regExp is None:
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
122 return self.__pattern in host
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
123
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
124 return self.__regExp.indexIn(host) > -1
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
125
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
126
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
127 class E5NetworkProxyFactory(QNetworkProxyFactory):
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
128 """
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
129 Class implementing a network proxy factory.
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
130 """
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
131 def __init__(self):
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
132 """
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
133 Constructor
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
134 """
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2401
diff changeset
135 super(E5NetworkProxyFactory, self).__init__()
4351
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
136
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
137 self.__hostnameMatchers = []
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
138 self.__exceptions = ""
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
139
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
140 def __setExceptions(self, exceptions):
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
141 """
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
142 Private method to set the host name exceptions.
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
143
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
144 @param exceptions list of exceptions separated by ','
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
145 @type str
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
146 """
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
147 self.__hostnameMatchers = []
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
148 self.__exceptions = exceptions
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
149 for exception in self.__exceptions.split(","):
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
150 self.__hostnameMatchers.append(HostnameMatcher(exception.strip()))
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
151
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
152 def queryProxy(self, query):
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
153 """
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
154 Public method to determine a proxy for a given query.
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
155
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
156 @param query reference to the query object (QNetworkProxyQuery)
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
157 @return list of proxies in order of preference (list of QNetworkProxy)
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
158 """
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
159 if query.queryType() == QNetworkProxyQuery.UrlRequest and \
4351
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
160 query.protocolTag() in ["http", "https", "ftp"]:
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
161 # use proxy at all ?
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
162 if not Preferences.getUI("UseProxy"):
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
163 return [QNetworkProxy(QNetworkProxy.NoProxy)]
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
164
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
165 # test for exceptions
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
166 exceptions = Preferences.getUI("ProxyExceptions")
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
167 if exceptions != self.__exceptions:
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
168 self.__setExceptions(exceptions)
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
169 urlHost = query.url().host()
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
170 for matcher in self.__hostnameMatchers:
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
171 if matcher.match(urlHost):
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
172 return [QNetworkProxy(QNetworkProxy.NoProxy)]
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
173
ace015b19c0f Implemented the proxy exception support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4350
diff changeset
174 # determine proxy
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
175 if Preferences.getUI("UseSystemProxy"):
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
176 proxyList = QNetworkProxyFactory.systemProxyForQuery(query)
1363
d650915a903c Some changes for Mac computers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1131
diff changeset
177 if not Globals.isWindowsPlatform() and \
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
178 len(proxyList) == 1 and \
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
179 proxyList[0].type() == QNetworkProxy.NoProxy:
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
180 # try it the Python way
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
181 # scan the environment for variables named <scheme>_proxy
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
182 # scan over whole environment to make this case insensitive
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
183 for name, value in os.environ.items():
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
184 name = name.lower()
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
185 if value and name[-6:] == '_proxy' and \
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
186 name[:-6] == query.protocolTag().lower():
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
187 url = QUrl(value)
6554
5f1f2fcd9dee UserInterface, NetworkManager, E5NetworkProxyFactory: some code changes to (hopefully) fix a network proxy related issue.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
188 if url.scheme() in ["http", "https"]:
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
189 proxyType = QNetworkProxy.HttpProxy
289
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
190 elif url.scheme() == "ftp":
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
191 proxyType = QNetworkProxy.FtpCachingProxy
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
192 else:
289
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
193 proxyType = QNetworkProxy.HttpProxy
2990
583beaf0b4b8 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2401
diff changeset
194 proxy = QNetworkProxy(
583beaf0b4b8 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2401
diff changeset
195 proxyType, url.host(), url.port(),
583beaf0b4b8 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2401
diff changeset
196 url.userName(), url.password())
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
197 proxyList = [proxy]
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
198 break
317
f6b0163a21be Fixed an issue in the network proxy factory class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 289
diff changeset
199 if proxyList:
f6b0163a21be Fixed an issue in the network proxy factory class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 289
diff changeset
200 scheme = schemeFromProxyType(proxyList[0].type())
f6b0163a21be Fixed an issue in the network proxy factory class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 289
diff changeset
201 if scheme == "":
f6b0163a21be Fixed an issue in the network proxy factory class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 289
diff changeset
202 scheme = "Http"
f6b0163a21be Fixed an issue in the network proxy factory class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 289
diff changeset
203 if scheme != "NoProxy":
f6b0163a21be Fixed an issue in the network proxy factory class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 289
diff changeset
204 proxyList[0].setUser(
f6b0163a21be Fixed an issue in the network proxy factory class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 289
diff changeset
205 Preferences.getUI("ProxyUser/{0}".format(scheme)))
f6b0163a21be Fixed an issue in the network proxy factory class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 289
diff changeset
206 proxyList[0].setPassword(
2990
583beaf0b4b8 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2401
diff changeset
207 Preferences.getUI(
583beaf0b4b8 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2401
diff changeset
208 "ProxyPassword/{0}".format(scheme)))
319
09f456ec6fcf Fixed a few issues in the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 317
diff changeset
209 return proxyList
09f456ec6fcf Fixed a few issues in the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 317
diff changeset
210 else:
09f456ec6fcf Fixed a few issues in the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 317
diff changeset
211 return [QNetworkProxy(QNetworkProxy.NoProxy)]
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
212 else:
289
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
213 if Preferences.getUI("UseHttpProxyForAll"):
1058
fa1c0fa31041 Fixed an issue in the web browser related to selecting the correct proxy type.
detlev@die-offenbachs.de
parents: 945
diff changeset
214 protocolKey = "Http"
289
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
215 else:
1058
fa1c0fa31041 Fixed an issue in the web browser related to selecting the correct proxy type.
detlev@die-offenbachs.de
parents: 945
diff changeset
216 protocolKey = query.protocolTag().capitalize()
fa1c0fa31041 Fixed an issue in the web browser related to selecting the correct proxy type.
detlev@die-offenbachs.de
parents: 945
diff changeset
217 host = Preferences.getUI("ProxyHost/{0}".format(protocolKey))
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
218 if not host:
3020
542e97d4ecb3 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2990
diff changeset
219 E5MessageBox.critical(
542e97d4ecb3 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2990
diff changeset
220 None,
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3020
diff changeset
221 QCoreApplication.translate(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3020
diff changeset
222 "E5NetworkProxyFactory",
289
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
223 "Proxy Configuration Error"),
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3020
diff changeset
224 QCoreApplication.translate(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3020
diff changeset
225 "E5NetworkProxyFactory",
289
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
226 """Proxy usage was activated"""
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
227 """ but no proxy host for protocol"""
1058
fa1c0fa31041 Fixed an issue in the web browser related to selecting the correct proxy type.
detlev@die-offenbachs.de
parents: 945
diff changeset
228 """ '{0}' configured.""").format(protocolKey))
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
229 return [QNetworkProxy(QNetworkProxy.DefaultProxy)]
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
230 else:
1058
fa1c0fa31041 Fixed an issue in the web browser related to selecting the correct proxy type.
detlev@die-offenbachs.de
parents: 945
diff changeset
231 if protocolKey in ["Http", "Https", "Ftp"]:
fa1c0fa31041 Fixed an issue in the web browser related to selecting the correct proxy type.
detlev@die-offenbachs.de
parents: 945
diff changeset
232 if query.protocolTag() == "ftp":
fa1c0fa31041 Fixed an issue in the web browser related to selecting the correct proxy type.
detlev@die-offenbachs.de
parents: 945
diff changeset
233 proxyType = QNetworkProxy.FtpCachingProxy
fa1c0fa31041 Fixed an issue in the web browser related to selecting the correct proxy type.
detlev@die-offenbachs.de
parents: 945
diff changeset
234 else:
fa1c0fa31041 Fixed an issue in the web browser related to selecting the correct proxy type.
detlev@die-offenbachs.de
parents: 945
diff changeset
235 proxyType = QNetworkProxy.HttpProxy
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3020
diff changeset
236 proxy = QNetworkProxy(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3020
diff changeset
237 proxyType, host,
1058
fa1c0fa31041 Fixed an issue in the web browser related to selecting the correct proxy type.
detlev@die-offenbachs.de
parents: 945
diff changeset
238 Preferences.getUI("ProxyPort/" + protocolKey),
fa1c0fa31041 Fixed an issue in the web browser related to selecting the correct proxy type.
detlev@die-offenbachs.de
parents: 945
diff changeset
239 Preferences.getUI("ProxyUser/" + protocolKey),
fa1c0fa31041 Fixed an issue in the web browser related to selecting the correct proxy type.
detlev@die-offenbachs.de
parents: 945
diff changeset
240 Preferences.getUI("ProxyPassword/" + protocolKey))
289
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
241 else:
baf4c1354c6a Extended the proxy handling to HTTPS and FTP.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 286
diff changeset
242 proxy = QNetworkProxy(QNetworkProxy.DefaultProxy)
286
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
243 return [proxy, QNetworkProxy(QNetworkProxy.DefaultProxy)]
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
244 else:
652f5159f1c3 Prepared to have individual proxies per scheme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 283
diff changeset
245 return [QNetworkProxy(QNetworkProxy.NoProxy)]

eric ide

mercurial