~bzr-pqm/bzr/bzr.dev

4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2006-2010 Canonical Ltd
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
16
2018.5.19 by Andrew Bennetts
Add docstrings to all the new modules, and a few other places.
17
"""Wire-level encoding and decoding of requests and responses for the smart
18
client and server.
19
"""
20
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
21
import collections
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
22
from cStringIO import StringIO
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
23
import struct
3245.4.49 by Andrew Bennetts
Distinguish between errors in decoding a message into message parts from errors in handling decoded message parts, and use that to make sure that entire requests are read even when they result in exceptions.
24
import sys
4913.1.1 by John Arbash Meinel
Switch to using thread.get_ident() which is available on all python versions.
25
import thread
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
26
import threading
2664.4.1 by John Arbash Meinel
Add timing information for call/response groups for hpss
27
import time
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
28
3195.3.13 by Andrew Bennetts
Start writing call* methods for version 3 of the HPSS client protocol.
29
import bzrlib
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
30
from bzrlib import (
31
    debug,
32
    errors,
33
    osutils,
34
    )
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
35
from bzrlib.smart import message, request
2621.3.1 by Andrew Bennetts
Log errors from the smart server in the trace file, to make debugging test failures (and live failures!) easier.
36
from bzrlib.trace import log_exception_quietly, mutter
2694.5.4 by Jelmer Vernooij
Move bzrlib.util.bencode to bzrlib._bencode_py.
37
from bzrlib.bencode import bdecode_as_tuple, bencode
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
38
39
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
40
# Protocol version strings.  These are sent as prefixes of bzr requests and
41
# responses to identify the protocol version being used. (There are no version
42
# 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'
45
3245.4.60 by Andrew Bennetts
Update the protocol v3 version string to say 'bzr 1.6'.
46
MESSAGE_VERSION_THREE = 'bzr message 3 (bzr 1.6)\n'
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
47
RESPONSE_VERSION_THREE = REQUEST_VERSION_THREE = MESSAGE_VERSION_THREE
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
48
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
49
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
50
def _recv_tuple(from_file):
51
    req_line = from_file.readline()
52
    return _decode_tuple(req_line)
53
54
55
def _decode_tuple(req_line):
3376.2.11 by Martin Pool
Compare to None using is/is not not ==
56
    if req_line is None or req_line == '':
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
57
        return None
58
    if req_line[-1] != '\n':
59
        raise errors.SmartProtocolError("request %r not terminated" % req_line)
60
    return tuple(req_line[:-1].split('\x01'))
61
62
63
def _encode_tuple(args):
64
    """Encode the tuple args to a bytestream."""
5011.3.7 by Andrew Bennetts
Make max_read_size param of _read_bytes_from_socket optional, workaround unicode vs. bytes issue.
65
    joined = '\x01'.join(args) + '\n'
66
    if type(joined) is unicode:
5011.3.15 by Andrew Bennetts
Use YYYYMMDD in XXX comment, not DDMMYYYY.
67
        # XXX: We should fix things so this never happens!  -AJB, 20100304
5011.3.7 by Andrew Bennetts
Make max_read_size param of _read_bytes_from_socket optional, workaround unicode vs. bytes issue.
68
        mutter('response args contain unicode, should be only bytes: %r',
69
               joined)
70
        joined = joined.encode('ascii')
71
    return joined
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
72
73
3245.4.26 by Andrew Bennetts
Rename 'setProtoAndMedium' to more accurate 'setProtoAndMediumRequest', add ABCs for Requesters and ResponseHandlers.
74
class Requester(object):
75
    """Abstract base class for an object that can issue requests on a smart
76
    medium.
77
    """
78
79
    def call(self, *args):
80
        """Make a remote call.
81
82
        :param args: the arguments of this call.
83
        """
84
        raise NotImplementedError(self.call)
85
86
    def call_with_body_bytes(self, args, body):
87
        """Make a remote call with a body.
88
89
        :param args: the arguments of this call.
90
        :type body: str
91
        :param body: the body to send with the request.
92
        """
93
        raise NotImplementedError(self.call_with_body_bytes)
94
95
    def call_with_body_readv_array(self, args, body):
96
        """Make a remote call with a readv array.
97
98
        :param args: the arguments of this call.
99
        :type body: iterable of (start, length) tuples.
100
        :param body: the readv ranges to send with this request.
101
        """
102
        raise NotImplementedError(self.call_with_body_readv_array)
103
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
104
    def set_headers(self, headers):
105
        raise NotImplementedError(self.set_headers)
106
3245.4.26 by Andrew Bennetts
Rename 'setProtoAndMedium' to more accurate 'setProtoAndMediumRequest', add ABCs for Requesters and ResponseHandlers.
107
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
108
class SmartProtocolBase(object):
109
    """Methods common to client and server"""
110
111
    # TODO: this only actually accomodates a single block; possibly should
112
    # support multiple chunks?
113
    def _encode_bulk_data(self, body):
114
        """Encode body as a bulk data chunk."""
115
        return ''.join(('%d\n' % len(body), body, 'done\n'))
116
117
    def _serialise_offsets(self, offsets):
118
        """Serialise a readv offset list."""
119
        txt = []
120
        for start, length in offsets:
121
            txt.append('%d,%d' % (start, length))
122
        return '\n'.join(txt)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
123
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
124
125
class SmartServerRequestProtocolOne(SmartProtocolBase):
126
    """Server-side encoding and decoding logic for smart version 1."""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
127
4760.1.1 by Andrew Bennetts
Add optional jail_root argument to SmartServerRequest and friends, and use it in the WSGI glue. Allows opening branches in shared repos via bzr+http (assuming the repo should be accessible).
128
    def __init__(self, backing_transport, write_func, root_client_path='/',
129
            jail_root=None):
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
130
        self._backing_transport = backing_transport
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
131
        self._root_client_path = root_client_path
4760.1.1 by Andrew Bennetts
Add optional jail_root argument to SmartServerRequest and friends, and use it in the WSGI glue. Allows opening branches in shared repos via bzr+http (assuming the repo should be accessible).
132
        self._jail_root = jail_root
3245.4.21 by Andrew Bennetts
Remove 'excess_buffer' attribute and another crufty comment.
133
        self.unused_data = ''
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
134
        self._finished = False
135
        self.in_buffer = ''
3245.4.59 by Andrew Bennetts
Various tweaks in response to Martin's review.
136
        self._has_dispatched = False
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
137
        self.request = None
138
        self._body_decoder = None
2664.4.6 by John Arbash Meinel
Restore a line that shouldn't have been removed
139
        self._write_func = write_func
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
140
141
    def accept_bytes(self, bytes):
142
        """Take bytes, and advance the internal state machine appropriately.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
143
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
144
        :param bytes: must be a byte string
145
        """
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
146
        if not isinstance(bytes, str):
147
            raise ValueError(bytes)
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
148
        self.in_buffer += bytes
3245.4.59 by Andrew Bennetts
Various tweaks in response to Martin's review.
149
        if not self._has_dispatched:
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
150
            if '\n' not in self.in_buffer:
151
                # no command line yet
152
                return
3245.4.59 by Andrew Bennetts
Various tweaks in response to Martin's review.
153
            self._has_dispatched = True
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
154
            try:
155
                first_line, self.in_buffer = self.in_buffer.split('\n', 1)
156
                first_line += '\n'
157
                req_args = _decode_tuple(first_line)
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
158
                self.request = request.SmartServerRequestHandler(
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
159
                    self._backing_transport, commands=request.request_handlers,
4760.1.1 by Andrew Bennetts
Add optional jail_root argument to SmartServerRequest and friends, and use it in the WSGI glue. Allows opening branches in shared repos via bzr+http (assuming the repo should be accessible).
160
                    root_client_path=self._root_client_path,
161
                    jail_root=self._jail_root)
4634.6.30 by Andrew Bennetts
Remove SmartServerRequest.dispatch_command, fix SmartServerRequest.args_received.
162
                self.request.args_received(req_args)
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
163
                if self.request.finished_reading:
164
                    # trivial request
3245.4.21 by Andrew Bennetts
Remove 'excess_buffer' attribute and another crufty comment.
165
                    self.unused_data = self.in_buffer
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
166
                    self.in_buffer = ''
2432.4.3 by Robert Collins
Refactor the HPSS Response code to take SmartServerResponse rather than args and body.
167
                    self._send_response(self.request.response)
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
168
            except KeyboardInterrupt:
169
                raise
3245.4.29 by Andrew Bennetts
Add/tidy some comments, remove dud test_errors_are_logged test, add explicit UnknownSmartMethod to v3.
170
            except errors.UnknownSmartMethod, err:
171
                protocol_error = errors.SmartProtocolError(
172
                    "bad request %r" % (err.verb,))
173
                failure = request.FailedSmartServerResponse(
174
                    ('error', str(protocol_error)))
175
                self._send_response(failure)
176
                return
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
177
            except Exception, exception:
178
                # everything else: pass to client, flush, and quit
2621.3.1 by Andrew Bennetts
Log errors from the smart server in the trace file, to make debugging test failures (and live failures!) easier.
179
                log_exception_quietly()
2432.4.3 by Robert Collins
Refactor the HPSS Response code to take SmartServerResponse rather than args and body.
180
                self._send_response(request.FailedSmartServerResponse(
181
                    ('error', str(exception))))
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
182
                return
183
3245.4.59 by Andrew Bennetts
Various tweaks in response to Martin's review.
184
        if self._has_dispatched:
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
185
            if self._finished:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
186
                # nothing to do.XXX: this routine should be a single state
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
187
                # machine too.
3245.4.21 by Andrew Bennetts
Remove 'excess_buffer' attribute and another crufty comment.
188
                self.unused_data += self.in_buffer
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
189
                self.in_buffer = ''
190
                return
191
            if self._body_decoder is None:
192
                self._body_decoder = LengthPrefixedBodyDecoder()
193
            self._body_decoder.accept_bytes(self.in_buffer)
194
            self.in_buffer = self._body_decoder.unused_data
195
            body_data = self._body_decoder.read_pending_data()
196
            self.request.accept_body(body_data)
197
            if self._body_decoder.finished_reading:
198
                self.request.end_of_body()
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
199
                if not self.request.finished_reading:
