~bzr-pqm/bzr/bzr.dev

3224.5.8 by Andrew Bennetts
Fix failing tests.
1
# Copyright (C) 2006, 2007 Canonical Ltd
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
16
4160.2.14 by Andrew Bennetts
Merge bzr.dev, improve NEWS entry and bzrlib.smart.request docs.
17
"""Infrastructure for server-side request handlers.
18
19
Interesting module attributes:
20
    * The request_handlers registry maps verb names to SmartServerRequest
21
      classes.
22
    * The jail_info threading.local() object is used to prevent accidental
23
      opening of BzrDirs outside of the backing transport, or any other
24
      transports placed in jail_info.transports.  The jail_info is reset on
25
      every call into a request handler (which can happen an arbitrary number
26
      of times during a request).
3195.3.4 by Andrew Bennetts
Make the general request handler dispatch version 3 events to the specific request handler (i.e. to the SmartServerRequest instance).
27
"""
2018.5.19 by Andrew Bennetts
Add docstrings to all the new modules, and a few other places.
28
4160.2.14 by Andrew Bennetts
Merge bzr.dev, improve NEWS entry and bzrlib.smart.request docs.
29
# XXX: The class names are a little confusing: the protocol will instantiate a
30
# SmartServerRequestHandler, whose dispatch_command method creates an instance
31
# of a SmartServerRequest subclass.
32
33
2018.5.6 by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects.
34
import tempfile
4160.2.2 by Andrew Bennetts
Add setup_jail and teardown_jail to SmartServerRequest.
35
import threading
2018.5.6 by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects.
36
2402.1.2 by Andrew Bennetts
Deal with review comments.
37
from bzrlib import (
38
    bzrdir,
39
    errors,
40
    registry,
41
    revision,
4064.1.2 by Andrew Bennetts
Refactor server-side error translation, improve tests.
42
    trace,
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
43
    urlutils,
2402.1.2 by Andrew Bennetts
Deal with review comments.
44
    )
3224.5.6 by Andrew Bennetts
Don't import bzrlib.bundle in bzrlib.smart.request until it's needed.
45
from bzrlib.lazy_import import lazy_import
46
lazy_import(globals(), """
47
from bzrlib.bundle import serializer
48
""")
2018.5.6 by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects.
49
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
50
4160.2.2 by Andrew Bennetts
Add setup_jail and teardown_jail to SmartServerRequest.
51
jail_info = threading.local()
52
jail_info.transports = None
53
54
4160.2.4 by Andrew Bennetts
Use BzrDir pre_open hook to jail request code from accessing transports other than the backing transport.
55
def _install_hook():
56
    bzrdir.BzrDir.hooks.install_named_hook(
57
        'pre_open', _pre_open_hook, 'checking server jail')
58
59
60
def _pre_open_hook(transport):
4205.2.1 by Andrew Bennetts
Fix BzrDir.open in non-main (and non-server-request) thread when bzrlib.smart.request's _pre_open_hook is installed.
61
    allowed_transports = getattr(jail_info, 'transports', None)
4160.2.4 by Andrew Bennetts
Use BzrDir pre_open hook to jail request code from accessing transports other than the backing transport.
62
    if allowed_transports is None:
63
        return
64
    abspath = transport.base
65
    for allowed_transport in allowed_transports:
66
        try:
67
            allowed_transport.relpath(abspath)
68
        except errors.PathNotChild:
69
            continue
70
        else:
71
            return
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
72
    raise errors.JailBreak(abspath)
4160.2.4 by Andrew Bennetts
Use BzrDir pre_open hook to jail request code from accessing transports other than the backing transport.
73
74
75
_install_hook()
76
77
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
78
class SmartServerRequest(object):
2692.1.10 by Andrew Bennetts
More docstring polish
79
    """Base class for request handlers.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
80
2692.1.10 by Andrew Bennetts
More docstring polish
81
    To define a new request, subclass this class and override the `do` method
82
    (and if appropriate, `do_body` as well).  Request implementors should take
83
    care to call `translate_client_path` and `transport_from_client_path` as
84
    appropriate when dealing with paths received from the client.
85
    """
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
86
    # XXX: rename this class to BaseSmartServerRequestHandler ?  A request
87
    # *handler* is a different concept to the request.
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
88
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
89
    def __init__(self, backing_transport, root_client_path='/'):
