Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: net/tools/testserver/testserver.py

Issue 6134003: Prototype of chunked transfer encoded POST. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 #!/usr/bin/python2.4 1 #!/usr/bin/python2.4
2 # Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2006-2010 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 575 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 # request is subject to value of the request header being echoed. 586 # request is subject to value of the request header being echoed.
587 if len(header_name) > 0: 587 if len(header_name) > 0:
588 self.send_header('Vary', header_name) 588 self.send_header('Vary', header_name)
589 self.end_headers() 589 self.end_headers()
590 590
591 if len(header_name) > 0: 591 if len(header_name) > 0:
592 self.wfile.write(self.headers.getheader(header_name)) 592 self.wfile.write(self.headers.getheader(header_name))
593 593
594 return True 594 return True
595 595
596 def ReadRequestBody(self):
597 """This function reads the body of the current HTTP request, handling
598 both plain and chunked transfer encoded requests."""
599
600 if self.headers.getheader('transfer-encoding') != 'chunked':
601 length = int(self.headers.getheader('content-length'))
602 return self.rfile.read(length)
603
604 # Read the request body as chunks.
605 body = ""
606 while True:
607 line = self.rfile.readline()
608 length = int(line, 16)
609 if length == 0:
610 self.rfile.readline()
611 break
612 body += self.rfile.read(length)
613 self.rfile.read(2)
614 return body
615
596 def EchoHandler(self): 616 def EchoHandler(self):
597 """This handler just echoes back the payload of the request, for testing 617 """This handler just echoes back the payload of the request, for testing
598 form submission.""" 618 form submission."""
599 619
600 if not self._ShouldHandleRequest("/echo"): 620 if not self._ShouldHandleRequest("/echo"):
601 return False 621 return False
602 622
603 self.send_response(200) 623 self.send_response(200)
604 self.send_header('Content-type', 'text/html') 624 self.send_header('Content-type', 'text/html')
605 self.end_headers() 625 self.end_headers()
606 length = int(self.headers.getheader('content-length')) 626 self.wfile.write(self.ReadRequestBody())
607 request = self.rfile.read(length)
608 self.wfile.write(request)
609 return True 627 return True
610 628
611 def EchoTitleHandler(self): 629 def EchoTitleHandler(self):
612 """This handler is like Echo, but sets the page title to the request.""" 630 """This handler is like Echo, but sets the page title to the request."""
613 631
614 if not self._ShouldHandleRequest("/echotitle"): 632 if not self._ShouldHandleRequest("/echotitle"):
615 return False 633 return False
616 634
617 self.send_response(200) 635 self.send_response(200)
618 self.send_header('Content-type', 'text/html') 636 self.send_header('Content-type', 'text/html')
619 self.end_headers() 637 self.end_headers()
620 length = int(self.headers.getheader('content-length')) 638 request = self.ReadRequestBody()
621 request = self.rfile.read(length)
622 self.wfile.write('<html><head><title>') 639 self.wfile.write('<html><head><title>')
623 self.wfile.write(request) 640 self.wfile.write(request)
624 self.wfile.write('</title></head></html>') 641 self.wfile.write('</title></head></html>')
625 return True 642 return True
626 643
627 def EchoAllHandler(self): 644 def EchoAllHandler(self):
628 """This handler yields a (more) human-readable page listing information 645 """This handler yields a (more) human-readable page listing information
629 about the request header & contents.""" 646 about the request header & contents."""
630 647
631 if not self._ShouldHandleRequest("/echoall"): 648 if not self._ShouldHandleRequest("/echoall"):
632 return False 649 return False
633 650
634 self.send_response(200) 651 self.send_response(200)
635 self.send_header('Content-type', 'text/html') 652 self.send_header('Content-type', 'text/html')
636 self.end_headers() 653 self.end_headers()
637 self.wfile.write('<html><head><style>' 654 self.wfile.write('<html><head><style>'
638 'pre { border: 1px solid black; margin: 5px; padding: 5px }' 655 'pre { border: 1px solid black; margin: 5px; padding: 5px }'
639 '</style></head><body>' 656 '</style></head><body>'
640 '<div style="float: right">' 657 '<div style="float: right">'
641 '<a href="/echo">back to referring page</a></div>' 658 '<a href="/echo">back to referring page</a></div>'
642 '<h1>Request Body:</h1><pre>') 659 '<h1>Request Body:</h1><pre>')
643 660
644 if self.command == 'POST' or self.command == 'PUT': 661 if self.command == 'POST' or self.command == 'PUT':
645 length = int(self.headers.getheader('content-length')) 662 qs = self.ReadRequestBody()
646 qs = self.rfile.read(length)
647 params = cgi.parse_qs(qs, keep_blank_values=1) 663 params = cgi.parse_qs(qs, keep_blank_values=1)
648 664
649 for param in params: 665 for param in params:
650 self.wfile.write('%s=%s\n' % (param, params[param][0])) 666 self.wfile.write('%s=%s\n' % (param, params[param][0]))
651 667
652 self.wfile.write('</pre>') 668 self.wfile.write('</pre>')
653 669
654 self.wfile.write('<h1>Request Headers:</h1><pre>%s</pre>' % self.headers) 670 self.wfile.write('<h1>Request Headers:</h1><pre>%s</pre>' % self.headers)
655 671
656 self.wfile.write('</body></html>') 672 self.wfile.write('</body></html>')
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 def FileHandler(self): 754 def FileHandler(self):
739 """This handler sends the contents of the requested file. Wow, it's like 755 """This handler sends the contents of the requested file. Wow, it's like
740 a real webserver!""" 756 a real webserver!"""
741 757
742 prefix = self.server.file_root_url 758 prefix = self.server.file_root_url
743 if not self.path.startswith(prefix): 759 if not self.path.startswith(prefix):
744 return False 760 return False
745 761
746 # Consume a request body if present. 762 # Consume a request body if present.
747 if self.command == 'POST' or self.command == 'PUT' : 763 if self.command == 'POST' or self.command == 'PUT' :
748 self.rfile.read(int(self.headers.getheader('content-length'))) 764 self.ReadRequestBody()
749 765
750 _, _, url_path, _, query, _ = urlparse.urlparse(self.path) 766 _, _, url_path, _, query, _ = urlparse.urlparse(self.path)
751 sub_path = url_path[len(prefix):] 767 sub_path = url_path[len(prefix):]
752 entries = sub_path.split('/') 768 entries = sub_path.split('/')
753 file_path = os.path.join(self.server.data_dir, *entries) 769 file_path = os.path.join(self.server.data_dir, *entries)
754 if os.path.isdir(file_path): 770 if os.path.isdir(file_path):
755 file_path = os.path.join(file_path, 'index.html') 771 file_path = os.path.join(file_path, 'index.html')
756 772
757 if not os.path.isfile(file_path): 773 if not os.path.isfile(file_path):
758 print "File not found " + sub_path + " full path:" + file_path 774 print "File not found " + sub_path + " full path:" + file_path
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after
1255 self.send_header("Content-Length", len(contents)) 1271 self.send_header("Content-Length", len(contents))
1256 self.end_headers() 1272 self.end_headers()
1257 self.wfile.write(contents) 1273 self.wfile.write(contents)
1258 return True 1274 return True
1259 1275
1260 def DeviceManagementHandler(self): 1276 def DeviceManagementHandler(self):
1261 """Delegates to the device management service used for cloud policy.""" 1277 """Delegates to the device management service used for cloud policy."""
1262 if not self._ShouldHandleRequest("/device_management"): 1278 if not self._ShouldHandleRequest("/device_management"):
1263 return False 1279 return False
1264 1280
1265 length = int(self.headers.getheader('content-length')) 1281 raw_request = self.ReadRequestBody()
1266 raw_request = self.rfile.read(length)
1267 1282
1268 if not self.server._device_management_handler: 1283 if not self.server._device_management_handler:
1269 import device_management 1284 import device_management
1270 policy_path = os.path.join(self.server.data_dir, 'device_management') 1285 policy_path = os.path.join(self.server.data_dir, 'device_management')
1271 self.server._device_management_handler = ( 1286 self.server._device_management_handler = (
1272 device_management.TestServer(policy_path)) 1287 device_management.TestServer(policy_path))
1273 1288
1274 http_response, raw_reply = ( 1289 http_response, raw_reply = (
1275 self.server._device_management_handler.HandleRequest(self.path, 1290 self.server._device_management_handler.HandleRequest(self.path,
1276 self.headers, 1291 self.headers,
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1317 def ChromiumSyncCommandHandler(self): 1332 def ChromiumSyncCommandHandler(self):
1318 """Handle a chromiumsync command arriving via http. 1333 """Handle a chromiumsync command arriving via http.
1319 1334
1320 This covers all sync protocol commands: authentication, getupdates, and 1335 This covers all sync protocol commands: authentication, getupdates, and
1321 commit. 1336 commit.
1322 """ 1337 """
1323 test_name = "/chromiumsync/command" 1338 test_name = "/chromiumsync/command"
1324 if not self._ShouldHandleRequest(test_name): 1339 if not self._ShouldHandleRequest(test_name):
1325 return False 1340 return False
1326 1341
1327 length = int(self.headers.getheader('content-length')) 1342 raw_request = self.ReadRequestBody()
1328 raw_request = self.rfile.read(length)
1329 1343
1330 http_response, raw_reply = self.server.HandleCommand( 1344 http_response, raw_reply = self.server.HandleCommand(
1331 self.path, raw_request) 1345 self.path, raw_request)
1332 self.send_response(http_response) 1346 self.send_response(http_response)
1333 self.end_headers() 1347 self.end_headers()
1334 self.wfile.write(raw_reply) 1348 self.wfile.write(raw_reply)
1335 return True 1349 return True
1336 1350
1337 1351
1338 def MakeDataDir(): 1352 def MakeDataDir():
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
1498 'option may appear multiple times, indicating ' 1512 'option may appear multiple times, indicating '
1499 'multiple algorithms should be enabled.'); 1513 'multiple algorithms should be enabled.');
1500 option_parser.add_option('', '--file-root-url', default='/files/', 1514 option_parser.add_option('', '--file-root-url', default='/files/',
1501 help='Specify a root URL for files served.') 1515 help='Specify a root URL for files served.')
1502 option_parser.add_option('', '--startup-pipe', type='int', 1516 option_parser.add_option('', '--startup-pipe', type='int',
1503 dest='startup_pipe', 1517 dest='startup_pipe',
1504 help='File handle of pipe to parent process') 1518 help='File handle of pipe to parent process')
1505 options, args = option_parser.parse_args() 1519 options, args = option_parser.parse_args()
1506 1520
1507 sys.exit(main(options, args)) 1521 sys.exit(main(options, args))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698