200
                    raise AssertionError("no more body, request not finished")
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
201
            if self.request.response is not None:
2432.4.3 by Robert Collins
Refactor the HPSS Response code to take SmartServerResponse rather than args and body.
202
                self._send_response(self.request.response)
3245.4.21 by Andrew Bennetts
Remove 'excess_buffer' attribute and another crufty comment.
203
                self.unused_data = self.in_buffer
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
204
                self.in_buffer = ''
205
            else:
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
206
                if self.request.finished_reading:
207
                    raise AssertionError(
208
                        "no response and we have finished reading.")
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
209
2432.4.3 by Robert Collins
Refactor the HPSS Response code to take SmartServerResponse rather than args and body.
210
    def _send_response(self, response):
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
211
        """Send a smart server response down the output stream."""
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
212
        if self._finished:
213
            raise AssertionError('response already sent')
2432.4.3 by Robert Collins
Refactor the HPSS Response code to take SmartServerResponse rather than args and body.
214
        args = response.args
215
        body = response.body
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
216
        self._finished = True
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
217
        self._write_protocol_version()
2432.4.6 by Robert Collins
Include success/failure feedback in SmartProtocolTwo responses to allow robust handling in the future.
218
        self._write_success_or_failure_prefix(response)
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
219
        self._write_func(_encode_tuple(args))
220
        if body is not None:
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
221
            if not isinstance(body, str):
222
                raise ValueError(body)
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
223
            bytes = self._encode_bulk_data(body)
224
            self._write_func(bytes)
225
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
226
    def _write_protocol_version(self):
227
        """Write any prefixes this protocol requires.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
228
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
229
        Version one doesn't send protocol versions.
230
        """
231
2432.4.6 by Robert Collins
Include success/failure feedback in SmartProtocolTwo responses to allow robust handling in the future.
232
    def _write_success_or_failure_prefix(self, response):
233
        """Write the protocol specific success/failure prefix.
234
235
        For SmartServerRequestProtocolOne this is omitted but we
236
        call is_successful to ensure that the response is valid.
237
        """
238
        response.is_successful()
239
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
240
    def next_read_size(self):
241
        if self._finished:
242
            return 0
243
        if self._body_decoder is None:
244
            return 1
245
        else:
246
            return self._body_decoder.next_read_size()
247
248
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
249
class SmartServerRequestProtocolTwo(SmartServerRequestProtocolOne):
250
    r"""Version two of the server side of the smart protocol.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
251
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
252
    This prefixes responses with the value of RESPONSE_VERSION_TWO.
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
253
    """
254
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
255
    response_marker = RESPONSE_VERSION_TWO
256
    request_marker = REQUEST_VERSION_TWO
257
2432.4.6 by Robert Collins
Include success/failure feedback in SmartProtocolTwo responses to allow robust handling in the future.
258
    def _write_success_or_failure_prefix(self, response):
259
        """Write the protocol specific success/failure prefix."""
260
        if response.is_successful():
261
            self._write_func('success\n')
262
        else:
263
            self._write_func('failed\n')
264
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
265
    def _write_protocol_version(self):
266
        r"""Write any prefixes this protocol requires.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
267
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
268
        Version two sends the value of RESPONSE_VERSION_TWO.
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
269
        """
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
270
        self._write_func(self.response_marker)
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
271
2748.4.2 by Andrew Bennetts
Add protocol (version two) support for streaming bodies (using chunking) in responses.
272
    def _send_response(self, response):
273
        """Send a smart server response down the output stream."""
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
274
        if (self._finished):
275
            raise AssertionError('response already sent')
2748.4.2 by Andrew Bennetts
Add protocol (version two) support for streaming bodies (using chunking) in responses.
276
        self._finished = True
277
        self._write_protocol_version()
278
        self._write_success_or_failure_prefix(response)
279
        self._write_func(_encode_tuple(response.args))
280
        if response.body is not None:
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
281
            if not isinstance(response.body, str):
282
                raise AssertionError('body must be a str')
283
            if not (response.body_stream is None):
284
                raise AssertionError(
285
                    'body_stream and body cannot both be set')
2748.4.2 by Andrew Bennetts
Add protocol (version two) support for streaming bodies (using chunking) in responses.
286
            bytes = self._encode_bulk_data(response.body)
287
            self._write_func(bytes)
288
        elif response.body_stream is not None:
2748.4.10 by Andrew Bennetts
Fix chunking serialisation to be current with the latest changes to the protocol, and improve the tests to make it harder to have them desynchronised.
289
            _send_stream(response.body_stream, self._write_func)
290
291
292
def _send_stream(stream, write_func):
2748.4.16 by Andrew Bennetts
Tweaks suggested by review.
293
    write_func('chunked\n')
2748.4.10 by Andrew Bennetts
Fix chunking serialisation to be current with the latest changes to the protocol, and improve the tests to make it harder to have them desynchronised.
294
    _send_chunks(stream, write_func)
295
    write_func('END\n')
2748.4.4 by Andrew Bennetts
Extract a _send_chunks function to make testing easier.
296
297
298
def _send_chunks(stream, write_func):
299
    for chunk in stream:
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
300
        if isinstance(chunk, str):
301
            bytes = "%x\n%s" % (len(chunk), chunk)
302
            write_func(bytes)
303
        elif isinstance(chunk, request.FailedSmartServerResponse):
2748.4.10 by Andrew Bennetts
Fix chunking serialisation to be current with the latest changes to the protocol, and improve the tests to make it harder to have them desynchronised.
304
            write_func('ERR\n')
305
            _send_chunks(chunk.args, write_func)
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
306
            return
307
        else:
2535.4.19 by Andrew Bennetts
Fix some trivial NameErrors in error handling.
308
            raise errors.BzrError(
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
309
                'Chunks must be str or FailedSmartServerResponse, got %r'
2535.4.19 by Andrew Bennetts
Fix some trivial NameErrors in error handling.
310
                % chunk)
2748.4.2 by Andrew Bennetts
Add protocol (version two) support for streaming bodies (using chunking) in responses.
311
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
312
3195.3.7 by Andrew Bennetts
Make the '_extract_* helpers more robust by adding a _NeedMoreBytes exception to _StatefulDecoder.
313
class _NeedMoreBytes(Exception):
314
    """Raise this inside a _StatefulDecoder to stop decoding until more bytes
315
    have been received.
316
    """
317
3195.3.18 by Andrew Bennetts
call_with_body_bytes now works with v3 (e.g. test_copy_content_remote_to_local passes). Lots of debugging cruft, though.
318
    def __init__(self, count=None):
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
319
        """Constructor.
320
321
        :param count: the total number of bytes needed by the current state.
322
            May be None if the number of bytes needed is unknown.
323
        """
3195.3.18 by Andrew Bennetts
call_with_body_bytes now works with v3 (e.g. test_copy_content_remote_to_local passes). Lots of debugging cruft, though.
324
        self.count = count
325
3195.3.7 by Andrew Bennetts
Make the '_extract_* helpers more robust by adding a _NeedMoreBytes exception to _StatefulDecoder.
326
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
327
class _StatefulDecoder(object):
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
328
    """Base class for writing state machines to decode byte streams.
329
330
    Subclasses should provide a self.state_accept attribute that accepts bytes
331
    and, if appropriate, updates self.state_accept to a different function.
332
    accept_bytes will call state_accept as often as necessary to make sure the
333
    state machine has progressed as far as possible before it returns.
334
335
    See ProtocolThreeDecoder for an example subclass.
336
    """
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
337
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
338
    def __init__(self):
339
        self.finished_reading = False
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
340
        self._in_buffer_list = []
341
        self._in_buffer_len = 0
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
342
        self.unused_data = ''
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
343
        self.bytes_left = None
3195.3.22 by Andrew Bennetts
Fix more tests.
344
        self._number_needed_bytes = None
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
345
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
346
    def _get_in_buffer(self):
347
        if len(self._in_buffer_list) == 1:
348
            return self._in_buffer_list[0]
349
        in_buffer = ''.join(self._in_buffer_list)
3649.5.5 by John Arbash Meinel
Fix the test suite.
350
        if len(in_buffer) != self._in_buffer_len:
351
            raise AssertionError(
352
                "Length of buffer did not match expected value: %s != %s"
353
                % self._in_buffer_len, len(in_buffer))
3649.5.2 by John Arbash Meinel
When we are waiting on a big stream, allow
354
        self._in_buffer_list = [in_buffer]
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
355
        return in_buffer
356
3649.5.2 by John Arbash Meinel
When we are waiting on a big stream, allow
357
    def _get_in_bytes(self, count):
358
        """Grab X bytes from the input_buffer.
359
360
        Callers should have already checked that self._in_buffer_len is >
361
        count. Note, this does not consume the bytes from the buffer. The
362
        caller will still need to call _get_in_buffer() and then
363
        _set_in_buffer() if they actually need to consume the bytes.
364
        """
365
        # check if we can yield the bytes from just the first entry in our list
3649.5.5 by John Arbash Meinel
Fix the test suite.
366
        if len(self._in_buffer_list) == 0:
367
            raise AssertionError('Callers must be sure we have buffered bytes'
368
                ' before calling _get_in_bytes')
3649.5.2 by John Arbash Meinel
When we are waiting on a big stream, allow
369
        if len(self._in_buffer_list[0]) > count:
370
            return self._in_buffer_list[0][:count]
371
        # We can't yield it from the first buffer, so collapse all buffers, and
372
        # yield it from that
373
        in_buf = self._get_in_buffer()
374
        return in_buf[:count]
375
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
376
    def _set_in_buffer(self, new_buf):
377
        if new_buf is not None:
378
            self._in_buffer_list = [new_buf]
379
            self._in_buffer_len = len(new_buf)
380
        else:
381
            self._in_buffer_list = []
382
            self._in_buffer_len = 0
383
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
384
    def accept_bytes(self, bytes):
385
        """Decode as much of bytes as possible.
386
387
        If 'bytes' contains too much data it will be appended to
388
        self.unused_data.
389
390
        finished_reading will be set when no more data is required.  Further
391
        data will be appended to self.unused_data.
392
        """
393
        # accept_bytes is allowed to change the state
3195.3.22 by Andrew Bennetts
Fix more tests.
394
        self._number_needed_bytes = None
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
395
        # lsprof puts a very large amount of time on this specific call for
