E5Network/E5NetworkProxyFactory.py

changeset 4351
ace015b19c0f
parent 4350
686c79ffbcff
child 4631
5c1a96925da4
equal deleted inserted replaced
4350:686c79ffbcff 4351:ace015b19c0f
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 import os 12 import os
13 13
14 from PyQt5.QtCore import QUrl, QCoreApplication 14 from PyQt5.QtCore import Qt, QUrl, QCoreApplication, QRegExp
15 from PyQt5.QtWidgets import QDialog 15 from PyQt5.QtWidgets import QDialog
16 from PyQt5.QtNetwork import QNetworkProxyFactory, QNetworkProxy, \ 16 from PyQt5.QtNetwork import QNetworkProxyFactory, QNetworkProxy, \
17 QNetworkProxyQuery 17 QNetworkProxyQuery
18 18
19 from E5Gui import E5MessageBox 19 from E5Gui import E5MessageBox
68 Preferences.setUI("ProxyPassword/{0}".format(scheme), password) 68 Preferences.setUI("ProxyPassword/{0}".format(scheme), password)
69 proxy.setUser(username) 69 proxy.setUser(username)
70 proxy.setPassword(password) 70 proxy.setPassword(password)
71 71
72 72
73 class HostnameMatcher(object):
74 """
75 Class implementing a matcher for host names.
76 """
77 def __init__(self, pattern):
78 """
79 Constructor
80
81 @param pattern pattern to be matched against
82 @type str
83 """
84 self.__regExp = None
85 self.setPattern(pattern)
86
87 def setPattern(self, pattern):
88 """
89 Public method to set the match pattern.
90
91 @param pattern pattern to be matched against
92 """
93 self.__pattern = pattern
94
95 if "?" in pattern or "*" in pattern:
96 regexp = "^.*{0}.*$".format(
97 pattern
98 .replace(".", "\\.")
99 .replace("*", ".*")
100 .replace("?", ".")
101 )
102 self.__regExp = QRegExp(regexp, Qt.CaseInsensitive)
103
104 def pattern(self):
105 """
106 Public method to get the match pattern.
107
108 @return match pattern
109 @rtype str
110 """
111 return self.__pattern
112
113 def match(self, host):
114 """
115 Public method to test the given string.
116
117 @param host host name to be matched
118 @type str
119 @return flag indicating a successful match
120 @rtype bool
121 """
122 if self.__regExp is None:
123 return self.__pattern in host
124
125 return self.__regExp.indexIn(host) > -1
126
127
73 class E5NetworkProxyFactory(QNetworkProxyFactory): 128 class E5NetworkProxyFactory(QNetworkProxyFactory):
74 """ 129 """
75 Class implementing a network proxy factory. 130 Class implementing a network proxy factory.
76 """ 131 """
77 def __init__(self): 132 def __init__(self):
78 """ 133 """
79 Constructor 134 Constructor
80 """ 135 """
81 super(E5NetworkProxyFactory, self).__init__() 136 super(E5NetworkProxyFactory, self).__init__()
137
138 self.__hostnameMatchers = []
139 self.__exceptions = ""
140
141 def __setExceptions(self, exceptions):
142 """
143 Private method to set the host name exceptions.
144
145 @param exceptions list of exceptions separated by ','
146 @type str
147 """
148 self.__hostnameMatchers = []
149 self.__exceptions = exceptions
150 for exception in self.__exceptions.split(","):
151 self.__hostnameMatchers.append(HostnameMatcher(exception.strip()))
82 152
83 def queryProxy(self, query): 153 def queryProxy(self, query):
84 """ 154 """
85 Public method to determine a proxy for a given query. 155 Public method to determine a proxy for a given query.
86 156
87 @param query reference to the query object (QNetworkProxyQuery) 157 @param query reference to the query object (QNetworkProxyQuery)
88 @return list of proxies in order of preference (list of QNetworkProxy) 158 @return list of proxies in order of preference (list of QNetworkProxy)
89 """ 159 """
90 # TODO: implement proxy exceptions
91 if query.queryType() == QNetworkProxyQuery.UrlRequest and \ 160 if query.queryType() == QNetworkProxyQuery.UrlRequest and \
92 query.protocolTag() in ["http", "https", "ftp"] and \ 161 query.protocolTag() in ["http", "https", "ftp"]:
93 Preferences.getUI("UseProxy"): 162 # use proxy at all ?
163 if not Preferences.getUI("UseProxy"):
164 return [QNetworkProxy(QNetworkProxy.NoProxy)]
165
166 # test for exceptions
167 exceptions = Preferences.getUI("ProxyExceptions")
168 if exceptions != self.__exceptions:
169 self.__setExceptions(exceptions)
170 urlHost = query.url().host()
171 for matcher in self.__hostnameMatchers:
172 if matcher.match(urlHost):
173 return [QNetworkProxy(QNetworkProxy.NoProxy)]
174
175 # determine proxy
94 if Preferences.getUI("UseSystemProxy"): 176 if Preferences.getUI("UseSystemProxy"):
95 proxyList = QNetworkProxyFactory.systemProxyForQuery(query) 177 proxyList = QNetworkProxyFactory.systemProxyForQuery(query)
96 if not Globals.isWindowsPlatform() and \ 178 if not Globals.isWindowsPlatform() and \
97 len(proxyList) == 1 and \ 179 len(proxyList) == 1 and \
98 proxyList[0].type() == QNetworkProxy.NoProxy: 180 proxyList[0].type() == QNetworkProxy.NoProxy:

eric ide

mercurial