| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import atexit | 5 import atexit |
| 6 import datetime | 6 import datetime |
| 7 import email.utils | 7 import email.utils |
| 8 import errno | 8 import errno |
| 9 import gzip | 9 import gzip |
| 10 import hashlib | 10 import hashlib |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 causes spurious error messages. | 42 causes spurious error messages. |
| 43 """ | 43 """ |
| 44 allow_reuse_address = True | 44 allow_reuse_address = True |
| 45 | 45 |
| 46 def handle_error(self, request, client_address): | 46 def handle_error(self, request, client_address): |
| 47 """Override the base class method to have conditional logging.""" | 47 """Override the base class method to have conditional logging.""" |
| 48 if logging.getLogger().isEnabledFor(logging.DEBUG): | 48 if logging.getLogger().isEnabledFor(logging.DEBUG): |
| 49 SocketServer.TCPServer.handle_error(self, request, client_address) | 49 SocketServer.TCPServer.handle_error(self, request, client_address) |
| 50 | 50 |
| 51 | 51 |
| 52 def _GetHandlerClassForPath(mappings): | 52 def _get_handler_class_for_path(mappings): |
| 53 """Creates a handler override for SimpleHTTPServer. | 53 """Creates a handler override for SimpleHTTPServer. |
| 54 | 54 |
| 55 Args: | 55 Args: |
| 56 mappings: List of tuples (prefix, local_base_path), mapping path prefixes | 56 mappings: List of tuples (prefix, local_base_path), mapping path prefixes |
| 57 without the leading slash to local filesystem directory paths. The first | 57 without the leading slash to local filesystem directory paths. The first |
| 58 matching prefix will be used each time. | 58 matching prefix will be used each time. |
| 59 """ | 59 """ |
| 60 for prefix, _ in mappings: | 60 for prefix, _ in mappings: |
| 61 assert not prefix.startswith('/'), ('Prefixes for the http server mappings ' | 61 assert not prefix.startswith('/'), ('Prefixes for the http server mappings ' |
| 62 'should skip the leading slash.') | 62 'should skip the leading slash.') |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 URLs that start with |prefix| to local directory at |local_base_path|. | 181 URLs that start with |prefix| to local directory at |local_base_path|. |
| 182 The prefixes should skip the leading slash. The first matching prefix | 182 The prefixes should skip the leading slash. The first matching prefix |
| 183 will be used each time. | 183 will be used each time. |
| 184 host_port: Port on the host machine to run the server on. Pass 0 to use a | 184 host_port: Port on the host machine to run the server on. Pass 0 to use a |
| 185 system-assigned port. | 185 system-assigned port. |
| 186 | 186 |
| 187 Returns: | 187 Returns: |
| 188 Tuple of the server address and the port on which it runs. | 188 Tuple of the server address and the port on which it runs. |
| 189 """ | 189 """ |
| 190 assert mappings | 190 assert mappings |
| 191 handler_class = _GetHandlerClassForPath(mappings) | 191 handler_class = _get_handler_class_for_path(mappings) |
| 192 | 192 |
| 193 try: | 193 try: |
| 194 httpd = _SilentTCPServer(('127.0.0.1', host_port), handler_class) | 194 httpd = _SilentTCPServer(('127.0.0.1', host_port), handler_class) |
| 195 atexit.register(httpd.shutdown) | 195 atexit.register(httpd.shutdown) |
| 196 | 196 |
| 197 http_thread = threading.Thread(target=httpd.serve_forever) | 197 http_thread = threading.Thread(target=httpd.serve_forever) |
| 198 http_thread.daemon = True | 198 http_thread.daemon = True |
| 199 http_thread.start() | 199 http_thread.start() |
| 200 print 'Started http://%s:%d to host %s.' % (httpd.server_address[0], | 200 print 'Started http://%s:%d to host %s.' % (httpd.server_address[0], |
| 201 httpd.server_address[1], | 201 httpd.server_address[1], |
| 202 str(mappings)) | 202 str(mappings)) |
| 203 return httpd.server_address | 203 return httpd.server_address |
| 204 except socket.error as v: | 204 except socket.error as v: |
| 205 error_code = v[0] | 205 error_code = v[0] |
| 206 print 'Failed to start http server for %s on port %d: %s.' % ( | 206 print 'Failed to start http server for %s on port %d: %s.' % ( |
| 207 str(mappings), host_port, os.strerror(error_code)) | 207 str(mappings), host_port, os.strerror(error_code)) |
| 208 if error_code == errno.EADDRINUSE: | 208 if error_code == errno.EADDRINUSE: |
| 209 print (' Run `fuser %d/tcp` to find out which process is using the port.' | 209 print (' Run `fuser %d/tcp` to find out which process is using the port.' |
| 210 % host_port) | 210 % host_port) |
| 211 print '---' | 211 print '---' |
| 212 raise | 212 raise |
| OLD | NEW |