##// END OF EJS Templates
httppeer: perform capabilities request in makepeer()...
httppeer: perform capabilities request in makepeer() Previously, we constructed an httppeer then always ran _fetchcaps() to issue the capabilities command. We want to issue the capabilities command before constructing a peer instance so we can construct an appropriate peer instance depending on the capabilities result. With the code for making and sending requests moved out of httppeer, it is now possible to send command requests without an httppeer. This commit creates a new function for making the capabilities request and calls it as part of makepeer(). This code should be functionality equivalent to what existed before. Differential Revision: https://phab.mercurial-scm.org/D3237

File last commit:

r37570:8b8a845c default
r37570:8b8a845c default
Show More
httppeer.py
661 lines | 22.2 KiB | text/x-python | PythonLexer
Peter Arrenbrecht
peer: introduce real peer classes...
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.
Gregory Szorc
httppeer: use absolute_import
r25954 from __future__ import absolute_import
import errno
Augie Fackler
httppeer: add support for httppostargs when we're sending a file...
r33820 import io
Gregory Szorc
httppeer: use absolute_import
r25954 import os
import socket
Gregory Szorc
httppeer: advertise and support application/mercurial-0.2...
r30763 import struct
Pierre-Yves David
httppeer: support for _calltwowaystream...
r21074 import tempfile
Gregory Szorc
httppeer: use absolute_import
r25954
from .i18n import _
Gregory Szorc
wireproto: crude support for version 2 HTTP peer...
r37501 from .thirdparty import (
cbor,
)
Gregory Szorc
httppeer: use absolute_import
r25954 from . import (
Martin von Zweigbergk
bundle: move writebundle() from changegroup.py to bundle2.py (API)...
r28666 bundle2,
Gregory Szorc
httppeer: use absolute_import
r25954 error,
httpconnection,
Pulkit Goyal
py3: convert the mode argument of os.fdopen to unicodes (1 of 2)...
r30924 pycompat,
Gregory Szorc
httppeer: use absolute_import
r25954 statichttprepo,
Gregory Szorc
httppeer: alias url as urlmod...
r36977 url as urlmod,
Gregory Szorc
httppeer: use absolute_import
r25954 util,
wireproto,
Gregory Szorc
wireproto: crude support for version 2 HTTP peer...
r37501 wireprotoframing,
Gregory Szorc
wireproto: extract HTTP version 2 code to own module...
r37563 wireprotov2server,
Gregory Szorc
httppeer: use absolute_import
r25954 )
Peter Arrenbrecht
peer: introduce real peer classes...
r17192
Pulkit Goyal
py3: conditionalize httplib import...
r29455 httplib = util.httplib
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 urlerr = util.urlerr
urlreq = util.urlreq
Gregory Szorc
httppeer: extract code for HTTP header spanning...
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.
Augie Fackler
httppeer: always produce native str header keys and values...
r34733 Returns an iterable of 2-tuples consisting of header names and
values as native strings.
Gregory Szorc
httppeer: extract code for HTTP header spanning...
r30759 """
Augie Fackler
httppeer: always produce native str header keys and values...
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')
Gregory Szorc
httppeer: extract code for HTTP header spanning...
r30759 result = []
n = 0
for i in xrange(0, len(value), valuelen):
n += 1
Augie Fackler
httppeer: always produce native str header keys and values...
r34733 result.append((fmt % str(n), pycompat.strurl(value[i:i + valuelen])))
Gregory Szorc
httppeer: extract code for HTTP header spanning...
r30759
return result
Gregory Szorc
httppeer: wrap HTTPResponse.read() globally...
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)')
Gregory Szorc
error: rename RichIOError to PeerTransportError...
r32023 raise error.PeerTransportError(
Gregory Szorc
httppeer: wrap HTTPResponse.read() globally...
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:
Gregory Szorc
error: rename RichIOError to PeerTransportError...
r32023 raise error.PeerTransportError(
Gregory Szorc
httppeer: wrap HTTPResponse.read() globally...
r32002 _('HTTP request error (%s)') % e,
FUJIWARA Katsunori
httppeer: unify hint message for PeerTransportError...
r32087 hint=_('this may be an intermittent network failure; '
Gregory Szorc
httppeer: wrap HTTPResponse.read() globally...
r32002 'if the error persists, consider contacting the '
'network or server operator'))
resp.__class__ = readerproxy
Augie Fackler
httppeer: add support for httppostargs when we're sending a file...
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
Gregory Szorc
httppeer: extract code for creating a request into own function...
r37567 def makev1commandrequest(ui, requestbuilder, caps, capablefn,
repobaseurl, cmd, args):
"""Make an HTTP request to run a command for a version 1 client.
``caps`` is a set of known server capabilities. The value may be
None if capabilities are not yet known.
``capablefn`` is a function to evaluate a capability.
``cmd``, ``args``, and ``data`` define the command, its arguments, and
raw data to pass to it.
"""
if cmd == 'pushkey':
args['data'] = ''
data = args.pop('data', None)
headers = args.pop('headers', {})
ui.debug("sending %s command\n" % cmd)
q = [('cmd', cmd)]
headersize = 0
varyheaders = []
# 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.
postargsok = caps is not None and 'httppostargs' in caps
# Send arguments via POST.
if postargsok and args:
strargs = urlreq.urlencode(sorted(args.items()))
if not data:
data = strargs
else:
if isinstance(data, bytes):
i = io.BytesIO(data)
i.length = len(data)
data = i
argsio = io.BytesIO(strargs)
argsio.length = len(strargs)
data = _multifile(argsio, data)
headers[r'X-HgArgs-Post'] = len(strargs)
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 = capablefn('httpheader')
if httpheader:
headersize = int(httpheader.split(',', 1)[0])
# Send arguments via HTTP headers.
if headersize > 0:
# The headers can typically carry more data than the URL.
encargs = urlreq.urlencode(sorted(args.items()))
for header, value in encodevalueinheaders(encargs, 'X-HgArg',
headersize):
headers[header] = value
varyheaders.append(header)
# Send arguments via query string (Mercurial <1.9).
else:
q += sorted(args.items())
qs = '?%s' % urlreq.urlencode(q)
cu = "%s%s" % (repobaseurl, qs)
size = 0
if util.safehasattr(data, 'length'):
size = data.length
elif data is not None:
size = len(data)
if data is not None and r'Content-Type' not in headers:
headers[r'Content-Type'] = r'application/mercurial-0.1'
# Tell the server we accept application/mercurial-0.2 and multiple
# compression formats if the server is capable of emitting those
# payloads.
protoparams = {'partial-pull'}
mediatypes = set()
if caps is not None:
mt = capablefn('httpmediatype')
if mt:
protoparams.add('0.1')
mediatypes = set(mt.split(','))
if '0.2tx' in mediatypes:
protoparams.add('0.2')
if '0.2tx' in mediatypes and capablefn('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.add('comp=%s' % ','.join(comps))
if protoparams:
protoheaders = encodevalueinheaders(' '.join(sorted(protoparams)),
'X-HgProto',
headersize or 1024)
for header, value in protoheaders:
headers[header] = value
varyheaders.append(header)
if varyheaders:
headers[r'Vary'] = r','.join(varyheaders)
req = requestbuilder(pycompat.strurl(cu), data, headers)
if data is not None:
ui.debug("sending %d bytes\n" % size)
req.add_unredirected_header(r'Content-Length', r'%d' % size)
return req, cu, qs
Gregory Szorc
httppeer: extract code for performing an HTTP request...
r37566 def sendrequest(ui, opener, req):
"""Send a prepared HTTP request.
Returns the response object.
"""
if (ui.debugflag
and ui.configbool('devel', 'debug.peer-request')):
dbg = 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()
Gregory Szorc
httppeer: move error handling and response wrapping into sendrequest...
r37568 try:
res = opener.open(req)
except urlerr.httperror as inst:
if inst.code == 401:
raise error.Abort(_('authorization failed'))
raise
except httplib.HTTPException as inst:
ui.debug('http error requesting %s\n' %
util.hidepassword(req.get_full_url()))
ui.traceback()
raise IOError(None, inst)
finally:
if ui.configbool('devel', 'debug.peer-request'):
dbg(line % ' finished in %.4f seconds (%s)'
% (util.timer() - start, res.code))
# Insert error handlers for common I/O failures.
_wraphttpresponse(res)
Gregory Szorc
httppeer: extract code for performing an HTTP request...
r37566
return res
Gregory Szorc
httppeer: extract common response handling into own function...
r37569 def parsev1commandresponse(ui, baseurl, requrl, qs, resp, compressible):
# record the url we got redirected to
respurl = pycompat.bytesurl(resp.geturl())
if respurl.endswith(qs):
respurl = respurl[:-len(qs)]
if baseurl.rstrip('/') != respurl.rstrip('/'):
if not ui.quiet:
ui.warn(_('real URL is %s\n') % respurl)
try:
proto = pycompat.bytesurl(resp.getheader(r'content-type', r''))
except AttributeError:
proto = pycompat.bytesurl(resp.headers.get(r'content-type', r''))
safeurl = util.hidepassword(baseurl)
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
(proto.startswith('text/plain')
and not resp.headers.get('content-length')) or
proto.startswith('application/hg-changegroup')):
ui.debug("requested URL: '%s'\n" % util.hidepassword(requrl))
raise error.RepoError(
_("'%s' does not appear to be an hg repository:\n"
"---%%<--- (%s)\n%s\n---%%<---\n")
% (safeurl, proto or 'no content-type', resp.read(1024)))
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))
# TODO consider switching to a decompression reader that uses
# generators.
if version_info == (0, 1):
if compressible:
resp = util.compengines['zlib'].decompressorreader(resp)
return respurl, 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)
return respurl, engine.decompressorreader(resp)
else:
raise error.RepoError(_("'%s' uses newer protocol %s") %
(safeurl, version))
if compressible:
resp = util.compengines['zlib'].decompressorreader(resp)
return respurl, resp
Gregory Szorc
wireproto: use new peer interface...
r33805 class httppeer(wireproto.wirepeer):
Gregory Szorc
httppeer: perform capabilities request in makepeer()...
r37570 def __init__(self, ui, path, url, opener, requestbuilder, caps):
Gregory Szorc
peer: make ui an attribute...
r37337 self.ui = ui
Gregory Szorc
httppeer: make several instance attributes internal (API)...
r33671 self._path = path
Gregory Szorc
httppeer: refactor how httppeer is created (API)...
r37024 self._url = url
Gregory Szorc
httppeer: perform capabilities request in makepeer()...
r37570 self._caps = caps
Gregory Szorc
httppeer: refactor how httppeer is created (API)...
r37024 self._urlopener = opener
Gregory Szorc
httppeer: move requestbuilder defaults into makepeer() argument...
r37565 self._requestbuilder = requestbuilder
Peter Arrenbrecht
peer: introduce real peer classes...
r17192
def __del__(self):
Gregory Szorc
httppeer: refactor how httppeer is created (API)...
r37024 for h in self._urlopener.handlers:
h.close()
getattr(h, "close_all", lambda: None)()
Peter Arrenbrecht
peer: introduce real peer classes...
r17192
Gregory Szorc
repository: port peer interfaces to zope.interface...
r37336 # Begin of ipeerconnection interface.
Gregory Szorc
httppeer: use peer interface...
r33804
Peter Arrenbrecht
peer: introduce real peer classes...
r17192 def url(self):
Gregory Szorc
httppeer: make several instance attributes internal (API)...
r33671 return self._path
Peter Arrenbrecht
peer: introduce real peer classes...
r17192
Gregory Szorc
httppeer: use peer interface...
r33804 def local(self):
return None
def peer(self):
return self
def canpush(self):
return True
Peter Arrenbrecht
peer: introduce real peer classes...
r17192
Gregory Szorc
httppeer: use peer interface...
r33804 def close(self):
pass
Peter Arrenbrecht
peer: introduce real peer classes...
r17192
Gregory Szorc
repository: port peer interfaces to zope.interface...
r37336 # End of ipeerconnection interface.
Gregory Szorc
httppeer: use peer interface...
r33804
Gregory Szorc
repository: port peer interfaces to zope.interface...
r37336 # Begin of ipeercommands interface.
Gregory Szorc
httppeer: use peer interface...
r33804
def capabilities(self):
Gregory Szorc
httppeer: make several instance attributes internal (API)...
r33671 return self._caps
Peter Arrenbrecht
peer: introduce real peer classes...
r17192
Gregory Szorc
repository: port peer interfaces to zope.interface...
r37336 # End of ipeercommands interface.
Gregory Szorc
httppeer: use peer interface...
r33804
# look up capabilities only when needed
Gregory Szorc
httppeer: do decompression inside _callstream...
r30464 def _callstream(self, cmd, _compressible=False, **args):
Pulkit Goyal
py3: handle keyword arguments correctly in httppeer.py...
r35360 args = pycompat.byteskwargs(args)
Gregory Szorc
httppeer: change logic around argument handling...
r36236
Gregory Szorc
httppeer: extract code for creating a request into own function...
r37567 req, cu, qs = makev1commandrequest(self.ui, self._requestbuilder,
self._caps, self.capable,
self._url, cmd, args)
Gregory Szorc
httppeer: advertise and support application/mercurial-0.2...
r30763
Gregory Szorc
httppeer: move error handling and response wrapping into sendrequest...
r37568 resp = sendrequest(self.ui, self._urlopener, req)
Gregory Szorc
httppeer: wrap HTTPResponse.read() globally...
r32002
Gregory Szorc
httppeer: extract common response handling into own function...
r37569 self._url, resp = parsev1commandresponse(self.ui, self._url, cu, qs,
resp, _compressible)
Gregory Szorc
httppeer: do decompression inside _callstream...
r30464
Peter Arrenbrecht
peer: introduce real peer classes...
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:
Martin von Zweigbergk
bundle: move writebundle() from changegroup.py to bundle2.py (API)...
r28666 if x in bundle2.bundletypes:
Peter Arrenbrecht
peer: introduce real peer classes...
r17192 type = x
break
Martin von Zweigbergk
bundle: move writebundle() from changegroup.py to bundle2.py (API)...
r28666 tempname = bundle2.writebundle(self.ui, cg, None, type)
Peter Arrenbrecht
peer: introduce real peer classes...
r17192 fp = httpconnection.httpsendfile(self.ui, tempname, "rb")
Augie Fackler
httppeer: headers are native strings...
r36311 headers = {r'Content-Type': r'application/mercurial-0.1'}
Peter Arrenbrecht
peer: introduce real peer classes...
r17192
try:
Matt Mackall
httppeer: use try/except/finally
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
Augie Fackler
httppeer: explicitly catch urlerr.httperror and re-raise...
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
Gregory Szorc
global: mass rewrite to use modern exception syntax...
r25660 except socket.error as err:
Matt Mackall
httppeer: use try/except/finally
r25085 if err.args[0] in (errno.ECONNRESET, errno.EPIPE):
Pierre-Yves David
error: get Abort from 'error' instead of 'util'...
r26587 raise error.Abort(_('push failed: %s') % err.args[1])
raise error.Abort(err.args[1])
Peter Arrenbrecht
peer: introduce real peer classes...
r17192 finally:
fp.close()
os.unlink(tempname)
Pierre-Yves David
httppeer: support for _calltwowaystream...
r21074 def _calltwowaystream(self, cmd, fp, **args):
fh = None
Matt Harbison
httppeer: close the temporary bundle file after two-way streaming it...
r23086 fp_ = None
Pierre-Yves David
httppeer: support for _calltwowaystream...
r21074 filename = None
try:
# dump bundle to disk
fd, filename = tempfile.mkstemp(prefix="hg-bundle-", suffix=".hg")
Yuya Nishihara
py3: use r'' instead of sysstr('') to get around code transformer...
r36853 fh = os.fdopen(fd, r"wb")
Pierre-Yves David
httppeer: support for _calltwowaystream...
r21074 d = fp.read(4096)
while d:
fh.write(d)
d = fp.read(4096)
fh.close()
# start http push
Matt Harbison
httppeer: close the temporary bundle file after two-way streaming it...
r23086 fp_ = httpconnection.httpsendfile(self.ui, filename, "rb")
Augie Fackler
httppeer: headers are native strings...
r36311 headers = {r'Content-Type': r'application/mercurial-0.1'}
Matt Harbison
httppeer: close the temporary bundle file after two-way streaming it...
r23086 return self._callstream(cmd, data=fp_, headers=headers, **args)
Pierre-Yves David
httppeer: support for _calltwowaystream...
r21074 finally:
Matt Harbison
httppeer: close the temporary bundle file after two-way streaming it...
r23086 if fp_ is not None:
fp_.close()
Pierre-Yves David
httppeer: support for _calltwowaystream...
r21074 if fh is not None:
fh.close()
os.unlink(filename)
Pierre-Yves David
wireproto: drop the _decompress method in favor a new call type...
r20905 def _callcompressable(self, cmd, **args):
Gregory Szorc
httppeer: do decompression inside _callstream...
r30464 return self._callstream(cmd, _compressible=True, **args)
Peter Arrenbrecht
peer: introduce real peer classes...
r17192
Mads Kiilerich
httppeer: reintroduce _abort that accidentally was removed in 167047ba3cfa...
r21188 def _abort(self, exception):
raise exception
Gregory Szorc
wireproto: crude support for version 2 HTTP peer...
r37501 # TODO implement interface for version 2 peers
class httpv2peer(object):
def __init__(self, ui, repourl, opener):
self.ui = ui
if repourl.endswith('/'):
repourl = repourl[:-1]
self.url = repourl
self._opener = opener
# This is an its own attribute to facilitate extensions overriding
# the default type.
self._requestbuilder = urlreq.request
def close(self):
pass
# TODO require to be part of a batched primitive, use futures.
def _call(self, name, **args):
"""Call a wire protocol command with arguments."""
Gregory Szorc
wireproto: move version 2 command handlers to wireprotov2server...
r37564 # Having this early has a side-effect of importing wireprotov2server,
# which has the side-effect of ensuring commands are registered.
# TODO modify user-agent to reflect v2.
headers = {
r'Accept': wireprotov2server.FRAMINGTYPE,
r'Content-Type': wireprotov2server.FRAMINGTYPE,
}
Gregory Szorc
wireproto: crude support for version 2 HTTP peer...
r37501 # TODO permissions should come from capabilities results.
permission = wireproto.commandsv2[name].permission
if permission not in ('push', 'pull'):
raise error.ProgrammingError('unknown permission type: %s' %
permission)
permission = {
'push': 'rw',
'pull': 'ro',
}[permission]
Gregory Szorc
wireproto: extract HTTP version 2 code to own module...
r37563 url = '%s/api/%s/%s/%s' % (self.url, wireprotov2server.HTTPV2,
permission, name)
Gregory Szorc
wireproto: crude support for version 2 HTTP peer...
r37501
# TODO this should be part of a generic peer for the frame-based
# protocol.
Gregory Szorc
wireproto: introduce a reactor for client-side state...
r37561 reactor = wireprotoframing.clientreactor(hasmultiplesend=False,
buffersends=True)
Gregory Szorc
wireproto: crude support for version 2 HTTP peer...
r37501
Gregory Szorc
wireproto: introduce a reactor for client-side state...
r37561 request, action, meta = reactor.callcommand(name, args)
assert action == 'noop'
action, meta = reactor.flushcommands()
assert action == 'sendframes'
body = b''.join(map(bytes, meta['framegen']))
Gregory Szorc
wireproto: crude support for version 2 HTTP peer...
r37501 req = self._requestbuilder(pycompat.strurl(url), body, headers)
req.add_unredirected_header(r'Content-Length', r'%d' % len(body))
# TODO unify this code with httppeer.
try:
res = self._opener.open(req)
except urlerr.httperror as e:
if e.code == 401:
raise error.Abort(_('authorization failed'))
raise
except httplib.HTTPException as e:
self.ui.traceback()
raise IOError(None, e)
# TODO validate response type, wrap response to handle I/O errors.
# TODO more robust frame receiver.
results = []
while True:
frame = wireprotoframing.readframe(res)
if frame is None:
break
self.ui.note(_('received %r\n') % frame)
Gregory Szorc
wireproto: client reactor support for receiving frames...
r37562 action, meta = reactor.onframerecv(frame)
if action == 'responsedata':
if meta['cbor']:
payload = util.bytesio(meta['data'])
Gregory Szorc
wireproto: crude support for version 2 HTTP peer...
r37501
decoder = cbor.CBORDecoder(payload)
Gregory Szorc
wireproto: client reactor support for receiving frames...
r37562 while payload.tell() + 1 < len(meta['data']):
Gregory Szorc
wireproto: crude support for version 2 HTTP peer...
r37501 results.append(decoder.decode())
else:
Gregory Szorc
wireproto: client reactor support for receiving frames...
r37562 results.append(meta['data'])
Gregory Szorc
wireproto: crude support for version 2 HTTP peer...
r37501 else:
Gregory Szorc
wireproto: client reactor support for receiving frames...
r37562 error.ProgrammingError('unhandled action: %s' % action)
Gregory Szorc
wireproto: crude support for version 2 HTTP peer...
r37501
return results
Gregory Szorc
httppeer: perform capabilities request in makepeer()...
r37570 def performhandshake(ui, url, opener, requestbuilder):
# The handshake is a request to the capabilities command.
caps = None
def capable(x):
raise error.ProgrammingError('should not be called')
req, requrl, qs = makev1commandrequest(ui, requestbuilder, caps,
capable, url, 'capabilities',
{})
resp = sendrequest(ui, opener, req)
respurl, resp = parsev1commandresponse(ui, url, requrl, qs, resp,
compressible=False)
try:
rawcaps = resp.read()
finally:
resp.close()
return respurl, set(rawcaps.split())
Gregory Szorc
httppeer: move requestbuilder defaults into makepeer() argument...
r37565 def makepeer(ui, path, requestbuilder=urlreq.request):
"""Construct an appropriate HTTP peer instance.
``requestbuilder`` is the type used for constructing HTTP requests.
It exists as an argument so extensions can override the default.
"""
Gregory Szorc
httppeer: refactor how httppeer is created (API)...
r37024 u = util.url(path)
if u.query or u.fragment:
raise error.Abort(_('unsupported URL component: "%s"') %
(u.query or u.fragment))
# urllib cannot handle URLs with embedded user or passwd.
url, authinfo = u.authinfo()
ui.debug('using %s\n' % url)
opener = urlmod.opener(ui, authinfo)
Gregory Szorc
httppeer: perform capabilities request in makepeer()...
r37570 respurl, caps = performhandshake(ui, url, opener, requestbuilder)
return httppeer(ui, path, respurl, opener, requestbuilder, caps)
Gregory Szorc
httppeer: refactor how httppeer is created (API)...
r37024
Peter Arrenbrecht
peer: introduce real peer classes...
r17192 def instance(ui, path, create):
if create:
Pierre-Yves David
error: get Abort from 'error' instead of 'util'...
r26587 raise error.Abort(_('cannot create new http repository'))
Peter Arrenbrecht
peer: introduce real peer classes...
r17192 try:
Gregory Szorc
httppeer: alias url as urlmod...
r36977 if path.startswith('https:') and not urlmod.has_https:
Gregory Szorc
httppeer: remove httpspeer...
r36238 raise error.Abort(_('Python support for SSL and HTTPS '
'is not installed'))
Gregory Szorc
httppeer: remove support for connecting to <0.9.1 servers (BC)...
r35902
Gregory Szorc
httppeer: refactor how httppeer is created (API)...
r37024 inst = makepeer(ui, path)
Gregory Szorc
httppeer: remove support for connecting to <0.9.1 servers (BC)...
r35902
Peter Arrenbrecht
peer: introduce real peer classes...
r17192 return inst
Gregory Szorc
global: mass rewrite to use modern exception syntax...
r25660 except error.RepoError as httpexception:
Peter Arrenbrecht
peer: introduce real peer classes...
r17192 try:
r = statichttprepo.instance(ui, "static-" + path, create)
FUJIWARA Katsunori
httppeer: make a message translatable...
r29241 ui.note(_('(falling back to static-http)\n'))
Peter Arrenbrecht
peer: introduce real peer classes...
r17192 return r
except error.RepoError:
raise httpexception # use the original http RepoError instead