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 893 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
904 return True | 904 return True |
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 expected_password = 'secret' | |
915 realm = 'testrealm' | |
916 set_cookie_if_challenged = False | |
914 | 917 |
915 set_cookie_if_challenged = self.path.find('?set-cookie-if-challenged') > 0 | 918 _, _, url_path, _, query, _ = urlparse.urlparse(self.path) |
919 for (name, value) in urlparse.parse_qsl(query, True): | |
cbentzel
2010/12/17 22:02:55
Use cgi.parse_qs instead of urlparse.parse_qs [and
asanka (google)
2010/12/20 17:44:08
parse_qsl() returns the query parameters as a list
| |
920 if name == 'set-cookie-if-challenged': | |
921 set_cookie_if_challenged = True | |
922 elif name == 'password': | |
923 expected_password = value | |
924 elif name == 'realm': | |
925 realm = value | |
916 | 926 |
917 auth = self.headers.getheader('authorization') | 927 auth = self.headers.getheader('authorization') |
918 try: | 928 try: |
919 if not auth: | 929 if not auth: |
920 raise Exception('no auth') | 930 raise Exception('no auth') |
921 b64str = re.findall(r'Basic (\S+)', auth)[0] | 931 b64str = re.findall(r'Basic (\S+)', auth)[0] |
922 userpass = base64.b64decode(b64str) | 932 userpass = base64.b64decode(b64str) |
923 username, password = re.findall(r'([^:]+):(\S+)', userpass)[0] | 933 username, password = re.findall(r'([^:]+):(\S+)', userpass)[0] |
924 if password != 'secret': | 934 if password != expected_password: |
925 raise Exception('wrong password') | 935 raise Exception('wrong password') |
926 except Exception, e: | 936 except Exception, e: |
927 # Authentication failed. | 937 # Authentication failed. |
928 self.send_response(401) | 938 self.send_response(401) |
929 self.send_header('WWW-Authenticate', 'Basic realm="testrealm"') | 939 self.send_header('WWW-Authenticate', 'Basic realm="%s"' % realm) |
930 self.send_header('Content-type', 'text/html') | 940 self.send_header('Content-type', 'text/html') |
931 if set_cookie_if_challenged: | 941 if set_cookie_if_challenged: |
932 self.send_header('Set-Cookie', 'got_challenged=true') | 942 self.send_header('Set-Cookie', 'got_challenged=true') |
933 self.end_headers() | 943 self.end_headers() |
934 self.wfile.write('<html><head>') | 944 self.wfile.write('<html><head>') |
935 self.wfile.write('<title>Denied: %s</title>' % e) | 945 self.wfile.write('<title>Denied: %s</title>' % e) |
936 self.wfile.write('</head><body>') | 946 self.wfile.write('</head><body>') |
937 self.wfile.write('auth=%s<p>' % auth) | 947 self.wfile.write('auth=%s<p>' % auth) |
938 self.wfile.write('b64str=%s<p>' % b64str) | 948 self.wfile.write('b64str=%s<p>' % b64str) |
939 self.wfile.write('username: %s<p>' % username) | 949 self.wfile.write('username: %s<p>' % username) |
940 self.wfile.write('userpass: %s<p>' % userpass) | 950 self.wfile.write('userpass: %s<p>' % userpass) |
941 self.wfile.write('password: %s<p>' % password) | 951 self.wfile.write('password: %s<p>' % password) |
942 self.wfile.write('You sent:<br>%s<p>' % self.headers) | 952 self.wfile.write('You sent:<br>%s<p>' % self.headers) |
943 self.wfile.write('</body></html>') | 953 self.wfile.write('</body></html>') |
944 return True | 954 return True |
945 | 955 |
946 # Authentication successful. (Return a cachable response to allow for | 956 # Authentication successful. (Return a cachable response to allow for |
947 # testing cached pages that require authentication.) | 957 # testing cached pages that require authentication.) |
948 if_none_match = self.headers.getheader('if-none-match') | 958 if_none_match = self.headers.getheader('if-none-match') |
949 if if_none_match == "abc": | 959 if if_none_match == "abc": |
950 self.send_response(304) | 960 self.send_response(304) |
951 self.end_headers() | 961 self.end_headers() |
952 else: | 962 else: |
953 self.send_response(200) | 963 self.send_response(200) |
954 self.send_header('Content-type', 'text/html') | 964 self.send_header('Content-type', 'text/html') |
cbentzel
2010/12/17 22:02:55
Ah, I remember the missing comment. I thought it w
asanka (google)
2010/12/20 17:44:08
Done.
| |
955 self.send_header('Cache-control', 'max-age=60000') | 965 self.send_header('Cache-control', 'max-age=60000') |
956 self.send_header('Etag', 'abc') | 966 self.send_header('Etag', 'abc') |
957 self.end_headers() | 967 self.end_headers() |
958 self.wfile.write('<html><head>') | 968 self.wfile.write('<html><head>') |
959 self.wfile.write('<title>%s/%s</title>' % (username, password)) | 969 self.wfile.write('<title>%s/%s</title>' % (username, password)) |
960 self.wfile.write('</head><body>') | 970 self.wfile.write('</head><body>') |
961 self.wfile.write('auth=%s<p>' % auth) | 971 self.wfile.write('auth=%s<p>' % auth) |
962 self.wfile.write('You sent:<br>%s<p>' % self.headers) | 972 self.wfile.write('You sent:<br>%s<p>' % self.headers) |
963 self.wfile.write('</body></html>') | 973 self.wfile.write('</body></html>') |
964 | 974 |
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1468 'option may appear multiple times, indicating ' | 1478 'option may appear multiple times, indicating ' |
1469 'multiple algorithms should be enabled.'); | 1479 'multiple algorithms should be enabled.'); |
1470 option_parser.add_option('', '--file-root-url', default='/files/', | 1480 option_parser.add_option('', '--file-root-url', default='/files/', |
1471 help='Specify a root URL for files served.') | 1481 help='Specify a root URL for files served.') |
1472 option_parser.add_option('', '--startup-pipe', type='int', | 1482 option_parser.add_option('', '--startup-pipe', type='int', |
1473 dest='startup_pipe', | 1483 dest='startup_pipe', |
1474 help='File handle of pipe to parent process') | 1484 help='File handle of pipe to parent process') |
1475 options, args = option_parser.parse_args() | 1485 options, args = option_parser.parse_args() |
1476 | 1486 |
1477 sys.exit(main(options, args)) | 1487 sys.exit(main(options, args)) |
OLD | NEW |