wireprotov2: move response handling out of httppeer
And fix some bugs while we're here.
The code for processing response data from the unified framing
protocol is mostly peer agnostic. The peer-specific bits are the
configuration of the client reactor and how I/O is performed. I
initially implemented things in httppeer for expediency.
This commit establishes a module for holding the peer API level
code for the framing based protocol. Inside this module we have
a class to help coordinate higher-level activities, such as managing
response object.
The client handler bits could be rolled into clientreactor. However,
I want clientreactor to be sans I/O and I want it to only be
concerned with protocol-level details, not higher-level concepts
like how protocol events are converted into peer API concepts. I
want clientreactor to receive a frame and then tell the caller what
should probably be done about it. If we start putting things like
future resolution into clientreactor, we'll constrain how the protocol
can be used (e.g. by requiring futures).
The new code is loosely based on what was in httppeer before. I
changed things a bit around response handling. We now buffer the
entire response "body" and then handle it as one atomic unit. This
fixed a bug around decoding CBOR data that spanned multiple frames.
I also fixed an off-by-one bug where we failed to read a single byte
CBOR value at the end of the stream. That's why tests have changed.
The new state of httppeer is much cleaner. It is largely agnostic
about framing protocol implementation details. That's how it should
be: the framing protocol is designed to be largely transport
agnostic. We want peers merely putting bytes on the wire and telling
the framing protocol where to read response data from.
There's still a bit of work to be done here, especially for
representing responses. But at least we're a step closer to having a
higher-level peer interface that can be plugged into the SSH peer
someday.
I initially added this class to wireprotoframing. However, we'll
eventually need version 2 specific functions to convert CBOR responses
into data structures expected by the code calling commands. This
needs to live somewhere. Since that code would be shared across peers,
we need a common module. We have wireprotov1peer for the equivalent
version 1 code. So I decided to establish wireprotov2peer.
Differential Revision: https://phab.mercurial-scm.org/D3379
# sshprotoext.py - Extension to test behavior of SSH protocol
#
# Copyright 2018 Gregory Szorc <gregory.szorc@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.
# This extension replaces the SSH server started via `hg serve --stdio`.
# The server behaves differently depending on environment variables.
from __future__ import absolute_import
from mercurial import (
error,
extensions,
registrar,
sshpeer,
wireproto,
wireprotoserver,
)
configtable = {}
configitem = registrar.configitem(configtable)
configitem(b'sshpeer', b'mode', default=None)
configitem(b'sshpeer', b'handshake-mode', default=None)
class bannerserver(wireprotoserver.sshserver):
"""Server that sends a banner to stdout."""
def serve_forever(self):
for i in range(10):
self._fout.write(b'banner: line %d\n' % i)
super(bannerserver, self).serve_forever()
class prehelloserver(wireprotoserver.sshserver):
"""Tests behavior when connecting to <0.9.1 servers.
The ``hello`` wire protocol command was introduced in Mercurial
0.9.1. Modern clients send the ``hello`` command when connecting
to SSH servers. This mock server tests behavior of the handshake
when ``hello`` is not supported.
"""
def serve_forever(self):
l = self._fin.readline()
assert l == b'hello\n'
# Respond to unknown commands with an empty reply.
wireprotoserver._sshv1respondbytes(self._fout, b'')
l = self._fin.readline()
assert l == b'between\n'
proto = wireprotoserver.sshv1protocolhandler(self._ui, self._fin,
self._fout)
rsp = wireproto.dispatch(self._repo, proto, b'between')
wireprotoserver._sshv1respondbytes(self._fout, rsp.data)
super(prehelloserver, self).serve_forever()
def performhandshake(orig, ui, stdin, stdout, stderr):
"""Wrapped version of sshpeer._performhandshake to send extra commands."""
mode = ui.config(b'sshpeer', b'handshake-mode')
if mode == b'pre-no-args':
ui.debug(b'sending no-args command\n')
stdin.write(b'no-args\n')
stdin.flush()
return orig(ui, stdin, stdout, stderr)
elif mode == b'pre-multiple-no-args':
ui.debug(b'sending unknown1 command\n')
stdin.write(b'unknown1\n')
ui.debug(b'sending unknown2 command\n')
stdin.write(b'unknown2\n')
ui.debug(b'sending unknown3 command\n')
stdin.write(b'unknown3\n')
stdin.flush()
return orig(ui, stdin, stdout, stderr)
else:
raise error.ProgrammingError(b'unknown HANDSHAKECOMMANDMODE: %s' %
mode)
def extsetup(ui):
# It's easier for tests to define the server behavior via environment
# variables than config options. This is because `hg serve --stdio`
# has to be invoked with a certain form for security reasons and
# `dummyssh` can't just add `--config` flags to the command line.
servermode = ui.environ.get(b'SSHSERVERMODE')
if servermode == b'banner':
wireprotoserver.sshserver = bannerserver
elif servermode == b'no-hello':
wireprotoserver.sshserver = prehelloserver
elif servermode:
raise error.ProgrammingError(b'unknown server mode: %s' % servermode)
peermode = ui.config(b'sshpeer', b'mode')
if peermode == b'extra-handshake-commands':
extensions.wrapfunction(sshpeer, '_performhandshake', performhandshake)
elif peermode:
raise error.ProgrammingError(b'unknown peer mode: %s' % peermode)