Mercurial > public > mercurial-scm > hg
comparison mercurial/wireprotoserver.py @ 36371:0c231df1ffdc
wireprototypes: move baseprotocolhandler from wireprotoserver
This is needed to prevent a cycle in an upcoming commit.
Differential Revision: https://phab.mercurial-scm.org/D2384
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Wed, 21 Feb 2018 14:21:05 -0800 |
parents | 685bcdd236b5 |
children | b8d0761a85c7 |
comparison
equal
deleted
inserted
replaced
36370:11ba1a96f946 | 36371:0c231df1ffdc |
---|---|
4 # This software may be used and distributed according to the terms of the | 4 # This software may be used and distributed according to the terms of the |
5 # GNU General Public License version 2 or any later version. | 5 # GNU General Public License version 2 or any later version. |
6 | 6 |
7 from __future__ import absolute_import | 7 from __future__ import absolute_import |
8 | 8 |
9 import abc | |
10 import contextlib | 9 import contextlib |
11 import struct | 10 import struct |
12 import sys | 11 import sys |
13 | 12 |
14 from .i18n import _ | 13 from .i18n import _ |
36 # Names of the SSH protocol implementations. | 35 # Names of the SSH protocol implementations. |
37 SSHV1 = 'ssh-v1' | 36 SSHV1 = 'ssh-v1' |
38 # This is advertised over the wire. Incremental the counter at the end | 37 # This is advertised over the wire. Incremental the counter at the end |
39 # to reflect BC breakages. | 38 # to reflect BC breakages. |
40 SSHV2 = 'exp-ssh-v2-0001' | 39 SSHV2 = 'exp-ssh-v2-0001' |
41 | |
42 class baseprotocolhandler(object): | |
43 """Abstract base class for wire protocol handlers. | |
44 | |
45 A wire protocol handler serves as an interface between protocol command | |
46 handlers and the wire protocol transport layer. Protocol handlers provide | |
47 methods to read command arguments, redirect stdio for the duration of | |
48 the request, handle response types, etc. | |
49 """ | |
50 | |
51 __metaclass__ = abc.ABCMeta | |
52 | |
53 @abc.abstractproperty | |
54 def name(self): | |
55 """The name of the protocol implementation. | |
56 | |
57 Used for uniquely identifying the transport type. | |
58 """ | |
59 | |
60 @abc.abstractmethod | |
61 def getargs(self, args): | |
62 """return the value for arguments in <args> | |
63 | |
64 returns a list of values (same order as <args>)""" | |
65 | |
66 @abc.abstractmethod | |
67 def forwardpayload(self, fp): | |
68 """Read the raw payload and forward to a file. | |
69 | |
70 The payload is read in full before the function returns. | |
71 """ | |
72 | |
73 @abc.abstractmethod | |
74 def mayberedirectstdio(self): | |
75 """Context manager to possibly redirect stdio. | |
76 | |
77 The context manager yields a file-object like object that receives | |
78 stdout and stderr output when the context manager is active. Or it | |
79 yields ``None`` if no I/O redirection occurs. | |
80 | |
81 The intent of this context manager is to capture stdio output | |
82 so it may be sent in the response. Some transports support streaming | |
83 stdio to the client in real time. For these transports, stdio output | |
84 won't be captured. | |
85 """ | |
86 | |
87 @abc.abstractmethod | |
88 def client(self): | |
89 """Returns a string representation of this client (as bytes).""" | |
90 | 40 |
91 def decodevaluefromheaders(req, headerprefix): | 41 def decodevaluefromheaders(req, headerprefix): |
92 """Decode a long value from multiple HTTP request headers. | 42 """Decode a long value from multiple HTTP request headers. |
93 | 43 |
94 Returns the value as a bytes, not a str. | 44 Returns the value as a bytes, not a str. |
103 chunks.append(pycompat.bytesurl(v)) | 53 chunks.append(pycompat.bytesurl(v)) |
104 i += 1 | 54 i += 1 |
105 | 55 |
106 return ''.join(chunks) | 56 return ''.join(chunks) |
107 | 57 |
108 class httpv1protocolhandler(baseprotocolhandler): | 58 class httpv1protocolhandler(wireprototypes.baseprotocolhandler): |
109 def __init__(self, req, ui): | 59 def __init__(self, req, ui): |
110 self._req = req | 60 self._req = req |
111 self._ui = ui | 61 self._ui = ui |
112 | 62 |
113 @property | 63 @property |
362 ferr.write(b'%s\n-\n' % rsp) | 312 ferr.write(b'%s\n-\n' % rsp) |
363 ferr.flush() | 313 ferr.flush() |
364 fout.write(b'\n') | 314 fout.write(b'\n') |
365 fout.flush() | 315 fout.flush() |
366 | 316 |
367 class sshv1protocolhandler(baseprotocolhandler): | 317 class sshv1protocolhandler(wireprototypes.baseprotocolhandler): |
368 """Handler for requests services via version 1 of SSH protocol.""" | 318 """Handler for requests services via version 1 of SSH protocol.""" |
369 def __init__(self, ui, fin, fout): | 319 def __init__(self, ui, fin, fout): |
370 self._ui = ui | 320 self._ui = ui |
371 self._fin = fin | 321 self._fin = fin |
372 self._fout = fout | 322 self._fout = fout |