86
89
# XXX: rename this class to BaseSmartServerRequestHandler ? A request
87
90
# *handler* is a different concept to the request.
89
def __init__(self, backing_transport, root_client_path='/'):
92
def __init__(self, backing_transport, root_client_path='/', jail_root=None):
92
95
:param backing_transport: the base transport to be used when performing
96
99
from the client. Clients will not be able to refer to paths above
97
100
this root. If root_client_path is None, then no translation will
98
101
be performed on client paths. Default is '/'.
102
:param jail_root: if specified, the root of the BzrDir.open jail to use
103
instead of backing_transport.
100
105
self._backing_transport = backing_transport
106
if jail_root is None:
107
jail_root = backing_transport
108
self._jail_root = jail_root
101
109
if root_client_path is not None:
102
110
if not root_client_path.startswith('/'):
103
111
root_client_path = '/' + root_client_path
265
273
# TODO: Better way of representing the body for commands that take it,
266
274
# and allow it to be streamed into the server.
268
def __init__(self, backing_transport, commands, root_client_path):
276
def __init__(self, backing_transport, commands, root_client_path,
271
280
:param backing_transport: a Transport to handle requests for.
275
284
self._backing_transport = backing_transport
276
285
self._root_client_path = root_client_path
277
286
self._commands = commands
287
if jail_root is None:
288
jail_root = backing_transport
289
self._jail_root = jail_root
278
290
self.response = None
279
291
self.finished_reading = False
280
292
self._command = None
293
if 'hpss' in debug.debug_flags:
294
self._request_start_time = osutils.timer_func()
295
self._thread_id = thread.get_ident()
297
def _trace(self, action, message, extra_bytes=None, include_time=False):
298
# It is a bit of a shame that this functionality overlaps with that of
299
# ProtocolThreeRequester._trace. However, there is enough difference
300
# that just putting it in a helper doesn't help a lot. And some state
301
# is taken from the instance.
303
t = '%5.3fs ' % (osutils.timer_func() - self._request_start_time)
306
if extra_bytes is None:
309
extra = ' ' + repr(extra_bytes[:40])
311
extra = extra[:29] + extra[-1] + '...'
312
trace.mutter('%12s: [%s] %s%s%s'
313
% (action, self._thread_id, t, message, extra))
282
315
def accept_body(self, bytes):
283
316
"""Accept body data."""
317
if self._command is None:
318
# no active command object, so ignore the event.
284
320
self._run_handler_code(self._command.do_chunk, (bytes,), {})
321
if 'hpss' in debug.debug_flags:
322
self._trace('accept body',
323
'%d bytes' % (len(bytes),), bytes)
286
325
def end_of_body(self):
287
326
"""No more body data will be received."""
288
327
self._run_handler_code(self._command.do_end, (), {})
289
328
# cannot read after this.
290
329
self.finished_reading = True
292
def dispatch_command(self, cmd, args):
293
"""Deprecated compatibility method.""" # XXX XXX
295
command = self._commands.get(cmd)
297
raise errors.UnknownSmartMethod(cmd)
298
self._command = command(self._backing_transport, self._root_client_path)
299
self._run_handler_code(self._command.execute, args, {})
330
if 'hpss' in debug.debug_flags:
331
self._trace('end of body', '', include_time=True)
301
333
def _run_handler_code(self, callable, args, kwargs):
302
334
"""Run some handler specific code 'callable'.
340
373
command = self._commands.get(cmd)
341
374
except LookupError:
375
if 'hpss' in debug.debug_flags:
376
self._trace('hpss unknown request',
377
cmd, repr(args)[1:-1])
342
378
raise errors.UnknownSmartMethod(cmd)
343
self._command = command(self._backing_transport)
379
if 'hpss' in debug.debug_flags:
380
from bzrlib.smart import vfs
381
if issubclass(command, vfs.VfsRequest):
382
action = 'hpss vfs req'
384
action = 'hpss request'
386
'%s %s' % (cmd, repr(args)[1:-1]))
387
self._command = command(
388
self._backing_transport, self._root_client_path, self._jail_root)
344
389
self._run_handler_code(self._command.execute, args, {})
346
391
def end_received(self):
392
if self._command is None:
393
# no active command object, so ignore the event.
347
395
self._run_handler_code(self._command.do_end, (), {})
396
if 'hpss' in debug.debug_flags:
397
self._trace('end', '', include_time=True)
349
399
def post_body_error_received(self, error_args):
350
400
# Just a no-op at the moment.
358
408
return ('FileExists', err.path)
359
409
elif isinstance(err, errors.DirectoryNotEmpty):
360
410
return ('DirectoryNotEmpty', err.path)
411
elif isinstance(err, errors.IncompatibleRepositories):
412
return ('IncompatibleRepositories', str(err.source), str(err.target),
361
414
elif isinstance(err, errors.ShortReadvError):
362
415
return ('ShortReadvError', err.path, str(err.offset), str(err.length),
392
445
elif isinstance(err, errors.TokenMismatch):
393
446
return ('TokenMismatch', err.given_token, err.lock_token)
394
447
elif isinstance(err, errors.LockContention):
395
return ('LockContention', err.lock, err.msg)
448
return ('LockContention',)
396
449
# Unserialisable error. Log it, and return a generic error
397
450
trace.log_exception_quietly()
398
451
return ('error', str(err))
445
498
'Branch.get_tags_bytes', 'bzrlib.smart.branch',
446
499
'SmartServerBranchGetTagsBytes')
447
500
request_handlers.register_lazy(
501
'Branch.set_tags_bytes', 'bzrlib.smart.branch',
502
'SmartServerBranchSetTagsBytes')
503
request_handlers.register_lazy(
448
504
'Branch.get_stacked_on_url', 'bzrlib.smart.branch', 'SmartServerBranchRequestGetStackedOnURL')
449
505
request_handlers.register_lazy(
450
506
'Branch.last_revision_info', 'bzrlib.smart.branch', 'SmartServerBranchRequestLastRevisionInfo')
454
510
'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
455
511
request_handlers.register_lazy( 'Branch.set_config_option',
456
512
'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOption')
513
request_handlers.register_lazy( 'Branch.set_config_option_dict',
514
'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOptionDict')
457
515
request_handlers.register_lazy( 'Branch.set_last_revision',
458
516
'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
459
517
request_handlers.register_lazy(
497
555
request_handlers.register_lazy(
498
556
'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')
499
557
request_handlers.register_lazy(
558
'BzrDir.open_2.1', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir_2_1')
559
request_handlers.register_lazy(
500
560
'BzrDir.open_branch', 'bzrlib.smart.bzrdir',
501
561
'SmartServerRequestOpenBranch')
502
562
request_handlers.register_lazy(
503
563
'BzrDir.open_branchV2', 'bzrlib.smart.bzrdir',
504
564
'SmartServerRequestOpenBranchV2')
505
565
request_handlers.register_lazy(
566
'BzrDir.open_branchV3', 'bzrlib.smart.bzrdir',
567
'SmartServerRequestOpenBranchV3')
568
request_handlers.register_lazy(
506
569
'delete', 'bzrlib.smart.vfs', 'DeleteRequest')
507
570
request_handlers.register_lazy(
508
571
'get', 'bzrlib.smart.vfs', 'GetRequest')
544
607
request_handlers.register_lazy(
545
608
'Repository.insert_stream', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStream')
546
609
request_handlers.register_lazy(
610
'Repository.insert_stream_1.19', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStream_1_19')
611
request_handlers.register_lazy(
547
612
'Repository.insert_stream_locked', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStreamLocked')
548
613
request_handlers.register_lazy(
549
614
'Repository.is_shared', 'bzrlib.smart.repository', 'SmartServerRepositoryIsShared')
561
626
'Repository.get_stream', 'bzrlib.smart.repository',
562
627
'SmartServerRepositoryGetStream')
563
628
request_handlers.register_lazy(
629
'Repository.get_stream_1.19', 'bzrlib.smart.repository',
630
'SmartServerRepositoryGetStream_1_19')
631
request_handlers.register_lazy(
564
632
'Repository.tarball', 'bzrlib.smart.repository',
565
633
'SmartServerRepositoryTarball')
566
634
request_handlers.register_lazy(