1
# Copyright (C) 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
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
17
"""Basic server-side logic for dealing with requests."""
29
from bzrlib.lazy_import import lazy_import
30
lazy_import(globals(), """
31
from bzrlib.bundle import serializer
35
class SmartServerRequest(object):
36
"""Base class for request handlers.
38
To define a new request, subclass this class and override the `do` method
39
(and if appropriate, `do_body` as well). Request implementors should take
40
care to call `translate_client_path` and `transport_from_client_path` as
41
appropriate when dealing with paths received from the client.
44
def __init__(self, backing_transport, root_client_path='/'):
47
:param backing_transport: the base transport to be used when performing
49
:param root_client_path: the client path that maps to the root of
50
backing_transport. This is used to interpret relpaths received
51
from the client. Clients will not be able to refer to paths above
52
this root. If root_client_path is None, then no translation will
53
be performed on client paths. Default is '/'.
55
self._backing_transport = backing_transport
56
if root_client_path is not None:
57
if not root_client_path.startswith('/'):
58
root_client_path = '/' + root_client_path
59
if not root_client_path.endswith('/'):
60
root_client_path += '/'
61
self._root_client_path = root_client_path
63
def _check_enabled(self):
64
"""Raises DisabledMethod if this method is disabled."""
68
"""Mandatory extension point for SmartServerRequest subclasses.
70
Subclasses must implement this.
72
This should return a SmartServerResponse if this command expects to
75
raise NotImplementedError(self.do)
77
def execute(self, *args):
78
"""Public entry point to execute this request.
80
It will return a SmartServerResponse if the command does not expect a
83
:param *args: the arguments of the request.
88
def do_body(self, body_bytes):
89
"""Called if the client sends a body with the request.
91
The do() method is still called, and must have returned None.
93
Must return a SmartServerResponse.
95
# TODO: if a client erroneously sends a request that shouldn't have a
96
# body, what to do? Probably SmartServerRequestHandler should catch
97
# this NotImplementedError and translate it into a 'bad request' error
98
# to send to the client.
99
raise NotImplementedError(self.do_body)
101
def translate_client_path(self, client_path):
102
"""Translate a path received from a network client into a local
105
All paths received from the client *must* be translated.
107
:param client_path: the path from the client.
108
:returns: a relpath that may be used with self._backing_transport
109
(unlike the untranslated client_path, which must not be used with
110
the backing transport).
112
if self._root_client_path is None:
113
# no translation necessary!
115
if not client_path.startswith('/'):
116
client_path = '/' + client_path
117
if client_path.startswith(self._root_client_path):
118
path = client_path[len(self._root_client_path):]
119
relpath = urlutils.joinpath('/', path)
120
assert relpath.startswith('/')
123
raise errors.PathNotChild(client_path, self._root_client_path)
125
def transport_from_client_path(self, client_path):
126
"""Get a backing transport corresponding to the location referred to by
129
:seealso: translate_client_path
130
:returns: a transport cloned from self._backing_transport
132
relpath = self.translate_client_path(client_path)
133
return self._backing_transport.clone(relpath)
136
class SmartServerResponse(object):
137
"""A response to a client request.
139
This base class should not be used. Instead use
140
SuccessfulSmartServerResponse and FailedSmartServerResponse as appropriate.
143
def __init__(self, args, body=None, body_stream=None):
146
:param args: tuple of response arguments.
147
:param body: string of a response body.
148
:param body_stream: iterable of bytestrings to be streamed to the
152
if body is not None and body_stream is not None:
153
raise errors.BzrError(
154
"'body' and 'body_stream' are mutually exclusive.")
156
self.body_stream = body_stream
158
def __eq__(self, other):
161
return (other.args == self.args and
162
other.body == self.body and
163
other.body_stream is self.body_stream)
166
status = {True: 'OK', False: 'ERR'}[self.is_successful()]
167
return "<SmartServerResponse status=%s args=%r body=%r>" % (status,
168
self.args, self.body)
171
class FailedSmartServerResponse(SmartServerResponse):
172
"""A SmartServerResponse for a request which failed."""
174
def is_successful(self):
175
"""FailedSmartServerResponse are not successful."""
179
class SuccessfulSmartServerResponse(SmartServerResponse):
180
"""A SmartServerResponse for a successfully completed request."""
182
def is_successful(self):
183
"""SuccessfulSmartServerResponse are successful."""
187
class SmartServerRequestHandler(object):
188
"""Protocol logic for smart server.
190
This doesn't handle serialization at all, it just processes requests and
194
# IMPORTANT FOR IMPLEMENTORS: It is important that SmartServerRequestHandler
195
# not contain encoding or decoding logic to allow the wire protocol to vary
196
# from the object protocol: we will want to tweak the wire protocol separate
197
# from the object model, and ideally we will be able to do that without
198
# having a SmartServerRequestHandler subclass for each wire protocol, rather
199
# just a Protocol subclass.
201
# TODO: Better way of representing the body for commands that take it,
202
# and allow it to be streamed into the server.
204
def __init__(self, backing_transport, commands, root_client_path):
207
:param backing_transport: a Transport to handle requests for.
208
:param commands: a registry mapping command names to SmartServerRequest
209
subclasses. e.g. bzrlib.transport.smart.vfs.vfs_commands.
211
self._backing_transport = backing_transport
212
self._root_client_path = root_client_path
213
self._commands = commands
214
self._body_bytes = ''
216
self.finished_reading = False
219
def accept_body(self, bytes):
220
"""Accept body data."""
222
# TODO: This should be overriden for each command that desired body data
223
# to handle the right format of that data, i.e. plain bytes, a bundle,
224
# etc. The deserialisation into that format should be done in the
227
# default fallback is to accumulate bytes.
228
self._body_bytes += bytes
230
def end_of_body(self):
231
"""No more body data will be received."""
232
self._run_handler_code(self._command.do_body, (self._body_bytes,), {})
233
# cannot read after this.
234
self.finished_reading = True
236
def dispatch_command(self, cmd, args):
237
"""Deprecated compatibility method.""" # XXX XXX
239
command = self._commands.get(cmd)
241
raise errors.SmartProtocolError("bad request %r" % (cmd,))
242
self._command = command(self._backing_transport, self._root_client_path)
243
self._run_handler_code(self._command.execute, args, {})
245
def _run_handler_code(self, callable, args, kwargs):
246
"""Run some handler specific code 'callable'.
248
If a result is returned, it is considered to be the commands response,
249
and finished_reading is set true, and its assigned to self.response.
251
Any exceptions caught are translated and a response object created
254
result = self._call_converting_errors(callable, args, kwargs)
256
if result is not None:
257
self.response = result
258
self.finished_reading = True
260
def _call_converting_errors(self, callable, args, kwargs):
261
"""Call callable converting errors to Response objects."""
262
# XXX: most of this error conversion is VFS-related, and thus ought to
263
# be in SmartServerVFSRequestHandler somewhere.
265
return callable(*args, **kwargs)
266
except errors.NoSuchFile, e:
267
return FailedSmartServerResponse(('NoSuchFile', e.path))
268
except errors.FileExists, e:
269
return FailedSmartServerResponse(('FileExists', e.path))
270
except errors.DirectoryNotEmpty, e:
271
return FailedSmartServerResponse(('DirectoryNotEmpty', e.path))
272
except errors.ShortReadvError, e:
273
return FailedSmartServerResponse(('ShortReadvError',
274
e.path, str(e.offset), str(e.length), str(e.actual)))
275
except UnicodeError, e:
276
# If it is a DecodeError, than most likely we are starting
277
# with a plain string
278
str_or_unicode = e.object
279
if isinstance(str_or_unicode, unicode):
280
# XXX: UTF-8 might have \x01 (our seperator byte) in it. We
281
# should escape it somehow.
282
val = 'u:' + str_or_unicode.encode('utf-8')
284
val = 's:' + str_or_unicode.encode('base64')
285
# This handles UnicodeEncodeError or UnicodeDecodeError
286
return FailedSmartServerResponse((e.__class__.__name__,
287
e.encoding, val, str(e.start), str(e.end), e.reason))
288
except errors.TransportNotPossible, e:
289
if e.msg == "readonly transport":
290
return FailedSmartServerResponse(('ReadOnlyError', ))
295
class HelloRequest(SmartServerRequest):
296
"""Answer a version request with the highest protocol version this server
301
return SuccessfulSmartServerResponse(('ok', '2'))
304
class GetBundleRequest(SmartServerRequest):
305
"""Get a bundle of from the null revision to the specified revision."""
307
def do(self, path, revision_id):
308
# open transport relative to our base
309
t = self.transport_from_client_path(path)
310
control, extra_path = bzrdir.BzrDir.open_containing_from_transport(t)
311
repo = control.open_repository()
312
tmpf = tempfile.TemporaryFile()
313
base_revision = revision.NULL_REVISION
314
serializer.write_bundle(repo, revision_id, base_revision, tmpf)
316
return SuccessfulSmartServerResponse((), tmpf.read())
319
class SmartServerIsReadonly(SmartServerRequest):
320
# XXX: this request method belongs somewhere else.
323
if self._backing_transport.is_readonly():
327
return SuccessfulSmartServerResponse((answer,))
330
request_handlers = registry.Registry()
331
request_handlers.register_lazy(
332
'append', 'bzrlib.smart.vfs', 'AppendRequest')
333
request_handlers.register_lazy(
334
'Branch.get_config_file', 'bzrlib.smart.branch', 'SmartServerBranchGetConfigFile')
335
request_handlers.register_lazy(
336
'Branch.last_revision_info', 'bzrlib.smart.branch', 'SmartServerBranchRequestLastRevisionInfo')
337
request_handlers.register_lazy(
338
'Branch.lock_write', 'bzrlib.smart.branch', 'SmartServerBranchRequestLockWrite')
339
request_handlers.register_lazy(
340
'Branch.revision_history', 'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
341
request_handlers.register_lazy(
342
'Branch.set_last_revision', 'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
343
request_handlers.register_lazy(
344
'Branch.unlock', 'bzrlib.smart.branch', 'SmartServerBranchRequestUnlock')
345
request_handlers.register_lazy(
346
'BzrDir.find_repository', 'bzrlib.smart.bzrdir', 'SmartServerRequestFindRepositoryV1')
347
request_handlers.register_lazy(
348
'BzrDir.find_repositoryV2', 'bzrlib.smart.bzrdir', 'SmartServerRequestFindRepositoryV2')
349
request_handlers.register_lazy(
350
'BzrDirFormat.initialize', 'bzrlib.smart.bzrdir', 'SmartServerRequestInitializeBzrDir')
351
request_handlers.register_lazy(
352
'BzrDir.open_branch', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBranch')
353
request_handlers.register_lazy(
354
'delete', 'bzrlib.smart.vfs', 'DeleteRequest')
355
request_handlers.register_lazy(
356
'get', 'bzrlib.smart.vfs', 'GetRequest')
357
request_handlers.register_lazy(
358
'get_bundle', 'bzrlib.smart.request', 'GetBundleRequest')
359
request_handlers.register_lazy(
360
'has', 'bzrlib.smart.vfs', 'HasRequest')
361
request_handlers.register_lazy(
362
'hello', 'bzrlib.smart.request', 'HelloRequest')
363
request_handlers.register_lazy(
364
'iter_files_recursive', 'bzrlib.smart.vfs', 'IterFilesRecursiveRequest')
365
request_handlers.register_lazy(
366
'list_dir', 'bzrlib.smart.vfs', 'ListDirRequest')
367
request_handlers.register_lazy(
368
'mkdir', 'bzrlib.smart.vfs', 'MkdirRequest')
369
request_handlers.register_lazy(
370
'move', 'bzrlib.smart.vfs', 'MoveRequest')
371
request_handlers.register_lazy(
372
'put', 'bzrlib.smart.vfs', 'PutRequest')
373
request_handlers.register_lazy(
374
'put_non_atomic', 'bzrlib.smart.vfs', 'PutNonAtomicRequest')
375
request_handlers.register_lazy(
376
'readv', 'bzrlib.smart.vfs', 'ReadvRequest')
377
request_handlers.register_lazy(
378
'rename', 'bzrlib.smart.vfs', 'RenameRequest')
379
request_handlers.register_lazy('Repository.gather_stats',
380
'bzrlib.smart.repository',
381
'SmartServerRepositoryGatherStats')
382
request_handlers.register_lazy('Repository.get_parent_map',
383
'bzrlib.smart.repository',
384
'SmartServerRepositoryGetParentMap')
385
request_handlers.register_lazy(
386
'Repository.stream_knit_data_for_revisions',
387
'bzrlib.smart.repository',
388
'SmartServerRepositoryStreamKnitDataForRevisions')
389
request_handlers.register_lazy(
390
'Repository.stream_revisions_chunked',
391
'bzrlib.smart.repository',
392
'SmartServerRepositoryStreamRevisionsChunked')
393
request_handlers.register_lazy(
394
'Repository.get_revision_graph', 'bzrlib.smart.repository', 'SmartServerRepositoryGetRevisionGraph')
395
request_handlers.register_lazy(
396
'Repository.has_revision', 'bzrlib.smart.repository', 'SmartServerRequestHasRevision')
397
request_handlers.register_lazy(
398
'Repository.is_shared', 'bzrlib.smart.repository', 'SmartServerRepositoryIsShared')
399
request_handlers.register_lazy(
400
'Repository.lock_write', 'bzrlib.smart.repository', 'SmartServerRepositoryLockWrite')
401
request_handlers.register_lazy(
402
'Repository.unlock', 'bzrlib.smart.repository', 'SmartServerRepositoryUnlock')
403
request_handlers.register_lazy(
404
'Repository.tarball', 'bzrlib.smart.repository',
405
'SmartServerRepositoryTarball')
406
request_handlers.register_lazy(
407
'rmdir', 'bzrlib.smart.vfs', 'RmdirRequest')
408
request_handlers.register_lazy(
409
'stat', 'bzrlib.smart.vfs', 'StatRequest')
410
request_handlers.register_lazy(
411
'Transport.is_readonly', 'bzrlib.smart.request', 'SmartServerIsReadonly')
412
request_handlers.register_lazy(
413
'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')