LEFT | RIGHT |
1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, |
2 # Copyright (C) 2006-present eyeo GmbH | 2 # Copyright (C) 2006-present eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
12 # | 12 # |
13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
15 | 15 |
16 from __future__ import print_function | 16 from __future__ import print_function |
17 | 17 |
18 import os | 18 import os |
19 import mimetypes | 19 import mimetypes |
20 import argparse | 20 import argparse |
21 import time | |
22 | 21 |
23 import jinja2 | 22 import jinja2 |
24 | 23 |
25 from cms.converters import converters | 24 from cms.converters import converters |
26 from cms.utils import process_page | 25 from cms.utils import process_page |
27 from cms.sources import create_source | 26 from cms.sources import create_source |
28 from cms.exceptions import ServerSetupTimeoutException | |
29 | 27 |
30 UNICODE_ENCODING = 'utf-8' | 28 UNICODE_ENCODING = 'utf-8' |
31 | 29 |
32 ERROR_TEMPLATE = ''' | 30 ERROR_TEMPLATE = ''' |
33 <html> | 31 <html> |
34 <head> | 32 <head> |
35 <title>{{status}}</title> | 33 <title>{{status}}</title> |
36 </head> | 34 </head> |
37 <body> | 35 <body> |
38 <h1>{{status}}</h1> | 36 <h1>{{status}}</h1> |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 parser.add_argument('--host', default='localhost', | 245 parser.add_argument('--host', default='localhost', |
248 help='Address of the host the server will listen on. ' | 246 help='Address of the host the server will listen on. ' |
249 'Defaults to "localhost".') | 247 'Defaults to "localhost".') |
250 parser.add_argument('--port', default=5000, type=int, | 248 parser.add_argument('--port', default=5000, type=int, |
251 help='TCP port the server will listen on. Default ' | 249 help='TCP port the server will listen on. Default ' |
252 '5000.') | 250 '5000.') |
253 | 251 |
254 return parser.parse_args() | 252 return parser.parse_args() |
255 | 253 |
256 | 254 |
257 def wait_for_address(run_fn, handler, timeout=1.0, *args, **kw): | |
258 """Try to setup the server at a given address. | |
259 | |
260 If an exception occurs, we sleep for .1 seconds and retry until | |
261 `timeout` has passed. | |
262 | |
263 Parameters | |
264 ---------- | |
265 run_fn: callable | |
266 Used to initiate the server. | |
267 handler: DynamicServerHandler | |
268 Defines the parameters and methods required to handle requests. | |
269 timeout: float | |
270 Number of seconds to wait until giving up. | |
271 | |
272 Raises | |
273 ------ | |
274 ServerSetupTimeoutException | |
275 If the server couldn't be setup at the required address after | |
276 `timeout` passed. | |
277 | |
278 """ | |
279 start_time = time.time() | |
280 | |
281 while True: | |
282 try: | |
283 with run_fn(handler.host, handler.port, handler, *args, **kw): | |
284 break | |
285 except Exception as err: | |
286 time.sleep(.1) | |
287 if time.time() - start_time >= timeout: | |
288 raise ServerSetupTimeoutException(handler.host, handler.port, | |
289 timeout, err) | |
290 | |
291 | |
292 def run_werkzeug_server(handler, **kw): | 255 def run_werkzeug_server(handler, **kw): |
293 """Set up a server that uses `werkzeug`. | 256 """Set up a server that uses `werkzeug`. |
294 | 257 |
295 Parameters | 258 Parameters |
296 ---------- | 259 ---------- |
297 handler: DynamicServerHandler | 260 handler: DynamicServerHandler |
298 Defines the parameters and methods required to handle requests. | 261 Defines the parameters and methods required to handle requests. |
299 | 262 |
300 Raises | 263 Raises |
301 ------ | 264 ------ |
302 ImportError | 265 ImportError |
303 If the package `werkzeug` is not installed | 266 If the package `werkzeug` is not installed |
304 | 267 |
305 """ | 268 """ |
306 from werkzeug.serving import run_simple | 269 from werkzeug.serving import run_simple |
307 import logging | 270 import logging |
308 | 271 |
309 def run(*args, **kwargs): | 272 def run(*args, **kwargs): |
310 # The werkzeug logger must be configured before the | 273 # The werkzeug logger must be configured before the |
311 # root logger. Also we must prevent it from propagating | 274 # root logger. Also we must prevent it from propagating |
312 # messages, otherwise messages are logged twice. | 275 # messages, otherwise messages are logged twice. |
313 logger = logging.getLogger('werkzeug') | 276 logger = logging.getLogger('werkzeug') |
314 logger.propagate = False | 277 logger.propagate = False |
315 logger.setLevel(logging.INFO) | 278 logger.setLevel(logging.INFO) |
316 logger.addHandler(logging.StreamHandler()) | 279 logger.addHandler(logging.StreamHandler()) |
317 | 280 |
318 run_simple(threaded=True, *args, **kwargs) | 281 run_simple(threaded=True, *args, **kwargs) |
319 | 282 |
320 wait_for_address(run, handler, **kw) | 283 run(handler.host, handler.port, handler, **kw) |
321 | 284 |
322 | 285 |
323 def run_builtins_server(handler, **kw): | 286 def run_builtins_server(handler, **kw): |
324 """Configure a server that only uses builtin packages. | 287 """Configure a server that only uses builtin packages. |
325 | 288 |
326 Parameters | 289 Parameters |
327 ---------- | 290 ---------- |
328 handler: DynamicServerHandler | 291 handler: DynamicServerHandler |
329 Defines the parameters and methods required to handle requests. | 292 Defines the parameters and methods required to handle requests. |
330 | 293 |
(...skipping 11 matching lines...) Expand all Loading... |
342 except Exception as e: | 305 except Exception as e: |
343 return handler.get_error_page( | 306 return handler.get_error_page( |
344 start_response, '500 Internal Server Error', | 307 start_response, '500 Internal Server Error', |
345 uri=environ.get('PATH_INFO'), error=e, | 308 uri=environ.get('PATH_INFO'), error=e, |
346 ) | 309 ) |
347 | 310 |
348 server = make_server(host, port, wrapper, ThreadedWSGIServer) | 311 server = make_server(host, port, wrapper, ThreadedWSGIServer) |
349 print(' * Running on {0}:{1}'.format(*server.server_address)) | 312 print(' * Running on {0}:{1}'.format(*server.server_address)) |
350 server.serve_forever() | 313 server.serve_forever() |
351 | 314 |
352 wait_for_address(run, handler, **kw) | 315 run(handler.host, handler.port, handler, **kw) |
353 | 316 |
354 | 317 |
355 def main(): | 318 def main(): |
356 args = parse_arguments() | 319 args = parse_arguments() |
357 handler = DynamicServerHandler(args.host, args.port, args.path) | 320 handler = DynamicServerHandler(args.host, args.port, args.path) |
358 | 321 |
359 try: | 322 try: |
360 run_werkzeug_server(handler, use_reloader=True, use_debugger=True) | 323 run_werkzeug_server(handler, use_reloader=True, use_debugger=True) |
361 except ImportError: | 324 except ImportError: |
362 run_builtins_server(handler) | 325 run_builtins_server(handler) |
363 | 326 |
364 | 327 |
365 if __name__ == '__main__': | 328 if __name__ == '__main__': |
366 main() | 329 main() |
LEFT | RIGHT |