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

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

Issue 19476: Make test server to store files by sending POST /writefile/<filename> HTTP re... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 10 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 to a pem file containing the certificate and private key that should be used. 11 to a pem file containing the certificate and private key that should be used.
12 To shut it down properly, visit localhost:8888/kill. 12 To shut it down properly, visit localhost:8888/kill.
13 """ 13 """
14 14
15 import base64 15 import base64
16 import BaseHTTPServer 16 import BaseHTTPServer
17 import cgi 17 import cgi
18 import md5 18 import md5
19 import optparse 19 import optparse
20 import os 20 import os
21 import re 21 import re
22 import shutil
22 import SocketServer 23 import SocketServer
23 import sys 24 import sys
24 import time 25 import time
25 import tlslite 26 import tlslite
26 import tlslite.api 27 import tlslite.api
27 import pyftpdlib.ftpserver 28 import pyftpdlib.ftpserver
28 29
29 SERVER_HTTP = 0 30 SERVER_HTTP = 0
30 SERVER_FTP = 1 31 SERVER_FTP = 1
31 32
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 self.RealFileWithCommonHeaderHandler, 98 self.RealFileWithCommonHeaderHandler,
98 self.RealBZ2FileWithCommonHeaderHandler, 99 self.RealBZ2FileWithCommonHeaderHandler,
99 self.AuthBasicHandler, 100 self.AuthBasicHandler,
100 self.AuthDigestHandler, 101 self.AuthDigestHandler,
101 self.SlowServerHandler, 102 self.SlowServerHandler,
102 self.ContentTypeHandler, 103 self.ContentTypeHandler,
103 self.ServerRedirectHandler, 104 self.ServerRedirectHandler,
104 self.ClientRedirectHandler, 105 self.ClientRedirectHandler,
105 self.DefaultResponseHandler] 106 self.DefaultResponseHandler]
106 self._post_handlers = [ 107 self._post_handlers = [
108 self.WriteFile,
107 self.EchoTitleHandler, 109 self.EchoTitleHandler,
108 self.EchoAllHandler, 110 self.EchoAllHandler,
109 self.EchoHandler] + self._get_handlers 111 self.EchoHandler] + self._get_handlers
110 112
111 self._mime_types = { 'gif': 'image/gif', 'jpeg' : 'image/jpeg', 'jpg' : 'im age/jpeg' } 113 self._mime_types = { 'gif': 'image/gif', 'jpeg' : 'image/jpeg', 'jpg' : 'im age/jpeg' }
112 self._default_mime_type = 'text/html' 114 self._default_mime_type = 'text/html'
113 115
114 BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, client_address , socket_server) 116 BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, client_address , socket_server)
115 117
116 def _ShouldHandleRequest(self, handler_name): 118 def _ShouldHandleRequest(self, handler_name):
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 return False 396 return False
395 397
396 self.send_response(200) 398 self.send_response(200)
397 self.send_header('Content-type', 'text/html') 399 self.send_header('Content-type', 'text/html')
398 self.end_headers() 400 self.end_headers()
399 length = int(self.headers.getheader('content-length')) 401 length = int(self.headers.getheader('content-length'))
400 request = self.rfile.read(length) 402 request = self.rfile.read(length)
401 self.wfile.write(request) 403 self.wfile.write(request)
402 return True 404 return True
403 405
406 def WriteFile(self):
407 """This is handler dumps the content of POST request to a disk file into
408 the data_dir/dump. Sub-directories are not supported."""
409
410 prefix='/writefile/'
411 if not self.path.startswith(prefix):
412 return False
413
414 file_name = self.path[len(prefix):]
415
416 # do not allow fancy chars in file name
417 re.sub('[^a-zA-Z0-9_.-]+', '', file_name)
418 if len(file_name) and file_name[0] != '.':
419 path = os.path.join(self.server.data_dir, 'dump', file_name);
420 length = int(self.headers.getheader('content-length'))
421 request = self.rfile.read(length)
422 f = open(path, "wb")
423 f.write(request);
424 f.close()
425
426 self.send_response(200)
427 self.send_header('Content-type', 'text/html')
428 self.end_headers()
429 self.wfile.write('<html>%s</html>' % file_name)
430 return True
431
404 def EchoTitleHandler(self): 432 def EchoTitleHandler(self):
405 """This handler is like Echo, but sets the page title to the request.""" 433 """This handler is like Echo, but sets the page title to the request."""
406 434
407 if not self._ShouldHandleRequest("/echotitle"): 435 if not self._ShouldHandleRequest("/echotitle"):
408 return False 436 return False
409 437
410 self.send_response(200) 438 self.send_response(200)
411 self.send_header('Content-type', 'text/html') 439 self.send_header('Content-type', 'text/html')
412 self.end_headers() 440 self.end_headers()
413 length = int(self.headers.getheader('content-length')) 441 length = int(self.headers.getheader('content-length'))
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after
877 905
878 # called by the redirect handling function when there is no parameter 906 # called by the redirect handling function when there is no parameter
879 def sendRedirectHelp(self, redirect_name): 907 def sendRedirectHelp(self, redirect_name):
880 self.send_response(200) 908 self.send_response(200)
881 self.send_header('Content-type', 'text/html') 909 self.send_header('Content-type', 'text/html')
882 self.end_headers() 910 self.end_headers()
883 self.wfile.write('<html><body><h1>Error: no redirect destination</h1>') 911 self.wfile.write('<html><body><h1>Error: no redirect destination</h1>')
884 self.wfile.write('Use <pre>%s?http://dest...</pre>' % redirect_name) 912 self.wfile.write('Use <pre>%s?http://dest...</pre>' % redirect_name)
885 self.wfile.write('</body></html>') 913 self.wfile.write('</body></html>')
886 914
915 def MakeDumpDir(data_dir):
916 """Create directory named 'dump' where uploaded data via HTTP POST request
917 will be stored. If the directory already exists all files and subdirectories
918 will be deleted."""
919 dump_dir = os.path.join(data_dir, 'dump');
920 if os.path.isdir(dump_dir):
921 shutil.rmtree(dump_dir)
922 os.mkdir(dump_dir)
923
887 def MakeDataDir(): 924 def MakeDataDir():
888 if options.data_dir: 925 if options.data_dir:
889 if not os.path.isdir(options.data_dir): 926 if not os.path.isdir(options.data_dir):
890 print 'specified data dir not found: ' + options.data_dir + ' exiting...' 927 print 'specified data dir not found: ' + options.data_dir + ' exiting...'
891 return None 928 return None
892 my_data_dir = options.data_dir 929 my_data_dir = options.data_dir
893 else: 930 else:
894 # Create the default path to our data dir, relative to the exe dir. 931 # Create the default path to our data dir, relative to the exe dir.
895 my_data_dir = os.path.dirname(sys.argv[0]) 932 my_data_dir = os.path.dirname(sys.argv[0])
896 my_data_dir = os.path.join(my_data_dir, "..", "..", "..", "..", 933 my_data_dir = os.path.join(my_data_dir, "..", "..", "..", "..",
(...skipping 17 matching lines...) Expand all
914 if not os.path.isfile(options.cert): 951 if not os.path.isfile(options.cert):
915 print 'specified cert file not found: ' + options.cert + ' exiting...' 952 print 'specified cert file not found: ' + options.cert + ' exiting...'
916 return 953 return
917 server = HTTPSServer(('127.0.0.1', port), TestPageHandler, options.cert) 954 server = HTTPSServer(('127.0.0.1', port), TestPageHandler, options.cert)
918 print 'HTTPS server started on port %d...' % port 955 print 'HTTPS server started on port %d...' % port
919 else: 956 else:
920 server = StoppableHTTPServer(('127.0.0.1', port), TestPageHandler) 957 server = StoppableHTTPServer(('127.0.0.1', port), TestPageHandler)
921 print 'HTTP server started on port %d...' % port 958 print 'HTTP server started on port %d...' % port
922 959
923 server.data_dir = MakeDataDir() 960 server.data_dir = MakeDataDir()
924 961 MakeDumpDir(server.data_dir)
962
925 # means FTP Server 963 # means FTP Server
926 else: 964 else:
927 my_data_dir = MakeDataDir() 965 my_data_dir = MakeDataDir()
928 966
929 def line_logger(msg): 967 def line_logger(msg):
930 if (msg.find("kill") >= 0): 968 if (msg.find("kill") >= 0):
931 server.stop = True 969 server.stop = True
932 print 'shutting down server' 970 print 'shutting down server'
933 sys.exit(0) 971 sys.exit(0)
934 972
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
971 option_parser.add_option('', '--data-dir', dest='data_dir', 1009 option_parser.add_option('', '--data-dir', dest='data_dir',
972 help='Directory from which to read the files') 1010 help='Directory from which to read the files')
973 option_parser.add_option('', '--https', dest='cert', 1011 option_parser.add_option('', '--https', dest='cert',
974 help='Specify that https should be used, specify ' 1012 help='Specify that https should be used, specify '
975 'the path to the cert containing the private key ' 1013 'the path to the cert containing the private key '
976 'the server should use') 1014 'the server should use')
977 options, args = option_parser.parse_args() 1015 options, args = option_parser.parse_args()
978 1016
979 sys.exit(main(options, args)) 1017 sys.exit(main(options, args))
980 1018
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698