1
# Copyright (C) 2006, 2007 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
"""Wire-level encoding and decoding of requests and responses for the smart
 
 
22
from cStringIO import StringIO
 
 
28
from bzrlib import debug
 
 
29
from bzrlib import errors
 
 
30
from bzrlib.smart import message, request
 
 
31
from bzrlib.trace import log_exception_quietly, mutter
 
 
32
from bzrlib.util.bencode import bdecode, bencode
 
 
35
# Protocol version strings.  These are sent as prefixes of bzr requests and
 
 
36
# responses to identify the protocol version being used. (There are no version
 
 
37
# one strings because that version doesn't send any).
 
 
38
REQUEST_VERSION_TWO = 'bzr request 2\n'
 
 
39
RESPONSE_VERSION_TWO = 'bzr response 2\n'
 
 
41
MESSAGE_VERSION_THREE = 'bzr message 3 (bzr 1.6)\n'
 
 
42
RESPONSE_VERSION_THREE = REQUEST_VERSION_THREE = MESSAGE_VERSION_THREE
 
 
45
def _recv_tuple(from_file):
 
 
46
    req_line = from_file.readline()
 
 
47
    return _decode_tuple(req_line)
 
 
50
def _decode_tuple(req_line):
 
 
51
    if req_line is None or req_line == '':
 
 
53
    if req_line[-1] != '\n':
 
 
54
        raise errors.SmartProtocolError("request %r not terminated" % req_line)
 
 
55
    return tuple(req_line[:-1].split('\x01'))
 
 
58
def _encode_tuple(args):
 
 
59
    """Encode the tuple args to a bytestream."""
 
 
60
    return '\x01'.join(args) + '\n'
 
 
63
class Requester(object):
 
 
64
    """Abstract base class for an object that can issue requests on a smart
 
 
68
    def call(self, *args):
 
 
69
        """Make a remote call.
 
 
71
        :param args: the arguments of this call.
 
 
73
        raise NotImplementedError(self.call)
 
 
75
    def call_with_body_bytes(self, args, body):
 
 
76
        """Make a remote call with a body.
 
 
78
        :param args: the arguments of this call.
 
 
80
        :param body: the body to send with the request.
 
 
82
        raise NotImplementedError(self.call_with_body_bytes)
 
 
84
    def call_with_body_readv_array(self, args, body):
 
 
85
        """Make a remote call with a readv array.
 
 
87
        :param args: the arguments of this call.
 
 
88
        :type body: iterable of (start, length) tuples.
 
 
89
        :param body: the readv ranges to send with this request.
 
 
91
        raise NotImplementedError(self.call_with_body_readv_array)
 
 
93
    def set_headers(self, headers):
 
 
94
        raise NotImplementedError(self.set_headers)
 
 
97
class SmartProtocolBase(object):
 
 
98
    """Methods common to client and server"""
 
 
100
    # TODO: this only actually accomodates a single block; possibly should
 
 
101
    # support multiple chunks?
 
 
102
    def _encode_bulk_data(self, body):
 
 
103
        """Encode body as a bulk data chunk."""
 
 
104
        return ''.join(('%d\n' % len(body), body, 'done\n'))
 
 
106
    def _serialise_offsets(self, offsets):
 
 
107
        """Serialise a readv offset list."""
 
 
109
        for start, length in offsets:
 
 
110
            txt.append('%d,%d' % (start, length))
 
 
111
        return '\n'.join(txt)
 
 
114
class SmartServerRequestProtocolOne(SmartProtocolBase):
 
 
115
    """Server-side encoding and decoding logic for smart version 1."""
 
 
117
    def __init__(self, backing_transport, write_func, root_client_path='/'):
 
 
118
        self._backing_transport = backing_transport
 
 
119
        self._root_client_path = root_client_path
 
 
120
        self.unused_data = ''
 
 
121
        self._finished = False
 
 
123
        self._has_dispatched = False
 
 
125
        self._body_decoder = None
 
 
126
        self._write_func = write_func
 
 
128
    def accept_bytes(self, bytes):
 
 
129
        """Take bytes, and advance the internal state machine appropriately.
 
 
131
        :param bytes: must be a byte string
 
 
133
        if not isinstance(bytes, str):
 
 
134
            raise ValueError(bytes)
 
 
135
        self.in_buffer += bytes
 
 
136
        if not self._has_dispatched:
 
 
137
            if '\n' not in self.in_buffer:
 
 
138
                # no command line yet
 
 
140
            self._has_dispatched = True
 
 
142
                first_line, self.in_buffer = self.in_buffer.split('\n', 1)
 
 
144
                req_args = _decode_tuple(first_line)
 
 
145
                self.request = request.SmartServerRequestHandler(
 
 
146
                    self._backing_transport, commands=request.request_handlers,
 
 
147
                    root_client_path=self._root_client_path)
 
 
148
                self.request.dispatch_command(req_args[0], req_args[1:])
 
 
149
                if self.request.finished_reading:
 
 
151
                    self.unused_data = self.in_buffer
 
 
153
                    self._send_response(self.request.response)
 
 
154
            except KeyboardInterrupt:
 
 
156
            except errors.UnknownSmartMethod, err:
 
 
157
                protocol_error = errors.SmartProtocolError(
 
 
158
                    "bad request %r" % (err.verb,))
 
 
159
                failure = request.FailedSmartServerResponse(
 
 
160
                    ('error', str(protocol_error)))
 
 
161
                self._send_response(failure)
 
 
163
            except Exception, exception:
 
 
164
                # everything else: pass to client, flush, and quit
 
 
165
                log_exception_quietly()
 
 
166
                self._send_response(request.FailedSmartServerResponse(
 
 
167
                    ('error', str(exception))))
 
 
170
        if self._has_dispatched:
 
 
