/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
1
# Copyright (C) 2006 Canonical Ltd
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
2018.5.19 by Andrew Bennetts
Add docstrings to all the new modules, and a few other places.
17
"""The 'medium' layer for the smart servers and clients.
18
19
"Medium" here is the noun meaning "a means of transmission", not the adjective
20
for "the quality between big and small."
21
22
Media carry the bytes of the requests somehow (e.g. via TCP, wrapped in HTTP, or
23
over SSH), and pass them to and from the protocol logic.  See the overview in
24
bzrlib/transport/smart/__init__.py.
25
"""
26
3750.1.2 by Vincent Ladeuil
Fixed as per Andrew's review.
27
import errno
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
28
import os
29
import socket
2018.5.162 by Andrew Bennetts
Add some missing _ensure_real calls, and a missing import.
30
import sys
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
31
import urllib
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
32
3530.1.1 by John Arbash Meinel
Make bzrlib.smart use lazy imports.
33
from bzrlib.lazy_import import lazy_import
34
lazy_import(globals(), """
3731.2.4 by Andrew Bennetts
Minor tweaks.
35
import atexit
36
import weakref
1551.18.17 by Aaron Bentley
Introduce bzr_remote_path configuration variable
37
from bzrlib import (
3731.2.1 by Andrew Bennetts
Show total HPSS calls (if any) on stderr when -Dhpss is active.
38
    debug,
1551.18.17 by Aaron Bentley
Introduce bzr_remote_path configuration variable
39
    errors,
3118.2.1 by Andrew Bennetts
(andrew) Fix #115781 by passing no more than 64k at a time to socket.sendall.
40
    osutils,
1551.18.17 by Aaron Bentley
Introduce bzr_remote_path configuration variable
41
    symbol_versioning,
3731.2.5 by Andrew Bennetts
Rework hpss call counter.
42
    trace,
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
43
    urlutils,
1551.18.17 by Aaron Bentley
Introduce bzr_remote_path configuration variable
44
    )
3731.2.1 by Andrew Bennetts
Show total HPSS calls (if any) on stderr when -Dhpss is active.
45
from bzrlib.smart import client, protocol
3066.2.1 by John Arbash Meinel
We don't require paramiko for bzr+ssh.
46
from bzrlib.transport import ssh
3530.1.1 by John Arbash Meinel
Make bzrlib.smart use lazy imports.
47
""")
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
48
2018.5.17 by Andrew Bennetts
Paramaterise the commands handled by SmartServerRequestHandler.
49
3565.1.3 by Andrew Bennetts
Define a _MAX_READ_SIZE constant as suggested by John's review.
50
# We must not read any more than 64k at a time so we don't risk "no buffer
51
# space available" errors on some platforms.  Windows in particular is likely
52
# to give error 10053 or 10055 if we read more than 64k from a socket.
53
_MAX_READ_SIZE = 64 * 1024
54
55
3245.4.16 by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py
56
def _get_protocol_factory_for_bytes(bytes):
57
    """Determine the right protocol factory for 'bytes'.
58
59
    This will return an appropriate protocol factory depending on the version
60
    of the protocol being used, as determined by inspecting the given bytes.
61
    The bytes should have at least one newline byte (i.e. be a whole line),
62
    otherwise it's possible that a request will be incorrectly identified as
63
    version 1.
64
65
    Typical use would be::
66
67
         factory, unused_bytes = _get_protocol_factory_for_bytes(bytes)
68
         server_protocol = factory(transport, write_func, root_client_path)
69
         server_protocol.accept_bytes(unused_bytes)
70
71
    :param bytes: a str of bytes of the start of the request.
72
    :returns: 2-tuple of (protocol_factory, unused_bytes).  protocol_factory is
73
        a callable that takes three args: transport, write_func,
74
        root_client_path.  unused_bytes are any bytes that were not part of a
75
        protocol version marker.
76
    """
3530.1.1 by John Arbash Meinel
Make bzrlib.smart use lazy imports.
77
    if bytes.startswith(protocol.MESSAGE_VERSION_THREE):
78
        protocol_factory = protocol.build_server_protocol_three
79
        bytes = bytes[len(protocol.MESSAGE_VERSION_THREE):]
80
    elif bytes.startswith(protocol.REQUEST_VERSION_TWO):
81
        protocol_factory = protocol.SmartServerRequestProtocolTwo
82
        bytes = bytes[len(protocol.REQUEST_VERSION_TWO):]
3245.4.16 by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py
83
    else:
3530.1.1 by John Arbash Meinel
Make bzrlib.smart use lazy imports.
84
        protocol_factory = protocol.SmartServerRequestProtocolOne
3245.4.16 by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py
85
    return protocol_factory, bytes
86
87
3606.4.1 by Andrew Bennetts
Fix NotImplementedError when probing for smart protocol via HTTP.
88
def _get_line(read_bytes_func):
89
    """Read bytes using read_bytes_func until a newline byte.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
90
3606.4.1 by Andrew Bennetts
Fix NotImplementedError when probing for smart protocol via HTTP.
91
    This isn't particularly efficient, so should only be used when the
92
    expected size of the line is quite short.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
93
3606.4.1 by Andrew Bennetts
Fix NotImplementedError when probing for smart protocol via HTTP.
94
    :returns: a tuple of two strs: (line, excess)
95
    """
96
    newline_pos = -1
