86
88
# XXX: rename this class to BaseSmartServerRequestHandler ? A request
87
89
# *handler* is a different concept to the request.
89
def __init__(self, backing_transport, root_client_path='/'):
91
def __init__(self, backing_transport, root_client_path='/', jail_root=None):
92
94
:param backing_transport: the base transport to be used when performing
96
98
from the client. Clients will not be able to refer to paths above
97
99
this root. If root_client_path is None, then no translation will
98
100
be performed on client paths. Default is '/'.
101
:param jail_root: if specified, the root of the BzrDir.open jail to use
102
instead of backing_transport.
100
104
self._backing_transport = backing_transport
105
if jail_root is None:
106
jail_root = backing_transport
107
self._jail_root = jail_root
101
108
if root_client_path is not None:
102
109
if not root_client_path.startswith('/'):
103
110
root_client_path = '/' + root_client_path
183
190
relpath = urlutils.joinpath('/', path)
184
191
if not relpath.startswith('/'):
185
192
raise ValueError(relpath)
193
return urlutils.escape('.' + relpath)
188
195
raise errors.PathNotChild(client_path, self._root_client_path)
265
272
# TODO: Better way of representing the body for commands that take it,
266
273
# and allow it to be streamed into the server.
268
def __init__(self, backing_transport, commands, root_client_path):
275
def __init__(self, backing_transport, commands, root_client_path,
271
279
:param backing_transport: a Transport to handle requests for.
275
283
self._backing_transport = backing_transport
276
284
self._root_client_path = root_client_path
277
285
self._commands = commands
286
if jail_root is None:
287
jail_root = backing_transport
288
self._jail_root = jail_root
278
289
self.response = None
279
290
self.finished_reading = False
280
291
self._command = None
292
if 'hpss' in debug.debug_flags:
293
self._request_start_time = osutils.timer_func()
294
cur_thread = threading.currentThread()
295
self._thread_id = getattr(cur_thread, 'ident', None)
296
if self._thread_id is None:
297
self._thread_id = cur_thread.getName()
299
def _trace(self, action, message, extra_bytes=None, include_time=False):
300
# It is a bit of a shame that this functionality overlaps with that of
301
# ProtocolThreeRequester._trace. However, there is enough difference
302
# that just putting it in a helper doesn't help a lot. And some state
303
# is taken from the instance.
305
t = '%5.3fs ' % (osutils.timer_func() - self._request_start_time)
308
if extra_bytes is None:
311
extra = ' ' + repr(extra_bytes[:40])
313
extra = extra[:29] + extra[-1] + '...'
314
trace.mutter('%12s: [%s] %s%s%s'
315
% (action, self._thread_id, t, message, extra))
282
317
def accept_body(self, bytes):
283
318
"""Accept body data."""
285
320
# no active command object, so ignore the event.
287
322
self._run_handler_code(self._command.do_chunk, (bytes,), {})
323
if 'hpss' in debug.debug_flags:
324
self._trace('accept body',
325
'%d bytes' % (len(bytes),), bytes)
289
327
def end_of_body(self):
290
328
"""No more body data will be received."""
291
329
self._run_handler_code(self._command.do_end, (), {})
292
330
# cannot read after this.
293
331
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, {})
332
if 'hpss' in debug.debug_flags:
333
self._trace('end of body', '', include_time=True)
304
335
def _run_handler_code(self, callable, args, kwargs):
305
336
"""Run some handler specific code 'callable'.
335
366
def headers_received(self, headers):
336
367
# Just a no-op at the moment.
368
if 'hpss' in debug.debug_flags:
369
self._trace('headers', repr(headers))
339
371
def args_received(self, args):
343
375
command = self._commands.get(cmd)
344
376
except LookupError:
377
if 'hpss' in debug.debug_flags:
378
self._trace('hpss unknown request',
379
cmd, repr(args)[1:-1])
345
380
raise errors.UnknownSmartMethod(cmd)
346
self._command = command(self._backing_transport)
381
if 'hpss' in debug.debug_flags:
382
from bzrlib.smart import vfs
383
if issubclass(command, vfs.VfsRequest):
384
action = 'hpss vfs req'
386
action = 'hpss request'
388
'%s %s' % (cmd, repr(args)[1:-1]))
389
self._command = command(
390
self._backing_transport, self._root_client_path, self._jail_root)
347
391
self._run_handler_code(self._command.execute, args, {})
349
393
def end_received(self):
351
395
# no active command object, so ignore the event.
353
397
self._run_handler_code(self._command.do_end, (), {})
398
if 'hpss' in debug.debug_flags:
399
self._trace('end', '', include_time=True)
355
401
def post_body_error_received(self, error_args):
356
402
# Just a no-op at the moment.
509
555
request_handlers.register_lazy(
510
556
'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')
511
557
request_handlers.register_lazy(
558
'BzrDir.open_2.1', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir_2_1')
559
request_handlers.register_lazy(
512
560
'BzrDir.open_branch', 'bzrlib.smart.bzrdir',
513
561
'SmartServerRequestOpenBranch')
514
562
request_handlers.register_lazy(