172
                # nothing to do.XXX: this routine should be a single state 
 
 
174
                self.unused_data += self.in_buffer
 
 
177
            if self._body_decoder is None:
 
 
178
                self._body_decoder = LengthPrefixedBodyDecoder()
 
 
179
            self._body_decoder.accept_bytes(self.in_buffer)
 
 
180
            self.in_buffer = self._body_decoder.unused_data
 
 
181
            body_data = self._body_decoder.read_pending_data()
 
 
182
            self.request.accept_body(body_data)
 
 
183
            if self._body_decoder.finished_reading:
 
 
184
                self.request.end_of_body()
 
 
185
                if not self.request.finished_reading:
 
 
186
                    raise AssertionError("no more body, request not finished")
 
 
187
            if self.request.response is not None:
 
 
188
                self._send_response(self.request.response)
 
 
189
                self.unused_data = self.in_buffer
 
 
192
                if self.request.finished_reading:
 
 
193
                    raise AssertionError(
 
 
194
                        "no response and we have finished reading.")
 
 
196
    def _send_response(self, response):
 
 
197
        """Send a smart server response down the output stream."""
 
 
199
            raise AssertionError('response already sent')
 
 
202
        self._finished = True
 
 
203
        self._write_protocol_version()
 
 
204
        self._write_success_or_failure_prefix(response)
 
 
205
        self._write_func(_encode_tuple(args))
 
 
207
            if not isinstance(body, str):
 
 
208
                raise ValueError(body)
 
 
209
            bytes = self._encode_bulk_data(body)
 
 
210
            self._write_func(bytes)
 
 
212
    def _write_protocol_version(self):
 
 
213
        """Write any prefixes this protocol requires.
 
 
215
        Version one doesn't send protocol versions.
 
 
218
    def _write_success_or_failure_prefix(self, response):
 
 
219
        """Write the protocol specific success/failure prefix.
 
 
221
        For SmartServerRequestProtocolOne this is omitted but we
 
 
222
        call is_successful to ensure that the response is valid.
 
 
224
        response.is_successful()
 
 
226
    def next_read_size(self):
 
 
229
        if self._body_decoder is None:
 
 
232
            return self._body_decoder.next_read_size()
 
 
235
class SmartServerRequestProtocolTwo(SmartServerRequestProtocolOne):
 
 
236
    r"""Version two of the server side of the smart protocol.
 
 
238
    This prefixes responses with the value of RESPONSE_VERSION_TWO.
 
 
241
    response_marker = RESPONSE_VERSION_TWO
 
 
242
    request_marker = REQUEST_VERSION_TWO
 
 
244
    def _write_success_or_failure_prefix(self, response):
 
 
245
        """Write the protocol specific success/failure prefix."""
 
 
246
        if response.is_successful():
 
 
247
            self._write_func('success\n')
 
 
249
            self._write_func('failed\n')
 
 
251
    def _write_protocol_version(self):
 
 
252
        r"""Write any prefixes this protocol requires.
 
 
254
        Version two sends the value of RESPONSE_VERSION_TWO.
 
 
256
        self._write_func(self.response_marker)
 
 
258
    def _send_response(self, response):
 
 
259
        """Send a smart server response down the output stream."""
 
 
261
            raise AssertionError('response already sent')
 
 
262
        self._finished = True
 
 
263
        self._write_protocol_version()
 
 
264
        self._write_success_or_failure_prefix(response)
 
 
265
        self._write_func(_encode_tuple(response.args))
 
 
266
        if response.body is not None:
 
 
267
            if not isinstance(response.body, str):
 
 
268
                raise AssertionError('body must be a str')
 
 
269
            if not (response.body_stream is None):
 
 
270
                raise AssertionError(
 
 
271
                    'body_stream and body cannot both be set')
 
 
272
            bytes = self._encode_bulk_data(response.body)
 
 
273
            self._write_func(bytes)
 
 
274
        elif response.body_stream is not None:
 
 
275
            _send_stream(response.body_stream, self._write_func)
 
 
278
def _send_stream(stream, write_func):
 
 
279
    write_func('chunked\n')
 
 
280
    _send_chunks(stream, write_func)
 
 
284
def _send_chunks(stream, write_func):
 
 
286
        if isinstance(chunk, str):
 
 
287
            bytes = "%x\n%s" % (len(chunk), chunk)
 
 
289
        elif isinstance(chunk, request.FailedSmartServerResponse):
 
 
291
            _send_chunks(chunk.args, write_func)
 
 
