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., 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).
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Basic server-side logic for dealing with requests.
21
The class names are a little confusing: the protocol will instantiate a
22
SmartServerRequestHandler, whose dispatch_command method creates an instance of
23
a SmartServerRequest subclass.
25
The request_handlers registry tracks SmartServerRequest classes (rather than
26
SmartServerRequestHandler).
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.
36
31
from bzrlib import (
46
from bzrlib.lazy_import import lazy_import
47
lazy_import(globals(), """
48
from bzrlib.bundle import serializer
55
jail_info = threading.local()
56
jail_info.transports = None
60
bzrdir.BzrDir.hooks.install_named_hook(
61
'pre_open', _pre_open_hook, 'checking server jail')
64
def _pre_open_hook(transport):
65
allowed_transports = getattr(jail_info, 'transports', None)
66
if allowed_transports is None:
68
abspath = transport.base
69
for allowed_transport in allowed_transports:
71
allowed_transport.relpath(abspath)
72
except errors.PathNotChild:
76
raise errors.JailBreak(abspath)
38
from bzrlib.bundle.serializer import write_bundle
82
41
class SmartServerRequest(object):
83
42
"""Base class for request handlers.
85
44
To define a new request, subclass this class and override the `do` method
86
45
(and if appropriate, `do_body` as well). Request implementors should take
87
46
care to call `translate_client_path` and `transport_from_client_path` as
285
227
self._backing_transport = backing_transport
286
228
self._root_client_path = root_client_path
287
229
self._commands = commands
288
if jail_root is None:
289
jail_root = backing_transport
290
self._jail_root = jail_root
230
self._body_bytes = ''
291
231
self.response = None
292
232
self.finished_reading = False
293
233
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))
316
235
def accept_body(self, bytes):
317
236
"""Accept body data."""
318
if self._command is None:
319
# no active command object, so ignore the event.
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)
238
# TODO: This should be overriden for each command that desired body data
239
# to handle the right format of that data, i.e. plain bytes, a bundle,
240
# etc. The deserialisation into that format should be done in the
243
# default fallback is to accumulate bytes.
244
self._body_bytes += bytes
326
246
def end_of_body(self):
327
247
"""No more body data will be received."""
328
self._run_handler_code(self._command.do_end, (), {})
248
self._run_handler_code(self._command.do_body, (self._body_bytes,), {})
329
249
# cannot read after this.
330
250
self.finished_reading = True
331
if 'hpss' in debug.debug_flags:
332
self._trace('end of body', '', include_time=True)
252
def dispatch_command(self, cmd, args):
253
"""Deprecated compatibility method.""" # XXX XXX
255
command = self._commands.get(cmd)
257
raise errors.UnknownSmartMethod(cmd)
258
self._command = command(self._backing_transport, self._root_client_path)
259
self._run_handler_code(self._command.execute, args, {})
334
261
def _run_handler_code(self, callable, args, kwargs):
335
262
"""Run some handler specific code 'callable'.
351
278
# XXX: most of this error conversion is VFS-related, and thus ought to
352
279
# be in SmartServerVFSRequestHandler somewhere.
354
self._command.setup_jail()
356
return callable(*args, **kwargs)
358
self._command.teardown_jail()
359
except (KeyboardInterrupt, SystemExit):
361
except Exception, err:
362
err_struct = _translate_error(err)
363
return FailedSmartServerResponse(err_struct)
281
return callable(*args, **kwargs)
282
except errors.NoSuchFile, e:
283
return FailedSmartServerResponse(('NoSuchFile', e.path))
284
except errors.FileExists, e:
285
return FailedSmartServerResponse(('FileExists', e.path))
286
except errors.DirectoryNotEmpty, e:
287
return FailedSmartServerResponse(('DirectoryNotEmpty', e.path))
288
except errors.ShortReadvError, e:
289
return FailedSmartServerResponse(('ShortReadvError',
290
e.path, str(e.offset), str(e.length), str(e.actual)))
291
except UnicodeError, e:
292
# If it is a DecodeError, than most likely we are starting
293
# with a plain string
294
str_or_unicode = e.object
295
if isinstance(str_or_unicode, unicode):
296
# XXX: UTF-8 might have \x01 (our seperator byte) in it. We
297
# should escape it somehow.
298
val = 'u:' + str_or_unicode.encode('utf-8')
300
val = 's:' + str_or_unicode.encode('base64')
301
# This handles UnicodeEncodeError or UnicodeDecodeError
302
return FailedSmartServerResponse((e.__class__.__name__,
303
e.encoding, val, str(e.start), str(e.end), e.reason))
304
except errors.TransportNotPossible, e:
305
if e.msg == "readonly transport":
306
return FailedSmartServerResponse(('ReadOnlyError', ))
365
310
def headers_received(self, headers):
366
311
# Just a no-op at the moment.
367
if 'hpss' in debug.debug_flags:
368
self._trace('headers', repr(headers))
370
314
def args_received(self, args):
374
318
command = self._commands.get(cmd)
375
319
except LookupError:
376
if 'hpss' in debug.debug_flags:
377
self._trace('hpss unknown request',
378
cmd, repr(args)[1:-1])
379
320
raise errors.UnknownSmartMethod(cmd)
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)
321
self._command = command(self._backing_transport)
390
322
self._run_handler_code(self._command.execute, args, {})
324
def prefixed_body_received(self, body_bytes):
325
"""No more body data will be received."""
326
self._run_handler_code(self._command.do_body, (body_bytes,), {})
327
# cannot read after this.
328
self.finished_reading = True
330
def body_chunk_received(self, chunk_bytes):
331
self._run_handler_code(self._command.do_chunk, (chunk_bytes,), {})
392
333
def end_received(self):
393
if self._command is None:
394
# no active command object, so ignore the event.
396
334
self._run_handler_code(self._command.do_end, (), {})
397
if 'hpss' in debug.debug_flags:
398
self._trace('end', '', include_time=True)
400
def post_body_error_received(self, error_args):
401
# Just a no-op at the moment.
405
def _translate_error(err):
406
if isinstance(err, errors.NoSuchFile):
407
return ('NoSuchFile', err.path)
408
elif isinstance(err, errors.FileExists):
409
return ('FileExists', err.path)
410
elif isinstance(err, errors.DirectoryNotEmpty):
411
return ('DirectoryNotEmpty', err.path)
412
elif isinstance(err, errors.IncompatibleRepositories):
413
return ('IncompatibleRepositories', str(err.source), str(err.target),
415
elif isinstance(err, errors.ShortReadvError):
416
return ('ShortReadvError', err.path, str(err.offset), str(err.length),
418
elif isinstance(err, errors.UnstackableRepositoryFormat):
419
return (('UnstackableRepositoryFormat', str(err.format), err.url))
420
elif isinstance(err, errors.UnstackableBranchFormat):
421
return ('UnstackableBranchFormat', str(err.format), err.url)
422
elif isinstance(err, errors.NotStacked):
423
return ('NotStacked',)
424
elif isinstance(err, UnicodeError):
425
# If it is a DecodeError, than most likely we are starting
426
# with a plain string
427
str_or_unicode = err.object
428
if isinstance(str_or_unicode, unicode):
429
# XXX: UTF-8 might have \x01 (our protocol v1 and v2 seperator
430
# byte) in it, so this encoding could cause broken responses.
431
# Newer clients use protocol v3, so will be fine.
432
val = 'u:' + str_or_unicode.encode('utf-8')
434
val = 's:' + str_or_unicode.encode('base64')
435
# This handles UnicodeEncodeError or UnicodeDecodeError
436
return (err.__class__.__name__, err.encoding, val, str(err.start),
437
str(err.end), err.reason)
438
elif isinstance(err, errors.TransportNotPossible):
439
if err.msg == "readonly transport":
440
return ('ReadOnlyError', )
441
elif isinstance(err, errors.ReadError):
442
# cannot read the file
443
return ('ReadError', err.path)
444
elif isinstance(err, errors.PermissionDenied):
445
return ('PermissionDenied', err.path, err.extra)
446
elif isinstance(err, errors.TokenMismatch):
447
return ('TokenMismatch', err.given_token, err.lock_token)
448
elif isinstance(err, errors.LockContention):
449
return ('LockContention',)
450
elif isinstance(err, MemoryError):
451
# GZ 2011-02-24: Copy bzrlib.trace -Dmem_dump functionality here?
452
return ('MemoryError',)
453
# Unserialisable error. Log it, and return a generic error
454
trace.log_exception_quietly()
455
return ('error', trace._qualified_exception_name(err.__class__, True),
459
337
class HelloRequest(SmartServerRequest):
495
373
request_handlers.register_lazy(
496
374
'append', 'bzrlib.smart.vfs', 'AppendRequest')
497
375
request_handlers.register_lazy(
498
'Branch.get_config_file', 'bzrlib.smart.branch',
499
'SmartServerBranchGetConfigFile')
500
request_handlers.register_lazy(
501
'Branch.get_parent', 'bzrlib.smart.branch', 'SmartServerBranchGetParent')
502
request_handlers.register_lazy(
503
'Branch.get_tags_bytes', 'bzrlib.smart.branch',
504
'SmartServerBranchGetTagsBytes')
505
request_handlers.register_lazy(
506
'Branch.set_tags_bytes', 'bzrlib.smart.branch',
507
'SmartServerBranchSetTagsBytes')
508
request_handlers.register_lazy(
509
'Branch.heads_to_fetch', 'bzrlib.smart.branch',
510
'SmartServerBranchHeadsToFetch')
511
request_handlers.register_lazy(
512
'Branch.get_stacked_on_url', 'bzrlib.smart.branch', 'SmartServerBranchRequestGetStackedOnURL')
376
'Branch.get_config_file', 'bzrlib.smart.branch', 'SmartServerBranchGetConfigFile')
513
377
request_handlers.register_lazy(
514
378
'Branch.last_revision_info', 'bzrlib.smart.branch', 'SmartServerBranchRequestLastRevisionInfo')
515
379
request_handlers.register_lazy(
516
380
'Branch.lock_write', 'bzrlib.smart.branch', 'SmartServerBranchRequestLockWrite')
517
request_handlers.register_lazy( 'Branch.revision_history',
518
'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
519
request_handlers.register_lazy( 'Branch.set_config_option',
520
'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOption')
521
request_handlers.register_lazy( 'Branch.set_config_option_dict',
522
'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOptionDict')
523
request_handlers.register_lazy( 'Branch.set_last_revision',
524
'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
381
request_handlers.register_lazy(
382
'Branch.revision_history', 'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
383
request_handlers.register_lazy(
384
'Branch.set_last_revision', 'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
525
385
request_handlers.register_lazy(
526
386
'Branch.set_last_revision_info', 'bzrlib.smart.branch',
527
387
'SmartServerBranchRequestSetLastRevisionInfo')
528
388
request_handlers.register_lazy(
529
'Branch.set_last_revision_ex', 'bzrlib.smart.branch',
530
'SmartServerBranchRequestSetLastRevisionEx')
531
request_handlers.register_lazy(
532
'Branch.set_parent_location', 'bzrlib.smart.branch',
533
'SmartServerBranchRequestSetParentLocation')
534
request_handlers.register_lazy(
535
389
'Branch.unlock', 'bzrlib.smart.branch', 'SmartServerBranchRequestUnlock')
536
390
request_handlers.register_lazy(
537
'BzrDir.cloning_metadir', 'bzrlib.smart.bzrdir',
538
'SmartServerBzrDirRequestCloningMetaDir')
539
request_handlers.register_lazy(
540
'BzrDir.create_branch', 'bzrlib.smart.bzrdir',
541
'SmartServerRequestCreateBranch')
542
request_handlers.register_lazy(
543
'BzrDir.create_repository', 'bzrlib.smart.bzrdir',
544
'SmartServerRequestCreateRepository')
545
request_handlers.register_lazy(
546
'BzrDir.find_repository', 'bzrlib.smart.bzrdir',
547
'SmartServerRequestFindRepositoryV1')
548
request_handlers.register_lazy(
549
'BzrDir.find_repositoryV2', 'bzrlib.smart.bzrdir',
550
'SmartServerRequestFindRepositoryV2')
551
request_handlers.register_lazy(
552
'BzrDir.find_repositoryV3', 'bzrlib.smart.bzrdir',
553
'SmartServerRequestFindRepositoryV3')
554
request_handlers.register_lazy(
555
'BzrDir.get_config_file', 'bzrlib.smart.bzrdir',
556
'SmartServerBzrDirRequestConfigFile')
557
request_handlers.register_lazy(
558
'BzrDirFormat.initialize', 'bzrlib.smart.bzrdir',
559
'SmartServerRequestInitializeBzrDir')
560
request_handlers.register_lazy(
561
'BzrDirFormat.initialize_ex_1.16', 'bzrlib.smart.bzrdir',
562
'SmartServerRequestBzrDirInitializeEx')
563
request_handlers.register_lazy(
564
'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')
565
request_handlers.register_lazy(
566
'BzrDir.open_2.1', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir_2_1')
567
request_handlers.register_lazy(
568
'BzrDir.open_branch', 'bzrlib.smart.bzrdir',
569
'SmartServerRequestOpenBranch')
570
request_handlers.register_lazy(
571
'BzrDir.open_branchV2', 'bzrlib.smart.bzrdir',
572
'SmartServerRequestOpenBranchV2')
573
request_handlers.register_lazy(
574
'BzrDir.open_branchV3', 'bzrlib.smart.bzrdir',
575
'SmartServerRequestOpenBranchV3')
391
'BzrDir.find_repository', 'bzrlib.smart.bzrdir', 'SmartServerRequestFindRepositoryV1')
392
request_handlers.register_lazy(
393
'BzrDir.find_repositoryV2', 'bzrlib.smart.bzrdir', 'SmartServerRequestFindRepositoryV2')
394
request_handlers.register_lazy(
395
'BzrDirFormat.initialize', 'bzrlib.smart.bzrdir', 'SmartServerRequestInitializeBzrDir')
396
request_handlers.register_lazy(
397
'BzrDir.open_branch', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBranch')
576
398
request_handlers.register_lazy(
577
399
'delete', 'bzrlib.smart.vfs', 'DeleteRequest')
578
400
request_handlers.register_lazy(
609
428
'bzrlib.smart.repository',
610
429
'SmartServerRepositoryGetParentMap')
611
430
request_handlers.register_lazy(
431
'Repository.stream_knit_data_for_revisions',
432
'bzrlib.smart.repository',
433
'SmartServerRepositoryStreamKnitDataForRevisions')
434
request_handlers.register_lazy(
435
'Repository.stream_revisions_chunked',
436
'bzrlib.smart.repository',
437
'SmartServerRepositoryStreamRevisionsChunked')
438
request_handlers.register_lazy(
612
439
'Repository.get_revision_graph', 'bzrlib.smart.repository', 'SmartServerRepositoryGetRevisionGraph')
613
440
request_handlers.register_lazy(
614
441
'Repository.has_revision', 'bzrlib.smart.repository', 'SmartServerRequestHasRevision')
615
442
request_handlers.register_lazy(
616
'Repository.insert_stream', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStream')
617
request_handlers.register_lazy(
618
'Repository.insert_stream_1.19', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStream_1_19')
619
request_handlers.register_lazy(
620
'Repository.insert_stream_locked', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStreamLocked')
621
request_handlers.register_lazy(
622
443
'Repository.is_shared', 'bzrlib.smart.repository', 'SmartServerRepositoryIsShared')
623
444
request_handlers.register_lazy(
624
445
'Repository.lock_write', 'bzrlib.smart.repository', 'SmartServerRepositoryLockWrite')
625
446
request_handlers.register_lazy(
626
'Repository.set_make_working_trees', 'bzrlib.smart.repository',
627
'SmartServerRepositorySetMakeWorkingTrees')
628
request_handlers.register_lazy(
629
447
'Repository.unlock', 'bzrlib.smart.repository', 'SmartServerRepositoryUnlock')
630
448
request_handlers.register_lazy(
631
'Repository.get_rev_id_for_revno', 'bzrlib.smart.repository',
632
'SmartServerRepositoryGetRevIdForRevno')
633
request_handlers.register_lazy(
634
'Repository.get_stream', 'bzrlib.smart.repository',
635
'SmartServerRepositoryGetStream')
636
request_handlers.register_lazy(
637
'Repository.get_stream_1.19', 'bzrlib.smart.repository',
638
'SmartServerRepositoryGetStream_1_19')
639
request_handlers.register_lazy(
640
449
'Repository.tarball', 'bzrlib.smart.repository',
641
450
'SmartServerRepositoryTarball')
642
451
request_handlers.register_lazy(