97
    bytes = ''
98
    while newline_pos == -1:
99
        new_bytes = read_bytes_func(1)
100
        bytes += new_bytes
101
        if new_bytes == '':
102
            # Ran out of bytes before receiving a complete line.
103
            return bytes, ''
104
        newline_pos = bytes.find('\n')
105
    line = bytes[:newline_pos+1]
106
    excess = bytes[newline_pos+1:]
107
    return line, excess
108
109
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
110
class SmartMedium(object):
111
    """Base class for smart protocol media, both client- and server-side."""
112
113
    def __init__(self):
114
        self._push_back_buffer = None
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
115
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
116
    def _push_back(self, bytes):
117
        """Return unused bytes to the medium, because they belong to the next
118
        request(s).
119
120
        This sets the _push_back_buffer to the given bytes.
121
        """
122
        if self._push_back_buffer is not None:
123
            raise AssertionError(
124
                "_push_back called when self._push_back_buffer is %r"
125
                % (self._push_back_buffer,))
126
        if bytes == '':
127
            return
128
        self._push_back_buffer = bytes
129
130
    def _get_push_back_buffer(self):
131
        if self._push_back_buffer == '':
132
            raise AssertionError(
133
                '%s._push_back_buffer should never be the empty string, '
134
                'which can be confused with EOF' % (self,))
135
        bytes = self._push_back_buffer
136
        self._push_back_buffer = None
137
        return bytes
138
139
    def read_bytes(self, desired_count):
3565.1.2 by Andrew Bennetts
Delete some more code, fix some bugs, add more comments.
140
        """Read some bytes from this medium.
141
142
        :returns: some bytes, possibly more or less than the number requested
143
            in 'desired_count' depending on the medium.
144
        """
145
        if self._push_back_buffer is not None:
146
            return self._get_push_back_buffer()
3565.1.3 by Andrew Bennetts
Define a _MAX_READ_SIZE constant as suggested by John's review.
147
        bytes_to_read = min(desired_count, _MAX_READ_SIZE)
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
148
        return self._read_bytes(bytes_to_read)
149
150
    def _read_bytes(self, count):
151
        raise NotImplementedError(self._read_bytes)
152
153
    def _get_line(self):
154
        """Read bytes from this request's response until a newline byte.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
155
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
156
        This isn't particularly efficient, so should only be used when the
157
        expected size of the line is quite short.
158
159
        :returns: a string of bytes ending in a newline (byte 0x0A).
160
        """
3606.4.1 by Andrew Bennetts
Fix NotImplementedError when probing for smart protocol via HTTP.
161
        line, excess = _get_line(self.read_bytes)
162
        self._push_back(excess)
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
163
        return line
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
164
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
165
166
class SmartServerStreamMedium(SmartMedium):
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
167
    """Handles smart commands coming over a stream.
168
169
    The stream may be a pipe connected to sshd, or a tcp socket, or an
170
    in-process fifo for testing.
171
172
    One instance is created for each connected client; it can serve multiple
173
    requests in the lifetime of the connection.
174
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
175
    The server passes requests through to an underlying backing transport,
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
176
    which will typically be a LocalTransport looking at the server's filesystem.
3236.3.4 by Andrew Bennetts
Rename 'push_back' attribute to '_push_back_buffer', add some docstrings, and remove a little bit of redundant code from SmartServerSocketStreamMedium._serve_one_request_unguarded.
177
178
    :ivar _push_back_buffer: a str of bytes that have been read from the stream
179
        but not used yet, or None if there are no buffered bytes.  Subclasses
180
        should make sure to exhaust this buffer before reading more bytes from
181
        the stream.  See also the _push_back method.
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
182
    """
183
2692.1.11 by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured.
184
    def __init__(self, backing_transport, root_client_path='/'):
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
185
        """Construct new server.
186
187
        :param backing_transport: Transport for the directory served.
188
        """
189
        # backing_transport could be passed to serve instead of __init__
190
        self.backing_transport = backing_transport
2692.1.11 by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured.
191
        self.root_client_path = root_client_path
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
192
        self.finished = False
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
193
        SmartMedium.__init__(self)
3236.3.5 by Andrew Bennetts
Add _get_push_back_buffer helper.
194
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
195
    def serve(self):
196
        """Serve requests until the client disconnects."""
197
        # Keep a reference to stderr because the sys module's globals get set to
198
        # None during interpreter shutdown.
199
        from sys import stderr
200
        try:
201
            while not self.finished:
2432.2.3 by Andrew Bennetts
Merge from bzr.dev.
202
                server_protocol = self._build_protocol()
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
203
                self._serve_one_request(server_protocol)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
204
        except Exception, e:
205
            stderr.write("%s terminating on exception %s\n" % (self, e))
206
            raise
207
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
208
    def _build_protocol(self):
2432.2.8 by Andrew Bennetts
NEWS entry, greatly improved docstring in bzrlib.smart.
209
        """Identifies the version of the incoming request, and returns an
210
        a protocol object that can interpret it.
211
212
        If more bytes than the version prefix of the request are read, they will
213
        be fed into the protocol before it is returned.
214
215
        :returns: a SmartServerRequestProtocol.
216
        """
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
217
        bytes = self._get_line()
3245.4.16 by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py
218
        protocol_factory, unused_bytes = _get_protocol_factory_for_bytes(bytes)
