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