httppeer.py
503 lines
| 17.9 KiB
| text/x-python
|
PythonLexer
/ mercurial / httppeer.py
|
r17192 | # httppeer.py - HTTP repository proxy classes for mercurial | ||
# | ||||
# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | ||||
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
|
r25954 | from __future__ import absolute_import | ||
import errno | ||||
|
r33820 | import io | ||
|
r25954 | import os | ||
import socket | ||||
|
r30763 | import struct | ||
|
r21074 | import tempfile | ||
|
r25954 | |||
from .i18n import _ | ||||
from . import ( | ||||
|
r28666 | bundle2, | ||
|
r25954 | error, | ||
httpconnection, | ||||
|
r30924 | pycompat, | ||
|
r25954 | statichttprepo, | ||
url, | ||||
util, | ||||
wireproto, | ||||
) | ||||
|
r17192 | |||
|
r29455 | httplib = util.httplib | ||
|
r28883 | urlerr = util.urlerr | ||
urlreq = util.urlreq | ||||
|
r30759 | def encodevalueinheaders(value, header, limit): | ||
"""Encode a string value into multiple HTTP headers. | ||||
``value`` will be encoded into 1 or more HTTP headers with the names | ||||
``header-<N>`` where ``<N>`` is an integer starting at 1. Each header | ||||
name + value will be at most ``limit`` bytes long. | ||||
|
r34733 | Returns an iterable of 2-tuples consisting of header names and | ||
values as native strings. | ||||
|
r30759 | """ | ||
|
r34733 | # HTTP Headers are ASCII. Python 3 requires them to be unicodes, | ||
# not bytes. This function always takes bytes in as arguments. | ||||
fmt = pycompat.strurl(header) + r'-%s' | ||||
# Note: it is *NOT* a bug that the last bit here is a bytestring | ||||
# and not a unicode: we're just getting the encoded length anyway, | ||||
# and using an r-string to make it portable between Python 2 and 3 | ||||
# doesn't work because then the \r is a literal backslash-r | ||||
# instead of a carriage return. | ||||
valuelen = limit - len(fmt % r'000') - len(': \r\n') | ||||
|
r30759 | result = [] | ||
n = 0 | ||||
for i in xrange(0, len(value), valuelen): | ||||
n += 1 | ||||
|
r34733 | result.append((fmt % str(n), pycompat.strurl(value[i:i + valuelen]))) | ||
|
r30759 | |||
return result | ||||
|
r32002 | def _wraphttpresponse(resp): | ||
"""Wrap an HTTPResponse with common error handlers. | ||||
This ensures that any I/O from any consumer raises the appropriate | ||||
error and messaging. | ||||
""" | ||||
origread = resp.read | ||||
class readerproxy(resp.__class__): | ||||
def read(self, size=None): | ||||
try: | ||||
return origread(size) | ||||
except httplib.IncompleteRead as e: | ||||
# e.expected is an integer if length known or None otherwise. | ||||
if e.expected: | ||||
msg = _('HTTP request error (incomplete response; ' | ||||
'expected %d bytes got %d)') % (e.expected, | ||||
len(e.partial)) | ||||
else: | ||||
msg = _('HTTP request error (incomplete response)') | ||||
|
r32023 | raise error.PeerTransportError( | ||
|
r32002 | msg, | ||
hint=_('this may be an intermittent network failure; ' | ||||
'if the error persists, consider contacting the ' | ||||
'network or server operator')) | ||||
except httplib.HTTPException as e: | ||||
|
r32023 | raise error.PeerTransportError( | ||
|
r32002 | _('HTTP request error (%s)') % e, | ||
|
r32087 | hint=_('this may be an intermittent network failure; ' | ||
|
r32002 | 'if the error persists, consider contacting the ' | ||
'network or server operator')) | ||||
resp.__class__ = readerproxy | ||||
|
r33820 | class _multifile(object): | ||
def __init__(self, *fileobjs): | ||||
for f in fileobjs: | ||||
if not util.safehasattr(f, 'length'): | ||||
raise ValueError( | ||||
'_multifile only supports file objects that ' | ||||
'have a length but this one does not:', type(f), f) | ||||
self._fileobjs = fileobjs | ||||
self._index = 0 | ||||
@property | ||||
def length(self): | ||||
return sum(f.length for f in self._fileobjs) | ||||
def read(self, amt=None): | ||||
if amt <= 0: | ||||
return ''.join(f.read() for f in self._fileobjs) | ||||
parts = [] | ||||
while amt and self._index < len(self._fileobjs): | ||||
parts.append(self._fileobjs[self._index].read(amt)) | ||||
got = len(parts[-1]) | ||||
if got < amt: | ||||
self._index += 1 | ||||
amt -= got | ||||
return ''.join(parts) | ||||
def seek(self, offset, whence=os.SEEK_SET): | ||||
if whence != os.SEEK_SET: | ||||
raise NotImplementedError( | ||||
'_multifile does not support anything other' | ||||
' than os.SEEK_SET for whence on seek()') | ||||
if offset != 0: | ||||
raise NotImplementedError( | ||||
'_multifile only supports seeking to start, but that ' | ||||
'could be fixed if you need it') | ||||
for f in self._fileobjs: | ||||
f.seek(0) | ||||
self._index = 0 | ||||
|
r33805 | class httppeer(wireproto.wirepeer): | ||
|
r17192 | def __init__(self, ui, path): | ||
|
r33671 | self._path = path | ||
self._caps = None | ||||
self._urlopener = None | ||||
self._requestbuilder = None | ||||
|
r17192 | u = util.url(path) | ||
if u.query or u.fragment: | ||||
|
r26587 | raise error.Abort(_('unsupported URL component: "%s"') % | ||
|
r17192 | (u.query or u.fragment)) | ||
# urllib cannot handle URLs with embedded user or passwd | ||||
self._url, authinfo = u.authinfo() | ||||
|
r33804 | self._ui = ui | ||
ui.debug('using %s\n' % self._url) | ||||
|
r17192 | |||
|
r33671 | self._urlopener = url.opener(ui, authinfo) | ||
self._requestbuilder = urlreq.request | ||||
|
r17192 | |||
def __del__(self): | ||||
|
r33671 | urlopener = getattr(self, '_urlopener', None) | ||
|
r30241 | if urlopener: | ||
for h in urlopener.handlers: | ||||
|
r17192 | h.close() | ||
|
r34487 | getattr(h, "close_all", lambda: None)() | ||
|
r17192 | |||
|
r35715 | def _openurl(self, req): | ||
|
r35716 | if (self._ui.debugflag | ||
and self._ui.configbool('devel', 'debug.peer-request')): | ||||
dbg = self._ui.debug | ||||
line = 'devel-peer-request: %s\n' | ||||
dbg(line % '%s %s' % (req.get_method(), req.get_full_url())) | ||||
hgargssize = None | ||||
for header, value in sorted(req.header_items()): | ||||
if header.startswith('X-hgarg-'): | ||||
if hgargssize is None: | ||||
hgargssize = 0 | ||||
hgargssize += len(value) | ||||
else: | ||||
dbg(line % ' %s %s' % (header, value)) | ||||
if hgargssize is not None: | ||||
dbg(line % ' %d bytes of commands arguments in headers' | ||||
% hgargssize) | ||||
if req.has_data(): | ||||
data = req.get_data() | ||||
length = getattr(data, 'length', None) | ||||
if length is None: | ||||
length = len(data) | ||||
dbg(line % ' %d bytes of data' % length) | ||||
start = util.timer() | ||||
ret = self._urlopener.open(req) | ||||
if self._ui.configbool('devel', 'debug.peer-request'): | ||||
dbg(line % ' finished in %.4f seconds (%s)' | ||||
% (util.timer() - start, ret.code)) | ||||
return ret | ||||
|
r35715 | |||
|
r33804 | # Begin of _basepeer interface. | ||
@util.propertycache | ||||
def ui(self): | ||||
return self._ui | ||||
|
r17192 | def url(self): | ||
|
r33671 | return self._path | ||
|
r17192 | |||
|
r33804 | def local(self): | ||
return None | ||||
def peer(self): | ||||
return self | ||||
def canpush(self): | ||||
return True | ||||
|
r17192 | |||
|
r33804 | def close(self): | ||
pass | ||||
|
r17192 | |||
|
r33804 | # End of _basepeer interface. | ||
# Begin of _basewirepeer interface. | ||||
def capabilities(self): | ||||
|
r36237 | # self._fetchcaps() should have been called as part of peer | ||
# handshake. So self._caps should always be set. | ||||
assert self._caps is not None | ||||
|
r33671 | return self._caps | ||
|
r17192 | |||
|
r33804 | # End of _basewirepeer interface. | ||
# look up capabilities only when needed | ||||
def _fetchcaps(self): | ||||
self._caps = set(self._call('capabilities').split()) | ||||
|
r30464 | def _callstream(self, cmd, _compressible=False, **args): | ||
|
r35360 | args = pycompat.byteskwargs(args) | ||
|
r17192 | if cmd == 'pushkey': | ||
args['data'] = '' | ||||
data = args.pop('data', None) | ||||
headers = args.pop('headers', {}) | ||||
self.ui.debug("sending %s command\n" % cmd) | ||||
q = [('cmd', cmd)] | ||||
headersize = 0 | ||||
|
r30564 | varyheaders = [] | ||
|
r28530 | # Important: don't use self.capable() here or else you end up | ||
# with infinite recursion when trying to look up capabilities | ||||
# for the first time. | ||||
|
r33671 | postargsok = self._caps is not None and 'httppostargs' in self._caps | ||
|
r36236 | |||
# Send arguments via POST. | ||||
|
r33820 | if postargsok and args: | ||
|
r28883 | strargs = urlreq.urlencode(sorted(args.items())) | ||
|
r33820 | if not data: | ||
data = strargs | ||||
else: | ||||
|
r35198 | if isinstance(data, bytes): | ||
|
r33820 | i = io.BytesIO(data) | ||
i.length = len(data) | ||||
data = i | ||||
argsio = io.BytesIO(strargs) | ||||
argsio.length = len(strargs) | ||||
data = _multifile(argsio, data) | ||||
|
r34702 | headers[r'X-HgArgs-Post'] = len(strargs) | ||
|
r36236 | elif args: | ||
# Calling self.capable() can infinite loop if we are calling | ||||
# "capabilities". But that command should never accept wire | ||||
# protocol arguments. So this should never happen. | ||||
assert cmd != 'capabilities' | ||||
httpheader = self.capable('httpheader') | ||||
if httpheader: | ||||
headersize = int(httpheader.split(',', 1)[0]) | ||||
# Send arguments via HTTP headers. | ||||
|
r28485 | if headersize > 0: | ||
# The headers can typically carry more data than the URL. | ||||
|
r28883 | encargs = urlreq.urlencode(sorted(args.items())) | ||
|
r30759 | for header, value in encodevalueinheaders(encargs, 'X-HgArg', | ||
headersize): | ||||
headers[header] = value | ||||
|
r28486 | varyheaders.append(header) | ||
|
r36236 | # Send arguments via query string (Mercurial <1.9). | ||
|
r28485 | else: | ||
q += sorted(args.items()) | ||||
|
r36236 | |||
|
r28883 | qs = '?%s' % urlreq.urlencode(q) | ||
|
r17192 | cu = "%s%s" % (self._url, qs) | ||
|
r28484 | size = 0 | ||
if util.safehasattr(data, 'length'): | ||||
size = data.length | ||||
elif data is not None: | ||||
size = len(data) | ||||
|
r34702 | if data is not None and r'Content-Type' not in headers: | ||
headers[r'Content-Type'] = r'application/mercurial-0.1' | ||||
|
r30564 | |||
|
r30763 | # Tell the server we accept application/mercurial-0.2 and multiple | ||
# compression formats if the server is capable of emitting those | ||||
# payloads. | ||||
protoparams = [] | ||||
mediatypes = set() | ||||
|
r33671 | if self._caps is not None: | ||
|
r30763 | mt = self.capable('httpmediatype') | ||
if mt: | ||||
protoparams.append('0.1') | ||||
mediatypes = set(mt.split(',')) | ||||
if '0.2tx' in mediatypes: | ||||
protoparams.append('0.2') | ||||
if '0.2tx' in mediatypes and self.capable('compression'): | ||||
# We /could/ compare supported compression formats and prune | ||||
# non-mutually supported or error if nothing is mutually supported. | ||||
# For now, send the full list to the server and have it error. | ||||
comps = [e.wireprotosupport().name for e in | ||||
util.compengines.supportedwireengines(util.CLIENTROLE)] | ||||
protoparams.append('comp=%s' % ','.join(comps)) | ||||
if protoparams: | ||||
protoheaders = encodevalueinheaders(' '.join(protoparams), | ||||
'X-HgProto', | ||||
headersize or 1024) | ||||
for header, value in protoheaders: | ||||
headers[header] = value | ||||
varyheaders.append(header) | ||||
|
r32022 | if varyheaders: | ||
|
r34702 | headers[r'Vary'] = r','.join(varyheaders) | ||
|
r32022 | |||
|
r34700 | req = self._requestbuilder(pycompat.strurl(cu), data, headers) | ||
|
r30564 | |||
|
r17192 | if data is not None: | ||
|
r36284 | self.ui.debug("sending %d bytes\n" % size) | ||
|
r36311 | req.add_unredirected_header(r'Content-Length', r'%d' % size) | ||
|
r17192 | try: | ||
|
r35715 | resp = self._openurl(req) | ||
|
r28883 | except urlerr.httperror as inst: | ||
|
r17192 | if inst.code == 401: | ||
|
r26587 | raise error.Abort(_('authorization failed')) | ||
|
r17192 | raise | ||
|
r25660 | except httplib.HTTPException as inst: | ||
|
r17192 | self.ui.debug('http error while sending %s command\n' % cmd) | ||
self.ui.traceback() | ||||
raise IOError(None, inst) | ||||
|
r32002 | |||
# Insert error handlers for common I/O failures. | ||||
_wraphttpresponse(resp) | ||||
|
r17192 | # record the url we got redirected to | ||
|
r34725 | resp_url = pycompat.bytesurl(resp.geturl()) | ||
|
r17192 | if resp_url.endswith(qs): | ||
resp_url = resp_url[:-len(qs)] | ||||
if self._url.rstrip('/') != resp_url.rstrip('/'): | ||||
if not self.ui.quiet: | ||||
self.ui.warn(_('real URL is %s\n') % resp_url) | ||||
self._url = resp_url | ||||
try: | ||||
|
r34726 | proto = pycompat.bytesurl(resp.getheader(r'content-type', r'')) | ||
|
r17192 | except AttributeError: | ||
|
r34726 | proto = pycompat.bytesurl(resp.headers.get(r'content-type', r'')) | ||
|
r17192 | |||
safeurl = util.hidepassword(self._url) | ||||
if proto.startswith('application/hg-error'): | ||||
raise error.OutOfBandError(resp.read()) | ||||
# accept old "text/plain" and "application/hg-changegroup" for now | ||||
if not (proto.startswith('application/mercurial-') or | ||||
|
r18737 | (proto.startswith('text/plain') | ||
and not resp.headers.get('content-length')) or | ||||
|
r17192 | proto.startswith('application/hg-changegroup')): | ||
self.ui.debug("requested URL: '%s'\n" % util.hidepassword(cu)) | ||||
raise error.RepoError( | ||||
_("'%s' does not appear to be an hg repository:\n" | ||||
"---%%<--- (%s)\n%s\n---%%<---\n") | ||||
|
r18738 | % (safeurl, proto or 'no content-type', resp.read(1024))) | ||
|
r17192 | |||
if proto.startswith('application/mercurial-'): | ||||
try: | ||||
version = proto.split('-', 1)[1] | ||||
version_info = tuple([int(n) for n in version.split('.')]) | ||||
except ValueError: | ||||
raise error.RepoError(_("'%s' sent a broken Content-Type " | ||||
"header (%s)") % (safeurl, proto)) | ||||
|
r30763 | |||
|
r32003 | # TODO consider switching to a decompression reader that uses | ||
# generators. | ||||
|
r30763 | if version_info == (0, 1): | ||
if _compressible: | ||||
|
r32003 | return util.compengines['zlib'].decompressorreader(resp) | ||
|
r30763 | return resp | ||
elif version_info == (0, 2): | ||||
# application/mercurial-0.2 always identifies the compression | ||||
# engine in the payload header. | ||||
elen = struct.unpack('B', resp.read(1))[0] | ||||
ename = resp.read(elen) | ||||
engine = util.compengines.forwiretype(ename) | ||||
|
r32003 | return engine.decompressorreader(resp) | ||
|
r30763 | else: | ||
|
r17192 | raise error.RepoError(_("'%s' uses newer protocol %s") % | ||
(safeurl, version)) | ||||
|
r30464 | if _compressible: | ||
|
r32003 | return util.compengines['zlib'].decompressorreader(resp) | ||
|
r30464 | |||
|
r17192 | return resp | ||
def _call(self, cmd, **args): | ||||
fp = self._callstream(cmd, **args) | ||||
try: | ||||
return fp.read() | ||||
finally: | ||||
# if using keepalive, allow connection to be reused | ||||
fp.close() | ||||
def _callpush(self, cmd, cg, **args): | ||||
# have to stream bundle to a temp file because we do not have | ||||
# http 1.1 chunked transfer. | ||||
types = self.capable('unbundle') | ||||
try: | ||||
types = types.split(',') | ||||
except AttributeError: | ||||
# servers older than d1b16a746db6 will send 'unbundle' as a | ||||
# boolean capability. They only support headerless/uncompressed | ||||
# bundles. | ||||
types = [""] | ||||
for x in types: | ||||
|
r28666 | if x in bundle2.bundletypes: | ||
|
r17192 | type = x | ||
break | ||||
|
r28666 | tempname = bundle2.writebundle(self.ui, cg, None, type) | ||
|
r17192 | fp = httpconnection.httpsendfile(self.ui, tempname, "rb") | ||
|
r36311 | headers = {r'Content-Type': r'application/mercurial-0.1'} | ||
|
r17192 | |||
try: | ||||
|
r25085 | r = self._call(cmd, data=fp, headers=headers, **args) | ||
vals = r.split('\n', 1) | ||||
if len(vals) < 2: | ||||
raise error.ResponseError(_("unexpected response:"), r) | ||||
return vals | ||||
|
r36448 | except urlerr.httperror: | ||
# Catch and re-raise these so we don't try and treat them | ||||
# like generic socket errors. They lack any values in | ||||
# .args on Python 3 which breaks our socket.error block. | ||||
raise | ||||
|
r25660 | except socket.error as err: | ||
|
r25085 | if err.args[0] in (errno.ECONNRESET, errno.EPIPE): | ||
|
r26587 | raise error.Abort(_('push failed: %s') % err.args[1]) | ||
raise error.Abort(err.args[1]) | ||||
|
r17192 | finally: | ||
fp.close() | ||||
os.unlink(tempname) | ||||
|
r21074 | def _calltwowaystream(self, cmd, fp, **args): | ||
fh = None | ||||
|
r23086 | fp_ = None | ||
|
r21074 | filename = None | ||
try: | ||||
# dump bundle to disk | ||||
fd, filename = tempfile.mkstemp(prefix="hg-bundle-", suffix=".hg") | ||||
|
r30924 | fh = os.fdopen(fd, pycompat.sysstr("wb")) | ||
|
r21074 | d = fp.read(4096) | ||
while d: | ||||
fh.write(d) | ||||
d = fp.read(4096) | ||||
fh.close() | ||||
# start http push | ||||
|
r23086 | fp_ = httpconnection.httpsendfile(self.ui, filename, "rb") | ||
|
r36311 | headers = {r'Content-Type': r'application/mercurial-0.1'} | ||
|
r23086 | return self._callstream(cmd, data=fp_, headers=headers, **args) | ||
|
r21074 | finally: | ||
|
r23086 | if fp_ is not None: | ||
fp_.close() | ||||
|
r21074 | if fh is not None: | ||
fh.close() | ||||
os.unlink(filename) | ||||
|
r20905 | def _callcompressable(self, cmd, **args): | ||
|
r30464 | return self._callstream(cmd, _compressible=True, **args) | ||
|
r17192 | |||
|
r21188 | def _abort(self, exception): | ||
raise exception | ||||
|
r17192 | def instance(ui, path, create): | ||
if create: | ||||
|
r26587 | raise error.Abort(_('cannot create new http repository')) | ||
|
r17192 | try: | ||
|
r36238 | if path.startswith('https:') and not url.has_https: | ||
raise error.Abort(_('Python support for SSL and HTTPS ' | ||||
'is not installed')) | ||||
|
r35902 | |||
|
r36238 | inst = httppeer(ui, path) | ||
|
r35902 | inst._fetchcaps() | ||
|
r17192 | return inst | ||
|
r25660 | except error.RepoError as httpexception: | ||
|
r17192 | try: | ||
r = statichttprepo.instance(ui, "static-" + path, create) | ||||
|
r29241 | ui.note(_('(falling back to static-http)\n')) | ||
|
r17192 | return r | ||
except error.RepoError: | ||||
raise httpexception # use the original http RepoError instead | ||||