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 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
27 |
import os |
28 |
import socket |
|
2018.5.162
by Andrew Bennetts
Add some missing _ensure_real calls, and a missing import. |
29 |
import sys |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
30 |
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
31 |
from bzrlib import ( |
32 |
errors, |
|
3118.2.1
by Andrew Bennetts
(andrew) Fix #115781 by passing no more than 64k at a time to socket.sendall. |
33 |
osutils, |
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
34 |
symbol_versioning, |
35 |
)
|
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
36 |
from bzrlib.smart.protocol import ( |
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
37 |
REQUEST_VERSION_TWO, |
3241.1.1
by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium. |
38 |
SmartClientRequestProtocolOne, |
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
39 |
SmartServerRequestProtocolOne, |
40 |
SmartServerRequestProtocolTwo, |
|
41 |
)
|
|
3066.2.1
by John Arbash Meinel
We don't require paramiko for bzr+ssh. |
42 |
from bzrlib.transport import ssh |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
43 |
|
2018.5.17
by Andrew Bennetts
Paramaterise the commands handled by SmartServerRequestHandler. |
44 |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
45 |
class SmartServerStreamMedium(object): |
46 |
"""Handles smart commands coming over a stream.
|
|
47 |
||
48 |
The stream may be a pipe connected to sshd, or a tcp socket, or an
|
|
49 |
in-process fifo for testing.
|
|
50 |
||
51 |
One instance is created for each connected client; it can serve multiple
|
|
52 |
requests in the lifetime of the connection.
|
|
53 |
||
54 |
The server passes requests through to an underlying backing transport,
|
|
55 |
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. |
56 |
|
57 |
:ivar _push_back_buffer: a str of bytes that have been read from the stream
|
|
58 |
but not used yet, or None if there are no buffered bytes. Subclasses
|
|
59 |
should make sure to exhaust this buffer before reading more bytes from
|
|
60 |
the stream. See also the _push_back method.
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
61 |
"""
|
62 |
||
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. |
63 |
def __init__(self, backing_transport, root_client_path='/'): |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
64 |
"""Construct new server.
|
65 |
||
66 |
:param backing_transport: Transport for the directory served.
|
|
67 |
"""
|
|
68 |
# backing_transport could be passed to serve instead of __init__
|
|
69 |
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. |
70 |
self.root_client_path = root_client_path |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
71 |
self.finished = False |
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. |
72 |
self._push_back_buffer = None |
3236.3.2
by Andrew Bennetts
Fix SmartServerPipeStreamMedium._get_line too. |
73 |
|
74 |
def _push_back(self, bytes): |
|
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. |
75 |
"""Return unused bytes to the medium, because they belong to the next
|
76 |
request(s).
|
|
77 |
||
78 |
This sets the _push_back_buffer to the given bytes.
|
|
79 |
"""
|
|
3376.2.4
by Martin Pool
Remove every assert statement from bzrlib! |
80 |
if self._push_back_buffer is not None: |
81 |
raise AssertionError( |
|
82 |
"_push_back called when self._push_back_buffer is %r" |
|
83 |
% (self._push_back_buffer,)) |
|
3236.3.2
by Andrew Bennetts
Fix SmartServerPipeStreamMedium._get_line too. |
84 |
if bytes == '': |
85 |
return
|
|
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. |
86 |
self._push_back_buffer = bytes |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
87 |
|
3236.3.5
by Andrew Bennetts
Add _get_push_back_buffer helper. |
88 |
def _get_push_back_buffer(self): |
3376.2.4
by Martin Pool
Remove every assert statement from bzrlib! |
89 |
if self._push_back_buffer == '': |
90 |
raise AssertionError( |
|
91 |
'%s._push_back_buffer should never be the empty string, ' |
|
92 |
'which can be confused with EOF' % (self,)) |
|
3236.3.5
by Andrew Bennetts
Add _get_push_back_buffer helper. |
93 |
bytes = self._push_back_buffer |
94 |
self._push_back_buffer = None |
|
95 |
return bytes |
|
96 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
97 |
def serve(self): |
98 |
"""Serve requests until the client disconnects."""
|
|
99 |
# Keep a reference to stderr because the sys module's globals get set to
|
|
100 |
# None during interpreter shutdown.
|
|
101 |
from sys import stderr |
|
102 |
try: |
|
103 |
while not self.finished: |
|
2432.2.3
by Andrew Bennetts
Merge from bzr.dev. |
104 |
server_protocol = self._build_protocol() |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
105 |
self._serve_one_request(server_protocol) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
106 |
except Exception, e: |
107 |
stderr.write("%s terminating on exception %s\n" % (self, e)) |
|
108 |
raise
|
|
109 |
||
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
110 |
def _build_protocol(self): |
2432.2.8
by Andrew Bennetts
NEWS entry, greatly improved docstring in bzrlib.smart. |
111 |
"""Identifies the version of the incoming request, and returns an
|
112 |
a protocol object that can interpret it.
|
|
113 |
||
114 |
If more bytes than the version prefix of the request are read, they will
|
|
115 |
be fed into the protocol before it is returned.
|
|
116 |
||
117 |
:returns: a SmartServerRequestProtocol.
|
|
118 |
"""
|
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
119 |
# Identify the protocol version.
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
120 |
bytes = self._get_line() |
121 |
if bytes.startswith(REQUEST_VERSION_TWO): |
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
122 |
protocol_class = SmartServerRequestProtocolTwo |
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
123 |
bytes = bytes[len(REQUEST_VERSION_TWO):] |
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
124 |
else: |
125 |
protocol_class = SmartServerRequestProtocolOne |
|
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. |
126 |
protocol = protocol_class( |
127 |
self.backing_transport, self._write_out, self.root_client_path) |
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
128 |
protocol.accept_bytes(bytes) |
129 |
return protocol |
|
130 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
131 |
def _serve_one_request(self, protocol): |
132 |
"""Read one request from input, process, send back a response.
|
|
133 |
|
|
134 |
:param protocol: a SmartServerRequestProtocol.
|
|
135 |
"""
|
|
136 |
try: |
|
137 |
self._serve_one_request_unguarded(protocol) |
|
138 |
except KeyboardInterrupt: |
|
139 |
raise
|
|
140 |
except Exception, e: |
|
141 |
self.terminate_due_to_error() |
|
142 |
||
143 |
def terminate_due_to_error(self): |
|
144 |
"""Called when an unhandled exception from the protocol occurs."""
|
|
145 |
raise NotImplementedError(self.terminate_due_to_error) |
|
146 |
||
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
147 |
def _get_bytes(self, desired_count): |
148 |
"""Get some bytes from the medium.
|
|
149 |
||
150 |
:param desired_count: number of bytes we want to read.
|
|
151 |
"""
|
|
152 |
raise NotImplementedError(self._get_bytes) |
|
153 |
||
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
154 |
def _get_line(self): |
155 |
"""Read bytes from this request's response until a newline byte.
|
|
156 |
|
|
157 |
This isn't particularly efficient, so should only be used when the
|
|
158 |
expected size of the line is quite short.
|
|
159 |
||
160 |
:returns: a string of bytes ending in a newline (byte 0x0A).
|
|
161 |
"""
|
|
3236.3.2
by Andrew Bennetts
Fix SmartServerPipeStreamMedium._get_line too. |
162 |
newline_pos = -1 |
163 |
bytes = '' |
|
164 |
while newline_pos == -1: |
|
165 |
new_bytes = self._get_bytes(1) |
|
166 |
bytes += new_bytes |
|
167 |
if new_bytes == '': |
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
168 |
# Ran out of bytes before receiving a complete line.
|
3236.3.2
by Andrew Bennetts
Fix SmartServerPipeStreamMedium._get_line too. |
169 |
return bytes |
170 |
newline_pos = bytes.find('\n') |
|
171 |
line = bytes[:newline_pos+1] |
|
172 |
self._push_back(bytes[newline_pos+1:]) |
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
173 |
return line |
3236.3.2
by Andrew Bennetts
Fix SmartServerPipeStreamMedium._get_line too. |
174 |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
175 |
|
176 |
class SmartServerSocketStreamMedium(SmartServerStreamMedium): |
|
177 |
||
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. |
178 |
def __init__(self, sock, backing_transport, root_client_path='/'): |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
179 |
"""Constructor.
|
180 |
||
181 |
:param sock: the socket the server will read from. It will be put
|
|
182 |
into blocking mode.
|
|
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 |
SmartServerStreamMedium.__init__( |
185 |
self, backing_transport, root_client_path=root_client_path) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
186 |
sock.setblocking(True) |
187 |
self.socket = sock |
|
188 |
||
189 |
def _serve_one_request_unguarded(self, protocol): |
|
190 |
while protocol.next_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. |
191 |
bytes = self._get_bytes(4096) |
192 |
if bytes == '': |
|
193 |
self.finished = True |
|
194 |
return
|
|
195 |
protocol.accept_bytes(bytes) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
196 |
|
3236.3.1
by Andrew Bennetts
Fix a bug in SmartServerSocketStreamMedium._get_line, and add some asserts to catch this sort of mistake sooner. |
197 |
self._push_back(protocol.excess_buffer) |
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
198 |
|
199 |
def _get_bytes(self, desired_count): |
|
3236.3.5
by Andrew Bennetts
Add _get_push_back_buffer helper. |
200 |
if self._push_back_buffer is not None: |
201 |
return self._get_push_back_buffer() |
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
202 |
# We ignore the desired_count because on sockets it's more efficient to
|
203 |
# read 4k at a time.
|
|
204 |
return self.socket.recv(4096) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
205 |
|
206 |
def terminate_due_to_error(self): |
|
207 |
"""Called when an unhandled exception from the protocol occurs."""
|
|
208 |
# TODO: This should log to a server log file, but no such thing
|
|
209 |
# exists yet. Andrew Bennetts 2006-09-29.
|
|
210 |
self.socket.close() |
|
211 |
self.finished = True |
|
212 |
||
213 |
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. |
214 |
osutils.send_all(self.socket, bytes) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
215 |
|
216 |
||
217 |
class SmartServerPipeStreamMedium(SmartServerStreamMedium): |
|
218 |
||
219 |
def __init__(self, in_file, out_file, backing_transport): |
|
220 |
"""Construct new server.
|
|
221 |
||
222 |
:param in_file: Python file from which requests can be read.
|
|
223 |
:param out_file: Python file to write responses.
|
|
224 |
:param backing_transport: Transport for the directory served.
|
|
225 |
"""
|
|
226 |
SmartServerStreamMedium.__init__(self, backing_transport) |
|
2018.5.161
by Andrew Bennetts
Reinstate forcing binary mode on windows in SmartServerStreamMedium. |
227 |
if sys.platform == 'win32': |
228 |
# force binary mode for files
|
|
229 |
import msvcrt |
|
230 |
for f in (in_file, out_file): |
|
231 |
fileno = getattr(f, 'fileno', None) |
|
232 |
if fileno: |
|
233 |
msvcrt.setmode(fileno(), os.O_BINARY) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
234 |
self._in = in_file |
235 |
self._out = out_file |
|
236 |
||
237 |
def _serve_one_request_unguarded(self, protocol): |
|
238 |
while True: |
|
239 |
bytes_to_read = protocol.next_read_size() |
|
240 |
if bytes_to_read == 0: |
|
241 |
# Finished serving this request.
|
|
242 |
self._out.flush() |
|
243 |
return
|
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
244 |
bytes = self._get_bytes(bytes_to_read) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
245 |
if bytes == '': |
246 |
# Connection has been closed.
|
|
247 |
self.finished = True |
|
248 |
self._out.flush() |
|
249 |
return
|
|
250 |
protocol.accept_bytes(bytes) |
|
251 |
||
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
252 |
def _get_bytes(self, desired_count): |
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. |
253 |
if self._push_back_buffer is not None: |
3236.3.5
by Andrew Bennetts
Add _get_push_back_buffer helper. |
254 |
return self._get_push_back_buffer() |
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
255 |
return self._in.read(desired_count) |
256 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
257 |
def terminate_due_to_error(self): |
258 |
# TODO: This should log to a server log file, but no such thing
|
|
259 |
# exists yet. Andrew Bennetts 2006-09-29.
|
|
260 |
self._out.close() |
|
261 |
self.finished = True |
|
262 |
||
263 |
def _write_out(self, bytes): |
|
264 |
self._out.write(bytes) |
|
265 |
||
266 |
||
267 |
class SmartClientMediumRequest(object): |
|
268 |
"""A request on a SmartClientMedium.
|
|
269 |
||
270 |
Each request allows bytes to be provided to it via accept_bytes, and then
|
|
271 |
the response bytes to be read via read_bytes.
|
|
272 |
||
273 |
For instance:
|
|
274 |
request.accept_bytes('123')
|
|
275 |
request.finished_writing()
|
|
276 |
result = request.read_bytes(3)
|
|
277 |
request.finished_reading()
|
|
278 |
||
279 |
It is up to the individual SmartClientMedium whether multiple concurrent
|
|
280 |
requests can exist. See SmartClientMedium.get_request to obtain instances
|
|
281 |
of SmartClientMediumRequest, and the concrete Medium you are using for
|
|
282 |
details on concurrency and pipelining.
|
|
283 |
"""
|
|
284 |
||
285 |
def __init__(self, medium): |
|
286 |
"""Construct a SmartClientMediumRequest for the medium medium."""
|
|
287 |
self._medium = medium |
|
288 |
# we track state by constants - we may want to use the same
|
|
289 |
# pattern as BodyReader if it gets more complex.
|
|
290 |
# valid states are: "writing", "reading", "done"
|
|
291 |
self._state = "writing" |
|
292 |
||
293 |
def accept_bytes(self, bytes): |
|
294 |
"""Accept bytes for inclusion in this request.
|
|
295 |
||
296 |
This method may not be be called after finished_writing() has been
|
|
297 |
called. It depends upon the Medium whether or not the bytes will be
|
|
298 |
immediately transmitted. Message based Mediums will tend to buffer the
|
|
299 |
bytes until finished_writing() is called.
|
|
300 |
||
301 |
:param bytes: A bytestring.
|
|
302 |
"""
|
|
303 |
if self._state != "writing": |
|
304 |
raise errors.WritingCompleted(self) |
|
305 |
self._accept_bytes(bytes) |
|
306 |
||
307 |
def _accept_bytes(self, bytes): |
|
308 |
"""Helper for accept_bytes.
|
|
309 |
||
310 |
Accept_bytes checks the state of the request to determing if bytes
|
|
311 |
should be accepted. After that it hands off to _accept_bytes to do the
|
|
312 |
actual acceptance.
|
|
313 |
"""
|
|
314 |
raise NotImplementedError(self._accept_bytes) |
|
315 |
||
316 |
def finished_reading(self): |
|
317 |
"""Inform the request that all desired data has been read.
|
|
318 |
||
319 |
This will remove the request from the pipeline for its medium (if the
|
|
320 |
medium supports pipelining) and any further calls to methods on the
|
|
321 |
request will raise ReadingCompleted.
|
|
322 |
"""
|
|
323 |
if self._state == "writing": |
|
324 |
raise errors.WritingNotComplete(self) |
|
325 |
if self._state != "reading": |
|
326 |
raise errors.ReadingCompleted(self) |
|
327 |
self._state = "done" |
|
328 |
self._finished_reading() |
|
329 |
||
330 |
def _finished_reading(self): |
|
331 |
"""Helper for finished_reading.
|
|
332 |
||
333 |
finished_reading checks the state of the request to determine if
|
|
334 |
finished_reading is allowed, and if it is hands off to _finished_reading
|
|
335 |
to perform the action.
|
|
336 |
"""
|
|
337 |
raise NotImplementedError(self._finished_reading) |
|
338 |
||
339 |
def finished_writing(self): |
|
340 |
"""Finish the writing phase of this request.
|
|
341 |
||
342 |
This will flush all pending data for this request along the medium.
|
|
343 |
After calling finished_writing, you may not call accept_bytes anymore.
|
|
344 |
"""
|
|
345 |
if self._state != "writing": |
|
346 |
raise errors.WritingCompleted(self) |
|
347 |
self._state = "reading" |
|
348 |
self._finished_writing() |
|
349 |
||
350 |
def _finished_writing(self): |
|
351 |
"""Helper for finished_writing.
|
|
352 |
||
353 |
finished_writing checks the state of the request to determine if
|
|
354 |
finished_writing is allowed, and if it is hands off to _finished_writing
|
|
355 |
to perform the action.
|
|
356 |
"""
|
|
357 |
raise NotImplementedError(self._finished_writing) |
|
358 |
||
359 |
def read_bytes(self, count): |
|
360 |
"""Read bytes from this requests response.
|
|
361 |
||
362 |
This method will block and wait for count bytes to be read. It may not
|
|
363 |
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. |
364 |
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. |
365 |
based mediums like HTTP.
|
366 |
"""
|
|
367 |
if self._state == "writing": |
|
368 |
raise errors.WritingNotComplete(self) |
|
369 |
if self._state != "reading": |
|
370 |
raise errors.ReadingCompleted(self) |
|
371 |
return self._read_bytes(count) |
|
372 |
||
373 |
def _read_bytes(self, count): |
|
374 |
"""Helper for read_bytes.
|
|
375 |
||
376 |
read_bytes checks the state of the request to determing if bytes
|
|
377 |
should be read. After that it hands off to _read_bytes to do the
|
|
378 |
actual read.
|
|
379 |
"""
|
|
380 |
raise NotImplementedError(self._read_bytes) |
|
381 |
||
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
382 |
def read_line(self): |
383 |
"""Read bytes from this request's response until a newline byte.
|
|
384 |
|
|
385 |
This isn't particularly efficient, so should only be used when the
|
|
386 |
expected size of the line is quite short.
|
|
387 |
||
388 |
:returns: a string of bytes ending in a newline (byte 0x0A).
|
|
389 |
"""
|
|
390 |
# XXX: this duplicates SmartClientRequestProtocolOne._recv_tuple
|
|
391 |
line = '' |
|
392 |
while not line or line[-1] != '\n': |
|
393 |
new_char = self.read_bytes(1) |
|
394 |
line += new_char |
|
395 |
if new_char == '': |
|
3195.2.1
by Andrew Bennetts
Improve test coverage, and fix a bug revealed by the improved coverage. |
396 |
# end of file encountered reading from server
|
397 |
raise errors.ConnectionReset( |
|
398 |
"please check connectivity and permissions", |
|
399 |
"(and try -Dhpss if further diagnosis is required)") |
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
400 |
return line |
401 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
402 |
|
403 |
class SmartClientMedium(object): |
|
404 |
"""Smart client is a medium for sending smart protocol requests over."""
|
|
405 |
||
3241.1.1
by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium. |
406 |
def __init__(self): |
407 |
super(SmartClientMedium, self).__init__() |
|
3241.1.4
by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout. |
408 |
self._protocol_version_error = None |
3241.1.1
by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium. |
409 |
self._protocol_version = None |
410 |
||
411 |
def protocol_version(self): |
|
412 |
"""Find out the best protocol version to use."""
|
|
3241.1.4
by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout. |
413 |
if self._protocol_version_error is not None: |
414 |
raise self._protocol_version_error |
|
3241.1.1
by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium. |
415 |
if self._protocol_version is None: |
3241.1.4
by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout. |
416 |
try: |
417 |
medium_request = self.get_request() |
|
418 |
# Send a 'hello' request in protocol version one, for maximum
|
|
419 |
# backwards compatibility.
|
|
420 |
client_protocol = SmartClientRequestProtocolOne(medium_request) |
|
421 |
self._protocol_version = client_protocol.query_version() |
|
422 |
except errors.SmartProtocolError, e: |
|
423 |
# Cache the error, just like we would cache a successful
|
|
424 |
# result.
|
|
425 |
self._protocol_version_error = e |
|
426 |
raise
|
|
3241.1.1
by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium. |
427 |
return self._protocol_version |
428 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
429 |
def disconnect(self): |
430 |
"""If this medium maintains a persistent connection, close it.
|
|
431 |
|
|
432 |
The default implementation does nothing.
|
|
433 |
"""
|
|
434 |
||
435 |
||
436 |
class SmartClientStreamMedium(SmartClientMedium): |
|
437 |
"""Stream based medium common class.
|
|
438 |
||
439 |
SmartClientStreamMediums operate on a stream. All subclasses use a common
|
|
440 |
SmartClientStreamMediumRequest for their requests, and should implement
|
|
441 |
_accept_bytes and _read_bytes to allow the request objects to send and
|
|
442 |
receive bytes.
|
|
443 |
"""
|
|
444 |
||
445 |
def __init__(self): |
|
3241.1.1
by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium. |
446 |
SmartClientMedium.__init__(self) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
447 |
self._current_request = None |
3213.1.1
by Andrew Bennetts
Recover (by reconnecting) if the server turns out not to understand the new requests in 1.2 that send bodies. |
448 |
# Be optimistic: we assume the remote end can accept new remote
|
449 |
# requests until we get an error saying otherwise. (1.2 adds some
|
|
450 |
# requests that send bodies, which confuses older servers.)
|
|
451 |
self._remote_is_at_least_1_2 = True |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
452 |
|
453 |
def accept_bytes(self, bytes): |
|
454 |
self._accept_bytes(bytes) |
|
455 |
||
456 |
def __del__(self): |
|
457 |
"""The SmartClientStreamMedium knows how to close the stream when it is
|
|
458 |
finished with it.
|
|
459 |
"""
|
|
460 |
self.disconnect() |
|
461 |
||
462 |
def _flush(self): |
|
463 |
"""Flush the output stream.
|
|
464 |
|
|
465 |
This method is used by the SmartClientStreamMediumRequest to ensure that
|
|
466 |
all data for a request is sent, to avoid long timeouts or deadlocks.
|
|
467 |
"""
|
|
468 |
raise NotImplementedError(self._flush) |
|
469 |
||
470 |
def get_request(self): |
|
471 |
"""See SmartClientMedium.get_request().
|
|
472 |
||
473 |
SmartClientStreamMedium always returns a SmartClientStreamMediumRequest
|
|
474 |
for get_request.
|
|
475 |
"""
|
|
476 |
return SmartClientStreamMediumRequest(self) |
|
477 |
||
478 |
def read_bytes(self, count): |
|
479 |
return self._read_bytes(count) |
|
480 |
||
481 |
||
482 |
class SmartSimplePipesClientMedium(SmartClientStreamMedium): |
|
483 |
"""A client medium using simple pipes.
|
|
484 |
|
|
485 |
This client does not manage the pipes: it assumes they will always be open.
|
|
486 |
"""
|
|
487 |
||
488 |
def __init__(self, readable_pipe, writeable_pipe): |
|
489 |
SmartClientStreamMedium.__init__(self) |
|
490 |
self._readable_pipe = readable_pipe |
|
491 |
self._writeable_pipe = writeable_pipe |
|
492 |
||
493 |
def _accept_bytes(self, bytes): |
|
494 |
"""See SmartClientStreamMedium.accept_bytes."""
|
|
495 |
self._writeable_pipe.write(bytes) |
|
496 |
||
497 |
def _flush(self): |
|
498 |
"""See SmartClientStreamMedium._flush()."""
|
|
499 |
self._writeable_pipe.flush() |
|
500 |
||
501 |
def _read_bytes(self, count): |
|
502 |
"""See SmartClientStreamMedium._read_bytes."""
|
|
503 |
return self._readable_pipe.read(count) |
|
504 |
||
505 |
||
506 |
class SmartSSHClientMedium(SmartClientStreamMedium): |
|
507 |
"""A client medium using SSH."""
|
|
508 |
||
509 |
def __init__(self, host, port=None, username=None, password=None, |
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
510 |
vendor=None, bzr_remote_path=None): |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
511 |
"""Creates a client that will connect on the first use.
|
512 |
|
|
513 |
:param vendor: An optional override for the ssh vendor to use. See
|
|
514 |
bzrlib.transport.ssh for details on ssh vendors.
|
|
515 |
"""
|
|
516 |
SmartClientStreamMedium.__init__(self) |
|
517 |
self._connected = False |
|
518 |
self._host = host |
|
519 |
self._password = password |
|
520 |
self._port = port |
|
521 |
self._username = username |
|
522 |
self._read_from = None |
|
523 |
self._ssh_connection = None |
|
524 |
self._vendor = vendor |
|
525 |
self._write_to = None |
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
526 |
self._bzr_remote_path = bzr_remote_path |
527 |
if self._bzr_remote_path is None: |
|
528 |
symbol_versioning.warn( |
|
529 |
'bzr_remote_path is required as of bzr 0.92', |
|
530 |
DeprecationWarning, stacklevel=2) |
|
531 |
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. |
532 |
|
533 |
def _accept_bytes(self, bytes): |
|
534 |
"""See SmartClientStreamMedium.accept_bytes."""
|
|
535 |
self._ensure_connection() |
|
536 |
self._write_to.write(bytes) |
|
537 |
||
538 |
def disconnect(self): |
|
539 |
"""See SmartClientMedium.disconnect()."""
|
|
540 |
if not self._connected: |
|
541 |
return
|
|
542 |
self._read_from.close() |
|
543 |
self._write_to.close() |
|
544 |
self._ssh_connection.close() |
|
545 |
self._connected = False |
|
546 |
||
547 |
def _ensure_connection(self): |
|
548 |
"""Connect this medium if not already connected."""
|
|
549 |
if self._connected: |
|
550 |
return
|
|
551 |
if self._vendor is None: |
|
552 |
vendor = ssh._get_ssh_vendor() |
|
553 |
else: |
|
554 |
vendor = self._vendor |
|
555 |
self._ssh_connection = vendor.connect_ssh(self._username, |
|
556 |
self._password, self._host, self._port, |
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
557 |
command=[self._bzr_remote_path, 'serve', '--inet', |
558 |
'--directory=/', '--allow-writes']) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
559 |
self._read_from, self._write_to = \ |
560 |
self._ssh_connection.get_filelike_channels() |
|
561 |
self._connected = True |
|
562 |
||
563 |
def _flush(self): |
|
564 |
"""See SmartClientStreamMedium._flush()."""
|
|
565 |
self._write_to.flush() |
|
566 |
||
567 |
def _read_bytes(self, count): |
|
568 |
"""See SmartClientStreamMedium.read_bytes."""
|
|
569 |
if not self._connected: |
|
570 |
raise errors.MediumNotConnected(self) |
|
571 |
return self._read_from.read(count) |
|
572 |
||
573 |
||
3004.2.1
by Vincent Ladeuil
Fix 150860 by leaving port as user specified it. |
574 |
# Port 4155 is the default port for bzr://, registered with IANA.
|
575 |
BZR_DEFAULT_INTERFACE = '0.0.0.0' |
|
576 |
BZR_DEFAULT_PORT = 4155 |
|
577 |
||
578 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
579 |
class SmartTCPClientMedium(SmartClientStreamMedium): |
580 |
"""A client medium using TCP."""
|
|
581 |
||
582 |
def __init__(self, host, port): |
|
583 |
"""Creates a client that will connect on the first use."""
|
|
584 |
SmartClientStreamMedium.__init__(self) |
|
585 |
self._connected = False |
|
586 |
self._host = host |
|
587 |
self._port = port |
|
588 |
self._socket = None |
|
589 |
||
590 |
def _accept_bytes(self, bytes): |
|
591 |
"""See SmartClientMedium.accept_bytes."""
|
|
592 |
self._ensure_connection() |
|
3118.2.1
by Andrew Bennetts
(andrew) Fix #115781 by passing no more than 64k at a time to socket.sendall. |
593 |
osutils.send_all(self._socket, bytes) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
594 |
|
595 |
def disconnect(self): |
|
596 |
"""See SmartClientMedium.disconnect()."""
|
|
597 |
if not self._connected: |
|
598 |
return
|
|
599 |
self._socket.close() |
|
600 |
self._socket = None |
|
601 |
self._connected = False |
|
602 |
||
603 |
def _ensure_connection(self): |
|
604 |
"""Connect this medium if not already connected."""
|
|
605 |
if self._connected: |
|
606 |
return
|
|
607 |
self._socket = socket.socket() |
|
608 |
self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) |
|
3004.2.1
by Vincent Ladeuil
Fix 150860 by leaving port as user specified it. |
609 |
if self._port is None: |
610 |
port = BZR_DEFAULT_PORT |
|
611 |
else: |
|
612 |
port = int(self._port) |
|
3180.1.1
by Andrew Bennetts
Don't traceback on host name errors when connecting to bzr://... |
613 |
try: |
614 |
self._socket.connect((self._host, port)) |
|
615 |
except socket.error, err: |
|
616 |
# socket errors either have a (string) or (errno, string) as their
|
|
617 |
# args.
|
|
618 |
if type(err.args) is str: |
|
619 |
err_msg = err.args |
|
620 |
else: |
|
621 |
err_msg = err.args[1] |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
622 |
raise errors.ConnectionError("failed to connect to %s:%d: %s" % |
3180.1.1
by Andrew Bennetts
Don't traceback on host name errors when connecting to bzr://... |
623 |
(self._host, port, err_msg)) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
624 |
self._connected = True |
625 |
||
626 |
def _flush(self): |
|
627 |
"""See SmartClientStreamMedium._flush().
|
|
628 |
|
|
629 |
For TCP we do no flushing. We may want to turn off TCP_NODELAY and
|
|
630 |
add a means to do a flush, but that can be done in the future.
|
|
631 |
"""
|
|
632 |
||
633 |
def _read_bytes(self, count): |
|
634 |
"""See SmartClientMedium.read_bytes."""
|
|
635 |
if not self._connected: |
|
636 |
raise errors.MediumNotConnected(self) |
|
637 |
return self._socket.recv(count) |
|
638 |
||
639 |
||
640 |
class SmartClientStreamMediumRequest(SmartClientMediumRequest): |
|
641 |
"""A SmartClientMediumRequest that works with an SmartClientStreamMedium."""
|
|
642 |
||
643 |
def __init__(self, medium): |
|
644 |
SmartClientMediumRequest.__init__(self, medium) |
|
645 |
# check that we are safe concurrency wise. If some streams start
|
|
646 |
# allowing concurrent requests - i.e. via multiplexing - then this
|
|
647 |
# assert should be moved to SmartClientStreamMedium.get_request,
|
|
648 |
# and the setting/unsetting of _current_request likewise moved into
|
|
649 |
# that class : but its unneeded overhead for now. RBC 20060922
|
|
650 |
if self._medium._current_request is not None: |
|
651 |
raise errors.TooManyConcurrentRequests(self._medium) |
|
652 |
self._medium._current_request = self |
|
653 |
||
654 |
def _accept_bytes(self, bytes): |
|
655 |
"""See SmartClientMediumRequest._accept_bytes.
|
|
656 |
|
|
657 |
This forwards to self._medium._accept_bytes because we are operating
|
|
658 |
on the mediums stream.
|
|
659 |
"""
|
|
660 |
self._medium._accept_bytes(bytes) |
|
661 |
||
662 |
def _finished_reading(self): |
|
663 |
"""See SmartClientMediumRequest._finished_reading.
|
|
664 |
||
665 |
This clears the _current_request on self._medium to allow a new
|
|
666 |
request to be created.
|
|
667 |
"""
|
|
3376.2.4
by Martin Pool
Remove every assert statement from bzrlib! |
668 |
if self._medium._current_request is not self: |
669 |
raise AssertionError() |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
670 |
self._medium._current_request = None |
671 |
||
672 |
def _finished_writing(self): |
|
673 |
"""See SmartClientMediumRequest._finished_writing.
|
|
674 |
||
675 |
This invokes self._medium._flush to ensure all bytes are transmitted.
|
|
676 |
"""
|
|
677 |
self._medium._flush() |
|
678 |
||
679 |
def _read_bytes(self, count): |
|
680 |
"""See SmartClientMediumRequest._read_bytes.
|
|
681 |
|
|
682 |
This forwards to self._medium._read_bytes because we are operating
|
|
683 |
on the mediums stream.
|
|
684 |
"""
|
|
685 |
return self._medium._read_bytes(count) |
|
686 |