• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
5
6"""This is a simple HTTP server for manually testing exponential
7back-off functionality in Chrome.
8"""
9
10
11import BaseHTTPServer
12import sys
13import urlparse
14
15
16AJAX_TEST_PAGE = '''
17<html>
18<head>
19<script>
20
21function reportResult(txt) {
22  var element = document.createElement('p');
23  element.innerHTML = txt;
24  document.body.appendChild(element);
25}
26
27function 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</html>'''
54
55
56class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
57  keep_running = True
58  local_ip = ''
59  port = 0
60
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
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
79    params = urlparse.parse_qs(urlparse.urlparse(self.path).query)
80
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))
91
92
93def 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()
104
105
106if __name__ == '__main__':
107  main()
108