2402.1.2 by Andrew Bennetts
Deal with review comments.
90
        """Constructor.
91
92
        :param backing_transport: the base transport to be used when performing
93
            this request.
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
94
        :param root_client_path: the client path that maps to the root of
2692.1.9 by Andrew Bennetts
Docstrings for translate_client_path and transport_from_client_path.
95
            backing_transport.  This is used to interpret relpaths received
96
            from the client.  Clients will not be able to refer to paths above
2692.1.16 by Andrew Bennetts
Improve comments.
97
            this root.  If root_client_path is None, then no translation will
98
            be performed on client paths.  Default is '/'.
2402.1.2 by Andrew Bennetts
Deal with review comments.
99
        """
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
100
        self._backing_transport = backing_transport
2692.1.14 by Andrew Bennetts
All WSGI tests passing, and manual testing works too.
101
        if root_client_path is not None:
102
            if not root_client_path.startswith('/'):
103
                root_client_path = '/' + root_client_path
104
            if not root_client_path.endswith('/'):
105
                root_client_path += '/'
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
106
        self._root_client_path = root_client_path
3842.3.19 by Andrew Bennetts
Tweaks suggested by review.
107
        self._body_chunks = []
2018.5.5 by Andrew Bennetts
Pass body_bytes directly to SmartServerRequest.do_body
108
2018.5.24 by Andrew Bennetts
Setting NO_SMART_VFS in environment will disable VFS methods in the smart server. (Robert Collins, John Arbash Meinel, Andrew Bennetts)
109
    def _check_enabled(self):
110
        """Raises DisabledMethod if this method is disabled."""
111
        pass
112
2018.5.19 by Andrew Bennetts
Add docstrings to all the new modules, and a few other places.
113
    def do(self, *args):
2018.5.24 by Andrew Bennetts
Setting NO_SMART_VFS in environment will disable VFS methods in the smart server. (Robert Collins, John Arbash Meinel, Andrew Bennetts)
114
        """Mandatory extension point for SmartServerRequest subclasses.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
115
2018.5.24 by Andrew Bennetts
Setting NO_SMART_VFS in environment will disable VFS methods in the smart server. (Robert Collins, John Arbash Meinel, Andrew Bennetts)
116
        Subclasses must implement this.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
117
2018.5.19 by Andrew Bennetts
Add docstrings to all the new modules, and a few other places.
118
        This should return a SmartServerResponse if this command expects to
119
        receive no body.
120
        """
2018.5.5 by Andrew Bennetts
Pass body_bytes directly to SmartServerRequest.do_body
121
        raise NotImplementedError(self.do)
122
2018.5.24 by Andrew Bennetts
Setting NO_SMART_VFS in environment will disable VFS methods in the smart server. (Robert Collins, John Arbash Meinel, Andrew Bennetts)
123
    def execute(self, *args):
124
        """Public entry point to execute this request.
125
126
        It will return a SmartServerResponse if the command does not expect a
127
        body.
128
129
        :param *args: the arguments of the request.
130
        """
131
        self._check_enabled()
132
        return self.do(*args)
133
2018.5.5 by Andrew Bennetts
Pass body_bytes directly to SmartServerRequest.do_body
134
    def do_body(self, body_bytes):
2018.5.19 by Andrew Bennetts
Add docstrings to all the new modules, and a few other places.
135
        """Called if the client sends a body with the request.
3184.1.10 by Robert Collins
Change the smart server verb for Repository.stream_revisions_chunked to use SearchResults as the request mechanism for downloads.
136
137
        The do() method is still called, and must have returned None.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
138
2018.5.19 by Andrew Bennetts
Add docstrings to all the new modules, and a few other places.
139
        Must return a SmartServerResponse.
140
        """
3990.3.2 by Andrew Bennetts
Fix the do_body NotImplementedError log spam.
141
        if body_bytes != '':
3990.3.3 by Andrew Bennetts
Add a test that unexpected request bodies trigger a SmartProtocolError from request implementations.
142
            raise errors.SmartProtocolError('Request does not expect a body')
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
143
3195.3.4 by Andrew Bennetts
Make the general request handler dispatch version 3 events to the specific request handler (i.e. to the SmartServerRequest instance).
144
    def do_chunk(self, chunk_bytes):
145
        """Called with each body chunk if the request has a streamed body.
146
147
        The do() method is still called, and must have returned None.
148
        """
3842.3.19 by Andrew Bennetts
Tweaks suggested by review.
149
        self._body_chunks.append(chunk_bytes)
3195.3.4 by Andrew Bennetts
Make the general request handler dispatch version 3 events to the specific request handler (i.e. to the SmartServerRequest instance).
150
151
    def do_end(self):
152
        """Called when the end of the request has been received."""
3842.3.19 by Andrew Bennetts
Tweaks suggested by review.
153
        body_bytes = ''.join(self._body_chunks)
