Mercurial > hg
annotate mercurial/wireprotoserver.py @ 52282:51a350a22d0c
branching: merge stable into default
author | Raphaël Gomès <rgomes@octobus.net> |
---|---|
date | Wed, 20 Nov 2024 15:53:19 +0100 |
parents | 1938d72e7a16 |
children |
rev | line source |
---|---|
5598
d534ba1c4eb4
separate the wire protocol commands from the user interface commands
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff
changeset
|
1 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> |
46819
d4ba4d51f85f
contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents:
46697
diff
changeset
|
2 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com> |
5598
d534ba1c4eb4
separate the wire protocol commands from the user interface commands
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff
changeset
|
3 # |
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
8109
diff
changeset
|
4 # This software may be used and distributed according to the terms of the |
10263 | 5 # GNU General Public License version 2 or any later version. |
5598
d534ba1c4eb4
separate the wire protocol commands from the user interface commands
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff
changeset
|
6 |
51863
f4733654f144
typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents:
51572
diff
changeset
|
7 from __future__ import annotations |
27046
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
20903
diff
changeset
|
8 |
36066
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
9 import contextlib |
30764
e75463e3179f
protocol: send application/mercurial-0.2 responses to capable clients
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30759
diff
changeset
|
10 import struct |
36523
e7411fb7ba7f
wireprotoserver: ability to run an SSH server until an event is set
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36372
diff
changeset
|
11 import threading |
27046
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
20903
diff
changeset
|
12 |
35859
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
13 from .i18n import _ |
35856
ef3a24a023ec
wireprotoserver: rename hgweb.protocol to wireprotoserver (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35750
diff
changeset
|
14 from . import ( |
35859
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
15 encoding, |
34509
e21f274cccea
hgweb: in protocol adapter, avoid control reaching end of non-void function
Augie Fackler <augie@google.com>
parents:
33821
diff
changeset
|
16 error, |
34742
5a9cad0dfddb
hgweb: when unpacking args from request form, convert to bytes
Augie Fackler <augie@google.com>
parents:
34740
diff
changeset
|
17 pycompat, |
27046
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
20903
diff
changeset
|
18 util, |
36073
cd6ab329c5c7
wireprototypes: move wire protocol response types to new module
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36072
diff
changeset
|
19 wireprototypes, |
37785
b4d85bc122bd
wireproto: rename wireproto to wireprotov1server (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37783
diff
changeset
|
20 wireprotov1server, |
27046
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
20903
diff
changeset
|
21 ) |
37120
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37061
diff
changeset
|
22 from .utils import ( |
42041
3e47d1ec9da5
util: extract compression code in `mercurial.utils.compression`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
41285
diff
changeset
|
23 compression, |
46697
71443f742886
wireprotoserver: convert ErrorResponse to bytes
Matt Harbison <matt_harbison@yahoo.com>
parents:
45818
diff
changeset
|
24 stringutil, |
37120
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37061
diff
changeset
|
25 ) |
35856
ef3a24a023ec
wireprotoserver: rename hgweb.protocol to wireprotoserver (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35750
diff
changeset
|
26 |
28861
86db5cb55d46
pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents:
28530
diff
changeset
|
27 stringio = util.stringio |
5963
5be210afe1b8
hgweb: explicitly check if requested command exists
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
5915
diff
changeset
|
28 |
28883
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28861
diff
changeset
|
29 urlerr = util.urlerr |
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28861
diff
changeset
|
30 urlreq = util.urlreq |
032c4c2f802a
pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents:
28861
diff
changeset
|
31 |
35858
1b76a9e0a9de
wireprotoserver: don't import symbol from hgweb.common
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35857
diff
changeset
|
32 HTTP_OK = 200 |
1b76a9e0a9de
wireprotoserver: don't import symbol from hgweb.common
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35857
diff
changeset
|
33 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
34 HGTYPE = b'application/mercurial-0.1' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
35 HGTYPE2 = b'application/mercurial-0.2' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
36 HGERRTYPE = b'application/hg-error' |
11595
368cd5325348
protocol: move hgweb protocol support back into protocol.py
Matt Mackall <mpm@selenic.com>
parents:
11594
diff
changeset
|
37 |
36536
3cd245945ef3
wireprotoserver: move SSHV1 and SSHV2 constants to wireprototypes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36526
diff
changeset
|
38 SSHV1 = wireprototypes.SSHV1 |
35976
48a3a9283f09
sshpeer: initial definition and implementation of new SSH protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35900
diff
changeset
|
39 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
40 |
36846
14f70c44af6c
wireprotoserver: access headers through parsed request
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36813
diff
changeset
|
41 def decodevaluefromheaders(req, headerprefix): |
34744
0a2ef612ad50
hgweb: fix decodevaluefromheaders to always return a bytes value
Augie Fackler <augie@google.com>
parents:
34743
diff
changeset
|
42 """Decode a long value from multiple HTTP request headers. |
0a2ef612ad50
hgweb: fix decodevaluefromheaders to always return a bytes value
Augie Fackler <augie@google.com>
parents:
34743
diff
changeset
|
43 |
0a2ef612ad50
hgweb: fix decodevaluefromheaders to always return a bytes value
Augie Fackler <augie@google.com>
parents:
34743
diff
changeset
|
44 Returns the value as a bytes, not a str. |
0a2ef612ad50
hgweb: fix decodevaluefromheaders to always return a bytes value
Augie Fackler <augie@google.com>
parents:
34743
diff
changeset
|
45 """ |
30759
3f5f0c98cd18
httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30562
diff
changeset
|
46 chunks = [] |
3f5f0c98cd18
httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30562
diff
changeset
|
47 i = 1 |
3f5f0c98cd18
httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30562
diff
changeset
|
48 while True: |
36846
14f70c44af6c
wireprotoserver: access headers through parsed request
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36813
diff
changeset
|
49 v = req.headers.get(b'%s-%d' % (headerprefix, i)) |
30759
3f5f0c98cd18
httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30562
diff
changeset
|
50 if v is None: |
3f5f0c98cd18
httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30562
diff
changeset
|
51 break |
34744
0a2ef612ad50
hgweb: fix decodevaluefromheaders to always return a bytes value
Augie Fackler <augie@google.com>
parents:
34743
diff
changeset
|
52 chunks.append(pycompat.bytesurl(v)) |
30759
3f5f0c98cd18
httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30562
diff
changeset
|
53 i += 1 |
3f5f0c98cd18
httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30562
diff
changeset
|
54 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
55 return b''.join(chunks) |
30759
3f5f0c98cd18
httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30562
diff
changeset
|
56 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
57 |
52125
1938d72e7a16
wireprotoserver: subclass the new `baseprotocolhandler` Protocol class
Matt Harbison <matt_harbison@yahoo.com>
parents:
51863
diff
changeset
|
58 class httpv1protocolhandler(wireprototypes.baseprotocolhandler): |
36867
a755fd3b7146
hgweb: expose URL scheme and REMOTE_* attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36862
diff
changeset
|
59 def __init__(self, req, ui, checkperm): |
36846
14f70c44af6c
wireprotoserver: access headers through parsed request
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36813
diff
changeset
|
60 self._req = req |
35866
d747cf39cf70
wireprotoserver: make attributes private
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35865
diff
changeset
|
61 self._ui = ui |
36801
66de4555cefd
wireproto: formalize permissions checking as part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36800
diff
changeset
|
62 self._checkperm = checkperm |
37393
afcfdf53e4b5
wireproto: provide accessors for client capabilities
Joerg Sonnenberger <joerg@bec.de>
parents:
37296
diff
changeset
|
63 self._protocaps = None |
35873
29759c46aa1a
wireprotoserver: make name part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35872
diff
changeset
|
64 |
29759c46aa1a
wireprotoserver: make name part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35872
diff
changeset
|
65 @property |
52125
1938d72e7a16
wireprotoserver: subclass the new `baseprotocolhandler` Protocol class
Matt Harbison <matt_harbison@yahoo.com>
parents:
51863
diff
changeset
|
66 def name(self) -> bytes: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
67 return b'http-v1' |
30562
b3a9ef3d30e8
protocol: declare transport protocol name
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30466
diff
changeset
|
68 |
11595
368cd5325348
protocol: move hgweb protocol support back into protocol.py
Matt Mackall <mpm@selenic.com>
parents:
11594
diff
changeset
|
69 def getargs(self, args): |
14093
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
13721
diff
changeset
|
70 knownargs = self._args() |
11595
368cd5325348
protocol: move hgweb protocol support back into protocol.py
Matt Mackall <mpm@selenic.com>
parents:
11594
diff
changeset
|
71 data = {} |
368cd5325348
protocol: move hgweb protocol support back into protocol.py
Matt Mackall <mpm@selenic.com>
parents:
11594
diff
changeset
|
72 keys = args.split() |
368cd5325348
protocol: move hgweb protocol support back into protocol.py
Matt Mackall <mpm@selenic.com>
parents:
11594
diff
changeset
|
73 for k in keys: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
74 if k == b'*': |
11595
368cd5325348
protocol: move hgweb protocol support back into protocol.py
Matt Mackall <mpm@selenic.com>
parents:
11594
diff
changeset
|
75 star = {} |
14093
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
13721
diff
changeset
|
76 for key in knownargs.keys(): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
77 if key != b'cmd' and key not in keys: |
14093
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
13721
diff
changeset
|
78 star[key] = knownargs[key][0] |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
79 data[b'*'] = star |
11595
368cd5325348
protocol: move hgweb protocol support back into protocol.py
Matt Mackall <mpm@selenic.com>
parents:
11594
diff
changeset
|
80 else: |
14093
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
13721
diff
changeset
|
81 data[k] = knownargs[k][0] |
11595
368cd5325348
protocol: move hgweb protocol support back into protocol.py
Matt Mackall <mpm@selenic.com>
parents:
11594
diff
changeset
|
82 return [data[k] for k in keys] |
35863
49426bb4476c
wireprotoserver: add some blank lines between methods
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35860
diff
changeset
|
83 |
14093
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
13721
diff
changeset
|
84 def _args(self): |
36862
ec0af9c59270
hgweb: use a multidict for holding query string parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36861
diff
changeset
|
85 args = self._req.qsparams.asdictoflists() |
36846
14f70c44af6c
wireprotoserver: access headers through parsed request
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36813
diff
changeset
|
86 postlen = int(self._req.headers.get(b'X-HgArgs-Post', 0)) |
28530
fd2acc5046f6
http: support sending hgargs via POST body instead of in GET or headers
Augie Fackler <augie@google.com>
parents:
27046
diff
changeset
|
87 if postlen: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
88 args.update( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
89 urlreq.parseqs( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
90 self._req.bodyfh.read(postlen), keep_blank_values=True |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
91 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
92 ) |
28530
fd2acc5046f6
http: support sending hgargs via POST body instead of in GET or headers
Augie Fackler <augie@google.com>
parents:
27046
diff
changeset
|
93 return args |
30759
3f5f0c98cd18
httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30562
diff
changeset
|
94 |
36846
14f70c44af6c
wireprotoserver: access headers through parsed request
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36813
diff
changeset
|
95 argvalue = decodevaluefromheaders(self._req, b'X-HgArg') |
36077
a3d42d1865f1
wireprotoserver: define and use parse_qs from urllib
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36075
diff
changeset
|
96 args.update(urlreq.parseqs(argvalue, keep_blank_values=True)) |
14093
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
13721
diff
changeset
|
97 return args |
35863
49426bb4476c
wireprotoserver: add some blank lines between methods
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35860
diff
changeset
|
98 |
37393
afcfdf53e4b5
wireproto: provide accessors for client capabilities
Joerg Sonnenberger <joerg@bec.de>
parents:
37296
diff
changeset
|
99 def getprotocaps(self): |
afcfdf53e4b5
wireproto: provide accessors for client capabilities
Joerg Sonnenberger <joerg@bec.de>
parents:
37296
diff
changeset
|
100 if self._protocaps is None: |
37590
9170df9106a8
wireprotoserver: headers are bytes for us internally, use bytes
Augie Fackler <augie@google.com>
parents:
37557
diff
changeset
|
101 value = decodevaluefromheaders(self._req, b'X-HgProto') |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
102 self._protocaps = set(value.split(b' ')) |
37393
afcfdf53e4b5
wireproto: provide accessors for client capabilities
Joerg Sonnenberger <joerg@bec.de>
parents:
37296
diff
changeset
|
103 return self._protocaps |
afcfdf53e4b5
wireproto: provide accessors for client capabilities
Joerg Sonnenberger <joerg@bec.de>
parents:
37296
diff
changeset
|
104 |
37414
2d965bfeb8f6
wireproto: allow direct stream processing for unbundle
Joerg Sonnenberger <joerg@bec.de>
parents:
37393
diff
changeset
|
105 def getpayload(self): |
36847
ed0456fde625
hgweb: handle CONTENT_LENGTH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36846
diff
changeset
|
106 # Existing clients *always* send Content-Length. |
ed0456fde625
hgweb: handle CONTENT_LENGTH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36846
diff
changeset
|
107 length = int(self._req.headers[b'Content-Length']) |
ed0456fde625
hgweb: handle CONTENT_LENGTH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36846
diff
changeset
|
108 |
33821
3c91cc0c5fde
httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents:
33228
diff
changeset
|
109 # If httppostargs is used, we need to read Content-Length |
3c91cc0c5fde
httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents:
33228
diff
changeset
|
110 # minus the amount that was consumed by args. |
36846
14f70c44af6c
wireprotoserver: access headers through parsed request
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36813
diff
changeset
|
111 length -= int(self._req.headers.get(b'X-HgArgs-Post', 0)) |
37414
2d965bfeb8f6
wireproto: allow direct stream processing for unbundle
Joerg Sonnenberger <joerg@bec.de>
parents:
37393
diff
changeset
|
112 return util.filechunkiter(self._req.bodyfh, limit=length) |
35863
49426bb4476c
wireprotoserver: add some blank lines between methods
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35860
diff
changeset
|
113 |
36066
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
114 @contextlib.contextmanager |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
115 def mayberedirectstdio(self): |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
116 oldout = self._ui.fout |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
117 olderr = self._ui.ferr |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
118 |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
119 out = util.stringio() |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
120 |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
121 try: |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
122 self._ui.fout = out |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
123 self._ui.ferr = out |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
124 yield out |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
125 finally: |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
126 self._ui.fout = oldout |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
127 self._ui.ferr = olderr |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
128 |
36069
957e773614d0
wireprotoserver: rename _client to client (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36068
diff
changeset
|
129 def client(self): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
130 return b'remote:%s:%s:%s' % ( |
36867
a755fd3b7146
hgweb: expose URL scheme and REMOTE_* attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36862
diff
changeset
|
131 self._req.urlscheme, |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
132 urlreq.quote(self._req.remotehost or b''), |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
133 urlreq.quote(self._req.remoteuser or b''), |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
134 ) |
11595
368cd5325348
protocol: move hgweb protocol support back into protocol.py
Matt Mackall <mpm@selenic.com>
parents:
11594
diff
changeset
|
135 |
36613
6e585bca962e
wireproto: add transport specific capabilities in the transport
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36610
diff
changeset
|
136 def addcapabilities(self, repo, caps): |
37053
cd0ca979a8b8
wireproto: nominally don't expose "batch" to version 2 wire transports
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37052
diff
changeset
|
137 caps.append(b'batch') |
cd0ca979a8b8
wireproto: nominally don't expose "batch" to version 2 wire transports
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37052
diff
changeset
|
138 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
139 caps.append( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
140 b'httpheader=%d' % repo.ui.configint(b'server', b'maxhttpheaderlen') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
141 ) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
142 if repo.ui.configbool(b'experimental', b'httppostargs'): |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
143 caps.append(b'httppostargs') |
36613
6e585bca962e
wireproto: add transport specific capabilities in the transport
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36610
diff
changeset
|
144 |
6e585bca962e
wireproto: add transport specific capabilities in the transport
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36610
diff
changeset
|
145 # FUTURE advertise 0.2rx once support is implemented |
6e585bca962e
wireproto: add transport specific capabilities in the transport
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36610
diff
changeset
|
146 # FUTURE advertise minrx and mintx after consulting config option |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
147 caps.append(b'httpmediatype=0.1rx,0.1tx,0.2tx') |
36613
6e585bca962e
wireproto: add transport specific capabilities in the transport
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36610
diff
changeset
|
148 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
149 compengines = wireprototypes.supportedcompengines( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
150 repo.ui, compression.SERVERROLE |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
151 ) |
36613
6e585bca962e
wireproto: add transport specific capabilities in the transport
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36610
diff
changeset
|
152 if compengines: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
153 comptypes = b','.join( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
154 urlreq.quote(e.wireprotosupport().name) for e in compengines |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
155 ) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
156 caps.append(b'compression=%s' % comptypes) |
36613
6e585bca962e
wireproto: add transport specific capabilities in the transport
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36610
diff
changeset
|
157 |
6e585bca962e
wireproto: add transport specific capabilities in the transport
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36610
diff
changeset
|
158 return caps |
6e585bca962e
wireproto: add transport specific capabilities in the transport
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36610
diff
changeset
|
159 |
36801
66de4555cefd
wireproto: formalize permissions checking as part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36800
diff
changeset
|
160 def checkperm(self, perm): |
66de4555cefd
wireproto: formalize permissions checking as part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36800
diff
changeset
|
161 return self._checkperm(perm) |
66de4555cefd
wireproto: formalize permissions checking as part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36800
diff
changeset
|
162 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
163 |
36231
72812ad205d1
wireprotoserver: return to using iscmd() method
Augie Fackler <augie@google.com>
parents:
36223
diff
changeset
|
164 # This method exists mostly so that extensions like remotefilelog can |
72812ad205d1
wireprotoserver: return to using iscmd() method
Augie Fackler <augie@google.com>
parents:
36223
diff
changeset
|
165 # disable a kludgey legacy method only over http. As of early 2018, |
72812ad205d1
wireprotoserver: return to using iscmd() method
Augie Fackler <augie@google.com>
parents:
36223
diff
changeset
|
166 # there are no other known users, so with any luck we can discard this |
72812ad205d1
wireprotoserver: return to using iscmd() method
Augie Fackler <augie@google.com>
parents:
36223
diff
changeset
|
167 # hook if remotefilelog becomes a first-party extension. |
11595
368cd5325348
protocol: move hgweb protocol support back into protocol.py
Matt Mackall <mpm@selenic.com>
parents:
11594
diff
changeset
|
168 def iscmd(cmd): |
37785
b4d85bc122bd
wireproto: rename wireproto to wireprotov1server (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37783
diff
changeset
|
169 return cmd in wireprotov1server.commands |
11595
368cd5325348
protocol: move hgweb protocol support back into protocol.py
Matt Mackall <mpm@selenic.com>
parents:
11594
diff
changeset
|
170 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
171 |
36877
02bea04b4c54
hgweb: transition permissions hooks to modern request type (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36867
diff
changeset
|
172 def handlewsgirequest(rctx, req, res, checkperm): |
36812
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
173 """Possibly process a wire protocol request. |
35984
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
174 |
36812
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
175 If the current request is a wire protocol request, the request is |
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
176 processed by this function. |
35984
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
177 |
36810
886fba199022
hgweb: only recognize wire protocol commands from query string (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36804
diff
changeset
|
178 ``req`` is a ``parsedrequest`` instance. |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
179 ``res`` is a ``wsgiresponse`` instance. |
36812
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
180 |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
181 Returns a bool indicating if the request was serviced. If set, the caller |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
182 should stop processing the request, as a response has already been issued. |
35984
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
183 """ |
36812
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
184 # Avoid cycle involving hg module. |
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
185 from .hgweb import common as hgwebcommon |
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
186 |
36801
66de4555cefd
wireproto: formalize permissions checking as part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36800
diff
changeset
|
187 repo = rctx.repo |
66de4555cefd
wireproto: formalize permissions checking as part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36800
diff
changeset
|
188 |
35984
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
189 # HTTP version 1 wire protocol requests are denoted by a "cmd" query |
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
190 # string parameter. If it isn't present, this isn't a wire protocol |
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
191 # request. |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
192 if b'cmd' not in req.qsparams: |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
193 return False |
35984
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
194 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
195 cmd = req.qsparams[b'cmd'] |
35984
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
196 |
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
197 # The "cmd" request parameter is used by both the wire protocol and hgweb. |
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
198 # While not all wire protocol commands are available for all transports, |
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
199 # if we see a "cmd" value that resembles a known wire protocol command, we |
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
200 # route it to a protocol handler. This is better than routing possible |
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
201 # wire protocol requests to hgweb because it prevents hgweb from using |
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
202 # known wire protocol commands and it is less confusing for machine |
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
203 # clients. |
36231
72812ad205d1
wireprotoserver: return to using iscmd() method
Augie Fackler <augie@google.com>
parents:
36223
diff
changeset
|
204 if not iscmd(cmd): |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
205 return False |
36812
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
206 |
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
207 # The "cmd" query string argument is only valid on the root path of the |
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
208 # repo. e.g. ``/?cmd=foo``, ``/repo?cmd=foo``. URL paths within the repo |
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
209 # like ``/blah?cmd=foo`` are not allowed. So don't recognize the request |
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
210 # in this case. We send an HTTP 404 for backwards compatibility reasons. |
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
211 if req.dispatchpath: |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
212 res.status = hgwebcommon.statusmessage(404) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
213 res.headers[b'Content-Type'] = HGTYPE |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
214 # TODO This is not a good response to issue for this request. This |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
215 # is mostly for BC for now. |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
216 res.setbodybytes(b'0\n%s\n' % b'Not Found') |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
217 return True |
35984
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
218 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
219 proto = httpv1protocolhandler( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
220 req, repo.ui, lambda perm: checkperm(rctx, req, perm) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
221 ) |
30764
e75463e3179f
protocol: send application/mercurial-0.2 responses to capable clients
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30759
diff
changeset
|
222 |
36812
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
223 # The permissions checker should be the only thing that can raise an |
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
224 # ErrorResponse. It is kind of a layer violation to catch an hgweb |
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
225 # exception here. So consider refactoring into a exception type that |
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
226 # is associated with the wire protocol. |
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
227 try: |
36867
a755fd3b7146
hgweb: expose URL scheme and REMOTE_* attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36862
diff
changeset
|
228 _callhttp(repo, req, res, proto, cmd) |
36812
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
229 except hgwebcommon.ErrorResponse as e: |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
230 for k, v in e.headers: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
231 res.headers[k] = v |
46697
71443f742886
wireprotoserver: convert ErrorResponse to bytes
Matt Harbison <matt_harbison@yahoo.com>
parents:
45818
diff
changeset
|
232 res.status = hgwebcommon.statusmessage( |
71443f742886
wireprotoserver: convert ErrorResponse to bytes
Matt Harbison <matt_harbison@yahoo.com>
parents:
45818
diff
changeset
|
233 e.code, stringutil.forcebytestr(e) |
71443f742886
wireprotoserver: convert ErrorResponse to bytes
Matt Harbison <matt_harbison@yahoo.com>
parents:
45818
diff
changeset
|
234 ) |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
235 # TODO This response body assumes the failed command was |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
236 # "unbundle." That assumption is not always valid. |
46697
71443f742886
wireprotoserver: convert ErrorResponse to bytes
Matt Harbison <matt_harbison@yahoo.com>
parents:
45818
diff
changeset
|
237 res.setbodybytes(b'0\n%s\n' % stringutil.forcebytestr(e)) |
36812
158d4ecc03c8
wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36810
diff
changeset
|
238 |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
239 return True |
35984
cdc93fe1da77
wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35982
diff
changeset
|
240 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
241 |
37393
afcfdf53e4b5
wireproto: provide accessors for client capabilities
Joerg Sonnenberger <joerg@bec.de>
parents:
37296
diff
changeset
|
242 def _httpresponsetype(ui, proto, prefer_uncompressed): |
36072
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
243 """Determine the appropriate response type and compression settings. |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
244 |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
245 Returns a tuple of (mediatype, compengine, engineopts). |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
246 """ |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
247 # Determine the response media type and compression engine based |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
248 # on the request parameters. |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
249 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
250 if b'0.2' in proto.getprotocaps(): |
36072
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
251 # All clients are expected to support uncompressed data. |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
252 if prefer_uncompressed: |
42041
3e47d1ec9da5
util: extract compression code in `mercurial.utils.compression`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
41285
diff
changeset
|
253 return HGTYPE2, compression._noopengine(), {} |
36072
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
254 |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
255 # Now find an agreed upon compression format. |
37785
b4d85bc122bd
wireproto: rename wireproto to wireprotov1server (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37783
diff
changeset
|
256 compformats = wireprotov1server.clientcompressionsupport(proto) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
257 for engine in wireprototypes.supportedcompengines( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
258 ui, compression.SERVERROLE |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
259 ): |
36072
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
260 if engine.wireprotosupport().name in compformats: |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
261 opts = {} |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
262 level = ui.configint(b'server', b'%slevel' % engine.name()) |
36072
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
263 if level is not None: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
264 opts[b'level'] = level |
36072
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
265 |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
266 return HGTYPE2, engine, opts |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
267 |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
268 # No mutually supported compression format. Fall back to the |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
269 # legacy protocol. |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
270 |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
271 # Don't allow untrusted settings because disabling compression or |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
272 # setting a very high compression level could lead to flooding |
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
273 # the server's network or CPU. |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
274 opts = {b'level': ui.configint(b'server', b'zliblevel')} |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
275 return HGTYPE, util.compengines[b'zlib'], opts |
36072
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
276 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
277 |
36867
a755fd3b7146
hgweb: expose URL scheme and REMOTE_* attributes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36862
diff
changeset
|
278 def _callhttp(repo, req, res, proto, cmd): |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
279 # Avoid cycle involving hg module. |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
280 from .hgweb import common as hgwebcommon |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
281 |
35750
a39a9df7ecca
wireproto: split streamres into legacy and modern case
Joerg Sonnenberger <joerg@bec.de>
parents:
35705
diff
changeset
|
282 def genversion2(gen, engine, engineopts): |
30764
e75463e3179f
protocol: send application/mercurial-0.2 responses to capable clients
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30759
diff
changeset
|
283 # application/mercurial-0.2 always sends a payload header |
e75463e3179f
protocol: send application/mercurial-0.2 responses to capable clients
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30759
diff
changeset
|
284 # identifying the compression engine. |
e75463e3179f
protocol: send application/mercurial-0.2 responses to capable clients
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30759
diff
changeset
|
285 name = engine.wireprotosupport().name |
e75463e3179f
protocol: send application/mercurial-0.2 responses to capable clients
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30759
diff
changeset
|
286 assert 0 < len(name) < 256 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
287 yield struct.pack(b'B', len(name)) |
30764
e75463e3179f
protocol: send application/mercurial-0.2 responses to capable clients
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30759
diff
changeset
|
288 yield name |
e75463e3179f
protocol: send application/mercurial-0.2 responses to capable clients
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30759
diff
changeset
|
289 |
35750
a39a9df7ecca
wireproto: split streamres into legacy and modern case
Joerg Sonnenberger <joerg@bec.de>
parents:
35705
diff
changeset
|
290 for chunk in gen: |
a39a9df7ecca
wireproto: split streamres into legacy and modern case
Joerg Sonnenberger <joerg@bec.de>
parents:
35705
diff
changeset
|
291 yield chunk |
30764
e75463e3179f
protocol: send application/mercurial-0.2 responses to capable clients
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30759
diff
changeset
|
292 |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
293 def setresponse(code, contenttype, bodybytes=None, bodygen=None): |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
294 if code == HTTP_OK: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
295 res.status = b'200 Script output follows' |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
296 else: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
297 res.status = hgwebcommon.statusmessage(code) |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
298 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
299 res.headers[b'Content-Type'] = contenttype |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
300 |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
301 if bodybytes is not None: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
302 res.setbodybytes(bodybytes) |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
303 if bodygen is not None: |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
304 res.setbodygen(bodygen) |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
305 |
37785
b4d85bc122bd
wireproto: rename wireproto to wireprotov1server (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37783
diff
changeset
|
306 if not wireprotov1server.commands.commandavailable(cmd, proto): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
307 setresponse( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
308 HTTP_OK, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
309 HGERRTYPE, |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
310 _( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
311 b'requested wire protocol command is not available over ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
312 b'HTTP' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
313 ), |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
314 ) |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
315 return |
35982
5a56bf4180ad
wireproto: function for testing if wire protocol command is available
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35976
diff
changeset
|
316 |
37785
b4d85bc122bd
wireproto: rename wireproto to wireprotov1server (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37783
diff
changeset
|
317 proto.checkperm(wireprotov1server.commands[cmd].permission) |
36799
c638a13093cf
wireprotoserver: check permissions in main dispatch function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36798
diff
changeset
|
318 |
50443
3b199593fedd
hidden: add support to explicitly access hidden changesets via HTTP
Manuel Jacob <me@manueljacob.de>
parents:
49284
diff
changeset
|
319 accesshidden = hgwebcommon.hashiddenaccess(repo, req) |
3b199593fedd
hidden: add support to explicitly access hidden changesets via HTTP
Manuel Jacob <me@manueljacob.de>
parents:
49284
diff
changeset
|
320 rsp = wireprotov1server.dispatch(repo, proto, cmd, accesshidden) |
36798
7574c8173d5e
wireprotoserver: check if command available before calling it
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36760
diff
changeset
|
321 |
34510
c23fa3103925
hgweb: in protocol adapter, look for bytes instances, not str
Augie Fackler <augie@google.com>
parents:
34509
diff
changeset
|
322 if isinstance(rsp, bytes): |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
323 setresponse(HTTP_OK, HGTYPE, bodybytes=rsp) |
36074
2f7290555c96
wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36073
diff
changeset
|
324 elif isinstance(rsp, wireprototypes.bytesresponse): |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
325 setresponse(HTTP_OK, HGTYPE, bodybytes=rsp.data) |
36073
cd6ab329c5c7
wireprototypes: move wire protocol response types to new module
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36072
diff
changeset
|
326 elif isinstance(rsp, wireprototypes.streamreslegacy): |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
327 setresponse(HTTP_OK, HGTYPE, bodygen=rsp.gen) |
36073
cd6ab329c5c7
wireprototypes: move wire protocol response types to new module
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36072
diff
changeset
|
328 elif isinstance(rsp, wireprototypes.streamres): |
35705
8cdb671dbd0b
wireproto: drop support for reader interface from streamres (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
34744
diff
changeset
|
329 gen = rsp.gen |
30466
2add671bf55b
wireproto: perform chunking and compression at protocol layer (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30366
diff
changeset
|
330 |
30764
e75463e3179f
protocol: send application/mercurial-0.2 responses to capable clients
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30759
diff
changeset
|
331 # This code for compression should not be streamres specific. It |
e75463e3179f
protocol: send application/mercurial-0.2 responses to capable clients
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30759
diff
changeset
|
332 # is here because we only compress streamres at the moment. |
36072
341c886e411e
wireprotoserver: move responsetype() out of http handler
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36071
diff
changeset
|
333 mediatype, engine, engineopts = _httpresponsetype( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
334 repo.ui, proto, rsp.prefer_uncompressed |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
335 ) |
35750
a39a9df7ecca
wireproto: split streamres into legacy and modern case
Joerg Sonnenberger <joerg@bec.de>
parents:
35705
diff
changeset
|
336 gen = engine.compressstream(gen, engineopts) |
30466
2add671bf55b
wireproto: perform chunking and compression at protocol layer (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30366
diff
changeset
|
337 |
35750
a39a9df7ecca
wireproto: split streamres into legacy and modern case
Joerg Sonnenberger <joerg@bec.de>
parents:
35705
diff
changeset
|
338 if mediatype == HGTYPE2: |
a39a9df7ecca
wireproto: split streamres into legacy and modern case
Joerg Sonnenberger <joerg@bec.de>
parents:
35705
diff
changeset
|
339 gen = genversion2(gen, engine, engineopts) |
30764
e75463e3179f
protocol: send application/mercurial-0.2 responses to capable clients
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30759
diff
changeset
|
340 |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
341 setresponse(HTTP_OK, mediatype, bodygen=gen) |
36073
cd6ab329c5c7
wireprototypes: move wire protocol response types to new module
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36072
diff
changeset
|
342 elif isinstance(rsp, wireprototypes.pushres): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
343 rsp = b'%d\n%s' % (rsp.res, rsp.output) |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
344 setresponse(HTTP_OK, HGTYPE, bodybytes=rsp) |
36073
cd6ab329c5c7
wireprototypes: move wire protocol response types to new module
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36072
diff
changeset
|
345 elif isinstance(rsp, wireprototypes.pusherr): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
346 rsp = b'0\n%s\n' % rsp.res |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
347 res.drain = True |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
348 setresponse(HTTP_OK, HGTYPE, bodybytes=rsp) |
36073
cd6ab329c5c7
wireprototypes: move wire protocol response types to new module
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36072
diff
changeset
|
349 elif isinstance(rsp, wireprototypes.ooberror): |
36861
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
350 setresponse(HTTP_OK, HGERRTYPE, bodybytes=rsp.message) |
a88d68dc3ee8
hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36857
diff
changeset
|
351 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
352 raise error.ProgrammingError(b'hgweb.protocol internal failure', rsp) |
35986
98a00aa0288d
wireprotoserver: move error response handling out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35984
diff
changeset
|
353 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
354 |
36064
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
355 def _sshv1respondbytes(fout, value): |
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
356 """Send a bytes response for protocol version 1.""" |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
357 fout.write(b'%d\n' % len(value)) |
36064
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
358 fout.write(value) |
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
359 fout.flush() |
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
360 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
361 |
36064
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
362 def _sshv1respondstream(fout, source): |
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
363 write = fout.write |
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
364 for chunk in source.gen: |
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
365 write(chunk) |
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
366 fout.flush() |
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
367 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
368 |
36064
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
369 def _sshv1respondooberror(fout, ferr, rsp): |
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
370 ferr.write(b'%s\n-\n' % rsp) |
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
371 ferr.flush() |
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
372 fout.write(b'\n') |
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
373 fout.flush() |
5767664d39a5
wireprotoserver: extract SSH response handling functions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35988
diff
changeset
|
374 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
375 |
52125
1938d72e7a16
wireprotoserver: subclass the new `baseprotocolhandler` Protocol class
Matt Harbison <matt_harbison@yahoo.com>
parents:
51863
diff
changeset
|
376 class sshv1protocolhandler(wireprototypes.baseprotocolhandler): |
36065
bf676267f64f
wireprotoserver: split ssh protocol handler and server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36064
diff
changeset
|
377 """Handler for requests services via version 1 of SSH protocol.""" |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
378 |
36065
bf676267f64f
wireprotoserver: split ssh protocol handler and server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36064
diff
changeset
|
379 def __init__(self, ui, fin, fout): |
35870
f1efc0caeab7
wireprotoserver: make some instance attributes private
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35869
diff
changeset
|
380 self._ui = ui |
36065
bf676267f64f
wireprotoserver: split ssh protocol handler and server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36064
diff
changeset
|
381 self._fin = fin |
bf676267f64f
wireprotoserver: split ssh protocol handler and server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36064
diff
changeset
|
382 self._fout = fout |
37393
afcfdf53e4b5
wireproto: provide accessors for client capabilities
Joerg Sonnenberger <joerg@bec.de>
parents:
37296
diff
changeset
|
383 self._protocaps = set() |
35859
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
384 |
35873
29759c46aa1a
wireprotoserver: make name part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35872
diff
changeset
|
385 @property |
52125
1938d72e7a16
wireprotoserver: subclass the new `baseprotocolhandler` Protocol class
Matt Harbison <matt_harbison@yahoo.com>
parents:
51863
diff
changeset
|
386 def name(self) -> bytes: |
36536
3cd245945ef3
wireprotoserver: move SSHV1 and SSHV2 constants to wireprototypes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36526
diff
changeset
|
387 return wireprototypes.SSHV1 |
35873
29759c46aa1a
wireprotoserver: make name part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35872
diff
changeset
|
388 |
35859
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
389 def getargs(self, args): |
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
390 data = {} |
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
391 keys = args.split() |
49284
d44e3c45f0e4
py3: replace `pycompat.xrange` by `range`
Manuel Jacob <me@manueljacob.de>
parents:
48946
diff
changeset
|
392 for n in range(len(keys)): |
35870
f1efc0caeab7
wireprotoserver: make some instance attributes private
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35869
diff
changeset
|
393 argline = self._fin.readline()[:-1] |
35859
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
394 arg, l = argline.split() |
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
395 if arg not in keys: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
396 raise error.Abort(_(b"unexpected parameter %r") % arg) |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
397 if arg == b'*': |
35859
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
398 star = {} |
49284
d44e3c45f0e4
py3: replace `pycompat.xrange` by `range`
Manuel Jacob <me@manueljacob.de>
parents:
48946
diff
changeset
|
399 for k in range(int(l)): |
35870
f1efc0caeab7
wireprotoserver: make some instance attributes private
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35869
diff
changeset
|
400 argline = self._fin.readline()[:-1] |
35859
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
401 arg, l = argline.split() |
35870
f1efc0caeab7
wireprotoserver: make some instance attributes private
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35869
diff
changeset
|
402 val = self._fin.read(int(l)) |
35859
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
403 star[arg] = val |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
404 data[b'*'] = star |
35859
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
405 else: |
35870
f1efc0caeab7
wireprotoserver: make some instance attributes private
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35869
diff
changeset
|
406 val = self._fin.read(int(l)) |
35859
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
407 data[arg] = val |
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
408 return [data[k] for k in keys] |
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
409 |
37393
afcfdf53e4b5
wireproto: provide accessors for client capabilities
Joerg Sonnenberger <joerg@bec.de>
parents:
37296
diff
changeset
|
410 def getprotocaps(self): |
afcfdf53e4b5
wireproto: provide accessors for client capabilities
Joerg Sonnenberger <joerg@bec.de>
parents:
37296
diff
changeset
|
411 return self._protocaps |
afcfdf53e4b5
wireproto: provide accessors for client capabilities
Joerg Sonnenberger <joerg@bec.de>
parents:
37296
diff
changeset
|
412 |
37414
2d965bfeb8f6
wireproto: allow direct stream processing for unbundle
Joerg Sonnenberger <joerg@bec.de>
parents:
37393
diff
changeset
|
413 def getpayload(self): |
36372
b8d0761a85c7
wireproto: document the wonky push protocol for SSH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36371
diff
changeset
|
414 # We initially send an empty response. This tells the client it is |
b8d0761a85c7
wireproto: document the wonky push protocol for SSH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36371
diff
changeset
|
415 # OK to start sending data. If a client sees any other response, it |
b8d0761a85c7
wireproto: document the wonky push protocol for SSH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36371
diff
changeset
|
416 # interprets it as an error. |
b8d0761a85c7
wireproto: document the wonky push protocol for SSH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36371
diff
changeset
|
417 _sshv1respondbytes(self._fout, b'') |
b8d0761a85c7
wireproto: document the wonky push protocol for SSH
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36371
diff
changeset
|
418 |
36070
90ca4986616c
wireprotoserver: rename getfile() to forwardpayload() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36069
diff
changeset
|
419 # The file is in the form: |
90ca4986616c
wireprotoserver: rename getfile() to forwardpayload() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36069
diff
changeset
|
420 # |
90ca4986616c
wireprotoserver: rename getfile() to forwardpayload() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36069
diff
changeset
|
421 # <chunk size>\n<chunk> |
90ca4986616c
wireprotoserver: rename getfile() to forwardpayload() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36069
diff
changeset
|
422 # ... |
90ca4986616c
wireprotoserver: rename getfile() to forwardpayload() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36069
diff
changeset
|
423 # 0\n |
35870
f1efc0caeab7
wireprotoserver: make some instance attributes private
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35869
diff
changeset
|
424 count = int(self._fin.readline()) |
35859
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
425 while count: |
37414
2d965bfeb8f6
wireproto: allow direct stream processing for unbundle
Joerg Sonnenberger <joerg@bec.de>
parents:
37393
diff
changeset
|
426 yield self._fin.read(count) |
35870
f1efc0caeab7
wireprotoserver: make some instance attributes private
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35869
diff
changeset
|
427 count = int(self._fin.readline()) |
35859
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
428 |
36066
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
429 @contextlib.contextmanager |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
430 def mayberedirectstdio(self): |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
431 yield None |
2ad145fbde54
wireprotoserver: add context manager mechanism for redirecting stdio
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36065
diff
changeset
|
432 |
36069
957e773614d0
wireprotoserver: rename _client to client (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36068
diff
changeset
|
433 def client(self): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
434 client = encoding.environ.get(b'SSH_CLIENT', b'').split(b' ', 1)[0] |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
435 return b'remote:ssh:' + client |
36065
bf676267f64f
wireprotoserver: split ssh protocol handler and server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36064
diff
changeset
|
436 |
36613
6e585bca962e
wireproto: add transport specific capabilities in the transport
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36610
diff
changeset
|
437 def addcapabilities(self, repo, caps): |
37393
afcfdf53e4b5
wireproto: provide accessors for client capabilities
Joerg Sonnenberger <joerg@bec.de>
parents:
37296
diff
changeset
|
438 if self.name == wireprototypes.SSHV1: |
afcfdf53e4b5
wireproto: provide accessors for client capabilities
Joerg Sonnenberger <joerg@bec.de>
parents:
37296
diff
changeset
|
439 caps.append(b'protocaps') |
37053
cd0ca979a8b8
wireproto: nominally don't expose "batch" to version 2 wire transports
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37052
diff
changeset
|
440 caps.append(b'batch') |
36613
6e585bca962e
wireproto: add transport specific capabilities in the transport
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36610
diff
changeset
|
441 return caps |
6e585bca962e
wireproto: add transport specific capabilities in the transport
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36610
diff
changeset
|
442 |
36801
66de4555cefd
wireproto: formalize permissions checking as part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36800
diff
changeset
|
443 def checkperm(self, perm): |
66de4555cefd
wireproto: formalize permissions checking as part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36800
diff
changeset
|
444 pass |
66de4555cefd
wireproto: formalize permissions checking as part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36800
diff
changeset
|
445 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
446 |
50447
45c7bada5200
hidden: add support to explicitly access hidden changesets with SSH peers
Manuel Jacob <me@manueljacob.de>
parents:
50443
diff
changeset
|
447 def _runsshserver(ui, repo, fin, fout, ev, accesshidden=False): |
36215
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
448 # This function operates like a state machine of sorts. The following |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
449 # states are defined: |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
450 # |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
451 # protov1-serving |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
452 # Server is in protocol version 1 serving mode. Commands arrive on |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
453 # new lines. These commands are processed in this state, one command |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
454 # after the other. |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
455 # |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
456 # shutdown |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
457 # The server is shutting down, possibly in reaction to a client event. |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
458 # |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
459 # And here are their transitions: |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
460 # |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
461 # protov1-serving -> shutdown |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
462 # When server receives an empty request or encounters another |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
463 # error. |
464bedc0fdb4
wireprotoserver: handle SSH protocol version 2 upgrade requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36214
diff
changeset
|
464 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
465 state = b'protov1-serving' |
36214
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
466 proto = sshv1protocolhandler(ui, fin, fout) |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
467 |
36523
e7411fb7ba7f
wireprotoserver: ability to run an SSH server until an event is set
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36372
diff
changeset
|
468 while not ev.is_set(): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
469 if state == b'protov1-serving': |
36214
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
470 # Commands are issued on new lines. |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
471 request = fin.readline()[:-1] |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
472 |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
473 # Empty lines signal to terminate the connection. |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
474 if not request: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
475 state = b'shutdown' |
36214
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
476 continue |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
477 |
37785
b4d85bc122bd
wireproto: rename wireproto to wireprotov1server (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37783
diff
changeset
|
478 available = wireprotov1server.commands.commandavailable( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
479 request, proto |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
480 ) |
36214
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
481 |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
482 # This command isn't available. Send an empty response and go |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
483 # back to waiting for a new command. |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
484 if not available: |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
485 _sshv1respondbytes(fout, b'') |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
486 continue |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
487 |
50447
45c7bada5200
hidden: add support to explicitly access hidden changesets with SSH peers
Manuel Jacob <me@manueljacob.de>
parents:
50443
diff
changeset
|
488 rsp = wireprotov1server.dispatch( |
45c7bada5200
hidden: add support to explicitly access hidden changesets with SSH peers
Manuel Jacob <me@manueljacob.de>
parents:
50443
diff
changeset
|
489 repo, proto, request, accesshidden=accesshidden |
45c7bada5200
hidden: add support to explicitly access hidden changesets with SSH peers
Manuel Jacob <me@manueljacob.de>
parents:
50443
diff
changeset
|
490 ) |
42896
7e19b640c53e
sshserver: flush stream after command dispatch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42814
diff
changeset
|
491 repo.ui.fout.flush() |
7e19b640c53e
sshserver: flush stream after command dispatch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
42814
diff
changeset
|
492 repo.ui.ferr.flush() |
36214
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
493 |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
494 if isinstance(rsp, bytes): |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
495 _sshv1respondbytes(fout, rsp) |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
496 elif isinstance(rsp, wireprototypes.bytesresponse): |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
497 _sshv1respondbytes(fout, rsp.data) |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
498 elif isinstance(rsp, wireprototypes.streamres): |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
499 _sshv1respondstream(fout, rsp) |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
500 elif isinstance(rsp, wireprototypes.streamreslegacy): |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
501 _sshv1respondstream(fout, rsp) |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
502 elif isinstance(rsp, wireprototypes.pushres): |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
503 _sshv1respondbytes(fout, b'') |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
504 _sshv1respondbytes(fout, b'%d' % rsp.res) |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
505 elif isinstance(rsp, wireprototypes.pusherr): |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
506 _sshv1respondbytes(fout, rsp.res) |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
507 elif isinstance(rsp, wireprototypes.ooberror): |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
508 _sshv1respondooberror(fout, ui.ferr, rsp.message) |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
509 else: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
510 raise error.ProgrammingError( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
511 b'unhandled response type from ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
512 b'wire protocol command: %s' % rsp |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
513 ) |
36214
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
514 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
515 elif state == b'shutdown': |
36214
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
516 break |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
517 |
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
518 else: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
519 raise error.ProgrammingError( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
520 b'unhandled ssh server state: %s' % state |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
521 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42896
diff
changeset
|
522 |
36214
3b3a987bbbaa
wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36213
diff
changeset
|
523 |
48946
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
524 class sshserver: |
50447
45c7bada5200
hidden: add support to explicitly access hidden changesets with SSH peers
Manuel Jacob <me@manueljacob.de>
parents:
50443
diff
changeset
|
525 def __init__(self, ui, repo, logfh=None, accesshidden=False): |
36065
bf676267f64f
wireprotoserver: split ssh protocol handler and server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36064
diff
changeset
|
526 self._ui = ui |
bf676267f64f
wireprotoserver: split ssh protocol handler and server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36064
diff
changeset
|
527 self._repo = repo |
50447
45c7bada5200
hidden: add support to explicitly access hidden changesets with SSH peers
Manuel Jacob <me@manueljacob.de>
parents:
50443
diff
changeset
|
528 self._accesshidden = accesshidden |
51572
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
529 self._logfh = logfh |
36526
7cc4a9b9732a
wireprotoserver: support logging SSH server I/O to a file descriptor
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36523
diff
changeset
|
530 |
35859
1bf5263fe5cc
wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35858
diff
changeset
|
531 def serve_forever(self): |
36523
e7411fb7ba7f
wireprotoserver: ability to run an SSH server until an event is set
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36372
diff
changeset
|
532 self.serveuntil(threading.Event()) |
e7411fb7ba7f
wireprotoserver: ability to run an SSH server until an event is set
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36372
diff
changeset
|
533 |
e7411fb7ba7f
wireprotoserver: ability to run an SSH server until an event is set
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36372
diff
changeset
|
534 def serveuntil(self, ev): |
e7411fb7ba7f
wireprotoserver: ability to run an SSH server until an event is set
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36372
diff
changeset
|
535 """Serve until a threading.Event is set.""" |
51572
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
536 with self._ui.protectedfinout() as (fin, fout): |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
537 if self._logfh: |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
538 # Log write I/O to stdout and stderr if configured. |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
539 fout = util.makeloggingfileobject( |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
540 self._logfh, |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
541 fout, |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
542 b'o', |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
543 logdata=True, |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
544 ) |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
545 self._ui.ferr = util.makeloggingfileobject( |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
546 self._logfh, |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
547 self._ui.ferr, |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
548 b'e', |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
549 logdata=True, |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
550 ) |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
551 _runsshserver( |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
552 self._ui, |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
553 self._repo, |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
554 fin, |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
555 fout, |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
556 ev, |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
557 self._accesshidden, |
13c004b54cbe
wireprotoserver: ensure that output stream gets flushed on exception
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50447
diff
changeset
|
558 ) |