--- a/mercurial/wireprotov2peer.py Sat Oct 05 10:29:34 2019 -0400
+++ b/mercurial/wireprotov2peer.py Sun Oct 06 09:45:02 2019 -0400
@@ -20,9 +20,8 @@
wireprotoframing,
wireprototypes,
)
-from .utils import (
- cborutil,
-)
+from .utils import cborutil
+
def formatrichmessage(atoms):
"""Format an encoded message from the framing protocol."""
@@ -39,6 +38,7 @@
return b''.join(chunks)
+
SUPPORTED_REDIRECT_PROTOCOLS = {
b'http',
b'https',
@@ -49,6 +49,7 @@
b'sha256',
}
+
def redirecttargetsupported(ui, target):
"""Determine whether a redirect target entry is supported.
@@ -56,13 +57,17 @@
the server.
"""
if target.get(b'protocol') not in SUPPORTED_REDIRECT_PROTOCOLS:
- ui.note(_('(remote redirect target %s uses unsupported protocol: %s)\n')
- % (target[b'name'], target.get(b'protocol', b'')))
+ ui.note(
+ _('(remote redirect target %s uses unsupported protocol: %s)\n')
+ % (target[b'name'], target.get(b'protocol', b''))
+ )
return False
if target.get(b'snirequired') and not sslutil.hassni:
- ui.note(_('(redirect target %s requires SNI, which is unsupported)\n') %
- target[b'name'])
+ ui.note(
+ _('(redirect target %s requires SNI, which is unsupported)\n')
+ % target[b'name']
+ )
return False
if b'tlsversions' in target:
@@ -74,15 +79,20 @@
supported.add(v[3:])
if not tlsversions & supported:
- ui.note(_('(remote redirect target %s requires unsupported TLS '
- 'versions: %s)\n') % (
- target[b'name'], b', '.join(sorted(tlsversions))))
+ ui.note(
+ _(
+ '(remote redirect target %s requires unsupported TLS '
+ 'versions: %s)\n'
+ )
+ % (target[b'name'], b', '.join(sorted(tlsversions)))
+ )
return False
ui.note(_('(remote redirect target %s is compatible)\n') % target[b'name'])
return True
+
def supportedredirects(ui, apidescriptor):
"""Resolve the "redirect" command request key given an API descriptor.
@@ -96,17 +106,24 @@
if not apidescriptor or b'redirect' not in apidescriptor:
return None
- targets = [t[b'name'] for t in apidescriptor[b'redirect'][b'targets']
- if redirecttargetsupported(ui, t)]
+ targets = [
+ t[b'name']
+ for t in apidescriptor[b'redirect'][b'targets']
+ if redirecttargetsupported(ui, t)
+ ]
- hashes = [h for h in apidescriptor[b'redirect'][b'hashes']
- if h in SUPPORTED_CONTENT_HASHES]
+ hashes = [
+ h
+ for h in apidescriptor[b'redirect'][b'hashes']
+ if h in SUPPORTED_CONTENT_HASHES
+ ]
return {
b'targets': targets,
b'hashes': hashes,
}
+
class commandresponse(object):
"""Represents the response to a command request.
@@ -162,9 +179,13 @@
# content redirect is the only object in the stream. Fail
# if we see a misbehaving server.
if self._redirect:
- raise error.Abort(_('received unexpected response data '
- 'after content redirect; the remote is '
- 'buggy'))
+ raise error.Abort(
+ _(
+ 'received unexpected response data '
+ 'after content redirect; the remote is '
+ 'buggy'
+ )
+ )
self._pendingevents.append(o)
@@ -190,7 +211,8 @@
fullhashes=l.get(b'fullhashes'),
fullhashseed=l.get(b'fullhashseed'),
serverdercerts=l.get(b'serverdercerts'),
- servercadercerts=l.get(b'servercadercerts'))
+ servercadercerts=l.get(b'servercadercerts'),
+ )
return
atoms = [{'msg': o[b'error'][b'message']}]
@@ -237,6 +259,7 @@
if stop:
break
+
class clienthandler(object):
"""Object to handle higher-level client activities.
@@ -248,8 +271,9 @@
with the higher-level peer API.
"""
- def __init__(self, ui, clientreactor, opener=None,
- requestbuilder=util.urlreq.request):
+ def __init__(
+ self, ui, clientreactor, opener=None, requestbuilder=util.urlreq.request
+ ):
self._ui = ui
self._reactor = clientreactor
self._requests = {}
@@ -265,8 +289,9 @@
Returns an iterable of frames that should be sent over the wire.
"""
- request, action, meta = self._reactor.callcommand(command, args,
- redirect=redirect)
+ request, action, meta = self._reactor.callcommand(
+ command, args, redirect=redirect
+ )
if action != 'noop':
raise error.ProgrammingError('%s not yet supported' % action)
@@ -347,7 +372,8 @@
raise error.ProgrammingError(
'received frame for unknown request; this is either a bug in '
'the clientreactor not screening for this or this instance was '
- 'never told about this request: %r' % frame)
+ 'never told about this request: %r' % frame
+ )
response = self._responses[frame.requestid]
@@ -371,7 +397,8 @@
response._onerror(e)
else:
raise error.ProgrammingError(
- 'unhandled action from clientreactor: %s' % action)
+ 'unhandled action from clientreactor: %s' % action
+ )
def _processresponsedata(self, frame, meta, response):
# This can raise. The caller can handle it.
@@ -423,16 +450,26 @@
# TODO handle framed responses.
if redirect.mediatype != b'application/mercurial-cbor':
- raise error.Abort(_('cannot handle redirects for the %s media type')
- % redirect.mediatype)
+ raise error.Abort(
+ _('cannot handle redirects for the %s media type')
+ % redirect.mediatype
+ )
if redirect.fullhashes:
- self._ui.warn(_('(support for validating hashes on content '
- 'redirects not supported)\n'))
+ self._ui.warn(
+ _(
+ '(support for validating hashes on content '
+ 'redirects not supported)\n'
+ )
+ )
if redirect.serverdercerts or redirect.servercadercerts:
- self._ui.warn(_('(support for pinning server certificates on '
- 'content redirects not supported)\n'))
+ self._ui.warn(
+ _(
+ '(support for pinning server certificates on '
+ 'content redirects not supported)\n'
+ )
+ )
headers = {
r'Accept': redirect.mediatype,
@@ -456,9 +493,9 @@
# The existing response object is associated with frame data. Rather
# than try to normalize its state, just create a new object.
oldresponse = self._responses[requestid]
- self._responses[requestid] = commandresponse(requestid,
- oldresponse.command,
- fromredirect=True)
+ self._responses[requestid] = commandresponse(
+ requestid, oldresponse.command, fromredirect=True
+ )
self._redirects.append((requestid, res))
@@ -496,32 +533,39 @@
response._oninputcomplete()
return False
+
def decodebranchmap(objs):
# Response should be a single CBOR map of branch name to array of nodes.
bm = next(objs)
return {encoding.tolocal(k): v for k, v in bm.items()}
+
def decodeheads(objs):
# Array of node bytestrings.
return next(objs)
+
def decodeknown(objs):
# Bytestring where each byte is a 0 or 1.
raw = next(objs)
- return [True if raw[i:i + 1] == b'1' else False for i in range(len(raw))]
+ return [True if raw[i : i + 1] == b'1' else False for i in range(len(raw))]
+
def decodelistkeys(objs):
# Map with bytestring keys and values.
return next(objs)
+
def decodelookup(objs):
return next(objs)
+
def decodepushkey(objs):
return next(objs)
+
COMMAND_DECODERS = {
'branchmap': decodebranchmap,
'heads': decodeheads,