| 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 self.wfile.write(data) | 902 self.wfile.write(data) |
| 903 | 903 |
| 904 return True | 904 return True |
| 905 | 905 |
| 906 def FileHandler(self): | 906 def FileHandler(self): |
| 907 """This handler sends the contents of the requested file. Wow, it's like | 907 """This handler sends the contents of the requested file. Wow, it's like |
| 908 a real webserver!""" | 908 a real webserver!""" |
| 909 prefix = self.server.file_root_url | 909 prefix = self.server.file_root_url |
| 910 if not self.path.startswith(prefix): | 910 if not self.path.startswith(prefix): |
| 911 return False | 911 return False |
| 912 # Consume a request body if present. | |
| 913 if self.command == 'POST' or self.command == 'PUT' : | |
| 914 self.ReadRequestBody() | |
| 915 return self._FileHandlerHelper(prefix) | 912 return self._FileHandlerHelper(prefix) |
| 916 | 913 |
| 917 def PostOnlyFileHandler(self): | 914 def PostOnlyFileHandler(self): |
| 918 """This handler sends the contents of the requested file on a POST.""" | 915 """This handler sends the contents of the requested file on a POST.""" |
| 919 prefix = urlparse.urljoin(self.server.file_root_url, 'post/') | 916 prefix = urlparse.urljoin(self.server.file_root_url, 'post/') |
| 920 if not self.path.startswith(prefix): | 917 if not self.path.startswith(prefix): |
| 921 return False | 918 return False |
| 922 self.ReadRequestBody() | |
| 923 return self._FileHandlerHelper(prefix) | 919 return self._FileHandlerHelper(prefix) |
| 924 | 920 |
| 925 def _FileHandlerHelper(self, prefix): | 921 def _FileHandlerHelper(self, prefix): |
| 926 old_protocol_version = self.protocol_version | 922 request_body = '' |
| 923 if self.command == 'POST' or self.command == 'PUT': |
| 924 # Consume a request body if present. |
| 925 request_body = self.ReadRequestBody() |
| 926 |
| 927 _, _, url_path, _, query, _ = urlparse.urlparse(self.path) | 927 _, _, url_path, _, query, _ = urlparse.urlparse(self.path) |
| 928 query_dict = cgi.parse_qs(query) |
| 929 |
| 930 expected_body = query_dict.get('expected_body', []) |
| 931 if expected_body and request_body not in expected_body: |
| 932 self.send_response(404) |
| 933 self.end_headers() |
| 934 self.wfile.write('') |
| 935 return True |
| 936 |
| 937 expected_headers = query_dict.get('expected_headers', []) |
| 938 for expected_header in expected_headers: |
| 939 header_name, expected_value = expected_header.split(':') |
| 940 if self.headers.getheader(header_name) != expected_value: |
| 941 self.send_response(404) |
| 942 self.end_headers() |
| 943 self.wfile.write('') |
| 944 return True |
| 945 |
| 928 sub_path = url_path[len(prefix):] | 946 sub_path = url_path[len(prefix):] |
| 929 entries = sub_path.split('/') | 947 entries = sub_path.split('/') |
| 930 file_path = os.path.join(self.server.data_dir, *entries) | 948 file_path = os.path.join(self.server.data_dir, *entries) |
| 931 if os.path.isdir(file_path): | 949 if os.path.isdir(file_path): |
| 932 file_path = os.path.join(file_path, 'index.html') | 950 file_path = os.path.join(file_path, 'index.html') |
| 933 | 951 |
| 934 if not os.path.isfile(file_path): | 952 if not os.path.isfile(file_path): |
| 935 print "File not found " + sub_path + " full path:" + file_path | 953 print "File not found " + sub_path + " full path:" + file_path |
| 936 self.send_error(404) | 954 self.send_error(404) |
| 937 return True | 955 return True |
| 938 | 956 |
| 939 f = open(file_path, "rb") | 957 f = open(file_path, "rb") |
| 940 data = f.read() | 958 data = f.read() |
| 941 f.close() | 959 f.close() |
| 942 | 960 |
| 943 data = self._ReplaceFileData(data, query) | 961 data = self._ReplaceFileData(data, query) |
| 944 | 962 |
| 963 old_protocol_version = self.protocol_version |
| 964 |
| 945 # If file.mock-http-headers exists, it contains the headers we | 965 # If file.mock-http-headers exists, it contains the headers we |
| 946 # should send. Read them in and parse them. | 966 # should send. Read them in and parse them. |
| 947 headers_path = file_path + '.mock-http-headers' | 967 headers_path = file_path + '.mock-http-headers' |
| 948 if os.path.isfile(headers_path): | 968 if os.path.isfile(headers_path): |
| 949 f = open(headers_path, "r") | 969 f = open(headers_path, "r") |
| 950 | 970 |
| 951 # "HTTP/1.1 200 OK" | 971 # "HTTP/1.1 200 OK" |
| 952 response = f.readline() | 972 response = f.readline() |
| 953 http_major, http_minor, status_code = re.findall( | 973 http_major, http_minor, status_code = re.findall( |
| 954 'HTTP/(\d+).(\d+) (\d+)', response)[0] | 974 'HTTP/(\d+).(\d+) (\d+)', response)[0] |
| (...skipping 1064 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2019 'random key if none is specified on the command ' | 2039 'random key if none is specified on the command ' |
| 2020 'line.') | 2040 'line.') |
| 2021 option_parser.add_option('', '--policy-user', default='user@example.com', | 2041 option_parser.add_option('', '--policy-user', default='user@example.com', |
| 2022 dest='policy_user', | 2042 dest='policy_user', |
| 2023 help='Specify the user name the server should ' | 2043 help='Specify the user name the server should ' |
| 2024 'report back to the client as the user owning the ' | 2044 'report back to the client as the user owning the ' |
| 2025 'token used for making the policy request.') | 2045 'token used for making the policy request.') |
| 2026 options, args = option_parser.parse_args() | 2046 options, args = option_parser.parse_args() |
| 2027 | 2047 |
| 2028 sys.exit(main(options, args)) | 2048 sys.exit(main(options, args)) |
| OLD | NEW |