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.
21 function reportResult(txt) {
22 var element = document.createElement('p');
23 element.innerHTML = txt;
24 document.body.appendChild(element);
28 var response_code = document.getElementById('response_code');
30 xmlhttp = new XMLHttpRequest();
32 "http://%s:%d/%s?code=" + response_code.value,
34 xmlhttp.onreadystatechange = function() {
36 'readyState=' + xmlhttp.readyState + ', status=' + xmlhttp.status);
41 reportResult('Exception: ' + e);
48 <form action="javascript:fetch()">
49 Response code to get: <input id="response_code" type="text" value="503">
56 class RequestHandler(BaseHTTPServer
.BaseHTTPRequestHandler
):
62 if self
.path
== '/quitquitquit':
63 self
.send_response(200)
64 self
.send_header('Content-Type', 'text/plain')
66 self
.wfile
.write('QUITTING')
67 RequestHandler
.keep_running
= False
70 if self
.path
.startswith('/ajax/'):
71 self
.send_response(200)
72 self
.send_header('Content-Type', 'text/html')
74 self
.wfile
.write(AJAX_TEST_PAGE
% (self
.local_ip
,
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')
85 self
.wfile
.write('OK')
87 status_code
= int(params
['code'][0])
88 self
.send_response(status_code
)
90 self
.wfile
.write('Error %d' % int(status_code
))
94 if len(sys
.argv
) != 3:
95 print "Usage: %s LOCAL_IP PORT" % sys
.argv
[0]
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__':