##// END OF EJS Templates
wireprototypes: move wire protocol response types to new module...
Gregory Szorc -
r36090:cd6ab329 default
parent child Browse files
Show More
@@ -0,0 +1,61 b''
1 # Copyright 2018 Gregory Szorc <gregory.szorc@gmail.com>
2 #
3 # This software may be used and distributed according to the terms of the
4 # GNU General Public License version 2 or any later version.
5
6 from __future__ import absolute_import
7
8 class ooberror(object):
9 """wireproto reply: failure of a batch of operation
10
11 Something failed during a batch call. The error message is stored in
12 `self.message`.
13 """
14 def __init__(self, message):
15 self.message = message
16
17 class pushres(object):
18 """wireproto reply: success with simple integer return
19
20 The call was successful and returned an integer contained in `self.res`.
21 """
22 def __init__(self, res, output):
23 self.res = res
24 self.output = output
25
26 class pusherr(object):
27 """wireproto reply: failure
28
29 The call failed. The `self.res` attribute contains the error message.
30 """
31 def __init__(self, res, output):
32 self.res = res
33 self.output = output
34
35 class streamres(object):
36 """wireproto reply: binary stream
37
38 The call was successful and the result is a stream.
39
40 Accepts a generator containing chunks of data to be sent to the client.
41
42 ``prefer_uncompressed`` indicates that the data is expected to be
43 uncompressable and that the stream should therefore use the ``none``
44 engine.
45 """
46 def __init__(self, gen=None, prefer_uncompressed=False):
47 self.gen = gen
48 self.prefer_uncompressed = prefer_uncompressed
49
50 class streamreslegacy(object):
51 """wireproto reply: uncompressed binary stream
52
53 The call was successful and the result is a stream.
54
55 Accepts a generator containing chunks of data to be sent to the client.
56
57 Like ``streamres``, but sends an uncompressed data for "version 1" clients
58 using the application/mercurial-0.1 media type.
59 """
60 def __init__(self, gen=None):
61 self.gen = gen
@@ -31,11 +31,18 b' from . import ('
31 repository,
31 repository,
32 streamclone,
32 streamclone,
33 util,
33 util,
34 wireprototypes,
34 )
35 )
35
36
36 urlerr = util.urlerr
37 urlerr = util.urlerr
37 urlreq = util.urlreq
38 urlreq = util.urlreq
38
39
40 ooberror = wireprototypes.ooberror
41 pushres = wireprototypes.pushres
42 pusherr = wireprototypes.pusherr
43 streamres = wireprototypes.streamres
44 streamres_legacy = wireprototypes.streamreslegacy
45
39 bundle2requiredmain = _('incompatible Mercurial client; bundle2 required')
46 bundle2requiredmain = _('incompatible Mercurial client; bundle2 required')
40 bundle2requiredhint = _('see https://www.mercurial-scm.org/wiki/'
47 bundle2requiredhint = _('see https://www.mercurial-scm.org/wiki/'
41 'IncompatibleClient')
48 'IncompatibleClient')
@@ -477,60 +484,6 b' class wirepeer(repository.legacypeer):'
477 # server side
484 # server side
478
485
479 # wire protocol command can either return a string or one of these classes.
486 # wire protocol command can either return a string or one of these classes.
480 class streamres(object):
481 """wireproto reply: binary stream
482
483 The call was successful and the result is a stream.
484
485 Accepts a generator containing chunks of data to be sent to the client.
486
487 ``prefer_uncompressed`` indicates that the data is expected to be
488 uncompressable and that the stream should therefore use the ``none``
489 engine.
490 """
491 def __init__(self, gen=None, prefer_uncompressed=False):
492 self.gen = gen
493 self.prefer_uncompressed = prefer_uncompressed
494
495 class streamres_legacy(object):
496 """wireproto reply: uncompressed binary stream
497
498 The call was successful and the result is a stream.
499
500 Accepts a generator containing chunks of data to be sent to the client.
501
502 Like ``streamres``, but sends an uncompressed data for "version 1" clients
503 using the application/mercurial-0.1 media type.
504 """
505 def __init__(self, gen=None):
506 self.gen = gen
507
508 class pushres(object):
509 """wireproto reply: success with simple integer return
510
511 The call was successful and returned an integer contained in `self.res`.
512 """
513 def __init__(self, res, output):
514 self.res = res
515 self.output = output
516
517 class pusherr(object):
518 """wireproto reply: failure
519
520 The call failed. The `self.res` attribute contains the error message.
521 """
522 def __init__(self, res, output):
523 self.res = res
524 self.output = output
525
526 class ooberror(object):
527 """wireproto reply: failure of a batch of operation
528
529 Something failed during a batch call. The error message is stored in
530 `self.message`.
531 """
532 def __init__(self, message):
533 self.message = message
534
487
535 def getdispatchrepo(repo, proto, command):
488 def getdispatchrepo(repo, proto, command):
536 """Obtain the repo used for processing wire protocol commands.
489 """Obtain the repo used for processing wire protocol commands.
@@ -20,6 +20,7 b' from . import ('
20 pycompat,
20 pycompat,
21 util,
21 util,
22 wireproto,
22 wireproto,
23 wireprototypes,
23 )
24 )
24
25
25 stringio = util.stringio
26 stringio = util.stringio
@@ -273,11 +274,11 b' def _callhttp(repo, req, proto, cmd):'
273 if isinstance(rsp, bytes):
274 if isinstance(rsp, bytes):
274 req.respond(HTTP_OK, HGTYPE, body=rsp)
275 req.respond(HTTP_OK, HGTYPE, body=rsp)
275 return []
276 return []
276 elif isinstance(rsp, wireproto.streamres_legacy):
277 elif isinstance(rsp, wireprototypes.streamreslegacy):
277 gen = rsp.gen
278 gen = rsp.gen
278 req.respond(HTTP_OK, HGTYPE)
279 req.respond(HTTP_OK, HGTYPE)
279 return gen
280 return gen
280 elif isinstance(rsp, wireproto.streamres):
281 elif isinstance(rsp, wireprototypes.streamres):
281 gen = rsp.gen
282 gen = rsp.gen
282
283
283 # This code for compression should not be streamres specific. It
284 # This code for compression should not be streamres specific. It
@@ -291,18 +292,18 b' def _callhttp(repo, req, proto, cmd):'
291
292
292 req.respond(HTTP_OK, mediatype)
293 req.respond(HTTP_OK, mediatype)
293 return gen
294 return gen
294 elif isinstance(rsp, wireproto.pushres):
295 elif isinstance(rsp, wireprototypes.pushres):
295 rsp = '%d\n%s' % (rsp.res, rsp.output)
296 rsp = '%d\n%s' % (rsp.res, rsp.output)
296 req.respond(HTTP_OK, HGTYPE, body=rsp)
297 req.respond(HTTP_OK, HGTYPE, body=rsp)
297 return []
298 return []
298 elif isinstance(rsp, wireproto.pusherr):
299 elif isinstance(rsp, wireprototypes.pusherr):
299 # This is the httplib workaround documented in _handlehttperror().
300 # This is the httplib workaround documented in _handlehttperror().
300 req.drain()
301 req.drain()
301
302
302 rsp = '0\n%s\n' % rsp.res
303 rsp = '0\n%s\n' % rsp.res
303 req.respond(HTTP_OK, HGTYPE, body=rsp)
304 req.respond(HTTP_OK, HGTYPE, body=rsp)
304 return []
305 return []
305 elif isinstance(rsp, wireproto.ooberror):
306 elif isinstance(rsp, wireprototypes.ooberror):
306 rsp = rsp.message
307 rsp = rsp.message
307 req.respond(HTTP_OK, HGERRTYPE, body=rsp)
308 req.respond(HTTP_OK, HGERRTYPE, body=rsp)
308 return []
309 return []
@@ -434,16 +435,16 b' class sshserver(object):'
434
435
435 if isinstance(rsp, bytes):
436 if isinstance(rsp, bytes):
436 _sshv1respondbytes(self._fout, rsp)
437 _sshv1respondbytes(self._fout, rsp)
437 elif isinstance(rsp, wireproto.streamres):
438 elif isinstance(rsp, wireprototypes.streamres):
438 _sshv1respondstream(self._fout, rsp)
439 _sshv1respondstream(self._fout, rsp)
439 elif isinstance(rsp, wireproto.streamres_legacy):
440 elif isinstance(rsp, wireprototypes.streamreslegacy):
440 _sshv1respondstream(self._fout, rsp)
441 _sshv1respondstream(self._fout, rsp)
441 elif isinstance(rsp, wireproto.pushres):
442 elif isinstance(rsp, wireprototypes.pushres):
442 _sshv1respondbytes(self._fout, b'')
443 _sshv1respondbytes(self._fout, b'')
443 _sshv1respondbytes(self._fout, bytes(rsp.res))
444 _sshv1respondbytes(self._fout, bytes(rsp.res))
444 elif isinstance(rsp, wireproto.pusherr):
445 elif isinstance(rsp, wireprototypes.pusherr):
445 _sshv1respondbytes(self._fout, rsp.res)
446 _sshv1respondbytes(self._fout, rsp.res)
446 elif isinstance(rsp, wireproto.ooberror):
447 elif isinstance(rsp, wireprototypes.ooberror):
447 _sshv1respondooberror(self._fout, self._ui.ferr, rsp.message)
448 _sshv1respondooberror(self._fout, self._ui.ferr, rsp.message)
448 else:
449 else:
449 raise error.ProgrammingError('unhandled response type from '
450 raise error.ProgrammingError('unhandled response type from '
General Comments 0
You need to be logged in to leave comments. Login now