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) |
@@ -1,174 +1,226 b'' | |||
|
1 | 1 | # httprepo.py - HTTP repository proxy classes for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms |
|
6 | 6 | # of the GNU General Public License, incorporated herein by reference. |
|
7 | 7 | |
|
8 | 8 | from node import * |
|
9 | 9 | from remoterepo import * |
|
10 | 10 | from i18n import gettext as _ |
|
11 | 11 | from demandload import * |
|
12 | 12 | demandload(globals(), "hg os urllib urllib2 urlparse zlib util httplib") |
|
13 | 13 | |
|
14 | 14 | class passwordmgr(urllib2.HTTPPasswordMgr): |
|
15 | 15 | def __init__(self, ui): |
|
16 | 16 | urllib2.HTTPPasswordMgr.__init__(self) |
|
17 | 17 | self.ui = ui |
|
18 | 18 | |
|
19 | 19 | def find_user_password(self, realm, authuri): |
|
20 | 20 | authinfo = urllib2.HTTPPasswordMgr.find_user_password( |
|
21 | 21 | self, realm, authuri) |
|
22 | 22 | if authinfo != (None, None): |
|
23 | 23 | return authinfo |
|
24 | 24 | |
|
25 | if not ui.interactive: | |
|
26 | raise util.Abort(_('http authorization required')) | |
|
27 | ||
|
25 | 28 | self.ui.write(_("http authorization required\n")) |
|
26 | 29 | self.ui.status(_("realm: %s\n") % realm) |
|
27 | 30 | user = self.ui.prompt(_("user:"), default=None) |
|
28 | 31 | passwd = self.ui.getpass() |
|
29 | 32 | |
|
30 | 33 | self.add_password(realm, authuri, user, passwd) |
|
31 | 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 | 71 | class httprepository(remoterepository): |
|
34 | 72 | def __init__(self, ui, path): |
|
35 | # fix missing / after hostname | |
|
36 | s = urlparse.urlsplit(path) | |
|
37 | partial = s[2] | |
|
38 | if not partial: partial = "/" | |
|
39 | self.url = urlparse.urlunsplit((s[0], s[1], partial, '', '')) | |
|
73 | scheme, netloc, urlpath, query, frag = urlparse.urlsplit(path) | |
|
74 | if query or frag: | |
|
75 | raise util.Abort(_('unsupported URL component: "%s"') % | |
|
76 | (query or frag)) | |
|
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 | 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 | 104 | no_list = [ "localhost", "127.0.0.1" ] |
|
42 | host = ui.config("http_proxy", "host") | |
|
43 | if host is None: | |
|
44 | host = os.environ.get("http_proxy") | |
|
45 | if host and host.startswith('http://'): | |
|
46 | host = host[7:] | |
|
47 | user = ui.config("http_proxy", "user") | |
|
48 | passwd = ui.config("http_proxy", "passwd") | |
|
49 |
|
|
|
50 | if no is None: | |
|
51 | no = os.environ.get("no_proxy") | |
|
52 |
|
|
|
53 | no_list = no_list + no.split(",") | |
|
105 | no_list.extend([p.strip().lower() for | |
|
106 | p in ui.config("http_proxy", "no", '').split(',') | |
|
107 | if p.strip()]) | |
|
108 | no_list.extend([p.strip().lower() for | |
|
109 | p in os.getenv("no_proxy", '').split(',') | |
|
110 | if p.strip()]) | |
|
111 | # "http_proxy.always" config is for running tests on localhost | |
|
112 | if (not ui.configbool("http_proxy", "always") and | |
|
113 | host.lower() in no_list): | |
|
114 | ui.debug(_('disabling proxy for %s\n') % host) | |
|
115 | else: | |
|
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 | |
|
56 | for h in no_list: | |
|
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 | |
|
123 | # urllib2 takes proxy values from the environment and those | |
|
124 | # will take precedence if found, so drop them | |
|
64 | 125 | for env in ["HTTP_PROXY", "http_proxy", "no_proxy"]: |
|
65 | 126 | try: |
|
66 | 127 | if os.environ.has_key(env): |
|
67 | 128 | del os.environ[env] |
|
68 | 129 | except OSError: |
|
69 | 130 | pass |
|
70 | 131 | |
|
71 | proxy_handler = urllib2.BaseHandler() | |
|
72 | if host and not no_proxy: | |
|
73 | proxy_handler = urllib2.ProxyHandler({"http" : "http://" + host}) | |
|
132 | passmgr = passwordmgr(ui) | |
|
133 | if user: | |
|
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 | 137 |
|
|
84 |
|
|
|
138 | handler, | |
|
85 | 139 |
|
|
86 | 140 |
|
|
87 | else: | |
|
88 | opener = urllib2.build_opener(proxy_handler, proxyauthinfo) | |
|
89 | 141 | |
|
90 | 142 | # 1.0 here is the _protocol_ version |
|
91 | 143 | opener.addheaders = [('User-agent', 'mercurial/proto-1.0')] |
|
92 | 144 | urllib2.install_opener(opener) |
|
93 | 145 | |
|
94 | 146 | def dev(self): |
|
95 | 147 | return -1 |
|
96 | 148 | |
|
97 | 149 | def lock(self): |
|
98 | 150 | raise util.Abort(_('operation not supported over http')) |
|
99 | 151 | |
|
100 | 152 | def do_cmd(self, cmd, **args): |
|
101 | 153 | self.ui.debug(_("sending %s command\n") % cmd) |
|
102 | 154 | q = {"cmd": cmd} |
|
103 | 155 | q.update(args) |
|
104 | 156 | qs = urllib.urlencode(q) |
|
105 | 157 | cu = "%s?%s" % (self.url, qs) |
|
106 | 158 | try: |
|
107 | 159 | resp = urllib2.urlopen(cu) |
|
108 | 160 | except httplib.HTTPException, inst: |
|
109 | 161 | self.ui.debug(_('http error while sending %s command\n') % cmd) |
|
110 | 162 | self.ui.print_exc() |
|
111 | 163 | raise IOError(None, inst) |
|
112 | 164 | proto = resp.headers['content-type'] |
|
113 | 165 | |
|
114 | 166 | # accept old "text/plain" and "application/hg-changegroup" for now |
|
115 | 167 | if not proto.startswith('application/mercurial') and \ |
|
116 | 168 | not proto.startswith('text/plain') and \ |
|
117 | 169 | not proto.startswith('application/hg-changegroup'): |
|
118 | 170 | raise hg.RepoError(_("'%s' does not appear to be an hg repository") % |
|
119 | 171 | self.url) |
|
120 | 172 | |
|
121 | 173 | if proto.startswith('application/mercurial'): |
|
122 | 174 | version = proto[22:] |
|
123 | 175 | if float(version) > 0.1: |
|
124 | 176 | raise hg.RepoError(_("'%s' uses newer protocol %s") % |
|
125 | 177 | (self.url, version)) |
|
126 | 178 | |
|
127 | 179 | return resp |
|
128 | 180 | |
|
129 | 181 | def heads(self): |
|
130 | 182 | d = self.do_cmd("heads").read() |
|
131 | 183 | try: |
|
132 | 184 | return map(bin, d[:-1].split(" ")) |
|
133 | 185 | except: |
|
134 | 186 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
|
135 | 187 | raise |
|
136 | 188 | |
|
137 | 189 | def branches(self, nodes): |
|
138 | 190 | n = " ".join(map(hex, nodes)) |
|
139 | 191 | d = self.do_cmd("branches", nodes=n).read() |
|
140 | 192 | try: |
|
141 | 193 | br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ] |
|
142 | 194 | return br |
|
143 | 195 | except: |
|
144 | 196 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
|
145 | 197 | raise |
|
146 | 198 | |
|
147 | 199 | def between(self, pairs): |
|
148 | 200 | n = "\n".join(["-".join(map(hex, p)) for p in pairs]) |
|
149 | 201 | d = self.do_cmd("between", pairs=n).read() |
|
150 | 202 | try: |
|
151 | 203 | p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ] |
|
152 | 204 | return p |
|
153 | 205 | except: |
|
154 | 206 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
|
155 | 207 | raise |
|
156 | 208 | |
|
157 | 209 | def changegroup(self, nodes, kind): |
|
158 | 210 | n = " ".join(map(hex, nodes)) |
|
159 | 211 | f = self.do_cmd("changegroup", roots=n) |
|
160 | 212 | bytes = 0 |
|
161 | 213 | |
|
162 | 214 | def zgenerator(f): |
|
163 | 215 | zd = zlib.decompressobj() |
|
164 | 216 | try: |
|
165 | 217 | for chnk in f: |
|
166 | 218 | yield zd.decompress(chnk) |
|
167 | 219 | except httplib.HTTPException, inst: |
|
168 | 220 | raise IOError(None, _('connection ended unexpectedly')) |
|
169 | 221 | yield zd.flush() |
|
170 | 222 | |
|
171 | 223 | return util.chunkbuffer(zgenerator(util.filechunkiter(f))) |
|
172 | 224 | |
|
173 | 225 | class httpsrepository(httprepository): |
|
174 | 226 | pass |
General Comments 0
You need to be logged in to leave comments.
Login now