1
# Copyright (C) 2006-2010 Canonical Ltd
1
# Copyright (C) 2006, 2007 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
89
86
# XXX: rename this class to BaseSmartServerRequestHandler ? A request
90
87
# *handler* is a different concept to the request.
92
def __init__(self, backing_transport, root_client_path='/', jail_root=None):
89
def __init__(self, backing_transport, root_client_path='/'):
95
92
:param backing_transport: the base transport to be used when performing
99
96
from the client. Clients will not be able to refer to paths above
100
97
this root. If root_client_path is None, then no translation will
101
98
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.
105
100
self._backing_transport = backing_transport
106
if jail_root is None:
107
jail_root = backing_transport
108
self._jail_root = jail_root
109
101
if root_client_path is not None:
110
102
if not root_client_path.startswith('/'):
111
103
root_client_path = '/' + root_client_path
191
183
relpath = urlutils.joinpath('/', path)
192
184
if not relpath.startswith('/'):
193
185
raise ValueError(relpath)
194
return urlutils.escape('.' + relpath)
196
188
raise errors.PathNotChild(client_path, self._root_client_path)
273
265
# TODO: Better way of representing the body for commands that take it,
274
266
# and allow it to be streamed into the server.
276
def __init__(self, backing_transport, commands, root_client_path,
268
def __init__(self, backing_transport, commands, root_client_path):
280
271
:param backing_transport: a Transport to handle requests for.
284
275
self._backing_transport = backing_transport
285
276
self._root_client_path = root_client_path
286
277
self._commands = commands
287
if jail_root is None:
288
jail_root = backing_transport
289
self._jail_root = jail_root
290
278
self.response = None
291
279
self.finished_reading = False
292
280
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))
315
282
def accept_body(self, bytes):
316
283
"""Accept body data."""
318
285
# no active command object, so ignore the event.
320
287
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)
325
289
def end_of_body(self):
326
290
"""No more body data will be received."""
327
291
self._run_handler_code(self._command.do_end, (), {})
328
292
# cannot read after this.
329
293
self.finished_reading = True
330
if 'hpss' in debug.debug_flags:
331
self._trace('end of body', '', include_time=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, {})
333
304
def _run_handler_code(self, callable, args, kwargs):
334
305
"""Run some handler specific code 'callable'.
364
335
def headers_received(self, headers):
365
336
# Just a no-op at the moment.
366
if 'hpss' in debug.debug_flags:
367
self._trace('headers', repr(headers))
369
339
def args_received(self, args):
373
343
command = self._commands.get(cmd)
374
344
except LookupError:
375
if 'hpss' in debug.debug_flags:
376
self._trace('hpss unknown request',
377
cmd, repr(args)[1:-1])
378
345
raise errors.UnknownSmartMethod(cmd)
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)
346
self._command = command(self._backing_transport)
389
347
self._run_handler_code(self._command.execute, args, {})
391
349
def end_received(self):
393
351
# no active command object, so ignore the event.
395
353
self._run_handler_code(self._command.do_end, (), {})
396
if 'hpss' in debug.debug_flags:
397
self._trace('end', '', include_time=True)
399
355
def post_body_error_received(self, error_args):
400
356
# Just a no-op at the moment.
510
466
'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
511
467
request_handlers.register_lazy( 'Branch.set_config_option',
512
468
'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOption')
513
request_handlers.register_lazy( 'Branch.set_config_option_dict',
514
'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOptionDict')
515
469
request_handlers.register_lazy( 'Branch.set_last_revision',
516
470
'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
517
471
request_handlers.register_lazy(
555
509
request_handlers.register_lazy(
556
510
'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')
557
511
request_handlers.register_lazy(
558
'BzrDir.open_2.1', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir_2_1')
559
request_handlers.register_lazy(
560
512
'BzrDir.open_branch', 'bzrlib.smart.bzrdir',
561
513
'SmartServerRequestOpenBranch')
562
514
request_handlers.register_lazy(
563
515
'BzrDir.open_branchV2', 'bzrlib.smart.bzrdir',
564
516
'SmartServerRequestOpenBranchV2')
565
517
request_handlers.register_lazy(
566
'BzrDir.open_branchV3', 'bzrlib.smart.bzrdir',
567
'SmartServerRequestOpenBranchV3')
568
request_handlers.register_lazy(
569
518
'delete', 'bzrlib.smart.vfs', 'DeleteRequest')
570
519
request_handlers.register_lazy(
571
520
'get', 'bzrlib.smart.vfs', 'GetRequest')