396
        # large readv arrays
397
        self._in_buffer_list.append(bytes)
398
        self._in_buffer_len += len(bytes)
3195.3.7 by Andrew Bennetts
Make the '_extract_* helpers more robust by adding a _NeedMoreBytes exception to _StatefulDecoder.
399
        try:
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
400
            # Run the function for the current state.
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
401
            current_state = self.state_accept
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
402
            self.state_accept()
3195.3.7 by Andrew Bennetts
Make the '_extract_* helpers more robust by adding a _NeedMoreBytes exception to _StatefulDecoder.
403
            while current_state != self.state_accept:
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
404
                # The current state has changed.  Run the function for the new
405
                # current state, so that it can:
406
                #   - decode any unconsumed bytes left in a buffer, and
407
                #   - signal how many more bytes are expected (via raising
408
                #     _NeedMoreBytes).
3195.3.7 by Andrew Bennetts
Make the '_extract_* helpers more robust by adding a _NeedMoreBytes exception to _StatefulDecoder.
409
                current_state = self.state_accept
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
410
                self.state_accept()
3195.3.22 by Andrew Bennetts
Fix more tests.
411
        except _NeedMoreBytes, e:
412
            self._number_needed_bytes = e.count
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
413
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
414
415
class ChunkedBodyDecoder(_StatefulDecoder):
416
    """Decoder for chunked body data.
417
2748.4.9 by Andrew Bennetts
Merge from hpss-protocol-docs.
418
    This is very similar the HTTP's chunked encoding.  See the description of
419
    streamed body data in `doc/developers/network-protocol.txt` for details.
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
420
    """
421
422
    def __init__(self):
423
        _StatefulDecoder.__init__(self)
2748.4.16 by Andrew Bennetts
Tweaks suggested by review.
424
        self.state_accept = self._state_accept_expecting_header
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
425
        self.chunk_in_progress = None
426
        self.chunks = collections.deque()
2748.4.6 by Andrew Bennetts
Use chunks for stream errors, rather than the response tuple format.
427
        self.error = False
428
        self.error_in_progress = None
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
429
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
430
    def next_read_size(self):
2748.4.7 by Andrew Bennetts
Change the end-of-body marker to something clearer than a zero-length chunk.
431
        # Note: the shortest possible chunk is 2 bytes: '0\n', and the
432
        # end-of-body marker is 4 bytes: 'END\n'.
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
433
        if self.state_accept == self._state_accept_reading_chunk:
434
            # We're expecting more chunk content.  So we're expecting at least
2748.4.7 by Andrew Bennetts
Change the end-of-body marker to something clearer than a zero-length chunk.
435
            # the rest of this chunk plus an END chunk.
436
            return self.bytes_left + 4
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
437
        elif self.state_accept == self._state_accept_expecting_length:
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
438
            if self._in_buffer_len == 0:
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
439
                # We're expecting a chunk length.  There's at least two bytes
440
                # left: a digit plus '\n'.
441
                return 2
442
            else:
443
                # We're in the middle of reading a chunk length.  So there's at
444
                # least one byte left, the '\n' that terminates the length.
445
                return 1
446
        elif self.state_accept == self._state_accept_reading_unused:
447
            return 1
2748.4.16 by Andrew Bennetts
Tweaks suggested by review.
448
        elif self.state_accept == self._state_accept_expecting_header:
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
449
            return max(0, len('chunked\n') - self._in_buffer_len)
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
450
        else:
451
            raise AssertionError("Impossible state: %r" % (self.state_accept,))
452
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
453
    def read_next_chunk(self):
454
        try:
455
            return self.chunks.popleft()
456
        except IndexError:
457
            return None
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
458
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
459
    def _extract_line(self):
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
460
        in_buf = self._get_in_buffer()
461
        pos = in_buf.find('\n')
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
462
        if pos == -1:
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
463
            # We haven't read a complete line yet, so request more bytes before
464
            # we continue.
3245.4.29 by Andrew Bennetts
Add/tidy some comments, remove dud test_errors_are_logged test, add explicit UnknownSmartMethod to v3.
465
            raise _NeedMoreBytes(1)
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
466
        line = in_buf[:pos]
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
467
        # Trim the prefix (including '\n' delimiter) from the _in_buffer.
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
468
        self._set_in_buffer(in_buf[pos+1:])
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
469
        return line
470
471
    def _finished(self):
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
472
        self.unused_data = self._get_in_buffer()
473
        self._in_buffer_list = []
474
        self._in_buffer_len = 0
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
475
        self.state_accept = self._state_accept_reading_unused
2748.4.6 by Andrew Bennetts
Use chunks for stream errors, rather than the response tuple format.
476
        if self.error:
477
            error_args = tuple(self.error_in_progress)
478
            self.chunks.append(request.FailedSmartServerResponse(error_args))
479
            self.error_in_progress = None
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
480
        self.finished_reading = True
481
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
482
    def _state_accept_expecting_header(self):
2748.4.16 by Andrew Bennetts
Tweaks suggested by review.
483
        prefix = self._extract_line()
3195.3.8 by Andrew Bennetts
Use _NeedMoreBytes to improve earlier protocol implementations a little, and make test_errors_are_logged pass.
484
        if prefix == 'chunked':
2748.4.16 by Andrew Bennetts
Tweaks suggested by review.
485
            self.state_accept = self._state_accept_expecting_length
486
        else:
487
            raise errors.SmartProtocolError(
488
                'Bad chunked body header: "%s"' % (prefix,))
489
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
490
    def _state_accept_expecting_length(self):
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
491
        prefix = self._extract_line()
3195.3.8 by Andrew Bennetts
Use _NeedMoreBytes to improve earlier protocol implementations a little, and make test_errors_are_logged pass.
492
        if prefix == 'ERR':
2748.4.6 by Andrew Bennetts
Use chunks for stream errors, rather than the response tuple format.
493
            self.error = True
494
            self.error_in_progress = []
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
495
            self._state_accept_expecting_length()
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
496
            return
2748.4.7 by Andrew Bennetts
Change the end-of-body marker to something clearer than a zero-length chunk.
497
        elif prefix == 'END':
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
498
            # We've read the end-of-body marker.
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
499
            # Any further bytes are unused data, including the bytes left in
500
            # the _in_buffer.
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
501
            self._finished()
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
502
            return
2748.4.7 by Andrew Bennetts
Change the end-of-body marker to something clearer than a zero-length chunk.
503
        else:
504
            self.bytes_left = int(prefix, 16)
505
            self.chunk_in_progress = ''
506
            self.state_accept = self._state_accept_reading_chunk
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
507
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
508
    def _state_accept_reading_chunk(self):
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
509
        in_buf = self._get_in_buffer()
510
        in_buffer_len = len(in_buf)
511
        self.chunk_in_progress += in_buf[:self.bytes_left]
512
        self._set_in_buffer(in_buf[self.bytes_left:])
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
513
        self.bytes_left -= in_buffer_len
514
        if self.bytes_left <= 0:
515
            # Finished with chunk
516
            self.bytes_left = None
2748.4.6 by Andrew Bennetts
Use chunks for stream errors, rather than the response tuple format.
517
            if self.error:
518
                self.error_in_progress.append(self.chunk_in_progress)
519
            else:
520
                self.chunks.append(self.chunk_in_progress)
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
521
            self.chunk_in_progress = None
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
522
            self.state_accept = self._state_accept_expecting_length
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
523
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
524
    def _state_accept_reading_unused(self):
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
525
        self.unused_data += self._get_in_buffer()
526
        self._in_buffer_list = []
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
527
528
529
class LengthPrefixedBodyDecoder(_StatefulDecoder):
530
    """Decodes the length-prefixed bulk data."""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
531
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
532
    def __init__(self):
533
        _StatefulDecoder.__init__(self)
534
        self.state_accept = self._state_accept_expecting_length
535
        self.state_read = self._state_read_no_data
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
536
        self._body = ''
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
537
        self._trailer_buffer = ''
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
538
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
539
    def next_read_size(self):
540
        if self.bytes_left is not None:
541
            # Ideally we want to read all the remainder of the body and the
542
            # trailer in one go.
543
            return self.bytes_left + 5
544
        elif self.state_accept == self._state_accept_reading_trailer:
545
            # Just the trailer left
546
            return 5 - len(self._trailer_buffer)
547
        elif self.state_accept == self._state_accept_expecting_length:
548
            # There's still at least 6 bytes left ('\n' to end the length, plus
549
            # 'done\n').
550
            return 6
551
        else:
552
            # Reading excess data.  Either way, 1 byte at a time is fine.
553
            return 1
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
554
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
555
    def read_pending_data(self):
556
        """Return any pending data that has been decoded."""
557
        return self.state_read()
558
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
559
    def _state_accept_expecting_length(self):
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
560
        in_buf = self._get_in_buffer()
561
        pos = in_buf.find('\n')
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
562
        if pos == -1:
563
            return
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
564
        self.bytes_left = int(in_buf[:pos])
565
        self._set_in_buffer(in_buf[pos+1:])
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
566
        self.state_accept = self._state_accept_reading_body
567
        self.state_read = self._state_read_body_buffer
568
569
    def _state_accept_reading_body(self):
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
570
        in_buf = self._get_in_buffer()
571
        self._body += in_buf
572
        self.bytes_left -= len(in_buf)
573
        self._set_in_buffer(None)
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
574
        if self.bytes_left <= 0:
575
            # Finished with body
576
            if self.bytes_left != 0:
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
577
                self._trailer_buffer = self._body[self.bytes_left:]
578
                self._body = self._body[:self.bytes_left]
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
579
            self.bytes_left = None
580
            self.state_accept = self._state_accept_reading_trailer
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
581
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
582
    def _state_accept_reading_trailer(self):
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
583
        self._trailer_buffer += self._get_in_buffer()
584
        self._set_in_buffer(None)
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
585
        # TODO: what if the trailer does not match "done\n"?  Should this raise
586
        # a ProtocolViolation exception?
587
        if self._trailer_buffer.startswith('done\n'):
588
            self.unused_data = self._trailer_buffer[len('done\n'):]
589
            self.state_accept = self._state_accept_reading_unused
