eric6/E5Network/E5NetworkProxyFactory.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7923
91e843545d9a
child 8143
2c730d5fd177
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

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

eric ide

mercurial