| OLD | NEW |
| 1 # Copyright (c) 2011 The Native Client Authors. All rights reserved. | 1 # Copyright (c) 2011 The Native Client 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 BaseHTTPServer | 5 import BaseHTTPServer |
| 6 import cgi | 6 import cgi |
| 7 import os | 7 import os |
| 8 import os.path | 8 import os.path |
| 9 import posixpath | 9 import posixpath |
| 10 import SimpleHTTPServer | 10 import SimpleHTTPServer |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 words = [word for word in words if word not in bad] | 39 words = [word for word in words if word not in bad] |
| 40 # The path of the request should always use POSIX-style path separators, so | 40 # The path of the request should always use POSIX-style path separators, so |
| 41 # that the filename input of --map_file can be a POSIX-style path and still | 41 # that the filename input of --map_file can be a POSIX-style path and still |
| 42 # match correctly in translate_path(). | 42 # match correctly in translate_path(). |
| 43 return '/'.join(words) | 43 return '/'.join(words) |
| 44 | 44 |
| 45 def translate_path(self, path): | 45 def translate_path(self, path): |
| 46 path = self.NormalizePath(path) | 46 path = self.NormalizePath(path) |
| 47 if path in self.server.file_mapping: | 47 if path in self.server.file_mapping: |
| 48 return self.server.file_mapping[path] | 48 return self.server.file_mapping[path] |
| 49 elif not path.endswith('favicon.ico') and not self.server.allow_404: | 49 for extra_dir in self.server.serving_dirs: |
| 50 full_path = os.path.join(extra_dir, path) |
| 51 if os.path.isfile(full_path): |
| 52 return full_path |
| 53 if not path.endswith('favicon.ico') and not self.server.allow_404: |
| 50 self.server.listener.ServerError('Cannot find file \'%s\'' % path) | 54 self.server.listener.ServerError('Cannot find file \'%s\'' % path) |
| 51 return path | 55 return path |
| 52 | 56 |
| 53 def SendRPCResponse(self, response): | 57 def SendRPCResponse(self, response): |
| 54 self.send_response(200) | 58 self.send_response(200) |
| 55 self.send_header("Content-type", "text/plain") | 59 self.send_header("Content-type", "text/plain") |
| 56 self.send_header("Content-length", str(len(response))) | 60 self.send_header("Content-length", str(len(response))) |
| 57 self.end_headers() | 61 self.end_headers() |
| 58 self.wfile.write(response) | 62 self.wfile.write(response) |
| 59 | 63 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 # Disable the built-in logging | 168 # Disable the built-in logging |
| 165 def log_message(self, format, *args): | 169 def log_message(self, format, *args): |
| 166 pass | 170 pass |
| 167 | 171 |
| 168 | 172 |
| 169 # NOTE: SocketServer.ThreadingMixIn seems to cause stability problems | 173 # NOTE: SocketServer.ThreadingMixIn seems to cause stability problems |
| 170 # when using older versions of Python. | 174 # when using older versions of Python. |
| 171 class Server(BaseHTTPServer.HTTPServer): | 175 class Server(BaseHTTPServer.HTTPServer): |
| 172 | 176 |
| 173 def Configure( | 177 def Configure( |
| 174 self, file_mapping, redirect_mapping, allow_404, bandwidth, listener): | 178 self, file_mapping, redirect_mapping, allow_404, bandwidth, listener, |
| 179 serving_dirs=[]): |
| 175 self.file_mapping = file_mapping | 180 self.file_mapping = file_mapping |
| 176 self.redirect_mapping = redirect_mapping | 181 self.redirect_mapping = redirect_mapping |
| 177 self.allow_404 = allow_404 | 182 self.allow_404 = allow_404 |
| 178 self.bandwidth = bandwidth | 183 self.bandwidth = bandwidth |
| 179 self.listener = listener | 184 self.listener = listener |
| 180 self.rpc_lock = threading.Lock() | 185 self.rpc_lock = threading.Lock() |
| 186 self.serving_dirs = serving_dirs |
| 181 | 187 |
| 182 def TestingBegun(self, timeout): | 188 def TestingBegun(self, timeout): |
| 183 self.test_in_progress = True | 189 self.test_in_progress = True |
| 184 # self.timeout does not affect Python 2.5. | 190 # self.timeout does not affect Python 2.5. |
| 185 self.timeout = timeout | 191 self.timeout = timeout |
| 186 self.ResetTimeout() | 192 self.ResetTimeout() |
| 187 self.JavaScriptIsAlive() | 193 self.JavaScriptIsAlive() |
| 188 | 194 |
| 189 def ResetTimeout(self): | 195 def ResetTimeout(self): |
| 190 self.last_activity = time.time() | 196 self.last_activity = time.time() |
| 191 | 197 |
| 192 def JavaScriptIsAlive(self): | 198 def JavaScriptIsAlive(self): |
| 193 self.last_js_activity = time.time() | 199 self.last_js_activity = time.time() |
| 194 | 200 |
| 195 def TimeSinceJSHeartbeat(self): | 201 def TimeSinceJSHeartbeat(self): |
| 196 return time.time() - self.last_js_activity | 202 return time.time() - self.last_js_activity |
| 197 | 203 |
| 198 def TestingEnded(self): | 204 def TestingEnded(self): |
| 199 self.test_in_progress = False | 205 self.test_in_progress = False |
| 200 | 206 |
| 201 def TimedOut(self, total_time): | 207 def TimedOut(self, total_time): |
| 202 return (total_time >= 0.0 and | 208 return (total_time >= 0.0 and |
| 203 (time.time() - self.last_activity) >= total_time) | 209 (time.time() - self.last_activity) >= total_time) |
| 204 | 210 |
| 205 | 211 |
| 206 def Create(host, port): | 212 def Create(host, port): |
| 207 return Server((host, port), RequestHandler) | 213 return Server((host, port), RequestHandler) |
| OLD | NEW |