Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / tools / testserver / backoff_server.py
blobca2c57cbf12d0f8a6ce99bdf3d3011b329f9f004
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """This is a simple HTTP server for manually testing exponential
7 back-off functionality in Chrome.
8 """
11 import BaseHTTPServer
12 import sys
13 import urlparse
16 AJAX_TEST_PAGE = '''
17 <html>
18 <head>
19 <script>
21 function reportResult(txt) {
22 var element = document.createElement('p');
23 element.innerHTML = txt;
24 document.body.appendChild(element);
27 function fetch() {
28 var response_code = document.getElementById('response_code');
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);
38 try {
39 xmlhttp.send(null);
40 } catch (e) {
41 reportResult('Exception: ' + e);
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 </html>'''
56 class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
57 keep_running = True
58 local_ip = ''
59 port = 0
61 def do_GET(self):
62 if self.path == '/quitquitquit':
63 self.send_response(200)
64 self.send_header('Content-Type', 'text/plain')
65 self.end_headers()
66 self.wfile.write('QUITTING')
67 RequestHandler.keep_running = False
68 return
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
79 params = urlparse.parse_qs(urlparse.urlparse(self.path).query)
81 if not params or not 'code' in params or params['code'][0] == '200':
82 self.send_response(200)
83 self.send_header('Content-Type', 'text/plain')
84 self.end_headers()
85 self.wfile.write('OK')
86 else:
87 status_code = int(params['code'][0])
88 self.send_response(status_code)
89 self.end_headers()
90 self.wfile.write('Error %d' % int(status_code))
93 def main():
94 if len(sys.argv) != 3:
95 print "Usage: %s LOCAL_IP PORT" % sys.argv[0]
96 sys.exit(1)
97 RequestHandler.local_ip = sys.argv[1]
98 port = int(sys.argv[2])
99 RequestHandler.port = port
100 print "To stop the server, go to http://localhost:%d/quitquitquit" % port
101 httpd = BaseHTTPServer.HTTPServer(('', port), RequestHandler)
102 while RequestHandler.keep_running:
103 httpd.handle_request()
106 if __name__ == '__main__':
107 main()