rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | # This is a simple HTTP server that's used by the |
| 8 | # libcurl_http_fetcher_unittest, though it could be used by others. In |
| 9 | # general, you can fork off this server, repeatedly request /test or |
| 10 | # some URL until that URL succeeds; then you know the server is |
| 11 | # running. The url /big returns 100,000 bytes of predictable data. The |
| 12 | # url /quitquitquit causes the server to exit. |
| 13 | |
| 14 | import SimpleHTTPServer, BaseHTTPServer, httplib |
| 15 | |
| 16 | class TestHttpRequestHandler (SimpleHTTPServer.SimpleHTTPRequestHandler): |
| 17 | def do_GET(self): |
| 18 | # Exit the server |
| 19 | if self.path == '/quitquitquit': |
| 20 | self.server.stop = True |
| 21 | |
| 22 | # Write 100,000 bytes out |
| 23 | if self.path == '/big': |
| 24 | self.send_response(200, 'OK') |
| 25 | self.send_header('Content-type', 'text/html') |
| 26 | self.end_headers() |
| 27 | for i in range(0, 10000): |
| 28 | try: |
| 29 | self.wfile.write('abcdefghij'); # 10 characters |
| 30 | except IOError: |
| 31 | return |
| 32 | return |
| 33 | |
| 34 | # Everything else |
| 35 | self.send_response(200, 'OK') |
| 36 | self.send_header('Content-type', 'text/html') |
| 37 | self.end_headers() |
| 38 | self.wfile.write('unhandled path') |
| 39 | |
| 40 | class TestHttpServer (BaseHTTPServer.HTTPServer): |
| 41 | def serve_forever(self): |
| 42 | self.stop = False |
| 43 | while not self.stop: |
| 44 | self.handle_request() |
| 45 | |
| 46 | def main(): |
| 47 | # TODO(adlr): Choose a port that works with build bots and report it to |
| 48 | # caller. |
| 49 | # WARNING, if you update this, you must also update http_fetcher_unittest.cc |
| 50 | port = 8080 |
| 51 | server = TestHttpServer(('', 8080), TestHttpRequestHandler) |
| 52 | server.serve_forever() |
| 53 | |
| 54 | if __name__ == '__main__': |
| 55 | main() |