3245.4.14 by Andrew Bennetts
Merge from bzr.dev (via loom thread).
219
        protocol = protocol_factory(
2692.1.11 by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured.
220
            self.backing_transport, self._write_out, self.root_client_path)
3245.4.16 by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py
221
        protocol.accept_bytes(unused_bytes)
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
222
        return protocol
223
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
224
    def _serve_one_request(self, protocol):
225
        """Read one request from input, process, send back a response.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
226
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
227
        :param protocol: a SmartServerRequestProtocol.
228
        """
229
        try:
230
            self._serve_one_request_unguarded(protocol)
231
        except KeyboardInterrupt:
232
            raise
233
        except Exception, e:
234
            self.terminate_due_to_error()
235
236
    def terminate_due_to_error(self):
237
        """Called when an unhandled exception from the protocol occurs."""
238
        raise NotImplementedError(self.terminate_due_to_error)
239
3565.1.2 by Andrew Bennetts
Delete some more code, fix some bugs, add more comments.
240
    def _read_bytes(self, desired_count):
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
241
        """Get some bytes from the medium.
242
243
        :param desired_count: number of bytes we want to read.
244
        """
3565.1.2 by Andrew Bennetts
Delete some more code, fix some bugs, add more comments.
245
        raise NotImplementedError(self._read_bytes)
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
246
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
247
248
class SmartServerSocketStreamMedium(SmartServerStreamMedium):
249
2692.1.11 by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured.
250
    def __init__(self, sock, backing_transport, root_client_path='/'):
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
251
        """Constructor.
252
253
        :param sock: the socket the server will read from.  It will be put
254
            into blocking mode.
255
        """
2692.1.11 by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured.
256
        SmartServerStreamMedium.__init__(
257
            self, backing_transport, root_client_path=root_client_path)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
258
        sock.setblocking(True)
259
        self.socket = sock
260
261
    def _serve_one_request_unguarded(self, protocol):
262
        while protocol.next_read_size():
3565.1.3 by Andrew Bennetts
Define a _MAX_READ_SIZE constant as suggested by John's review.
263
            # We can safely try to read large chunks.  If there is less data
264
            # than _MAX_READ_SIZE ready, the socket wil just return a short
265
            # read immediately rather than block.
266
            bytes = self.read_bytes(_MAX_READ_SIZE)
3236.3.4 by Andrew Bennetts
Rename 'push_back' attribute to '_push_back_buffer', add some docstrings, and remove a little bit of redundant code from SmartServerSocketStreamMedium._serve_one_request_unguarded.
267
            if bytes == '':
268
                self.finished = True
269
                return
270
            protocol.accept_bytes(bytes)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
271
3245.4.21 by Andrew Bennetts
Remove 'excess_buffer' attribute and another crufty comment.
272
        self._push_back(protocol.unused_data)
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.
273
3565.1.2 by Andrew Bennetts
Delete some more code, fix some bugs, add more comments.
274
    def _read_bytes(self, desired_count):
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
275
        # We ignore the desired_count because on sockets it's more efficient to
3565.1.3 by Andrew Bennetts
Define a _MAX_READ_SIZE constant as suggested by John's review.
276
        # read large chunks (of _MAX_READ_SIZE bytes) at a time.
3923.3.1 by Andrew Bennetts
Quick attempt at adding some EINTR-proofing to smart protocol code.
277
        return osutils.until_no_eintr(self.socket.recv, _MAX_READ_SIZE)
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
278
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
279
    def terminate_due_to_error(self):
3245.4.59 by Andrew Bennetts
Various tweaks in response to Martin's review.
280
        # TODO: This should log to a server log file, but no such thing
281
        # exists yet.  Andrew Bennetts 2006-09-29.
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
282
        self.socket.close()
283
        self.finished = True
284
285
    def _write_out(self, bytes):
3118.2.1 by Andrew Bennetts
(andrew) Fix #115781 by passing no more than 64k at a time to socket.sendall.
286
        osutils.send_all(self.socket, bytes)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
287
288
289
class SmartServerPipeStreamMedium(SmartServerStreamMedium):
290
291
    def __init__(self, in_file, out_file, backing_transport):
292
        """Construct new server.
293
294
        :param in_file: Python file from which requests can be read.
295
        :param out_file: Python file to write responses.
296
        :param backing_transport: Transport for the directory served.
297
        """
298
        SmartServerStreamMedium.__init__(self, backing_transport)
2018.5.161 by Andrew Bennetts
Reinstate forcing binary mode on windows in SmartServerStreamMedium.
299
        if sys.platform == 'win32':
300
            # force binary mode for files
301
            import msvcrt
302
            for f in (in_file, out_file):
303
                fileno = getattr(f, 'fileno', None)
304
                if fileno:
305
                    msvcrt.setmode(fileno(), os.O_BINARY)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
306
        self._in = in_file
307
        self._out = out_file
308
309
    def _serve_one_request_unguarded(self, protocol):
310
        while True:
3565.1.2 by Andrew Bennetts
Delete some more code, fix some bugs, add more comments.
311
            # We need to be careful not to read past the end of the current
312
            # request, or else the read from the pipe will block, so we use
313
            # protocol.next_read_size().
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
314
            bytes_to_read = protocol.next_read_size()
315
            if bytes_to_read == 0:
316
                # Finished serving this request.
317
                self._out.flush()
