/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/smart/request.py

  • Committer: Robert Collins
  • Date: 2009-05-23 20:57:12 UTC
  • mfrom: (4371 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4441.
  • Revision ID: robertc@robertcollins.net-20090523205712-lcwbfqk6vwavinuv
MergeĀ .dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
"""Basic server-side logic for dealing with requests.
18
 
 
19
 
**XXX**:
20
 
 
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.
24
 
 
25
 
The request_handlers registry tracks SmartServerRequest classes (rather than
26
 
SmartServerRequestHandler).
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Infrastructure for server-side request handlers.
 
18
 
 
19
Interesting module attributes:
 
20
    * The request_handlers registry maps verb names to SmartServerRequest
 
21
      classes.
 
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).
27
27
"""
28
28
 
 
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.
 
32
 
 
33
 
29
34
import tempfile
 
35
import threading
30
36
 
31
37
from bzrlib import (
32
38
    bzrdir,
42
48
""")
43
49
 
44
50
 
 
51
jail_info = threading.local()
 
52
jail_info.transports = None
 
53
 
 
54
 
 
55
def _install_hook():
 
56
    bzrdir.BzrDir.hooks.install_named_hook(
 
57
        'pre_open', _pre_open_hook, 'checking server jail')
 
58
 
 
59
 
 
60
def _pre_open_hook(transport):
 
61
    allowed_transports = getattr(jail_info, 'transports', None)
 
62
    if allowed_transports is None:
 
63
        return
 
64
    abspath = transport.base
 
65
    for allowed_transport in allowed_transports:
 
66
        try:
 
67
            allowed_transport.relpath(abspath)
 
68
        except errors.PathNotChild:
 
69
            continue
 
70
        else:
 
71
            return
 
72
    raise errors.JailBreak(abspath)
 
73
 
 
74
 
 
75
_install_hook()
 
76
 
 
77
 
45
78
class SmartServerRequest(object):
46
79
    """Base class for request handlers.
47
80
 
121
154
        self._body_chunks = None
122
155
        return self.do_body(body_bytes)
123
156
 
 
157
    def setup_jail(self):
 
158
        jail_info.transports = [self._backing_transport]
 
159
 
 
160
    def teardown_jail(self):
 
161
        jail_info.transports = None
 
162
 
124
163
    def translate_client_path(self, client_path):
125
164
        """Translate a path received from a network client into a local
126
165
        relpath.
137
176
            return client_path
138
177
        if not client_path.startswith('/'):
139
178
            client_path = '/' + client_path
 
179
        if client_path + '/' == self._root_client_path:
 
180
            return '.'
140
181
        if client_path.startswith(self._root_client_path):
141
182
            path = client_path[len(self._root_client_path):]
142
183
            relpath = urlutils.joinpath('/', path)
277
318
        # XXX: most of this error conversion is VFS-related, and thus ought to
278
319
        # be in SmartServerVFSRequestHandler somewhere.
279
320
        try:
280
 
            return callable(*args, **kwargs)
 
321
            self._command.setup_jail()
 
322
            try:
 
323
                return callable(*args, **kwargs)
 
324
            finally:
 
325
                self._command.teardown_jail()
281
326
        except (KeyboardInterrupt, SystemExit):
282
327
            raise
283
328
        except Exception, err:
344
389
        return ('ReadError', err.path)
345
390
    elif isinstance(err, errors.PermissionDenied):
346
391
        return ('PermissionDenied', err.path, err.extra)
 
392
    elif isinstance(err, errors.TokenMismatch):
 
393
        return ('TokenMismatch', err.given_token, err.lock_token)
 
394
    elif isinstance(err, errors.LockContention):
 
395
        return ('LockContention', err.lock, err.msg)
347
396
    # Unserialisable error.  Log it, and return a generic error
348
397
    trace.log_exception_quietly()
349
398
    return ('error', str(err))
401
450
    'Branch.last_revision_info', 'bzrlib.smart.branch', 'SmartServerBranchRequestLastRevisionInfo')
402
451
request_handlers.register_lazy(
403
452
    'Branch.lock_write', 'bzrlib.smart.branch', 'SmartServerBranchRequestLockWrite')
404
 
request_handlers.register_lazy(
405
 
    'Branch.revision_history', 'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
406
 
request_handlers.register_lazy(
407
 
    'Branch.set_last_revision', 'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
 
453
request_handlers.register_lazy( 'Branch.revision_history',
 
454
    'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
 
455
request_handlers.register_lazy( 'Branch.set_config_option',
 
456
    'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOption')
 
457
request_handlers.register_lazy( 'Branch.set_last_revision',
 
458
    'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
408
459
request_handlers.register_lazy(
409
460
    'Branch.set_last_revision_info', 'bzrlib.smart.branch',
410
461
    'SmartServerBranchRequestSetLastRevisionInfo')
412
463
    'Branch.set_last_revision_ex', 'bzrlib.smart.branch',
413
464
    'SmartServerBranchRequestSetLastRevisionEx')
414
465
request_handlers.register_lazy(
 
466
    'Branch.set_parent_location', 'bzrlib.smart.branch',
 
467
    'SmartServerBranchRequestSetParentLocation')
 
468
request_handlers.register_lazy(
415
469
    'Branch.unlock', 'bzrlib.smart.branch', 'SmartServerBranchRequestUnlock')
416
470
request_handlers.register_lazy(
417
471
    'BzrDir.cloning_metadir', 'bzrlib.smart.bzrdir',
432
486
    'BzrDir.find_repositoryV3', 'bzrlib.smart.bzrdir',
433
487
    'SmartServerRequestFindRepositoryV3')
434
488
request_handlers.register_lazy(
 
489
    'BzrDir.get_config_file', 'bzrlib.smart.bzrdir',
 
490
    'SmartServerBzrDirRequestConfigFile')
 
491
request_handlers.register_lazy(
435
492
    'BzrDirFormat.initialize', 'bzrlib.smart.bzrdir',
436
493
    'SmartServerRequestInitializeBzrDir')
437
494
request_handlers.register_lazy(
 
495
    'BzrDirFormat.initialize_ex', 'bzrlib.smart.bzrdir',
 
496
    'SmartServerRequestBzrDirInitializeEx')
 
497
request_handlers.register_lazy(
 
498
    'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')
 
499
request_handlers.register_lazy(
438
500
    'BzrDir.open_branch', 'bzrlib.smart.bzrdir',
439
501
    'SmartServerRequestOpenBranch')
440
502
request_handlers.register_lazy(
482
544
request_handlers.register_lazy(
483
545
    'Repository.insert_stream', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStream')
484
546
request_handlers.register_lazy(
 
547
    'Repository.insert_stream_locked', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStreamLocked')
 
548
request_handlers.register_lazy(
485
549
    'Repository.is_shared', 'bzrlib.smart.repository', 'SmartServerRepositoryIsShared')
486
550
request_handlers.register_lazy(
487
551
    'Repository.lock_write', 'bzrlib.smart.repository', 'SmartServerRepositoryLockWrite')
502
566
    'stat', 'bzrlib.smart.vfs', 'StatRequest')
503
567
request_handlers.register_lazy(
504
568
    'Transport.is_readonly', 'bzrlib.smart.request', 'SmartServerIsReadonly')
505
 
request_handlers.register_lazy(
506
 
    'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')