##// END OF EJS Templates
httprepo: factor out proxy handling
Benoit Boissinot -
r7269:95a53961 default
parent child Browse files
Show More
@@ -39,6 +39,66 b' class passwordmgr(urllib2.HTTPPasswordMg'
39 self.add_password(realm, authuri, user, passwd)
39 self.add_password(realm, authuri, user, passwd)
40 return (user, passwd)
40 return (user, passwd)
41
41
42 class proxyhandler(urllib2.ProxyHandler):
43 def __init__(self, ui):
44 proxyurl = ui.config("http_proxy", "host") or os.getenv('http_proxy')
45 # XXX proxyauthinfo = None
46
47 if proxyurl:
48 # proxy can be proper url or host[:port]
49 if not (proxyurl.startswith('http:') or
50 proxyurl.startswith('https:')):
51 proxyurl = 'http://' + proxyurl + '/'
52 snpqf = urlparse.urlsplit(proxyurl)
53 proxyscheme, proxynetloc, proxypath, proxyquery, proxyfrag = snpqf
54 hpup = netlocsplit(proxynetloc)
55
56 proxyhost, proxyport, proxyuser, proxypasswd = hpup
57 if not proxyuser:
58 proxyuser = ui.config("http_proxy", "user")
59 proxypasswd = ui.config("http_proxy", "passwd")
60
61 # see if we should use a proxy for this url
62 no_list = [ "localhost", "127.0.0.1" ]
63 no_list.extend([p.lower() for
64 p in ui.configlist("http_proxy", "no")])
65 no_list.extend([p.strip().lower() for
66 p in os.getenv("no_proxy", '').split(',')
67 if p.strip()])
68 # "http_proxy.always" config is for running tests on localhost
69 if ui.configbool("http_proxy", "always"):
70 self.no_list = []
71 else:
72 self.no_list = no_list
73
74 proxyurl = urlparse.urlunsplit((
75 proxyscheme, netlocunsplit(proxyhost, proxyport,
76 proxyuser, proxypasswd or ''),
77 proxypath, proxyquery, proxyfrag))
78 proxies = {'http': proxyurl, 'https': proxyurl}
79 ui.debug(_('proxying through http://%s:%s\n') %
80 (proxyhost, proxyport))
81 else:
82 proxies = {}
83
84 # urllib2 takes proxy values from the environment and those
85 # will take precedence if found, so drop them
86 for env in ["HTTP_PROXY", "http_proxy", "no_proxy"]:
87 try:
88 if env in os.environ:
89 del os.environ[env]
90 except OSError:
91 pass
92
93 urllib2.ProxyHandler.__init__(self, proxies)
94 self.ui = ui
95
96 def proxy_open(self, req, proxy, type):
97 host = req.get_host().split(':')[0]
98 if host in self.no_list:
99 return None
100 return urllib2.ProxyHandler.proxy_open(self, req, proxy, type)
101
42 def netlocsplit(netloc):
102 def netlocsplit(netloc):
43 '''split [user[:passwd]@]host[:port] into 4-tuple.'''
103 '''split [user[:passwd]@]host[:port] into 4-tuple.'''
44
104
@@ -200,57 +260,11 b' class httprepository(repo.repository):'
200 self.ui = ui
260 self.ui = ui
201 self.ui.debug(_('using %s\n') % self._url)
261 self.ui.debug(_('using %s\n') % self._url)
202
262
203 proxyurl = ui.config("http_proxy", "host") or os.getenv('http_proxy')
204 # XXX proxyauthinfo = None
205 handlers = [httphandler()]
263 handlers = [httphandler()]
206 if has_https:
264 if has_https:
207 handlers.append(httpshandler())
265 handlers.append(httpshandler())
208
266
209 if proxyurl:
267 handlers.append(proxyhandler(ui))
210 # proxy can be proper url or host[:port]
211 if not (proxyurl.startswith('http:') or
212 proxyurl.startswith('https:')):
213 proxyurl = 'http://' + proxyurl + '/'
214 snpqf = urlparse.urlsplit(proxyurl)
215 proxyscheme, proxynetloc, proxypath, proxyquery, proxyfrag = snpqf
216 hpup = netlocsplit(proxynetloc)
217
218 proxyhost, proxyport, proxyuser, proxypasswd = hpup
219 if not proxyuser:
220 proxyuser = ui.config("http_proxy", "user")
221 proxypasswd = ui.config("http_proxy", "passwd")
222
223 # see if we should use a proxy for this url
224 no_list = [ "localhost", "127.0.0.1" ]
225 no_list.extend([p.lower() for
226 p in ui.configlist("http_proxy", "no")])
227 no_list.extend([p.strip().lower() for
228 p in os.getenv("no_proxy", '').split(',')
229 if p.strip()])
230 # "http_proxy.always" config is for running tests on localhost
231 if (not ui.configbool("http_proxy", "always") and
232 host.lower() in no_list):
233 # avoid auto-detection of proxy settings by appending
234 # a ProxyHandler with no proxies defined.
235 handlers.append(urllib2.ProxyHandler({}))
236 ui.debug(_('disabling proxy for %s\n') % host)
237 else:
238 proxyurl = urlparse.urlunsplit((
239 proxyscheme, netlocunsplit(proxyhost, proxyport,
240 proxyuser, proxypasswd or ''),
241 proxypath, proxyquery, proxyfrag))
242 handlers.append(urllib2.ProxyHandler({scheme: proxyurl}))
243 ui.debug(_('proxying through http://%s:%s\n') %
244 (proxyhost, proxyport))
245
246 # urllib2 takes proxy values from the environment and those
247 # will take precedence if found, so drop them
248 for env in ["HTTP_PROXY", "http_proxy", "no_proxy"]:
249 try:
250 if env in os.environ:
251 del os.environ[env]
252 except OSError:
253 pass
254
268
255 passmgr = passwordmgr(ui)
269 passmgr = passwordmgr(ui)
256 if user:
270 if user:
@@ -38,5 +38,8 b' http_proxy=http://user:passwd@localhost:'
38 echo %% bad host:port for proxy
38 echo %% bad host:port for proxy
39 http_proxy=localhost:$HGPORT2 hg clone --config http_proxy.always=True http://localhost:$HGPORT/ f
39 http_proxy=localhost:$HGPORT2 hg clone --config http_proxy.always=True http://localhost:$HGPORT/ f
40
40
41 echo %% do not use the proxy if it is in the no list
42 http_proxy=localhost:$HGPORT1 hg clone --config http_proxy.no=localhost http://localhost:$HGPORT/ g
43
41 cat proxy.log | sed -e 's/^.*\] /XXX /'
44 cat proxy.log | sed -e 's/^.*\] /XXX /'
42 exit 0
45 exit 0
@@ -49,6 +49,14 b' updating working directory'
49 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
49 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
50 %% bad host:port for proxy
50 %% bad host:port for proxy
51 abort: error: Connection refused
51 abort: error: Connection refused
52 %% do not use the proxy if it is in the no list
53 requesting all changes
54 adding changesets
55 adding manifests
56 adding file changes
57 added 1 changesets with 1 changes to 1 files
58 updating working directory
59 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
52 XXX "GET http://localhost:20059/?pairs=0000000000000000000000000000000000000000-0000000000000000000000000000000000000000&cmd=between HTTP/1.1" - -
60 XXX "GET http://localhost:20059/?pairs=0000000000000000000000000000000000000000-0000000000000000000000000000000000000000&cmd=between HTTP/1.1" - -
53 XXX "GET http://localhost:20059/?cmd=capabilities HTTP/1.1" - -
61 XXX "GET http://localhost:20059/?cmd=capabilities HTTP/1.1" - -
54 XXX "GET http://localhost:20059/?cmd=stream_out HTTP/1.1" - -
62 XXX "GET http://localhost:20059/?cmd=stream_out HTTP/1.1" - -
General Comments 0
You need to be logged in to leave comments. Login now