154
        self._body_chunks = None
3923.5.2 by Andrew Bennetts
Completely delegate handling of request body chunks to the command object. The default implementation accumulates, like the existing behaviour.
155
        return self.do_body(body_bytes)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
156
4160.2.2 by Andrew Bennetts
Add setup_jail and teardown_jail to SmartServerRequest.
157
    def setup_jail(self):
158
        jail_info.transports = [self._backing_transport]
159
160
    def teardown_jail(self):
161
        jail_info.transports = None
162
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
163
    def translate_client_path(self, client_path):
2692.1.9 by Andrew Bennetts
Docstrings for translate_client_path and transport_from_client_path.
164
        """Translate a path received from a network client into a local
165
        relpath.
166
167
        All paths received from the client *must* be translated.
168
2692.1.14 by Andrew Bennetts
All WSGI tests passing, and manual testing works too.
169
        :param client_path: the path from the client.
2692.1.9 by Andrew Bennetts
Docstrings for translate_client_path and transport_from_client_path.
170
        :returns: a relpath that may be used with self._backing_transport
2692.1.14 by Andrew Bennetts
All WSGI tests passing, and manual testing works too.
171
            (unlike the untranslated client_path, which must not be used with
172
            the backing transport).
2692.1.9 by Andrew Bennetts
Docstrings for translate_client_path and transport_from_client_path.
173
        """
2692.1.14 by Andrew Bennetts
All WSGI tests passing, and manual testing works too.
174
        if self._root_client_path is None:
175
            # no translation necessary!
176
            return client_path
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
177
        if not client_path.startswith('/'):
178
            client_path = '/' + client_path
4294.2.1 by Robert Collins
Move directory checking for bzr push options into Branch.create_clone_on_transport.
179
        if client_path + '/' == self._root_client_path:
180
            return '.'
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
181
        if client_path.startswith(self._root_client_path):
182
            path = client_path[len(self._root_client_path):]
183
            relpath = urlutils.joinpath('/', path)
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
184
            if not relpath.startswith('/'):
185
                raise ValueError(relpath)
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
186
            return '.' + relpath
187
        else:
188
            raise errors.PathNotChild(client_path, self._root_client_path)
189
190
    def transport_from_client_path(self, client_path):
2692.1.9 by Andrew Bennetts
Docstrings for translate_client_path and transport_from_client_path.
191
        """Get a backing transport corresponding to the location referred to by
192
        a network client.
193
194
        :seealso: translate_client_path
195
        :returns: a transport cloned from self._backing_transport
196
        """
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
197
        relpath = self.translate_client_path(client_path)
2692.1.16 by Andrew Bennetts
Improve comments.
198
        return self._backing_transport.clone(relpath)
3195.3.4 by Andrew Bennetts
Make the general request handler dispatch version 3 events to the specific request handler (i.e. to the SmartServerRequest instance).
199
2018.5.6 by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects.
200
2018.5.16 by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies.
201
class SmartServerResponse(object):
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
202
    """A response to a client request.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
203
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
204
    This base class should not be used. Instead use
205
    SuccessfulSmartServerResponse and FailedSmartServerResponse as appropriate.
206
    """
2018.5.16 by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies.
207
2748.4.2 by Andrew Bennetts
Add protocol (version two) support for streaming bodies (using chunking) in responses.
208
    def __init__(self, args, body=None, body_stream=None):
209
        """Constructor.
210
211
        :param args: tuple of response arguments.
212
        :param body: string of a response body.
213
        :param body_stream: iterable of bytestrings to be streamed to the
214
            client.
215
        """
2018.5.16 by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies.
216
        self.args = args
2748.4.2 by Andrew Bennetts
Add protocol (version two) support for streaming bodies (using chunking) in responses.
217
        if body is not None and body_stream is not None:
218
            raise errors.BzrError(
219
                "'body' and 'body_stream' are mutually exclusive.")
2018.5.16 by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies.
220
        self.body = body
2748.4.2 by Andrew Bennetts
Add protocol (version two) support for streaming bodies (using chunking) in responses.
221
        self.body_stream = body_stream
2018.5.16 by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies.
222
2402.1.1 by Andrew Bennetts
Use the Command pattern for handling smart server commands.
223
    def __eq__(self, other):
224
        if other is None:
225
            return False
2748.4.2 by Andrew Bennetts
Add protocol (version two) support for streaming bodies (using chunking) in responses.
226
        return (other.args == self.args and
227
                other.body == self.body and
228
                other.body_stream is self.body_stream)
