Show More
@@ -1,297 +1,306 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 | demandload(globals(), "keepalive tempfile socket") | |
|
13 | demandload(globals(), "errno keepalive tempfile socket") | |
|
14 | 14 | |
|
15 | 15 | class passwordmgr(urllib2.HTTPPasswordMgrWithDefaultRealm): |
|
16 | 16 | def __init__(self, ui): |
|
17 | 17 | urllib2.HTTPPasswordMgrWithDefaultRealm.__init__(self) |
|
18 | 18 | self.ui = ui |
|
19 | 19 | |
|
20 | 20 | def find_user_password(self, realm, authuri): |
|
21 | 21 | authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm.find_user_password( |
|
22 | 22 | self, realm, authuri) |
|
23 | 23 | if authinfo != (None, None): |
|
24 | 24 | return authinfo |
|
25 | 25 | |
|
26 | 26 | if not self.ui.interactive: |
|
27 | 27 | raise util.Abort(_('http authorization required')) |
|
28 | 28 | |
|
29 | 29 | self.ui.write(_("http authorization required\n")) |
|
30 | 30 | self.ui.status(_("realm: %s\n") % realm) |
|
31 | 31 | user = self.ui.prompt(_("user:"), default=None) |
|
32 | 32 | passwd = self.ui.getpass() |
|
33 | 33 | |
|
34 | 34 | self.add_password(realm, authuri, user, passwd) |
|
35 | 35 | return (user, passwd) |
|
36 | 36 | |
|
37 | 37 | def netlocsplit(netloc): |
|
38 | 38 | '''split [user[:passwd]@]host[:port] into 4-tuple.''' |
|
39 | 39 | |
|
40 | 40 | a = netloc.find('@') |
|
41 | 41 | if a == -1: |
|
42 | 42 | user, passwd = None, None |
|
43 | 43 | else: |
|
44 | 44 | userpass, netloc = netloc[:a], netloc[a+1:] |
|
45 | 45 | c = userpass.find(':') |
|
46 | 46 | if c == -1: |
|
47 | 47 | user, passwd = urllib.unquote(userpass), None |
|
48 | 48 | else: |
|
49 | 49 | user = urllib.unquote(userpass[:c]) |
|
50 | 50 | passwd = urllib.unquote(userpass[c+1:]) |
|
51 | 51 | c = netloc.find(':') |
|
52 | 52 | if c == -1: |
|
53 | 53 | host, port = netloc, None |
|
54 | 54 | else: |
|
55 | 55 | host, port = netloc[:c], netloc[c+1:] |
|
56 | 56 | return host, port, user, passwd |
|
57 | 57 | |
|
58 | 58 | def netlocunsplit(host, port, user=None, passwd=None): |
|
59 | 59 | '''turn host, port, user, passwd into [user[:passwd]@]host[:port].''' |
|
60 | 60 | if port: |
|
61 | 61 | hostport = host + ':' + port |
|
62 | 62 | else: |
|
63 | 63 | hostport = host |
|
64 | 64 | if user: |
|
65 | 65 | if passwd: |
|
66 | 66 | userpass = urllib.quote(user) + ':' + urllib.quote(passwd) |
|
67 | 67 | else: |
|
68 | 68 | userpass = urllib.quote(user) |
|
69 | 69 | return userpass + '@' + hostport |
|
70 | 70 | return hostport |
|
71 | 71 | |
|
72 | 72 | class httpconnection(keepalive.HTTPConnection): |
|
73 | 73 | # must be able to send big bundle as stream. |
|
74 | 74 | |
|
75 | 75 | def send(self, data): |
|
76 | 76 | if isinstance(data, str): |
|
77 | 77 | keepalive.HTTPConnection.send(self, data) |
|
78 | 78 | else: |
|
79 | 79 | # if auth required, some data sent twice, so rewind here |
|
80 | 80 | data.seek(0) |
|
81 | 81 | for chunk in util.filechunkiter(data): |
|
82 | 82 | keepalive.HTTPConnection.send(self, chunk) |
|
83 | 83 | |
|
84 | 84 | class httphandler(keepalive.HTTPHandler): |
|
85 | 85 | def http_open(self, req): |
|
86 | 86 | return self.do_open(httpconnection, req) |
|
87 | 87 | |
|
88 | 88 | class httprepository(remoterepository): |
|
89 | 89 | def __init__(self, ui, path): |
|
90 | 90 | self.caps = None |
|
91 | 91 | scheme, netloc, urlpath, query, frag = urlparse.urlsplit(path) |
|
92 | 92 | if query or frag: |
|
93 | 93 | raise util.Abort(_('unsupported URL component: "%s"') % |
|
94 | 94 | (query or frag)) |
|
95 | 95 | if not urlpath: urlpath = '/' |
|
96 | 96 | host, port, user, passwd = netlocsplit(netloc) |
|
97 | 97 | |
|
98 | 98 | # urllib cannot handle URLs with embedded user or passwd |
|
99 | 99 | self.url = urlparse.urlunsplit((scheme, netlocunsplit(host, port), |
|
100 | 100 | urlpath, '', '')) |
|
101 | 101 | self.ui = ui |
|
102 | 102 | |
|
103 | 103 | proxyurl = ui.config("http_proxy", "host") or os.getenv('http_proxy') |
|
104 | 104 | proxyauthinfo = None |
|
105 | 105 | handler = httphandler() |
|
106 | 106 | |
|
107 | 107 | if proxyurl: |
|
108 | 108 | # proxy can be proper url or host[:port] |
|
109 | 109 | if not (proxyurl.startswith('http:') or |
|
110 | 110 | proxyurl.startswith('https:')): |
|
111 | 111 | proxyurl = 'http://' + proxyurl + '/' |
|
112 | 112 | snpqf = urlparse.urlsplit(proxyurl) |
|
113 | 113 | proxyscheme, proxynetloc, proxypath, proxyquery, proxyfrag = snpqf |
|
114 | 114 | hpup = netlocsplit(proxynetloc) |
|
115 | 115 | |
|
116 | 116 | proxyhost, proxyport, proxyuser, proxypasswd = hpup |
|
117 | 117 | if not proxyuser: |
|
118 | 118 | proxyuser = ui.config("http_proxy", "user") |
|
119 | 119 | proxypasswd = ui.config("http_proxy", "passwd") |
|
120 | 120 | |
|
121 | 121 | # see if we should use a proxy for this url |
|
122 | 122 | no_list = [ "localhost", "127.0.0.1" ] |
|
123 | 123 | no_list.extend([p.strip().lower() for |
|
124 | 124 | p in ui.config("http_proxy", "no", '').split(',') |
|
125 | 125 | if p.strip()]) |
|
126 | 126 | no_list.extend([p.strip().lower() for |
|
127 | 127 | p in os.getenv("no_proxy", '').split(',') |
|
128 | 128 | if p.strip()]) |
|
129 | 129 | # "http_proxy.always" config is for running tests on localhost |
|
130 | 130 | if (not ui.configbool("http_proxy", "always") and |
|
131 | 131 | host.lower() in no_list): |
|
132 | 132 | ui.debug(_('disabling proxy for %s\n') % host) |
|
133 | 133 | else: |
|
134 | 134 | proxyurl = urlparse.urlunsplit(( |
|
135 | 135 | proxyscheme, netlocunsplit(proxyhost, proxyport, |
|
136 | 136 | proxyuser, proxypasswd or ''), |
|
137 | 137 | proxypath, proxyquery, proxyfrag)) |
|
138 | 138 | handler = urllib2.ProxyHandler({scheme: proxyurl}) |
|
139 | 139 | ui.debug(_('proxying through %s\n') % proxyurl) |
|
140 | 140 | |
|
141 | 141 | # urllib2 takes proxy values from the environment and those |
|
142 | 142 | # will take precedence if found, so drop them |
|
143 | 143 | for env in ["HTTP_PROXY", "http_proxy", "no_proxy"]: |
|
144 | 144 | try: |
|
145 | 145 | if os.environ.has_key(env): |
|
146 | 146 | del os.environ[env] |
|
147 | 147 | except OSError: |
|
148 | 148 | pass |
|
149 | 149 | |
|
150 | 150 | passmgr = passwordmgr(ui) |
|
151 | 151 | if user: |
|
152 | 152 | ui.debug(_('will use user %s, password %s for http auth\n') % |
|
153 | 153 | (user, '*' * len(passwd))) |
|
154 | 154 | passmgr.add_password(None, host, user, passwd or '') |
|
155 | 155 | |
|
156 | 156 | opener = urllib2.build_opener( |
|
157 | 157 | handler, |
|
158 | 158 | urllib2.HTTPBasicAuthHandler(passmgr), |
|
159 | 159 | urllib2.HTTPDigestAuthHandler(passmgr)) |
|
160 | 160 | |
|
161 | 161 | # 1.0 here is the _protocol_ version |
|
162 | 162 | opener.addheaders = [('User-agent', 'mercurial/proto-1.0')] |
|
163 | 163 | urllib2.install_opener(opener) |
|
164 | 164 | |
|
165 | 165 | # look up capabilities only when needed |
|
166 | 166 | |
|
167 | 167 | def get_caps(self): |
|
168 | 168 | if self.caps is None: |
|
169 | 169 | try: |
|
170 | 170 | self.caps = self.do_read('capabilities').split() |
|
171 | 171 | except hg.RepoError: |
|
172 | 172 | self.caps = () |
|
173 | 173 | self.ui.debug(_('capabilities: %s\n') % |
|
174 | 174 | (' '.join(self.caps or ['none']))) |
|
175 | 175 | return self.caps |
|
176 | 176 | |
|
177 | 177 | capabilities = property(get_caps) |
|
178 | 178 | |
|
179 | 179 | def dev(self): |
|
180 | 180 | return -1 |
|
181 | 181 | |
|
182 | 182 | def lock(self): |
|
183 | 183 | raise util.Abort(_('operation not supported over http')) |
|
184 | 184 | |
|
185 | 185 | def do_cmd(self, cmd, **args): |
|
186 | 186 | data = args.pop('data', None) |
|
187 | 187 | headers = args.pop('headers', {}) |
|
188 | 188 | self.ui.debug(_("sending %s command\n") % cmd) |
|
189 | 189 | q = {"cmd": cmd} |
|
190 | 190 | q.update(args) |
|
191 | 191 | qs = urllib.urlencode(q) |
|
192 | 192 | cu = "%s?%s" % (self.url, qs) |
|
193 | 193 | try: |
|
194 | 194 | resp = urllib2.urlopen(urllib2.Request(cu, data, headers)) |
|
195 | except urllib2.HTTPError, inst: | |
|
196 | if inst.code == 401: | |
|
197 | raise util.Abort(_('authorization failed')) | |
|
198 | raise | |
|
195 | 199 | except httplib.HTTPException, inst: |
|
196 | 200 | self.ui.debug(_('http error while sending %s command\n') % cmd) |
|
197 | 201 | self.ui.print_exc() |
|
198 | 202 | raise IOError(None, inst) |
|
199 | 203 | try: |
|
200 | 204 | proto = resp.getheader('content-type') |
|
201 | 205 | except AttributeError: |
|
202 | 206 | proto = resp.headers['content-type'] |
|
203 | 207 | |
|
204 | 208 | # accept old "text/plain" and "application/hg-changegroup" for now |
|
205 | 209 | if not proto.startswith('application/mercurial') and \ |
|
206 | 210 | not proto.startswith('text/plain') and \ |
|
207 | 211 | not proto.startswith('application/hg-changegroup'): |
|
208 | 212 | raise hg.RepoError(_("'%s' does not appear to be an hg repository") % |
|
209 | 213 | self.url) |
|
210 | 214 | |
|
211 | 215 | if proto.startswith('application/mercurial'): |
|
212 | 216 | version = proto[22:] |
|
213 | 217 | if float(version) > 0.1: |
|
214 | 218 | raise hg.RepoError(_("'%s' uses newer protocol %s") % |
|
215 | 219 | (self.url, version)) |
|
216 | 220 | |
|
217 | 221 | return resp |
|
218 | 222 | |
|
219 | 223 | def do_read(self, cmd, **args): |
|
220 | 224 | fp = self.do_cmd(cmd, **args) |
|
221 | 225 | try: |
|
222 | 226 | return fp.read() |
|
223 | 227 | finally: |
|
224 | 228 | # if using keepalive, allow connection to be reused |
|
225 | 229 | fp.close() |
|
226 | 230 | |
|
227 | 231 | def heads(self): |
|
228 | 232 | d = self.do_read("heads") |
|
229 | 233 | try: |
|
230 | 234 | return map(bin, d[:-1].split(" ")) |
|
231 | 235 | except: |
|
232 | 236 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
|
233 | 237 | raise |
|
234 | 238 | |
|
235 | 239 | def branches(self, nodes): |
|
236 | 240 | n = " ".join(map(hex, nodes)) |
|
237 | 241 | d = self.do_read("branches", nodes=n) |
|
238 | 242 | try: |
|
239 | 243 | br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ] |
|
240 | 244 | return br |
|
241 | 245 | except: |
|
242 | 246 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
|
243 | 247 | raise |
|
244 | 248 | |
|
245 | 249 | def between(self, pairs): |
|
246 | 250 | n = "\n".join(["-".join(map(hex, p)) for p in pairs]) |
|
247 | 251 | d = self.do_read("between", pairs=n) |
|
248 | 252 | try: |
|
249 | 253 | p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ] |
|
250 | 254 | return p |
|
251 | 255 | except: |
|
252 | 256 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
|
253 | 257 | raise |
|
254 | 258 | |
|
255 | 259 | def changegroup(self, nodes, kind): |
|
256 | 260 | n = " ".join(map(hex, nodes)) |
|
257 | 261 | f = self.do_cmd("changegroup", roots=n) |
|
258 | 262 | bytes = 0 |
|
259 | 263 | |
|
260 | 264 | def zgenerator(f): |
|
261 | 265 | zd = zlib.decompressobj() |
|
262 | 266 | try: |
|
263 | 267 | for chnk in f: |
|
264 | 268 | yield zd.decompress(chnk) |
|
265 | 269 | except httplib.HTTPException, inst: |
|
266 | 270 | raise IOError(None, _('connection ended unexpectedly')) |
|
267 | 271 | yield zd.flush() |
|
268 | 272 | |
|
269 | 273 | return util.chunkbuffer(zgenerator(util.filechunkiter(f))) |
|
270 | 274 | |
|
271 | 275 | def unbundle(self, cg, heads, source): |
|
272 | 276 | # have to stream bundle to a temp file because we do not have |
|
273 | 277 | # http 1.1 chunked transfer. |
|
274 | 278 | |
|
275 | 279 | fd, tempname = tempfile.mkstemp(prefix='hg-unbundle-') |
|
276 | 280 | fp = os.fdopen(fd, 'wb+') |
|
277 | 281 | try: |
|
278 | 282 | for chunk in util.filechunkiter(cg): |
|
279 | 283 | fp.write(chunk) |
|
280 | 284 | length = fp.tell() |
|
281 | rfp = self.do_cmd( | |
|
282 | 'unbundle', data=fp, | |
|
283 | headers={'content-length': length, | |
|
284 | 'content-type': 'application/octet-stream'}, | |
|
285 | heads=' '.join(map(hex, heads))) | |
|
286 | 285 | try: |
|
287 | ret = int(rfp.readline()) | |
|
288 | self.ui.write(rfp.read()) | |
|
289 | return ret | |
|
290 | finally: | |
|
291 | rfp.close() | |
|
286 | rfp = self.do_cmd( | |
|
287 | 'unbundle', data=fp, | |
|
288 | headers={'content-length': length, | |
|
289 | 'content-type': 'application/octet-stream'}, | |
|
290 | heads=' '.join(map(hex, heads))) | |
|
291 | try: | |
|
292 | ret = int(rfp.readline()) | |
|
293 | self.ui.write(rfp.read()) | |
|
294 | return ret | |
|
295 | finally: | |
|
296 | rfp.close() | |
|
297 | except socket.error, err: | |
|
298 | if err[0] in (errno.ECONNRESET, errno.EPIPE): | |
|
299 | raise util.Abort(_('push failed: %s'), err[1]) | |
|
300 | raise util.Abort(err[1]) | |
|
292 | 301 | finally: |
|
293 | 302 | fp.close() |
|
294 | 303 | os.unlink(tempname) |
|
295 | 304 | |
|
296 | 305 | class httpsrepository(httprepository): |
|
297 | 306 | pass |
General Comments 0
You need to be logged in to leave comments.
Login now