318
                return
3565.1.2 by Andrew Bennetts
Delete some more code, fix some bugs, add more comments.
319
            bytes = self.read_bytes(bytes_to_read)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
320
            if bytes == '':
321
                # Connection has been closed.
322
                self.finished = True
323
                self._out.flush()
324
                return
325
            protocol.accept_bytes(bytes)
326
3565.1.2 by Andrew Bennetts
Delete some more code, fix some bugs, add more comments.
327
    def _read_bytes(self, desired_count):
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
328
        return self._in.read(desired_count)
329
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
330
    def terminate_due_to_error(self):
331
        # TODO: This should log to a server log file, but no such thing
332
        # exists yet.  Andrew Bennetts 2006-09-29.
333
        self._out.close()
334
        self.finished = True
335
336
    def _write_out(self, bytes):
337
        self._out.write(bytes)
338
339
3565.1.2 by Andrew Bennetts
Delete some more code, fix some bugs, add more comments.
340
class SmartClientMediumRequest(object):
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
341
    """A request on a SmartClientMedium.
342
343
    Each request allows bytes to be provided to it via accept_bytes, and then
344
    the response bytes to be read via read_bytes.
345
346
    For instance:
347
    request.accept_bytes('123')
348
    request.finished_writing()
349
    result = request.read_bytes(3)
350
    request.finished_reading()
351
352
    It is up to the individual SmartClientMedium whether multiple concurrent
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
353
    requests can exist. See SmartClientMedium.get_request to obtain instances
354
    of SmartClientMediumRequest, and the concrete Medium you are using for
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
355
    details on concurrency and pipelining.
356
    """
357
358
    def __init__(self, medium):
359
        """Construct a SmartClientMediumRequest for the medium medium."""
360
        self._medium = medium
361
        # we track state by constants - we may want to use the same
362
        # pattern as BodyReader if it gets more complex.
363
        # valid states are: "writing", "reading", "done"
364
        self._state = "writing"
365
366
    def accept_bytes(self, bytes):
367
        """Accept bytes for inclusion in this request.
368
369
        This method may not be be called after finished_writing() has been
370
        called.  It depends upon the Medium whether or not the bytes will be
371
        immediately transmitted. Message based Mediums will tend to buffer the
372
        bytes until finished_writing() is called.
373
374
        :param bytes: A bytestring.
375
        """
376
        if self._state != "writing":
377
            raise errors.WritingCompleted(self)
378
        self._accept_bytes(bytes)
379
380
    def _accept_bytes(self, bytes):
381
        """Helper for accept_bytes.
382
383
        Accept_bytes checks the state of the request to determing if bytes
384
        should be accepted. After that it hands off to _accept_bytes to do the
385
        actual acceptance.
386
        """
387
        raise NotImplementedError(self._accept_bytes)
388
389
    def finished_reading(self):
390
        """Inform the request that all desired data has been read.
391
392
        This will remove the request from the pipeline for its medium (if the
393
        medium supports pipelining) and any further calls to methods on the
394
        request will raise ReadingCompleted.
395
        """
396
        if self._state == "writing":
397
            raise errors.WritingNotComplete(self)
398
        if self._state != "reading":
399
            raise errors.ReadingCompleted(self)
400
        self._state = "done"
401
        self._finished_reading()
402
403
    def _finished_reading(self):
404
        """Helper for finished_reading.
405
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
406
        finished_reading checks the state of the request to determine if
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
407
        finished_reading is allowed, and if it is hands off to _finished_reading
408
        to perform the action.
409
        """
410
        raise NotImplementedError(self._finished_reading)
411
412
    def finished_writing(self):
413
        """Finish the writing phase of this request.
414
415
        This will flush all pending data for this request along the medium.
416
        After calling finished_writing, you may not call accept_bytes anymore.
417
        """
418
        if self._state != "writing":
419
            raise errors.WritingCompleted(self)
420
        self._state = "reading"
421
        self._finished_writing()
422
423
    def _finished_writing(self):
424
        """Helper for finished_writing.
425
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
426
        finished_writing checks the state of the request to determine if
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
427
        finished_writing is allowed, and if it is hands off to _finished_writing
428
        to perform the action.
429
        """
430
        raise NotImplementedError(self._finished_writing)
431
432
    def read_bytes(self, count):
433
        """Read bytes from this requests response.
434
435
        This method will block and wait for count bytes to be read. It may not
436
        be invoked until finished_writing() has been called - this is to ensure
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
437
        a message-based approach to requests, for compatibility with message
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
438
        based mediums like HTTP.
439
        """
440
        if self._state == "writing":
441
            raise errors.WritingNotComplete(self)
442
        if self._state != "reading":
443
            raise errors.ReadingCompleted(self)
444
        return self._read_bytes(count)
445
446
    def _read_bytes(self, count):
3565.1.2 by Andrew Bennetts
Delete some more code, fix some bugs, add more comments.
447
        """Helper for SmartClientMediumRequest.read_bytes.
448
449
        read_bytes checks the state of the request to determing if bytes
450
        should be read. After that it hands off to _read_bytes to do the
451
        actual read.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
452
3565.1.2 by Andrew Bennetts
Delete some more code, fix some bugs, add more comments.
453
        By default this forwards to self._medium.read_bytes because we are
454
        operating on the medium's stream.
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
455
        """
