1
# Copyright (C) 2006, 2007 Canonical Ltd
1
# Copyright (C) 2006-2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
86
90
# XXX: rename this class to BaseSmartServerRequestHandler ? A request
87
91
# *handler* is a different concept to the request.
89
def __init__(self, backing_transport, root_client_path='/'):
93
def __init__(self, backing_transport, root_client_path='/', jail_root=None):
92
96
:param backing_transport: the base transport to be used when performing
96
100
from the client. Clients will not be able to refer to paths above
97
101
this root. If root_client_path is None, then no translation will
98
102
be performed on client paths. Default is '/'.
103
:param jail_root: if specified, the root of the BzrDir.open jail to use
104
instead of backing_transport.
100
106
self._backing_transport = backing_transport
107
if jail_root is None:
108
jail_root = backing_transport
109
self._jail_root = jail_root
101
110
if root_client_path is not None:
102
111
if not root_client_path.startswith('/'):
103
112
root_client_path = '/' + root_client_path
183
192
relpath = urlutils.joinpath('/', path)
184
193
if not relpath.startswith('/'):
185
194
raise ValueError(relpath)
195
return urlutils.escape('.' + relpath)
188
197
raise errors.PathNotChild(client_path, self._root_client_path)
265
274
# TODO: Better way of representing the body for commands that take it,
266
275
# and allow it to be streamed into the server.
268
def __init__(self, backing_transport, commands, root_client_path):
277
def __init__(self, backing_transport, commands, root_client_path,
271
281
:param backing_transport: a Transport to handle requests for.
275
285
self._backing_transport = backing_transport
276
286
self._root_client_path = root_client_path
277
287
self._commands = commands
288
if jail_root is None:
289
jail_root = backing_transport
290
self._jail_root = jail_root
278
291
self.response = None
279
292
self.finished_reading = False
280
293
self._command = None
294
if 'hpss' in debug.debug_flags:
295
self._request_start_time = osutils.timer_func()
296
self._thread_id = thread.get_ident()
298
def _trace(self, action, message, extra_bytes=None, include_time=False):
299
# It is a bit of a shame that this functionality overlaps with that of
300
# ProtocolThreeRequester._trace. However, there is enough difference
301
# that just putting it in a helper doesn't help a lot. And some state
302
# is taken from the instance.
304
t = '%5.3fs ' % (osutils.timer_func() - self._request_start_time)
307
if extra_bytes is None:
310
extra = ' ' + repr(extra_bytes[:40])
312
extra = extra[:29] + extra[-1] + '...'
313
trace.mutter('%12s: [%s] %s%s%s'
314
% (action, self._thread_id, t, message, extra))
282
316
def accept_body(self, bytes):
283
317
"""Accept body data."""
285
319
# no active command object, so ignore the event.
287
321
self._run_handler_code(self._command.do_chunk, (bytes,), {})
322
if 'hpss' in debug.debug_flags:
323
self._trace('accept body',
324
'%d bytes' % (len(bytes),), bytes)
289
326
def end_of_body(self):
290
327
"""No more body data will be received."""
291
328
self._run_handler_code(self._command.do_end, (), {})
292
329
# cannot read after this.
293
330
self.finished_reading = True
295
def dispatch_command(self, cmd, args):
296
"""Deprecated compatibility method.""" # XXX XXX
298
command = self._commands.get(cmd)
300
raise errors.UnknownSmartMethod(cmd)
301
self._command = command(self._backing_transport, self._root_client_path)
302
self._run_handler_code(self._command.execute, args, {})
331
if 'hpss' in debug.debug_flags:
332
self._trace('end of body', '', include_time=True)
304
334
def _run_handler_code(self, callable, args, kwargs):
305
335
"""Run some handler specific code 'callable'.
335
365
def headers_received(self, headers):
336
366
# Just a no-op at the moment.
367
if 'hpss' in debug.debug_flags:
368
self._trace('headers', repr(headers))
339
370
def args_received(self, args):
343
374
command = self._commands.get(cmd)
344
375
except LookupError:
376
if 'hpss' in debug.debug_flags:
377
self._trace('hpss unknown request',
378
cmd, repr(args)[1:-1])
345
379
raise errors.UnknownSmartMethod(cmd)
346
self._command = command(self._backing_transport)
380
if 'hpss' in debug.debug_flags:
381
from bzrlib.smart import vfs
382
if issubclass(command, vfs.VfsRequest):
383
action = 'hpss vfs req'
385
action = 'hpss request'
387
'%s %s' % (cmd, repr(args)[1:-1]))
388
self._command = command(
389
self._backing_transport, self._root_client_path, self._jail_root)
347
390
self._run_handler_code(self._command.execute, args, {})
349
392
def end_received(self):
351
394
# no active command object, so ignore the event.
353
396
self._run_handler_code(self._command.do_end, (), {})
397
if 'hpss' in debug.debug_flags:
398
self._trace('end', '', include_time=True)
355
400
def post_body_error_received(self, error_args):
356
401
# Just a no-op at the moment.
364
409
return ('FileExists', err.path)
365
410
elif isinstance(err, errors.DirectoryNotEmpty):
366
411
return ('DirectoryNotEmpty', err.path)
412
elif isinstance(err, errors.IncompatibleRepositories):
413
return ('IncompatibleRepositories', str(err.source), str(err.target),
367
415
elif isinstance(err, errors.ShortReadvError):
368
416
return ('ShortReadvError', err.path, str(err.offset), str(err.length),
399
447
return ('TokenMismatch', err.given_token, err.lock_token)
400
448
elif isinstance(err, errors.LockContention):
401
449
return ('LockContention',)
450
elif isinstance(err, MemoryError):
451
# GZ 2011-02-24: Copy bzrlib.trace -Dmem_dump functionality here?
452
return ('MemoryError',)
402
453
# Unserialisable error. Log it, and return a generic error
403
454
trace.log_exception_quietly()
404
return ('error', str(err))
455
return ('error', trace._qualified_exception_name(err.__class__, True),
407
459
class HelloRequest(SmartServerRequest):
454
506
'Branch.set_tags_bytes', 'bzrlib.smart.branch',
455
507
'SmartServerBranchSetTagsBytes')
456
508
request_handlers.register_lazy(
509
'Branch.heads_to_fetch', 'bzrlib.smart.branch',
510
'SmartServerBranchHeadsToFetch')
511
request_handlers.register_lazy(
457
512
'Branch.get_stacked_on_url', 'bzrlib.smart.branch', 'SmartServerBranchRequestGetStackedOnURL')
458
513
request_handlers.register_lazy(
459
514
'Branch.last_revision_info', 'bzrlib.smart.branch', 'SmartServerBranchRequestLastRevisionInfo')
463
518
'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
464
519
request_handlers.register_lazy( 'Branch.set_config_option',
465
520
'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOption')
521
request_handlers.register_lazy( 'Branch.set_config_option_dict',
522
'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOptionDict')
466
523
request_handlers.register_lazy( 'Branch.set_last_revision',
467
524
'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
468
525
request_handlers.register_lazy(
506
563
request_handlers.register_lazy(
507
564
'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')
508
565
request_handlers.register_lazy(
566
'BzrDir.open_2.1', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir_2_1')
567
request_handlers.register_lazy(
509
568
'BzrDir.open_branch', 'bzrlib.smart.bzrdir',
510
569
'SmartServerRequestOpenBranch')
511
570
request_handlers.register_lazy(
512
571
'BzrDir.open_branchV2', 'bzrlib.smart.bzrdir',
513
572
'SmartServerRequestOpenBranchV2')
514
573
request_handlers.register_lazy(
574
'BzrDir.open_branchV3', 'bzrlib.smart.bzrdir',
575
'SmartServerRequestOpenBranchV3')
576
request_handlers.register_lazy(
515
577
'delete', 'bzrlib.smart.vfs', 'DeleteRequest')
516
578
request_handlers.register_lazy(
517
579
'get', 'bzrlib.smart.vfs', 'GetRequest')
553
615
request_handlers.register_lazy(
554
616
'Repository.insert_stream', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStream')
555
617
request_handlers.register_lazy(
618
'Repository.insert_stream_1.19', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStream_1_19')
619
request_handlers.register_lazy(
556
620
'Repository.insert_stream_locked', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStreamLocked')
557
621
request_handlers.register_lazy(
558
622
'Repository.is_shared', 'bzrlib.smart.repository', 'SmartServerRepositoryIsShared')
570
634
'Repository.get_stream', 'bzrlib.smart.repository',
571
635
'SmartServerRepositoryGetStream')
572
636
request_handlers.register_lazy(
637
'Repository.get_stream_1.19', 'bzrlib.smart.repository',
638
'SmartServerRepositoryGetStream_1_19')
639
request_handlers.register_lazy(
573
640
'Repository.tarball', 'bzrlib.smart.repository',
574
641
'SmartServerRepositoryTarball')
575
642
request_handlers.register_lazy(