httprepo.py
201 lines
| 7.1 KiB
| text/x-python
|
PythonLexer
/ mercurial / httprepo.py
mpm@selenic.com
|
r1089 | # httprepo.py - HTTP repository proxy classes for mercurial | ||
# | ||||
Vadim Gelfer
|
r2859 | # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | ||
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> | ||||
mpm@selenic.com
|
r1089 | # | ||
Martin Geisler
|
r8225 | # This software may be used and distributed according to the terms of the | ||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
mpm@selenic.com
|
r1089 | |||
Brodie Rao
|
r12062 | from node import nullid | ||
Matt Mackall
|
r3891 | from i18n import _ | ||
Brodie Rao
|
r12062 | import changegroup, statichttprepo, error, url, util, wireproto | ||
Simon Heimberg
|
r8312 | import os, urllib, urllib2, urlparse, zlib, httplib | ||
import errno, socket | ||||
Alexis S. L. Carvalho
|
r4678 | |||
Matt Mackall
|
r3661 | def zgenerator(f): | ||
zd = zlib.decompressobj() | ||||
try: | ||||
for chunk in util.filechunkiter(f): | ||||
Matt Mackall
|
r11757 | while chunk: | ||
yield zd.decompress(chunk, 2**18) | ||||
chunk = zd.unconsumed_tail | ||||
Benoit Boissinot
|
r7280 | except httplib.HTTPException: | ||
Matt Mackall
|
r3661 | raise IOError(None, _('connection ended unexpectedly')) | ||
yield zd.flush() | ||||
Matt Mackall
|
r11587 | class httprepository(wireproto.wirerepository): | ||
mpm@selenic.com
|
r1089 | def __init__(self, ui, path): | ||
Vadim Gelfer
|
r2673 | self.path = path | ||
Vadim Gelfer
|
r2442 | self.caps = None | ||
Andrei Vermel
|
r4132 | self.handler = None | ||
Vadim Gelfer
|
r2337 | scheme, netloc, urlpath, query, frag = urlparse.urlsplit(path) | ||
if query or frag: | ||||
raise util.Abort(_('unsupported URL component: "%s"') % | ||||
(query or frag)) | ||||
# urllib cannot handle URLs with embedded user or passwd | ||||
Benoit Boissinot
|
r7270 | self._url, authinfo = url.getauthinfo(path) | ||
mpm@selenic.com
|
r1089 | self.ui = ui | ||
Martin Geisler
|
r9467 | self.ui.debug('using %s\n' % self._url) | ||
Vadim Gelfer
|
r2337 | |||
Benoit Boissinot
|
r7270 | self.urlopener = url.opener(ui, authinfo) | ||
Thomas Arendsen Hein
|
r4516 | |||
Steve Borho
|
r7752 | def __del__(self): | ||
for h in self.urlopener.handlers: | ||||
h.close() | ||||
if hasattr(h, "close_all"): | ||||
h.close_all() | ||||
Vadim Gelfer
|
r2673 | def url(self): | ||
return self.path | ||||
Vadim Gelfer
|
r2442 | # look up capabilities only when needed | ||
def get_caps(self): | ||||
if self.caps is None: | ||||
try: | ||||
Matt Mackall
|
r11589 | self.caps = set(self._call('capabilities').split()) | ||
Matt Mackall
|
r7637 | except error.RepoError: | ||
Martin Geisler
|
r8150 | self.caps = set() | ||
Martin Geisler
|
r9467 | self.ui.debug('capabilities: %s\n' % | ||
Vadim Gelfer
|
r2465 | (' '.join(self.caps or ['none']))) | ||
Vadim Gelfer
|
r2442 | return self.caps | ||
capabilities = property(get_caps) | ||||
Vadim Gelfer
|
r1870 | def lock(self): | ||
raise util.Abort(_('operation not supported over http')) | ||||
Matt Mackall
|
r11589 | def _callstream(self, cmd, **args): | ||
Vadim Gelfer
|
r2465 | data = args.pop('data', None) | ||
headers = args.pop('headers', {}) | ||||
Martin Geisler
|
r9467 | self.ui.debug("sending %s command\n" % cmd) | ||
mpm@selenic.com
|
r1089 | q = {"cmd": cmd} | ||
q.update(args) | ||||
Benoit Boissinot
|
r3562 | qs = '?%s' % urllib.urlencode(q) | ||
cu = "%s%s" % (self._url, qs) | ||||
Benoit Boissinot
|
r10491 | req = urllib2.Request(cu, data, headers) | ||
if data is not None: | ||||
# len(data) is broken if data doesn't fit into Py_ssize_t | ||||
# add the header ourself to avoid OverflowError | ||||
size = data.__len__() | ||||
self.ui.debug("sending %s bytes\n" % size) | ||||
req.add_unredirected_header('Content-Length', '%d' % size) | ||||
Thomas Arendsen Hein
|
r2294 | try: | ||
Benoit Boissinot
|
r10491 | resp = self.urlopener.open(req) | ||
Vadim Gelfer
|
r2467 | except urllib2.HTTPError, inst: | ||
if inst.code == 401: | ||||
raise util.Abort(_('authorization failed')) | ||||
raise | ||||
Thomas Arendsen Hein
|
r2294 | except httplib.HTTPException, inst: | ||
Martin Geisler
|
r9467 | self.ui.debug('http error while sending %s command\n' % cmd) | ||
Matt Mackall
|
r8206 | self.ui.traceback() | ||
Vadim Gelfer
|
r2336 | raise IOError(None, inst) | ||
Thomas Arendsen Hein
|
r3399 | except IndexError: | ||
# this only happens with Python 2.3, later versions raise URLError | ||||
raise util.Abort(_('http error, possibly caused by proxy setting')) | ||||
Benoit Boissinot
|
r3562 | # record the url we got redirected to | ||
Thomas Arendsen Hein
|
r3570 | resp_url = resp.geturl() | ||
if resp_url.endswith(qs): | ||||
resp_url = resp_url[:-len(qs)] | ||||
Dan Villiom Podlaski Christiansen
|
r9881 | if self._url.rstrip('/') != resp_url.rstrip('/'): | ||
Thomas Arendsen Hein
|
r3570 | self.ui.status(_('real URL is %s\n') % resp_url) | ||
Steve Borho
|
r10208 | self._url = resp_url | ||
Vadim Gelfer
|
r2435 | try: | ||
proto = resp.getheader('content-type') | ||||
except AttributeError: | ||||
proto = resp.headers['content-type'] | ||||
mpm@selenic.com
|
r1089 | |||
Steve Borho
|
r8053 | safeurl = url.hidepassword(self._url) | ||
mpm@selenic.com
|
r1089 | # accept old "text/plain" and "application/hg-changegroup" for now | ||
Thomas Arendsen Hein
|
r4633 | if not (proto.startswith('application/mercurial-') or | ||
proto.startswith('text/plain') or | ||||
proto.startswith('application/hg-changegroup')): | ||||
Martin Geisler
|
r9467 | self.ui.debug("requested URL: '%s'\n" % url.hidepassword(cu)) | ||
Matt Mackall
|
r10282 | raise error.RepoError( | ||
_("'%s' does not appear to be an hg repository:\n" | ||||
"---%%<--- (%s)\n%s\n---%%<---\n") | ||||
% (safeurl, proto, resp.read())) | ||||
mpm@selenic.com
|
r1089 | |||
Benoit Boissinot
|
r4012 | if proto.startswith('application/mercurial-'): | ||
try: | ||||
Thomas Arendsen Hein
|
r4356 | version = proto.split('-', 1)[1] | ||
version_info = tuple([int(n) for n in version.split('.')]) | ||||
Benoit Boissinot
|
r4012 | except ValueError: | ||
Matt Mackall
|
r7637 | raise error.RepoError(_("'%s' sent a broken Content-Type " | ||
Steve Borho
|
r8053 | "header (%s)") % (safeurl, proto)) | ||
Thomas Arendsen Hein
|
r4356 | if version_info > (0, 1): | ||
Matt Mackall
|
r7637 | raise error.RepoError(_("'%s' uses newer protocol %s") % | ||
Steve Borho
|
r8053 | (safeurl, version)) | ||
mpm@selenic.com
|
r1089 | |||
return resp | ||||
Matt Mackall
|
r11589 | def _call(self, cmd, **args): | ||
fp = self._callstream(cmd, **args) | ||||
Vadim Gelfer
|
r2435 | try: | ||
return fp.read() | ||||
finally: | ||||
# if using keepalive, allow connection to be reused | ||||
fp.close() | ||||
Matt Mackall
|
r11592 | def _callpush(self, cmd, cg, **args): | ||
Vadim Gelfer
|
r2465 | # have to stream bundle to a temp file because we do not have | ||
# http 1.1 chunked transfer. | ||||
Matt Mackall
|
r3662 | type = "" | ||
types = self.capable('unbundle') | ||||
Alexis S. L. Carvalho
|
r3703 | # servers older than d1b16a746db6 will send 'unbundle' as a | ||
# boolean capability | ||||
try: | ||||
types = types.split(',') | ||||
except AttributeError: | ||||
types = [""] | ||||
Matt Mackall
|
r3662 | if types: | ||
Alexis S. L. Carvalho
|
r3703 | for x in types: | ||
Matt Mackall
|
r3662 | if x in changegroup.bundletypes: | ||
type = x | ||||
break | ||||
Thomas Arendsen Hein
|
r3613 | |||
Matt Mackall
|
r3662 | tempname = changegroup.writebundle(cg, None, type) | ||
Benoit Boissinot
|
r7270 | fp = url.httpsendfile(tempname, "rb") | ||
Matt Mackall
|
r11592 | headers = {'Content-Type': 'application/mercurial-0.1'} | ||
Vadim Gelfer
|
r2465 | try: | ||
try: | ||||
Matt Mackall
|
r11592 | r = self._call(cmd, data=fp, headers=headers, **args) | ||
return r.split('\n', 1) | ||||
Vadim Gelfer
|
r2467 | except socket.error, err: | ||
Renato Cunha
|
r11567 | if err.args[0] in (errno.ECONNRESET, errno.EPIPE): | ||
raise util.Abort(_('push failed: %s') % err.args[1]) | ||||
raise util.Abort(err.args[1]) | ||||
Vadim Gelfer
|
r2465 | finally: | ||
fp.close() | ||||
os.unlink(tempname) | ||||
Vadim Gelfer
|
r2439 | |||
Matt Mackall
|
r11592 | def _abort(self, exception): | ||
raise exception | ||||
Vadim Gelfer
|
r2612 | |||
Matt Mackall
|
r11592 | def _decompress(self, stream): | ||
return util.chunkbuffer(zgenerator(stream)) | ||||
Matt Mackall
|
r11370 | |||
mpm@selenic.com
|
r1089 | class httpsrepository(httprepository): | ||
Alexis S. L. Carvalho
|
r2569 | def __init__(self, ui, path): | ||
Benoit Boissinot
|
r7279 | if not url.has_https: | ||
Alexis S. L. Carvalho
|
r2569 | raise util.Abort(_('Python support for SSL and HTTPS ' | ||
'is not installed')) | ||||
httprepository.__init__(self, ui, path) | ||||
Vadim Gelfer
|
r2740 | |||
def instance(ui, path, create): | ||||
if create: | ||||
raise util.Abort(_('cannot create new http repository')) | ||||
Matt Mackall
|
r7211 | try: | ||
if path.startswith('https:'): | ||||
inst = httpsrepository(ui, path) | ||||
else: | ||||
inst = httprepository(ui, path) | ||||
inst.between([(nullid, nullid)]) | ||||
return inst | ||||
Matt Mackall
|
r7637 | except error.RepoError: | ||
Matt Mackall
|
r7211 | ui.note('(falling back to static-http)\n') | ||
return statichttprepo.instance(ui, "static-" + path, create) | ||||