comparison mercurial/url.py @ 13820:65b89e80f892

url: use url.url in proxyhandler
author Brodie Rao <brodie@bitheap.org>
date Wed, 30 Mar 2011 20:01:44 -0700
parents d16894e29f91
children e574207e3bcd
comparison
equal deleted inserted replaced
13819:d16894e29f91 13820:65b89e80f892
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> 5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
6 # 6 #
7 # This software may be used and distributed according to the terms of the 7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version. 8 # GNU General Public License version 2 or any later version.
9 9
10 import urllib, urllib2, urlparse, httplib, os, socket, cStringIO 10 import urllib, urllib2, httplib, os, socket, cStringIO
11 import __builtin__ 11 import __builtin__
12 from i18n import _ 12 from i18n import _
13 import keepalive, util 13 import keepalive, util
14 14
15 class url(object): 15 class url(object):
405 if proxyurl: 405 if proxyurl:
406 # proxy can be proper url or host[:port] 406 # proxy can be proper url or host[:port]
407 if not (proxyurl.startswith('http:') or 407 if not (proxyurl.startswith('http:') or
408 proxyurl.startswith('https:')): 408 proxyurl.startswith('https:')):
409 proxyurl = 'http://' + proxyurl + '/' 409 proxyurl = 'http://' + proxyurl + '/'
410 snpqf = urlparse.urlsplit(proxyurl) 410 proxy = url(proxyurl)
411 proxyscheme, proxynetloc, proxypath, proxyquery, proxyfrag = snpqf 411 if not proxy.user:
412 hpup = netlocsplit(proxynetloc) 412 proxy.user = ui.config("http_proxy", "user")
413 413 proxy.passwd = ui.config("http_proxy", "passwd")
414 proxyhost, proxyport, proxyuser, proxypasswd = hpup
415 if not proxyuser:
416 proxyuser = ui.config("http_proxy", "user")
417 proxypasswd = ui.config("http_proxy", "passwd")
418 414
419 # see if we should use a proxy for this url 415 # see if we should use a proxy for this url
420 no_list = ["localhost", "127.0.0.1"] 416 no_list = ["localhost", "127.0.0.1"]
421 no_list.extend([p.lower() for 417 no_list.extend([p.lower() for
422 p in ui.configlist("http_proxy", "no")]) 418 p in ui.configlist("http_proxy", "no")])
427 if ui.configbool("http_proxy", "always"): 423 if ui.configbool("http_proxy", "always"):
428 self.no_list = [] 424 self.no_list = []
429 else: 425 else:
430 self.no_list = no_list 426 self.no_list = no_list
431 427
432 proxyurl = urlparse.urlunsplit(( 428 proxyurl = str(proxy)
433 proxyscheme, netlocunsplit(proxyhost, proxyport,
434 proxyuser, proxypasswd or ''),
435 proxypath, proxyquery, proxyfrag))
436 proxies = {'http': proxyurl, 'https': proxyurl} 429 proxies = {'http': proxyurl, 'https': proxyurl}
437 ui.debug('proxying through http://%s:%s\n' % 430 ui.debug('proxying through http://%s:%s\n' %
438 (proxyhost, proxyport)) 431 (proxy.host, proxy.port))
439 else: 432 else:
440 proxies = {} 433 proxies = {}
441 434
442 # urllib2 takes proxy values from the environment and those 435 # urllib2 takes proxy values from the environment and those
443 # will take precedence if found, so drop them 436 # will take precedence if found, so drop them
600 else: 593 else:
601 tunnel_host = req.get_selector() 594 tunnel_host = req.get_selector()
602 new_tunnel = False 595 new_tunnel = False
603 596
604 if new_tunnel or tunnel_host == req.get_full_url(): # has proxy 597 if new_tunnel or tunnel_host == req.get_full_url(): # has proxy
605 urlparts = urlparse.urlparse(tunnel_host) 598 u = url(tunnel_host)
606 if new_tunnel or urlparts[0] == 'https': # only use CONNECT for HTTPS 599 if new_tunnel or u.scheme == 'https': # only use CONNECT for HTTPS
607 realhostport = urlparts[1] 600 h.realhostport = ':'.join([u.host, (u.port or '443')])
608 if realhostport[-1] == ']' or ':' not in realhostport:
609 realhostport += ':443'
610
611 h.realhostport = realhostport
612 h.headers = req.headers.copy() 601 h.headers = req.headers.copy()
613 h.headers.update(handler.parent.addheaders) 602 h.headers.update(handler.parent.addheaders)
614 return 603 return
615 604
616 h.realhostport = None 605 h.realhostport = None