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.
37
31
from bzrlib import (
51
jail_info = threading.local()
52
jail_info.transports = None
56
bzrdir.BzrDir.hooks.install_named_hook(
57
'pre_open', _pre_open_hook, 'checking server jail')
60
def _pre_open_hook(transport):
61
allowed_transports = getattr(jail_info, 'transports', None)
62
if allowed_transports is None:
64
abspath = transport.base
65
for allowed_transport in allowed_transports:
67
allowed_transport.relpath(abspath)
68
except errors.PathNotChild:
72
raise errors.BzrError('jail break: %r' % (abspath,))
78
45
class SmartServerRequest(object):
79
46
"""Base class for request handlers.
316
277
# XXX: most of this error conversion is VFS-related, and thus ought to
317
278
# be in SmartServerVFSRequestHandler somewhere.
319
self._command.setup_jail()
321
return callable(*args, **kwargs)
323
self._command.teardown_jail()
280
return callable(*args, **kwargs)
324
281
except (KeyboardInterrupt, SystemExit):
326
283
except Exception, err:
387
344
return ('ReadError', err.path)
388
345
elif isinstance(err, errors.PermissionDenied):
389
346
return ('PermissionDenied', err.path, err.extra)
390
elif isinstance(err, errors.TokenMismatch):
391
return ('TokenMismatch', err.given_token, err.lock_token)
392
elif isinstance(err, errors.LockContention):
393
return ('LockContention', err.lock, err.msg)
394
347
# Unserialisable error. Log it, and return a generic error
395
348
trace.log_exception_quietly()
396
349
return ('error', str(err))
435
388
request_handlers.register_lazy(
436
389
'append', 'bzrlib.smart.vfs', 'AppendRequest')
437
390
request_handlers.register_lazy(
438
'Branch.get_config_file', 'bzrlib.smart.branch',
439
'SmartServerBranchGetConfigFile')
391
'Branch.get_config_file', 'bzrlib.smart.branch', 'SmartServerBranchGetConfigFile')
440
392
request_handlers.register_lazy(
441
393
'Branch.get_parent', 'bzrlib.smart.branch', 'SmartServerBranchGetParent')
442
394
request_handlers.register_lazy(
443
'Branch.get_tags_bytes', 'bzrlib.smart.branch',
444
'SmartServerBranchGetTagsBytes')
445
request_handlers.register_lazy(
446
395
'Branch.get_stacked_on_url', 'bzrlib.smart.branch', 'SmartServerBranchRequestGetStackedOnURL')
447
396
request_handlers.register_lazy(
448
397
'Branch.last_revision_info', 'bzrlib.smart.branch', 'SmartServerBranchRequestLastRevisionInfo')
449
398
request_handlers.register_lazy(
450
399
'Branch.lock_write', 'bzrlib.smart.branch', 'SmartServerBranchRequestLockWrite')
451
request_handlers.register_lazy( 'Branch.revision_history',
452
'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
453
request_handlers.register_lazy( 'Branch.set_config_option',
454
'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOption')
455
request_handlers.register_lazy( 'Branch.set_last_revision',
456
'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
400
request_handlers.register_lazy(
401
'Branch.revision_history', 'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
402
request_handlers.register_lazy(
403
'Branch.set_last_revision', 'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
457
404
request_handlers.register_lazy(
458
405
'Branch.set_last_revision_info', 'bzrlib.smart.branch',
459
406
'SmartServerBranchRequestSetLastRevisionInfo')
466
413
'BzrDir.cloning_metadir', 'bzrlib.smart.bzrdir',
467
414
'SmartServerBzrDirRequestCloningMetaDir')
468
415
request_handlers.register_lazy(
469
'BzrDir.create_branch', 'bzrlib.smart.bzrdir',
470
'SmartServerRequestCreateBranch')
471
request_handlers.register_lazy(
472
'BzrDir.create_repository', 'bzrlib.smart.bzrdir',
473
'SmartServerRequestCreateRepository')
474
request_handlers.register_lazy(
475
'BzrDir.find_repository', 'bzrlib.smart.bzrdir',
476
'SmartServerRequestFindRepositoryV1')
477
request_handlers.register_lazy(
478
'BzrDir.find_repositoryV2', 'bzrlib.smart.bzrdir',
479
'SmartServerRequestFindRepositoryV2')
480
request_handlers.register_lazy(
481
'BzrDir.find_repositoryV3', 'bzrlib.smart.bzrdir',
482
'SmartServerRequestFindRepositoryV3')
483
request_handlers.register_lazy(
484
'BzrDirFormat.initialize', 'bzrlib.smart.bzrdir',
485
'SmartServerRequestInitializeBzrDir')
486
request_handlers.register_lazy(
487
'BzrDir.open_branch', 'bzrlib.smart.bzrdir',
488
'SmartServerRequestOpenBranch')
489
request_handlers.register_lazy(
490
'BzrDir.open_branchV2', 'bzrlib.smart.bzrdir',
491
'SmartServerRequestOpenBranchV2')
416
'BzrDir.create_branch', 'bzrlib.smart.bzrdir', 'SmartServerRequestCreateBranch')
417
request_handlers.register_lazy(
418
'BzrDir.create_repository', 'bzrlib.smart.bzrdir', 'SmartServerRequestCreateRepository')
419
request_handlers.register_lazy(
420
'BzrDir.find_repository', 'bzrlib.smart.bzrdir', 'SmartServerRequestFindRepositoryV1')
421
request_handlers.register_lazy(
422
'BzrDir.find_repositoryV2', 'bzrlib.smart.bzrdir', 'SmartServerRequestFindRepositoryV2')
423
request_handlers.register_lazy(
424
'BzrDir.find_repositoryV3', 'bzrlib.smart.bzrdir', 'SmartServerRequestFindRepositoryV3')
425
request_handlers.register_lazy(
426
'BzrDirFormat.initialize', 'bzrlib.smart.bzrdir', 'SmartServerRequestInitializeBzrDir')
427
request_handlers.register_lazy(
428
'BzrDir.open_branch', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBranch')
492
429
request_handlers.register_lazy(
493
430
'delete', 'bzrlib.smart.vfs', 'DeleteRequest')
494
431
request_handlers.register_lazy(
531
468
request_handlers.register_lazy(
532
469
'Repository.insert_stream', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStream')
533
470
request_handlers.register_lazy(
534
'Repository.insert_stream_locked', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStreamLocked')
535
request_handlers.register_lazy(
536
471
'Repository.is_shared', 'bzrlib.smart.repository', 'SmartServerRepositoryIsShared')
537
472
request_handlers.register_lazy(
538
473
'Repository.lock_write', 'bzrlib.smart.repository', 'SmartServerRepositoryLockWrite')