OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/FTP/SYNC/TCP/UDP/ server used for testing Chrome. | 6 """This is a simple HTTP/FTP/SYNC/TCP/UDP/ 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 891 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
902 | 902 |
903 return True | 903 return True |
904 | 904 |
905 def FileHandler(self): | 905 def FileHandler(self): |
906 """This handler sends the contents of the requested file. Wow, it's like | 906 """This handler sends the contents of the requested file. Wow, it's like |
907 a real webserver!""" | 907 a real webserver!""" |
908 prefix = self.server.file_root_url | 908 prefix = self.server.file_root_url |
909 if not self.path.startswith(prefix): | 909 if not self.path.startswith(prefix): |
910 return False | 910 return False |
911 # Consume a request body if present. | 911 # Consume a request body if present. |
912 if self.command == 'POST' or self.command == 'PUT' : | 912 if self.command == 'POST' or self.command == 'PUT': |
913 self.ReadRequestBody() | 913 self.ReadRequestBody() |
914 _, _, _, _, query_str, _ = urlparse.urlparse(self.path) | |
cbentzel
2012/02/13 19:23:38
Feels like expected_headers and expected_body shou
benjhayden
2012/02/13 21:05:31
Done.
| |
915 query_dict = cgi.parse_qs(query_str) | |
916 expected_headers = query_dict.get('expected_headers', []) | |
917 for expected_header in expected_headers: | |
918 header_name, expected_value = expected_header.split(':') | |
919 if self.headers.getheader(header_name) != expected_value: | |
920 self.send_response(404) | |
921 self.end_headers() | |
922 self.wfile.write('') | |
923 return True | |
914 return self._FileHandlerHelper(prefix) | 924 return self._FileHandlerHelper(prefix) |
915 | 925 |
916 def PostOnlyFileHandler(self): | 926 def PostOnlyFileHandler(self): |
917 """This handler sends the contents of the requested file on a POST.""" | 927 """This handler sends the contents of the requested file on a POST.""" |
918 prefix = urlparse.urljoin(self.server.file_root_url, 'post/') | 928 prefix = urlparse.urljoin(self.server.file_root_url, 'post/') |
919 if not self.path.startswith(prefix): | 929 if not self.path.startswith(prefix): |
920 return False | 930 return False |
921 self.ReadRequestBody() | 931 body = self.ReadRequestBody() |
932 _, _, _, _, query_str, _ = urlparse.urlparse(self.path) | |
933 query_dict = cgi.parse_qs(query_str) | |
934 expected_body = query_dict.get('expected_body', []) | |
935 if expected_body and body not in expected_body: | |
936 self.send_response(404) | |
937 self.end_headers() | |
938 self.wfile.write('') | |
939 return True | |
922 return self._FileHandlerHelper(prefix) | 940 return self._FileHandlerHelper(prefix) |
923 | 941 |
924 def _FileHandlerHelper(self, prefix): | 942 def _FileHandlerHelper(self, prefix): |
925 _, _, url_path, _, query, _ = urlparse.urlparse(self.path) | 943 _, _, url_path, _, query, _ = urlparse.urlparse(self.path) |
926 sub_path = url_path[len(prefix):] | 944 sub_path = url_path[len(prefix):] |
927 entries = sub_path.split('/') | 945 entries = sub_path.split('/') |
928 file_path = os.path.join(self.server.data_dir, *entries) | 946 file_path = os.path.join(self.server.data_dir, *entries) |
929 if os.path.isdir(file_path): | 947 if os.path.isdir(file_path): |
930 file_path = os.path.join(file_path, 'index.html') | 948 file_path = os.path.join(file_path, 'index.html') |
931 | 949 |
(...skipping 1073 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2005 'random key if none is specified on the command ' | 2023 'random key if none is specified on the command ' |
2006 'line.') | 2024 'line.') |
2007 option_parser.add_option('', '--policy-user', default='user@example.com', | 2025 option_parser.add_option('', '--policy-user', default='user@example.com', |
2008 dest='policy_user', | 2026 dest='policy_user', |
2009 help='Specify the user name the server should ' | 2027 help='Specify the user name the server should ' |
2010 'report back to the client as the user owning the ' | 2028 'report back to the client as the user owning the ' |
2011 'token used for making the policy request.') | 2029 'token used for making the policy request.') |
2012 options, args = option_parser.parse_args() | 2030 options, args = option_parser.parse_args() |
2013 | 2031 |
2014 sys.exit(main(options, args)) | 2032 sys.exit(main(options, args)) |
OLD | NEW |