2402.1.1 by Andrew Bennetts
Use the Command pattern for handling smart server commands.
229
230
    def __repr__(self):
3691.2.6 by Martin Pool
Disable RemoteBranch stacking, but get get_stacked_on_url working, and passing back exceptions
231
        return "<%s args=%r body=%r>" % (self.__class__.__name__,
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
232
            self.args, self.body)
2402.1.1 by Andrew Bennetts
Use the Command pattern for handling smart server commands.
233
2018.5.16 by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies.
234
2432.4.2 by Robert Collins
Add FailedSmartServerResponse.
235
class FailedSmartServerResponse(SmartServerResponse):
236
    """A SmartServerResponse for a request which failed."""
237
238
    def is_successful(self):
239
        """FailedSmartServerResponse are not successful."""
240
        return False
241
242
2432.4.1 by Robert Collins
Add SuccessfulSmartServerResponse.
243
class SuccessfulSmartServerResponse(SmartServerResponse):
244
    """A SmartServerResponse for a successfully completed request."""
245
246
    def is_successful(self):
247
        """SuccessfulSmartServerResponse are successful."""
248
        return True
249
2018.5.16 by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies.
250
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
251
class SmartServerRequestHandler(object):
252
    """Protocol logic for smart server.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
253
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
254
    This doesn't handle serialization at all, it just processes requests and
255
    creates responses.
256
    """
257
258
    # IMPORTANT FOR IMPLEMENTORS: It is important that SmartServerRequestHandler
259
    # not contain encoding or decoding logic to allow the wire protocol to vary
260
    # from the object protocol: we will want to tweak the wire protocol separate
261
    # from the object model, and ideally we will be able to do that without
262
    # having a SmartServerRequestHandler subclass for each wire protocol, rather
263
    # just a Protocol subclass.
264
265
    # TODO: Better way of representing the body for commands that take it,
266
    # and allow it to be streamed into the server.
267
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
268
    def __init__(self, backing_transport, commands, root_client_path):
2018.5.17 by Andrew Bennetts
Paramaterise the commands handled by SmartServerRequestHandler.
269
        """Constructor.
270
271
        :param backing_transport: a Transport to handle requests for.
2018.5.23 by Andrew Bennetts
Use a Registry for smart server command handlers.
272
        :param commands: a registry mapping command names to SmartServerRequest
2018.5.17 by Andrew Bennetts
Paramaterise the commands handled by SmartServerRequestHandler.
273
            subclasses. e.g. bzrlib.transport.smart.vfs.vfs_commands.
274
        """
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
275
        self._backing_transport = backing_transport
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
276
        self._root_client_path = root_client_path
2018.5.17 by Andrew Bennetts
Paramaterise the commands handled by SmartServerRequestHandler.
277
        self._commands = commands
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
278
        self.response = None
279
        self.finished_reading = False
280
        self._command = None
281
282
    def accept_body(self, bytes):
283
        """Accept body data."""
3923.5.2 by Andrew Bennetts
Completely delegate handling of request body chunks to the command object. The default implementation accumulates, like the existing behaviour.
284
        self._run_handler_code(self._command.do_chunk, (bytes,), {})
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
285
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
286
    def end_of_body(self):
287
        """No more body data will be received."""
3923.5.2 by Andrew Bennetts
Completely delegate handling of request body chunks to the command object. The default implementation accumulates, like the existing behaviour.
288
        self._run_handler_code(self._command.do_end, (), {})
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
289
        # cannot read after this.
290
        self.finished_reading = True
291
292
    def dispatch_command(self, cmd, args):
293
        """Deprecated compatibility method.""" # XXX XXX
2018.5.23 by Andrew Bennetts
Use a Registry for smart server command handlers.
294
        try:
295
            command = self._commands.get(cmd)
296
        except LookupError:
3245.4.29 by Andrew Bennetts
Add/tidy some comments, remove dud test_errors_are_logged test, add explicit UnknownSmartMethod to v3.
297
            raise errors.UnknownSmartMethod(cmd)
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
298
        self._command = command(self._backing_transport, self._root_client_path)
2018.5.24 by Andrew Bennetts
Setting NO_SMART_VFS in environment will disable VFS methods in the smart server. (Robert Collins, John Arbash Meinel, Andrew Bennetts)
299
        self._run_handler_code(self._command.execute, args, {})
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
300
301
    def _run_handler_code(self, callable, args, kwargs):
302
        """Run some handler specific code 'callable'.
303
304
        If a result is returned, it is considered to be the commands response,
305
        and finished_reading is set true, and its assigned to self.response.
306
307
        Any exceptions caught are translated and a response object created
308
        from them.
309
        """