590
            self.finished_reading = True
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
591
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
592
    def _state_accept_reading_unused(self):
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
593
        self.unused_data += self._get_in_buffer()
594
        self._set_in_buffer(None)
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
595
596
    def _state_read_no_data(self):
597
        return ''
598
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
599
    def _state_read_body_buffer(self):
600
        result = self._body
601
        self._body = ''
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
602
        return result
603
604
3245.4.26 by Andrew Bennetts
Rename 'setProtoAndMedium' to more accurate 'setProtoAndMediumRequest', add ABCs for Requesters and ResponseHandlers.
605
class SmartClientRequestProtocolOne(SmartProtocolBase, Requester,
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
606
                                    message.ResponseHandler):
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
607
    """The client-side protocol for smart version 1."""
608
609
    def __init__(self, request):
610
        """Construct a SmartClientRequestProtocolOne.
611
612
        :param request: A SmartClientMediumRequest to serialise onto and
613
            deserialise from.
614
        """
615
        self._request = request
616
        self._body_buffer = None
2664.4.1 by John Arbash Meinel
Add timing information for call/response groups for hpss
617
        self._request_start_time = None
3297.3.1 by Andrew Bennetts
Raise UnknownSmartMethod automatically from read_response_tuple.
618
        self._last_verb = None
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
619
        self._headers = None
620
621
    def set_headers(self, headers):
622
        self._headers = dict(headers)
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
623
624
    def call(self, *args):
2593.3.1 by Andrew Bennetts
Add a -Dhpss debug flag.
625
        if 'hpss' in debug.debug_flags:
2664.4.3 by John Arbash Meinel
Update to include a bit better formatting
626
            mutter('hpss call:   %s', repr(args)[1:-1])
3104.4.2 by Andrew Bennetts
All tests passing.
627
            if getattr(self._request._medium, 'base', None) is not None:
628
                mutter('             (to %s)', self._request._medium.base)
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
629
            self._request_start_time = osutils.timer_func()
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
630
        self._write_args(args)
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
631
        self._request.finished_writing()
3297.3.1 by Andrew Bennetts
Raise UnknownSmartMethod automatically from read_response_tuple.
632
        self._last_verb = args[0]
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
633
634
    def call_with_body_bytes(self, args, body):
635
        """Make a remote call of args with body bytes 'body'.
636
637
        After calling this, call read_response_tuple to find the result out.
638
        """
2593.3.1 by Andrew Bennetts
Add a -Dhpss debug flag.
639
        if 'hpss' in debug.debug_flags:
2664.4.3 by John Arbash Meinel
Update to include a bit better formatting
640
            mutter('hpss call w/body: %s (%r...)', repr(args)[1:-1], body[:20])
3104.4.2 by Andrew Bennetts
All tests passing.
641
            if getattr(self._request._medium, '_path', None) is not None:
642
                mutter('                  (to %s)', self._request._medium._path)
2664.4.4 by John Arbash Meinel
Switch around what bytes get logged.
643
            mutter('              %d bytes', len(body))
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
644
            self._request_start_time = osutils.timer_func()
3211.5.1 by Robert Collins
Change the smart server get_parents method to take a graph search to exclude already recieved parents from. This prevents history shortcuts causing huge numbers of duplicates.
645
            if 'hpssdetail' in debug.debug_flags:
646
                mutter('hpss body content: %s', body)
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
647
        self._write_args(args)
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
648
        bytes = self._encode_bulk_data(body)
649
        self._request.accept_bytes(bytes)
650
        self._request.finished_writing()
3297.3.1 by Andrew Bennetts
Raise UnknownSmartMethod automatically from read_response_tuple.
651
        self._last_verb = args[0]
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
652
653
    def call_with_body_readv_array(self, args, body):
654
        """Make a remote call with a readv array.
655
656
        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.
658
        """
2593.3.1 by Andrew Bennetts
Add a -Dhpss debug flag.
659
        if 'hpss' in debug.debug_flags:
2664.4.3 by John Arbash Meinel
Update to include a bit better formatting
660
            mutter('hpss call w/readv: %s', repr(args)[1:-1])
3104.4.2 by Andrew Bennetts
All tests passing.
661
            if getattr(self._request._medium, '_path', None) is not None:
662
                mutter('                  (to %s)', self._request._medium._path)
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
663
            self._request_start_time = osutils.timer_func()
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
664
        self._write_args(args)
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
665
        readv_bytes = self._serialise_offsets(body)
666
        bytes = self._encode_bulk_data(readv_bytes)
667
        self._request.accept_bytes(bytes)
668
        self._request.finished_writing()
2664.4.2 by John Arbash Meinel
Add debug timings for operations that have to send data
669
        if 'hpss' in debug.debug_flags:
2664.4.4 by John Arbash Meinel
Switch around what bytes get logged.
670
            mutter('              %d bytes in readv request', len(readv_bytes))
3297.3.1 by Andrew Bennetts
Raise UnknownSmartMethod automatically from read_response_tuple.
671
        self._last_verb = args[0]
4032.1.2 by John Arbash Meinel
Track down a few more files that have trailing whitespace.
672
3842.3.5 by Andrew Bennetts
Remove some debugging cruft, make more tests pass.
673
    def call_with_body_stream(self, args, stream):
674
        # Protocols v1 and v2 don't support body streams.  So it's safe to
675
        # assume that a v1/v2 server doesn't support whatever method we're
676
        # trying to call with a body stream.
3842.3.7 by Andrew Bennetts
Implement a simple fallback (that buffers the whole record stream) for dealing with older servers.
677
        self._request.finished_writing()
678
        self._request.finished_reading()
3842.3.5 by Andrew Bennetts
Remove some debugging cruft, make more tests pass.
679
        raise errors.UnknownSmartMethod(args[0])
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
680
681
    def cancel_read_body(self):
682
        """After expecting a body, a response code may indicate one otherwise.
683
684
        This method lets the domain client inform the protocol that no body
685
        will be transmitted. This is a terminal method: after calling it the
686
        protocol is not able to be used further.
687
        """
688
        self._request.finished_reading()
689
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
690
    def _read_response_tuple(self):
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
691
        result = self._recv_tuple()
2593.3.1 by Andrew Bennetts
Add a -Dhpss debug flag.
692
        if 'hpss' in debug.debug_flags:
2664.4.1 by John Arbash Meinel
Add timing information for call/response groups for hpss
693
            if self._request_start_time is not None:
2664.4.3 by John Arbash Meinel
Update to include a bit better formatting
694
                mutter('   result:   %6.3fs  %s',
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
695
                       osutils.timer_func() - self._request_start_time,
2664.4.3 by John Arbash Meinel
Update to include a bit better formatting
696
                       repr(result)[1:-1])
2664.4.1 by John Arbash Meinel
Add timing information for call/response groups for hpss
697
                self._request_start_time = None
698
            else:
2664.4.3 by John Arbash Meinel
Update to include a bit better formatting
699
                mutter('   result:   %s', repr(result)[1:-1])
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
700
        return result
701
702
    def read_response_tuple(self, expect_body=False):
703
        """Read a response tuple from the wire.
704
705
        This should only be called once.
706
        """
707
        result = self._read_response_tuple()
3297.3.1 by Andrew Bennetts
Raise UnknownSmartMethod automatically from read_response_tuple.
708
        self._response_is_unknown_method(result)
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
709
        self._raise_args_if_error(result)
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
710
        if not expect_body:
711
            self._request.finished_reading()
712
        return result
713
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
714
    def _raise_args_if_error(self, result_tuple):
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
715
        # Later protocol versions have an explicit flag in the protocol to say
716
        # if an error response is "failed" or not.  In version 1 we don't have
717
        # that luxury.  So here is a complete list of errors that can be
718
        # returned in response to existing version 1 smart requests.  Responses
719
        # starting with these codes are always "failed" responses.
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
720
        v1_error_codes = [
721
            'norepository',
722
            'NoSuchFile',
723
            'FileExists',
724
            'DirectoryNotEmpty',
725
            'ShortReadvError',
726
            'UnicodeEncodeError',
727
            'UnicodeDecodeError',
728
            'ReadOnlyError',
729
            'nobranch',
730
            'NoSuchRevision',
731
            'nosuchrevision',
732
            'LockContention',
733
            'UnlockableTransport',
734
            'LockFailed',
735
            'TokenMismatch',
736
            'ReadError',
737
            'PermissionDenied',
738
            ]
739
        if result_tuple[0] in v1_error_codes:
740
            self._request.finished_reading()
741
            raise errors.ErrorFromSmartServer(result_tuple)
742
3297.3.1 by Andrew Bennetts
Raise UnknownSmartMethod automatically from read_response_tuple.
743
    def _response_is_unknown_method(self, result_tuple):
744
        """Raise UnexpectedSmartServerResponse if the response is an 'unknonwn
745
        method' response to the request.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
746
3297.3.1 by Andrew Bennetts
Raise UnknownSmartMethod automatically from read_response_tuple.
747
        :param response: The response from a smart client call_expecting_body
748
            call.
749
        :param verb: The verb used in that call.
750
        :raises: UnexpectedSmartServerResponse
751
        """
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)):
756
            # The response will have no body, so we've finished reading.
757
            self._request.finished_reading()
758
            raise errors.UnknownSmartMethod(self._last_verb)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
759
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
760
    def read_body_bytes(self, count=-1):
761
        """Read bytes from the body, decoding into a byte stream.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
762
763
        We read all bytes at once to ensure we've checked the trailer for
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
764
        errors, and then feed the buffer back as read_body_bytes is called.
765
        """
766
        if self._body_buffer is not None:
767
            return self._body_buffer.read(count)
768
        _body_decoder = LengthPrefixedBodyDecoder()
769
770
        while not _body_decoder.finished_reading:
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
771
            bytes = self._request.read_bytes(_body_decoder.next_read_size())
3464.4.1 by Andrew Bennetts
Fix infinite busy-loop caused by connection loss during read of response body in HPSS v1 and v2.
772
            if bytes == '':
773
                # end of file encountered reading from server
774
                raise errors.ConnectionReset(
775
                    "Connection lost while reading response body.")
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
776
            _body_decoder.accept_bytes(bytes)
777
        self._request.finished_reading()
778
        self._body_buffer = StringIO(_body_decoder.read_pending_data())
