bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
2018.5.4
by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler. |
1 |
# Copyright (C) 2006 Canonical Ltd
|
2 |
#
|
|
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.
|
|
7 |
#
|
|
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.
|
|
12 |
#
|
|
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
|
|
16 |
||
|
2018.5.19
by Andrew Bennetts
Add docstrings to all the new modules, and a few other places. |
17 |
"""Basic server-side logic for dealing with requests."""
|
18 |
||
19 |
||
|
2018.5.6
by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects. |
20 |
import tempfile |
21 |
||
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
22 |
from bzrlib import bzrdir, errors, revision |
|
2018.5.6
by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects. |
23 |
from bzrlib.bundle.serializer import write_bundle |
24 |
||
|
2018.5.4
by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler. |
25 |
|
26 |
class SmartServerRequest(object): |
|
27 |
"""Base class for request handlers. |
|
28 |
"""
|
|
29 |
||
30 |
def __init__(self, backing_transport): |
|
31 |
self._backing_transport = backing_transport |
|
|
2018.5.5
by Andrew Bennetts
Pass body_bytes directly to SmartServerRequest.do_body |
32 |
|
|
2018.5.19
by Andrew Bennetts
Add docstrings to all the new modules, and a few other places. |
33 |
def do(self, *args): |
34 |
"""Called with the arguments of the request. |
|
35 |
|
|
36 |
This should return a SmartServerResponse if this command expects to
|
|
37 |
receive no body.
|
|
38 |
"""
|
|
|
2018.5.5
by Andrew Bennetts
Pass body_bytes directly to SmartServerRequest.do_body |
39 |
raise NotImplementedError(self.do) |
40 |
||
41 |
def do_body(self, body_bytes): |
|
|
2018.5.19
by Andrew Bennetts
Add docstrings to all the new modules, and a few other places. |
42 |
"""Called if the client sends a body with the request. |
43 |
|
|
44 |
Must return a SmartServerResponse.
|
|
45 |
"""
|
|
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.
|
|
|
2018.5.4
by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler. |
50 |
raise NotImplementedError(self.do_body) |
51 |
||
|
2018.5.6
by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects. |
52 |
|
|
2018.5.16
by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies. |
53 |
class SmartServerResponse(object): |
54 |
"""Response generated by SmartServerRequestHandler.""" |
|
55 |
||
56 |
def __init__(self, args, body=None): |
|
57 |
self.args = args |
|
58 |
self.body = body |
|
59 |
||
60 |
||
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
61 |
class SmartServerRequestHandler(object): |
62 |
"""Protocol logic for smart server. |
|
63 |
|
|
64 |
This doesn't handle serialization at all, it just processes requests and
|
|
65 |
creates responses.
|
|
66 |
"""
|
|
67 |
||
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.
|
|
74 |
||
75 |
# TODO: Better way of representing the body for commands that take it,
|
|
76 |
# and allow it to be streamed into the server.
|
|
77 |
||
|
2018.5.17
by Andrew Bennetts
Paramaterise the commands handled by SmartServerRequestHandler. |
78 |
def __init__(self, backing_transport, commands): |
79 |
"""Constructor. |
|
80 |
||
81 |
:param backing_transport: a Transport to handle requests for.
|
|
82 |
:param commands: a dict mapping command names to SmartServerRequest
|
|
83 |
subclasses. e.g. bzrlib.transport.smart.vfs.vfs_commands.
|
|
84 |
"""
|
|
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
85 |
self._backing_transport = backing_transport |
|
2018.5.17
by Andrew Bennetts
Paramaterise the commands handled by SmartServerRequestHandler. |
86 |
self._commands = commands |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
87 |
self._body_bytes = '' |
88 |
self.response = None |
|
89 |
self.finished_reading = False |
|
90 |
self._command = None |
|
91 |
||
92 |
def accept_body(self, bytes): |
|
93 |
"""Accept body data.""" |
|
94 |
||
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
|
|
98 |
# Protocol object.
|
|
99 |
||
100 |
# default fallback is to accumulate bytes.
|
|
101 |
self._body_bytes += bytes |
|
102 |
||
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 |
|
108 |
||
109 |
def dispatch_command(self, cmd, args): |
|
110 |
"""Deprecated compatibility method.""" # XXX XXX |
|
|
2018.5.17
by Andrew Bennetts
Paramaterise the commands handled by SmartServerRequestHandler. |
111 |
command = self._commands.get(cmd) |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
112 |
if command is None: |
113 |
raise errors.SmartProtocolError("bad request %r" % (cmd,)) |
|
114 |
self._command = command(self._backing_transport) |
|
115 |
self._run_handler_code(self._command.do, args, {}) |
|
116 |
||
117 |
def _run_handler_code(self, callable, args, kwargs): |
|
118 |
"""Run some handler specific code 'callable'. |
|
119 |
||
120 |
If a result is returned, it is considered to be the commands response,
|
|
121 |
and finished_reading is set true, and its assigned to self.response.
|
|
122 |
||
123 |
Any exceptions caught are translated and a response object created
|
|
124 |
from them.
|
|
125 |
"""
|
|
126 |
result = self._call_converting_errors(callable, args, kwargs) |
|
|
2018.5.19
by Andrew Bennetts
Add docstrings to all the new modules, and a few other places. |
127 |
|
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
128 |
if result is not None: |
129 |
self.response = result |
|
130 |
self.finished_reading = True |
|
131 |
||
132 |
def _call_converting_errors(self, callable, args, kwargs): |
|
133 |
"""Call callable converting errors to Response objects.""" |
|
134 |
# XXX: most of this error conversion is VFS-related, and thus ought to
|
|
135 |
# be in SmartServerVFSRequestHandler somewhere.
|
|
136 |
try: |
|
137 |
return callable(*args, **kwargs) |
|
138 |
except errors.NoSuchFile, e: |
|
|
2018.5.16
by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies. |
139 |
return SmartServerResponse(('NoSuchFile', e.path)) |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
140 |
except errors.FileExists, e: |
|
2018.5.16
by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies. |
141 |
return SmartServerResponse(('FileExists', e.path)) |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
142 |
except errors.DirectoryNotEmpty, e: |
|
2018.5.16
by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies. |
143 |
return SmartServerResponse(('DirectoryNotEmpty', e.path)) |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
144 |
except errors.ShortReadvError, e: |
|
2018.5.16
by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies. |
145 |
return SmartServerResponse(('ShortReadvError', |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
146 |
e.path, str(e.offset), str(e.length), str(e.actual))) |
147 |
except UnicodeError, e: |
|
148 |
# If it is a DecodeError, than most likely we are starting
|
|
149 |
# with a plain string
|
|
150 |
str_or_unicode = e.object |
|
151 |
if isinstance(str_or_unicode, unicode): |
|
152 |
# XXX: UTF-8 might have \x01 (our seperator byte) in it. We
|
|
153 |
# should escape it somehow.
|
|
154 |
val = 'u:' + str_or_unicode.encode('utf-8') |
|
155 |
else: |
|
156 |
val = 's:' + str_or_unicode.encode('base64') |
|
157 |
# This handles UnicodeEncodeError or UnicodeDecodeError
|
|
|
2018.5.16
by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies. |
158 |
return SmartServerResponse((e.__class__.__name__, |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
159 |
e.encoding, val, str(e.start), str(e.end), e.reason)) |
160 |
except errors.TransportNotPossible, e: |
|
161 |
if e.msg == "readonly transport": |
|
|
2018.5.16
by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies. |
162 |
return SmartServerResponse(('ReadOnlyError', )) |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
163 |
else: |
164 |
raise
|
|
165 |
||
166 |
||
|
2018.5.6
by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects. |
167 |
class HelloRequest(SmartServerRequest): |
168 |
"""Answer a version request with my version.""" |
|
169 |
||
170 |
method = 'hello' |
|
171 |
||
172 |
def do(self): |
|
|
2018.5.16
by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies. |
173 |
return SmartServerResponse(('ok', '1')) |
|
2018.5.6
by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects. |
174 |
|
175 |
||
176 |
class GetBundleRequest(SmartServerRequest): |
|
177 |
||
178 |
method = 'get_bundle' |
|
179 |
||
180 |
def do(self, path, revision_id): |
|
181 |
# open transport relative to our base
|
|
182 |
t = self._backing_transport.clone(path) |
|
183 |
control, extra_path = bzrdir.BzrDir.open_containing_from_transport(t) |
|
184 |
repo = control.open_repository() |
|
185 |
tmpf = tempfile.TemporaryFile() |
|
186 |
base_revision = revision.NULL_REVISION |
|
187 |
write_bundle(repo, revision_id, base_revision, tmpf) |
|
188 |
tmpf.seek(0) |
|
|
2018.5.16
by Andrew Bennetts
Move SmartServerResponse to smart/request.py, untangling more import dependencies. |
189 |
return SmartServerResponse((), tmpf.read()) |
|
2018.5.6
by Andrew Bennetts
Tidy ups, and turn do_hello and do_get_bundle into command objects. |
190 |
|
191 |
||
192 |
# This is extended by bzrlib/transport/smart/vfs.py
|
|
193 |
version_one_commands = { |
|
194 |
HelloRequest.method: HelloRequest, |
|
195 |
GetBundleRequest.method: GetBundleRequest, |
|
196 |
}
|
|
197 |
||
198 |