21
from __future__ import absolute_import
22
from cStringIO import StringIO
29
import thread as _thread
35
from bzrlib.smart import message, request
36
from bzrlib.trace import log_exception_quietly, mutter
37
from bzrlib.bencode import bdecode_as_tuple, bencode
38
from ...sixish import (
42
from . import message, request
43
from ...sixish import text_type
44
from ...trace import log_exception_quietly, mutter
45
from ...bencode import bdecode_as_tuple, bencode
40
48
# Protocol version strings. These are sent as prefixes of bzr requests and
41
49
# responses to identify the protocol version being used. (There are no version
42
50
# one strings because that version doesn't send any).
43
REQUEST_VERSION_TWO = 'bzr request 2\n'
44
RESPONSE_VERSION_TWO = 'bzr response 2\n'
51
REQUEST_VERSION_TWO = b'bzr request 2\n'
52
RESPONSE_VERSION_TWO = b'bzr response 2\n'
46
MESSAGE_VERSION_THREE = 'bzr message 3 (bzr 1.6)\n'
54
MESSAGE_VERSION_THREE = b'bzr message 3 (bzr 1.6)\n'
47
55
RESPONSE_VERSION_THREE = REQUEST_VERSION_THREE = MESSAGE_VERSION_THREE
55
63
def _decode_tuple(req_line):
56
if req_line is None or req_line == '':
64
if req_line is None or req_line == b'':
58
if req_line[-1] != '\n':
66
if not req_line.endswith(b'\n'):
59
67
raise errors.SmartProtocolError("request %r not terminated" % req_line)
60
return tuple(req_line[:-1].split('\x01'))
68
return tuple(req_line[:-1].split(b'\x01'))
63
71
def _encode_tuple(args):
64
72
"""Encode the tuple args to a bytestream."""
65
joined = '\x01'.join(args) + '\n'
66
if type(joined) is unicode:
67
# XXX: We should fix things so this never happens! -AJB, 20100304
68
mutter('response args contain unicode, should be only bytes: %r',
70
joined = joined.encode('ascii')
74
if isinstance(arg, text_type):
76
return b'\x01'.join(args) + b'\n'
74
79
class Requester(object):
112
117
# support multiple chunks?
113
118
def _encode_bulk_data(self, body):
114
119
"""Encode body as a bulk data chunk."""
115
return ''.join(('%d\n' % len(body), body, 'done\n'))
120
return b''.join((b'%d\n' % len(body), body, b'done\n'))
117
122
def _serialise_offsets(self, offsets):
118
123
"""Serialise a readv offset list."""
120
125
for start, length in offsets:
121
txt.append('%d,%d' % (start, length))
122
return '\n'.join(txt)
126
txt.append(b'%d,%d' % (start, length))
127
return b'\n'.join(txt)
125
130
class SmartServerRequestProtocolOne(SmartProtocolBase):
126
131
"""Server-side encoding and decoding logic for smart version 1."""
128
133
def __init__(self, backing_transport, write_func, root_client_path='/',
130
135
self._backing_transport = backing_transport
131
136
self._root_client_path = root_client_path
132
137
self._jail_root = jail_root
133
self.unused_data = ''
138
self.unused_data = b''
134
139
self._finished = False
136
141
self._has_dispatched = False
137
142
self.request = None
138
143
self._body_decoder = None
139
144
self._write_func = write_func
141
def accept_bytes(self, bytes):
146
def accept_bytes(self, data):
142
147
"""Take bytes, and advance the internal state machine appropriately.
144
:param bytes: must be a byte string
149
:param data: must be a byte string
146
if not isinstance(bytes, str):
147
raise ValueError(bytes)
148
self.in_buffer += bytes
151
if not isinstance(data, bytes):
152
raise ValueError(data)
153
self.in_buffer += data
149
154
if not self._has_dispatched:
150
if '\n' not in self.in_buffer:
155
if b'\n' not in self.in_buffer:
151
156
# no command line yet
153
158
self._has_dispatched = True
155
first_line, self.in_buffer = self.in_buffer.split('\n', 1)
160
first_line, self.in_buffer = self.in_buffer.split(b'\n', 1)
157
162
req_args = _decode_tuple(first_line)
158
163
self.request = request.SmartServerRequestHandler(
159
164
self._backing_transport, commands=request.request_handlers,
163
168
if self.request.finished_reading:
164
169
# trivial request
165
170
self.unused_data = self.in_buffer
167
172
self._send_response(self.request.response)
168
173
except KeyboardInterrupt:
170
except errors.UnknownSmartMethod, err:
175
except errors.UnknownSmartMethod as err:
171
176
protocol_error = errors.SmartProtocolError(
172
"bad request %r" % (err.verb,))
177
"bad request '%s'" % (err.verb.decode('ascii'),))
173
178
failure = request.FailedSmartServerResponse(
174
('error', str(protocol_error)))
179
(b'error', str(protocol_error).encode('utf-8')))
175
180
self._send_response(failure)
177
except Exception, exception:
182
except Exception as exception:
178
183
# everything else: pass to client, flush, and quit
179
184
log_exception_quietly()
180
185
self._send_response(request.FailedSmartServerResponse(
181
('error', str(exception))))
186
(b'error', str(exception).encode('utf-8'))))
184
189
if self._has_dispatched:
218
223
self._write_success_or_failure_prefix(response)
219
224
self._write_func(_encode_tuple(args))
220
225
if body is not None:
221
if not isinstance(body, str):
226
if not isinstance(body, bytes):
222
227
raise ValueError(body)
223
bytes = self._encode_bulk_data(body)
224
self._write_func(bytes)
228
data = self._encode_bulk_data(body)
229
self._write_func(data)
226
231
def _write_protocol_version(self):
227
232
"""Write any prefixes this protocol requires.
258
263
def _write_success_or_failure_prefix(self, response):
259
264
"""Write the protocol specific success/failure prefix."""
260
265
if response.is_successful():
261
self._write_func('success\n')
266
self._write_func(b'success\n')
263
self._write_func('failed\n')
268
self._write_func(b'failed\n')
265
270
def _write_protocol_version(self):
266
271
r"""Write any prefixes this protocol requires.
278
283
self._write_success_or_failure_prefix(response)
279
284
self._write_func(_encode_tuple(response.args))
280
285
if response.body is not None:
281
if not isinstance(response.body, str):
282
raise AssertionError('body must be a str')
286
if not isinstance(response.body, bytes):
287
raise AssertionError('body must be bytes')
283
288
if not (response.body_stream is None):
284
289
raise AssertionError(
285
290
'body_stream and body cannot both be set')
286
bytes = self._encode_bulk_data(response.body)
287
self._write_func(bytes)
291
data = self._encode_bulk_data(response.body)
292
self._write_func(data)
288
293
elif response.body_stream is not None:
289
294
_send_stream(response.body_stream, self._write_func)
292
297
def _send_stream(stream, write_func):
293
write_func('chunked\n')
298
write_func(b'chunked\n')
294
299
_send_chunks(stream, write_func)
298
303
def _send_chunks(stream, write_func):
299
304
for chunk in stream:
300
if isinstance(chunk, str):
301
bytes = "%x\n%s" % (len(chunk), chunk)
305
if isinstance(chunk, bytes):
306
data = ("%x\n" % len(chunk)).encode('ascii') + chunk
303
308
elif isinstance(chunk, request.FailedSmartServerResponse):
305
310
_send_chunks(chunk.args, write_func)
339
344
self.finished_reading = False
340
345
self._in_buffer_list = []
341
346
self._in_buffer_len = 0
342
self.unused_data = ''
347
self.unused_data = b''
343
348
self.bytes_left = None
344
349
self._number_needed_bytes = None
346
351
def _get_in_buffer(self):
347
352
if len(self._in_buffer_list) == 1:
348
353
return self._in_buffer_list[0]
349
in_buffer = ''.join(self._in_buffer_list)
354
in_buffer = b''.join(self._in_buffer_list)
350
355
if len(in_buffer) != self._in_buffer_len:
351
356
raise AssertionError(
352
357
"Length of buffer did not match expected value: %s != %s"
365
370
# check if we can yield the bytes from just the first entry in our list
366
371
if len(self._in_buffer_list) == 0:
367
372
raise AssertionError('Callers must be sure we have buffered bytes'
368
' before calling _get_in_bytes')
373
' before calling _get_in_bytes')
369
374
if len(self._in_buffer_list[0]) > count:
370
375
return self._in_buffer_list[0][:count]
371
376
# We can't yield it from the first buffer, so collapse all buffers, and
376
381
def _set_in_buffer(self, new_buf):
377
382
if new_buf is not None:
383
if not isinstance(new_buf, bytes):
384
raise TypeError(new_buf)
378
385
self._in_buffer_list = [new_buf]
379
386
self._in_buffer_len = len(new_buf)
381
388
self._in_buffer_list = []
382
389
self._in_buffer_len = 0
384
def accept_bytes(self, bytes):
391
def accept_bytes(self, new_buf):
385
392
"""Decode as much of bytes as possible.
387
If 'bytes' contains too much data it will be appended to
394
If 'new_buf' contains too much data it will be appended to
388
395
self.unused_data.
390
397
finished_reading will be set when no more data is required. Further
391
398
data will be appended to self.unused_data.
400
if not isinstance(new_buf, bytes):
401
raise TypeError(new_buf)
393
402
# accept_bytes is allowed to change the state
394
403
self._number_needed_bytes = None
395
404
# lsprof puts a very large amount of time on this specific call for
396
405
# large readv arrays
397
self._in_buffer_list.append(bytes)
398
self._in_buffer_len += len(bytes)
406
self._in_buffer_list.append(new_buf)
407
self._in_buffer_len += len(new_buf)
400
409
# Run the function for the current state.
401
410
current_state = self.state_accept
490
499
def _state_accept_expecting_length(self):
491
500
prefix = self._extract_line()
493
502
self.error = True
494
503
self.error_in_progress = []
495
504
self._state_accept_expecting_length()
497
elif prefix == 'END':
506
elif prefix == b'END':
498
507
# We've read the end-of-body marker.
499
508
# Any further bytes are unused data, including the bytes left in
500
509
# the _in_buffer.
533
542
_StatefulDecoder.__init__(self)
534
543
self.state_accept = self._state_accept_expecting_length
535
544
self.state_read = self._state_read_no_data
537
self._trailer_buffer = ''
546
self._trailer_buffer = b''
539
548
def next_read_size(self):
540
549
if self.bytes_left is not None:
559
568
def _state_accept_expecting_length(self):
560
569
in_buf = self._get_in_buffer()
561
pos = in_buf.find('\n')
570
pos = in_buf.find(b'\n')
564
573
self.bytes_left = int(in_buf[:pos])
565
self._set_in_buffer(in_buf[pos+1:])
574
self._set_in_buffer(in_buf[pos + 1:])
566
575
self.state_accept = self._state_accept_reading_body
567
576
self.state_read = self._state_read_body_buffer
584
593
self._set_in_buffer(None)
585
594
# TODO: what if the trailer does not match "done\n"? Should this raise
586
595
# a ProtocolViolation exception?
587
if self._trailer_buffer.startswith('done\n'):
588
self.unused_data = self._trailer_buffer[len('done\n'):]
596
if self._trailer_buffer.startswith(b'done\n'):
597
self.unused_data = self._trailer_buffer[len(b'done\n'):]
589
598
self.state_accept = self._state_accept_reading_unused
590
599
self.finished_reading = True
639
648
if 'hpss' in debug.debug_flags:
640
649
mutter('hpss call w/body: %s (%r...)', repr(args)[1:-1], body[:20])
641
650
if getattr(self._request._medium, '_path', None) is not None:
642
mutter(' (to %s)', self._request._medium._path)
652
self._request._medium._path)
643
653
mutter(' %d bytes', len(body))
644
654
self._request_start_time = osutils.timer_func()
645
655
if 'hpssdetail' in debug.debug_flags:
654
664
"""Make a remote call with a readv array.
656
666
The body is encoded with one line per readv offset pair. The numbers in
657
each pair are separated by a comma, and no trailing \n is emitted.
667
each pair are separated by a comma, and no trailing \\n is emitted.
659
669
if 'hpss' in debug.debug_flags:
660
670
mutter('hpss call w/readv: %s', repr(args)[1:-1])
661
671
if getattr(self._request._medium, '_path', None) is not None:
662
mutter(' (to %s)', self._request._medium._path)
673
self._request._medium._path)
663
674
self._request_start_time = osutils.timer_func()
664
675
self._write_args(args)
665
676
readv_bytes = self._serialise_offsets(body)
749
760
:param verb: The verb used in that call.
750
761
:raises: UnexpectedSmartServerResponse
752
if (result_tuple == ('error', "Generic bzr smart protocol error: "
753
"bad request '%s'" % self._last_verb) or
754
result_tuple == ('error', "Generic bzr smart protocol error: "
755
"bad request u'%s'" % self._last_verb)):
763
if (result_tuple == (b'error', b"Generic bzr smart protocol error: "
764
b"bad request '" + self._last_verb + b"'")
765
or result_tuple == (b'error', b"Generic bzr smart protocol error: "
766
b"bad request u'%s'" % self._last_verb)):
756
767
# The response will have no body, so we've finished reading.
757
768
self._request.finished_reading()
758
769
raise errors.UnknownSmartMethod(self._last_verb)
770
781
while not _body_decoder.finished_reading:
771
782
bytes = self._request.read_bytes(_body_decoder.next_read_size())
773
784
# end of file encountered reading from server
774
785
raise errors.ConnectionReset(
775
786
"Connection lost while reading response body.")
776
787
_body_decoder.accept_bytes(bytes)
777
788
self._request.finished_reading()
778
self._body_buffer = StringIO(_body_decoder.read_pending_data())
789
self._body_buffer = BytesIO(_body_decoder.read_pending_data())
779
790
# XXX: TODO check the trailer result.
780
791
if 'hpss' in debug.debug_flags:
781
792
mutter(' %d body bytes read',
789
800
def query_version(self):
790
801
"""Return protocol version number of the server."""
792
803
resp = self.read_response_tuple()
793
if resp == ('ok', '1'):
804
if resp == (b'ok', b'1'):
795
elif resp == ('ok', '2'):
806
elif resp == (b'ok', b'2'):
798
809
raise errors.SmartProtocolError("bad response %r" % (resp,))
830
841
response_status = self._request.read_line()
831
842
result = SmartClientRequestProtocolOne._read_response_tuple(self)
832
843
self._response_is_unknown_method(result)
833
if response_status == 'success\n':
844
if response_status == b'success\n':
834
845
self.response_status = True
835
846
if not expect_body:
836
847
self._request.finished_reading()
838
elif response_status == 'failed\n':
849
elif response_status == b'failed\n':
839
850
self.response_status = False
840
851
self._request.finished_reading()
841
852
raise errors.ErrorFromSmartServer(result)
858
869
_body_decoder = ChunkedBodyDecoder()
859
870
while not _body_decoder.finished_reading:
860
871
bytes = self._request.read_bytes(_body_decoder.next_read_size())
862
873
# end of file encountered reading from server
863
874
raise errors.ConnectionReset(
864
875
"Connection lost while reading streamed body.")
865
876
_body_decoder.accept_bytes(bytes)
866
877
for body_bytes in iter(_body_decoder.read_next_chunk, None):
867
if 'hpss' in debug.debug_flags and type(body_bytes) is str:
878
if 'hpss' in debug.debug_flags and isinstance(body_bytes, str):
868
879
mutter(' %d byte chunk read',
877
888
backing_transport, commands=request.request_handlers,
878
889
root_client_path=root_client_path, jail_root=jail_root)
879
890
responder = ProtocolThreeResponder(write_func)
880
message_handler = message.ConventionalRequestHandler(request_handler, responder)
891
message_handler = message.ConventionalRequestHandler(
892
request_handler, responder)
881
893
return ProtocolThreeDecoder(message_handler)
907
919
_StatefulDecoder.accept_bytes(self, bytes)
908
920
except KeyboardInterrupt:
910
except errors.SmartMessageHandlerError, exception:
922
except errors.SmartMessageHandlerError as exception:
911
923
# We do *not* set self.decoding_failed here. The message handler
912
924
# has raised an error, but the decoder is still able to parse bytes
913
925
# and determine when this message ends.
917
929
# The state machine is ready to continue decoding, but the
918
930
# exception has interrupted the loop that runs the state machine.
919
931
# So we call accept_bytes again to restart it.
920
self.accept_bytes('')
921
except Exception, exception:
932
self.accept_bytes(b'')
933
except Exception as exception:
922
934
# The decoder itself has raised an exception. We cannot continue
924
936
self.decoding_failed = True
993
1005
def _state_accept_expecting_headers(self):
994
1006
decoded = self._extract_prefixed_bencoded_data()
995
if type(decoded) is not dict:
1007
if not isinstance(decoded, dict):
996
1008
raise errors.SmartProtocolError(
997
1009
'Header object %r is not a dict' % (decoded,))
998
1010
self.state_accept = self._state_accept_expecting_message_part
1004
1016
def _state_accept_expecting_message_part(self):
1005
1017
message_part_kind = self._extract_single_byte()
1006
if message_part_kind == 'o':
1018
if message_part_kind == b'o':
1007
1019
self.state_accept = self._state_accept_expecting_one_byte
1008
elif message_part_kind == 's':
1020
elif message_part_kind == b's':
1009
1021
self.state_accept = self._state_accept_expecting_structure
1010
elif message_part_kind == 'b':
1022
elif message_part_kind == b'b':
1011
1023
self.state_accept = self._state_accept_expecting_bytes
1012
elif message_part_kind == 'e':
1024
elif message_part_kind == b'e':
1015
1027
raise errors.SmartProtocolError(
1081
1093
self._real_write_func = write_func
1083
1095
def _write_func(self, bytes):
1084
# TODO: It is probably more appropriate to use sum(map(len, _buf))
1085
# for total number of bytes to write, rather than buffer based on
1086
# the number of write() calls
1087
1096
# TODO: Another possibility would be to turn this into an async model.
1088
1097
# Where we let another thread know that we have some bytes if
1089
1098
# they want it, but we don't actually block for it
1104
1113
"""Serialise a readv offset list."""
1106
1115
for start, length in offsets:
1107
txt.append('%d,%d' % (start, length))
1108
return '\n'.join(txt)
1116
txt.append(b'%d,%d' % (start, length))
1117
return b'\n'.join(txt)
1110
1119
def _write_protocol_version(self):
1111
1120
self._write_func(MESSAGE_VERSION_THREE)
1119
1128
self._write_prefixed_bencode(headers)
1121
1130
def _write_structure(self, args):
1122
self._write_func('s')
1131
self._write_func(b's')
1124
1133
for arg in args:
1125
if type(arg) is unicode:
1134
if isinstance(arg, text_type):
1126
1135
utf8_args.append(arg.encode('utf8'))
1128
1137
utf8_args.append(arg)
1129
1138
self._write_prefixed_bencode(utf8_args)
1131
1140
def _write_end(self):
1132
self._write_func('e')
1141
self._write_func(b'e')
1135
1144
def _write_prefixed_body(self, bytes):
1136
self._write_func('b')
1145
self._write_func(b'b')
1137
1146
self._write_func(struct.pack('!L', len(bytes)))
1138
1147
self._write_func(bytes)
1140
1149
def _write_chunked_body_start(self):
1141
self._write_func('oC')
1150
self._write_func(b'oC')
1143
1152
def _write_error_status(self):
1144
self._write_func('oE')
1153
self._write_func(b'oE')
1146
1155
def _write_success_status(self):
1147
self._write_func('oS')
1156
self._write_func(b'oS')
1150
1159
class ProtocolThreeResponder(_ProtocolThreeEncoder):
1152
1161
def __init__(self, write_func):
1153
1162
_ProtocolThreeEncoder.__init__(self, write_func)
1154
1163
self.response_sent = False
1155
self._headers = {'Software version': bzrlib.__version__}
1165
b'Software version': breezy.__version__.encode('utf-8')}
1156
1166
if 'hpss' in debug.debug_flags:
1157
self._thread_id = thread.get_ident()
1167
self._thread_id = _thread.get_ident()
1158
1168
self._response_start_time = None
1160
1170
def _trace(self, action, message, extra_bytes=None, include_time=False):
1180
1190
% (exception,))
1181
1191
if isinstance(exception, errors.UnknownSmartMethod):
1182
1192
failure = request.FailedSmartServerResponse(
1183
('UnknownMethod', exception.verb))
1193
(b'UnknownMethod', exception.verb))
1184
1194
self.send_response(failure)
1186
1196
if 'hpss' in debug.debug_flags:
1189
1199
self._write_protocol_version()
1190
1200
self._write_headers(self._headers)
1191
1201
self._write_error_status()
1192
self._write_structure(('error', str(exception)))
1202
self._write_structure(
1203
(b'error', str(exception).encode('utf-8', 'replace')))
1193
1204
self._write_end()
1195
1206
def send_response(self, response):
1331
1344
"""Make a remote call with a readv array.
1333
1346
The body is encoded with one line per readv offset pair. The numbers in
1334
each pair are separated by a comma, and no trailing \n is emitted.
1347
each pair are separated by a comma, and no trailing \\n is emitted.
1336
1349
if 'hpss' in debug.debug_flags:
1337
1350
mutter('hpss call w/readv: %s', repr(args)[1:-1])
1363
1377
# have finished sending the stream. We would notice at the end
1364
1378
# anyway, but if the medium can deliver it early then it's good
1365
1379
# to short-circuit the whole request...
1380
# Provoke any ConnectionReset failures before we start the body stream.
1382
self.body_stream_started = True
1366
1383
for exc_info, part in _iter_with_errors(stream):
1367
1384
if exc_info is not None:
1368
1385
# Iterating the stream failed. Cleanly abort the request.
1369
1386
self._write_error_status()
1370
1387
# Currently the client unconditionally sends ('error',) as the
1372
self._write_structure(('error',))
1389
self._write_structure((b'error',))
1373
1390
self._write_end()
1374
1391
self._medium_request.finished_writing()
1375
raise exc_info[0], exc_info[1], exc_info[2]
1377
1397
self._write_prefixed_body(part)
1379
1399
self._write_end()
1380
1400
self._medium_request.finished_writing()