779
        # XXX: TODO check the trailer result.
2664.4.3 by John Arbash Meinel
Update to include a bit better formatting
780
        if 'hpss' in debug.debug_flags:
2664.4.4 by John Arbash Meinel
Switch around what bytes get logged.
781
            mutter('              %d body bytes read',
2664.4.3 by John Arbash Meinel
Update to include a bit better formatting
782
                   len(self._body_buffer.getvalue()))
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
783
        return self._body_buffer.read(count)
784
785
    def _recv_tuple(self):
786
        """Receive a tuple from the medium request."""
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
787
        return _decode_tuple(self._request.read_line())
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
788
789
    def query_version(self):
790
        """Return protocol version number of the server."""
791
        self.call('hello')
792
        resp = self.read_response_tuple()
793
        if resp == ('ok', '1'):
794
            return 1
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
795
        elif resp == ('ok', '2'):
796
            return 2
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
797
        else:
798
            raise errors.SmartProtocolError("bad response %r" % (resp,))
799
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
800
    def _write_args(self, args):
801
        self._write_protocol_version()
802
        bytes = _encode_tuple(args)
803
        self._request.accept_bytes(bytes)
804
805
    def _write_protocol_version(self):
806
        """Write any prefixes this protocol requires.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
807
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
808
        Version one doesn't send protocol versions.
809
        """
810
811
812
class SmartClientRequestProtocolTwo(SmartClientRequestProtocolOne):
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
813
    """Version two of the client side of the smart protocol.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
814
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
815
    This prefixes the request with the value of REQUEST_VERSION_TWO.
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
816
    """
817
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
818
    response_marker = RESPONSE_VERSION_TWO
819
    request_marker = REQUEST_VERSION_TWO
820
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
821
    def read_response_tuple(self, expect_body=False):
822
        """Read a response tuple from the wire.
823
824
        This should only be called once.
825
        """
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
826
        version = self._request.read_line()
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
827
        if version != self.response_marker:
3245.4.47 by Andrew Bennetts
Don't automatically send 'hello' requests from RemoteBzrDirFormat.probe_transport unless we have to (i.e. the transport is HTTP).
828
            self._request.finished_reading()
3245.4.43 by Andrew Bennetts
Improve tests for automatic detection of protocol version.
829
            raise errors.UnexpectedProtocolVersionMarker(version)
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
830
        response_status = self._request.read_line()
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
831
        result = SmartClientRequestProtocolOne._read_response_tuple(self)
3245.4.55 by Andrew Bennetts
Test improvements suggested by John's review.
832
        self._response_is_unknown_method(result)
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
833
        if response_status == 'success\n':
834
            self.response_status = True
835
            if not expect_body:
836
                self._request.finished_reading()
837
            return result
838
        elif response_status == 'failed\n':
839
            self.response_status = False
840
            self._request.finished_reading()
841
            raise errors.ErrorFromSmartServer(result)
842
        else:
2432.4.6 by Robert Collins
Include success/failure feedback in SmartProtocolTwo responses to allow robust handling in the future.
843
            raise errors.SmartProtocolError(
844
                'bad protocol status %r' % response_status)
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
845
846
    def _write_protocol_version(self):
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
847
        """Write any prefixes this protocol requires.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
848
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
849
        Version two sends the value of REQUEST_VERSION_TWO.
2432.2.1 by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker.
850
        """
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
851
        self._request.accept_bytes(self.request_marker)
2018.5.3 by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py
852
2748.4.2 by Andrew Bennetts
Add protocol (version two) support for streaming bodies (using chunking) in responses.
853
    def read_streamed_body(self):
854
        """Read bytes from the body, decoding into a byte stream.
855
        """
3170.5.1 by Andrew Bennetts
Fix the other half of bug #115781: don't read more than 64k at a time either.
856
        # Read no more than 64k at a time so that we don't risk error 10055 (no
857
        # buffer space available) on Windows.
2748.4.2 by Andrew Bennetts
Add protocol (version two) support for streaming bodies (using chunking) in responses.
858
        _body_decoder = ChunkedBodyDecoder()
859
        while not _body_decoder.finished_reading:
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
860
            bytes = self._request.read_bytes(_body_decoder.next_read_size())
3464.4.1 by Andrew Bennetts
Fix infinite busy-loop caused by connection loss during read of response body in HPSS v1 and v2.
861
            if bytes == '':
862
                # end of file encountered reading from server
863
                raise errors.ConnectionReset(
864
                    "Connection lost while reading streamed body.")
2748.4.2 by Andrew Bennetts
Add protocol (version two) support for streaming bodies (using chunking) in responses.
865
            _body_decoder.accept_bytes(bytes)
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
866
            for body_bytes in iter(_body_decoder.read_next_chunk, None):
3245.4.18 by Andrew Bennetts
Remove a bunch of cruft, especially the SmartClientRequestProtocolThree class.
867
                if 'hpss' in debug.debug_flags and type(body_bytes) is str:
2748.4.5 by Andrew Bennetts
Allow an error to interrupt (and terminate) a streamed response body.
868
                    mutter('              %d byte chunk read',
2535.4.3 by Andrew Bennetts
Remove some useless mutters.
869
                           len(body_bytes))
2748.4.2 by Andrew Bennetts
Add protocol (version two) support for streaming bodies (using chunking) in responses.
870
                yield body_bytes
871
        self._request.finished_reading()
872
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
873
3245.4.14 by Andrew Bennetts
Merge from bzr.dev (via loom thread).
874
def build_server_protocol_three(backing_transport, write_func,
4760.1.1 by Andrew Bennetts
Add optional jail_root argument to SmartServerRequest and friends, and use it in the WSGI glue. Allows opening branches in shared repos via bzr+http (assuming the repo should be accessible).
875
                                root_client_path, jail_root=None):
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
876
    request_handler = request.SmartServerRequestHandler(
3245.4.14 by Andrew Bennetts
Merge from bzr.dev (via loom thread).
877
        backing_transport, commands=request.request_handlers,
4760.1.1 by Andrew Bennetts
Add optional jail_root argument to SmartServerRequest and friends, and use it in the WSGI glue. Allows opening branches in shared repos via bzr+http (assuming the repo should be accessible).
878
        root_client_path=root_client_path, jail_root=jail_root)
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
879
    responder = ProtocolThreeResponder(write_func)
880
    message_handler = message.ConventionalRequestHandler(request_handler, responder)
3245.4.7 by Andrew Bennetts
Rename _ProtocolThreeBase to ProtocolThreeDecoder, remove SmartServerRequestProtocolThree.
881
    return ProtocolThreeDecoder(message_handler)
882
883
884
class ProtocolThreeDecoder(_StatefulDecoder):
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
885
886
    response_marker = RESPONSE_VERSION_THREE
887
    request_marker = REQUEST_VERSION_THREE
888
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
889
    def __init__(self, message_handler, expect_version_marker=False):
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
890
        _StatefulDecoder.__init__(self)
3245.4.59 by Andrew Bennetts
Various tweaks in response to Martin's review.
891
        self._has_dispatched = False
3195.3.5 by Andrew Bennetts
Start writing the client-side protocol logic for HPSS v3.
892
        # Initial state
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
893
        if expect_version_marker:
894
            self.state_accept = self._state_accept_expecting_protocol_version
895
            # We're expecting at least the protocol version marker + some
896
            # headers.
897
            self._number_needed_bytes = len(MESSAGE_VERSION_THREE) + 4
898
        else:
899
            self.state_accept = self._state_accept_expecting_headers
900
            self._number_needed_bytes = 4
3245.4.49 by Andrew Bennetts
Distinguish between errors in decoding a message into message parts from errors in handling decoded message parts, and use that to make sure that entire requests are read even when they result in exceptions.
901
        self.decoding_failed = False
3195.3.16 by Andrew Bennetts
Update tests for revised v3 spec.
902
        self.request_handler = self.message_handler = message_handler
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
903
3195.3.8 by Andrew Bennetts
Use _NeedMoreBytes to improve earlier protocol implementations a little, and make test_errors_are_logged pass.
904
    def accept_bytes(self, bytes):
3195.3.18 by Andrew Bennetts
call_with_body_bytes now works with v3 (e.g. test_copy_content_remote_to_local passes). Lots of debugging cruft, though.
905
        self._number_needed_bytes = None
3195.3.8 by Andrew Bennetts
Use _NeedMoreBytes to improve earlier protocol implementations a little, and make test_errors_are_logged pass.
906
        try:
907
            _StatefulDecoder.accept_bytes(self, bytes)
908
        except KeyboardInterrupt:
909
            raise
3245.4.49 by Andrew Bennetts
Distinguish between errors in decoding a message into message parts from errors in handling decoded message parts, and use that to make sure that entire requests are read even when they result in exceptions.
910
        except errors.SmartMessageHandlerError, exception:
911
            # We do *not* set self.decoding_failed here.  The message handler
912
            # has raised an error, but the decoder is still able to parse bytes
913
            # and determine when this message ends.
4475.2.1 by Andrew Bennetts
Don't log UnknownSmartMethod errors on the server.
914
            if not isinstance(exception.exc_value, errors.UnknownSmartMethod):
915
                log_exception_quietly()
3245.4.49 by Andrew Bennetts
Distinguish between errors in decoding a message into message parts from errors in handling decoded message parts, and use that to make sure that entire requests are read even when they result in exceptions.
916
            self.message_handler.protocol_error(exception.exc_value)
917
            # The state machine is ready to continue decoding, but the
918
            # exception has interrupted the loop that runs the state machine.
919
            # So we call accept_bytes again to restart it.
920
            self.accept_bytes('')
3195.3.8 by Andrew Bennetts
Use _NeedMoreBytes to improve earlier protocol implementations a little, and make test_errors_are_logged pass.
921
        except Exception, exception:
3245.4.50 by Andrew Bennetts
Clarify the code a little.
922
            # The decoder itself has raised an exception.  We cannot continue
923
            # decoding.
3245.4.49 by Andrew Bennetts
Distinguish between errors in decoding a message into message parts from errors in handling decoded message parts, and use that to make sure that entire requests are read even when they result in exceptions.
924
            self.decoding_failed = True