294
            raise errors.BzrError(
 
 
295
                'Chunks must be str or FailedSmartServerResponse, got %r'
 
 
299
class _NeedMoreBytes(Exception):
 
 
300
    """Raise this inside a _StatefulDecoder to stop decoding until more bytes
 
 
304
    def __init__(self, count=None):
 
 
307
        :param count: the total number of bytes needed by the current state.
 
 
308
            May be None if the number of bytes needed is unknown.
 
 
313
class _StatefulDecoder(object):
 
 
314
    """Base class for writing state machines to decode byte streams.
 
 
316
    Subclasses should provide a self.state_accept attribute that accepts bytes
 
 
317
    and, if appropriate, updates self.state_accept to a different function.
 
 
318
    accept_bytes will call state_accept as often as necessary to make sure the
 
 
319
    state machine has progressed as far as possible before it returns.
 
 
321
    See ProtocolThreeDecoder for an example subclass.
 
 
325
        self.finished_reading = False
 
 
327
        self.unused_data = ''
 
 
328
        self.bytes_left = None
 
 
329
        self._number_needed_bytes = None
 
 
331
    def accept_bytes(self, bytes):
 
 
332
        """Decode as much of bytes as possible.
 
 
334
        If 'bytes' contains too much data it will be appended to
 
 
337
        finished_reading will be set when no more data is required.  Further
 
 
338
        data will be appended to self.unused_data.
 
 
340
        # accept_bytes is allowed to change the state
 
 
341
        current_state = self.state_accept
 
 
342
        self._number_needed_bytes = None
 
 
343
        self._in_buffer += bytes
 
 
345
            # Run the function for the current state.
 
 
347
            while current_state != self.state_accept:
 
 
348
                # The current state has changed.  Run the function for the new
 
 
349
                # current state, so that it can:
 
 
350
                #   - decode any unconsumed bytes left in a buffer, and
 
 
351
                #   - signal how many more bytes are expected (via raising
 
 
353
                current_state = self.state_accept
 
 
355
        except _NeedMoreBytes, e:
 
 
356
            self._number_needed_bytes = e.count
 
 
359
class ChunkedBodyDecoder(_StatefulDecoder):
 
 
360
    """Decoder for chunked body data.
 
 
362
    This is very similar the HTTP's chunked encoding.  See the description of
 
 
363
    streamed body data in `doc/developers/network-protocol.txt` for details.
 
 
367
        _StatefulDecoder.__init__(self)
 
 
368
        self.state_accept = self._state_accept_expecting_header
 
 
369
        self.chunk_in_progress = None
 
 
370
        self.chunks = collections.deque()
 
 
372
        self.error_in_progress = None
 
 
374
    def next_read_size(self):
 
 
375
        # Note: the shortest possible chunk is 2 bytes: '0\n', and the
 
 
376
        # end-of-body marker is 4 bytes: 'END\n'.
 
 
377
        if self.state_accept == self._state_accept_reading_chunk:
 
 
378
            # We're expecting more chunk content.  So we're expecting at least
 
 
379
            # the rest of this chunk plus an END chunk.
 
 
380
            return self.bytes_left + 4
 
 
381
        elif self.state_accept == self._state_accept_expecting_length:
 
 
382
            if self._in_buffer == '':
 
 
383
                # We're expecting a chunk length.  There's at least two bytes
 
 
384
                # left: a digit plus '\n'.
 
 
387
                # We're in the middle of reading a chunk length.  So there's at
 
 
388
                # least one byte left, the '\n' that terminates the length.
 
 
390
        elif self.state_accept == self._state_accept_reading_unused:
 
 
392
        elif self.state_accept == self._state_accept_expecting_header:
 
 
393
            return max(0, len('chunked\n') - len(self._in_buffer))
 
 
395
            raise AssertionError("Impossible state: %r" % (self.state_accept,))
 
 
397
    def read_next_chunk(self):
 
 
399
            return self.chunks.popleft()
 
 
403
    def _extract_line(self):
 
 
404
        pos = self._in_buffer.find('\n')
 
 
406
            # We haven't read a complete line yet, so request more bytes before
 
 
408
            raise _NeedMoreBytes(1)
 
 
409
        line = self._in_buffer[:pos]
 
 
410
        # Trim the prefix (including '\n' delimiter) from the _in_buffer.
 
 
411
        self._in_buffer = self._in_buffer[pos+1:]
 
 
415
        self.unused_data = self._in_buffer
 
 
417
        self.state_accept = self._state_accept_reading_unused
 
 
419
            error_args = tuple(self.error_in_progress)
 
 
420
            self.chunks.append(request.FailedSmartServerResponse(error_args))
 
 
421
            self.error_in_progress = None
 
 
422
        self.finished_reading = True
 
 
424
    def _state_accept_expecting_header(self):
 
 
425
        prefix = self._extract_line()
 
 
426
        if prefix == 'chunked':
 
 
427
            self.state_accept = self._state_accept_expecting_length
 
 
429
            raise errors.SmartProtocolError(
 
 
430
                'Bad chunked body header: "%s"' % (prefix,))
 
 
432
    def _state_accept_expecting_length(self):
 
 
433
        prefix = self._extract_line()
 
 
436
            self.error_in_progress = []
 
 
437
            self._state_accept_expecting_length()
 
 
439
        elif prefix == 'END':
 
 
440
            # We've read the end-of-body marker.
 
 
441
            # Any further bytes are unused data, including the bytes left in
 
 
446
            self.bytes_left = int(prefix, 16)
 
 
447
            self.chunk_in_progress = ''
 
 
448
            self.state_accept = self._state_accept_reading_chunk
 
 
450
    def _state_accept_reading_chunk(self):
 
 
451
        in_buffer_len = len(self._in_buffer)
 
 
452
        self.chunk_in_progress += self._in_buffer[:self.bytes_left]
 
 
453
        self._in_buffer = self._in_buffer[self.bytes_left:]
 
 
454
        self.bytes_left -= in_buffer_len
 
 
455
        if self.bytes_left <= 0:
 
 
456
            # Finished with chunk
 
 
457
            self.bytes_left = None
 
 
459
                self.error_in_progress.append(self.chunk_in_progress)
 
 
461
                self.chunks.append(self.chunk_in_progress)
 
 
462
            self.chunk_in_progress = None
 
 
463
            self.state_accept = self._state_accept_expecting_length
 
 
465
    def _state_accept_reading_unused(self):
 
 
466
        self.unused_data += self._in_buffer
 
 
470
class LengthPrefixedBodyDecoder(_StatefulDecoder):
 
 
471
    """Decodes the length-prefixed bulk data."""
 
 
474
        _StatefulDecoder.__init__(self)
 
 
475
        self.state_accept = self._state_accept_expecting_length
 
 
476
        self.state_read = self._state_read_no_data
 
 
478
        self._trailer_buffer = ''
 
 
480
    def next_read_size(self):
 
 
481
        if self.bytes_left is not None:
 
 
482
            # Ideally we want to read all the remainder of the body and the
 
 
484
            return self.bytes_left + 5
 
 
485
        elif self.state_accept == self._state_accept_reading_trailer:
 
 
486
            # Just the trailer left
 
 
487
            return 5 - len(self._trailer_buffer)
 
 
488
        elif self.state_accept == self._state_accept_expecting_length:
 
 
489
            # There's still at least 6 bytes left ('\n' to end the length, plus
 
 
493
            # Reading excess data.  Either way, 1 byte at a time is fine.
 
 
496
    def read_pending_data(self):
 
 
497
        """Return any pending data that has been decoded."""
 
 
498
        return self.state_read()
 
 
500
    def _state_accept_expecting_length(self):
 
 
501
        pos = self._in_buffer.find('\n')
 
 
504
        self.bytes_left = int(self._in_buffer[:pos])
 
 
505
        self._in_buffer = self._in_buffer[pos+1:]
 
 
506
        self.state_accept = self._state_accept_reading_body
 
 
507
        self.state_read = self._state_read_body_buffer
 
 
509
    def _state_accept_reading_body(self):
 
 
510
        self._body += self._in_buffer
 
 
511
        self.bytes_left -= len(self._in_buffer)
 
 
513
        if self.bytes_left <= 0:
 
 
515
            if self.bytes_left != 0:
 
 
516
                self._trailer_buffer = self._body[self.bytes_left:]
 
 
517
                self._body = self._body[:self.bytes_left]
 
 
518
            self.bytes_left = None
 
 
519
            self.state_accept = self._state_accept_reading_trailer
 
 
521
    def _state_accept_reading_trailer(self):
 
 
522
        self._trailer_buffer += self._in_buffer
 
 
524
        # TODO: what if the trailer does not match "done\n"?  Should this raise
 
 
525
        # a ProtocolViolation exception?
 
 
526
        if self._trailer_buffer.startswith('done\n'):
 
 
527
            self.unused_data = self._trailer_buffer[len('done\n'):]
 
 
528
            self.state_accept = self._state_accept_reading_unused
 
 
529
            self.finished_reading = True
 
 
531
    def _state_accept_reading_unused(self):
 
 
532
        self.unused_data += self._in_buffer
 
 
535
    def _state_read_no_data(self):
 
 
538
    def _state_read_body_buffer(self):
 
 
544
class SmartClientRequestProtocolOne(SmartProtocolBase, Requester,
 
 
545
                                    message.ResponseHandler):
 
 
546
    """The client-side protocol for smart version 1."""
 
 
548
    def __init__(self, request):
 
 
549
        """Construct a SmartClientRequestProtocolOne.
 
 
551
        :param request: A SmartClientMediumRequest to serialise onto and
 
 
554
        self._request = request
 
 
555
        self._body_buffer = None
 
 
556
        self._request_start_time = None
 
 
557
        self._last_verb = None
 
 
560
    def set_headers(self, headers):
 
 
561
        self._headers = dict(headers)
 
 
563
    def call(self, *args):
 
 
564
        if 'hpss' in debug.debug_flags:
 
 
565
            mutter('hpss call:   %s', repr(args)[1:-1])
 
 
566
            if getattr(self._request._medium, 'base', None) is not None:
 
 
567
                mutter('             (to %s)', self._request._medium.base)
 
 
568
            self._request_start_time = time.time()
 
 
569
        self._write_args(args)
 
 
570
        self._request.finished_writing()
 
 
571
        self._last_verb = args[0]
 
 
573
    def call_with_body_bytes(self, args, body):
 
 
574
        """Make a remote call of args with body bytes 'body'.
 
 
576
        After calling this, call read_response_tuple to find the result out.
 
 
578
        if 'hpss' in debug.debug_flags:
 
 
579
            mutter('hpss call w/body: %s (%r...)', repr(args)[1:-1], body[:20])
 
 
580
            if getattr(self._request._medium, '_path', None) is not None:
 
 
581
                mutter('                  (to %s)', self._request._medium._path)
 
 
582
            mutter('              %d bytes', len(body))
 
 
583
            self._request_start_time = time.time()
 
 
584
            if 'hpssdetail' in debug.debug_flags:
 
 
585
                mutter('hpss body content: %s', body)
 
 
586
        self._write_args(args)
 
 
587
        bytes = self._encode_bulk_data(body)
 
 
588
        self._request.accept_bytes(bytes)
 
 
589
        self._request.finished_writing()
 
 
590
        self._last_verb = args[0]
 
 
592
    def call_with_body_readv_array(self, args, body):
 
 
593
        """Make a remote call with a readv array.
 
 
595
        The body is encoded with one line per readv offset pair. The numbers in
 
 
596
        each pair are separated by a comma, and no trailing \n is emitted.
 
 
598
        if 'hpss' in debug.debug_flags:
 
 
599
            mutter('hpss call w/readv: %s', repr(args)[1:-1])
 
 
600
            if getattr(self._request._medium, '_path', None) is not None:
 
 
601
                mutter('                  (to %s)', self._request._medium._path)
 
 
602
            self._request_start_time = time.time()
 
 
603
        self._write_args(args)
 
 
604
        readv_bytes = self._serialise_offsets(body)
 
 
605
        bytes = self._encode_bulk_data(readv_bytes)
 
 
606
        self._request.accept_bytes(bytes)
 
 
607
        self._request.finished_writing()
 
 
608
        if 'hpss' in debug.debug_flags:
 
 
609
            mutter('              %d bytes in readv request', len(readv_bytes))
 
 
610
        self._last_verb = args[0]
 
 
612
    def cancel_read_body(self):
 
 
613
        """After expecting a body, a response code may indicate one otherwise.
 
 
615
        This method lets the domain client inform the protocol that no body
 
 
616
        will be transmitted. This is a terminal method: after calling it the
 
 
617
        protocol is not able to be used further.
 
 
619
        self._request.finished_reading()
 
 
621
    def _read_response_tuple(self):
 
 
622
        result = self._recv_tuple()
 
 
623
        if 'hpss' in debug.debug_flags:
 
 
624
            if self._request_start_time is not None:
 
 
625
                mutter('   result:   %6.3fs  %s',
 
 
626
                       time.time() - self._request_start_time,
 
 
628
                self._request_start_time = None
 
 
630
                mutter('   result:   %s', repr(result)[1:-1])
 
 
633
    def read_response_tuple(self, expect_body=False):
 
 
634
        """Read a response tuple from the wire.
 
 
636
        This should only be called once.
 
 
638
        result = self._read_response_tuple()
 
 
639
        self._response_is_unknown_method(result)
 
 
640
        self._raise_args_if_error(result)
 
 
642
            self._request.finished_reading()
 
 
645
    def _raise_args_if_error(self, result_tuple):
 
 
646
        # Later protocol versions have an explicit flag in the protocol to say
 
 
647
        # if an error response is "failed" or not.  In version 1 we don't have
 
 
648
        # that luxury.  So here is a complete list of errors that can be
 
 
649
        # returned in response to existing version 1 smart requests.  Responses
 
 
650
        # starting with these codes are always "failed" responses.
 
 
657
            'UnicodeEncodeError',
 
 
658
            'UnicodeDecodeError',
 
 
664
            'UnlockableTransport',
 
 
670
        if result_tuple[0] in v1_error_codes:
 
 
671
            self._request.finished_reading()
 
 
672
            raise errors.ErrorFromSmartServer(result_tuple)
 
 
674
    def _response_is_unknown_method(self, result_tuple):
 
 
675
        """Raise UnexpectedSmartServerResponse if the response is an 'unknonwn
 
 
676
        method' response to the request.
 
 
678
        :param response: The response from a smart client call_expecting_body
 
 
680
        :param verb: The verb used in that call.
 
 
681
        :raises: UnexpectedSmartServerResponse
 
 
683
        if (result_tuple == ('error', "Generic bzr smart protocol error: "
 
 
684
                "bad request '%s'" % self._last_verb) or
 
 
685
              result_tuple == ('error', "Generic bzr smart protocol error: "
 
 
686
                "bad request u'%s'" % self._last_verb)):
 
 
687
            # The response will have no body, so we've finished reading.
 
 
688
            self._request.finished_reading()
 
 
689
            raise errors.UnknownSmartMethod(self._last_verb)
 
 
691
    def read_body_bytes(self, count=-1):
 
 
692
        """Read bytes from the body, decoding into a byte stream.
 
 
694
        We read all bytes at once to ensure we've checked the trailer for 
 
 
695
        errors, and then feed the buffer back as read_body_bytes is called.
 
 
697
        if self._body_buffer is not None:
 
 
698
            return self._body_buffer.read(count)
 
 
699
        _body_decoder = LengthPrefixedBodyDecoder()
 
 
701
        while not _body_decoder.finished_reading:
 
 
702
            bytes = self._request.read_bytes(_body_decoder.next_read_size())
 
 
704
                # end of file encountered reading from server
 
 
705
                raise errors.ConnectionReset(
 
 
706
                    "Connection lost while reading response body.")
 
 
707
            _body_decoder.accept_bytes(bytes)
 
 
708
        self._request.finished_reading()
 
 
709
        self._body_buffer = StringIO(_body_decoder.read_pending_data())
 
 
710
        # XXX: TODO check the trailer result.
 
 
711
        if 'hpss' in debug.debug_flags:
 
 
712
            mutter('              %d body bytes read',
 
 
713
                   len(self._body_buffer.getvalue()))
 
 
714
        return self._body_buffer.read(count)
 
 
716
    def _recv_tuple(self):
 
 
717
        """Receive a tuple from the medium request."""
 
 
718
        return _decode_tuple(self._request.read_line())
 
 
720
    def query_version(self):
 
 
721
        """Return protocol version number of the server."""
 
 
723
        resp = self.read_response_tuple()
 
 
724
        if resp == ('ok', '1'):
 
 
726
        elif resp == ('ok', '2'):
 
 
729
            raise errors.SmartProtocolError("bad response %r" % (resp,))
 
 
731
    def _write_args(self, args):
 
 
732
        self._write_protocol_version()
 
 
733
        bytes = _encode_tuple(args)
 
 
734
        self._request.accept_bytes(bytes)
 
 
736
    def _write_protocol_version(self):
 
 
737
        """Write any prefixes this protocol requires.
 
 
739
        Version one doesn't send protocol versions.
 
 
743
class SmartClientRequestProtocolTwo(SmartClientRequestProtocolOne):
 
 
744
    """Version two of the client side of the smart protocol.
 
 
746
    This prefixes the request with the value of REQUEST_VERSION_TWO.
 
 
749
    response_marker = RESPONSE_VERSION_TWO
 
 
750
    request_marker = REQUEST_VERSION_TWO
 
 
752
    def read_response_tuple(self, expect_body=False):
 
 
753
        """Read a response tuple from the wire.
 
 
755
        This should only be called once.
 
 
757
        version = self._request.read_line()
 
 
758
        if version != self.response_marker:
 
 
759
            self._request.finished_reading()
 
 
760
            raise errors.UnexpectedProtocolVersionMarker(version)
 
 
761
        response_status = self._request.read_line()
 
 
762
        result = SmartClientRequestProtocolOne._read_response_tuple(self)
 
 
763
        self._response_is_unknown_method(result)
 
 
764
        if response_status == 'success\n':
 
 
765
            self.response_status = True
 
 
767
                self._request.finished_reading()
 
 
769
        elif response_status == 'failed\n':
 
 
770
            self.response_status = False
 
 
771
            self._request.finished_reading()
 
 
772
            raise errors.ErrorFromSmartServer(result)
 
 
774
            raise errors.SmartProtocolError(
 
 
775
                'bad protocol status %r' % response_status)
 
 
777
    def _write_protocol_version(self):
 
 
778
        """Write any prefixes this protocol requires.
 
 
780
        Version two sends the value of REQUEST_VERSION_TWO.
 
 
782
        self._request.accept_bytes(self.request_marker)
 
 
784
    def read_streamed_body(self):
 
 
785
        """Read bytes from the body, decoding into a byte stream.
 
 
787
        # Read no more than 64k at a time so that we don't risk error 10055 (no
 
 
788
        # buffer space available) on Windows.
 
 
789
        _body_decoder = ChunkedBodyDecoder()
 
 
790
        while not _body_decoder.finished_reading:
 
 
791
            bytes = self._request.read_bytes(_body_decoder.next_read_size())
 
 
793
                # end of file encountered reading from server
 
 
794
                raise errors.ConnectionReset(
 
 
795
                    "Connection lost while reading streamed body.")
 
 
796
            _body_decoder.accept_bytes(bytes)
 
 
797
            for body_bytes in iter(_body_decoder.read_next_chunk, None):
 
 
798
                if 'hpss' in debug.debug_flags and type(body_bytes) is str:
 
 
799
                    mutter('              %d byte chunk read',
 
 
802
        self._request.finished_reading()
 
 
805
def build_server_protocol_three(backing_transport, write_func,
 
 
807
    request_handler = request.SmartServerRequestHandler(
 
 
808
        backing_transport, commands=request.request_handlers,
 
 
809
        root_client_path=root_client_path)
 
 
810
    responder = ProtocolThreeResponder(write_func)
 
 
811
    message_handler = message.ConventionalRequestHandler(request_handler, responder)
 
 
812
    return ProtocolThreeDecoder(message_handler)
 
 
815
class ProtocolThreeDecoder(_StatefulDecoder):
 
 
817
    response_marker = RESPONSE_VERSION_THREE
 
 
818
    request_marker = REQUEST_VERSION_THREE
 
 
820
    def __init__(self, message_handler, expect_version_marker=False):
 
 
821
        _StatefulDecoder.__init__(self)
 
 
822
        self._has_dispatched = False
 
 
824
        if expect_version_marker:
 
 
825
            self.state_accept = self._state_accept_expecting_protocol_version
 
 
826
            # We're expecting at least the protocol version marker + some
 
 
828
            self._number_needed_bytes = len(MESSAGE_VERSION_THREE) + 4
 
 
830
            self.state_accept = self._state_accept_expecting_headers
 
 
831
            self._number_needed_bytes = 4
 
 
832
        self.decoding_failed = False
 
 
833
        self.request_handler = self.message_handler = message_handler
 
 
835
    def accept_bytes(self, bytes):
 
 
836
        self._number_needed_bytes = None
 
 
838
            _StatefulDecoder.accept_bytes(self, bytes)
 
 
839
        except KeyboardInterrupt:
 
 
841
        except errors.SmartMessageHandlerError, exception:
 
 
842
            # We do *not* set self.decoding_failed here.  The message handler
 
 
843
            # has raised an error, but the decoder is still able to parse bytes
 
 
844
            # and determine when this message ends.
 
 
845
            log_exception_quietly()
 
 
846
            self.message_handler.protocol_error(exception.exc_value)
 
 
847
            # The state machine is ready to continue decoding, but the
 
 
848
            # exception has interrupted the loop that runs the state machine.
 
 
849
            # So we call accept_bytes again to restart it.
 
 
850
            self.accept_bytes('')
 
 
851
        except Exception, exception:
 
 
852
            # The decoder itself has raised an exception.  We cannot continue
 
 
854
            self.decoding_failed = True
 
 
855
            if isinstance(exception, errors.UnexpectedProtocolVersionMarker):
 
 
856
                # This happens during normal operation when the client tries a
 
 
857
                # protocol version the server doesn't understand, so no need to
 
 
858
                # log a traceback every time.
 
 
859
                # Note that this can only happen when
 
 
860
                # expect_version_marker=True, which is only the case on the
 
 
864
                log_exception_quietly()
 
 
865
            self.message_handler.protocol_error(exception)
 
 
867
    def _extract_length_prefixed_bytes(self):
 
 
868
        if len(self._in_buffer) < 4:
 
 
869
            # A length prefix by itself is 4 bytes, and we don't even have that
 
 
871
            raise _NeedMoreBytes(4)
 
 
872
        (length,) = struct.unpack('!L', self._in_buffer[:4])
 
 
873
        end_of_bytes = 4 + length
 
 
874
        if len(self._in_buffer) < end_of_bytes:
 
 
875
            # We haven't yet read as many bytes as the length-prefix says there
 
 
877
            raise _NeedMoreBytes(end_of_bytes)
 
 
878
        # Extract the bytes from the buffer.
 
 
879
        bytes = self._in_buffer[4:end_of_bytes]
 
 
880
        self._in_buffer = self._in_buffer[end_of_bytes:]
 
 
883
    def _extract_prefixed_bencoded_data(self):
 
 
884
        prefixed_bytes = self._extract_length_prefixed_bytes()
 
 
886
            decoded = bdecode(prefixed_bytes)
 
 
888
            raise errors.SmartProtocolError(
 
 
889
                'Bytes %r not bencoded' % (prefixed_bytes,))
 
 
892
    def _extract_single_byte(self):
 
 
893
        if self._in_buffer == '':
 
 
894
            # The buffer is empty
 
 
895
            raise _NeedMoreBytes(1)
 
 
896
        one_byte = self._in_buffer[0]
 
 
897
        self._in_buffer = self._in_buffer[1:]
 
 
900
    def _state_accept_expecting_protocol_version(self):
 
 
901
        needed_bytes = len(MESSAGE_VERSION_THREE) - len(self._in_buffer)
 
 
903
            # We don't have enough bytes to check if the protocol version
 
 
904
            # marker is right.  But we can check if it is already wrong by
 
 
905
            # checking that the start of MESSAGE_VERSION_THREE matches what
 
 
907
            # [In fact, if the remote end isn't bzr we might never receive
 
 
908
            # len(MESSAGE_VERSION_THREE) bytes.  So if the bytes we have so far
 
 
909
            # are wrong then we should just raise immediately rather than
 
 
911
            if not MESSAGE_VERSION_THREE.startswith(self._in_buffer):
 
 
912
                # We have enough bytes to know the protocol version is wrong
 
 
913
                raise errors.UnexpectedProtocolVersionMarker(self._in_buffer)
 
 
914
            raise _NeedMoreBytes(len(MESSAGE_VERSION_THREE))
 
 
915
        if not self._in_buffer.startswith(MESSAGE_VERSION_THREE):
 
 
916
            raise errors.UnexpectedProtocolVersionMarker(self._in_buffer)
 
 
917
        self._in_buffer = self._in_buffer[len(MESSAGE_VERSION_THREE):]
 
 
918
        self.state_accept = self._state_accept_expecting_headers
 
 
920
    def _state_accept_expecting_headers(self):
 
 
921
        decoded = self._extract_prefixed_bencoded_data()
 
 
922
        if type(decoded) is not dict:
 
 
923
            raise errors.SmartProtocolError(
 
 
924
                'Header object %r is not a dict' % (decoded,))
 
 
925
        self.state_accept = self._state_accept_expecting_message_part
 
 
927
            self.message_handler.headers_received(decoded)
 
 
929
            raise errors.SmartMessageHandlerError(sys.exc_info())
 
 
931
    def _state_accept_expecting_message_part(self):
 
 
932
        message_part_kind = self._extract_single_byte()
 
 
933
        if message_part_kind == 'o':
 
 
934
            self.state_accept = self._state_accept_expecting_one_byte
 
 
935
        elif message_part_kind == 's':
 
 
936
            self.state_accept = self._state_accept_expecting_structure
 
 
937
        elif message_part_kind == 'b':
 
 
938
            self.state_accept = self._state_accept_expecting_bytes
 
 
939
        elif message_part_kind == 'e':
 
 
942
            raise errors.SmartProtocolError(
 
 
943
                'Bad message kind byte: %r' % (message_part_kind,))
 
 
945
    def _state_accept_expecting_one_byte(self):
 
 
946
        byte = self._extract_single_byte()
 
 
947
        self.state_accept = self._state_accept_expecting_message_part
 
 
949
            self.message_handler.byte_part_received(byte)
 
 
951
            raise errors.SmartMessageHandlerError(sys.exc_info())
 
 
953
    def _state_accept_expecting_bytes(self):
 
 
954
        # XXX: this should not buffer whole message part, but instead deliver
 
 
955
        # the bytes as they arrive.
 
 
956
        prefixed_bytes = self._extract_length_prefixed_bytes()
 
 
957
        self.state_accept = self._state_accept_expecting_message_part
 
 
959
            self.message_handler.bytes_part_received(prefixed_bytes)
 
 
961
            raise errors.SmartMessageHandlerError(sys.exc_info())
 
 
963
    def _state_accept_expecting_structure(self):
 
 
964
        structure = self._extract_prefixed_bencoded_data()
 
 
965
        self.state_accept = self._state_accept_expecting_message_part
 
 
967
            self.message_handler.structure_part_received(structure)
 
 
969
            raise errors.SmartMessageHandlerError(sys.exc_info())
 
 
972
        self.unused_data = self._in_buffer
 
 
974
        self.state_accept = self._state_accept_reading_unused
 
 
976
            self.message_handler.end_received()
 
 
978
            raise errors.SmartMessageHandlerError(sys.exc_info())
 
 
980
    def _state_accept_reading_unused(self):
 
 
981
        self.unused_data += self._in_buffer
 
 
984
    def next_read_size(self):
 
 
985
        if self.state_accept == self._state_accept_reading_unused:
 
 
987
        elif self.decoding_failed:
 
 
988
            # An exception occured while processing this message, probably from
 
 
989
            # self.message_handler.  We're not sure that this state machine is
 
 
990
            # in a consistent state, so just signal that we're done (i.e. give
 
 
994
            if self._number_needed_bytes is not None:
 
 
995
                return self._number_needed_bytes - len(self._in_buffer)
 
 
997
                raise AssertionError("don't know how many bytes are expected!")
 
 
1000
class _ProtocolThreeEncoder(object):
 
 
1002
    response_marker = request_marker = MESSAGE_VERSION_THREE
 
 
1004
    def __init__(self, write_func):
 
 
1006
        self._real_write_func = write_func
 
 
1008
    def _write_func(self, bytes):
 
 
1013
            self._real_write_func(self._buf)
 
 
1016
    def _serialise_offsets(self, offsets):
 
 
1017
        """Serialise a readv offset list."""
 
 
1019
        for start, length in offsets:
 
 
1020
            txt.append('%d,%d' % (start, length))
 
 
1021
        return '\n'.join(txt)
 
 
1023
    def _write_protocol_version(self):
 
 
1024
        self._write_func(MESSAGE_VERSION_THREE)
 
 
1026
    def _write_prefixed_bencode(self, structure):
 
 
1027
        bytes = bencode(structure)
 
 
1028
        self._write_func(struct.pack('!L', len(bytes)))
 
 
1029
        self._write_func(bytes)
 
 
1031
    def _write_headers(self, headers):
 
 
1032
        self._write_prefixed_bencode(headers)
 
 
1034
    def _write_structure(self, args):
 
 
1035
        self._write_func('s')
 
 
1038
            if type(arg) is unicode:
 
 
1039
                utf8_args.append(arg.encode('utf8'))
 
 
1041
                utf8_args.append(arg)
 
 
1042
        self._write_prefixed_bencode(utf8_args)
 
 
1044
    def _write_end(self):
 
 
1045
        self._write_func('e')
 
 
1048
    def _write_prefixed_body(self, bytes):
 
 
1049
        self._write_func('b')
 
 
1050
        self._write_func(struct.pack('!L', len(bytes)))
 
 
1051
        self._write_func(bytes)
 
 
1053
    def _write_error_status(self):
 
 
1054
        self._write_func('oE')
 
 
1056
    def _write_success_status(self):
 
 
1057
        self._write_func('oS')
 
 
1060
class ProtocolThreeResponder(_ProtocolThreeEncoder):
 
 
1062
    def __init__(self, write_func):
 
 
1063
        _ProtocolThreeEncoder.__init__(self, write_func)
 
 
1064
        self.response_sent = False
 
 
1065
        self._headers = {'Software version': bzrlib.__version__}
 
 
1067
    def send_error(self, exception):
 
 
1068
        if self.response_sent:
 
 
1069
            raise AssertionError(
 
 
1070
                "send_error(%s) called, but response already sent."
 
 
1072
        if isinstance(exception, errors.UnknownSmartMethod):
 
 
1073
            failure = request.FailedSmartServerResponse(
 
 
1074
                ('UnknownMethod', exception.verb))
 
 
1075
            self.send_response(failure)
 
 
1077
        self.response_sent = True
 
 
1078
        self._write_protocol_version()
 
 
1079
        self._write_headers(self._headers)
 
 
1080
        self._write_error_status()
 
 
1081
        self._write_structure(('error', str(exception)))
 
 
1084
    def send_response(self, response):
 
 
1085
        if self.response_sent:
 
 
1086
            raise AssertionError(
 
 
1087
                "send_response(%r) called, but response already sent."
 
 
1089
        self.response_sent = True
 
 
1090
        self._write_protocol_version()
 
 
1091
        self._write_headers(self._headers)
 
 
1092
        if response.is_successful():
 
 
1093
            self._write_success_status()
 
 
1095
            self._write_error_status()
 
 
1096
        self._write_structure(response.args)
 
 
1097
        if response.body is not None:
 
 
1098
            self._write_prefixed_body(response.body)
 
 
1099
        elif response.body_stream is not None:
 
 
1100
            for chunk in response.body_stream:
 
 
1101
                self._write_prefixed_body(chunk)
 
 
1106
class ProtocolThreeRequester(_ProtocolThreeEncoder, Requester):
 
 
1108
    def __init__(self, medium_request):
 
 
1109
        _ProtocolThreeEncoder.__init__(self, medium_request.accept_bytes)
 
 
1110
        self._medium_request = medium_request
 
 
1113
    def set_headers(self, headers):
 
 
1114
        self._headers = headers.copy()
 
 
1116
    def call(self, *args):
 
 
1117
        if 'hpss' in debug.debug_flags:
 
 
1118
            mutter('hpss call:   %s', repr(args)[1:-1])
 
 
1119
            base = getattr(self._medium_request._medium, 'base', None)
 
 
1120
            if base is not None:
 
 
1121
                mutter('             (to %s)', base)
 
 
1122
            self._request_start_time = time.time()
 
 
1123
        self._write_protocol_version()
 
 
1124
        self._write_headers(self._headers)
 
 
1125
        self._write_structure(args)
 
 
1127
        self._medium_request.finished_writing()
 
 
1129
    def call_with_body_bytes(self, args, body):
 
 
1130
        """Make a remote call of args with body bytes 'body'.
 
 
1132
        After calling this, call read_response_tuple to find the result out.
 
 
1134
        if 'hpss' in debug.debug_flags:
 
 
1135
            mutter('hpss call w/body: %s (%r...)', repr(args)[1:-1], body[:20])
 
 
1136
            path = getattr(self._medium_request._medium, '_path', None)
 
 
1137
            if path is not None:
 
 
1138
                mutter('                  (to %s)', path)
 
 
1139
            mutter('              %d bytes', len(body))
 
 
1140
            self._request_start_time = time.time()
 
 
1141
        self._write_protocol_version()
 
 
1142
        self._write_headers(self._headers)
 
 
1143
        self._write_structure(args)
 
 
1144
        self._write_prefixed_body(body)
 
 
1146
        self._medium_request.finished_writing()
 
 
1148
    def call_with_body_readv_array(self, args, body):
 
 
1149
        """Make a remote call with a readv array.
 
 
1151
        The body is encoded with one line per readv offset pair. The numbers in
 
 
1152
        each pair are separated by a comma, and no trailing \n is emitted.
 
 
1154
        if 'hpss' in debug.debug_flags:
 
 
1155
            mutter('hpss call w/readv: %s', repr(args)[1:-1])
 
 
1156
            path = getattr(self._medium_request._medium, '_path', None)
 
 
1157
            if path is not None:
 
 
1158
                mutter('                  (to %s)', path)
 
 
1159
            self._request_start_time = time.time()
 
 
1160
        self._write_protocol_version()
 
 
1161
        self._write_headers(self._headers)
 
 
1162
        self._write_structure(args)
 
 
1163
        readv_bytes = self._serialise_offsets(body)
 
 
1164
        if 'hpss' in debug.debug_flags:
 
 
1165
            mutter('              %d bytes in readv request', len(readv_bytes))
 
 
1166
        self._write_prefixed_body(readv_bytes)
 
 
1168
        self._medium_request.finished_writing()