OLD | NEW |
---|---|
1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
2 # Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2010 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 used for testing Chrome. | 6 """This is a simple HTTP server used for testing Chrome. |
7 | 7 |
8 It supports several test URLs, as specified by the handlers in TestPageHandler. | 8 It supports several test URLs, as specified by the handlers in TestPageHandler. |
9 By default, it listens on an ephemeral port and sends the port number back to | 9 By default, it listens on an ephemeral port and sends the port number back to |
10 the originating process over a pipe. The originating process can specify an | 10 the originating process over a pipe. The originating process can specify an |
(...skipping 894 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
905 | 905 |
906 def AuthBasicHandler(self): | 906 def AuthBasicHandler(self): |
907 """This handler tests 'Basic' authentication. It just sends a page with | 907 """This handler tests 'Basic' authentication. It just sends a page with |
908 title 'user/pass' if you succeed.""" | 908 title 'user/pass' if you succeed.""" |
909 | 909 |
910 if not self._ShouldHandleRequest("/auth-basic"): | 910 if not self._ShouldHandleRequest("/auth-basic"): |
911 return False | 911 return False |
912 | 912 |
913 username = userpass = password = b64str = "" | 913 username = userpass = password = b64str = "" |
914 | 914 |
915 set_cookie_if_challenged = self.path.find('?set-cookie-if-challenged') > 0 | 915 set_cookie_if_challenged = self.path.find('?set-cookie-if-challenged') > 0 |
cbentzel
2010/12/17 19:22:56
Could you shift this to urlparse and cgi.parse_qs
| |
916 | 916 |
917 pw_list = re.findall(r'[&?]password=([^&]+)', self.path) + ['secret'] | |
918 realm_list = re.findall(r'[&?]realm=([^&]+)', self.path) + ['testrealm'] | |
919 expected_password = pw_list[0] | |
920 realm = realm_list[0] | |
921 | |
917 auth = self.headers.getheader('authorization') | 922 auth = self.headers.getheader('authorization') |
918 try: | 923 try: |
919 if not auth: | 924 if not auth: |
920 raise Exception('no auth') | 925 raise Exception('no auth') |
921 b64str = re.findall(r'Basic (\S+)', auth)[0] | 926 b64str = re.findall(r'Basic (\S+)', auth)[0] |
922 userpass = base64.b64decode(b64str) | 927 userpass = base64.b64decode(b64str) |
923 username, password = re.findall(r'([^:]+):(\S+)', userpass)[0] | 928 username, password = re.findall(r'([^:]+):(\S+)', userpass)[0] |
924 if password != 'secret': | 929 if password != expected_password: |
925 raise Exception('wrong password') | 930 raise Exception('wrong password') |
926 except Exception, e: | 931 except Exception, e: |
927 # Authentication failed. | 932 # Authentication failed. |
928 self.send_response(401) | 933 self.send_response(401) |
929 self.send_header('WWW-Authenticate', 'Basic realm="testrealm"') | 934 self.send_header('WWW-Authenticate', 'Basic realm="%s"' % realm) |
930 self.send_header('Content-type', 'text/html') | 935 self.send_header('Content-type', 'text/html') |
931 if set_cookie_if_challenged: | 936 if set_cookie_if_challenged: |
932 self.send_header('Set-Cookie', 'got_challenged=true') | 937 self.send_header('Set-Cookie', 'got_challenged=true') |
933 self.end_headers() | 938 self.end_headers() |
934 self.wfile.write('<html><head>') | 939 self.wfile.write('<html><head>') |
935 self.wfile.write('<title>Denied: %s</title>' % e) | 940 self.wfile.write('<title>Denied: %s</title>' % e) |
936 self.wfile.write('</head><body>') | 941 self.wfile.write('</head><body>') |
937 self.wfile.write('auth=%s<p>' % auth) | 942 self.wfile.write('auth=%s<p>' % auth) |
938 self.wfile.write('b64str=%s<p>' % b64str) | 943 self.wfile.write('b64str=%s<p>' % b64str) |
939 self.wfile.write('username: %s<p>' % username) | 944 self.wfile.write('username: %s<p>' % username) |
940 self.wfile.write('userpass: %s<p>' % userpass) | 945 self.wfile.write('userpass: %s<p>' % userpass) |
941 self.wfile.write('password: %s<p>' % password) | 946 self.wfile.write('password: %s<p>' % password) |
942 self.wfile.write('You sent:<br>%s<p>' % self.headers) | 947 self.wfile.write('You sent:<br>%s<p>' % self.headers) |
943 self.wfile.write('</body></html>') | 948 self.wfile.write('</body></html>') |
944 return True | 949 return True |
945 | 950 |
946 # Authentication successful. (Return a cachable response to allow for | 951 # Authentication successful. (Return a cachable response to allow for |
947 # testing cached pages that require authentication.) | 952 # testing cached pages that require authentication.) |
948 if_none_match = self.headers.getheader('if-none-match') | 953 if_none_match = self.headers.getheader('if-none-match') |
949 if if_none_match == "abc": | 954 if if_none_match == "abc": |
950 self.send_response(304) | 955 self.send_response(304) |
951 self.end_headers() | 956 self.end_headers() |
952 else: | 957 else: |
953 self.send_response(200) | 958 self.send_response(200) |
954 self.send_header('Content-type', 'text/html') | 959 self.send_header('Content-type', 'text/html') |
cbentzel
2010/12/17 19:22:56
It seems a bit weird to return text/html when your
| |
955 self.send_header('Cache-control', 'max-age=60000') | 960 self.send_header('Cache-control', 'max-age=60000') |
956 self.send_header('Etag', 'abc') | 961 self.send_header('Etag', 'abc') |
957 self.end_headers() | 962 self.end_headers() |
958 self.wfile.write('<html><head>') | 963 self.wfile.write('<html><head>') |
959 self.wfile.write('<title>%s/%s</title>' % (username, password)) | 964 self.wfile.write('<title>%s/%s</title>' % (username, password)) |
960 self.wfile.write('</head><body>') | 965 self.wfile.write('</head><body>') |
961 self.wfile.write('auth=%s<p>' % auth) | 966 self.wfile.write('auth=%s<p>' % auth) |
962 self.wfile.write('You sent:<br>%s<p>' % self.headers) | 967 self.wfile.write('You sent:<br>%s<p>' % self.headers) |
963 self.wfile.write('</body></html>') | 968 self.wfile.write('</body></html>') |
964 | 969 |
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1468 'option may appear multiple times, indicating ' | 1473 'option may appear multiple times, indicating ' |
1469 'multiple algorithms should be enabled.'); | 1474 'multiple algorithms should be enabled.'); |
1470 option_parser.add_option('', '--file-root-url', default='/files/', | 1475 option_parser.add_option('', '--file-root-url', default='/files/', |
1471 help='Specify a root URL for files served.') | 1476 help='Specify a root URL for files served.') |
1472 option_parser.add_option('', '--startup-pipe', type='int', | 1477 option_parser.add_option('', '--startup-pipe', type='int', |
1473 dest='startup_pipe', | 1478 dest='startup_pipe', |
1474 help='File handle of pipe to parent process') | 1479 help='File handle of pipe to parent process') |
1475 options, args = option_parser.parse_args() | 1480 options, args = option_parser.parse_args() |
1476 | 1481 |
1477 sys.exit(main(options, args)) | 1482 sys.exit(main(options, args)) |
OLD | NEW |