bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2400.1.2
by Andrew Bennetts
Move SmartTCPServer classes into bzrlib/smart/server.py |
1 |
# Copyright (C) 2006, 2007 Canonical Ltd
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
2 |
#
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
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 |
"""Server for smart-server protocol."""
|
18 |
||
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
19 |
import errno |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
20 |
import socket |
21 |
import threading |
|
22 |
||
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
23 |
from bzrlib.hooks import Hooks |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
24 |
from bzrlib import ( |
2634.1.1
by Robert Collins
(robertc) Reinstate the accidentally backed out external_url patch. |
25 |
errors, |
2018.5.15
by Andrew Bennetts
Tidy some imports, and bugs introduced when adding server.py |
26 |
trace, |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
27 |
transport, |
28 |
)
|
|
2400.1.3
by Andrew Bennetts
Split smart transport code into several separate modules. |
29 |
from bzrlib.smart.medium import SmartServerSocketStreamMedium |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
30 |
|
31 |
||
32 |
class SmartTCPServer(object): |
|
33 |
"""Listens on a TCP socket and accepts connections from smart clients. |
|
2018.5.139
by Andrew Bennetts
Merge from bzr.dev, resolving conflicts. |
34 |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
35 |
Each connection will be served by a SmartServerSocketStreamMedium running in
|
2018.5.139
by Andrew Bennetts
Merge from bzr.dev, resolving conflicts. |
36 |
a thread.
|
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
37 |
|
38 |
hooks: An instance of SmartServerHooks.
|
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
39 |
"""
|
40 |
||
2692.1.14
by Andrew Bennetts
All WSGI tests passing, and manual testing works too. |
41 |
def __init__(self, backing_transport, host='127.0.0.1', port=0, |
42 |
root_client_path='/'): |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
43 |
"""Construct a new server. |
44 |
||
45 |
To actually start it running, call either start_background_thread or
|
|
46 |
serve.
|
|
47 |
||
2692.1.14
by Andrew Bennetts
All WSGI tests passing, and manual testing works too. |
48 |
:param backing_transport: The transport to serve.
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
49 |
:param host: Name of the interface to listen on.
|
50 |
:param port: TCP port to listen on, or 0 to allocate a transient port.
|
|
2692.1.14
by Andrew Bennetts
All WSGI tests passing, and manual testing works too. |
51 |
:param root_client_path: The client path that will correspond to root
|
52 |
of backing_transport.
|
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
53 |
"""
|
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
54 |
# let connections timeout so that we get a chance to terminate
|
55 |
# Keep a reference to the exceptions we want to catch because the socket
|
|
56 |
# module's globals get set to None during interpreter shutdown.
|
|
57 |
from socket import timeout as socket_timeout |
|
58 |
from socket import error as socket_error |
|
59 |
self._socket_error = socket_error |
|
60 |
self._socket_timeout = socket_timeout |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
61 |
self._server_socket = socket.socket() |
62 |
self._server_socket.bind((host, port)) |
|
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
63 |
self._sockname = self._server_socket.getsockname() |
64 |
self.port = self._sockname[1] |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
65 |
self._server_socket.listen(1) |
66 |
self._server_socket.settimeout(1) |
|
67 |
self.backing_transport = backing_transport |
|
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
68 |
self._started = threading.Event() |
69 |
self._stopped = threading.Event() |
|
2692.1.14
by Andrew Bennetts
All WSGI tests passing, and manual testing works too. |
70 |
self.root_client_path = root_client_path |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
71 |
|
72 |
def serve(self): |
|
73 |
self._should_terminate = False |
|
2634.1.1
by Robert Collins
(robertc) Reinstate the accidentally backed out external_url patch. |
74 |
# for hooks we are letting code know that a server has started (and
|
75 |
# later stopped).
|
|
76 |
# There are three interesting urls:
|
|
77 |
# The URL the server can be contacted on. (e.g. bzr://host/)
|
|
78 |
# The URL that a commit done on the same machine as the server will
|
|
79 |
# have within the servers space. (e.g. file:///home/user/source)
|
|
80 |
# The URL that will be given to other hooks in the same process -
|
|
81 |
# the URL of the backing transport itself. (e.g. chroot+:///)
|
|
82 |
# We need all three because:
|
|
83 |
# * other machines see the first
|
|
84 |
# * local commits on this machine should be able to be mapped to
|
|
85 |
# this server
|
|
86 |
# * commits the server does itself need to be mapped across to this
|
|
87 |
# server.
|
|
88 |
# The latter two urls are different aliases to the servers url,
|
|
89 |
# so we group those in a list - as there might be more aliases
|
|
90 |
# in the future.
|
|
91 |
backing_urls = [self.backing_transport.base] |
|
92 |
try: |
|
93 |
backing_urls.append(self.backing_transport.external_url()) |
|
94 |
except errors.InProcessTransport: |
|
95 |
pass
|
|
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
96 |
for hook in SmartTCPServer.hooks['server_started']: |
2634.1.1
by Robert Collins
(robertc) Reinstate the accidentally backed out external_url patch. |
97 |
hook(backing_urls, self.get_url()) |
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
98 |
self._started.set() |
99 |
try: |
|
100 |
try: |
|
101 |
while not self._should_terminate: |
|
102 |
try: |
|
103 |
conn, client_addr = self._server_socket.accept() |
|
104 |
except self._socket_timeout: |
|
105 |
# just check if we're asked to stop
|
|
106 |
pass
|
|
107 |
except self._socket_error, e: |
|
108 |
# if the socket is closed by stop_background_thread
|
|
109 |
# we might get a EBADF here, any other socket errors
|
|
110 |
# should get logged.
|
|
111 |
if e.args[0] != errno.EBADF: |
|
112 |
trace.warning("listening socket error: %s", e) |
|
113 |
else: |
|
114 |
self.serve_conn(conn) |
|
115 |
except KeyboardInterrupt: |
|
116 |
# dont log when CTRL-C'd.
|
|
117 |
raise
|
|
118 |
except Exception, e: |
|
119 |
trace.error("Unhandled smart server error.") |
|
120 |
trace.log_exception_quietly() |
|
121 |
raise
|
|
122 |
finally: |
|
123 |
self._stopped.set() |
|
124 |
try: |
|
125 |
# ensure the server socket is closed.
|
|
126 |
self._server_socket.close() |
|
127 |
except self._socket_error: |
|
128 |
# ignore errors on close
|
|
129 |
pass
|
|
130 |
for hook in SmartTCPServer.hooks['server_stopped']: |
|
2634.1.1
by Robert Collins
(robertc) Reinstate the accidentally backed out external_url patch. |
131 |
hook(backing_urls, self.get_url()) |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
132 |
|
133 |
def get_url(self): |
|
134 |
"""Return the url of the server""" |
|
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
135 |
return "bzr://%s:%d/" % self._sockname |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
136 |
|
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
137 |
def serve_conn(self, conn): |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
138 |
# For WIN32, where the timeout value from the listening socket
|
139 |
# propogates to the newly accepted socket.
|
|
140 |
conn.setblocking(True) |
|
141 |
conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) |
|
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. |
142 |
handler = SmartServerSocketStreamMedium( |
143 |
conn, self.backing_transport, self.root_client_path) |
|
2400.1.2
by Andrew Bennetts
Move SmartTCPServer classes into bzrlib/smart/server.py |
144 |
connection_thread = threading.Thread(None, handler.serve, name='smart-server-child') |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
145 |
connection_thread.setDaemon(True) |
146 |
connection_thread.start() |
|
147 |
||
148 |
def start_background_thread(self): |
|
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
149 |
self._started.clear() |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
150 |
self._server_thread = threading.Thread(None, |
151 |
self.serve, |
|
152 |
name='server-' + self.get_url()) |
|
153 |
self._server_thread.setDaemon(True) |
|
154 |
self._server_thread.start() |
|
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
155 |
self._started.wait() |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
156 |
|
157 |
def stop_background_thread(self): |
|
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
158 |
self._stopped.clear() |
159 |
# tell the main loop to quit on the next iteration.
|
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
160 |
self._should_terminate = True |
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
161 |
# close the socket - gives error to connections from here on in,
|
162 |
# rather than a connection reset error to connections made during
|
|
163 |
# the period between setting _should_terminate = True and
|
|
164 |
# the current request completing/aborting. It may also break out the
|
|
165 |
# main loop if it was currently in accept() (on some platforms).
|
|
166 |
try: |
|
167 |
self._server_socket.close() |
|
168 |
except self._socket_error: |
|
169 |
# ignore errors on close
|
|
170 |
pass
|
|
171 |
if not self._stopped.isSet(): |
|
172 |
# server has not stopped (though it may be stopping)
|
|
173 |
# its likely in accept(), so give it a connection
|
|
174 |
temp_socket = socket.socket() |
|
175 |
temp_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) |
|
176 |
if not temp_socket.connect_ex(self._sockname): |
|
177 |
# and close it immediately: we dont choose to send any requests.
|
|
178 |
temp_socket.close() |
|
179 |
self._stopped.wait() |
|
180 |
self._server_thread.join() |
|
181 |
||
182 |
||
183 |
class SmartServerHooks(Hooks): |
|
184 |
"""Hooks for the smart server.""" |
|
185 |
||
186 |
def __init__(self): |
|
187 |
"""Create the default hooks. |
|
188 |
||
189 |
These are all empty initially, because by default nothing should get
|
|
190 |
notified.
|
|
191 |
"""
|
|
192 |
Hooks.__init__(self) |
|
193 |
# Introduced in 0.16:
|
|
194 |
# invoked whenever the server starts serving a directory.
|
|
2634.1.1
by Robert Collins
(robertc) Reinstate the accidentally backed out external_url patch. |
195 |
# The api signature is (backing urls, public url).
|
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
196 |
self['server_started'] = [] |
197 |
# Introduced in 0.16:
|
|
198 |
# invoked whenever the server stops serving a directory.
|
|
2634.1.1
by Robert Collins
(robertc) Reinstate the accidentally backed out external_url patch. |
199 |
# The api signature is (backing urls, public url).
|
2400.1.7
by Andrew Bennetts
Merge from bzr.dev. |
200 |
self['server_stopped'] = [] |
201 |
||
202 |
SmartTCPServer.hooks = SmartServerHooks() |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
203 |
|
2400.1.2
by Andrew Bennetts
Move SmartTCPServer classes into bzrlib/smart/server.py |
204 |
|
205 |
class SmartTCPServer_for_testing(SmartTCPServer): |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
206 |
"""Server suitable for use by transport tests. |
207 |
|
|
2400.1.2
by Andrew Bennetts
Move SmartTCPServer classes into bzrlib/smart/server.py |
208 |
This server is backed by the process's cwd.
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
209 |
"""
|
210 |
||
2400.1.2
by Andrew Bennetts
Move SmartTCPServer classes into bzrlib/smart/server.py |
211 |
def __init__(self): |
2018.5.42
by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :). |
212 |
SmartTCPServer.__init__(self, None) |
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. |
213 |
self.client_path_extra = None |
2400.1.2
by Andrew Bennetts
Move SmartTCPServer classes into bzrlib/smart/server.py |
214 |
|
215 |
def get_backing_transport(self, backing_transport_server): |
|
216 |
"""Get a backing transport from a server we are decorating.""" |
|
2018.5.104
by Andrew Bennetts
Completely rework chrooted transports. |
217 |
return transport.get_transport(backing_transport_server.get_url()) |
2400.1.2
by Andrew Bennetts
Move SmartTCPServer classes into bzrlib/smart/server.py |
218 |
|
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. |
219 |
def setUp(self, backing_transport_server=None, |
220 |
client_path_extra='/extra/'): |
|
221 |
"""Set up server for testing. |
|
222 |
|
|
223 |
:param backing_transport_server: backing server to use. If not
|
|
224 |
specified, a LocalURLServer at the current working directory will
|
|
225 |
be used.
|
|
226 |
:param client_path_extra: a path segment starting with '/' to append to
|
|
227 |
the root URL for this server. For instance, a value of '/foo/bar/'
|
|
228 |
will mean the root of the backing transport will be published at a
|
|
229 |
URL like `bzr://127.0.0.1:nnnn/foo/bar/`, rather than
|
|
230 |
`bzr://127.0.0.1:nnnn/`. Default value is `extra`, so that tests
|
|
231 |
by default will fail unless they do the necessary path translation.
|
|
232 |
"""
|
|
233 |
assert client_path_extra.startswith('/') |
|
2018.5.104
by Andrew Bennetts
Completely rework chrooted transports. |
234 |
from bzrlib.transport.chroot import ChrootServer |
2018.5.47
by Andrew Bennetts
Make SmartTCPServer_for_Testing.setUp's backing_transport_server argument optional. |
235 |
if backing_transport_server is None: |
236 |
from bzrlib.transport.local import LocalURLServer |
|
237 |
backing_transport_server = LocalURLServer() |
|
2018.5.104
by Andrew Bennetts
Completely rework chrooted transports. |
238 |
self.chroot_server = ChrootServer( |
239 |
self.get_backing_transport(backing_transport_server)) |
|
240 |
self.chroot_server.setUp() |
|
241 |
self.backing_transport = transport.get_transport( |
|
242 |
self.chroot_server.get_url()) |
|
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. |
243 |
self.root_client_path = self.client_path_extra = client_path_extra |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
244 |
self.start_background_thread() |
245 |
||
246 |
def tearDown(self): |
|
247 |
self.stop_background_thread() |
|
2018.5.104
by Andrew Bennetts
Completely rework chrooted transports. |
248 |
self.chroot_server.tearDown() |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
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 get_url(self): |
251 |
url = super(SmartTCPServer_for_testing, self).get_url() |
|
252 |
assert url.endswith('/') |
|
253 |
return url[:-1] + self.client_path_extra |
|
254 |
||
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
255 |
def get_bogus_url(self): |
256 |
"""Return a URL which will fail to connect""" |
|
257 |
return 'bzr://127.0.0.1:1/' |
|
258 |
||
259 |
||
2018.5.42
by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :). |
260 |
class ReadonlySmartTCPServer_for_testing(SmartTCPServer_for_testing): |
261 |
"""Get a readonly server for testing.""" |
|
262 |
||
263 |
def get_backing_transport(self, backing_transport_server): |
|
264 |
"""Get a backing transport from a server we are decorating.""" |
|
2018.5.104
by Andrew Bennetts
Completely rework chrooted transports. |
265 |
url = 'readonly+' + backing_transport_server.get_url() |
2018.5.42
by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :). |
266 |
return transport.get_transport(url) |
2018.5.95
by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes. |
267 |