1
# Copyright (C) 2006 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."""
22
from bzrlib import bzrdir, errors, registry, revision
23
from bzrlib.bundle.serializer import write_bundle
26
class SmartServerRequest(object):
27
"""Base class for request handlers.
30
def __init__(self, backing_transport):
31
self._backing_transport = backing_transport
34
"""Called with the arguments of the request.
36
This should return a SmartServerResponse if this command expects to
39
raise NotImplementedError(self.do)
41
def do_body(self, body_bytes):
42
"""Called if the client sends a body with the request.
44
Must return a SmartServerResponse.
46
# TODO: if a client erroneously sends a request that shouldn't have a
47
# body, what to do? Probably SmartServerRequestHandler should catch
48
# this NotImplementedError and translate it into a 'bad request' error
49
# to send to the client.
50
raise NotImplementedError(self.do_body)
53
class SmartServerResponse(object):
54
"""Response generated by SmartServerRequestHandler."""
56
def __init__(self, args, body=None):
61
class SmartServerRequestHandler(object):
62
"""Protocol logic for smart server.
64
This doesn't handle serialization at all, it just processes requests and
68
# IMPORTANT FOR IMPLEMENTORS: It is important that SmartServerRequestHandler
69
# not contain encoding or decoding logic to allow the wire protocol to vary
70
# from the object protocol: we will want to tweak the wire protocol separate
71
# from the object model, and ideally we will be able to do that without
72
# having a SmartServerRequestHandler subclass for each wire protocol, rather
73
# just a Protocol subclass.
75
# TODO: Better way of representing the body for commands that take it,
76
# and allow it to be streamed into the server.
78
def __init__(self, backing_transport, commands):
81
:param backing_transport: a Transport to handle requests for.
82
:param commands: a registry mapping command names to SmartServerRequest
83
subclasses. e.g. bzrlib.transport.smart.vfs.vfs_commands.
85
self._backing_transport = backing_transport
86
self._commands = commands
89
self.finished_reading = False
92
def accept_body(self, bytes):
93
"""Accept body data."""
95
# TODO: This should be overriden for each command that desired body data
96
# to handle the right format of that data, i.e. plain bytes, a bundle,
97
# etc. The deserialisation into that format should be done in the
100
# default fallback is to accumulate bytes.
101
self._body_bytes += bytes
103
def end_of_body(self):
104
"""No more body data will be received."""
105
self._run_handler_code(self._command.do_body, (self._body_bytes,), {})
106
# cannot read after this.
107
self.finished_reading = True
109
def dispatch_command(self, cmd, args):
110
"""Deprecated compatibility method.""" # XXX XXX
112
command = self._commands.get(cmd)
114
raise errors.SmartProtocolError("bad request %r" % (cmd,))
115
self._command = command(self._backing_transport)
116
self._run_handler_code(self._command.do, args, {})
118
def _run_handler_code(self, callable, args, kwargs):
119
"""Run some handler specific code 'callable'.
121
If a result is returned, it is considered to be the commands response,
122
and finished_reading is set true, and its assigned to self.response.
124
Any exceptions caught are translated and a response object created
127
result = self._call_converting_errors(callable, args, kwargs)
129
if result is not None:
130
self.response = result
131
self.finished_reading = True
133
def _call_converting_errors(self, callable, args, kwargs):
134
"""Call callable converting errors to Response objects."""
135
# XXX: most of this error conversion is VFS-related, and thus ought to
136
# be in SmartServerVFSRequestHandler somewhere.
138
return callable(*args, **kwargs)
139
except errors.NoSuchFile, e:
140
return SmartServerResponse(('NoSuchFile', e.path))
141
except errors.FileExists, e:
142
return SmartServerResponse(('FileExists', e.path))
143
except errors.DirectoryNotEmpty, e:
144
return SmartServerResponse(('DirectoryNotEmpty', e.path))
145
except errors.ShortReadvError, e:
146
return SmartServerResponse(('ShortReadvError',
147
e.path, str(e.offset), str(e.length), str(e.actual)))
148
except UnicodeError, e:
149
# If it is a DecodeError, than most likely we are starting
150
# with a plain string
151
str_or_unicode = e.object
152
if isinstance(str_or_unicode, unicode):
153
# XXX: UTF-8 might have \x01 (our seperator byte) in it. We
154
# should escape it somehow.
155
val = 'u:' + str_or_unicode.encode('utf-8')
157
val = 's:' + str_or_unicode.encode('base64')
158
# This handles UnicodeEncodeError or UnicodeDecodeError
159
return SmartServerResponse((e.__class__.__name__,
160
e.encoding, val, str(e.start), str(e.end), e.reason))
161
except errors.TransportNotPossible, e:
162
if e.msg == "readonly transport":
163
return SmartServerResponse(('ReadOnlyError', ))
168
class HelloRequest(SmartServerRequest):
169
"""Answer a version request with my version."""
172
return SmartServerResponse(('ok', '1'))
175
class GetBundleRequest(SmartServerRequest):
177
def do(self, path, revision_id):
178
# open transport relative to our base
179
t = self._backing_transport.clone(path)
180
control, extra_path = bzrdir.BzrDir.open_containing_from_transport(t)
181
repo = control.open_repository()
182
tmpf = tempfile.TemporaryFile()
183
base_revision = revision.NULL_REVISION
184
write_bundle(repo, revision_id, base_revision, tmpf)
186
return SmartServerResponse((), tmpf.read())
189
request_handlers = registry.Registry()
190
request_handlers.register_lazy(
191
'append', 'bzrlib.smart.vfs', 'AppendRequest')
192
request_handlers.register_lazy(
193
'delete', 'bzrlib.smart.vfs', 'DeleteRequest')
194
request_handlers.register_lazy(
195
'get', 'bzrlib.smart.vfs', 'GetRequest')
196
request_handlers.register_lazy(
197
'get_bundle', 'bzrlib.smart.request', 'GetBundleRequest')
198
request_handlers.register_lazy(
199
'has', 'bzrlib.smart.vfs', 'HasRequest')
200
request_handlers.register_lazy(
201
'hello', 'bzrlib.smart.request', 'HelloRequest')
202
request_handlers.register_lazy(
203
'iter_files_recursive', 'bzrlib.smart.vfs', 'IterFilesRecursive')
204
request_handlers.register_lazy(
205
'list_dir', 'bzrlib.smart.vfs', 'ListDirRequest')
206
request_handlers.register_lazy(
207
'mkdir', 'bzrlib.smart.vfs', 'MkdirCommand')
208
request_handlers.register_lazy(
209
'move', 'bzrlib.smart.vfs', 'MoveCommand')
210
request_handlers.register_lazy(
211
'put', 'bzrlib.smart.vfs', 'PutCommand')
212
request_handlers.register_lazy(
213
'put_non_atomic', 'bzrlib.smart.vfs', 'PutNonAtomicCommand')
214
request_handlers.register_lazy(
215
'readv', 'bzrlib.smart.vfs', 'ReadvCommand')
216
request_handlers.register_lazy(
217
'rename', 'bzrlib.smart.vfs', 'RenameCommand')
218
request_handlers.register_lazy(
219
'rmdir', 'bzrlib.smart.vfs', 'RmdirCommand')
220
request_handlers.register_lazy(
221
'stat', 'bzrlib.smart.vfs', 'StatCommand')