| OLD | NEW |
| 1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
| 2 # Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2008 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 It defaults to living on localhost:8888. | 9 It defaults to living on localhost:8888. |
| 10 It can use https if you specify the flag --https=CERT where CERT is the path | 10 It can use https if you specify the flag --https=CERT where CERT is the path |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 self.SlowServerHandler, | 124 self.SlowServerHandler, |
| 125 self.ContentTypeHandler, | 125 self.ContentTypeHandler, |
| 126 self.ServerRedirectHandler, | 126 self.ServerRedirectHandler, |
| 127 self.ClientRedirectHandler, | 127 self.ClientRedirectHandler, |
| 128 self.DefaultResponseHandler] | 128 self.DefaultResponseHandler] |
| 129 self._post_handlers = [ | 129 self._post_handlers = [ |
| 130 self.WriteFile, | 130 self.WriteFile, |
| 131 self.EchoTitleHandler, | 131 self.EchoTitleHandler, |
| 132 self.EchoAllHandler, | 132 self.EchoAllHandler, |
| 133 self.EchoHandler] + self._get_handlers | 133 self.EchoHandler] + self._get_handlers |
| 134 self._put_handlers = [ |
| 135 self.WriteFile, |
| 136 self.EchoTitleHandler, |
| 137 self.EchoAllHandler, |
| 138 self.EchoHandler] + self._get_handlers |
| 134 | 139 |
| 135 self._mime_types = { | 140 self._mime_types = { |
| 136 'gif': 'image/gif', | 141 'gif': 'image/gif', |
| 137 'jpeg' : 'image/jpeg', | 142 'jpeg' : 'image/jpeg', |
| 138 'jpg' : 'image/jpeg', | 143 'jpg' : 'image/jpeg', |
| 139 'xml' : 'text/xml' | 144 'xml' : 'text/xml' |
| 140 } | 145 } |
| 141 self._default_mime_type = 'text/html' | 146 self._default_mime_type = 'text/html' |
| 142 | 147 |
| 143 BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, | 148 BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 | 460 |
| 456 self.send_response(200) | 461 self.send_response(200) |
| 457 self.send_header('Content-type', 'text/html') | 462 self.send_header('Content-type', 'text/html') |
| 458 self.end_headers() | 463 self.end_headers() |
| 459 length = int(self.headers.getheader('content-length')) | 464 length = int(self.headers.getheader('content-length')) |
| 460 request = self.rfile.read(length) | 465 request = self.rfile.read(length) |
| 461 self.wfile.write(request) | 466 self.wfile.write(request) |
| 462 return True | 467 return True |
| 463 | 468 |
| 464 def WriteFile(self): | 469 def WriteFile(self): |
| 465 """This is handler dumps the content of POST request to a disk file into | 470 """This is handler dumps the content of POST/PUT request to a disk file |
| 466 the data_dir/dump. Sub-directories are not supported.""" | 471 into the data_dir/dump. Sub-directories are not supported.""" |
| 467 | 472 |
| 468 prefix='/writefile/' | 473 prefix='/writefile/' |
| 469 if not self.path.startswith(prefix): | 474 if not self.path.startswith(prefix): |
| 470 return False | 475 return False |
| 471 | 476 |
| 472 file_name = self.path[len(prefix):] | 477 file_name = self.path[len(prefix):] |
| 473 | 478 |
| 474 # do not allow fancy chars in file name | 479 # do not allow fancy chars in file name |
| 475 re.sub('[^a-zA-Z0-9_.-]+', '', file_name) | 480 re.sub('[^a-zA-Z0-9_.-]+', '', file_name) |
| 476 if len(file_name) and file_name[0] != '.': | 481 if len(file_name) and file_name[0] != '.': |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 self.send_response(200) | 518 self.send_response(200) |
| 514 self.send_header('Content-type', 'text/html') | 519 self.send_header('Content-type', 'text/html') |
| 515 self.end_headers() | 520 self.end_headers() |
| 516 self.wfile.write('<html><head><style>' | 521 self.wfile.write('<html><head><style>' |
| 517 'pre { border: 1px solid black; margin: 5px; padding: 5px }' | 522 'pre { border: 1px solid black; margin: 5px; padding: 5px }' |
| 518 '</style></head><body>' | 523 '</style></head><body>' |
| 519 '<div style="float: right">' | 524 '<div style="float: right">' |
| 520 '<a href="http://localhost:8888/echo">back to referring page</a></div>' | 525 '<a href="http://localhost:8888/echo">back to referring page</a></div>' |
| 521 '<h1>Request Body:</h1><pre>') | 526 '<h1>Request Body:</h1><pre>') |
| 522 | 527 |
| 523 if self.command == 'POST': | 528 if self.command == 'POST' or self.command == 'PUT': |
| 524 length = int(self.headers.getheader('content-length')) | 529 length = int(self.headers.getheader('content-length')) |
| 525 qs = self.rfile.read(length) | 530 qs = self.rfile.read(length) |
| 526 params = cgi.parse_qs(qs, keep_blank_values=1) | 531 params = cgi.parse_qs(qs, keep_blank_values=1) |
| 527 | 532 |
| 528 for param in params: | 533 for param in params: |
| 529 self.wfile.write('%s=%s\n' % (param, params[param][0])) | 534 self.wfile.write('%s=%s\n' % (param, params[param][0])) |
| 530 | 535 |
| 531 self.wfile.write('</pre>') | 536 self.wfile.write('</pre>') |
| 532 | 537 |
| 533 self.wfile.write('<h1>Request Headers:</h1><pre>%s</pre>' % self.headers) | 538 self.wfile.write('<h1>Request Headers:</h1><pre>%s</pre>' % self.headers) |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 | 597 |
| 593 def FileHandler(self): | 598 def FileHandler(self): |
| 594 """This handler sends the contents of the requested file. Wow, it's like | 599 """This handler sends the contents of the requested file. Wow, it's like |
| 595 a real webserver!""" | 600 a real webserver!""" |
| 596 | 601 |
| 597 prefix = self.server.file_root_url | 602 prefix = self.server.file_root_url |
| 598 if not self.path.startswith(prefix): | 603 if not self.path.startswith(prefix): |
| 599 return False | 604 return False |
| 600 | 605 |
| 601 # Consume a request body if present. | 606 # Consume a request body if present. |
| 602 if self.command == 'POST': | 607 if self.command == 'POST' or self.command == 'PUT' : |
| 603 self.rfile.read(int(self.headers.getheader('content-length'))) | 608 self.rfile.read(int(self.headers.getheader('content-length'))) |
| 604 | 609 |
| 605 file = self.path[len(prefix):] | 610 file = self.path[len(prefix):] |
| 606 if file.find('?') > -1: | 611 if file.find('?') > -1: |
| 607 # Ignore the query parameters entirely. | 612 # Ignore the query parameters entirely. |
| 608 url, querystring = file.split('?') | 613 url, querystring = file.split('?') |
| 609 else: | 614 else: |
| 610 url = file | 615 url = file |
| 611 entries = url.split('/') | 616 entries = url.split('/') |
| 612 path = os.path.join(self.server.data_dir, *entries) | 617 path = os.path.join(self.server.data_dir, *entries) |
| (...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1045 def do_GET(self): | 1050 def do_GET(self): |
| 1046 for handler in self._get_handlers: | 1051 for handler in self._get_handlers: |
| 1047 if handler(): | 1052 if handler(): |
| 1048 return | 1053 return |
| 1049 | 1054 |
| 1050 def do_POST(self): | 1055 def do_POST(self): |
| 1051 for handler in self._post_handlers: | 1056 for handler in self._post_handlers: |
| 1052 if handler(): | 1057 if handler(): |
| 1053 return | 1058 return |
| 1054 | 1059 |
| 1060 def do_PUT(self): |
| 1061 for handler in self._put_handlers: |
| 1062 if handler(): |
| 1063 return |
| 1064 |
| 1055 # called by the redirect handling function when there is no parameter | 1065 # called by the redirect handling function when there is no parameter |
| 1056 def sendRedirectHelp(self, redirect_name): | 1066 def sendRedirectHelp(self, redirect_name): |
| 1057 self.send_response(200) | 1067 self.send_response(200) |
| 1058 self.send_header('Content-type', 'text/html') | 1068 self.send_header('Content-type', 'text/html') |
| 1059 self.end_headers() | 1069 self.end_headers() |
| 1060 self.wfile.write('<html><body><h1>Error: no redirect destination</h1>') | 1070 self.wfile.write('<html><body><h1>Error: no redirect destination</h1>') |
| 1061 self.wfile.write('Use <pre>%s?http://dest...</pre>' % redirect_name) | 1071 self.wfile.write('Use <pre>%s?http://dest...</pre>' % redirect_name) |
| 1062 self.wfile.write('</body></html>') | 1072 self.wfile.write('</body></html>') |
| 1063 | 1073 |
| 1064 def MakeDumpDir(data_dir): | 1074 def MakeDumpDir(data_dir): |
| 1065 """Create directory named 'dump' where uploaded data via HTTP POST request | 1075 """Create directory named 'dump' where uploaded data via HTTP POST/PUT |
| 1066 will be stored. If the directory already exists all files and subdirectories | 1076 requests will be stored. If the directory already exists all files and |
| 1067 will be deleted.""" | 1077 subdirectories will be deleted.""" |
| 1068 dump_dir = os.path.join(data_dir, 'dump'); | 1078 dump_dir = os.path.join(data_dir, 'dump'); |
| 1069 if os.path.isdir(dump_dir): | 1079 if os.path.isdir(dump_dir): |
| 1070 shutil.rmtree(dump_dir) | 1080 shutil.rmtree(dump_dir) |
| 1071 os.mkdir(dump_dir) | 1081 os.mkdir(dump_dir) |
| 1072 | 1082 |
| 1073 def MakeDataDir(): | 1083 def MakeDataDir(): |
| 1074 if options.data_dir: | 1084 if options.data_dir: |
| 1075 if not os.path.isdir(options.data_dir): | 1085 if not os.path.isdir(options.data_dir): |
| 1076 print 'specified data dir not found: ' + options.data_dir + ' exiting...' | 1086 print 'specified data dir not found: ' + options.data_dir + ' exiting...' |
| 1077 return None | 1087 return None |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1172 help='Directory from which to read the files') | 1182 help='Directory from which to read the files') |
| 1173 option_parser.add_option('', '--https', dest='cert', | 1183 option_parser.add_option('', '--https', dest='cert', |
| 1174 help='Specify that https should be used, specify ' | 1184 help='Specify that https should be used, specify ' |
| 1175 'the path to the cert containing the private key ' | 1185 'the path to the cert containing the private key ' |
| 1176 'the server should use') | 1186 'the server should use') |
| 1177 option_parser.add_option('', '--file-root-url', default='/files/', | 1187 option_parser.add_option('', '--file-root-url', default='/files/', |
| 1178 help='Specify a root URL for files served.') | 1188 help='Specify a root URL for files served.') |
| 1179 options, args = option_parser.parse_args() | 1189 options, args = option_parser.parse_args() |
| 1180 | 1190 |
| 1181 sys.exit(main(options, args)) | 1191 sys.exit(main(options, args)) |
| OLD | NEW |