comparison tests/test-http-api-httpv2.t @ 37054:40206e227412

wireproto: define and implement protocol for issuing requests The existing HTTP and SSH wire protocols suffer from a host of flaws and shortcomings. I've been wanting to rewrite the protocol for a while now. Supporting partial clone - which will require new wire protocol commands and capabilities - and other advanced server functionality will be much easier if we start from a clean slate and don't have to be constrained by limitations of the existing wire protocol. This commit starts to introduce a new data exchange format for use over the wire protocol. The new protocol is built on top of "frames," which are atomic units of metadata + data. Frames will make it easier to implement proxies and other mechanisms that want to inspect data without having to maintain state. The existing frame metadata is very minimal and it will evolve heavily. (We will eventually support things like concurrent requests, out-of-order responses, compression, side-channels for status updates, etc. Some of these will require additions to the frame header.) Another benefit of frames is that all reads are of a fixed size. A reader works by consuming a frame header, extracting the payload length, then reading that many bytes. No lookahead, buffering, or memory reallocations are needed. The new protocol attempts to be transport agnostic. I want all that's required to use the new protocol to be a pair of unidirectional, half-duplex pipes. (Yes, we will eventually make use of full-duplex pipes, but that's for another commit.) Notably, when the SSH transport switches to this new protocol, stderr will be unused. This is by design: the lack of stderr on HTTP harms protocol behavior there. By shoehorning everything into a pair of pipes, we can have more consistent behavior across transports. We currently only define the client side parts of the new protocol, specifically the bits for requesting that a command run. This keeps the new code and feature small and somewhat easy to review. We add support to `hg debugwireproto` for writing frames into HTTP request bodies. Our tests that issue commands to the new HTTP endpoint have been updated to transmit frames. The server bits haven't been touched to consume the frames yet. This will occur in the next commit... Astute readers may notice that the command name is transmitted in both the HTTP request URL and the command request frame. This is partially a kludge from me initially implementing the frame-based protocol for SSH first. But it is also a feature: I intend to eventually support issuing multiple commands per HTTP request. This will allow us to replace the abomination that is the "batch" wire protocol command with a protocol-level mechanism for performing multi-dispatch. Because I want the frame-based protocol to be as similar as possible across transports, I'd rather we (redundantly) include the command name in the frame than differ behavior between transports that have out-of-band routing information (like HTTP) readily available. Differential Revision: https://phab.mercurial-scm.org/D2851
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 19 Mar 2018 16:49:53 -0700
parents 37d7a1d18b97
children 8c3c47362934
comparison
equal deleted inserted replaced
37053:37d7a1d18b97 37054:40206e227412
1 $ HTTPV2=exp-http-v2-0001 1 $ HTTPV2=exp-http-v2-0001
2 $ MEDIATYPE=application/mercurial-tbd 2 $ MEDIATYPE=application/mercurial-exp-framing-0001
3 3
4 $ send() { 4 $ send() {
5 > hg --verbose debugwireproto --peer raw http://$LOCALIP:$HGPORT/ 5 > hg --verbose debugwireproto --peer raw http://$LOCALIP:$HGPORT/
6 > } 6 > }
7 7
118 s> makefile('rb', None) 118 s> makefile('rb', None)
119 s> HTTP/1.1 406 Not Acceptable\r\n 119 s> HTTP/1.1 406 Not Acceptable\r\n
120 s> Server: testing stub value\r\n 120 s> Server: testing stub value\r\n
121 s> Date: $HTTP_DATE$\r\n 121 s> Date: $HTTP_DATE$\r\n
122 s> Content-Type: text/plain\r\n 122 s> Content-Type: text/plain\r\n
123 s> Content-Length: 72\r\n 123 s> Content-Length: 85\r\n
124 s> \r\n 124 s> \r\n
125 s> client MUST specify Accept header with value: application/mercurial-tbd\n 125 s> client MUST specify Accept header with value: application/mercurial-exp-framing-0001\n
126 126
127 Bad Accept header results in 406 127 Bad Accept header results in 406
128 128
129 $ send << EOF 129 $ send << EOF
130 > httprequest POST api/$HTTPV2/ro/customreadonly 130 > httprequest POST api/$HTTPV2/ro/customreadonly
141 s> makefile('rb', None) 141 s> makefile('rb', None)
142 s> HTTP/1.1 406 Not Acceptable\r\n 142 s> HTTP/1.1 406 Not Acceptable\r\n
143 s> Server: testing stub value\r\n 143 s> Server: testing stub value\r\n
144 s> Date: $HTTP_DATE$\r\n 144 s> Date: $HTTP_DATE$\r\n
145 s> Content-Type: text/plain\r\n 145 s> Content-Type: text/plain\r\n
146 s> Content-Length: 72\r\n 146 s> Content-Length: 85\r\n
147 s> \r\n 147 s> \r\n
148 s> client MUST specify Accept header with value: application/mercurial-tbd\n 148 s> client MUST specify Accept header with value: application/mercurial-exp-framing-0001\n
149 149
150 Bad Content-Type header results in 415 150 Bad Content-Type header results in 415
151 151
152 $ send << EOF 152 $ send << EOF
153 > httprequest POST api/$HTTPV2/ro/customreadonly 153 > httprequest POST api/$HTTPV2/ro/customreadonly
156 > content-type: badmedia 156 > content-type: badmedia
157 > EOF 157 > EOF
158 using raw connection to peer 158 using raw connection to peer
159 s> POST /api/exp-http-v2-0001/ro/customreadonly HTTP/1.1\r\n 159 s> POST /api/exp-http-v2-0001/ro/customreadonly HTTP/1.1\r\n
160 s> Accept-Encoding: identity\r\n 160 s> Accept-Encoding: identity\r\n
161 s> accept: application/mercurial-tbd\r\n 161 s> accept: application/mercurial-exp-framing-0001\r\n
162 s> content-type: badmedia\r\n 162 s> content-type: badmedia\r\n
163 s> user-agent: test\r\n 163 s> user-agent: test\r\n
164 s> host: $LOCALIP:$HGPORT\r\n (glob) 164 s> host: $LOCALIP:$HGPORT\r\n (glob)
165 s> \r\n 165 s> \r\n
166 s> makefile('rb', None) 166 s> makefile('rb', None)
167 s> HTTP/1.1 415 Unsupported Media Type\r\n 167 s> HTTP/1.1 415 Unsupported Media Type\r\n
168 s> Server: testing stub value\r\n 168 s> Server: testing stub value\r\n
169 s> Date: $HTTP_DATE$\r\n 169 s> Date: $HTTP_DATE$\r\n
170 s> Content-Type: text/plain\r\n 170 s> Content-Type: text/plain\r\n
171 s> Content-Length: 75\r\n 171 s> Content-Length: 88\r\n
172 s> \r\n 172 s> \r\n
173 s> client MUST send Content-Type header with value: application/mercurial-tbd\n 173 s> client MUST send Content-Type header with value: application/mercurial-exp-framing-0001\n
174 174
175 Request to read-only command works out of the box 175 Request to read-only command works out of the box
176 176
177 $ send << EOF 177 $ send << EOF
178 > httprequest POST api/$HTTPV2/ro/customreadonly 178 > httprequest POST api/$HTTPV2/ro/customreadonly
179 > accept: $MEDIATYPE 179 > accept: $MEDIATYPE
180 > content-type: $MEDIATYPE 180 > content-type: $MEDIATYPE
181 > user-agent: test 181 > user-agent: test
182 > frame command-name eos customreadonly
182 > EOF 183 > EOF
183 using raw connection to peer 184 using raw connection to peer
184 s> POST /api/exp-http-v2-0001/ro/customreadonly HTTP/1.1\r\n 185 s> POST /api/exp-http-v2-0001/ro/customreadonly HTTP/1.1\r\n
185 s> Accept-Encoding: identity\r\n 186 s> Accept-Encoding: identity\r\n
186 s> accept: application/mercurial-tbd\r\n 187 s> accept: application/mercurial-exp-framing-0001\r\n
187 s> content-type: application/mercurial-tbd\r\n 188 s> content-type: application/mercurial-exp-framing-0001\r\n
188 s> user-agent: test\r\n 189 s> user-agent: test\r\n
189 s> host: $LOCALIP:$HGPORT\r\n (glob) 190 s> content-length: 18\r\n
190 s> \r\n 191 s> host: $LOCALIP:$HGPORT\r\n (glob)
192 s> \r\n
193 s> \x0e\x00\x00\x11customreadonly
191 s> makefile('rb', None) 194 s> makefile('rb', None)
192 s> HTTP/1.1 200 OK\r\n 195 s> HTTP/1.1 200 OK\r\n
193 s> Server: testing stub value\r\n 196 s> Server: testing stub value\r\n
194 s> Date: $HTTP_DATE$\r\n 197 s> Date: $HTTP_DATE$\r\n
195 s> Content-Type: text/plain\r\n 198 s> Content-Type: text/plain\r\n
281 $ send << EOF 284 $ send << EOF
282 > httprequest POST api/$HTTPV2/rw/customreadonly 285 > httprequest POST api/$HTTPV2/rw/customreadonly
283 > user-agent: test 286 > user-agent: test
284 > accept: $MEDIATYPE 287 > accept: $MEDIATYPE
285 > content-type: $MEDIATYPE 288 > content-type: $MEDIATYPE
289 > frame command-name eos customreadonly
286 > EOF 290 > EOF
287 using raw connection to peer 291 using raw connection to peer
288 s> POST /api/exp-http-v2-0001/rw/customreadonly HTTP/1.1\r\n 292 s> POST /api/exp-http-v2-0001/rw/customreadonly HTTP/1.1\r\n
289 s> Accept-Encoding: identity\r\n 293 s> Accept-Encoding: identity\r\n
290 s> accept: application/mercurial-tbd\r\n 294 s> accept: application/mercurial-exp-framing-0001\r\n
291 s> content-type: application/mercurial-tbd\r\n 295 s> content-type: application/mercurial-exp-framing-0001\r\n
292 s> user-agent: test\r\n 296 s> user-agent: test\r\n
293 s> host: $LOCALIP:$HGPORT\r\n (glob) 297 s> content-length: 18\r\n
294 s> \r\n 298 s> host: $LOCALIP:$HGPORT\r\n (glob)
299 s> \r\n
300 s> \x0e\x00\x00\x11customreadonly
295 s> makefile('rb', None) 301 s> makefile('rb', None)
296 s> HTTP/1.1 200 OK\r\n 302 s> HTTP/1.1 200 OK\r\n
297 s> Server: testing stub value\r\n 303 s> Server: testing stub value\r\n
298 s> Date: $HTTP_DATE$\r\n 304 s> Date: $HTTP_DATE$\r\n
299 s> Content-Type: text/plain\r\n 305 s> Content-Type: text/plain\r\n
309 > accept: $MEDIATYPE 315 > accept: $MEDIATYPE
310 > EOF 316 > EOF
311 using raw connection to peer 317 using raw connection to peer
312 s> POST /api/exp-http-v2-0001/rw/badcommand HTTP/1.1\r\n 318 s> POST /api/exp-http-v2-0001/rw/badcommand HTTP/1.1\r\n
313 s> Accept-Encoding: identity\r\n 319 s> Accept-Encoding: identity\r\n
314 s> accept: application/mercurial-tbd\r\n 320 s> accept: application/mercurial-exp-framing-0001\r\n
315 s> user-agent: test\r\n 321 s> user-agent: test\r\n
316 s> host: $LOCALIP:$HGPORT\r\n (glob) 322 s> host: $LOCALIP:$HGPORT\r\n (glob)
317 s> \r\n 323 s> \r\n
318 s> makefile('rb', None) 324 s> makefile('rb', None)
319 s> HTTP/1.1 404 Not Found\r\n 325 s> HTTP/1.1 404 Not Found\r\n