Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """This is a simple HTTP server for manually testing exponential | 6 """This is a simple HTTP server for manually testing exponential |
| 7 back-off functionality in Chrome. | 7 back-off functionality in Chrome. |
| 8 """ | 8 """ |
| 9 | 9 |
| 10 | 10 |
| 11 import BaseHTTPServer | 11 import BaseHTTPServer |
| 12 import sys | 12 import sys |
| 13 import urlparse | 13 import urlparse |
| 14 | 14 |
| 15 | 15 |
| 16 AJAX_TEST_PAGE = ''' | |
| 17 <html> | |
| 18 <head> | |
| 19 <script> | |
| 20 | |
| 21 function reportResult(txt) { | |
| 22 var element = document.createElement('p'); | |
| 23 element.innerHTML = txt; | |
| 24 document.body.appendChild(element); | |
| 25 } | |
| 26 | |
| 27 function fetch() { | |
| 28 var response_code = document.getElementById('response_code'); | |
| 29 | |
| 30 xmlhttp = new XMLHttpRequest(); | |
| 31 xmlhttp.open("GET", | |
| 32 "http://%s:%d/%s?code=" + response_code.value, | |
| 33 true); | |
| 34 xmlhttp.onreadystatechange = function() { | |
| 35 reportResult( | |
| 36 'readyState=' + xmlhttp.readyState + ', status=' + xmlhttp.status); | |
| 37 } | |
| 38 try { | |
| 39 xmlhttp.send(null); | |
| 40 } catch (e) { | |
| 41 reportResult('Exception: ' + e); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 </script> | |
| 46 </head> | |
| 47 <body> | |
| 48 <form action="javascript:fetch()"> | |
| 49 Response code to get: <input id="response_code" type="text" value="503"> | |
| 50 <input type="submit"> | |
| 51 </form> | |
| 52 </body> | |
| 53 ''' | |
|
wtc
2011/05/25 23:12:06
IMPORTANT: AJAX_TEST_PAGE is missing the closing <
Jói
2011/05/26 14:53:42
Not intentional. Fixed.
| |
| 54 | |
| 55 | |
| 16 class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): | 56 class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 17 keep_running = True | 57 keep_running = True |
| 58 local_ip = '' | |
| 59 port = 0 | |
| 18 | 60 |
| 19 def do_GET(self): | 61 def do_GET(self): |
| 20 if self.path == '/quitquitquit': | 62 if self.path == '/quitquitquit': |
| 21 self.send_response(200) | 63 self.send_response(200) |
| 22 self.send_header('Content-Type', 'text/plain') | 64 self.send_header('Content-Type', 'text/plain') |
| 23 self.end_headers() | 65 self.end_headers() |
| 24 self.wfile.write('QUITTING') | 66 self.wfile.write('QUITTING') |
| 25 RequestHandler.keep_running = False | 67 RequestHandler.keep_running = False |
| 26 return | 68 return |
| 27 | 69 |
| 70 if self.path.startswith('/ajax/'): | |
| 71 self.send_response(200) | |
| 72 self.send_header('Content-Type', 'text/html') | |
| 73 self.end_headers() | |
| 74 self.wfile.write(AJAX_TEST_PAGE % (self.local_ip, | |
| 75 self.port, | |
| 76 self.path[6:])) | |
| 77 return | |
| 78 | |
| 28 params = urlparse.parse_qs(urlparse.urlparse(self.path).query) | 79 params = urlparse.parse_qs(urlparse.urlparse(self.path).query) |
| 29 | 80 |
| 81 def SendCustomRetryIfRequested(): | |
| 82 if params and 'custom-retry-after' in params: | |
| 83 custom_retry = params['custom-retry-after'][0] | |
| 84 self.send_header('X-Retry-After', custom_retry) | |
| 85 | |
| 30 if not params or not 'code' in params or params['code'][0] == '200': | 86 if not params or not 'code' in params or params['code'][0] == '200': |
| 31 self.send_response(200) | 87 self.send_response(200) |
| 32 self.send_header('Content-Type', 'text/plain') | 88 self.send_header('Content-Type', 'text/plain') |
| 89 SendCustomRetryIfRequested() | |
| 33 self.end_headers() | 90 self.end_headers() |
| 34 self.wfile.write('OK') | 91 self.wfile.write('OK') |
| 35 else: | 92 else: |
| 36 self.send_error(int(params['code'][0])) | 93 status_code = int(params['code'][0]) |
| 94 self.send_response(status_code) | |
| 95 SendCustomRetryIfRequested() | |
| 96 self.end_headers() | |
| 97 self.wfile.write('Error %d' % int(status_code)) | |
| 37 | 98 |
| 38 | 99 |
| 39 def main(): | 100 def main(): |
| 40 if len(sys.argv) != 2: | 101 if len(sys.argv) != 3: |
| 41 print "Usage: %s PORT" % sys.argv[0] | 102 print "Usage: %s LOCAL_IP PORT" % sys.argv[0] |
| 42 sys.exit(1) | 103 sys.exit(1) |
| 43 port = int(sys.argv[1]) | 104 RequestHandler.local_ip = sys.argv[1] |
| 105 port = int(sys.argv[2]) | |
| 106 RequestHandler.port = port | |
| 44 print "To stop the server, go to http://localhost:%d/quitquitquit" % port | 107 print "To stop the server, go to http://localhost:%d/quitquitquit" % port |
| 45 httpd = BaseHTTPServer.HTTPServer(('', port), RequestHandler) | 108 httpd = BaseHTTPServer.HTTPServer(('', port), RequestHandler) |
| 46 while RequestHandler.keep_running: | 109 while RequestHandler.keep_running: |
| 47 httpd.handle_request() | 110 httpd.handle_request() |
| 48 | 111 |
| 49 | 112 |
| 50 if __name__ == '__main__': | 113 if __name__ == '__main__': |
| 51 main() | 114 main() |
| OLD | NEW |