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