Mercurial > public > mercurial-scm > hg
comparison mercurial/wireprotoserver.py @ 37295:45b39c69fae0
wireproto: separate commands tables for version 1 and 2 commands
We can't easily reuse existing command handlers for version 2
commands because the response types will be different. e.g. many
commands return nodes encoded as hex. Our new wire protocol is
binary safe, so we'll wish to encode nodes as binary.
We /could/ teach each command handler to look at the protocol
handler and change behavior based on the version in use. However,
this would make logic a bit unwieldy over time and would make
it harder to design a unified protocol handler interface. I think
it's better to create a clean break between version 1 and version 2
of commands on the server.
What I imagine happening is we will have separate @wireprotocommand
functions for each protocol generation. Those functions will parse the
request, dispatch to a common function to process it, then generate
the response in its own, transport-specific manner.
This commit establishes a separate table for tracking version 1
commands from version 2 commands. The HTTP server pieces have been
updated to use this new table.
Most commands are marked as both version 1 and version 2, so there is
little practical impact to this change.
A side-effect of this change is we now rely on transport registration
in wireprototypes.TRANSPORTS and certain properties of the protocol
interface. So a test had to be updated to conform.
Differential Revision: https://phab.mercurial-scm.org/D2982
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Wed, 28 Mar 2018 10:40:41 -0700 |
parents | 3d0e2cd86e05 |
children | 78103e4138b1 |
comparison
equal
deleted
inserted
replaced
37294:27527d8cff5c | 37295:45b39c69fae0 |
---|---|
333 # Extra commands that we handle that aren't really wire protocol | 333 # Extra commands that we handle that aren't really wire protocol |
334 # commands. Think extra hard before making this hackery available to | 334 # commands. Think extra hard before making this hackery available to |
335 # extension. | 335 # extension. |
336 extracommands = {'multirequest'} | 336 extracommands = {'multirequest'} |
337 | 337 |
338 if command not in wireproto.commands and command not in extracommands: | 338 if command not in wireproto.commandsv2 and command not in extracommands: |
339 res.status = b'404 Not Found' | 339 res.status = b'404 Not Found' |
340 res.headers[b'Content-Type'] = b'text/plain' | 340 res.headers[b'Content-Type'] = b'text/plain' |
341 res.setbodybytes(_('unknown wire protocol command: %s\n') % command) | 341 res.setbodybytes(_('unknown wire protocol command: %s\n') % command) |
342 return | 342 return |
343 | 343 |
344 repo = rctx.repo | 344 repo = rctx.repo |
345 ui = repo.ui | 345 ui = repo.ui |
346 | 346 |
347 proto = httpv2protocolhandler(req, ui) | 347 proto = httpv2protocolhandler(req, ui) |
348 | 348 |
349 if (not wireproto.commands.commandavailable(command, proto) | 349 if (not wireproto.commandsv2.commandavailable(command, proto) |
350 and command not in extracommands): | 350 and command not in extracommands): |
351 res.status = b'404 Not Found' | 351 res.status = b'404 Not Found' |
352 res.headers[b'Content-Type'] = b'text/plain' | 352 res.headers[b'Content-Type'] = b'text/plain' |
353 res.setbodybytes(_('invalid wire protocol command: %s') % command) | 353 res.setbodybytes(_('invalid wire protocol command: %s') % command) |
354 return | 354 return |
500 # iff each command is the same. | 500 # iff each command is the same. |
501 | 501 |
502 proto = httpv2protocolhandler(req, ui, args=command['args']) | 502 proto = httpv2protocolhandler(req, ui, args=command['args']) |
503 | 503 |
504 if reqcommand == b'multirequest': | 504 if reqcommand == b'multirequest': |
505 if not wireproto.commands.commandavailable(command['command'], proto): | 505 if not wireproto.commandsv2.commandavailable(command['command'], proto): |
506 # TODO proper error mechanism | 506 # TODO proper error mechanism |
507 res.status = b'200 OK' | 507 res.status = b'200 OK' |
508 res.headers[b'Content-Type'] = b'text/plain' | 508 res.headers[b'Content-Type'] = b'text/plain' |
509 res.setbodybytes(_('wire protocol command not available: %s') % | 509 res.setbodybytes(_('wire protocol command not available: %s') % |
510 command['command']) | 510 command['command']) |
511 return True | 511 return True |
512 | 512 |
513 # TODO don't use assert here, since it may be elided by -O. | 513 # TODO don't use assert here, since it may be elided by -O. |
514 assert authedperm in (b'ro', b'rw') | 514 assert authedperm in (b'ro', b'rw') |
515 wirecommand = wireproto.commands[command['command']] | 515 wirecommand = wireproto.commandsv2[command['command']] |
516 assert wirecommand.permission in ('push', 'pull') | 516 assert wirecommand.permission in ('push', 'pull') |
517 | 517 |
518 if authedperm == b'ro' and wirecommand.permission != 'pull': | 518 if authedperm == b'ro' and wirecommand.permission != 'pull': |
519 # TODO proper error mechanism | 519 # TODO proper error mechanism |
520 res.status = b'403 Forbidden' | 520 res.status = b'403 Forbidden' |