3245.4.47 by Andrew Bennetts
Don't automatically send 'hello' requests from RemoteBzrDirFormat.probe_transport unless we have to (i.e. the transport is HTTP).
925
            if isinstance(exception, errors.UnexpectedProtocolVersionMarker):
926
                # This happens during normal operation when the client tries a
927
                # protocol version the server doesn't understand, so no need to
928
                # log a traceback every time.
3245.4.51 by Andrew Bennetts
Add another comment.
929
                # Note that this can only happen when
930
                # expect_version_marker=True, which is only the case on the
931
                # client side.
3245.4.47 by Andrew Bennetts
Don't automatically send 'hello' requests from RemoteBzrDirFormat.probe_transport unless we have to (i.e. the transport is HTTP).
932
                pass
933
            else:
934
                log_exception_quietly()
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
935
            self.message_handler.protocol_error(exception)
3195.3.8 by Andrew Bennetts
Use _NeedMoreBytes to improve earlier protocol implementations a little, and make test_errors_are_logged pass.
936
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
937
    def _extract_length_prefixed_bytes(self):
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
938
        if self._in_buffer_len < 4:
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
939
            # A length prefix by itself is 4 bytes, and we don't even have that
940
            # many yet.
3195.3.18 by Andrew Bennetts
call_with_body_bytes now works with v3 (e.g. test_copy_content_remote_to_local passes). Lots of debugging cruft, though.
941
            raise _NeedMoreBytes(4)
3649.5.2 by John Arbash Meinel
When we are waiting on a big stream, allow
942
        (length,) = struct.unpack('!L', self._get_in_bytes(4))
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
943
        end_of_bytes = 4 + length
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
944
        if self._in_buffer_len < end_of_bytes:
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
945
            # We haven't yet read as many bytes as the length-prefix says there
946
            # are.
3195.3.18 by Andrew Bennetts
call_with_body_bytes now works with v3 (e.g. test_copy_content_remote_to_local passes). Lots of debugging cruft, though.
947
            raise _NeedMoreBytes(end_of_bytes)
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
948
        # Extract the bytes from the buffer.
3649.5.2 by John Arbash Meinel
When we are waiting on a big stream, allow
949
        in_buf = self._get_in_buffer()
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
950
        bytes = in_buf[4:end_of_bytes]
951
        self._set_in_buffer(in_buf[end_of_bytes:])
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
952
        return bytes
953
954
    def _extract_prefixed_bencoded_data(self):
955
        prefixed_bytes = self._extract_length_prefixed_bytes()
956
        try:
3842.3.21 by Andrew Bennetts
Merge bzr.dev.
957
            decoded = bdecode_as_tuple(prefixed_bytes)
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
958
        except ValueError:
959
            raise errors.SmartProtocolError(
960
                'Bytes %r not bencoded' % (prefixed_bytes,))
961
        return decoded
962
3195.3.7 by Andrew Bennetts
Make the '_extract_* helpers more robust by adding a _NeedMoreBytes exception to _StatefulDecoder.
963
    def _extract_single_byte(self):
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
964
        if self._in_buffer_len == 0:
3195.3.7 by Andrew Bennetts
Make the '_extract_* helpers more robust by adding a _NeedMoreBytes exception to _StatefulDecoder.
965
            # The buffer is empty
3245.4.28 by Andrew Bennetts
Remove another XXX, and include test ID in smart server thread names.
966
            raise _NeedMoreBytes(1)
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
967
        in_buf = self._get_in_buffer()
968
        one_byte = in_buf[0]
969
        self._set_in_buffer(in_buf[1:])
3195.3.7 by Andrew Bennetts
Make the '_extract_* helpers more robust by adding a _NeedMoreBytes exception to _StatefulDecoder.
970
        return one_byte
971
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
972
    def _state_accept_expecting_protocol_version(self):
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
973
        needed_bytes = len(MESSAGE_VERSION_THREE) - self._in_buffer_len
974
        in_buf = self._get_in_buffer()
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
975
        if needed_bytes > 0:
3245.4.46 by Andrew Bennetts
Apply John's review comments.
976
            # We don't have enough bytes to check if the protocol version
977
            # marker is right.  But we can check if it is already wrong by
978
            # checking that the start of MESSAGE_VERSION_THREE matches what
979
            # we've read so far.
980
            # [In fact, if the remote end isn't bzr we might never receive
981
            # len(MESSAGE_VERSION_THREE) bytes.  So if the bytes we have so far
982
            # are wrong then we should just raise immediately rather than
983
            # stall.]
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
984
            if not MESSAGE_VERSION_THREE.startswith(in_buf):
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
985
                # We have enough bytes to know the protocol version is wrong
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
986
                raise errors.UnexpectedProtocolVersionMarker(in_buf)
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
987
            raise _NeedMoreBytes(len(MESSAGE_VERSION_THREE))
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
988
        if not in_buf.startswith(MESSAGE_VERSION_THREE):
989
            raise errors.UnexpectedProtocolVersionMarker(in_buf)
990
        self._set_in_buffer(in_buf[len(MESSAGE_VERSION_THREE):])
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
991
        self.state_accept = self._state_accept_expecting_headers
992
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
993
    def _state_accept_expecting_headers(self):
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
994
        decoded = self._extract_prefixed_bencoded_data()
995
        if type(decoded) is not dict:
996
            raise errors.SmartProtocolError(
997
                'Header object %r is not a dict' % (decoded,))
3195.3.16 by Andrew Bennetts
Update tests for revised v3 spec.
998
        self.state_accept = self._state_accept_expecting_message_part
3245.4.49 by Andrew Bennetts
Distinguish between errors in decoding a message into message parts from errors in handling decoded message parts, and use that to make sure that entire requests are read even when they result in exceptions.
999
        try:
1000
            self.message_handler.headers_received(decoded)
1001
        except:
1002
            raise errors.SmartMessageHandlerError(sys.exc_info())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1003
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
1004
    def _state_accept_expecting_message_part(self):
3195.3.16 by Andrew Bennetts
Update tests for revised v3 spec.
1005
        message_part_kind = self._extract_single_byte()
1006
        if message_part_kind == 'o':
1007
            self.state_accept = self._state_accept_expecting_one_byte
1008
        elif message_part_kind == 's':
1009
            self.state_accept = self._state_accept_expecting_structure
1010
        elif message_part_kind == 'b':
1011
            self.state_accept = self._state_accept_expecting_bytes
1012
        elif message_part_kind == 'e':
1013
            self.done()
1014
        else:
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
1015
            raise errors.SmartProtocolError(
3195.3.16 by Andrew Bennetts
Update tests for revised v3 spec.
1016
                'Bad message kind byte: %r' % (message_part_kind,))
1017
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
1018
    def _state_accept_expecting_one_byte(self):
3195.3.16 by Andrew Bennetts
Update tests for revised v3 spec.
1019
        byte = self._extract_single_byte()
1020
        self.state_accept = self._state_accept_expecting_message_part
3245.4.49 by Andrew Bennetts
Distinguish between errors in decoding a message into message parts from errors in handling decoded message parts, and use that to make sure that entire requests are read even when they result in exceptions.
1021
        try:
1022
            self.message_handler.byte_part_received(byte)
1023
        except:
1024
            raise errors.SmartMessageHandlerError(sys.exc_info())
3195.3.16 by Andrew Bennetts
Update tests for revised v3 spec.
1025
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
1026
    def _state_accept_expecting_bytes(self):
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1027
        # XXX: this should not buffer whole message part, but instead deliver
1028
        # the bytes as they arrive.
3195.3.18 by Andrew Bennetts
call_with_body_bytes now works with v3 (e.g. test_copy_content_remote_to_local passes). Lots of debugging cruft, though.
1029
        prefixed_bytes = self._extract_length_prefixed_bytes()
3195.3.16 by Andrew Bennetts
Update tests for revised v3 spec.
1030
        self.state_accept = self._state_accept_expecting_message_part
3245.4.49 by Andrew Bennetts
Distinguish between errors in decoding a message into message parts from errors in handling decoded message parts, and use that to make sure that entire requests are read even when they result in exceptions.
1031
        try:
1032
            self.message_handler.bytes_part_received(prefixed_bytes)
1033
        except:
1034
            raise errors.SmartMessageHandlerError(sys.exc_info())
3195.3.16 by Andrew Bennetts
Update tests for revised v3 spec.
1035
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
1036
    def _state_accept_expecting_structure(self):
3195.3.16 by Andrew Bennetts
Update tests for revised v3 spec.
1037
        structure = self._extract_prefixed_bencoded_data()
1038
        self.state_accept = self._state_accept_expecting_message_part
3245.4.49 by Andrew Bennetts
Distinguish between errors in decoding a message into message parts from errors in handling decoded message parts, and use that to make sure that entire requests are read even when they result in exceptions.
1039
        try:
1040
            self.message_handler.structure_part_received(structure)
1041
        except:
1042
            raise errors.SmartMessageHandlerError(sys.exc_info())
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
1043
1044
    def done(self):
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
1045
        self.unused_data = self._get_in_buffer()
1046
        self._set_in_buffer(None)
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
1047
        self.state_accept = self._state_accept_reading_unused
3245.4.49 by Andrew Bennetts
Distinguish between errors in decoding a message into message parts from errors in handling decoded message parts, and use that to make sure that entire requests are read even when they result in exceptions.
1048
        try:
1049
            self.message_handler.end_received()
1050
        except:
1051
            raise errors.SmartMessageHandlerError(sys.exc_info())
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
1052
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
1053
    def _state_accept_reading_unused(self):
4515.1.1 by Andrew Bennetts
Fix bug in HPSS v3 decoder when receiving multiple lots of excess bytes.
1054
        self.unused_data += self._get_in_buffer()
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
1055
        self._set_in_buffer(None)
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
1056
1057
    def next_read_size(self):
1058
        if self.state_accept == self._state_accept_reading_unused:
1059
            return 0
3245.4.49 by Andrew Bennetts
Distinguish between errors in decoding a message into message parts from errors in handling decoded message parts, and use that to make sure that entire requests are read even when they result in exceptions.
1060
        elif self.decoding_failed:
3245.4.29 by Andrew Bennetts
Add/tidy some comments, remove dud test_errors_are_logged test, add explicit UnknownSmartMethod to v3.
1061
            # An exception occured while processing this message, probably from