310
        result = self._call_converting_errors(callable, args, kwargs)
2018.5.19 by Andrew Bennetts
Add docstrings to all the new modules, and a few other places.
311
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
312
        if result is not None:
313
            self.response = result
314
            self.finished_reading = True
315
316
    def _call_converting_errors(self, callable, args, kwargs):
317
        """Call callable converting errors to Response objects."""
318
        # XXX: most of this error conversion is VFS-related, and thus ought to
319
        # be in SmartServerVFSRequestHandler somewhere.
320
        try:
4160.2.2 by Andrew Bennetts
Add setup_jail and teardown_jail to SmartServerRequest.
321
            self._command.setup_jail()
322
            try:
323
                return callable(*args, **kwargs)
324
            finally:
325
                self._command.teardown_jail()
4064.1.2 by Andrew Bennetts
Refactor server-side error translation, improve tests.
326
        except (KeyboardInterrupt, SystemExit):
327
            raise
328
        except Exception, err:
329
            err_struct = _translate_error(err)
330
            return FailedSmartServerResponse(err_struct)
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
331
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
332
    def headers_received(self, headers):
3245.4.33 by Andrew Bennetts
Remove unused no_body_received method on SmartServerRequestHandler.
333
        # Just a no-op at the moment.
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
334
        pass
335
336
    def args_received(self, args):
3195.3.4 by Andrew Bennetts
Make the general request handler dispatch version 3 events to the specific request handler (i.e. to the SmartServerRequest instance).
337
        cmd = args[0]
338
        args = args[1:]
339
        try:
340
            command = self._commands.get(cmd)
341
        except LookupError:
3245.4.48 by Andrew Bennetts
raise UnknownSmartMethod from dispatch_command.
342
            raise errors.UnknownSmartMethod(cmd)
3195.3.4 by Andrew Bennetts
Make the general request handler dispatch version 3 events to the specific request handler (i.e. to the SmartServerRequest instance).
343
        self._command = command(self._backing_transport)
344
        self._run_handler_code(self._command.execute, args, {})
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
345
3195.3.4 by Andrew Bennetts
Make the general request handler dispatch version 3 events to the specific request handler (i.e. to the SmartServerRequest instance).
346
    def end_received(self):
347
        self._run_handler_code(self._command.do_end, (), {})
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
348
3923.5.4 by Andrew Bennetts
Allow a request's body part(s) to be followed by an error.
349
    def post_body_error_received(self, error_args):
350
        # Just a no-op at the moment.
351
        pass
352
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
353
4064.1.2 by Andrew Bennetts
Refactor server-side error translation, improve tests.
354
def _translate_error(err):
355
    if isinstance(err, errors.NoSuchFile):
356
        return ('NoSuchFile', err.path)
357
    elif isinstance(err, errors.FileExists):
358
        return ('FileExists', err.path)
359
    elif isinstance(err, errors.DirectoryNotEmpty):
360
        return ('DirectoryNotEmpty', err.path)
361
    elif isinstance(err, errors.ShortReadvError):
362
        return ('ShortReadvError', err.path, str(err.offset), str(err.length),
363
                str(err.actual))
364
    elif isinstance(err, errors.UnstackableRepositoryFormat):
365
        return (('UnstackableRepositoryFormat', str(err.format), err.url))
366
    elif isinstance(err, errors.UnstackableBranchFormat):
367
        return ('UnstackableBranchFormat', str(err.format), err.url)
368
    elif isinstance(err, errors.NotStacked):
369
        return ('NotStacked',)
370
    elif isinstance(err, UnicodeError):
371
        # If it is a DecodeError, than most likely we are starting
372
        # with a plain string
373
        str_or_unicode = err.object
374
        if isinstance(str_or_unicode, unicode):
375
            # XXX: UTF-8 might have \x01 (our protocol v1 and v2 seperator
376
            # byte) in it, so this encoding could cause broken responses.
377
            # Newer clients use protocol v3, so will be fine.
378
            val = 'u:' + str_or_unicode.encode('utf-8')
379
        else:
380
            val = 's:' + str_or_unicode.encode('base64')
381
        # This handles UnicodeEncodeError or UnicodeDecodeError
382
        return (err.__class__.__name__, err.encoding, val, str(err.start),
383
                str(err.end), err.reason)
384
    elif isinstance(err, errors.TransportNotPossible):
385
        if err.msg == "readonly transport":
386
            return ('ReadOnlyError', )
387
    elif isinstance(err, errors.ReadError):
