| 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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 return SimpleHTTPServer.SimpleHTTPRequestHandler.guess_type(self, path) | 164 return SimpleHTTPServer.SimpleHTTPRequestHandler.guess_type(self, path) |
| 165 | 165 |
| 166 def log_message(self, *_): | 166 def log_message(self, *_): |
| 167 """Override the base class method to disable logging.""" | 167 """Override the base class method to disable logging.""" |
| 168 pass | 168 pass |
| 169 | 169 |
| 170 RequestHandler.protocol_version = 'HTTP/1.1' | 170 RequestHandler.protocol_version = 'HTTP/1.1' |
| 171 return RequestHandler | 171 return RequestHandler |
| 172 | 172 |
| 173 | 173 |
| 174 def start_http_server(local_dir_path, host_port=0, additional_mappings=None): | 174 def start_http_server(mappings, host_port=0): |
| 175 """Starts an http server serving files from |local_dir_path| on |host_port|. | 175 """Starts an http server serving files from |local_dir_path| on |host_port|. |
| 176 | 176 |
| 177 Args: | 177 Args: |
| 178 local_dir_path: Path to the local filesystem directory to be served over | 178 mappings: List of tuples (prefix, local_base_path) mapping |
| 179 http under. | 179 URLs that start with |prefix| to local directory at |local_base_path|. |
| 180 The prefixes should skip the leading slash. The first matching prefix |
| 181 will be used each time. |
| 180 host_port: Port on the host machine to run the server on. Pass 0 to use a | 182 host_port: Port on the host machine to run the server on. Pass 0 to use a |
| 181 system-assigned port. | 183 system-assigned port. |
| 182 additional_mappings: List of tuples (prefix, local_base_path) mapping | |
| 183 URLs that start with |prefix| to local directory at |local_base_path|. | |
| 184 The prefixes should skip the leading slash. | |
| 185 | 184 |
| 186 Returns: | 185 Returns: |
| 187 Tuple of the server address and the port on which it runs. | 186 Tuple of the server address and the port on which it runs. |
| 188 """ | 187 """ |
| 189 assert local_dir_path | 188 assert mappings |
| 190 mappings = additional_mappings if additional_mappings else [] | |
| 191 mappings.append(('', local_dir_path)) | |
| 192 handler_class = _GetHandlerClassForPath(mappings) | 189 handler_class = _GetHandlerClassForPath(mappings) |
| 193 | 190 |
| 194 try: | 191 try: |
| 195 httpd = _SilentTCPServer(('127.0.0.1', host_port), handler_class) | 192 httpd = _SilentTCPServer(('127.0.0.1', host_port), handler_class) |
| 196 atexit.register(httpd.shutdown) | 193 atexit.register(httpd.shutdown) |
| 197 | 194 |
| 198 http_thread = threading.Thread(target=httpd.serve_forever) | 195 http_thread = threading.Thread(target=httpd.serve_forever) |
| 199 http_thread.daemon = True | 196 http_thread.daemon = True |
| 200 http_thread.start() | 197 http_thread.start() |
| 201 print 'Started http://%s:%d to host %s.' % (httpd.server_address[0], | 198 print 'Started http://%s:%d to host %s.' % (httpd.server_address[0], |
| 202 httpd.server_address[1], | 199 httpd.server_address[1], |
| 203 local_dir_path) | 200 str(mappings)) |
| 204 return httpd.server_address | 201 return httpd.server_address |
| 205 except socket.error as v: | 202 except socket.error as v: |
| 206 error_code = v[0] | 203 error_code = v[0] |
| 207 print 'Failed to start http server for %s on port %d: %s.' % ( | 204 print 'Failed to start http server for %s on port %d: %s.' % ( |
| 208 local_dir_path, host_port, os.strerror(error_code)) | 205 str(mappings), host_port, os.strerror(error_code)) |
| 209 if error_code == errno.EADDRINUSE: | 206 if error_code == errno.EADDRINUSE: |
| 210 print (' Run `fuser %d/tcp` to find out which process is using the port.' | 207 print (' Run `fuser %d/tcp` to find out which process is using the port.' |
| 211 % host_port) | 208 % host_port) |
| 212 print '---' | 209 print '---' |
| 213 raise | 210 raise |
| OLD | NEW |