1062
            # self.message_handler.  We're not sure that this state machine is
1063
            # in a consistent state, so just signal that we're done (i.e. give
1064
            # up).
3245.4.28 by Andrew Bennetts
Remove another XXX, and include test ID in smart server thread names.
1065
            return 0
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
1066
        else:
3195.3.18 by Andrew Bennetts
call_with_body_bytes now works with v3 (e.g. test_copy_content_remote_to_local passes). Lots of debugging cruft, though.
1067
            if self._number_needed_bytes is not None:
3649.5.1 by John Arbash Meinel
Change _StatefulDecoder._in_bytes into a _in_bytes_list
1068
                return self._number_needed_bytes - self._in_buffer_len
3195.3.18 by Andrew Bennetts
call_with_body_bytes now works with v3 (e.g. test_copy_content_remote_to_local passes). Lots of debugging cruft, though.
1069
            else:
3245.4.28 by Andrew Bennetts
Remove another XXX, and include test ID in smart server thread names.
1070
                raise AssertionError("don't know how many bytes are expected!")
3195.3.2 by Andrew Bennetts
Checkpoint first rough cut of SmartServerRequestProtocolThree, this implementation reuses the _StatefulDecoder class. Plus some attempts to start tidying the smart protocol tests.
1071
1072
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1073
class _ProtocolThreeEncoder(object):
1074
3245.4.18 by Andrew Bennetts
Remove a bunch of cruft, especially the SmartClientRequestProtocolThree class.
1075
    response_marker = request_marker = MESSAGE_VERSION_THREE
4889.4.2 by John Arbash Meinel
Change the amount buffered to be a 'constant' that we can get at.
1076
    BUFFER_SIZE = 1024*1024 # 1 MiB buffer before flushing
3245.4.18 by Andrew Bennetts
Remove a bunch of cruft, especially the SmartClientRequestProtocolThree class.
1077
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1078
    def __init__(self, write_func):
4078.1.1 by Andrew Bennetts
Slightly better buffering logic when generating protocol v3 messages, especially during streamed bodies.
1079
        self._buf = []
4889.4.1 by John Arbash Meinel
Change the buffering algorithm for ProtocolThree.
1080
        self._buf_len = 0
3441.3.1 by Andrew Bennetts
Buffer encoding of v3 messages to minimise write/send calls. Doubles the speed of pushing over TCP with 500ms latency loopback.
1081
        self._real_write_func = write_func
1082
1083
    def _write_func(self, bytes):
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
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
        # TODO: Another possibility would be to turn this into an async model.
1088
        #       Where we let another thread know that we have some bytes if
1089
        #       they want it, but we don't actually block for it
1090
        #       Note that osutils.send_all always sends 64kB chunks anyway, so
1091
        #       we might just push out smaller bits at a time?
4078.1.1 by Andrew Bennetts
Slightly better buffering logic when generating protocol v3 messages, especially during streamed bodies.
1092
        self._buf.append(bytes)
4889.4.1 by John Arbash Meinel
Change the buffering algorithm for ProtocolThree.
1093
        self._buf_len += len(bytes)
4889.4.2 by John Arbash Meinel
Change the amount buffered to be a 'constant' that we can get at.
1094
        if self._buf_len > self.BUFFER_SIZE:
4078.1.1 by Andrew Bennetts
Slightly better buffering logic when generating protocol v3 messages, especially during streamed bodies.
1095
            self.flush()
3441.3.1 by Andrew Bennetts
Buffer encoding of v3 messages to minimise write/send calls. Doubles the speed of pushing over TCP with 500ms latency loopback.
1096
1097
    def flush(self):
1098
        if self._buf:
4078.1.1 by Andrew Bennetts
Slightly better buffering logic when generating protocol v3 messages, especially during streamed bodies.
1099
            self._real_write_func(''.join(self._buf))
1100
            del self._buf[:]
4889.4.1 by John Arbash Meinel
Change the buffering algorithm for ProtocolThree.
1101
            self._buf_len = 0
3441.3.1 by Andrew Bennetts
Buffer encoding of v3 messages to minimise write/send calls. Doubles the speed of pushing over TCP with 500ms latency loopback.
1102
3245.4.18 by Andrew Bennetts
Remove a bunch of cruft, especially the SmartClientRequestProtocolThree class.
1103
    def _serialise_offsets(self, offsets):
1104
        """Serialise a readv offset list."""
1105
        txt = []
1106
        for start, length in offsets:
1107
            txt.append('%d,%d' % (start, length))
1108
        return '\n'.join(txt)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1109
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1110
    def _write_protocol_version(self):
1111
        self._write_func(MESSAGE_VERSION_THREE)
1112
1113
    def _write_prefixed_bencode(self, structure):
1114
        bytes = bencode(structure)
1115
        self._write_func(struct.pack('!L', len(bytes)))
1116
        self._write_func(bytes)
1117
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
1118
    def _write_headers(self, headers):
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1119
        self._write_prefixed_bencode(headers)
1120
1121
    def _write_structure(self, args):
1122
        self._write_func('s')
3195.3.23 by Andrew Bennetts
Improve the error handling, fixing more tests.
1123
        utf8_args = []
1124
        for arg in args:
1125
            if type(arg) is unicode:
1126
                utf8_args.append(arg.encode('utf8'))
1127
            else:
1128
                utf8_args.append(arg)
1129
        self._write_prefixed_bencode(utf8_args)
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1130
1131
    def _write_end(self):
1132
        self._write_func('e')
3441.3.2 by Andrew Bennetts
Simplify buffering logic in _ProtocolThreeEncoder.
1133
        self.flush()
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1134
1135
    def _write_prefixed_body(self, bytes):
1136
        self._write_func('b')
1137
        self._write_func(struct.pack('!L', len(bytes)))
1138
        self._write_func(bytes)
1139
3842.3.4 by Andrew Bennetts
TestStacking.test_fetch_copies_from_stacked_on now passes using the VersionedFile.insert_record_stream RPC; lots of debugging cruft needs removal though.
1140
    def _write_chunked_body_start(self):
1141
        self._write_func('oC')
1142
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1143
    def _write_error_status(self):
1144
        self._write_func('oE')
1145
1146
    def _write_success_status(self):
1147
        self._write_func('oS')
1148
1149
1150
class ProtocolThreeResponder(_ProtocolThreeEncoder):
1151
1152
    def __init__(self, write_func):
1153
        _ProtocolThreeEncoder.__init__(self, write_func)
1154
        self.response_sent = False
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
1155
        self._headers = {'Software version': bzrlib.__version__}
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1156
        if 'hpss' in debug.debug_flags:
4913.1.1 by John Arbash Meinel
Switch to using thread.get_ident() which is available on all python versions.
1157
            self._thread_id = thread.get_ident()
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1158
            self._response_start_time = None
1159
4889.2.4 by John Arbash Meinel
Clean up some bits, we don't really need the time reported except for end time.
1160
    def _trace(self, action, message, extra_bytes=None, include_time=False):
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1161
        if self._response_start_time is None:
1162
            self._response_start_time = osutils.timer_func()
4889.2.4 by John Arbash Meinel
Clean up some bits, we don't really need the time reported except for end time.
1163
        if include_time:
1164
            t = '%5.3fs ' % (time.clock() - self._response_start_time)
1165
        else:
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1166
            t = ''
1167
        if extra_bytes is None:
1168
            extra = ''
1169
        else:
1170
            extra = ' ' + repr(extra_bytes[:40])
1171
            if len(extra) > 33:
1172
                extra = extra[:29] + extra[-1] + '...'
4889.2.3 by John Arbash Meinel
Get rid of -Dhpssthread, just always include it.
1173
        mutter('%12s: [%s] %s%s%s'
1174
               % (action, self._thread_id, t, message, extra))
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1175
1176
    def send_error(self, exception):
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
1177
        if self.response_sent:
1178
            raise AssertionError(
1179
                "send_error(%s) called, but response already sent."
1180
                % (exception,))
3245.4.29 by Andrew Bennetts
Add/tidy some comments, remove dud test_errors_are_logged test, add explicit UnknownSmartMethod to v3.
1181
        if isinstance(exception, errors.UnknownSmartMethod):
3245.4.37 by Andrew Bennetts
Add test for sending ProtocolThreeResponder.send_error(UnknownSmartMethod(...)).
1182
            failure = request.FailedSmartServerResponse(
1183
                ('UnknownMethod', exception.verb))
3245.4.29 by Andrew Bennetts
Add/tidy some comments, remove dud test_errors_are_logged test, add explicit UnknownSmartMethod to v3.
1184
            self.send_response(failure)
1185
            return
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1186
        if 'hpss' in debug.debug_flags:
1187
            self._trace('error', str(exception))
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1188
        self.response_sent = True
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
1189
        self._write_protocol_version()
1190
        self._write_headers(self._headers)
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1191
        self._write_error_status()
1192
        self._write_structure(('error', str(exception)))
1193
        self._write_end()
1194
1195
    def send_response(self, response):
3245.4.54 by Andrew Bennetts
Improve _StatefulDecoder after John's review.
1196
        if self.response_sent:
1197
            raise AssertionError(
1198
                "send_response(%r) called, but response already sent."
1199
                % (response,))
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1200
        self.response_sent = True
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
1201
        self._write_protocol_version()
1202
        self._write_headers(self._headers)
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1203
        if response.is_successful():
1204
            self._write_success_status()
1205
        else:
1206
            self._write_error_status()
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1207
        if 'hpss' in debug.debug_flags:
4889.2.4 by John Arbash Meinel
Clean up some bits, we don't really need the time reported except for end time.
1208
            self._trace('response', repr(response.args))
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1209
        self._write_structure(response.args)
1210
        if response.body is not None:
1211
            self._write_prefixed_body(response.body)
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1212
            if 'hpss' in debug.debug_flags:
1213
                self._trace('body', '%d bytes' % (len(response.body),),
4889.2.4 by John Arbash Meinel
Clean up some bits, we don't really need the time reported except for end time.
1214
                            response.body, include_time=True)
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1215
        elif response.body_stream is not None:
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1216
            count = num_bytes = 0
1217
            first_chunk = None
4064.1.3 by Andrew Bennetts
Fix bug in _iter_with_errors; it should not report StopIteration as an exception.
1218
            for exc_info, chunk in _iter_with_errors(response.body_stream):
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1219
                count += 1
