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 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 self.AuthDigestHandler, | 371 self.AuthDigestHandler, |
372 self.SlowServerHandler, | 372 self.SlowServerHandler, |
373 self.ChunkedServerHandler, | 373 self.ChunkedServerHandler, |
374 self.ContentTypeHandler, | 374 self.ContentTypeHandler, |
375 self.NoContentHandler, | 375 self.NoContentHandler, |
376 self.ServerRedirectHandler, | 376 self.ServerRedirectHandler, |
377 self.ClientRedirectHandler, | 377 self.ClientRedirectHandler, |
378 self.MultipartHandler, | 378 self.MultipartHandler, |
379 self.MultipartSlowHandler, | 379 self.MultipartSlowHandler, |
380 self.GetSSLSessionCacheHandler, | 380 self.GetSSLSessionCacheHandler, |
381 self.CloseSocketHandler, | |
382 self.DefaultResponseHandler] | 381 self.DefaultResponseHandler] |
383 post_handlers = [ | 382 post_handlers = [ |
384 self.EchoTitleHandler, | 383 self.EchoTitleHandler, |
385 self.EchoHandler, | 384 self.EchoHandler, |
386 self.DeviceManagementHandler, | 385 self.DeviceManagementHandler, |
387 self.PostOnlyFileHandler] + get_handlers | 386 self.PostOnlyFileHandler] + get_handlers |
388 put_handlers = [ | 387 put_handlers = [ |
389 self.EchoTitleHandler, | 388 self.EchoTitleHandler, |
390 self.EchoHandler] + get_handlers | 389 self.EchoHandler] + get_handlers |
391 head_handlers = [ | 390 head_handlers = [ |
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
916 | 915 |
917 def PostOnlyFileHandler(self): | 916 def PostOnlyFileHandler(self): |
918 """This handler sends the contents of the requested file on a POST.""" | 917 """This handler sends the contents of the requested file on a POST.""" |
919 prefix = urlparse.urljoin(self.server.file_root_url, 'post/') | 918 prefix = urlparse.urljoin(self.server.file_root_url, 'post/') |
920 if not self.path.startswith(prefix): | 919 if not self.path.startswith(prefix): |
921 return False | 920 return False |
922 self.ReadRequestBody() | 921 self.ReadRequestBody() |
923 return self._FileHandlerHelper(prefix) | 922 return self._FileHandlerHelper(prefix) |
924 | 923 |
925 def _FileHandlerHelper(self, prefix): | 924 def _FileHandlerHelper(self, prefix): |
926 old_protocol_version = self.protocol_version | |
927 _, _, url_path, _, query, _ = urlparse.urlparse(self.path) | 925 _, _, url_path, _, query, _ = urlparse.urlparse(self.path) |
928 sub_path = url_path[len(prefix):] | 926 sub_path = url_path[len(prefix):] |
929 entries = sub_path.split('/') | 927 entries = sub_path.split('/') |
930 file_path = os.path.join(self.server.data_dir, *entries) | 928 file_path = os.path.join(self.server.data_dir, *entries) |
931 if os.path.isdir(file_path): | 929 if os.path.isdir(file_path): |
932 file_path = os.path.join(file_path, 'index.html') | 930 file_path = os.path.join(file_path, 'index.html') |
933 | 931 |
934 if not os.path.isfile(file_path): | 932 if not os.path.isfile(file_path): |
935 print "File not found " + sub_path + " full path:" + file_path | 933 print "File not found " + sub_path + " full path:" + file_path |
936 self.send_error(404) | 934 self.send_error(404) |
937 return True | 935 return True |
938 | 936 |
939 f = open(file_path, "rb") | 937 f = open(file_path, "rb") |
940 data = f.read() | 938 data = f.read() |
941 f.close() | 939 f.close() |
942 | 940 |
943 data = self._ReplaceFileData(data, query) | 941 data = self._ReplaceFileData(data, query) |
944 | 942 |
945 # If file.mock-http-headers exists, it contains the headers we | 943 # If file.mock-http-headers exists, it contains the headers we |
946 # should send. Read them in and parse them. | 944 # should send. Read them in and parse them. |
947 headers_path = file_path + '.mock-http-headers' | 945 headers_path = file_path + '.mock-http-headers' |
948 if os.path.isfile(headers_path): | 946 if os.path.isfile(headers_path): |
949 f = open(headers_path, "r") | 947 f = open(headers_path, "r") |
950 | 948 |
951 # "HTTP/1.1 200 OK" | 949 # "HTTP/1.1 200 OK" |
952 response = f.readline() | 950 response = f.readline() |
953 http_major, http_minor, status_code = re.findall( | 951 status_code = re.findall('HTTP/\d+.\d+ (\d+)', response)[0] |
954 'HTTP/(\d+).(\d+) (\d+)', response)[0] | |
955 self.protocol_version = "HTTP/%s.%s" % (http_major, http_minor) | |
956 self.send_response(int(status_code)) | 952 self.send_response(int(status_code)) |
957 | 953 |
958 for line in f: | 954 for line in f: |
959 header_values = re.findall('(\S+):\s*(.*)', line) | 955 header_values = re.findall('(\S+):\s*(.*)', line) |
960 if len(header_values) > 0: | 956 if len(header_values) > 0: |
961 # "name: value" | 957 # "name: value" |
962 name, value = header_values[0] | 958 name, value = header_values[0] |
963 self.send_header(name, value) | 959 self.send_header(name, value) |
964 f.close() | 960 f.close() |
965 else: | 961 else: |
(...skipping 21 matching lines...) Expand all Loading... |
987 | 983 |
988 self.send_header('Content-Type', self.GetMIMETypeFromName(file_path)) | 984 self.send_header('Content-Type', self.GetMIMETypeFromName(file_path)) |
989 self.send_header('Accept-Ranges', 'bytes') | 985 self.send_header('Accept-Ranges', 'bytes') |
990 self.send_header('Content-Length', len(data)) | 986 self.send_header('Content-Length', len(data)) |
991 self.send_header('ETag', '\'' + file_path + '\'') | 987 self.send_header('ETag', '\'' + file_path + '\'') |
992 self.end_headers() | 988 self.end_headers() |
993 | 989 |
994 if (self.command != 'HEAD'): | 990 if (self.command != 'HEAD'): |
995 self.wfile.write(data) | 991 self.wfile.write(data) |
996 | 992 |
997 self.protocol_version = old_protocol_version | |
998 return True | 993 return True |
999 | 994 |
1000 def SetCookieHandler(self): | 995 def SetCookieHandler(self): |
1001 """This handler just sets a cookie, for testing cookie handling.""" | 996 """This handler just sets a cookie, for testing cookie handling.""" |
1002 | 997 |
1003 if not self._ShouldHandleRequest("/set-cookie"): | 998 if not self._ShouldHandleRequest("/set-cookie"): |
1004 return False | 999 return False |
1005 | 1000 |
1006 query_char = self.path.find('?') | 1001 query_char = self.path.find('?') |
1007 if query_char != -1: | 1002 if query_char != -1: |
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1425 self.send_header('Content-Type', 'text/plain') | 1420 self.send_header('Content-Type', 'text/plain') |
1426 self.end_headers() | 1421 self.end_headers() |
1427 try: | 1422 try: |
1428 for (action, sessionID) in self.server.session_cache.log: | 1423 for (action, sessionID) in self.server.session_cache.log: |
1429 self.wfile.write('%s\t%s\n' % (action, sessionID.encode('hex'))) | 1424 self.wfile.write('%s\t%s\n' % (action, sessionID.encode('hex'))) |
1430 except AttributeError, e: | 1425 except AttributeError, e: |
1431 self.wfile.write('Pass --https-record-resume in order to use' + | 1426 self.wfile.write('Pass --https-record-resume in order to use' + |
1432 ' this request') | 1427 ' this request') |
1433 return True | 1428 return True |
1434 | 1429 |
1435 def CloseSocketHandler(self): | |
1436 """Closes the socket without sending anything.""" | |
1437 | |
1438 if not self._ShouldHandleRequest('/close-socket'): | |
1439 return False | |
1440 | |
1441 self.wfile.close() | |
1442 return True | |
1443 | |
1444 def DefaultResponseHandler(self): | 1430 def DefaultResponseHandler(self): |
1445 """This is the catch-all response handler for requests that aren't handled | 1431 """This is the catch-all response handler for requests that aren't handled |
1446 by one of the special handlers above. | 1432 by one of the special handlers above. |
1447 Note that we specify the content-length as without it the https connection | 1433 Note that we specify the content-length as without it the https connection |
1448 is not closed properly (and the browser keeps expecting data).""" | 1434 is not closed properly (and the browser keeps expecting data).""" |
1449 | 1435 |
1450 contents = "Default response given for path: " + self.path | 1436 contents = "Default response given for path: " + self.path |
1451 self.send_response(200) | 1437 self.send_response(200) |
1452 self.send_header('Content-Type', 'text/html') | 1438 self.send_header('Content-Type', 'text/html') |
1453 self.send_header('Content-Length', len(contents)) | 1439 self.send_header('Content-Length', len(contents)) |
(...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2019 'random key if none is specified on the command ' | 2005 'random key if none is specified on the command ' |
2020 'line.') | 2006 'line.') |
2021 option_parser.add_option('', '--policy-user', default='user@example.com', | 2007 option_parser.add_option('', '--policy-user', default='user@example.com', |
2022 dest='policy_user', | 2008 dest='policy_user', |
2023 help='Specify the user name the server should ' | 2009 help='Specify the user name the server should ' |
2024 'report back to the client as the user owning the ' | 2010 'report back to the client as the user owning the ' |
2025 'token used for making the policy request.') | 2011 'token used for making the policy request.') |
2026 options, args = option_parser.parse_args() | 2012 options, args = option_parser.parse_args() |
2027 | 2013 |
2028 sys.exit(main(options, args)) | 2014 sys.exit(main(options, args)) |
OLD | NEW |