OLD | NEW |
---|---|
1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 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 12 matching lines...) Expand all Loading... | |
23 import re | 23 import re |
24 import select | 24 import select |
25 import simplejson | 25 import simplejson |
26 import SocketServer | 26 import SocketServer |
27 import socket | 27 import socket |
28 import sys | 28 import sys |
29 import struct | 29 import struct |
30 import time | 30 import time |
31 import urlparse | 31 import urlparse |
32 import warnings | 32 import warnings |
33 import zlib | |
33 | 34 |
34 # Ignore deprecation warnings, they make our output more cluttered. | 35 # Ignore deprecation warnings, they make our output more cluttered. |
35 warnings.filterwarnings("ignore", category=DeprecationWarning) | 36 warnings.filterwarnings("ignore", category=DeprecationWarning) |
36 | 37 |
37 import pyftpdlib.ftpserver | 38 import pyftpdlib.ftpserver |
38 import tlslite | 39 import tlslite |
39 import tlslite.api | 40 import tlslite.api |
40 | 41 |
41 try: | 42 try: |
42 import hashlib | 43 import hashlib |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
273 self.CacheMustRevalidateHandler, | 274 self.CacheMustRevalidateHandler, |
274 self.CacheMustRevalidateMaxAgeHandler, | 275 self.CacheMustRevalidateMaxAgeHandler, |
275 self.CacheNoStoreHandler, | 276 self.CacheNoStoreHandler, |
276 self.CacheNoStoreMaxAgeHandler, | 277 self.CacheNoStoreMaxAgeHandler, |
277 self.CacheNoTransformHandler, | 278 self.CacheNoTransformHandler, |
278 self.DownloadHandler, | 279 self.DownloadHandler, |
279 self.DownloadFinishHandler, | 280 self.DownloadFinishHandler, |
280 self.EchoHeader, | 281 self.EchoHeader, |
281 self.EchoHeaderCache, | 282 self.EchoHeaderCache, |
282 self.EchoAllHandler, | 283 self.EchoAllHandler, |
284 self.ZipFileHandler, | |
283 self.FileHandler, | 285 self.FileHandler, |
284 self.SetCookieHandler, | 286 self.SetCookieHandler, |
285 self.AuthBasicHandler, | 287 self.AuthBasicHandler, |
286 self.AuthDigestHandler, | 288 self.AuthDigestHandler, |
287 self.SlowServerHandler, | 289 self.SlowServerHandler, |
288 self.ChunkedServerHandler, | 290 self.ChunkedServerHandler, |
289 self.ContentTypeHandler, | 291 self.ContentTypeHandler, |
290 self.NoContentHandler, | 292 self.NoContentHandler, |
291 self.ServerRedirectHandler, | 293 self.ServerRedirectHandler, |
292 self.ClientRedirectHandler, | 294 self.ClientRedirectHandler, |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
740 if len(replace_text_args) != 2: | 742 if len(replace_text_args) != 2: |
741 raise ValueError( | 743 raise ValueError( |
742 'replace_text must be of form old_text:new_text. Actual value: %s' % | 744 'replace_text must be of form old_text:new_text. Actual value: %s' % |
743 replace_text_value) | 745 replace_text_value) |
744 old_text_b64, new_text_b64 = replace_text_args | 746 old_text_b64, new_text_b64 = replace_text_args |
745 old_text = base64.urlsafe_b64decode(old_text_b64) | 747 old_text = base64.urlsafe_b64decode(old_text_b64) |
746 new_text = base64.urlsafe_b64decode(new_text_b64) | 748 new_text = base64.urlsafe_b64decode(new_text_b64) |
747 data = data.replace(old_text, new_text) | 749 data = data.replace(old_text, new_text) |
748 return data | 750 return data |
749 | 751 |
752 def ZipFileHandler(self): | |
753 """This handler sends the contents of the requested file in compressed form. | |
754 Can pass in a parameter that specifies that the content length be | |
755 C - the compressed size (OK), | |
756 U - the uncompressed size (Non-standard, but handled), | |
757 S - less than compressed (OK because we keep going), | |
758 M - larger than compressed but less than uncompressed (an error), | |
759 L - larger than uncompressed (an error) | |
760 Example: compressedfiles/Picture_1.doc?C | |
761 """ | |
762 | |
763 prefix = "/compressedfiles/" | |
764 if not self.path.startswith(prefix): | |
765 return False | |
766 | |
767 # Consume a request body if present. | |
768 if self.command == 'POST' or self.command == 'PUT' : | |
769 self.ReadRequestBody() | |
770 | |
771 _, _, url_path, _, query, _ = urlparse.urlparse(self.path) | |
772 | |
773 if not query in ('C', 'U', 'S', 'M', 'L'): | |
774 return False | |
775 | |
776 sub_path = url_path[len(prefix):] | |
777 entries = sub_path.split('/') | |
778 file_path = os.path.join(self.server.data_dir, *entries) | |
779 if os.path.isdir(file_path): | |
780 file_path = os.path.join(file_path, 'index.html') | |
781 | |
782 if not os.path.isfile(file_path): | |
783 print "File not found " + sub_path + " full path:" + file_path | |
784 self.send_error(404) | |
785 return True | |
786 | |
787 f = open(file_path, "rb") | |
788 data = f.read() | |
789 uncompressed_len = len(data) | |
790 f.close() | |
791 | |
792 # Compress the data. | |
793 data = zlib.compress(data) | |
794 compressed_len = len(data) | |
795 | |
796 content_length = compressed_len | |
797 if query == 'U': | |
798 content_length = uncompressed_len | |
799 elif query == 'S': | |
800 content_length = compressed_len / 2 | |
801 elif query == 'M': | |
802 content_length = (compressed_len + uncompressed_len) / 2 | |
803 elif query == 'L': | |
804 content_length = compressed_len + uncompressed_len | |
805 | |
806 self.send_response(200) | |
807 self.send_header('Content-type', 'application/msword') | |
808 self.send_header('Content-encoding', 'deflate') | |
809 self.send_header('Connection', 'close') | |
810 self.send_header('Content-Length', content_length) | |
811 self.send_header('ETag', '\'' + file_path + '\'') | |
rvargas (doing something else)
2011/05/24 18:25:35
nit: make sure the protocol version is 1.1 or don'
| |
812 self.end_headers() | |
813 | |
814 self.wfile.write(data) | |
815 | |
816 return True | |
817 | |
750 def FileHandler(self): | 818 def FileHandler(self): |
751 """This handler sends the contents of the requested file. Wow, it's like | 819 """This handler sends the contents of the requested file. Wow, it's like |
752 a real webserver!""" | 820 a real webserver!""" |
753 | 821 |
754 prefix = self.server.file_root_url | 822 prefix = self.server.file_root_url |
755 if not self.path.startswith(prefix): | 823 if not self.path.startswith(prefix): |
756 return False | 824 return False |
757 | 825 |
758 # Consume a request body if present. | 826 # Consume a request body if present. |
759 if self.command == 'POST' or self.command == 'PUT' : | 827 if self.command == 'POST' or self.command == 'PUT' : |
(...skipping 789 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1549 'random key if none is specified on the command ' | 1617 'random key if none is specified on the command ' |
1550 'line.') | 1618 'line.') |
1551 option_parser.add_option('', '--policy-user', default='user@example.com', | 1619 option_parser.add_option('', '--policy-user', default='user@example.com', |
1552 dest='policy_user', | 1620 dest='policy_user', |
1553 help='Specify the user name the server should ' | 1621 help='Specify the user name the server should ' |
1554 'report back to the client as the user owning the ' | 1622 'report back to the client as the user owning the ' |
1555 'token used for making the policy request.') | 1623 'token used for making the policy request.') |
1556 options, args = option_parser.parse_args() | 1624 options, args = option_parser.parse_args() |
1557 | 1625 |
1558 sys.exit(main(options, args)) | 1626 sys.exit(main(options, args)) |
OLD | NEW |