Show More
@@ -1,169 +1,172 b'' | |||||
1 | # httprepo.py - HTTP repository proxy classes for mercurial |
|
1 | # httprepo.py - HTTP repository proxy classes for mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2005 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms |
|
5 | # This software may be used and distributed according to the terms | |
6 | # of the GNU General Public License, incorporated herein by reference. |
|
6 | # of the GNU General Public License, incorporated herein by reference. | |
7 |
|
7 | |||
8 | from node import * |
|
8 | from node import * | |
9 | from remoterepo import * |
|
9 | from remoterepo import * | |
10 | from i18n import gettext as _ |
|
10 | from i18n import gettext as _ | |
11 | from demandload import * |
|
11 | from demandload import * | |
12 | demandload(globals(), "hg os urllib urllib2 urlparse zlib util httplib") |
|
12 | demandload(globals(), "hg os urllib urllib2 urlparse zlib util httplib") | |
13 |
|
13 | |||
14 | class passwordmgr(urllib2.HTTPPasswordMgr): |
|
14 | class passwordmgr(urllib2.HTTPPasswordMgr): | |
15 | def __init__(self, ui): |
|
15 | def __init__(self, ui): | |
16 | urllib2.HTTPPasswordMgr.__init__(self) |
|
16 | urllib2.HTTPPasswordMgr.__init__(self) | |
17 | self.ui = ui |
|
17 | self.ui = ui | |
18 |
|
18 | |||
19 | def find_user_password(self, realm, authuri): |
|
19 | def find_user_password(self, realm, authuri): | |
20 | authinfo = urllib2.HTTPPasswordMgr.find_user_password( |
|
20 | authinfo = urllib2.HTTPPasswordMgr.find_user_password( | |
21 | self, realm, authuri) |
|
21 | self, realm, authuri) | |
22 | if authinfo != (None, None): |
|
22 | if authinfo != (None, None): | |
23 | return authinfo |
|
23 | return authinfo | |
24 |
|
24 | |||
25 | self.ui.write(_("http authorization required\n")) |
|
25 | self.ui.write(_("http authorization required\n")) | |
26 | self.ui.status(_("realm: %s\n") % realm) |
|
26 | self.ui.status(_("realm: %s\n") % realm) | |
27 | user = self.ui.prompt(_("user:"), default=None) |
|
27 | user = self.ui.prompt(_("user:"), default=None) | |
28 | passwd = self.ui.getpass() |
|
28 | passwd = self.ui.getpass() | |
29 |
|
29 | |||
30 | self.add_password(realm, authuri, user, passwd) |
|
30 | self.add_password(realm, authuri, user, passwd) | |
31 | return (user, passwd) |
|
31 | return (user, passwd) | |
32 |
|
32 | |||
33 | class httprepository(remoterepository): |
|
33 | class httprepository(remoterepository): | |
34 | def __init__(self, ui, path): |
|
34 | def __init__(self, ui, path): | |
35 | # fix missing / after hostname |
|
35 | # fix missing / after hostname | |
36 | s = urlparse.urlsplit(path) |
|
36 | s = urlparse.urlsplit(path) | |
37 | partial = s[2] |
|
37 | partial = s[2] | |
38 | if not partial: partial = "/" |
|
38 | if not partial: partial = "/" | |
39 | self.url = urlparse.urlunsplit((s[0], s[1], partial, '', '')) |
|
39 | self.url = urlparse.urlunsplit((s[0], s[1], partial, '', '')) | |
40 | self.ui = ui |
|
40 | self.ui = ui | |
41 | no_list = [ "localhost", "127.0.0.1" ] |
|
41 | no_list = [ "localhost", "127.0.0.1" ] | |
42 | host = ui.config("http_proxy", "host") |
|
42 | host = ui.config("http_proxy", "host") | |
43 | if host is None: |
|
43 | if host is None: | |
44 | host = os.environ.get("http_proxy") |
|
44 | host = os.environ.get("http_proxy") | |
45 | if host and host.startswith('http://'): |
|
45 | if host and host.startswith('http://'): | |
46 | host = host[7:] |
|
46 | host = host[7:] | |
47 | user = ui.config("http_proxy", "user") |
|
47 | user = ui.config("http_proxy", "user") | |
48 | passwd = ui.config("http_proxy", "passwd") |
|
48 | passwd = ui.config("http_proxy", "passwd") | |
49 | no = ui.config("http_proxy", "no") |
|
49 | no = ui.config("http_proxy", "no") | |
50 | if no is None: |
|
50 | if no is None: | |
51 | no = os.environ.get("no_proxy") |
|
51 | no = os.environ.get("no_proxy") | |
52 | if no: |
|
52 | if no: | |
53 | no_list = no_list + no.split(",") |
|
53 | no_list = no_list + no.split(",") | |
54 |
|
54 | |||
55 | no_proxy = 0 |
|
55 | no_proxy = 0 | |
56 | for h in no_list: |
|
56 | for h in no_list: | |
57 | if (path.startswith("http://" + h + "/") or |
|
57 | if (path.startswith("http://" + h + "/") or | |
58 | path.startswith("http://" + h + ":") or |
|
58 | path.startswith("http://" + h + ":") or | |
59 | path == "http://" + h): |
|
59 | path == "http://" + h): | |
60 | no_proxy = 1 |
|
60 | no_proxy = 1 | |
61 |
|
61 | |||
62 | # Note: urllib2 takes proxy values from the environment and those will |
|
62 | # Note: urllib2 takes proxy values from the environment and those will | |
63 | # take precedence |
|
63 | # take precedence | |
64 | for env in ["HTTP_PROXY", "http_proxy", "no_proxy"]: |
|
64 | for env in ["HTTP_PROXY", "http_proxy", "no_proxy"]: | |
65 | try: |
|
65 | try: | |
66 | if os.environ.has_key(env): |
|
66 | if os.environ.has_key(env): | |
67 | del os.environ[env] |
|
67 | del os.environ[env] | |
68 | except OSError: |
|
68 | except OSError: | |
69 | pass |
|
69 | pass | |
70 |
|
70 | |||
71 | proxy_handler = urllib2.BaseHandler() |
|
71 | proxy_handler = urllib2.BaseHandler() | |
72 | if host and not no_proxy: |
|
72 | if host and not no_proxy: | |
73 | proxy_handler = urllib2.ProxyHandler({"http" : "http://" + host}) |
|
73 | proxy_handler = urllib2.ProxyHandler({"http" : "http://" + host}) | |
74 |
|
74 | |||
75 | proxyauthinfo = None |
|
75 | proxyauthinfo = None | |
76 | if user and passwd: |
|
76 | if user and passwd: | |
77 | passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm() |
|
77 | passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm() | |
78 | passmgr.add_password(None, host, user, passwd) |
|
78 | passmgr.add_password(None, host, user, passwd) | |
79 | proxyauthinfo = urllib2.ProxyBasicAuthHandler(passmgr) |
|
79 | proxyauthinfo = urllib2.ProxyBasicAuthHandler(passmgr) | |
80 |
|
80 | |||
81 | if ui.interactive: |
|
81 | if ui.interactive: | |
82 | passmgr = passwordmgr(ui) |
|
82 | passmgr = passwordmgr(ui) | |
83 | opener = urllib2.build_opener( |
|
83 | opener = urllib2.build_opener( | |
84 | proxy_handler, proxyauthinfo, |
|
84 | proxy_handler, proxyauthinfo, | |
85 | urllib2.HTTPBasicAuthHandler(passmgr), |
|
85 | urllib2.HTTPBasicAuthHandler(passmgr), | |
86 | urllib2.HTTPDigestAuthHandler(passmgr)) |
|
86 | urllib2.HTTPDigestAuthHandler(passmgr)) | |
87 | else: |
|
87 | else: | |
88 | opener = urllib2.build_opener(proxy_handler, proxyauthinfo) |
|
88 | opener = urllib2.build_opener(proxy_handler, proxyauthinfo) | |
89 |
|
89 | |||
90 | # 1.0 here is the _protocol_ version |
|
90 | # 1.0 here is the _protocol_ version | |
91 | opener.addheaders = [('User-agent', 'mercurial/proto-1.0')] |
|
91 | opener.addheaders = [('User-agent', 'mercurial/proto-1.0')] | |
92 | urllib2.install_opener(opener) |
|
92 | urllib2.install_opener(opener) | |
93 |
|
93 | |||
94 | def dev(self): |
|
94 | def dev(self): | |
95 | return -1 |
|
95 | return -1 | |
96 |
|
96 | |||
97 | def lock(self): |
|
97 | def lock(self): | |
98 | raise util.Abort(_('operation not supported over http')) |
|
98 | raise util.Abort(_('operation not supported over http')) | |
99 |
|
99 | |||
100 | def do_cmd(self, cmd, **args): |
|
100 | def do_cmd(self, cmd, **args): | |
101 | self.ui.debug(_("sending %s command\n") % cmd) |
|
101 | self.ui.debug(_("sending %s command\n") % cmd) | |
102 | q = {"cmd": cmd} |
|
102 | q = {"cmd": cmd} | |
103 | q.update(args) |
|
103 | q.update(args) | |
104 | qs = urllib.urlencode(q) |
|
104 | qs = urllib.urlencode(q) | |
105 | cu = "%s?%s" % (self.url, qs) |
|
105 | cu = "%s?%s" % (self.url, qs) | |
106 | resp = urllib2.urlopen(cu) |
|
106 | try: | |
|
107 | resp = urllib2.urlopen(cu) | |||
|
108 | except httplib.HTTPException, inst: | |||
|
109 | raise IOError(None, _('http error while sending %s command') % cmd) | |||
107 | proto = resp.headers['content-type'] |
|
110 | proto = resp.headers['content-type'] | |
108 |
|
111 | |||
109 | # accept old "text/plain" and "application/hg-changegroup" for now |
|
112 | # accept old "text/plain" and "application/hg-changegroup" for now | |
110 | if not proto.startswith('application/mercurial') and \ |
|
113 | if not proto.startswith('application/mercurial') and \ | |
111 | not proto.startswith('text/plain') and \ |
|
114 | not proto.startswith('text/plain') and \ | |
112 | not proto.startswith('application/hg-changegroup'): |
|
115 | not proto.startswith('application/hg-changegroup'): | |
113 | raise hg.RepoError(_("'%s' does not appear to be an hg repository") % |
|
116 | raise hg.RepoError(_("'%s' does not appear to be an hg repository") % | |
114 | self.url) |
|
117 | self.url) | |
115 |
|
118 | |||
116 | if proto.startswith('application/mercurial'): |
|
119 | if proto.startswith('application/mercurial'): | |
117 | version = proto[22:] |
|
120 | version = proto[22:] | |
118 | if float(version) > 0.1: |
|
121 | if float(version) > 0.1: | |
119 | raise hg.RepoError(_("'%s' uses newer protocol %s") % |
|
122 | raise hg.RepoError(_("'%s' uses newer protocol %s") % | |
120 | (self.url, version)) |
|
123 | (self.url, version)) | |
121 |
|
124 | |||
122 | return resp |
|
125 | return resp | |
123 |
|
126 | |||
124 | def heads(self): |
|
127 | def heads(self): | |
125 | d = self.do_cmd("heads").read() |
|
128 | d = self.do_cmd("heads").read() | |
126 | try: |
|
129 | try: | |
127 | return map(bin, d[:-1].split(" ")) |
|
130 | return map(bin, d[:-1].split(" ")) | |
128 | except: |
|
131 | except: | |
129 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
|
132 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") | |
130 | raise |
|
133 | raise | |
131 |
|
134 | |||
132 | def branches(self, nodes): |
|
135 | def branches(self, nodes): | |
133 | n = " ".join(map(hex, nodes)) |
|
136 | n = " ".join(map(hex, nodes)) | |
134 | d = self.do_cmd("branches", nodes=n).read() |
|
137 | d = self.do_cmd("branches", nodes=n).read() | |
135 | try: |
|
138 | try: | |
136 | br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ] |
|
139 | br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ] | |
137 | return br |
|
140 | return br | |
138 | except: |
|
141 | except: | |
139 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
|
142 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") | |
140 | raise |
|
143 | raise | |
141 |
|
144 | |||
142 | def between(self, pairs): |
|
145 | def between(self, pairs): | |
143 | n = "\n".join(["-".join(map(hex, p)) for p in pairs]) |
|
146 | n = "\n".join(["-".join(map(hex, p)) for p in pairs]) | |
144 | d = self.do_cmd("between", pairs=n).read() |
|
147 | d = self.do_cmd("between", pairs=n).read() | |
145 | try: |
|
148 | try: | |
146 | p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ] |
|
149 | p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ] | |
147 | return p |
|
150 | return p | |
148 | except: |
|
151 | except: | |
149 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
|
152 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") | |
150 | raise |
|
153 | raise | |
151 |
|
154 | |||
152 | def changegroup(self, nodes, kind): |
|
155 | def changegroup(self, nodes, kind): | |
153 | n = " ".join(map(hex, nodes)) |
|
156 | n = " ".join(map(hex, nodes)) | |
154 | f = self.do_cmd("changegroup", roots=n) |
|
157 | f = self.do_cmd("changegroup", roots=n) | |
155 | bytes = 0 |
|
158 | bytes = 0 | |
156 |
|
159 | |||
157 | def zgenerator(f): |
|
160 | def zgenerator(f): | |
158 | zd = zlib.decompressobj() |
|
161 | zd = zlib.decompressobj() | |
159 | try: |
|
162 | try: | |
160 | for chnk in f: |
|
163 | for chnk in f: | |
161 | yield zd.decompress(chnk) |
|
164 | yield zd.decompress(chnk) | |
162 | except httplib.HTTPException, inst: |
|
165 | except httplib.HTTPException, inst: | |
163 | raise IOError(None, _('connection ended unexpectedly')) |
|
166 | raise IOError(None, _('connection ended unexpectedly')) | |
164 | yield zd.flush() |
|
167 | yield zd.flush() | |
165 |
|
168 | |||
166 | return util.chunkbuffer(zgenerator(util.filechunkiter(f))) |
|
169 | return util.chunkbuffer(zgenerator(util.filechunkiter(f))) | |
167 |
|
170 | |||
168 | class httpsrepository(httprepository): |
|
171 | class httpsrepository(httprepository): | |
169 | pass |
|
172 | pass |
General Comments 0
You need to be logged in to leave comments.
Login now