httprepo.py
258 lines
| 9.2 KiB
| text/x-python
|
PythonLexer
/ mercurial / httprepo.py
|
r1089 | # httprepo.py - HTTP repository proxy classes for mercurial | ||
# | ||||
|
r2859 | # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | ||
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> | ||||
|
r1089 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
# GNU General Public License version 2, incorporated herein by reference. | ||||
|
r1089 | |||
|
r7211 | from node import bin, hex, nullid | ||
|
r3891 | from i18n import _ | ||
|
r8312 | import repo, changegroup, statichttprepo, error, url, util | ||
import os, urllib, urllib2, urlparse, zlib, httplib | ||||
import errno, socket | ||||
|
r4678 | |||
|
r3661 | def zgenerator(f): | ||
zd = zlib.decompressobj() | ||||
try: | ||||
for chunk in util.filechunkiter(f): | ||||
yield zd.decompress(chunk) | ||||
|
r7280 | except httplib.HTTPException: | ||
|
r3661 | raise IOError(None, _('connection ended unexpectedly')) | ||
yield zd.flush() | ||||
|
r6313 | class httprepository(repo.repository): | ||
|
r1089 | def __init__(self, ui, path): | ||
|
r2673 | self.path = path | ||
|
r2442 | self.caps = None | ||
|
r4132 | self.handler = None | ||
|
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 | ||||
|
r7270 | self._url, authinfo = url.getauthinfo(path) | ||
|
r1089 | self.ui = ui | ||
|
r9467 | self.ui.debug('using %s\n' % self._url) | ||
|
r2337 | |||
|
r7270 | self.urlopener = url.opener(ui, authinfo) | ||
|
r4516 | |||
|
r7752 | def __del__(self): | ||
for h in self.urlopener.handlers: | ||||
h.close() | ||||
if hasattr(h, "close_all"): | ||||
h.close_all() | ||||
|
r2673 | def url(self): | ||
return self.path | ||||
|
r2442 | # look up capabilities only when needed | ||
def get_caps(self): | ||||
if self.caps is None: | ||||
try: | ||||
|
r8150 | self.caps = set(self.do_read('capabilities').split()) | ||
|
r7637 | except error.RepoError: | ||
|
r8150 | self.caps = set() | ||
|
r9467 | self.ui.debug('capabilities: %s\n' % | ||
|
r2465 | (' '.join(self.caps or ['none']))) | ||
|
r2442 | return self.caps | ||
capabilities = property(get_caps) | ||||
|
r1870 | def lock(self): | ||
raise util.Abort(_('operation not supported over http')) | ||||
|
r1089 | def do_cmd(self, cmd, **args): | ||
|
r2465 | data = args.pop('data', None) | ||
headers = args.pop('headers', {}) | ||||
|
r9467 | self.ui.debug("sending %s command\n" % cmd) | ||
|
r1089 | q = {"cmd": cmd} | ||
q.update(args) | ||||
|
r3562 | qs = '?%s' % urllib.urlencode(q) | ||
cu = "%s%s" % (self._url, qs) | ||||
|
r2294 | try: | ||
|
r3567 | if data: | ||
|
r9467 | self.ui.debug("sending %s bytes\n" % len(data)) | ||
|
r7270 | resp = self.urlopener.open(urllib2.Request(cu, data, headers)) | ||
|
r2467 | except urllib2.HTTPError, inst: | ||
if inst.code == 401: | ||||
raise util.Abort(_('authorization failed')) | ||||
raise | ||||
|
r2294 | except httplib.HTTPException, inst: | ||
|
r9467 | self.ui.debug('http error while sending %s command\n' % cmd) | ||
|
r8206 | self.ui.traceback() | ||
|
r2336 | raise IOError(None, inst) | ||
|
r3399 | except IndexError: | ||
# this only happens with Python 2.3, later versions raise URLError | ||||
raise util.Abort(_('http error, possibly caused by proxy setting')) | ||||
|
r3562 | # record the url we got redirected to | ||
|
r3570 | resp_url = resp.geturl() | ||
if resp_url.endswith(qs): | ||||
resp_url = resp_url[:-len(qs)] | ||||
if self._url != resp_url: | ||||
self.ui.status(_('real URL is %s\n') % resp_url) | ||||
self._url = resp_url | ||||
|
r2435 | try: | ||
proto = resp.getheader('content-type') | ||||
except AttributeError: | ||||
proto = resp.headers['content-type'] | ||||
|
r1089 | |||
|
r8053 | safeurl = url.hidepassword(self._url) | ||
|
r1089 | # accept old "text/plain" and "application/hg-changegroup" for now | ||
|
r4633 | if not (proto.startswith('application/mercurial-') or | ||
proto.startswith('text/plain') or | ||||
proto.startswith('application/hg-changegroup')): | ||||
|
r9467 | self.ui.debug("requested URL: '%s'\n" % url.hidepassword(cu)) | ||
|
r7637 | raise error.RepoError(_("'%s' does not appear to be an hg repository") | ||
|
r8053 | % safeurl) | ||
|
r1089 | |||
|
r4012 | if proto.startswith('application/mercurial-'): | ||
try: | ||||
|
r4356 | version = proto.split('-', 1)[1] | ||
version_info = tuple([int(n) for n in version.split('.')]) | ||||
|
r4012 | except ValueError: | ||
|
r7637 | raise error.RepoError(_("'%s' sent a broken Content-Type " | ||
|
r8053 | "header (%s)") % (safeurl, proto)) | ||
|
r4356 | if version_info > (0, 1): | ||
|
r7637 | raise error.RepoError(_("'%s' uses newer protocol %s") % | ||
|
r8053 | (safeurl, version)) | ||
|
r1089 | |||
return resp | ||||
|
r2435 | def do_read(self, cmd, **args): | ||
fp = self.do_cmd(cmd, **args) | ||||
try: | ||||
return fp.read() | ||||
finally: | ||||
# if using keepalive, allow connection to be reused | ||||
fp.close() | ||||
|
r3444 | def lookup(self, key): | ||
|
r5259 | self.requirecap('lookup', _('look up remote revision')) | ||
|
r3445 | d = self.do_cmd("lookup", key = key).read() | ||
success, data = d[:-1].split(' ', 1) | ||||
if int(success): | ||||
return bin(data) | ||||
|
r7637 | raise error.RepoError(data) | ||
|
r3444 | |||
|
r1089 | def heads(self): | ||
|
r2435 | d = self.do_read("heads") | ||
|
r1089 | try: | ||
return map(bin, d[:-1].split(" ")) | ||||
except: | ||||
|
r7641 | raise error.ResponseError(_("unexpected response:"), d) | ||
|
r1089 | |||
|
r8563 | def branchmap(self): | ||
d = self.do_read("branchmap") | ||||
try: | ||||
branchmap = {} | ||||
for branchpart in d.splitlines(): | ||||
branchheads = branchpart.split(' ') | ||||
branchname = urllib.unquote(branchheads[0]) | ||||
branchheads = [bin(x) for x in branchheads[1:]] | ||||
branchmap[branchname] = branchheads | ||||
return branchmap | ||||
except: | ||||
raise error.ResponseError(_("unexpected response:"), d) | ||||
|
r1089 | def branches(self, nodes): | ||
n = " ".join(map(hex, nodes)) | ||||
|
r2435 | d = self.do_read("branches", nodes=n) | ||
|
r1089 | try: | ||
br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ] | ||||
return br | ||||
except: | ||||
|
r7641 | raise error.ResponseError(_("unexpected response:"), d) | ||
|
r1089 | |||
def between(self, pairs): | ||||
|
r7342 | batch = 8 # avoid giant requests | ||
r = [] | ||||
for i in xrange(0, len(pairs), batch): | ||||
n = " ".join(["-".join(map(hex, p)) for p in pairs[i:i + batch]]) | ||||
d = self.do_read("between", pairs=n) | ||||
try: | ||||
r += [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ] | ||||
except: | ||||
|
r7641 | raise error.ResponseError(_("unexpected response:"), d) | ||
|
r7342 | return r | ||
|
r1089 | |||
|
r1736 | def changegroup(self, nodes, kind): | ||
|
r1089 | n = " ".join(map(hex, nodes)) | ||
f = self.do_cmd("changegroup", roots=n) | ||||
|
r3661 | return util.chunkbuffer(zgenerator(f)) | ||
|
r3444 | |||
def changegroupsubset(self, bases, heads, source): | ||||
|
r5259 | self.requirecap('changegroupsubset', _('look up remote changes')) | ||
|
r3444 | baselst = " ".join([hex(n) for n in bases]) | ||
headlst = " ".join([hex(n) for n in heads]) | ||||
f = self.do_cmd("changegroupsubset", bases=baselst, heads=headlst) | ||||
|
r3661 | return util.chunkbuffer(zgenerator(f)) | ||
|
r1089 | |||
|
r2439 | def unbundle(self, cg, heads, source): | ||
|
r2465 | # have to stream bundle to a temp file because we do not have | ||
# http 1.1 chunked transfer. | ||||
|
r3662 | type = "" | ||
types = self.capable('unbundle') | ||||
|
r3703 | # servers older than d1b16a746db6 will send 'unbundle' as a | ||
# boolean capability | ||||
try: | ||||
types = types.split(',') | ||||
except AttributeError: | ||||
types = [""] | ||||
|
r3662 | if types: | ||
|
r3703 | for x in types: | ||
|
r3662 | if x in changegroup.bundletypes: | ||
type = x | ||||
break | ||||
|
r3613 | |||
|
r3662 | tempname = changegroup.writebundle(cg, None, type) | ||
|
r7270 | fp = url.httpsendfile(tempname, "rb") | ||
|
r2465 | try: | ||
try: | ||||
|
r7010 | resp = self.do_read( | ||
'unbundle', data=fp, | ||||
headers={'Content-Type': 'application/octet-stream'}, | ||||
heads=' '.join(map(hex, heads))) | ||||
resp_code, output = resp.split('\n', 1) | ||||
|
r2467 | try: | ||
|
r7010 | ret = int(resp_code) | ||
except ValueError, err: | ||||
|
r7641 | raise error.ResponseError( | ||
|
r7010 | _('push failed (unexpected response):'), resp) | ||
self.ui.write(output) | ||||
return ret | ||||
|
r2467 | except socket.error, err: | ||
if err[0] in (errno.ECONNRESET, errno.EPIPE): | ||||
|
r3072 | raise util.Abort(_('push failed: %s') % err[1]) | ||
|
r2467 | raise util.Abort(err[1]) | ||
|
r2465 | finally: | ||
fp.close() | ||||
os.unlink(tempname) | ||||
|
r2439 | |||
|
r2612 | def stream_out(self): | ||
return self.do_cmd('stream_out') | ||||
|
r1089 | class httpsrepository(httprepository): | ||
|
r2569 | def __init__(self, ui, path): | ||
|
r7279 | if not url.has_https: | ||
|
r2569 | raise util.Abort(_('Python support for SSL and HTTPS ' | ||
'is not installed')) | ||||
httprepository.__init__(self, ui, path) | ||||
|
r2740 | |||
def instance(ui, path, create): | ||||
if create: | ||||
raise util.Abort(_('cannot create new http repository')) | ||||
|
r7211 | try: | ||
if path.startswith('https:'): | ||||
inst = httpsrepository(ui, path) | ||||
else: | ||||
inst = httprepository(ui, path) | ||||
inst.between([(nullid, nullid)]) | ||||
return inst | ||||
|
r7637 | except error.RepoError: | ||
|
r7211 | ui.note('(falling back to static-http)\n') | ||
return statichttprepo.instance(ui, "static-" + path, create) | ||||