3565.1.2 by Andrew Bennetts
Delete some more code, fix some bugs, add more comments.
456
        return self._medium.read_bytes(count)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
457
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
458
    def read_line(self):
3606.4.1 by Andrew Bennetts
Fix NotImplementedError when probing for smart protocol via HTTP.
459
        line = self._read_line()
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
460
        if not line.endswith('\n'):
461
            # end of file encountered reading from server
462
            raise errors.ConnectionReset(
4070.8.1 by Martin Pool
Remove 'try -Dhpss' from error messages
463
                "please check connectivity and permissions")
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
464
        return line
465
3606.4.1 by Andrew Bennetts
Fix NotImplementedError when probing for smart protocol via HTTP.
466
    def _read_line(self):
467
        """Helper for SmartClientMediumRequest.read_line.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
468
3606.4.1 by Andrew Bennetts
Fix NotImplementedError when probing for smart protocol via HTTP.
469
        By default this forwards to self._medium._get_line because we are
470
        operating on the medium's stream.
471
        """
472
        return self._medium._get_line()
473
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
474
3731.2.5 by Andrew Bennetts
Rework hpss call counter.
475
class _DebugCounter(object):
476
    """An object that counts the HPSS calls made to each client medium.
477
478
    When a medium is garbage-collected, or failing that when atexit functions
479
    are run, the total number of calls made on that medium are reported via
480
    trace.note.
3731.2.1 by Andrew Bennetts
Show total HPSS calls (if any) on stderr when -Dhpss is active.
481
    """
482
3731.2.5 by Andrew Bennetts
Rework hpss call counter.
483
    def __init__(self):
484
        self.counts = weakref.WeakKeyDictionary()
485
        client._SmartClient.hooks.install_named_hook(
486
            'call', self.increment_call_count, 'hpss call counter')
487
        atexit.register(self.flush_all)
488
489
    def track(self, medium):
490
        """Start tracking calls made to a medium.
491
492
        This only keeps a weakref to the medium, so shouldn't affect the
493
        medium's lifetime.
494
        """
495
        medium_repr = repr(medium)
496
        # Add this medium to the WeakKeyDictionary
497
        self.counts[medium] = [0, medium_repr]
498
        # Weakref callbacks are fired in reverse order of their association
499
        # with the referenced object.  So we add a weakref *after* adding to
500
        # the WeakKeyDict so that we can report the value from it before the
501
        # entry is removed by the WeakKeyDict's own callback.
502
        ref = weakref.ref(medium, self.done)
503
504
    def increment_call_count(self, params):
505
        # Increment the count in the WeakKeyDictionary
506
        value = self.counts[params.medium]
507
        value[0] += 1
3731.2.1 by Andrew Bennetts
Show total HPSS calls (if any) on stderr when -Dhpss is active.
508
509
    def done(self, ref):
3731.2.5 by Andrew Bennetts
Rework hpss call counter.
510
        value = self.counts[ref]
511
        count, medium_repr = value
512
        # In case this callback is invoked for the same ref twice (by the
513
        # weakref callback and by the atexit function), set the call count back
514
        # to 0 so this item won't be reported twice.
515
        value[0] = 0
516
        if count != 0:
517
            trace.note('HPSS calls: %d %s', count, medium_repr)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
518
3731.2.5 by Andrew Bennetts
Rework hpss call counter.
519
    def flush_all(self):
520
        for ref in list(self.counts.keys()):
521
            self.done(ref)
522
523
_debug_counter = None
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
524
525
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
526
class SmartClientMedium(SmartMedium):
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
527
    """Smart client is a medium for sending smart protocol requests over."""
528
3431.3.3 by Andrew Bennetts
Set 'base' in SmartClientMedium base class.
529
    def __init__(self, base):
3241.1.1 by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium.
530
        super(SmartClientMedium, self).__init__()
3431.3.3 by Andrew Bennetts
Set 'base' in SmartClientMedium base class.
531
        self.base = base
3241.1.4 by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout.
532
        self._protocol_version_error = None
3241.1.1 by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium.
533
        self._protocol_version = None
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).
534
        self._done_hello = False
3435.1.1 by Andrew Bennetts
Define _remote_is_at_least_1_2 on SmartClientMedium base class, rather than just SmartClientStreamMedium.
535
        # Be optimistic: we assume the remote end can accept new remote
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
536
        # requests until we get an error saying otherwise.
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
537
        # _remote_version_is_before tracks the bzr version the remote side
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
538
        # can be based on what we've seen so far.
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
539
        self._remote_version_is_before = None
3731.2.5 by Andrew Bennetts
Rework hpss call counter.
540
        # Install debug hook function if debug flag is set.
3731.2.1 by Andrew Bennetts
Show total HPSS calls (if any) on stderr when -Dhpss is active.
541
        if 'hpss' in debug.debug_flags:
3731.2.5 by Andrew Bennetts
Rework hpss call counter.
542
            global _debug_counter
543
            if _debug_counter is None:
544
                _debug_counter = _DebugCounter()
545
            _debug_counter.track(self)
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
546
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
547
    def _is_remote_before(self, version_tuple):
