1
# Copyright (C) 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""The 'medium' layer for the smart servers and clients.
19
"Medium" here is the noun meaning "a means of transmission", not the adjective
20
for "the quality between big and small."
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.
32
from bzrlib.lazy_import import lazy_import
33
lazy_import(globals(), """
40
from bzrlib.smart import protocol
41
from bzrlib.transport import ssh
45
# We must not read any more than 64k at a time so we don't risk "no buffer
46
# space available" errors on some platforms. Windows in particular is likely
47
# to give error 10053 or 10055 if we read more than 64k from a socket.
48
_MAX_READ_SIZE = 64 * 1024
51
def _get_protocol_factory_for_bytes(bytes):
52
"""Determine the right protocol factory for 'bytes'.
54
This will return an appropriate protocol factory depending on the version
55
of the protocol being used, as determined by inspecting the given bytes.
56
The bytes should have at least one newline byte (i.e. be a whole line),
57
otherwise it's possible that a request will be incorrectly identified as
60
Typical use would be::
62
factory, unused_bytes = _get_protocol_factory_for_bytes(bytes)
63
server_protocol = factory(transport, write_func, root_client_path)
64
server_protocol.accept_bytes(unused_bytes)
66
:param bytes: a str of bytes of the start of the request.
67
:returns: 2-tuple of (protocol_factory, unused_bytes). protocol_factory is
68
a callable that takes three args: transport, write_func,
69
root_client_path. unused_bytes are any bytes that were not part of a
70
protocol version marker.
72
if bytes.startswith(protocol.MESSAGE_VERSION_THREE):
73
protocol_factory = protocol.build_server_protocol_three
74
bytes = bytes[len(protocol.MESSAGE_VERSION_THREE):]
75
elif bytes.startswith(protocol.REQUEST_VERSION_TWO):
76
protocol_factory = protocol.SmartServerRequestProtocolTwo
77
bytes = bytes[len(protocol.REQUEST_VERSION_TWO):]
79
protocol_factory = protocol.SmartServerRequestProtocolOne
80
return protocol_factory, bytes
83
class SmartMedium(object):
84
"""Base class for smart protocol media, both client- and server-side."""
87
self._push_back_buffer = None
89
def _push_back(self, bytes):
90
"""Return unused bytes to the medium, because they belong to the next
93
This sets the _push_back_buffer to the given bytes.
95
if self._push_back_buffer is not None:
97
"_push_back called when self._push_back_buffer is %r"
98
% (self._push_back_buffer,))
101
self._push_back_buffer = bytes
103
def _get_push_back_buffer(self):
104
if self._push_back_buffer == '':
105
raise AssertionError(
106
'%s._push_back_buffer should never be the empty string, '
107
'which can be confused with EOF' % (self,))
108
bytes = self._push_back_buffer
109
self._push_back_buffer = None
112
def read_bytes(self, desired_count):
113
"""Read some bytes from this medium.
115
:returns: some bytes, possibly more or less than the number requested
116
in 'desired_count' depending on the medium.
118
if self._push_back_buffer is not None:
119
return self._get_push_back_buffer()
120
bytes_to_read = min(desired_count, _MAX_READ_SIZE)
121
return self._read_bytes(bytes_to_read)
123
def _read_bytes(self, count):
124
raise NotImplementedError(self._read_bytes)
127
"""Read bytes from this request's response until a newline byte.
129
This isn't particularly efficient, so should only be used when the
130
expected size of the line is quite short.
132
:returns: a string of bytes ending in a newline (byte 0x0A).
136
while newline_pos == -1:
137
new_bytes = self.read_bytes(1)
140
# Ran out of bytes before receiving a complete line.
142
newline_pos = bytes.find('\n')
143
line = bytes[:newline_pos+1]
144
self._push_back(bytes[newline_pos+1:])
148
class SmartServerStreamMedium(SmartMedium):
149
"""Handles smart commands coming over a stream.
151
The stream may be a pipe connected to sshd, or a tcp socket, or an
152
in-process fifo for testing.
154
One instance is created for each connected client; it can serve multiple
155
requests in the lifetime of the connection.
157
The server passes requests through to an underlying backing transport,
158
which will typically be a LocalTransport looking at the server's filesystem.
160
:ivar _push_back_buffer: a str of bytes that have been read from the stream
161
but not used yet, or None if there are no buffered bytes. Subclasses
162
should make sure to exhaust this buffer before reading more bytes from
163
the stream. See also the _push_back method.
166
def __init__(self, backing_transport, root_client_path='/'):
167
"""Construct new server.
169
:param backing_transport: Transport for the directory served.
171
# backing_transport could be passed to serve instead of __init__
172
self.backing_transport = backing_transport
173
self.root_client_path = root_client_path
174
self.finished = False
175
SmartMedium.__init__(self)
178
"""Serve requests until the client disconnects."""
179
# Keep a reference to stderr because the sys module's globals get set to
180
# None during interpreter shutdown.
181
from sys import stderr
183
while not self.finished:
184
server_protocol = self._build_protocol()
185
self._serve_one_request(server_protocol)
187
stderr.write("%s terminating on exception %s\n" % (self, e))
190
def _build_protocol(self):
191
"""Identifies the version of the incoming request, and returns an
192
a protocol object that can interpret it.
194
If more bytes than the version prefix of the request are read, they will
195
be fed into the protocol before it is returned.
197
:returns: a SmartServerRequestProtocol.
199
bytes = self._get_line()
200
protocol_factory, unused_bytes = _get_protocol_factory_for_bytes(bytes)
201
protocol = protocol_factory(
202
self.backing_transport, self._write_out, self.root_client_path)
203
protocol.accept_bytes(unused_bytes)
206
def _serve_one_request(self, protocol):
207
"""Read one request from input, process, send back a response.
209
:param protocol: a SmartServerRequestProtocol.
212
self._serve_one_request_unguarded(protocol)
213
except KeyboardInterrupt:
216
self.terminate_due_to_error()
218
def terminate_due_to_error(self):
219
"""Called when an unhandled exception from the protocol occurs."""
220
raise NotImplementedError(self.terminate_due_to_error)
222
def _read_bytes(self, desired_count):
223
"""Get some bytes from the medium.
225
:param desired_count: number of bytes we want to read.
227
raise NotImplementedError(self._read_bytes)
230
class SmartServerSocketStreamMedium(SmartServerStreamMedium):
232
def __init__(self, sock, backing_transport, root_client_path='/'):
235
:param sock: the socket the server will read from. It will be put
238
SmartServerStreamMedium.__init__(
239
self, backing_transport, root_client_path=root_client_path)
240
sock.setblocking(True)
243
def _serve_one_request_unguarded(self, protocol):
244
while protocol.next_read_size():
245
# We can safely try to read large chunks. If there is less data
246
# than _MAX_READ_SIZE ready, the socket wil just return a short
247
# read immediately rather than block.
248
bytes = self.read_bytes(_MAX_READ_SIZE)
252
protocol.accept_bytes(bytes)
254
self._push_back(protocol.unused_data)
256
def _read_bytes(self, desired_count):
257
# We ignore the desired_count because on sockets it's more efficient to
258
# read large chunks (of _MAX_READ_SIZE bytes) at a time.
259
return self.socket.recv(_MAX_READ_SIZE)
261
def terminate_due_to_error(self):
262
# TODO: This should log to a server log file, but no such thing
263
# exists yet. Andrew Bennetts 2006-09-29.
267
def _write_out(self, bytes):
268
osutils.send_all(self.socket, bytes)
271
class SmartServerPipeStreamMedium(SmartServerStreamMedium):
273
def __init__(self, in_file, out_file, backing_transport):
274
"""Construct new server.
276
:param in_file: Python file from which requests can be read.
277
:param out_file: Python file to write responses.
278
:param backing_transport: Transport for the directory served.
280
SmartServerStreamMedium.__init__(self, backing_transport)
281
if sys.platform == 'win32':
282
# force binary mode for files
284
for f in (in_file, out_file):
285
fileno = getattr(f, 'fileno', None)
287
msvcrt.setmode(fileno(), os.O_BINARY)
291
def _serve_one_request_unguarded(self, protocol):
293
# We need to be careful not to read past the end of the current
294
# request, or else the read from the pipe will block, so we use
295
# protocol.next_read_size().
296
bytes_to_read = protocol.next_read_size()
297
if bytes_to_read == 0:
298
# Finished serving this request.
301
bytes = self.read_bytes(bytes_to_read)
303
# Connection has been closed.
307
protocol.accept_bytes(bytes)
309
def _read_bytes(self, desired_count):
310
return self._in.read(desired_count)
312
def terminate_due_to_error(self):
313
# TODO: This should log to a server log file, but no such thing
314
# exists yet. Andrew Bennetts 2006-09-29.
318
def _write_out(self, bytes):
319
self._out.write(bytes)
322
class SmartClientMediumRequest(object):
323
"""A request on a SmartClientMedium.
325
Each request allows bytes to be provided to it via accept_bytes, and then
326
the response bytes to be read via read_bytes.
329
request.accept_bytes('123')
330
request.finished_writing()
331
result = request.read_bytes(3)
332
request.finished_reading()
334
It is up to the individual SmartClientMedium whether multiple concurrent
335
requests can exist. See SmartClientMedium.get_request to obtain instances
336
of SmartClientMediumRequest, and the concrete Medium you are using for
337
details on concurrency and pipelining.
340
def __init__(self, medium):
341
"""Construct a SmartClientMediumRequest for the medium medium."""
342
self._medium = medium
343
# we track state by constants - we may want to use the same
344
# pattern as BodyReader if it gets more complex.
345
# valid states are: "writing", "reading", "done"
346
self._state = "writing"
348
def accept_bytes(self, bytes):
349
"""Accept bytes for inclusion in this request.
351
This method may not be be called after finished_writing() has been
352
called. It depends upon the Medium whether or not the bytes will be
353
immediately transmitted. Message based Mediums will tend to buffer the
354
bytes until finished_writing() is called.
356
:param bytes: A bytestring.
358
if self._state != "writing":
359
raise errors.WritingCompleted(self)
360
self._accept_bytes(bytes)
362
def _accept_bytes(self, bytes):
363
"""Helper for accept_bytes.
365
Accept_bytes checks the state of the request to determing if bytes
366
should be accepted. After that it hands off to _accept_bytes to do the
369
raise NotImplementedError(self._accept_bytes)
371
def finished_reading(self):
372
"""Inform the request that all desired data has been read.
374
This will remove the request from the pipeline for its medium (if the
375
medium supports pipelining) and any further calls to methods on the
376
request will raise ReadingCompleted.
378
if self._state == "writing":
379
raise errors.WritingNotComplete(self)
380
if self._state != "reading":
381
raise errors.ReadingCompleted(self)
383
self._finished_reading()
385
def _finished_reading(self):
386
"""Helper for finished_reading.
388
finished_reading checks the state of the request to determine if
389
finished_reading is allowed, and if it is hands off to _finished_reading
390
to perform the action.
392
raise NotImplementedError(self._finished_reading)
394
def finished_writing(self):
395
"""Finish the writing phase of this request.
397
This will flush all pending data for this request along the medium.
398
After calling finished_writing, you may not call accept_bytes anymore.
400
if self._state != "writing":
401
raise errors.WritingCompleted(self)
402
self._state = "reading"
403
self._finished_writing()
405
def _finished_writing(self):
406
"""Helper for finished_writing.
408
finished_writing checks the state of the request to determine if
409
finished_writing is allowed, and if it is hands off to _finished_writing
410
to perform the action.
412
raise NotImplementedError(self._finished_writing)
414
def read_bytes(self, count):
415
"""Read bytes from this requests response.
417
This method will block and wait for count bytes to be read. It may not
418
be invoked until finished_writing() has been called - this is to ensure
419
a message-based approach to requests, for compatibility with message
420
based mediums like HTTP.
422
if self._state == "writing":
423
raise errors.WritingNotComplete(self)
424
if self._state != "reading":
425
raise errors.ReadingCompleted(self)
426
return self._read_bytes(count)
428
def _read_bytes(self, count):
429
"""Helper for SmartClientMediumRequest.read_bytes.
431
read_bytes checks the state of the request to determing if bytes
432
should be read. After that it hands off to _read_bytes to do the
435
By default this forwards to self._medium.read_bytes because we are
436
operating on the medium's stream.
438
return self._medium.read_bytes(count)
441
line = self._medium._get_line()
442
if not line.endswith('\n'):
443
# end of file encountered reading from server
444
raise errors.ConnectionReset(
445
"please check connectivity and permissions",
446
"(and try -Dhpss if further diagnosis is required)")
450
class SmartClientMedium(SmartMedium):
451
"""Smart client is a medium for sending smart protocol requests over."""
453
def __init__(self, base):
454
super(SmartClientMedium, self).__init__()
456
self._protocol_version_error = None
457
self._protocol_version = None
458
self._done_hello = False
459
# Be optimistic: we assume the remote end can accept new remote
460
# requests until we get an error saying otherwise.
461
# _remote_version_is_before tracks the bzr version the remote side
462
# can be based on what we've seen so far.
463
self._remote_version_is_before = None
465
def _is_remote_before(self, version_tuple):
466
"""Is it possible the remote side supports RPCs for a given version?
470
needed_version = (1, 2)
471
if medium._is_remote_before(needed_version):
472
fallback_to_pre_1_2_rpc()
476
except UnknownSmartMethod:
477
medium._remember_remote_is_before(needed_version)
478
fallback_to_pre_1_2_rpc()
480
:seealso: _remember_remote_is_before
482
if self._remote_version_is_before is None:
483
# So far, the remote side seems to support everything
485
return version_tuple >= self._remote_version_is_before
487
def _remember_remote_is_before(self, version_tuple):
488
"""Tell this medium that the remote side is older the given version.
490
:seealso: _is_remote_before
492
if (self._remote_version_is_before is not None and
493
version_tuple > self._remote_version_is_before):
494
raise AssertionError(
495
"_remember_remote_is_before(%r) called, but "
496
"_remember_remote_is_before(%r) was called previously."
497
% (version_tuple, self._remote_version_is_before))
498
self._remote_version_is_before = version_tuple
500
def protocol_version(self):
501
"""Find out if 'hello' smart request works."""
502
if self._protocol_version_error is not None:
503
raise self._protocol_version_error
504
if not self._done_hello:
506
medium_request = self.get_request()
507
# Send a 'hello' request in protocol version one, for maximum
508
# backwards compatibility.
509
client_protocol = protocol.SmartClientRequestProtocolOne(medium_request)
510
client_protocol.query_version()
511
self._done_hello = True
512
except errors.SmartProtocolError, e:
513
# Cache the error, just like we would cache a successful
515
self._protocol_version_error = e
519
def should_probe(self):
520
"""Should RemoteBzrDirFormat.probe_transport send a smart request on
523
Some transports are unambiguously smart-only; there's no need to check
524
if the transport is able to carry smart requests, because that's all
525
it is for. In those cases, this method should return False.
527
But some HTTP transports can sometimes fail to carry smart requests,
528
but still be usuable for accessing remote bzrdirs via plain file
529
accesses. So for those transports, their media should return True here
530
so that RemoteBzrDirFormat can determine if it is appropriate for that
535
def disconnect(self):
536
"""If this medium maintains a persistent connection, close it.
538
The default implementation does nothing.
541
def remote_path_from_transport(self, transport):
542
"""Convert transport into a path suitable for using in a request.
544
Note that the resulting remote path doesn't encode the host name or
545
anything but path, so it is only safe to use it in requests sent over
546
the medium from the matching transport.
548
medium_base = urlutils.join(self.base, '/')
549
rel_url = urlutils.relative_url(medium_base, transport.base)
550
return urllib.unquote(rel_url)
553
class SmartClientStreamMedium(SmartClientMedium):
554
"""Stream based medium common class.
556
SmartClientStreamMediums operate on a stream. All subclasses use a common
557
SmartClientStreamMediumRequest for their requests, and should implement
558
_accept_bytes and _read_bytes to allow the request objects to send and
562
def __init__(self, base):
563
SmartClientMedium.__init__(self, base)
564
self._current_request = None
566
def accept_bytes(self, bytes):
567
self._accept_bytes(bytes)
570
"""The SmartClientStreamMedium knows how to close the stream when it is
576
"""Flush the output stream.
578
This method is used by the SmartClientStreamMediumRequest to ensure that
579
all data for a request is sent, to avoid long timeouts or deadlocks.
581
raise NotImplementedError(self._flush)
583
def get_request(self):
584
"""See SmartClientMedium.get_request().
586
SmartClientStreamMedium always returns a SmartClientStreamMediumRequest
589
return SmartClientStreamMediumRequest(self)
592
class SmartSimplePipesClientMedium(SmartClientStreamMedium):
593
"""A client medium using simple pipes.
595
This client does not manage the pipes: it assumes they will always be open.
598
def __init__(self, readable_pipe, writeable_pipe, base):
599
SmartClientStreamMedium.__init__(self, base)
600
self._readable_pipe = readable_pipe
601
self._writeable_pipe = writeable_pipe
603
def _accept_bytes(self, bytes):
604
"""See SmartClientStreamMedium.accept_bytes."""
605
self._writeable_pipe.write(bytes)
608
"""See SmartClientStreamMedium._flush()."""
609
self._writeable_pipe.flush()
611
def _read_bytes(self, count):
612
"""See SmartClientStreamMedium._read_bytes."""
613
return self._readable_pipe.read(count)
616
class SmartSSHClientMedium(SmartClientStreamMedium):
617
"""A client medium using SSH."""
619
def __init__(self, host, port=None, username=None, password=None,
620
base=None, vendor=None, bzr_remote_path=None):
621
"""Creates a client that will connect on the first use.
623
:param vendor: An optional override for the ssh vendor to use. See
624
bzrlib.transport.ssh for details on ssh vendors.
626
SmartClientStreamMedium.__init__(self, base)
627
self._connected = False
629
self._password = password
631
self._username = username
632
self._read_from = None
633
self._ssh_connection = None
634
self._vendor = vendor
635
self._write_to = None
636
self._bzr_remote_path = bzr_remote_path
637
if self._bzr_remote_path is None:
638
symbol_versioning.warn(
639
'bzr_remote_path is required as of bzr 0.92',
640
DeprecationWarning, stacklevel=2)
641
self._bzr_remote_path = os.environ.get('BZR_REMOTE_PATH', 'bzr')
643
def _accept_bytes(self, bytes):
644
"""See SmartClientStreamMedium.accept_bytes."""
645
self._ensure_connection()
646
self._write_to.write(bytes)
648
def disconnect(self):
649
"""See SmartClientMedium.disconnect()."""
650
if not self._connected:
652
self._read_from.close()
653
self._write_to.close()
654
self._ssh_connection.close()
655
self._connected = False
657
def _ensure_connection(self):
658
"""Connect this medium if not already connected."""
661
if self._vendor is None:
662
vendor = ssh._get_ssh_vendor()
664
vendor = self._vendor
665
self._ssh_connection = vendor.connect_ssh(self._username,
666
self._password, self._host, self._port,
667
command=[self._bzr_remote_path, 'serve', '--inet',
668
'--directory=/', '--allow-writes'])
669
self._read_from, self._write_to = \
670
self._ssh_connection.get_filelike_channels()
671
self._connected = True
674
"""See SmartClientStreamMedium._flush()."""
675
self._write_to.flush()
677
def _read_bytes(self, count):
678
"""See SmartClientStreamMedium.read_bytes."""
679
if not self._connected:
680
raise errors.MediumNotConnected(self)
681
bytes_to_read = min(count, _MAX_READ_SIZE)
682
return self._read_from.read(bytes_to_read)
685
# Port 4155 is the default port for bzr://, registered with IANA.
686
BZR_DEFAULT_INTERFACE = '0.0.0.0'
687
BZR_DEFAULT_PORT = 4155
690
class SmartTCPClientMedium(SmartClientStreamMedium):
691
"""A client medium using TCP."""
693
def __init__(self, host, port, base):
694
"""Creates a client that will connect on the first use."""
695
SmartClientStreamMedium.__init__(self, base)
696
self._connected = False
701
def _accept_bytes(self, bytes):
702
"""See SmartClientMedium.accept_bytes."""
703
self._ensure_connection()
704
osutils.send_all(self._socket, bytes)
706
def disconnect(self):
707
"""See SmartClientMedium.disconnect()."""
708
if not self._connected:
712
self._connected = False
714
def _ensure_connection(self):
715
"""Connect this medium if not already connected."""
718
self._socket = socket.socket()
719
self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
720
if self._port is None:
721
port = BZR_DEFAULT_PORT
723
port = int(self._port)
725
self._socket.connect((self._host, port))
726
except socket.error, err:
727
# socket errors either have a (string) or (errno, string) as their
729
if type(err.args) is str:
732
err_msg = err.args[1]
733
raise errors.ConnectionError("failed to connect to %s:%d: %s" %
734
(self._host, port, err_msg))
735
self._connected = True
738
"""See SmartClientStreamMedium._flush().
740
For TCP we do no flushing. We may want to turn off TCP_NODELAY and
741
add a means to do a flush, but that can be done in the future.
744
def _read_bytes(self, count):
745
"""See SmartClientMedium.read_bytes."""
746
if not self._connected:
747
raise errors.MediumNotConnected(self)
748
# We ignore the desired_count because on sockets it's more efficient to
749
# read large chunks (of _MAX_READ_SIZE bytes) at a time.
750
return self._socket.recv(_MAX_READ_SIZE)
753
class SmartClientStreamMediumRequest(SmartClientMediumRequest):
754
"""A SmartClientMediumRequest that works with an SmartClientStreamMedium."""
756
def __init__(self, medium):
757
SmartClientMediumRequest.__init__(self, medium)
758
# check that we are safe concurrency wise. If some streams start
759
# allowing concurrent requests - i.e. via multiplexing - then this
760
# assert should be moved to SmartClientStreamMedium.get_request,
761
# and the setting/unsetting of _current_request likewise moved into
762
# that class : but its unneeded overhead for now. RBC 20060922
763
if self._medium._current_request is not None:
764
raise errors.TooManyConcurrentRequests(self._medium)
765
self._medium._current_request = self
767
def _accept_bytes(self, bytes):
768
"""See SmartClientMediumRequest._accept_bytes.
770
This forwards to self._medium._accept_bytes because we are operating
771
on the mediums stream.
773
self._medium._accept_bytes(bytes)
775
def _finished_reading(self):
776
"""See SmartClientMediumRequest._finished_reading.
778
This clears the _current_request on self._medium to allow a new
779
request to be created.
781
if self._medium._current_request is not self:
782
raise AssertionError()
783
self._medium._current_request = None
785
def _finished_writing(self):
786
"""See SmartClientMediumRequest._finished_writing.
788
This invokes self._medium._flush to ensure all bytes are transmitted.
790
self._medium._flush()