##// END OF EJS Templates
internals: refactor wire protocol documentation...
Gregory Szorc -
r35993:40d94ea5 default
parent child Browse files
Show More
@@ -10,11 +10,43 b' return multiple response types.'
10 10 The protocol is synchronous and does not support multiplexing (concurrent
11 11 commands).
12 12
13 Transport Protocols
14 ===================
13 Handshake
14 =========
15
16 It is required or common for clients to perform a *handshake* when connecting
17 to a server. The handshake serves the following purposes:
18
19 * Negotiating protocol/transport level options
20 * Allows the client to learn about server capabilities to influence
21 future requests
22 * Ensures the underlying transport channel is in a *clean* state
15 23
16 HTTP Transport
17 --------------
24 An important goal of the handshake is to allow clients to use more modern
25 wire protocol features. By default, clients must assume they are talking
26 to an old version of Mercurial server (possibly even the very first
27 implementation). So, clients should not attempt to call or utilize modern
28 wire protocol features until they have confirmation that the server
29 supports them. The handshake implementation is designed to allow both
30 ends to utilize the latest set of features and capabilities with as
31 few round trips as possible.
32
33 The handshake mechanism varies by transport and protocol and is documented
34 in the sections below.
35
36 HTTP Protocol
37 =============
38
39 Handshake
40 ---------
41
42 The client sends a ``capabilities`` command request (``?cmd=capabilities``)
43 as soon as HTTP requests may be issued.
44
45 The server responds with a capabilities string, which the client parses to
46 learn about the server's abilities.
47
48 HTTP Version 1 Transport
49 ------------------------
18 50
19 51 Commands are issued as HTTP/1.0 or HTTP/1.1 requests. Commands are
20 52 sent to the base URL of the repository with the command name sent in
@@ -112,11 +144,86 b' A command returning a ``stream`` respons'
112 144 ``application/mercurial-0.*`` media type and the HTTP response is typically
113 145 using *chunked transfer* (``Transfer-Encoding: chunked``).
114 146
115 SSH Transport
116 =============
147 SSH Protocol
148 ============
149
150 Handshake
151 ---------
152
153 For all clients, the handshake consists of the client sending 1 or more
154 commands to the server using version 1 of the transport. Servers respond
155 to commands they know how to respond to and send an empty response (``0\n``)
156 for unknown commands (per standard behavior of version 1 of the transport).
157 Clients then typically look for a response to the newest sent command to
158 determine which transport version to use and what the available features for
159 the connection and server are.
160
161 Preceding any response from client-issued commands, the server may print
162 non-protocol output. It is common for SSH servers to print banners, message
163 of the day announcements, etc when clients connect. It is assumed that any
164 such *banner* output will precede any Mercurial server output. So clients
165 must be prepared to handle server output on initial connect that isn't
166 in response to any client-issued command and doesn't conform to Mercurial's
167 wire protocol. This *banner* output should only be on stdout. However,
168 some servers may send output on stderr.
169
170 Pre 0.9.1 clients issue a ``between`` command with the ``pairs`` argument
171 having the value
172 ``0000000000000000000000000000000000000000-0000000000000000000000000000000000000000``.
173
174 The ``between`` command has been supported since the original Mercurial
175 SSH server. Requesting the empty range will return a ``\n`` string response,
176 which will be encoded as ``1\n\n`` (value length of ``1`` followed by a newline
177 followed by the value, which happens to be a newline).
178
179 For pre 0.9.1 clients and all servers, the exchange looks like::
180
181 c: between\n
182 c: pairs 81\n
183 c: 0000000000000000000000000000000000000000-0000000000000000000000000000000000000000
184 s: 1\n
185 s: \n
117 186
118 The SSH transport is a custom text-based protocol suitable for use over any
119 bi-directional stream transport. It is most commonly used with SSH.
187 0.9.1+ clients send a ``hello`` command (with no arguments) before the
188 ``between`` command. The response to this command allows clients to
189 discover server capabilities and settings.
190
191 An example exchange between 0.9.1+ clients and a ``hello`` aware server looks
192 like::
193
194 c: hello\n
195 c: between\n
196 c: pairs 81\n
197 c: 0000000000000000000000000000000000000000-0000000000000000000000000000000000000000
198 s: 324\n
199 s: capabilities: lookup changegroupsubset branchmap pushkey known getbundle ...\n
200 s: 1\n
201 s: \n
202
203 And a similar scenario but with servers sending a banner on connect::
204
205 c: hello\n
206 c: between\n
207 c: pairs 81\n
208 c: 0000000000000000000000000000000000000000-0000000000000000000000000000000000000000
209 s: welcome to the server\n
210 s: if you find any issues, email someone@somewhere.com\n
211 s: 324\n
212 s: capabilities: lookup changegroupsubset branchmap pushkey known getbundle ...\n
213 s: 1\n
214 s: \n
215
216 Note that output from the ``hello`` command is terminated by a ``\n``. This is
217 part of the response payload and not part of the wire protocol adding a newline
218 after responses. In other words, the length of the response contains the
219 trailing ``\n``.
220
221 SSH Version 1 Transport
222 -----------------------
223
224 The SSH transport (version 1) is a custom text-based protocol suitable for
225 use over any bi-directional stream transport. It is most commonly used with
226 SSH.
120 227
121 228 A SSH transport server can be started with ``hg serve --stdio``. The stdin,
122 229 stderr, and stdout file descriptors of the started process are used to exchange
@@ -463,53 +570,6 b' comma-delimited list. e.g. ``HG10GZ,HG10'
463 570 reflects the priority/preference of that type, where the first value is the
464 571 most preferred type.
465 572
466 Handshake Protocol
467 ==================
468
469 While not explicitly required, it is common for clients to perform a
470 *handshake* when connecting to a server. The handshake accomplishes 2 things:
471
472 * Obtaining capabilities and other server features
473 * Flushing extra server output (e.g. SSH servers may print extra text
474 when connecting that may confuse the wire protocol)
475
476 This isn't a traditional *handshake* as far as network protocols go because
477 there is no persistent state as a result of the handshake: the handshake is
478 simply the issuing of commands and commands are stateless.
479
480 The canonical clients perform a capabilities lookup at connection establishment
481 time. This is because clients must assume a server only supports the features
482 of the original Mercurial server implementation until proven otherwise (from
483 advertised capabilities). Nearly every server running today supports features
484 that weren't present in the original Mercurial server implementation. Rather
485 than wait for a client to perform functionality that needs to consult
486 capabilities, it issues the lookup at connection start to avoid any delay later.
487
488 For HTTP servers, the client sends a ``capabilities`` command request as
489 soon as the connection is established. The server responds with a capabilities
490 string, which the client parses.
491
492 For SSH servers, the client sends the ``hello`` command (no arguments)
493 and a ``between`` command with the ``pairs`` argument having the value
494 ``0000000000000000000000000000000000000000-0000000000000000000000000000000000000000``.
495
496 The ``between`` command has been supported since the original Mercurial
497 server. Requesting the empty range will return a ``\n`` string response,
498 which will be encoded as ``1\n\n`` (value length of ``1`` followed by a newline
499 followed by the value, which happens to be a newline).
500
501 The ``hello`` command was later introduced. Servers supporting it will issue
502 a response to that command before sending the ``1\n\n`` response to the
503 ``between`` command. Servers not supporting ``hello`` will send an empty
504 response (``0\n``).
505
506 In addition to the expected output from the ``hello`` and ``between`` commands,
507 servers may also send other output, such as *message of the day (MOTD)*
508 announcements. Clients assume servers will send this output before the
509 Mercurial server replies to the client-issued commands. So any server output
510 not conforming to the expected command responses is assumed to be not related
511 to Mercurial and can be ignored.
512
513 573 Content Negotiation
514 574 ===================
515 575
@@ -519,8 +579,8 b' have been built into commands themselves'
519 579 well-defined response type and only certain commands needed to support
520 580 functionality like compression.
521 581
522 Currently, only the HTTP transport supports content negotiation at the protocol
523 layer.
582 Currently, only the HTTP version 1 transport supports content negotiation
583 at the protocol layer.
524 584
525 585 HTTP requests advertise supported response formats via the ``X-HgProto-<N>``
526 586 request header, where ``<N>`` is an integer starting at 1 allowing the logical
@@ -739,7 +799,7 b' phases'
739 799 Boolean indicating whether phases data is requested.
740 800
741 801 The return type on success is a ``stream`` where the value is bundle.
742 On the HTTP transport, the response is zlib compressed.
802 On the HTTP version 1 transport, the response is zlib compressed.
743 803
744 804 If an error occurs, a generic error response can be sent.
745 805
@@ -842,13 +902,14 b' All arguments are string types.'
842 902
843 903 The return type is a ``string``. The value depends on the transport protocol.
844 904
845 The SSH transport sends a string encoded integer followed by a newline
846 (``\n``) which indicates operation result. The server may send additional
847 output on the ``stderr`` stream that should be displayed to the user.
905 The SSH version 1 transport sends a string encoded integer followed by a
906 newline (``\n``) which indicates operation result. The server may send
907 additional output on the ``stderr`` stream that should be displayed to the
908 user.
848 909
849 The HTTP transport sends a string encoded integer followed by a newline
850 followed by additional server output that should be displayed to the user.
851 This may include output from hooks, etc.
910 The HTTP version 1 transport sends a string encoded integer followed by a
911 newline followed by additional server output that should be displayed to
912 the user. This may include output from hooks, etc.
852 913
853 914 The integer result varies by namespace. ``0`` means an error has occurred
854 915 and there should be additional output to display to the user.
@@ -912,18 +973,18 b' is 1 fewer head.'
912 973
913 974 The encoding of the ``push response`` type varies by transport.
914 975
915 For the SSH transport, this type is composed of 2 ``string`` responses: an
916 empty response (``0\n``) followed by the integer result value. e.g.
917 ``1\n2``. So the full response might be ``0\n1\n2``.
976 For the SSH version 1 transport, this type is composed of 2 ``string``
977 responses: an empty response (``0\n``) followed by the integer result value.
978 e.g. ``1\n2``. So the full response might be ``0\n1\n2``.
918 979
919 For the HTTP transport, the response is a ``string`` type composed of an
920 integer result value followed by a newline (``\n``) followed by string
980 For the HTTP version 1 transport, the response is a ``string`` type composed
981 of an integer result value followed by a newline (``\n``) followed by string
921 982 content holding server output that should be displayed on the client (output
922 983 hooks, etc).
923 984
924 985 In some cases, the server may respond with a ``bundle2`` bundle. In this
925 case, the response type is ``stream``. For the HTTP transport, the response
926 is zlib compressed.
986 case, the response type is ``stream``. For the HTTP version 1 transport, the
987 response is zlib compressed.
927 988
928 989 The server may also respond with a generic error type, which contains a string
929 990 indicating the failure.
General Comments 0
You need to be logged in to leave comments. Login now