3502.1.1 by Matt Nordhoff
Fix a docstring typo, and a two-expression ``raise`` statement
548
        """Is it possible the remote side supports RPCs for a given version?
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
549
550
        Typical use::
551
552
            needed_version = (1, 2)
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
553
            if medium._is_remote_before(needed_version):
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
554
                fallback_to_pre_1_2_rpc()
555
            else:
556
                try:
557
                    do_1_2_rpc()
558
                except UnknownSmartMethod:
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
559
                    medium._remember_remote_is_before(needed_version)
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
560
                    fallback_to_pre_1_2_rpc()
561
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
562
        :seealso: _remember_remote_is_before
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
563
        """
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
564
        if self._remote_version_is_before is None:
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
565
            # So far, the remote side seems to support everything
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
566
            return False
567
        return version_tuple >= self._remote_version_is_before
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
568
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
569
    def _remember_remote_is_before(self, version_tuple):
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
570
        """Tell this medium that the remote side is older the given version.
571
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
572
        :seealso: _is_remote_before
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
573
        """
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
574
        if (self._remote_version_is_before is not None and
575
            version_tuple > self._remote_version_is_before):
4017.3.3 by Robert Collins
Review feedback - make RemoteRepository.initialize use helpers, and version-lock the new method to not attempt the method on older servers.
576
            # We have been told that the remote side is older than some version
577
            # which is newer than a previously supplied older-than version.
578
            # This indicates that some smart verb call is not guarded
579
            # appropriately (it should simply not have been tried).
3502.1.1 by Matt Nordhoff
Fix a docstring typo, and a two-expression ``raise`` statement
580
            raise AssertionError(
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
581
                "_remember_remote_is_before(%r) called, but "
582
                "_remember_remote_is_before(%r) was called previously."
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
583
                % (version_tuple, self._remote_version_is_before))
584
        self._remote_version_is_before = version_tuple
3241.1.1 by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium.
585
586
    def protocol_version(self):
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).
587
        """Find out if 'hello' smart request works."""
3241.1.4 by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout.
588
        if self._protocol_version_error is not None:
589
            raise self._protocol_version_error
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).
590
        if not self._done_hello:
3241.1.4 by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout.
591
            try:
592
                medium_request = self.get_request()
593
                # Send a 'hello' request in protocol version one, for maximum
594
                # backwards compatibility.
3530.1.2 by John Arbash Meinel
missed one of the imports
595
                client_protocol = protocol.SmartClientRequestProtocolOne(medium_request)
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).
596
                client_protocol.query_version()
597
                self._done_hello = True
3241.1.4 by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout.
598
            except errors.SmartProtocolError, e:
599
                # Cache the error, just like we would cache a successful
600
                # result.
601
                self._protocol_version_error = e
602
                raise
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).
603
        return '2'
604
605
    def should_probe(self):
606
        """Should RemoteBzrDirFormat.probe_transport send a smart request on
607
        this medium?
608
609
        Some transports are unambiguously smart-only; there's no need to check
610
        if the transport is able to carry smart requests, because that's all
611
        it is for.  In those cases, this method should return False.
612
613
        But some HTTP transports can sometimes fail to carry smart requests,
614
        but still be usuable for accessing remote bzrdirs via plain file
615
        accesses.  So for those transports, their media should return True here
616
        so that RemoteBzrDirFormat can determine if it is appropriate for that
617
        transport.
618
        """
619
        return False
3241.1.1 by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium.
620
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
621
    def disconnect(self):
622
        """If this medium maintains a persistent connection, close it.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
623
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
624
        The default implementation does nothing.
625
        """
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
626
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
627
    def remote_path_from_transport(self, transport):
628
        """Convert transport into a path suitable for using in a request.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
629
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
630
        Note that the resulting remote path doesn't encode the host name or
631
        anything but path, so it is only safe to use it in requests sent over
632
        the medium from the matching transport.
633
        """
634
        medium_base = urlutils.join(self.base, '/')
635
        rel_url = urlutils.relative_url(medium_base, transport.base)
636
        return urllib.unquote(rel_url)
637
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
638
639
class SmartClientStreamMedium(SmartClientMedium):
640
    """Stream based medium common class.
641
642
    SmartClientStreamMediums operate on a stream. All subclasses use a common
643
    SmartClientStreamMediumRequest for their requests, and should implement
644
    _accept_bytes and _read_bytes to allow the request objects to send and
645
    receive bytes.
646
    """
647
3431.3.3 by Andrew Bennetts
Set 'base' in SmartClientMedium base class.
648
    def __init__(self, base):
649
        SmartClientMedium.__init__(self, base)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
650
        self._current_request = None
651
652
    def accept_bytes(self, bytes):
653
        self._accept_bytes(bytes)
654
655
    def __del__(self):
656
        """The SmartClientStreamMedium knows how to close the stream when it is
657
        finished with it.
658
        """
659
        self.disconnect()
660
661
    def _flush(self):
662
        """Flush the output stream.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
663
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
664
        This method is used by the SmartClientStreamMediumRequest to ensure that
665
        all data for a request is sent, to avoid long timeouts or deadlocks.
666
        """
667
        raise NotImplementedError(self._flush)
668
669
    def get_request(self):
670
        """See SmartClientMedium.get_request().
671
672
        SmartClientStreamMedium always returns a SmartClientStreamMediumRequest
673
        for get_request.
674
        """
675
        return SmartClientStreamMediumRequest(self)
676
677
678
class SmartSimplePipesClientMedium(SmartClientStreamMedium):
679
    """A client medium using simple pipes.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
680
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
681
    This client does not manage the pipes: it assumes they will always be open.
682
    """
