14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from cStringIO import StringIO
17
from __future__ import absolute_import
20
from collections.abc import deque
21
except ImportError: # python < 3.7
22
from collections import deque
24
from bzrlib.trace import mutter
28
from ...sixish import (
31
from ...trace import mutter
27
34
class MessageHandler(object):
109
116
self.responder.send_error(exception)
111
118
def byte_part_received(self, byte):
119
if not isinstance(byte, bytes):
120
raise TypeError(byte)
112
121
if self.expecting == 'body':
114
123
# Success. Nothing more to come except the end of message.
115
124
self.expecting = 'end'
117
126
# Error. Expect an error structure.
118
127
self.expecting = 'error'
212
221
MessageHandler.__init__(self)
213
222
self.status = None
215
self._bytes_parts = collections.deque()
224
self._bytes_parts = deque()
216
225
self._body_started = False
217
226
self._body_stream_status = None
218
227
self._body = None
224
233
self._medium_request = medium_request
226
235
def byte_part_received(self, byte):
227
if byte not in ['E', 'S']:
236
if not isinstance(byte, bytes):
237
raise TypeError(byte)
238
if byte not in [b'E', b'S']:
228
239
raise errors.SmartProtocolError(
229
240
'Unknown response status: %r' % (byte,))
230
241
if self._body_started:
243
254
self._bytes_parts.append(bytes)
245
256
def structure_part_received(self, structure):
246
if type(structure) is not tuple:
257
if not isinstance(structure, tuple):
247
258
raise errors.SmartProtocolError(
248
259
'Args structure is not a sequence: %r' % (structure,))
249
260
if not self._body_started:
253
264
% (structure, self.args))
254
265
self.args = structure
256
if self._body_stream_status != 'E':
267
if self._body_stream_status != b'E':
257
268
raise errors.SmartProtocolError(
258
269
'Unexpected structure received after body: %r'
274
285
self.finished_reading = True
275
286
self._medium_request.finished_reading()
277
bytes = self._medium_request.read_bytes(next_read_size)
288
data = self._medium_request.read_bytes(next_read_size)
279
290
# end of file encountered reading from server
280
291
if 'hpss' in debug.debug_flags:
286
297
"Unexpected end of message. "
287
298
"Please check connectivity and permissions, and report a bug "
288
299
"if problems persist.")
289
self._protocol_decoder.accept_bytes(bytes)
300
self._protocol_decoder.accept_bytes(data)
291
302
def protocol_error(self, exception):
292
303
# Whatever the error is, we're done with this request.
301
312
self._wait_for_response_end()
302
313
if 'hpss' in debug.debug_flags:
303
314
mutter(' result: %r', self.args)
304
if self.status == 'E':
315
if self.status == b'E':
305
316
self._wait_for_response_end()
306
_translate_error(self.args)
317
_raise_smart_server_error(self.args)
307
318
return tuple(self.args)
309
320
def read_body_bytes(self, count=-1):
319
330
# != -1. (2008/04/30, Andrew Bennetts)
320
331
if self._body is None:
321
332
self._wait_for_response_end()
322
body_bytes = ''.join(self._bytes_parts)
333
body_bytes = b''.join(self._bytes_parts)
323
334
if 'hpss' in debug.debug_flags:
324
335
mutter(' %d body bytes read', len(body_bytes))
325
self._body = StringIO(body_bytes)
336
self._body = BytesIO(body_bytes)
326
337
self._bytes_parts = None
327
338
return self._body.read(count)
334
345
mutter(' %d byte part read', len(bytes_part))
336
347
self._read_more()
337
if self._body_stream_status == 'E':
338
_translate_error(self._body_error_args)
348
if self._body_stream_status == b'E':
349
_raise_smart_server_error(self._body_error_args)
340
351
def cancel_read_body(self):
341
352
self._wait_for_response_end()
344
def _translate_error(error_tuple):
345
# Many exceptions need some state from the requestor to be properly
346
# translated (e.g. they need a branch object). So this only translates a
347
# few errors, and the rest are turned into a generic ErrorFromSmartServer.
348
error_name = error_tuple[0]
349
error_args = error_tuple[1:]
350
if error_name == 'UnknownMethod':
351
raise errors.UnknownSmartMethod(error_args[0])
352
if error_name == 'LockContention':
353
raise errors.LockContention('(remote lock)')
354
elif error_name == 'LockFailed':
355
raise errors.LockFailed(*error_args[:2])
356
elif error_name == 'FileExists':
357
raise errors.FileExists(error_args[0])
358
elif error_name == 'NoSuchFile':
359
raise errors.NoSuchFile(error_args[0])
361
raise errors.ErrorFromSmartServer(error_tuple)
355
def _raise_smart_server_error(error_tuple):
356
"""Raise exception based on tuple received from smart server
358
Specific error translation is handled by breezy.bzr.remote._translate_error
360
if error_tuple[0] == b'UnknownMethod':
361
raise errors.UnknownSmartMethod(error_tuple[1])
362
raise errors.ErrorFromSmartServer(error_tuple)