httprepo.py
244 lines
| 8.8 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 | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r1089 | |||
|
r12062 | from node import nullid | ||
|
r3891 | from i18n import _ | ||
|
r14244 | import changegroup, statichttprepo, error, httpconnection, url, util, wireproto | ||
|
r13819 | import os, urllib, urllib2, zlib, httplib | ||
|
r8312 | import errno, socket | ||
|
r4678 | |||
|
r3661 | def zgenerator(f): | ||
zd = zlib.decompressobj() | ||||
try: | ||||
for chunk in util.filechunkiter(f): | ||||
|
r11757 | while chunk: | ||
yield zd.decompress(chunk, 2**18) | ||||
chunk = zd.unconsumed_tail | ||||
|
r7280 | except httplib.HTTPException: | ||
|
r3661 | raise IOError(None, _('connection ended unexpectedly')) | ||
yield zd.flush() | ||||
|
r11587 | class httprepository(wireproto.wirerepository): | ||
|
r1089 | def __init__(self, ui, path): | ||
|
r2673 | self.path = path | ||
|
r2442 | self.caps = None | ||
|
r4132 | self.handler = None | ||
|
r14076 | u = util.url(path) | ||
|
r13819 | if u.query or u.fragment: | ||
|
r2337 | raise util.Abort(_('unsupported URL component: "%s"') % | ||
|
r13819 | (u.query or u.fragment)) | ||
|
r2337 | |||
# urllib cannot handle URLs with embedded user or passwd | ||||
|
r13819 | self._url, authinfo = u.authinfo() | ||
|
r7270 | |||
|
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 | ||
|
r13603 | def _fetchcaps(self): | ||
self.caps = set(self._call('capabilities').split()) | ||||
|
r2442 | def get_caps(self): | ||
if self.caps is None: | ||||
try: | ||||
|
r13603 | self._fetchcaps() | ||
|
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')) | ||||
|
r11589 | def _callstream(self, cmd, **args): | ||
|
r13006 | if cmd == 'pushkey': | ||
|
r12969 | args['data'] = '' | ||
|
r2465 | data = args.pop('data', None) | ||
|
r15152 | size = 0 | ||
if util.safehasattr(data, 'length'): | ||||
size = data.length | ||||
elif data is not None: | ||||
size = len(data) | ||||
|
r2465 | headers = args.pop('headers', {}) | ||
|
r14245 | |||
|
r15152 | if size and self.ui.configbool('ui', 'usehttp2', False): | ||
|
r14245 | headers['Expect'] = '100-Continue' | ||
|
r14991 | headers['X-HgHttp2'] = '1' | ||
|
r14245 | |||
|
r9467 | self.ui.debug("sending %s command\n" % cmd) | ||
|
r14093 | q = [('cmd', cmd)] | ||
headersize = 0 | ||||
if len(args) > 0: | ||||
httpheader = self.capable('httpheader') | ||||
if httpheader: | ||||
headersize = int(httpheader.split(',')[0]) | ||||
if headersize > 0: | ||||
# The headers can typically carry more data than the URL. | ||||
encargs = urllib.urlencode(sorted(args.items())) | ||||
|
r14094 | headerfmt = 'X-HgArg-%s' | ||
|
r14093 | contentlen = headersize - len(headerfmt % '000' + ': \r\n') | ||
headernum = 0 | ||||
for i in xrange(0, len(encargs), contentlen): | ||||
headernum += 1 | ||||
header = headerfmt % str(headernum) | ||||
headers[header] = encargs[i:i + contentlen] | ||||
varyheaders = [headerfmt % str(h) for h in range(1, headernum + 1)] | ||||
headers['Vary'] = ','.join(varyheaders) | ||||
else: | ||||
q += sorted(args.items()) | ||||
|
r3562 | qs = '?%s' % urllib.urlencode(q) | ||
cu = "%s%s" % (self._url, qs) | ||||
|
r10491 | req = urllib2.Request(cu, data, headers) | ||
if data is not None: | ||||
self.ui.debug("sending %s bytes\n" % size) | ||||
req.add_unredirected_header('Content-Length', '%d' % size) | ||||
|
r2294 | try: | ||
|
r10491 | resp = self.urlopener.open(req) | ||
|
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)] | ||||
|
r9881 | if self._url.rstrip('/') != resp_url.rstrip('/'): | ||
|
r14504 | if not self.ui.quiet: | ||
self.ui.warn(_('real URL is %s\n') % resp_url) | ||||
|
r10208 | self._url = resp_url | ||
|
r2435 | try: | ||
proto = resp.getheader('content-type') | ||||
except AttributeError: | ||||
|
r14503 | proto = resp.headers.get('content-type', '') | ||
|
r1089 | |||
|
r14076 | safeurl = util.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')): | ||||
|
r14076 | self.ui.debug("requested URL: '%s'\n" % util.hidepassword(cu)) | ||
|
r10282 | raise error.RepoError( | ||
_("'%s' does not appear to be an hg repository:\n" | ||||
"---%%<--- (%s)\n%s\n---%%<---\n") | ||||
|
r14503 | % (safeurl, proto or 'no content-type', resp.read())) | ||
|
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 | ||||
|
r11589 | def _call(self, cmd, **args): | ||
fp = self._callstream(cmd, **args) | ||||
|
r2435 | try: | ||
return fp.read() | ||||
finally: | ||||
# if using keepalive, allow connection to be reused | ||||
fp.close() | ||||
|
r11592 | def _callpush(self, cmd, cg, **args): | ||
|
r2465 | # have to stream bundle to a temp file because we do not have | ||
# http 1.1 chunked transfer. | ||||
|
r3662 | types = self.capable('unbundle') | ||
|
r3703 | try: | ||
types = types.split(',') | ||||
except AttributeError: | ||||
|
r14060 | # servers older than d1b16a746db6 will send 'unbundle' as a | ||
# boolean capability. They only support headerless/uncompressed | ||||
# bundles. | ||||
|
r3703 | types = [""] | ||
|
r14060 | for x in types: | ||
if x in changegroup.bundletypes: | ||||
type = x | ||||
break | ||||
|
r3613 | |||
|
r3662 | tempname = changegroup.writebundle(cg, None, type) | ||
|
r14244 | fp = httpconnection.httpsendfile(self.ui, tempname, "rb") | ||
|
r11592 | headers = {'Content-Type': 'application/mercurial-0.1'} | ||
|
r2465 | try: | ||
try: | ||||
|
r11592 | r = self._call(cmd, data=fp, headers=headers, **args) | ||
|
r14641 | vals = r.split('\n', 1) | ||
if len(vals) < 2: | ||||
|
r14648 | raise error.ResponseError(_("unexpected response:"), r) | ||
|
r14641 | return vals | ||
|
r2467 | except socket.error, err: | ||
|
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]) | ||||
|
r2465 | finally: | ||
fp.close() | ||||
os.unlink(tempname) | ||||
|
r2439 | |||
|
r11592 | def _abort(self, exception): | ||
raise exception | ||||
|
r2612 | |||
|
r11592 | def _decompress(self, stream): | ||
return util.chunkbuffer(zgenerator(stream)) | ||||
|
r11370 | |||
|
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) | ||||
|
r13603 | try: | ||
# Try to do useful work when checking compatibility. | ||||
# Usually saves a roundtrip since we want the caps anyway. | ||||
inst._fetchcaps() | ||||
except error.RepoError: | ||||
# No luck, try older compatibility check. | ||||
inst.between([(nullid, nullid)]) | ||||
|
r7211 | return inst | ||
|
r14148 | except error.RepoError, httpexception: | ||
try: | ||||
r = statichttprepo.instance(ui, "static-" + path, create) | ||||
ui.note('(falling back to static-http)\n') | ||||
return r | ||||
except error.RepoError: | ||||
raise httpexception # use the original http RepoError instead | ||||