683
3431.3.1 by Andrew Bennetts
First rough cut of a fix for bug #230550, by adding .base to SmartClientMedia rather than relying on other objects to track this accurately while reusing client media.
684
    def __init__(self, readable_pipe, writeable_pipe, base):
3431.3.3 by Andrew Bennetts
Set 'base' in SmartClientMedium base class.
685
        SmartClientStreamMedium.__init__(self, base)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
686
        self._readable_pipe = readable_pipe
687
        self._writeable_pipe = writeable_pipe
688
689
    def _accept_bytes(self, bytes):
690
        """See SmartClientStreamMedium.accept_bytes."""
691
        self._writeable_pipe.write(bytes)
692
693
    def _flush(self):
694
        """See SmartClientStreamMedium._flush()."""
695
        self._writeable_pipe.flush()
696
697
    def _read_bytes(self, count):
698
        """See SmartClientStreamMedium._read_bytes."""
699
        return self._readable_pipe.read(count)
700
701
702
class SmartSSHClientMedium(SmartClientStreamMedium):
703
    """A client medium using SSH."""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
704
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
705
    def __init__(self, host, port=None, username=None, password=None,
3431.3.1 by Andrew Bennetts
First rough cut of a fix for bug #230550, by adding .base to SmartClientMedia rather than relying on other objects to track this accurately while reusing client media.
706
            base=None, vendor=None, bzr_remote_path=None):
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
707
        """Creates a client that will connect on the first use.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
708
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
709
        :param vendor: An optional override for the ssh vendor to use. See
710
            bzrlib.transport.ssh for details on ssh vendors.
711
        """
3431.3.3 by Andrew Bennetts
Set 'base' in SmartClientMedium base class.
712
        SmartClientStreamMedium.__init__(self, base)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
713
        self._connected = False
714
        self._host = host
715
        self._password = password
716
        self._port = port
717
        self._username = username
718
        self._read_from = None
719
        self._ssh_connection = None
720
        self._vendor = vendor
721
        self._write_to = None
1551.18.17 by Aaron Bentley
Introduce bzr_remote_path configuration variable
722
        self._bzr_remote_path = bzr_remote_path
723
        if self._bzr_remote_path is None:
724
            symbol_versioning.warn(
725
                'bzr_remote_path is required as of bzr 0.92',
726
                DeprecationWarning, stacklevel=2)
727
            self._bzr_remote_path = os.environ.get('BZR_REMOTE_PATH', 'bzr')
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
728
729
    def _accept_bytes(self, bytes):
730
        """See SmartClientStreamMedium.accept_bytes."""
731
        self._ensure_connection()
732
        self._write_to.write(bytes)
733
734
    def disconnect(self):
735
        """See SmartClientMedium.disconnect()."""
736
        if not self._connected:
737
            return
738
        self._read_from.close()
739
        self._write_to.close()
740
        self._ssh_connection.close()
741
        self._connected = False
742
743
    def _ensure_connection(self):
744
        """Connect this medium if not already connected."""
745
        if self._connected:
746
            return
747
        if self._vendor is None:
748
            vendor = ssh._get_ssh_vendor()
749
        else:
750
            vendor = self._vendor
751
        self._ssh_connection = vendor.connect_ssh(self._username,
752
                self._password, self._host, self._port,
1551.18.17 by Aaron Bentley
Introduce bzr_remote_path configuration variable
753
                command=[self._bzr_remote_path, 'serve', '--inet',
754
                         '--directory=/', '--allow-writes'])
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
755
        self._read_from, self._write_to = \
756
            self._ssh_connection.get_filelike_channels()
757
        self._connected = True
758
759
    def _flush(self):
760
        """See SmartClientStreamMedium._flush()."""
761
        self._write_to.flush()
762
763
    def _read_bytes(self, count):
764
        """See SmartClientStreamMedium.read_bytes."""
765
        if not self._connected:
766
            raise errors.MediumNotConnected(self)
3565.1.3 by Andrew Bennetts
Define a _MAX_READ_SIZE constant as suggested by John's review.
767
        bytes_to_read = min(count, _MAX_READ_SIZE)
3565.1.1 by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code.
768
        return self._read_from.read(bytes_to_read)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
769
770
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
771
# Port 4155 is the default port for bzr://, registered with IANA.
3665.4.1 by Jelmer Vernooij
Support IPv6 in the smart server.
772
BZR_DEFAULT_INTERFACE = None
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
773
BZR_DEFAULT_PORT = 4155
774
775
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
776
class SmartTCPClientMedium(SmartClientStreamMedium):
777
    """A client medium using TCP."""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
778
3431.3.1 by Andrew Bennetts
First rough cut of a fix for bug #230550, by adding .base to SmartClientMedia rather than relying on other objects to track this accurately while reusing client media.
779
    def __init__(self, host, port, base):
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
780
        """Creates a client that will connect on the first use."""
3431.3.3 by Andrew Bennetts
Set 'base' in SmartClientMedium base class.
781
        SmartClientStreamMedium.__init__(self, base)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
782
        self._connected = False
783
        self._host = host
784
        self._port = port
785
        self._socket = None
786
787
    def _accept_bytes(self, bytes):
788
        """See SmartClientMedium.accept_bytes."""
789
        self._ensure_connection()
