1
# Copyright (C) 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
20
from cStringIO import StringIO
26
from bzrlib.trace import mutter
29
class MessageHandler(object):
30
"""Base class for handling messages received via the smart protocol.
32
As parts of a message are received, the corresponding PART_received method
39
def headers_received(self, headers):
40
"""Called when message headers are received.
42
This default implementation just stores them in self.headers.
44
self.headers = headers
46
def byte_part_received(self, byte):
47
"""Called when a 'byte' part is received.
49
Note that a 'byte' part is a message part consisting of exactly one
52
raise NotImplementedError(self.byte_received)
54
def bytes_part_received(self, bytes):
55
"""Called when a 'bytes' part is received.
57
A 'bytes' message part can contain any number of bytes. It should not
58
be confused with a 'byte' part, which is always a single byte.
60
raise NotImplementedError(self.bytes_received)
62
def structure_part_received(self, structure):
63
"""Called when a 'structure' part is received.
65
:param structure: some structured data, which will be some combination
66
of list, dict, int, and str objects.
68
raise NotImplementedError(self.bytes_received)
70
def protocol_error(self, exception):
71
"""Called when there is a protocol decoding error.
73
The default implementation just re-raises the exception.
77
def end_received(self):
78
"""Called when the end of the message is received."""
83
class ConventionalRequestHandler(MessageHandler):
84
"""A message handler for "conventional" requests.
86
"Conventional" is used in the sense described in
87
doc/developers/network-protocol.txt: a simple message with arguments and an
91
* args: expecting args
92
* body: expecting body (terminated by receiving a post-body status)
93
* error: expecting post-body error
94
* end: expecting end of message
98
def __init__(self, request_handler, responder):
99
MessageHandler.__init__(self)
100
self.request_handler = request_handler
101
self.responder = responder
102
self.expecting = 'args'
103
self._should_finish_body = False
104
self._response_sent = False
106
def protocol_error(self, exception):
107
if self.responder.response_sent:
108
# We can only send one response to a request, no matter how many
109
# errors happen while processing it.
111
self.responder.send_error(exception)
113
def byte_part_received(self, byte):
114
if self.expecting == 'body':
116
# Success. Nothing more to come except the end of message.
117
self.expecting = 'end'
119
# Error. Expect an error structure.
120
self.expecting = 'error'
122
raise errors.SmartProtocolError(
123
'Non-success status byte in request body: %r' % (byte,))
125
raise errors.SmartProtocolError(
126
'Unexpected message part: byte(%r)' % (byte,))
128
def structure_part_received(self, structure):
129
if self.expecting == 'args':
130
self._args_received(structure)
131
elif self.expecting == 'error':
132
self._error_received(structure)
134
raise errors.SmartProtocolError(
135
'Unexpected message part: structure(%r)' % (structure,))
137
def _args_received(self, args):
138
self.expecting = 'body'
139
self.request_handler.args_received(args)
140
if self.request_handler.finished_reading:
141
self._response_sent = True
142
self.responder.send_response(self.request_handler.response)
143
self.expecting = 'end'
145
def _error_received(self, error_args):
146
self.expecting = 'end'
147
self.request_handler.post_body_error_received(error_args)
149
def bytes_part_received(self, bytes):
150
if self.expecting == 'body':
151
self._should_finish_body = True
152
self.request_handler.accept_body(bytes)
154
raise errors.SmartProtocolError(
155
'Unexpected message part: bytes(%r)' % (bytes,))
157
def end_received(self):
158
if self.expecting not in ['body', 'end']:
159
raise errors.SmartProtocolError(
160
'End of message received prematurely (while expecting %s)'
162
self.expecting = 'nothing'
163
self.request_handler.end_received()
164
if not self.request_handler.finished_reading:
165
raise errors.SmartProtocolError(
166
"Complete conventional request was received, but request "
167
"handler has not finished reading.")
168
if not self._response_sent:
169
self.responder.send_response(self.request_handler.response)
172
class ResponseHandler(object):
173
"""Abstract base class for an object that handles a smart response."""
175
def read_response_tuple(self, expect_body=False):
176
"""Reads and returns the response tuple for the current request.
178
:keyword expect_body: a boolean indicating if a body is expected in the
179
response. Some protocol versions needs this information to know
180
when a response is finished. If False, read_body_bytes should
181
*not* be called afterwards. Defaults to False.
182
:returns: tuple of response arguments.
184
raise NotImplementedError(self.read_response_tuple)
186
def read_body_bytes(self, count=-1):
187
"""Read and return some bytes from the body.
189
:param count: if specified, read up to this many bytes. By default,
190
reads the entire body.
191
:returns: str of bytes from the response body.
193
raise NotImplementedError(self.read_body_bytes)
195
def read_streamed_body(self):
196
"""Returns an iterable that reads and returns a series of body chunks.
198
raise NotImplementedError(self.read_streamed_body)
200
def cancel_read_body(self):
201
"""Stop expecting a body for this response.
203
If expect_body was passed to read_response_tuple, this cancels that
204
expectation (and thus finishes reading the response, allowing a new
205
request to be issued). This is useful if a response turns out to be an
206
error rather than a normal result with a body.
208
raise NotImplementedError(self.cancel_read_body)
211
class ConventionalResponseHandler(MessageHandler, ResponseHandler):
214
MessageHandler.__init__(self)
217
self._bytes_parts = collections.deque()
218
self._body_started = False
219
self._body_stream_status = None
221
self._body_error_args = None
222
self.finished_reading = False
224
def setProtoAndMediumRequest(self, protocol_decoder, medium_request):
225
self._protocol_decoder = protocol_decoder
226
self._medium_request = medium_request
228
def byte_part_received(self, byte):
229
if byte not in ['E', 'S']:
230
raise errors.SmartProtocolError(
231
'Unknown response status: %r' % (byte,))
232
if self._body_started:
233
if self._body_stream_status is not None:
234
raise errors.SmartProtocolError(
235
'Unexpected byte part received: %r' % (byte,))
236
self._body_stream_status = byte
238
if self.status is not None:
239
raise errors.SmartProtocolError(
240
'Unexpected byte part received: %r' % (byte,))
243
def bytes_part_received(self, bytes):
244
self._body_started = True
245
self._bytes_parts.append(bytes)
247
def structure_part_received(self, structure):
248
if type(structure) is not tuple:
249
raise errors.SmartProtocolError(
250
'Args structure is not a sequence: %r' % (structure,))
251
if not self._body_started:
252
if self.args is not None:
253
raise errors.SmartProtocolError(
254
'Unexpected structure received: %r (already got %r)'
255
% (structure, self.args))
256
self.args = structure
258
if self._body_stream_status != 'E':
259
raise errors.SmartProtocolError(
260
'Unexpected structure received after body: %r'
262
self._body_error_args = structure
264
def _wait_for_response_args(self):
265
while self.args is None and not self.finished_reading:
268
def _wait_for_response_end(self):
269
while not self.finished_reading:
272
def _read_more(self):
273
next_read_size = self._protocol_decoder.next_read_size()
274
if next_read_size == 0:
275
# a complete request has been read.
276
self.finished_reading = True
277
self._medium_request.finished_reading()
279
bytes = self._medium_request.read_bytes(next_read_size)
281
# end of file encountered reading from server
282
if 'hpss' in debug.debug_flags:
284
'decoder state: buf[:10]=%r, state_accept=%s',
285
self._protocol_decoder._get_in_buffer()[:10],
286
self._protocol_decoder.state_accept.__name__)
287
raise errors.ConnectionReset(
288
"Unexpected end of message. "
289
"Please check connectivity and permissions, and report a bug "
290
"if problems persist.")
291
self._protocol_decoder.accept_bytes(bytes)
293
def protocol_error(self, exception):
294
# Whatever the error is, we're done with this request.
295
self.finished_reading = True
296
self._medium_request.finished_reading()
299
def read_response_tuple(self, expect_body=False):
300
"""Read a response tuple from the wire."""
301
self._wait_for_response_args()
303
self._wait_for_response_end()
304
if 'hpss' in debug.debug_flags:
305
mutter(' result: %r', self.args)
306
if self.status == 'E':
307
self._wait_for_response_end()
308
_raise_smart_server_error(self.args)
309
return tuple(self.args)
311
def read_body_bytes(self, count=-1):
312
"""Read bytes from the body, decoding into a byte stream.
314
We read all bytes at once to ensure we've checked the trailer for
315
errors, and then feed the buffer back as read_body_bytes is called.
317
Like the builtin file.read in Python, a count of -1 (the default) means
318
read the entire body.
320
# TODO: we don't necessarily need to buffer the full request if count
321
# != -1. (2008/04/30, Andrew Bennetts)
322
if self._body is None:
323
self._wait_for_response_end()
324
body_bytes = ''.join(self._bytes_parts)
325
if 'hpss' in debug.debug_flags:
326
mutter(' %d body bytes read', len(body_bytes))
327
self._body = StringIO(body_bytes)
328
self._bytes_parts = None
329
return self._body.read(count)
331
def read_streamed_body(self):
332
while not self.finished_reading:
333
while self._bytes_parts:
334
bytes_part = self._bytes_parts.popleft()
335
if 'hpssdetail' in debug.debug_flags:
336
mutter(' %d byte part read', len(bytes_part))
339
if self._body_stream_status == 'E':
340
_raise_smart_server_error(self._body_error_args)
342
def cancel_read_body(self):
343
self._wait_for_response_end()
346
def _raise_smart_server_error(error_tuple):
347
"""Raise exception based on tuple received from smart server
349
Specific error translation is handled by bzrlib.remote._translate_error
351
if error_tuple[0] == 'UnknownMethod':
352
raise errors.UnknownSmartMethod(error_tuple[1])
353
raise errors.ErrorFromSmartServer(error_tuple)