388
        # cannot read the file
389
        return ('ReadError', err.path)
390
    elif isinstance(err, errors.PermissionDenied):
391
        return ('PermissionDenied', err.path, err.extra)
4144.3.1 by Andrew Bennetts
Add Repository.insert_stream_locked server-side implementation, plus tests for server-side _translate_error.
392
    elif isinstance(err, errors.TokenMismatch):
393
        return ('TokenMismatch', err.given_token, err.lock_token)
394
    elif isinstance(err, errors.LockContention):
395
        return ('LockContention', err.lock, err.msg)
4064.1.2 by Andrew Bennetts
Refactor server-side error translation, improve tests.
396
    # Unserialisable error.  Log it, and return a generic error
397
    trace.log_exception_quietly()
398
    return ('error', str(err))
399
400
2018.5.6 by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects.
401
class HelloRequest(SmartServerRequest):
2432.2.6 by Andrew Bennetts
Improve HelloRequest's docstring.
402
    """Answer a version request with the highest protocol version this server
403
    supports.
404
    """
2018.5.6 by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects.
405
406
    def do(self):
3245.4.59 by Andrew Bennetts
Various tweaks in response to Martin's review.
407
        return SuccessfulSmartServerResponse(('ok', '2'))
2018.5.6 by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects.
408
409
410
class GetBundleRequest(SmartServerRequest):
2402.1.2 by Andrew Bennetts
Deal with review comments.
411
    """Get a bundle of from the null revision to the specified revision."""
2018.5.6 by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects.
412
413
    def do(self, path, revision_id):
414
        # open transport relative to our base
2692.1.7 by Andrew Bennetts
Translate path in GetBundleRequest too.
415
        t = self.transport_from_client_path(path)
2018.5.6 by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects.
416
        control, extra_path = bzrdir.BzrDir.open_containing_from_transport(t)
417
        repo = control.open_repository()
418
        tmpf = tempfile.TemporaryFile()
419
        base_revision = revision.NULL_REVISION
3224.5.6 by Andrew Bennetts
Don't import bzrlib.bundle in bzrlib.smart.request until it's needed.
420
        serializer.write_bundle(repo, revision_id, base_revision, tmpf)
2018.5.6 by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects.
421
        tmpf.seek(0)
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
422
        return SuccessfulSmartServerResponse((), tmpf.read())
2018.5.6 by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects.
423
424
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
425
class SmartServerIsReadonly(SmartServerRequest):
426
    # XXX: this request method belongs somewhere else.
427
428
    def do(self):
429
        if self._backing_transport.is_readonly():
430
            answer = 'yes'
431
        else:
432
            answer = 'no'
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
433
        return SuccessfulSmartServerResponse((answer,))
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
434
435
2018.5.23 by Andrew Bennetts
Use a Registry for smart server command handlers.
436
request_handlers = registry.Registry()
437
request_handlers.register_lazy(
438
    'append', 'bzrlib.smart.vfs', 'AppendRequest')
439
request_handlers.register_lazy(
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
440
    'Branch.get_config_file', 'bzrlib.smart.branch',
441
    'SmartServerBranchGetConfigFile')
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
442
request_handlers.register_lazy(
4078.2.1 by Robert Collins
Add a Branch.get_parent remote call for RemoteBranch.
443
    'Branch.get_parent', 'bzrlib.smart.branch', 'SmartServerBranchGetParent')
444
request_handlers.register_lazy(
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
445
    'Branch.get_tags_bytes', 'bzrlib.smart.branch',
446
    'SmartServerBranchGetTagsBytes')
447
request_handlers.register_lazy(
3691.2.5 by Martin Pool
Add Branch.get_stacked_on_url rpc and tests for same
448
    'Branch.get_stacked_on_url', 'bzrlib.smart.branch', 'SmartServerBranchRequestGetStackedOnURL')
449
request_handlers.register_lazy(
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
450
    'Branch.last_revision_info', 'bzrlib.smart.branch', 'SmartServerBranchRequestLastRevisionInfo')
451
request_handlers.register_lazy(
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
452
    'Branch.lock_write', 'bzrlib.smart.branch', 'SmartServerBranchRequestLockWrite')
