13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Basic server-side logic for dealing with requests."""
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Infrastructure for server-side request handlers.
19
Interesting module attributes:
20
* The request_handlers registry maps verb names to SmartServerRequest
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).
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.
22
37
from bzrlib import (
28
from bzrlib.bundle.serializer import write_bundle
45
from bzrlib.lazy_import import lazy_import
46
lazy_import(globals(), """
47
from bzrlib.bundle import serializer
51
jail_info = threading.local()
52
jail_info.transports = None
56
bzrdir.BzrDir.hooks.install_named_hook(
57
'pre_open', _pre_open_hook, 'checking server jail')
60
def _pre_open_hook(transport):
61
allowed_transports = getattr(jail_info, 'transports', None)
62
if allowed_transports is None:
64
abspath = transport.base
65
for allowed_transport in allowed_transports:
67
allowed_transport.relpath(abspath)
68
except errors.PathNotChild:
72
raise errors.JailBreak(abspath)
31
78
class SmartServerRequest(object):
32
"""Base class for request handlers."""
34
def __init__(self, backing_transport):
79
"""Base class for request handlers.
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.
86
# XXX: rename this class to BaseSmartServerRequestHandler ? A request
87
# *handler* is a different concept to the request.
89
def __init__(self, backing_transport, root_client_path='/'):
37
92
:param backing_transport: the base transport to be used when performing
94
:param root_client_path: the client path that maps to the root of
95
backing_transport. This is used to interpret relpaths received
96
from the client. Clients will not be able to refer to paths above
97
this root. If root_client_path is None, then no translation will
98
be performed on client paths. Default is '/'.
40
100
self._backing_transport = backing_transport
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 += '/'
106
self._root_client_path = root_client_path
107
self._body_chunks = []
42
109
def _check_enabled(self):
43
110
"""Raises DisabledMethod if this method is disabled."""
67
134
def do_body(self, body_bytes):
68
135
"""Called if the client sends a body with the request.
137
The do() method is still called, and must have returned None.
70
139
Must return a SmartServerResponse.
72
# TODO: if a client erroneously sends a request that shouldn't have a
73
# body, what to do? Probably SmartServerRequestHandler should catch
74
# this NotImplementedError and translate it into a 'bad request' error
75
# to send to the client.
76
raise NotImplementedError(self.do_body)
142
raise errors.SmartProtocolError('Request does not expect a body')
144
def do_chunk(self, chunk_bytes):
145
"""Called with each body chunk if the request has a streamed body.
147
The do() method is still called, and must have returned None.
149
self._body_chunks.append(chunk_bytes)
152
"""Called when the end of the request has been received."""
153
body_bytes = ''.join(self._body_chunks)
154
self._body_chunks = None
155
return self.do_body(body_bytes)
157
def setup_jail(self):
158
jail_info.transports = [self._backing_transport]
160
def teardown_jail(self):
161
jail_info.transports = None
163
def translate_client_path(self, client_path):
164
"""Translate a path received from a network client into a local
167
All paths received from the client *must* be translated.
169
:param client_path: the path from the client.
170
:returns: a relpath that may be used with self._backing_transport
171
(unlike the untranslated client_path, which must not be used with
172
the backing transport).
174
if self._root_client_path is None:
175
# no translation necessary!
177
if not client_path.startswith('/'):
178
client_path = '/' + client_path
179
if client_path + '/' == self._root_client_path:
181
if client_path.startswith(self._root_client_path):
182
path = client_path[len(self._root_client_path):]
183
relpath = urlutils.joinpath('/', path)
184
if not relpath.startswith('/'):
185
raise ValueError(relpath)
188
raise errors.PathNotChild(client_path, self._root_client_path)
190
def transport_from_client_path(self, client_path):
191
"""Get a backing transport corresponding to the location referred to by
194
:seealso: translate_client_path
195
:returns: a transport cloned from self._backing_transport
197
relpath = self.translate_client_path(client_path)
198
return self._backing_transport.clone(relpath)
79
201
class SmartServerResponse(object):
80
202
"""A response to a client request.
82
204
This base class should not be used. Instead use
83
205
SuccessfulSmartServerResponse and FailedSmartServerResponse as appropriate.
86
def __init__(self, args, body=None):
208
def __init__(self, args, body=None, body_stream=None):
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
217
if body is not None and body_stream is not None:
218
raise errors.BzrError(
219
"'body' and 'body_stream' are mutually exclusive.")
221
self.body_stream = body_stream
90
223
def __eq__(self, other):
93
return other.args == self.args and other.body == self.body
226
return (other.args == self.args and
227
other.body == self.body and
228
other.body_stream is self.body_stream)
95
230
def __repr__(self):
96
return "<SmartServerResponse args=%r body=%r>" % (self.is_successful(),
231
return "<%s args=%r body=%r>" % (self.__class__.__name__,
97
232
self.args, self.body)
138
273
subclasses. e.g. bzrlib.transport.smart.vfs.vfs_commands.
140
275
self._backing_transport = backing_transport
276
self._root_client_path = root_client_path
141
277
self._commands = commands
142
self._body_bytes = ''
143
278
self.response = None
144
279
self.finished_reading = False
145
280
self._command = None
147
282
def accept_body(self, bytes):
148
283
"""Accept body data."""
150
# TODO: This should be overriden for each command that desired body data
151
# to handle the right format of that data, i.e. plain bytes, a bundle,
152
# etc. The deserialisation into that format should be done in the
155
# default fallback is to accumulate bytes.
156
self._body_bytes += bytes
284
self._run_handler_code(self._command.do_chunk, (bytes,), {})
158
286
def end_of_body(self):
159
287
"""No more body data will be received."""
160
self._run_handler_code(self._command.do_body, (self._body_bytes,), {})
288
self._run_handler_code(self._command.do_end, (), {})
161
289
# cannot read after this.
162
290
self.finished_reading = True
190
318
# XXX: most of this error conversion is VFS-related, and thus ought to
191
319
# be in SmartServerVFSRequestHandler somewhere.
193
return callable(*args, **kwargs)
194
except errors.NoSuchFile, e:
195
return FailedSmartServerResponse(('NoSuchFile', e.path))
196
except errors.FileExists, e:
197
return FailedSmartServerResponse(('FileExists', e.path))
198
except errors.DirectoryNotEmpty, e:
199
return FailedSmartServerResponse(('DirectoryNotEmpty', e.path))
200
except errors.ShortReadvError, e:
201
return FailedSmartServerResponse(('ShortReadvError',
202
e.path, str(e.offset), str(e.length), str(e.actual)))
203
except UnicodeError, e:
204
# If it is a DecodeError, than most likely we are starting
205
# with a plain string
206
str_or_unicode = e.object
207
if isinstance(str_or_unicode, unicode):
208
# XXX: UTF-8 might have \x01 (our seperator byte) in it. We
209
# should escape it somehow.
210
val = 'u:' + str_or_unicode.encode('utf-8')
212
val = 's:' + str_or_unicode.encode('base64')
213
# This handles UnicodeEncodeError or UnicodeDecodeError
214
return FailedSmartServerResponse((e.__class__.__name__,
215
e.encoding, val, str(e.start), str(e.end), e.reason))
216
except errors.TransportNotPossible, e:
217
if e.msg == "readonly transport":
218
return FailedSmartServerResponse(('ReadOnlyError', ))
321
self._command.setup_jail()
323
return callable(*args, **kwargs)
325
self._command.teardown_jail()
326
except (KeyboardInterrupt, SystemExit):
328
except Exception, err:
329
err_struct = _translate_error(err)
330
return FailedSmartServerResponse(err_struct)
332
def headers_received(self, headers):
333
# Just a no-op at the moment.
336
def args_received(self, args):
340
command = self._commands.get(cmd)
342
raise errors.UnknownSmartMethod(cmd)
343
self._command = command(self._backing_transport)
344
self._run_handler_code(self._command.execute, args, {})
346
def end_received(self):
347
self._run_handler_code(self._command.do_end, (), {})
349
def post_body_error_received(self, error_args):
350
# Just a no-op at the moment.
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),
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')
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)
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)
396
# Unserialisable error. Log it, and return a generic error
397
trace.log_exception_quietly()
398
return ('error', str(err))
223
401
class HelloRequest(SmartServerRequest):
259
437
request_handlers.register_lazy(
260
438
'append', 'bzrlib.smart.vfs', 'AppendRequest')
261
439
request_handlers.register_lazy(
262
'Branch.get_config_file', 'bzrlib.smart.branch', 'SmartServerBranchGetConfigFile')
440
'Branch.get_config_file', 'bzrlib.smart.branch',
441
'SmartServerBranchGetConfigFile')
442
request_handlers.register_lazy(
443
'Branch.get_parent', 'bzrlib.smart.branch', 'SmartServerBranchGetParent')
444
request_handlers.register_lazy(
445
'Branch.get_tags_bytes', 'bzrlib.smart.branch',
446
'SmartServerBranchGetTagsBytes')
447
request_handlers.register_lazy(
448
'Branch.get_stacked_on_url', 'bzrlib.smart.branch', 'SmartServerBranchRequestGetStackedOnURL')
263
449
request_handlers.register_lazy(
264
450
'Branch.last_revision_info', 'bzrlib.smart.branch', 'SmartServerBranchRequestLastRevisionInfo')
265
451
request_handlers.register_lazy(
266
452
'Branch.lock_write', 'bzrlib.smart.branch', 'SmartServerBranchRequestLockWrite')
267
request_handlers.register_lazy(
268
'Branch.revision_history', 'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
269
request_handlers.register_lazy(
270
'Branch.set_last_revision', 'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
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')
459
request_handlers.register_lazy(
460
'Branch.set_last_revision_info', 'bzrlib.smart.branch',
461
'SmartServerBranchRequestSetLastRevisionInfo')
462
request_handlers.register_lazy(
463
'Branch.set_last_revision_ex', 'bzrlib.smart.branch',
464
'SmartServerBranchRequestSetLastRevisionEx')
465
request_handlers.register_lazy(
466
'Branch.set_parent_location', 'bzrlib.smart.branch',
467
'SmartServerBranchRequestSetParentLocation')
271
468
request_handlers.register_lazy(
272
469
'Branch.unlock', 'bzrlib.smart.branch', 'SmartServerBranchRequestUnlock')
273
470
request_handlers.register_lazy(
274
'BzrDir.find_repository', 'bzrlib.smart.bzrdir', 'SmartServerRequestFindRepository')
275
request_handlers.register_lazy(
276
'BzrDirFormat.initialize', 'bzrlib.smart.bzrdir', 'SmartServerRequestInitializeBzrDir')
277
request_handlers.register_lazy(
278
'BzrDir.open_branch', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBranch')
471
'BzrDir.cloning_metadir', 'bzrlib.smart.bzrdir',
472
'SmartServerBzrDirRequestCloningMetaDir')
473
request_handlers.register_lazy(
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(
489
'BzrDir.get_config_file', 'bzrlib.smart.bzrdir',
490
'SmartServerBzrDirRequestConfigFile')
491
request_handlers.register_lazy(
492
'BzrDirFormat.initialize', 'bzrlib.smart.bzrdir',
493
'SmartServerRequestInitializeBzrDir')
494
request_handlers.register_lazy(
495
'BzrDirFormat.initialize_ex_1.16', 'bzrlib.smart.bzrdir',
496
'SmartServerRequestBzrDirInitializeEx')
497
request_handlers.register_lazy(
498
'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')
499
request_handlers.register_lazy(
500
'BzrDir.open_branch', 'bzrlib.smart.bzrdir',
501
'SmartServerRequestOpenBranch')
502
request_handlers.register_lazy(
503
'BzrDir.open_branchV2', 'bzrlib.smart.bzrdir',
504
'SmartServerRequestOpenBranchV2')
279
505
request_handlers.register_lazy(
280
506
'delete', 'bzrlib.smart.vfs', 'DeleteRequest')
281
507
request_handlers.register_lazy(
302
528
'readv', 'bzrlib.smart.vfs', 'ReadvRequest')
303
529
request_handlers.register_lazy(
304
530
'rename', 'bzrlib.smart.vfs', 'RenameRequest')
531
request_handlers.register_lazy(
532
'PackRepository.autopack', 'bzrlib.smart.packrepository',
533
'SmartServerPackRepositoryAutopack')
305
534
request_handlers.register_lazy('Repository.gather_stats',
306
535
'bzrlib.smart.repository',
307
536
'SmartServerRepositoryGatherStats')
537
request_handlers.register_lazy('Repository.get_parent_map',
538
'bzrlib.smart.repository',
539
'SmartServerRepositoryGetParentMap')
308
540
request_handlers.register_lazy(
309
541
'Repository.get_revision_graph', 'bzrlib.smart.repository', 'SmartServerRepositoryGetRevisionGraph')
310
542
request_handlers.register_lazy(
311
543
'Repository.has_revision', 'bzrlib.smart.repository', 'SmartServerRequestHasRevision')
312
544
request_handlers.register_lazy(
545
'Repository.insert_stream', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStream')
546
request_handlers.register_lazy(
547
'Repository.insert_stream_locked', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStreamLocked')
548
request_handlers.register_lazy(
313
549
'Repository.is_shared', 'bzrlib.smart.repository', 'SmartServerRepositoryIsShared')
314
550
request_handlers.register_lazy(
315
551
'Repository.lock_write', 'bzrlib.smart.repository', 'SmartServerRepositoryLockWrite')
316
552
request_handlers.register_lazy(
553
'Repository.set_make_working_trees', 'bzrlib.smart.repository',
554
'SmartServerRepositorySetMakeWorkingTrees')
555
request_handlers.register_lazy(
317
556
'Repository.unlock', 'bzrlib.smart.repository', 'SmartServerRepositoryUnlock')
318
557
request_handlers.register_lazy(
558
'Repository.get_rev_id_for_revno', 'bzrlib.smart.repository',
559
'SmartServerRepositoryGetRevIdForRevno')
560
request_handlers.register_lazy(
561
'Repository.get_stream', 'bzrlib.smart.repository',
562
'SmartServerRepositoryGetStream')
563
request_handlers.register_lazy(
319
564
'Repository.tarball', 'bzrlib.smart.repository',
320
565
'SmartServerRepositoryTarball')
321
566
request_handlers.register_lazy(