3118.2.1 by Andrew Bennetts
(andrew) Fix #115781 by passing no more than 64k at a time to socket.sendall.
790
        osutils.send_all(self._socket, bytes)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
791
792
    def disconnect(self):
793
        """See SmartClientMedium.disconnect()."""
794
        if not self._connected:
795
            return
796
        self._socket.close()
797
        self._socket = None
798
        self._connected = False
799
800
    def _ensure_connection(self):
801
        """Connect this medium if not already connected."""
802
        if self._connected:
803
            return
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
804
        if self._port is None:
805
            port = BZR_DEFAULT_PORT
806
        else:
807
            port = int(self._port)
3711.2.2 by Jelmer Vernooij
Avoid using AI_ADDRCONFIG since it's not portable.
808
        try:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
809
            sockaddrs = socket.getaddrinfo(self._host, port, socket.AF_UNSPEC,
3711.2.2 by Jelmer Vernooij
Avoid using AI_ADDRCONFIG since it's not portable.
810
                socket.SOCK_STREAM, 0, 0)
811
        except socket.gaierror, (err_num, err_msg):
812
            raise errors.ConnectionError("failed to lookup %s:%d: %s" %
813
                    (self._host, port, err_msg))
3711.2.3 by Jelmer Vernooij
Add comment.
814
        # Initialize err in case there are no addresses returned:
3665.4.2 by Jelmer Vernooij
Fall through to next available address if previous fails.
815
        err = socket.error("no address found for %s" % self._host)
3665.4.1 by Jelmer Vernooij
Support IPv6 in the smart server.
816
        for (family, socktype, proto, canonname, sockaddr) in sockaddrs:
817
            try:
3665.4.2 by Jelmer Vernooij
Fall through to next available address if previous fails.
818
                self._socket = socket.socket(family, socktype, proto)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
819
                self._socket.setsockopt(socket.IPPROTO_TCP,
3665.4.2 by Jelmer Vernooij
Fall through to next available address if previous fails.
820
                                        socket.TCP_NODELAY, 1)
3665.4.1 by Jelmer Vernooij
Support IPv6 in the smart server.
821
                self._socket.connect(sockaddr)
822
            except socket.error, err:
3665.4.2 by Jelmer Vernooij
Fall through to next available address if previous fails.
823
                if self._socket is not None:
824
                    self._socket.close()
825
                self._socket = None
826
                continue
827
            break
828
        if self._socket is None:
829
            # socket errors either have a (string) or (errno, string) as their
830
            # args.
831
            if type(err.args) is str:
832
                err_msg = err.args
833
            else:
834
                err_msg = err.args[1]
835
            raise errors.ConnectionError("failed to connect to %s:%d: %s" %
836
                    (self._host, port, err_msg))
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
837
        self._connected = True
838
839
    def _flush(self):
840
        """See SmartClientStreamMedium._flush().
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
841
842
        For TCP we do no flushing. We may want to turn off TCP_NODELAY and
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
843
        add a means to do a flush, but that can be done in the future.
844
        """
845
846
    def _read_bytes(self, count):
847
        """See SmartClientMedium.read_bytes."""
848
        if not self._connected:
849
            raise errors.MediumNotConnected(self)
3565.1.3 by Andrew Bennetts
Define a _MAX_READ_SIZE constant as suggested by John's review.
850
        # We ignore the desired_count because on sockets it's more efficient to
851
        # read large chunks (of _MAX_READ_SIZE bytes) at a time.
3750.1.2 by Vincent Ladeuil
Fixed as per Andrew's review.
852
        try:
853
            return self._socket.recv(_MAX_READ_SIZE)
854
        except socket.error, e:
855
            if len(e.args) and e.args[0] == errno.ECONNRESET:
856
                # Callers expect an empty string in that case
857
                return ''
858
            else:
859
                raise
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
860
861
862
class SmartClientStreamMediumRequest(SmartClientMediumRequest):
863
    """A SmartClientMediumRequest that works with an SmartClientStreamMedium."""
864
865
    def __init__(self, medium):
866
        SmartClientMediumRequest.__init__(self, medium)
867
        # check that we are safe concurrency wise. If some streams start
868
        # allowing concurrent requests - i.e. via multiplexing - then this
869
        # assert should be moved to SmartClientStreamMedium.get_request,
870
        # and the setting/unsetting of _current_request likewise moved into
871
        # that class : but its unneeded overhead for now. RBC 20060922
872
        if self._medium._current_request is not None:
873
            raise errors.TooManyConcurrentRequests(self._medium)
874
        self._medium._current_request = self
875
876
    def _accept_bytes(self, bytes):
877
        """See SmartClientMediumRequest._accept_bytes.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
878
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
879
        This forwards to self._medium._accept_bytes because we are operating
880
        on the mediums stream.
881
        """
882
        self._medium._accept_bytes(bytes)
883
884
    def _finished_reading(self):
885
        """See SmartClientMediumRequest._finished_reading.
886
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
887
        This clears the _current_request on self._medium to allow a new
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
888
        request to be created.
889
        """
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
890
        if self._medium._current_request is not self:
891
            raise AssertionError()
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
892
        self._medium._current_request = None
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
893
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
894
    def _finished_writing(self):
895
        """See SmartClientMediumRequest._finished_writing.
896
897
        This invokes self._medium._flush to ensure all bytes are transmitted.
898
        """
899
        self._medium._flush()
900