##// END OF EJS Templates
http: fix many problems with url parsing and auth. added proxy test....
Vadim Gelfer -
r2337:3f24bc5d default
parent child Browse files
Show More
@@ -0,0 +1,30 b''
1 #!/bin/sh
2
3 hg init a
4 cd a
5 echo a > a
6 hg ci -Ama -d '1123456789 0'
7 hg serve -p 20059 -d --pid-file=hg.pid
8
9 cd ..
10 ("$TESTDIR/tinyproxy.py" 20060 localhost >/dev/null 2>&1 </dev/null &
11 echo $! > proxy.pid)
12 sleep 2
13
14 echo %% url for proxy
15 http_proxy=http://localhost:20060/ hg --config http_proxy.always=True clone http://localhost:20059/ b
16
17 echo %% host:port for proxy
18 http_proxy=localhost:20060 hg clone --config http_proxy.always=True http://localhost:20059/ c
19
20 echo %% proxy url with user name and password
21 http_proxy=http://user:passwd@localhost:20060 hg clone --config http_proxy.always=True http://localhost:20059/ d
22
23 echo %% url with user name and password
24 http_proxy=http://user:passwd@localhost:20060 hg clone --config http_proxy.always=True http://user:passwd@localhost:20059/ e
25
26 echo %% bad host:port for proxy
27 http_proxy=localhost:20061 hg clone --config http_proxy.always=True http://localhost:20059/ f
28
29 kill $(cat proxy.pid a/hg.pid)
30 exit 0
@@ -0,0 +1,31 b''
1 adding a
2 %% url for proxy
3 requesting all changes
4 adding changesets
5 adding manifests
6 adding file changes
7 added 1 changesets with 1 changes to 1 files
8 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
9 %% host:port for proxy
10 requesting all changes
11 adding changesets
12 adding manifests
13 adding file changes
14 added 1 changesets with 1 changes to 1 files
15 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
16 %% proxy url with user name and password
17 requesting all changes
18 adding changesets
19 adding manifests
20 adding file changes
21 added 1 changesets with 1 changes to 1 files
22 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
23 %% url with user name and password
24 requesting all changes
25 adding changesets
26 adding manifests
27 adding file changes
28 added 1 changesets with 1 changes to 1 files
29 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
30 %% bad host:port for proxy
31 abort: error: Connection refused
@@ -0,0 +1,132 b''
1 #!/usr/bin/env python
2
3 __doc__ = """Tiny HTTP Proxy.
4
5 This module implements GET, HEAD, POST, PUT and DELETE methods
6 on BaseHTTPServer, and behaves as an HTTP proxy. The CONNECT
7 method is also implemented experimentally, but has not been
8 tested yet.
9
10 Any help will be greatly appreciated. SUZUKI Hisao
11 """
12
13 __version__ = "0.2.1"
14
15 import BaseHTTPServer, select, socket, SocketServer, urlparse
16
17 class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler):
18 __base = BaseHTTPServer.BaseHTTPRequestHandler
19 __base_handle = __base.handle
20
21 server_version = "TinyHTTPProxy/" + __version__
22 rbufsize = 0 # self.rfile Be unbuffered
23
24 def handle(self):
25 (ip, port) = self.client_address
26 if hasattr(self, 'allowed_clients') and ip not in self.allowed_clients:
27 self.raw_requestline = self.rfile.readline()
28 if self.parse_request(): self.send_error(403)
29 else:
30 self.__base_handle()
31
32 def _connect_to(self, netloc, soc):
33 i = netloc.find(':')
34 if i >= 0:
35 host_port = netloc[:i], int(netloc[i+1:])
36 else:
37 host_port = netloc, 80
38 print "\t" "connect to %s:%d" % host_port
39 try: soc.connect(host_port)
40 except socket.error, arg:
41 try: msg = arg[1]
42 except: msg = arg
43 self.send_error(404, msg)
44 return 0
45 return 1
46
47 def do_CONNECT(self):
48 soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
49 try:
50 if self._connect_to(self.path, soc):
51 self.log_request(200)
52 self.wfile.write(self.protocol_version +
53 " 200 Connection established\r\n")
54 self.wfile.write("Proxy-agent: %s\r\n" % self.version_string())
55 self.wfile.write("\r\n")
56 self._read_write(soc, 300)
57 finally:
58 print "\t" "bye"
59 soc.close()
60 self.connection.close()
61
62 def do_GET(self):
63 (scm, netloc, path, params, query, fragment) = urlparse.urlparse(
64 self.path, 'http')
65 if scm != 'http' or fragment or not netloc:
66 self.send_error(400, "bad url %s" % self.path)
67 return
68 soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
69 try:
70 if self._connect_to(netloc, soc):
71 self.log_request()
72 soc.send("%s %s %s\r\n" % (
73 self.command,
74 urlparse.urlunparse(('', '', path, params, query, '')),
75 self.request_version))
76 self.headers['Connection'] = 'close'
77 del self.headers['Proxy-Connection']
78 for key_val in self.headers.items():
79 soc.send("%s: %s\r\n" % key_val)
80 soc.send("\r\n")
81 self._read_write(soc)
82 finally:
83 print "\t" "bye"
84 soc.close()
85 self.connection.close()
86
87 def _read_write(self, soc, max_idling=20):
88 iw = [self.connection, soc]
89 ow = []
90 count = 0
91 while 1:
92 count += 1
93 (ins, _, exs) = select.select(iw, ow, iw, 3)
94 if exs: break
95 if ins:
96 for i in ins:
97 if i is soc:
98 out = self.connection
99 else:
100 out = soc
101 data = i.recv(8192)
102 if data:
103 out.send(data)
104 count = 0
105 else:
106 print "\t" "idle", count
107 if count == max_idling: break
108
109 do_HEAD = do_GET
110 do_POST = do_GET
111 do_PUT = do_GET
112 do_DELETE=do_GET
113
114 class ThreadingHTTPServer (SocketServer.ThreadingMixIn,
115 BaseHTTPServer.HTTPServer): pass
116
117 if __name__ == '__main__':
118 from sys import argv
119 if argv[1:] and argv[1] in ('-h', '--help'):
120 print argv[0], "[port [allowed_client_name ...]]"
121 else:
122 if argv[2:]:
123 allowed = []
124 for name in argv[2:]:
125 client = socket.gethostbyname(name)
126 allowed.append(client)
127 print "Accept: %s (%s)" % (client, name)
128 ProxyHandler.allowed_clients = allowed
129 del argv[2:]
130 else:
131 print "Any clients will be served..."
132 BaseHTTPServer.test(ProxyHandler, ThreadingHTTPServer)
@@ -22,6 +22,9 b' class passwordmgr(urllib2.HTTPPasswordMg'
22 if authinfo != (None, None):
22 if authinfo != (None, None):
23 return authinfo
23 return authinfo
24
24
25 if not ui.interactive:
26 raise util.Abort(_('http authorization required'))
27
25 self.ui.write(_("http authorization required\n"))
28 self.ui.write(_("http authorization required\n"))
26 self.ui.status(_("realm: %s\n") % realm)
29 self.ui.status(_("realm: %s\n") % realm)
27 user = self.ui.prompt(_("user:"), default=None)
30 user = self.ui.prompt(_("user:"), default=None)
@@ -30,37 +33,95 b' class passwordmgr(urllib2.HTTPPasswordMg'
30 self.add_password(realm, authuri, user, passwd)
33 self.add_password(realm, authuri, user, passwd)
31 return (user, passwd)
34 return (user, passwd)
32
35
36 def netlocsplit(netloc):
37 '''split [user[:passwd]@]host[:port] into 4-tuple.'''
38
39 a = netloc.find('@')
40 if a == -1:
41 user, passwd = None, None
42 else:
43 userpass, netloc = netloc[:a], netloc[a+1:]
44 c = userpass.find(':')
45 if c == -1:
46 user, passwd = urllib.unquote(userpass), None
47 else:
48 user = urllib.unquote(userpass[:c])
49 passwd = urllib.unquote(userpass[c+1:])
50 c = netloc.find(':')
51 if c == -1:
52 host, port = netloc, None
53 else:
54 host, port = netloc[:c], netloc[c+1:]
55 return host, port, user, passwd
56
57 def netlocunsplit(host, port, user=None, passwd=None):
58 '''turn host, port, user, passwd into [user[:passwd]@]host[:port].'''
59 if port:
60 hostport = host + ':' + port
61 else:
62 hostport = host
63 if user:
64 if passwd:
65 userpass = urllib.quote(user) + ':' + urllib.quote(passwd)
66 else:
67 userpass = urllib.quote(user)
68 return userpass + '@' + hostport
69 return hostport
70
33 class httprepository(remoterepository):
71 class httprepository(remoterepository):
34 def __init__(self, ui, path):
72 def __init__(self, ui, path):
35 # fix missing / after hostname
73 scheme, netloc, urlpath, query, frag = urlparse.urlsplit(path)
36 s = urlparse.urlsplit(path)
74 if query or frag:
37 partial = s[2]
75 raise util.Abort(_('unsupported URL component: "%s"') %
38 if not partial: partial = "/"
76 (query or frag))
39 self.url = urlparse.urlunsplit((s[0], s[1], partial, '', ''))
77 if not urlpath: urlpath = '/'
78 host, port, user, passwd = netlocsplit(netloc)
79
80 # urllib cannot handle URLs with embedded user or passwd
81 self.url = urlparse.urlunsplit((scheme, netlocunsplit(host, port),
82 urlpath, '', ''))
40 self.ui = ui
83 self.ui = ui
84
85 proxyurl = ui.config("http_proxy", "host") or os.getenv('http_proxy')
86 proxyauthinfo = None
87 handler = urllib2.BaseHandler()
88
89 if proxyurl:
90 # proxy can be proper url or host[:port]
91 if not (proxyurl.startswith('http:') or
92 proxyurl.startswith('https:')):
93 proxyurl = 'http://' + proxyurl + '/'
94 snpqf = urlparse.urlsplit(proxyurl)
95 proxyscheme, proxynetloc, proxypath, proxyquery, proxyfrag = snpqf
96 hpup = netlocsplit(proxynetloc)
97
98 proxyhost, proxyport, proxyuser, proxypasswd = hpup
99 if not proxyuser:
100 proxyuser = ui.config("http_proxy", "user")
101 proxypasswd = ui.config("http_proxy", "passwd")
102
103 # see if we should use a proxy for this url
41 no_list = [ "localhost", "127.0.0.1" ]
104 no_list = [ "localhost", "127.0.0.1" ]
42 host = ui.config("http_proxy", "host")
105 no_list.extend([p.strip().lower() for
43 if host is None:
106 p in ui.config("http_proxy", "no", '').split(',')
44 host = os.environ.get("http_proxy")
107 if p.strip()])
45 if host and host.startswith('http://'):
108 no_list.extend([p.strip().lower() for
46 host = host[7:]
109 p in os.getenv("no_proxy", '').split(',')
47 user = ui.config("http_proxy", "user")
110 if p.strip()])
48 passwd = ui.config("http_proxy", "passwd")
111 # "http_proxy.always" config is for running tests on localhost
49 no = ui.config("http_proxy", "no")
112 if (not ui.configbool("http_proxy", "always") and
50 if no is None:
113 host.lower() in no_list):
51 no = os.environ.get("no_proxy")
114 ui.debug(_('disabling proxy for %s\n') % host)
52 if no:
115 else:
53 no_list = no_list + no.split(",")
116 proxyurl = urlparse.urlunsplit((
117 proxyscheme, netlocunsplit(proxyhost, proxyport,
118 proxyuser, proxypasswd or ''),
119 proxypath, proxyquery, proxyfrag))
120 handler = urllib2.ProxyHandler({scheme: proxyurl})
121 ui.debug(_('proxying through %s\n') % proxyurl)
54
122
55 no_proxy = 0
123 # urllib2 takes proxy values from the environment and those
56 for h in no_list:
124 # will take precedence if found, so drop them
57 if (path.startswith("http://" + h + "/") or
58 path.startswith("http://" + h + ":") or
59 path == "http://" + h):
60 no_proxy = 1
61
62 # Note: urllib2 takes proxy values from the environment and those will
63 # take precedence
64 for env in ["HTTP_PROXY", "http_proxy", "no_proxy"]:
125 for env in ["HTTP_PROXY", "http_proxy", "no_proxy"]:
65 try:
126 try:
66 if os.environ.has_key(env):
127 if os.environ.has_key(env):
@@ -68,24 +129,15 b' class httprepository(remoterepository):'
68 except OSError:
129 except OSError:
69 pass
130 pass
70
131
71 proxy_handler = urllib2.BaseHandler()
132 passmgr = passwordmgr(ui)
72 if host and not no_proxy:
133 if user:
73 proxy_handler = urllib2.ProxyHandler({"http" : "http://" + host})
134 ui.debug(_('will use user %s for http auth\n') % user)
135 passmgr.add_password(None, host, user, passwd or '')
74
136
75 proxyauthinfo = None
76 if user and passwd:
77 passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
78 passmgr.add_password(None, host, user, passwd)
79 proxyauthinfo = urllib2.ProxyBasicAuthHandler(passmgr)
80
81 if ui.interactive:
82 passmgr = passwordmgr(ui)
83 opener = urllib2.build_opener(
137 opener = urllib2.build_opener(
84 proxy_handler, proxyauthinfo,
138 handler,
85 urllib2.HTTPBasicAuthHandler(passmgr),
139 urllib2.HTTPBasicAuthHandler(passmgr),
86 urllib2.HTTPDigestAuthHandler(passmgr))
140 urllib2.HTTPDigestAuthHandler(passmgr))
87 else:
88 opener = urllib2.build_opener(proxy_handler, proxyauthinfo)
89
141
90 # 1.0 here is the _protocol_ version
142 # 1.0 here is the _protocol_ version
91 opener.addheaders = [('User-agent', 'mercurial/proto-1.0')]
143 opener.addheaders = [('User-agent', 'mercurial/proto-1.0')]
General Comments 0
You need to be logged in to leave comments. Login now