4064.1.3 by Andrew Bennetts
Fix bug in _iter_with_errors; it should not report StopIteration as an exception.
1220
                if exc_info is not None:
4064.1.1 by Andrew Bennetts
Add TestResponseEncodingProtocolThree.test_send_broken_body_stream, and make it pass.
1221
                    self._write_error_status()
4064.1.3 by Andrew Bennetts
Fix bug in _iter_with_errors; it should not report StopIteration as an exception.
1222
                    error_struct = request._translate_error(exc_info[1])
4064.1.1 by Andrew Bennetts
Add TestResponseEncodingProtocolThree.test_send_broken_body_stream, and make it pass.
1223
                    self._write_structure(error_struct)
1224
                    break
1225
                else:
4070.9.2 by Andrew Bennetts
Rough prototype of allowing a SearchResult to be passed to fetch, and using that to improve network conversations.
1226
                    if isinstance(chunk, request.FailedSmartServerResponse):
1227
                        self._write_error_status()
1228
                        self._write_structure(chunk.args)
1229
                        break
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1230
                    num_bytes += len(chunk)
1231
                    if first_chunk is None:
1232
                        first_chunk = chunk
4064.1.1 by Andrew Bennetts
Add TestResponseEncodingProtocolThree.test_send_broken_body_stream, and make it pass.
1233
                    self._write_prefixed_body(chunk)
5283.5.1 by Martin Pool
Flush smart protocol to network after every chunk
1234
                    self.flush()
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1235
                    if 'hpssdetail' in debug.debug_flags:
1236
                        # Not worth timing separately, as _write_func is
1237
                        # actually buffered
1238
                        self._trace('body chunk',
1239
                                    '%d bytes' % (len(chunk),),
1240
                                    chunk, suppress_time=True)
1241
            if 'hpss' in debug.debug_flags:
4889.2.4 by John Arbash Meinel
Clean up some bits, we don't really need the time reported except for end time.
1242
                self._trace('body stream',
1243
                            '%d bytes %d chunks' % (num_bytes, count),
1244
                            first_chunk)
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1245
        self._write_end()
4889.2.4 by John Arbash Meinel
Clean up some bits, we don't really need the time reported except for end time.
1246
        if 'hpss' in debug.debug_flags:
1247
            self._trace('response end', '', include_time=True)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1248
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1249
4064.1.2 by Andrew Bennetts
Refactor server-side error translation, improve tests.
1250
def _iter_with_errors(iterable):
1251
    """Handle errors from iterable.next().
1252
1253
    Use like::
1254
1255
        for exc_info, value in _iter_with_errors(iterable):
1256
            ...
1257
1258
    This is a safer alternative to::
1259
1260
        try:
1261
            for value in iterable:
1262
               ...
1263
        except:
1264
            ...
1265
1266
    Because the latter will catch errors from the for-loop body, not just
1267
    iterable.next()
1268
1269
    If an error occurs, exc_info will be a exc_info tuple, and the generator
1270
    will terminate.  Otherwise exc_info will be None, and value will be the
1271
    value from iterable.next().  Note that KeyboardInterrupt and SystemExit
1272
    will not be itercepted.
1273
    """
4064.1.1 by Andrew Bennetts
Add TestResponseEncodingProtocolThree.test_send_broken_body_stream, and make it pass.
1274
    iterator = iter(iterable)
1275
    while True:
1276
        try:
1277
            yield None, iterator.next()
4064.1.3 by Andrew Bennetts
Fix bug in _iter_with_errors; it should not report StopIteration as an exception.
1278
        except StopIteration:
1279
            return
4064.1.2 by Andrew Bennetts
Refactor server-side error translation, improve tests.
1280
        except (KeyboardInterrupt, SystemExit):
1281
            raise
1282
        except Exception:
4476.3.15 by Andrew Bennetts
Partially working fallback for pre-1.17 servers.
1283
            mutter('_iter_with_errors caught error')
1284
            log_exception_quietly()
4064.1.1 by Andrew Bennetts
Add TestResponseEncodingProtocolThree.test_send_broken_body_stream, and make it pass.
1285
            yield sys.exc_info(), None
1286
            return
1287
1288
3245.4.26 by Andrew Bennetts
Rename 'setProtoAndMedium' to more accurate 'setProtoAndMediumRequest', add ABCs for Requesters and ResponseHandlers.
1289
class ProtocolThreeRequester(_ProtocolThreeEncoder, Requester):
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1290
1291
    def __init__(self, medium_request):
1292
        _ProtocolThreeEncoder.__init__(self, medium_request.accept_bytes)
1293
        self._medium_request = medium_request
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
1294
        self._headers = {}
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1295
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
1296
    def set_headers(self, headers):
3245.4.46 by Andrew Bennetts
Apply John's review comments.
1297
        self._headers = headers.copy()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1298
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
1299
    def call(self, *args):
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1300
        if 'hpss' in debug.debug_flags:
1301
            mutter('hpss call:   %s', repr(args)[1:-1])
1302
            base = getattr(self._medium_request._medium, 'base', None)
1303
            if base is not None:
1304
                mutter('             (to %s)', base)
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1305
            self._request_start_time = osutils.timer_func()
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1306
        self._write_protocol_version()
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
1307
        self._write_headers(self._headers)
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1308
        self._write_structure(args)
1309
        self._write_end()
1310
        self._medium_request.finished_writing()
1311
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
1312
    def call_with_body_bytes(self, args, body):
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1313
        """Make a remote call of args with body bytes 'body'.
1314
1315
        After calling this, call read_response_tuple to find the result out.
1316
        """
1317
        if 'hpss' in debug.debug_flags:
3842.3.2 by Andrew Bennetts
Revert the RemoteVersionedFiles.get_parent_map implementation, leaving just the skeleton of RemoteVersionedFiles.
1318
            mutter('hpss call w/body: %s (%r...)', repr(args)[1:-1], body[:20])
3245.4.3 by Andrew Bennetts
Fix crash in -Dhpss.
1319
            path = getattr(self._medium_request._medium, '_path', None)
1320
            if path is not None:
1321
                mutter('                  (to %s)', path)
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1322
            mutter('              %d bytes', len(body))
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1323
            self._request_start_time = osutils.timer_func()
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1324
        self._write_protocol_version()
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
1325
        self._write_headers(self._headers)
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1326
        self._write_structure(args)
1327
        self._write_prefixed_body(body)
1328
        self._write_end()
3195.3.18 by Andrew Bennetts
call_with_body_bytes now works with v3 (e.g. test_copy_content_remote_to_local passes). Lots of debugging cruft, though.
1329
        self._medium_request.finished_writing()
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1330
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
1331
    def call_with_body_readv_array(self, args, body):
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1332
        """Make a remote call with a readv array.
1333
1334
        The body is encoded with one line per readv offset pair. The numbers in
1335
        each pair are separated by a comma, and no trailing \n is emitted.
1336
        """
1337
        if 'hpss' in debug.debug_flags:
1338
            mutter('hpss call w/readv: %s', repr(args)[1:-1])
3245.4.3 by Andrew Bennetts
Fix crash in -Dhpss.
1339
            path = getattr(self._medium_request._medium, '_path', None)
1340
            if path is not None:
1341
                mutter('                  (to %s)', path)
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1342
            self._request_start_time = osutils.timer_func()
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1343
        self._write_protocol_version()
3245.4.42 by Andrew Bennetts
Make _SmartClient automatically detect and use the highest protocol version compatible with the server.
1344
        self._write_headers(self._headers)
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1345
        self._write_structure(args)
1346
        readv_bytes = self._serialise_offsets(body)
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1347
        if 'hpss' in debug.debug_flags:
1348
            mutter('              %d bytes in readv request', len(readv_bytes))
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1349
        self._write_prefixed_body(readv_bytes)
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1350
        self._write_end()
1351
        self._medium_request.finished_writing()
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
1352
3842.3.4 by Andrew Bennetts
TestStacking.test_fetch_copies_from_stacked_on now passes using the VersionedFile.insert_record_stream RPC; lots of debugging cruft needs removal though.
1353
    def call_with_body_stream(self, args, stream):
1354
        if 'hpss' in debug.debug_flags:
1355
            mutter('hpss call w/body stream: %r', args)
1356
            path = getattr(self._medium_request._medium, '_path', None)
1357
            if path is not None:
1358
                mutter('                  (to %s)', path)
4889.2.1 by John Arbash Meinel
Make -Dhpss log debug information for the server process.
1359
            self._request_start_time = osutils.timer_func()
3842.3.4 by Andrew Bennetts
TestStacking.test_fetch_copies_from_stacked_on now passes using the VersionedFile.insert_record_stream RPC; lots of debugging cruft needs removal though.
1360
        self._write_protocol_version()
1361
        self._write_headers(self._headers)
1362
        self._write_structure(args)
3923.5.5 by Andrew Bennetts
Cleanly abort the request if an error occurs while iterating a body stream.
1363
        # TODO: notice if the server has sent an early error reply before we
1364
        #       have finished sending the stream.  We would notice at the end
1365
        #       anyway, but if the medium can deliver it early then it's good
1366
        #       to short-circuit the whole request...
4064.1.3 by Andrew Bennetts
Fix bug in _iter_with_errors; it should not report StopIteration as an exception.
1367
        for exc_info, part in _iter_with_errors(stream):
1368
            if exc_info is not None:
1369
                # Iterating the stream failed.  Cleanly abort the request.
1370
                self._write_error_status()
1371
                # Currently the client unconditionally sends ('error',) as the
1372
                # error args.
1373
                self._write_structure(('error',))
1374
                self._write_end()
1375
                self._medium_request.finished_writing()
1376
                raise exc_info[0], exc_info[1], exc_info[2]
1377
            else:
3923.5.5 by Andrew Bennetts
Cleanly abort the request if an error occurs while iterating a body stream.
1378
                self._write_prefixed_body(part)
1379
                self.flush()
3842.3.4 by Andrew Bennetts
TestStacking.test_fetch_copies_from_stacked_on now passes using the VersionedFile.insert_record_stream RPC; lots of debugging cruft needs removal though.
1380
        self._write_end()
1381
        self._medium_request.finished_writing()
1382