4226.2.1 by Robert Collins
Set branch config options via a smart method.
453
request_handlers.register_lazy( 'Branch.revision_history',
454
    'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
455
request_handlers.register_lazy( 'Branch.set_config_option',
456
    'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOption')
457
request_handlers.register_lazy( 'Branch.set_last_revision',
458
    'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
459
request_handlers.register_lazy(
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
460
    'Branch.set_last_revision_info', 'bzrlib.smart.branch',
461
    'SmartServerBranchRequestSetLastRevisionInfo')
462
request_handlers.register_lazy(
3441.5.25 by Andrew Bennetts
Rename Branch.set_last_revision_descendant verb to Branch.set_last_revision_ex. It's a cop out, but at least it's not misleading.
463
    'Branch.set_last_revision_ex', 'bzrlib.smart.branch',
464
    'SmartServerBranchRequestSetLastRevisionEx')
3441.5.6 by Andrew Bennetts
Greatly simplify RemoteBranch.update_revisions. Still needs more tests.
465
request_handlers.register_lazy(
4288.1.7 by Robert Collins
Add new remote server verb Branch.set_parent_location, dropping roundtrips further on push operations.
466
    'Branch.set_parent_location', 'bzrlib.smart.branch',
467
    'SmartServerBranchRequestSetParentLocation')
468
request_handlers.register_lazy(
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
469
    'Branch.unlock', 'bzrlib.smart.branch', 'SmartServerBranchRequestUnlock')
470
request_handlers.register_lazy(
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
471
    'BzrDir.cloning_metadir', 'bzrlib.smart.bzrdir',
472
    'SmartServerBzrDirRequestCloningMetaDir')
473
request_handlers.register_lazy(
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
474
    'BzrDir.create_branch', 'bzrlib.smart.bzrdir',
475
    'SmartServerRequestCreateBranch')
476
request_handlers.register_lazy(
477
    'BzrDir.create_repository', 'bzrlib.smart.bzrdir',
478
    'SmartServerRequestCreateRepository')
479
request_handlers.register_lazy(
480
    'BzrDir.find_repository', 'bzrlib.smart.bzrdir',
481
    'SmartServerRequestFindRepositoryV1')
482
request_handlers.register_lazy(
483
    'BzrDir.find_repositoryV2', 'bzrlib.smart.bzrdir',
484
    'SmartServerRequestFindRepositoryV2')
485
request_handlers.register_lazy(
486
    'BzrDir.find_repositoryV3', 'bzrlib.smart.bzrdir',
487
    'SmartServerRequestFindRepositoryV3')
488
request_handlers.register_lazy(
4288.1.2 by Robert Collins
Create a server verb for doing BzrDir.get_config()
489
    'BzrDir.get_config_file', 'bzrlib.smart.bzrdir',
490
    'SmartServerBzrDirRequestConfigFile')
491
request_handlers.register_lazy(
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
492
    'BzrDirFormat.initialize', 'bzrlib.smart.bzrdir',
493
    'SmartServerRequestInitializeBzrDir')
494
request_handlers.register_lazy(
4436.1.1 by Andrew Bennetts
Rename BzrDirFormat.initialize_ex verb to BzrDirFormat.initialize_ex_1.16.
495
    'BzrDirFormat.initialize_ex_1.16', 'bzrlib.smart.bzrdir',
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
496
    'SmartServerRequestBzrDirInitializeEx')
497
request_handlers.register_lazy(
4288.1.2 by Robert Collins
Create a server verb for doing BzrDir.get_config()
498
    'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')
499
request_handlers.register_lazy(
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
500
    'BzrDir.open_branch', 'bzrlib.smart.bzrdir',
501
    'SmartServerRequestOpenBranch')
502
request_handlers.register_lazy(
503
    'BzrDir.open_branchV2', 'bzrlib.smart.bzrdir',
504
    'SmartServerRequestOpenBranchV2')
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
505
request_handlers.register_lazy(
2018.5.23 by Andrew Bennetts
Use a Registry for smart server command handlers.
506
    'delete', 'bzrlib.smart.vfs', 'DeleteRequest')
507
request_handlers.register_lazy(
508
    'get', 'bzrlib.smart.vfs', 'GetRequest')
509
request_handlers.register_lazy(
510
    'get_bundle', 'bzrlib.smart.request', 'GetBundleRequest')
511
request_handlers.register_lazy(
512
    'has', 'bzrlib.smart.vfs', 'HasRequest')
513
request_handlers.register_lazy(
514
    'hello', 'bzrlib.smart.request', 'HelloRequest')
515
request_handlers.register_lazy(
2018.5.37 by Andrew Bennetts
Make sure all the request handlers in bzrlib/smart/vfs.py have consistent names.
516
    'iter_files_recursive', 'bzrlib.smart.vfs', 'IterFilesRecursiveRequest')
2018.5.23 by Andrew Bennetts
Use a Registry for smart server command handlers.
517
request_handlers.register_lazy(
518
    'list_dir', 'bzrlib.smart.vfs', 'ListDirRequest')
519
request_handlers.register_lazy(
2018.5.37 by Andrew Bennetts
Make sure all the request handlers in bzrlib/smart/vfs.py have consistent names.
520
    'mkdir', 'bzrlib.smart.vfs', 'MkdirRequest')
521
request_handlers.register_lazy(
522
    'move', 'bzrlib.smart.vfs', 'MoveRequest')
523
request_handlers.register_lazy(
524
    'put', 'bzrlib.smart.vfs', 'PutRequest')
525
request_handlers.register_lazy(
526
    'put_non_atomic', 'bzrlib.smart.vfs', 'PutNonAtomicRequest')
527
request_handlers.register_lazy(
528
    'readv', 'bzrlib.smart.vfs', 'ReadvRequest')
529
request_handlers.register_lazy(
530
    'rename', 'bzrlib.smart.vfs', 'RenameRequest')
3452.2.2 by Andrew Bennetts
Experimental PackRepository.{check_references,autopack} RPCs.
531
request_handlers.register_lazy(
532
    'PackRepository.autopack', 'bzrlib.smart.packrepository',
533
    'SmartServerPackRepositoryAutopack')
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
534
request_handlers.register_lazy('Repository.gather_stats',
535
                               'bzrlib.smart.repository',
536
                               'SmartServerRepositoryGatherStats')
3842.3.3 by Andrew Bennetts
Remove RPC registration for VersionedFiles.get_parent_map, not Repository.get_parent_map.
537
request_handlers.register_lazy('Repository.get_parent_map',
538
                               'bzrlib.smart.repository',
539
                               'SmartServerRepositoryGetParentMap')
2018.5.37 by Andrew Bennetts
Make sure all the request handlers in bzrlib/smart/vfs.py have consistent names.
540
request_handlers.register_lazy(
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
541
    'Repository.get_revision_graph', 'bzrlib.smart.repository', 'SmartServerRepositoryGetRevisionGraph')
542
request_handlers.register_lazy(
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
543
    'Repository.has_revision', 'bzrlib.smart.repository', 'SmartServerRequestHasRevision')
544
request_handlers.register_lazy(
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
545
    'Repository.insert_stream', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStream')
546
request_handlers.register_lazy(
4144.3.1 by Andrew Bennetts
Add Repository.insert_stream_locked server-side implementation, plus tests for server-side _translate_error.
547
    'Repository.insert_stream_locked', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStreamLocked')
548
request_handlers.register_lazy(
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
549
    'Repository.is_shared', 'bzrlib.smart.repository', 'SmartServerRepositoryIsShared')
550
request_handlers.register_lazy(
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
551
    'Repository.lock_write', 'bzrlib.smart.repository', 'SmartServerRepositoryLockWrite')
552
request_handlers.register_lazy(
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
553
    'Repository.set_make_working_trees', 'bzrlib.smart.repository',
554
    'SmartServerRepositorySetMakeWorkingTrees')
555
request_handlers.register_lazy(
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
556
    'Repository.unlock', 'bzrlib.smart.repository', 'SmartServerRepositoryUnlock')
557
request_handlers.register_lazy(
4419.2.4 by Andrew Bennetts
Add Repository.get_rev_id_for_revno RPC, removes VFS calls from 'pull -r 123' case.
558
    'Repository.get_rev_id_for_revno', 'bzrlib.smart.repository',
559
    'SmartServerRepositoryGetRevIdForRevno')
560
request_handlers.register_lazy(
4060.1.5 by Robert Collins
Verb change name requested by Andrew.
561
    'Repository.get_stream', 'bzrlib.smart.repository',
562
    'SmartServerRepositoryGetStream')
4060.1.4 by Robert Collins
Streaming fetch from remote servers.
563
request_handlers.register_lazy(
2018.18.1 by Martin Pool
Add stub Repository.tarball smart method
564
    'Repository.tarball', 'bzrlib.smart.repository',
565
    'SmartServerRepositoryTarball')
566
request_handlers.register_lazy(
2018.5.37 by Andrew Bennetts
Make sure all the request handlers in bzrlib/smart/vfs.py have consistent names.
567
    'rmdir', 'bzrlib.smart.vfs', 'RmdirRequest')
568
request_handlers.register_lazy(
569
    'stat', 'bzrlib.smart.vfs', 'StatRequest')
2018.5.26 by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport.
570
request_handlers.register_lazy(
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
571
    'Transport.is_readonly', 'bzrlib.smart.request', 'SmartServerIsReadonly')