blob: 0f4b9ba7b785b5af013e51bc7880e055af4b419a [file] [log] [blame]
Georg Brandlb533e262008-05-25 18:19:30 +00001"""Unittests for the various HTTPServer modules.
2
3Written by Cody A.W. Somerville <cody-somerville@ubuntu.com>,
4Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest.
5"""
6
Georg Brandl24420152008-05-26 16:32:26 +00007from http.server import BaseHTTPRequestHandler, HTTPServer, \
8 SimpleHTTPRequestHandler, CGIHTTPRequestHandler
Benjamin Petersonad71f0f2009-04-11 20:12:10 +00009from http import server
Georg Brandlb533e262008-05-25 18:19:30 +000010
11import os
12import sys
Senthil Kumaran0f476d42010-09-30 06:09:18 +000013import re
Georg Brandlb533e262008-05-25 18:19:30 +000014import base64
15import shutil
Jeremy Hylton1afc1692008-06-18 20:49:58 +000016import urllib.parse
Georg Brandl24420152008-05-26 16:32:26 +000017import http.client
Georg Brandlb533e262008-05-25 18:19:30 +000018import tempfile
Senthil Kumaran0f476d42010-09-30 06:09:18 +000019from io import BytesIO
Georg Brandlb533e262008-05-25 18:19:30 +000020
21import unittest
22from test import support
Victor Stinner45df8202010-04-28 22:31:17 +000023threading = support.import_module('threading')
Georg Brandlb533e262008-05-25 18:19:30 +000024
Georg Brandlb533e262008-05-25 18:19:30 +000025class NoLogRequestHandler:
26 def log_message(self, *args):
27 # don't write log messages to stderr
28 pass
29
Barry Warsaw820c1202008-06-12 04:06:45 +000030 def read(self, n=None):
31 return ''
32
Georg Brandlb533e262008-05-25 18:19:30 +000033
34class TestServerThread(threading.Thread):
35 def __init__(self, test_object, request_handler):
36 threading.Thread.__init__(self)
37 self.request_handler = request_handler
38 self.test_object = test_object
Georg Brandlb533e262008-05-25 18:19:30 +000039
40 def run(self):
Antoine Pitroucb342182011-03-21 00:26:51 +010041 self.server = HTTPServer(('localhost', 0), self.request_handler)
42 self.test_object.HOST, self.test_object.PORT = self.server.socket.getsockname()
Antoine Pitrou08911bd2010-04-25 22:19:43 +000043 self.test_object.server_started.set()
44 self.test_object = None
Georg Brandlb533e262008-05-25 18:19:30 +000045 try:
Antoine Pitrou08911bd2010-04-25 22:19:43 +000046 self.server.serve_forever(0.05)
Georg Brandlb533e262008-05-25 18:19:30 +000047 finally:
48 self.server.server_close()
49
50 def stop(self):
51 self.server.shutdown()
52
53
54class BaseTestCase(unittest.TestCase):
55 def setUp(self):
Antoine Pitrou45ebeb82009-10-27 18:52:30 +000056 self._threads = support.threading_setup()
Nick Coghlan6ead5522009-10-18 13:19:33 +000057 os.environ = support.EnvironmentVarGuard()
Antoine Pitrou08911bd2010-04-25 22:19:43 +000058 self.server_started = threading.Event()
Georg Brandlb533e262008-05-25 18:19:30 +000059 self.thread = TestServerThread(self, self.request_handler)
60 self.thread.start()
Antoine Pitrou08911bd2010-04-25 22:19:43 +000061 self.server_started.wait()
Georg Brandlb533e262008-05-25 18:19:30 +000062
63 def tearDown(self):
Georg Brandlb533e262008-05-25 18:19:30 +000064 self.thread.stop()
Antoine Pitrouf7270822012-09-30 01:05:30 +020065 self.thread = None
Nick Coghlan6ead5522009-10-18 13:19:33 +000066 os.environ.__exit__()
Antoine Pitrou45ebeb82009-10-27 18:52:30 +000067 support.threading_cleanup(*self._threads)
Georg Brandlb533e262008-05-25 18:19:30 +000068
69 def request(self, uri, method='GET', body=None, headers={}):
Antoine Pitroucb342182011-03-21 00:26:51 +010070 self.connection = http.client.HTTPConnection(self.HOST, self.PORT)
Georg Brandlb533e262008-05-25 18:19:30 +000071 self.connection.request(method, uri, body, headers)
72 return self.connection.getresponse()
73
74
75class BaseHTTPServerTestCase(BaseTestCase):
76 class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler):
77 protocol_version = 'HTTP/1.1'
78 default_request_version = 'HTTP/1.1'
79
80 def do_TEST(self):
81 self.send_response(204)
82 self.send_header('Content-Type', 'text/html')
83 self.send_header('Connection', 'close')
84 self.end_headers()
85
86 def do_KEEP(self):
87 self.send_response(204)
88 self.send_header('Content-Type', 'text/html')
89 self.send_header('Connection', 'keep-alive')
90 self.end_headers()
91
92 def do_KEYERROR(self):
93 self.send_error(999)
94
Senthil Kumaran52d27202012-10-10 23:16:21 -070095 def do_NOTFOUND(self):
96 self.send_error(404)
97
Senthil Kumaran26886442013-03-15 07:53:21 -070098 def do_EXPLAINERROR(self):
99 self.send_error(999, "Short Message",
100 "This is a long \n explaination")
101
Georg Brandlb533e262008-05-25 18:19:30 +0000102 def do_CUSTOM(self):
103 self.send_response(999)
104 self.send_header('Content-Type', 'text/html')
105 self.send_header('Connection', 'close')
106 self.end_headers()
107
Armin Ronacher8d96d772011-01-22 13:13:05 +0000108 def do_LATINONEHEADER(self):
109 self.send_response(999)
110 self.send_header('X-Special', 'Dängerous Mind')
Armin Ronacher59531282011-01-22 13:44:22 +0000111 self.send_header('Connection', 'close')
Armin Ronacher8d96d772011-01-22 13:13:05 +0000112 self.end_headers()
Armin Ronacher59531282011-01-22 13:44:22 +0000113 body = self.headers['x-special-incoming'].encode('utf-8')
114 self.wfile.write(body)
Armin Ronacher8d96d772011-01-22 13:13:05 +0000115
Georg Brandlb533e262008-05-25 18:19:30 +0000116 def setUp(self):
117 BaseTestCase.setUp(self)
Antoine Pitroucb342182011-03-21 00:26:51 +0100118 self.con = http.client.HTTPConnection(self.HOST, self.PORT)
Georg Brandlb533e262008-05-25 18:19:30 +0000119 self.con.connect()
120
121 def test_command(self):
122 self.con.request('GET', '/')
123 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000124 self.assertEqual(res.status, 501)
Georg Brandlb533e262008-05-25 18:19:30 +0000125
126 def test_request_line_trimming(self):
127 self.con._http_vsn_str = 'HTTP/1.1\n'
R David Murray14199f92014-06-24 16:39:49 -0400128 self.con.putrequest('XYZBOGUS', '/')
Georg Brandlb533e262008-05-25 18:19:30 +0000129 self.con.endheaders()
130 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000131 self.assertEqual(res.status, 501)
Georg Brandlb533e262008-05-25 18:19:30 +0000132
133 def test_version_bogus(self):
134 self.con._http_vsn_str = 'FUBAR'
135 self.con.putrequest('GET', '/')
136 self.con.endheaders()
137 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000138 self.assertEqual(res.status, 400)
Georg Brandlb533e262008-05-25 18:19:30 +0000139
140 def test_version_digits(self):
141 self.con._http_vsn_str = 'HTTP/9.9.9'
142 self.con.putrequest('GET', '/')
143 self.con.endheaders()
144 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000145 self.assertEqual(res.status, 400)
Georg Brandlb533e262008-05-25 18:19:30 +0000146
147 def test_version_none_get(self):
148 self.con._http_vsn_str = ''
149 self.con.putrequest('GET', '/')
150 self.con.endheaders()
151 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000152 self.assertEqual(res.status, 501)
Georg Brandlb533e262008-05-25 18:19:30 +0000153
154 def test_version_none(self):
R David Murray14199f92014-06-24 16:39:49 -0400155 # Test that a valid method is rejected when not HTTP/1.x
Georg Brandlb533e262008-05-25 18:19:30 +0000156 self.con._http_vsn_str = ''
R David Murray14199f92014-06-24 16:39:49 -0400157 self.con.putrequest('CUSTOM', '/')
Georg Brandlb533e262008-05-25 18:19:30 +0000158 self.con.endheaders()
159 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000160 self.assertEqual(res.status, 400)
Georg Brandlb533e262008-05-25 18:19:30 +0000161
162 def test_version_invalid(self):
163 self.con._http_vsn = 99
164 self.con._http_vsn_str = 'HTTP/9.9'
165 self.con.putrequest('GET', '/')
166 self.con.endheaders()
167 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000168 self.assertEqual(res.status, 505)
Georg Brandlb533e262008-05-25 18:19:30 +0000169
170 def test_send_blank(self):
171 self.con._http_vsn_str = ''
172 self.con.putrequest('', '')
173 self.con.endheaders()
174 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000175 self.assertEqual(res.status, 400)
Georg Brandlb533e262008-05-25 18:19:30 +0000176
177 def test_header_close(self):
178 self.con.putrequest('GET', '/')
179 self.con.putheader('Connection', 'close')
180 self.con.endheaders()
181 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000182 self.assertEqual(res.status, 501)
Georg Brandlb533e262008-05-25 18:19:30 +0000183
184 def test_head_keep_alive(self):
185 self.con._http_vsn_str = 'HTTP/1.1'
186 self.con.putrequest('GET', '/')
187 self.con.putheader('Connection', 'keep-alive')
188 self.con.endheaders()
189 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000190 self.assertEqual(res.status, 501)
Georg Brandlb533e262008-05-25 18:19:30 +0000191
192 def test_handler(self):
193 self.con.request('TEST', '/')
194 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000195 self.assertEqual(res.status, 204)
Georg Brandlb533e262008-05-25 18:19:30 +0000196
197 def test_return_header_keep_alive(self):
198 self.con.request('KEEP', '/')
199 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000200 self.assertEqual(res.getheader('Connection'), 'keep-alive')
Georg Brandlb533e262008-05-25 18:19:30 +0000201 self.con.request('TEST', '/')
Brian Curtin61d0d602010-10-31 00:34:23 +0000202 self.addCleanup(self.con.close)
Georg Brandlb533e262008-05-25 18:19:30 +0000203
204 def test_internal_key_error(self):
205 self.con.request('KEYERROR', '/')
206 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000207 self.assertEqual(res.status, 999)
Georg Brandlb533e262008-05-25 18:19:30 +0000208
209 def test_return_custom_status(self):
210 self.con.request('CUSTOM', '/')
211 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000212 self.assertEqual(res.status, 999)
Georg Brandlb533e262008-05-25 18:19:30 +0000213
Senthil Kumaran26886442013-03-15 07:53:21 -0700214 def test_return_explain_error(self):
215 self.con.request('EXPLAINERROR', '/')
216 res = self.con.getresponse()
217 self.assertEqual(res.status, 999)
218 self.assertTrue(int(res.getheader('Content-Length')))
219
Armin Ronacher8d96d772011-01-22 13:13:05 +0000220 def test_latin1_header(self):
Armin Ronacher59531282011-01-22 13:44:22 +0000221 self.con.request('LATINONEHEADER', '/', headers={
222 'X-Special-Incoming': 'Ärger mit Unicode'
223 })
Armin Ronacher8d96d772011-01-22 13:13:05 +0000224 res = self.con.getresponse()
225 self.assertEqual(res.getheader('X-Special'), 'Dängerous Mind')
Armin Ronacher59531282011-01-22 13:44:22 +0000226 self.assertEqual(res.read(), 'Ärger mit Unicode'.encode('utf-8'))
Armin Ronacher8d96d772011-01-22 13:13:05 +0000227
Senthil Kumaran52d27202012-10-10 23:16:21 -0700228 def test_error_content_length(self):
229 # Issue #16088: standard error responses should have a content-length
230 self.con.request('NOTFOUND', '/')
231 res = self.con.getresponse()
232 self.assertEqual(res.status, 404)
233 data = res.read()
Senthil Kumaran52d27202012-10-10 23:16:21 -0700234 self.assertEqual(int(res.getheader('Content-Length')), len(data))
235
Georg Brandlb533e262008-05-25 18:19:30 +0000236
237class SimpleHTTPServerTestCase(BaseTestCase):
238 class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler):
239 pass
240
241 def setUp(self):
242 BaseTestCase.setUp(self)
243 self.cwd = os.getcwd()
244 basetempdir = tempfile.gettempdir()
245 os.chdir(basetempdir)
246 self.data = b'We are the knights who say Ni!'
247 self.tempdir = tempfile.mkdtemp(dir=basetempdir)
248 self.tempdir_name = os.path.basename(self.tempdir)
Brett Cannon105df5d2010-10-29 23:43:42 +0000249 with open(os.path.join(self.tempdir, 'test'), 'wb') as temp:
250 temp.write(self.data)
Georg Brandlb533e262008-05-25 18:19:30 +0000251
252 def tearDown(self):
253 try:
254 os.chdir(self.cwd)
255 try:
256 shutil.rmtree(self.tempdir)
257 except:
258 pass
259 finally:
260 BaseTestCase.tearDown(self)
261
262 def check_status_and_reason(self, response, status, data=None):
263 body = response.read()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000264 self.assertTrue(response)
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000265 self.assertEqual(response.status, status)
266 self.assertIsNotNone(response.reason)
Georg Brandlb533e262008-05-25 18:19:30 +0000267 if data:
268 self.assertEqual(data, body)
269
270 def test_get(self):
271 #constructs the path relative to the root directory of the HTTPServer
272 response = self.request(self.tempdir_name + '/test')
273 self.check_status_and_reason(response, 200, data=self.data)
Senthil Kumaran72c238e2013-09-13 00:21:18 -0700274 # check for trailing "/" which should return 404. See Issue17324
275 response = self.request(self.tempdir_name + '/test/')
276 self.check_status_and_reason(response, 404)
Georg Brandlb533e262008-05-25 18:19:30 +0000277 response = self.request(self.tempdir_name + '/')
278 self.check_status_and_reason(response, 200)
279 response = self.request(self.tempdir_name)
280 self.check_status_and_reason(response, 301)
281 response = self.request('/ThisDoesNotExist')
282 self.check_status_and_reason(response, 404)
283 response = self.request('/' + 'ThisDoesNotExist' + '/')
284 self.check_status_and_reason(response, 404)
Brett Cannon105df5d2010-10-29 23:43:42 +0000285 with open(os.path.join(self.tempdir_name, 'index.html'), 'w') as f:
286 response = self.request('/' + self.tempdir_name + '/')
287 self.check_status_and_reason(response, 200)
Charles-François Natalif7ed9fc2011-11-02 19:35:14 +0100288 # chmod() doesn't work as expected on Windows, and filesystem
289 # permissions are ignored by root on Unix.
290 if os.name == 'posix' and os.geteuid() != 0:
Brett Cannon105df5d2010-10-29 23:43:42 +0000291 os.chmod(self.tempdir, 0)
292 response = self.request(self.tempdir_name + '/')
293 self.check_status_and_reason(response, 404)
294 os.chmod(self.tempdir, 0o755)
Georg Brandlb533e262008-05-25 18:19:30 +0000295
296 def test_head(self):
297 response = self.request(
298 self.tempdir_name + '/test', method='HEAD')
299 self.check_status_and_reason(response, 200)
300 self.assertEqual(response.getheader('content-length'),
301 str(len(self.data)))
302 self.assertEqual(response.getheader('content-type'),
303 'application/octet-stream')
304
305 def test_invalid_requests(self):
306 response = self.request('/', method='FOO')
307 self.check_status_and_reason(response, 501)
308 # requests must be case sensitive,so this should fail too
309 response = self.request('/', method='get')
310 self.check_status_and_reason(response, 501)
311 response = self.request('/', method='GETs')
312 self.check_status_and_reason(response, 501)
313
314
315cgi_file1 = """\
316#!%s
317
318print("Content-type: text/html")
319print()
320print("Hello World")
321"""
322
323cgi_file2 = """\
324#!%s
325import cgi
326
327print("Content-type: text/html")
328print()
329
330form = cgi.FieldStorage()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000331print("%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"),
332 form.getfirst("bacon")))
Georg Brandlb533e262008-05-25 18:19:30 +0000333"""
334
Charles-François Natalif7ed9fc2011-11-02 19:35:14 +0100335
336@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
337 "This test can't be run reliably as root (issue #13308).")
Georg Brandlb533e262008-05-25 18:19:30 +0000338class CGIHTTPServerTestCase(BaseTestCase):
339 class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler):
340 pass
341
Antoine Pitroue768c392012-08-05 14:52:45 +0200342 linesep = os.linesep.encode('ascii')
343
Georg Brandlb533e262008-05-25 18:19:30 +0000344 def setUp(self):
345 BaseTestCase.setUp(self)
Victor Stinner0b0ca0c2010-10-17 19:46:36 +0000346 self.cwd = os.getcwd()
Georg Brandlb533e262008-05-25 18:19:30 +0000347 self.parent_dir = tempfile.mkdtemp()
348 self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin')
Ned Deily915a30f2014-07-12 22:06:26 -0700349 self.cgi_child_dir = os.path.join(self.cgi_dir, 'child-dir')
Georg Brandlb533e262008-05-25 18:19:30 +0000350 os.mkdir(self.cgi_dir)
Ned Deily915a30f2014-07-12 22:06:26 -0700351 os.mkdir(self.cgi_child_dir)
Benjamin Peterson35aca892013-10-30 12:48:59 -0400352 self.nocgi_path = None
Victor Stinner0b0ca0c2010-10-17 19:46:36 +0000353 self.file1_path = None
354 self.file2_path = None
Ned Deily915a30f2014-07-12 22:06:26 -0700355 self.file3_path = None
Georg Brandlb533e262008-05-25 18:19:30 +0000356
Florent Xiclunafd1b0932010-03-28 00:25:02 +0000357 # The shebang line should be pure ASCII: use symlink if possible.
358 # See issue #7668.
Brian Curtin3b4499c2010-12-28 14:31:47 +0000359 if support.can_symlink():
Florent Xiclunafd1b0932010-03-28 00:25:02 +0000360 self.pythonexe = os.path.join(self.parent_dir, 'python')
361 os.symlink(sys.executable, self.pythonexe)
362 else:
363 self.pythonexe = sys.executable
364
Victor Stinner3218c312010-10-17 20:13:36 +0000365 try:
366 # The python executable path is written as the first line of the
367 # CGI Python script. The encoding cookie cannot be used, and so the
368 # path should be encodable to the default script encoding (utf-8)
369 self.pythonexe.encode('utf-8')
370 except UnicodeEncodeError:
371 self.tearDown()
Serhiy Storchaka0b4591e2013-02-04 15:45:00 +0200372 self.skipTest("Python executable path is not encodable to utf-8")
Victor Stinner3218c312010-10-17 20:13:36 +0000373
Benjamin Peterson04e9de42013-10-30 12:43:09 -0400374 self.nocgi_path = os.path.join(self.parent_dir, 'nocgi.py')
375 with open(self.nocgi_path, 'w') as fp:
376 fp.write(cgi_file1 % self.pythonexe)
377 os.chmod(self.nocgi_path, 0o777)
378
Georg Brandlb533e262008-05-25 18:19:30 +0000379 self.file1_path = os.path.join(self.cgi_dir, 'file1.py')
Victor Stinner6fb45752010-10-17 20:17:41 +0000380 with open(self.file1_path, 'w', encoding='utf-8') as file1:
Florent Xiclunafd1b0932010-03-28 00:25:02 +0000381 file1.write(cgi_file1 % self.pythonexe)
Georg Brandlb533e262008-05-25 18:19:30 +0000382 os.chmod(self.file1_path, 0o777)
383
384 self.file2_path = os.path.join(self.cgi_dir, 'file2.py')
Victor Stinner6fb45752010-10-17 20:17:41 +0000385 with open(self.file2_path, 'w', encoding='utf-8') as file2:
Florent Xiclunafd1b0932010-03-28 00:25:02 +0000386 file2.write(cgi_file2 % self.pythonexe)
Georg Brandlb533e262008-05-25 18:19:30 +0000387 os.chmod(self.file2_path, 0o777)
388
Ned Deily915a30f2014-07-12 22:06:26 -0700389 self.file3_path = os.path.join(self.cgi_child_dir, 'file3.py')
390 with open(self.file3_path, 'w', encoding='utf-8') as file3:
391 file3.write(cgi_file1 % self.pythonexe)
392 os.chmod(self.file3_path, 0o777)
393
Georg Brandlb533e262008-05-25 18:19:30 +0000394 os.chdir(self.parent_dir)
395
396 def tearDown(self):
397 try:
398 os.chdir(self.cwd)
Florent Xiclunafd1b0932010-03-28 00:25:02 +0000399 if self.pythonexe != sys.executable:
400 os.remove(self.pythonexe)
Benjamin Peterson35aca892013-10-30 12:48:59 -0400401 if self.nocgi_path:
402 os.remove(self.nocgi_path)
Victor Stinner0b0ca0c2010-10-17 19:46:36 +0000403 if self.file1_path:
404 os.remove(self.file1_path)
405 if self.file2_path:
406 os.remove(self.file2_path)
Ned Deily915a30f2014-07-12 22:06:26 -0700407 if self.file3_path:
408 os.remove(self.file3_path)
409 os.rmdir(self.cgi_child_dir)
Georg Brandlb533e262008-05-25 18:19:30 +0000410 os.rmdir(self.cgi_dir)
411 os.rmdir(self.parent_dir)
412 finally:
413 BaseTestCase.tearDown(self)
414
Senthil Kumarand70846b2012-04-12 02:34:32 +0800415 def test_url_collapse_path(self):
416 # verify tail is the last portion and head is the rest on proper urls
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000417 test_vectors = {
Senthil Kumarand70846b2012-04-12 02:34:32 +0800418 '': '//',
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000419 '..': IndexError,
420 '/.//..': IndexError,
Senthil Kumarand70846b2012-04-12 02:34:32 +0800421 '/': '//',
422 '//': '//',
423 '/\\': '//\\',
424 '/.//': '//',
425 'cgi-bin/file1.py': '/cgi-bin/file1.py',
426 '/cgi-bin/file1.py': '/cgi-bin/file1.py',
427 'a': '//a',
428 '/a': '//a',
429 '//a': '//a',
430 './a': '//a',
431 './C:/': '/C:/',
432 '/a/b': '/a/b',
433 '/a/b/': '/a/b/',
434 '/a/b/.': '/a/b/',
435 '/a/b/c/..': '/a/b/',
436 '/a/b/c/../d': '/a/b/d',
437 '/a/b/c/../d/e/../f': '/a/b/d/f',
438 '/a/b/c/../d/e/../../f': '/a/b/f',
439 '/a/b/c/../d/e/.././././..//f': '/a/b/f',
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000440 '../a/b/c/../d/e/.././././..//f': IndexError,
Senthil Kumarand70846b2012-04-12 02:34:32 +0800441 '/a/b/c/../d/e/../../../f': '/a/f',
442 '/a/b/c/../d/e/../../../../f': '//f',
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000443 '/a/b/c/../d/e/../../../../../f': IndexError,
Senthil Kumarand70846b2012-04-12 02:34:32 +0800444 '/a/b/c/../d/e/../../../../f/..': '//',
445 '/a/b/c/../d/e/../../../../f/../.': '//',
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000446 }
447 for path, expected in test_vectors.items():
448 if isinstance(expected, type) and issubclass(expected, Exception):
449 self.assertRaises(expected,
Senthil Kumarand70846b2012-04-12 02:34:32 +0800450 server._url_collapse_path, path)
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000451 else:
Senthil Kumarand70846b2012-04-12 02:34:32 +0800452 actual = server._url_collapse_path(path)
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000453 self.assertEqual(expected, actual,
454 msg='path = %r\nGot: %r\nWanted: %r' %
455 (path, actual, expected))
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000456
Georg Brandlb533e262008-05-25 18:19:30 +0000457 def test_headers_and_content(self):
458 res = self.request('/cgi-bin/file1.py')
Antoine Pitroue768c392012-08-05 14:52:45 +0200459 self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000460 (res.read(), res.getheader('Content-type'), res.status))
Georg Brandlb533e262008-05-25 18:19:30 +0000461
Benjamin Peterson04e9de42013-10-30 12:43:09 -0400462 def test_issue19435(self):
463 res = self.request('///////////nocgi.py/../cgi-bin/nothere.sh')
464 self.assertEqual(res.status, 404)
465
Georg Brandlb533e262008-05-25 18:19:30 +0000466 def test_post(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000467 params = urllib.parse.urlencode(
468 {'spam' : 1, 'eggs' : 'python', 'bacon' : 123456})
Georg Brandlb533e262008-05-25 18:19:30 +0000469 headers = {'Content-type' : 'application/x-www-form-urlencoded'}
470 res = self.request('/cgi-bin/file2.py', 'POST', params, headers)
471
Antoine Pitroue768c392012-08-05 14:52:45 +0200472 self.assertEqual(res.read(), b'1, python, 123456' + self.linesep)
Georg Brandlb533e262008-05-25 18:19:30 +0000473
474 def test_invaliduri(self):
475 res = self.request('/cgi-bin/invalid')
476 res.read()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000477 self.assertEqual(res.status, 404)
Georg Brandlb533e262008-05-25 18:19:30 +0000478
479 def test_authorization(self):
480 headers = {b'Authorization' : b'Basic ' +
481 base64.b64encode(b'username:pass')}
482 res = self.request('/cgi-bin/file1.py', 'GET', headers=headers)
Antoine Pitroue768c392012-08-05 14:52:45 +0200483 self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000484 (res.read(), res.getheader('Content-type'), res.status))
Georg Brandlb533e262008-05-25 18:19:30 +0000485
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000486 def test_no_leading_slash(self):
487 # http://bugs.python.org/issue2254
488 res = self.request('cgi-bin/file1.py')
Antoine Pitroue768c392012-08-05 14:52:45 +0200489 self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000490 (res.read(), res.getheader('Content-type'), res.status))
491
Senthil Kumaran42713722010-10-03 17:55:45 +0000492 def test_os_environ_is_not_altered(self):
493 signature = "Test CGI Server"
494 os.environ['SERVER_SOFTWARE'] = signature
495 res = self.request('/cgi-bin/file1.py')
Antoine Pitroue768c392012-08-05 14:52:45 +0200496 self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
Senthil Kumaran42713722010-10-03 17:55:45 +0000497 (res.read(), res.getheader('Content-type'), res.status))
498 self.assertEqual(os.environ['SERVER_SOFTWARE'], signature)
499
Benjamin Peterson73b8b1c2014-06-14 18:36:29 -0700500 def test_urlquote_decoding_in_cgi_check(self):
501 res = self.request('/cgi-bin%2ffile1.py')
Benjamin Peterson314dc122014-06-16 23:15:50 -0700502 self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
Benjamin Peterson73b8b1c2014-06-14 18:36:29 -0700503 (res.read(), res.getheader('Content-type'), res.status))
504
Ned Deily915a30f2014-07-12 22:06:26 -0700505 def test_nested_cgi_path_issue21323(self):
506 res = self.request('/cgi-bin/child-dir/file3.py')
507 self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
508 (res.read(), res.getheader('Content-type'), res.status))
509
Georg Brandlb533e262008-05-25 18:19:30 +0000510
Georg Brandl6fcac0d2010-08-02 18:56:54 +0000511class SocketlessRequestHandler(SimpleHTTPRequestHandler):
512 def __init__(self):
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000513 self.get_called = False
514 self.protocol_version = "HTTP/1.1"
515
516 def do_GET(self):
517 self.get_called = True
518 self.send_response(200)
519 self.send_header('Content-Type', 'text/html')
520 self.end_headers()
521 self.wfile.write(b'<html><body>Data</body></html>\r\n')
522
523 def log_message(self, format, *args):
Georg Brandl6fcac0d2010-08-02 18:56:54 +0000524 pass
525
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000526class RejectingSocketlessRequestHandler(SocketlessRequestHandler):
527 def handle_expect_100(self):
528 self.send_error(417)
529 return False
530
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800531
532class AuditableBytesIO:
533
534 def __init__(self):
535 self.datas = []
536
537 def write(self, data):
538 self.datas.append(data)
539
540 def getData(self):
541 return b''.join(self.datas)
542
543 @property
544 def numWrites(self):
545 return len(self.datas)
546
547
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000548class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
Ezio Melotti3b3499b2011-03-16 11:35:38 +0200549 """Test the functionality of the BaseHTTPServer.
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000550
551 Test the support for the Expect 100-continue header.
552 """
553
554 HTTPResponseMatch = re.compile(b'HTTP/1.[0-9]+ 200 OK')
555
556 def setUp (self):
557 self.handler = SocketlessRequestHandler()
558
559 def send_typical_request(self, message):
560 input = BytesIO(message)
561 output = BytesIO()
562 self.handler.rfile = input
563 self.handler.wfile = output
564 self.handler.handle_one_request()
565 output.seek(0)
566 return output.readlines()
567
568 def verify_get_called(self):
569 self.assertTrue(self.handler.get_called)
570
571 def verify_expected_headers(self, headers):
572 for fieldName in b'Server: ', b'Date: ', b'Content-Type: ':
573 self.assertEqual(sum(h.startswith(fieldName) for h in headers), 1)
574
575 def verify_http_server_response(self, response):
576 match = self.HTTPResponseMatch.search(response)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200577 self.assertIsNotNone(match)
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000578
579 def test_http_1_1(self):
580 result = self.send_typical_request(b'GET / HTTP/1.1\r\n\r\n')
581 self.verify_http_server_response(result[0])
582 self.verify_expected_headers(result[1:-1])
583 self.verify_get_called()
584 self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n')
585
586 def test_http_1_0(self):
587 result = self.send_typical_request(b'GET / HTTP/1.0\r\n\r\n')
588 self.verify_http_server_response(result[0])
589 self.verify_expected_headers(result[1:-1])
590 self.verify_get_called()
591 self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n')
592
593 def test_http_0_9(self):
594 result = self.send_typical_request(b'GET / HTTP/0.9\r\n\r\n')
595 self.assertEqual(len(result), 1)
596 self.assertEqual(result[0], b'<html><body>Data</body></html>\r\n')
597 self.verify_get_called()
598
599 def test_with_continue_1_0(self):
600 result = self.send_typical_request(b'GET / HTTP/1.0\r\nExpect: 100-continue\r\n\r\n')
601 self.verify_http_server_response(result[0])
602 self.verify_expected_headers(result[1:-1])
603 self.verify_get_called()
604 self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n')
605
606 def test_with_continue_1_1(self):
607 result = self.send_typical_request(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n')
608 self.assertEqual(result[0], b'HTTP/1.1 100 Continue\r\n')
Benjamin Peterson04424232014-01-18 21:50:18 -0500609 self.assertEqual(result[1], b'\r\n')
610 self.assertEqual(result[2], b'HTTP/1.1 200 OK\r\n')
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000611 self.verify_expected_headers(result[2:-1])
612 self.verify_get_called()
613 self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n')
614
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800615 def test_header_buffering_of_send_error(self):
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000616
617 input = BytesIO(b'GET / HTTP/1.1\r\n\r\n')
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800618 output = AuditableBytesIO()
619 handler = SocketlessRequestHandler()
620 handler.rfile = input
621 handler.wfile = output
622 handler.request_version = 'HTTP/1.1'
623 handler.requestline = ''
624 handler.command = None
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000625
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800626 handler.send_error(418)
627 self.assertEqual(output.numWrites, 2)
628
629 def test_header_buffering_of_send_response_only(self):
630
631 input = BytesIO(b'GET / HTTP/1.1\r\n\r\n')
632 output = AuditableBytesIO()
633 handler = SocketlessRequestHandler()
634 handler.rfile = input
635 handler.wfile = output
636 handler.request_version = 'HTTP/1.1'
637
638 handler.send_response_only(418)
639 self.assertEqual(output.numWrites, 0)
640 handler.end_headers()
641 self.assertEqual(output.numWrites, 1)
642
643 def test_header_buffering_of_send_header(self):
644
645 input = BytesIO(b'GET / HTTP/1.1\r\n\r\n')
646 output = AuditableBytesIO()
647 handler = SocketlessRequestHandler()
648 handler.rfile = input
649 handler.wfile = output
650 handler.request_version = 'HTTP/1.1'
651
652 handler.send_header('Foo', 'foo')
653 handler.send_header('bar', 'bar')
654 self.assertEqual(output.numWrites, 0)
655 handler.end_headers()
656 self.assertEqual(output.getData(), b'Foo: foo\r\nbar: bar\r\n\r\n')
657 self.assertEqual(output.numWrites, 1)
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000658
659 def test_header_unbuffered_when_continue(self):
660
661 def _readAndReseek(f):
662 pos = f.tell()
663 f.seek(0)
664 data = f.read()
665 f.seek(pos)
666 return data
667
668 input = BytesIO(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n')
669 output = BytesIO()
670 self.handler.rfile = input
671 self.handler.wfile = output
672 self.handler.request_version = 'HTTP/1.1'
673
674 self.handler.handle_one_request()
675 self.assertNotEqual(_readAndReseek(output), b'')
676 result = _readAndReseek(output).split(b'\r\n')
677 self.assertEqual(result[0], b'HTTP/1.1 100 Continue')
Benjamin Peterson04424232014-01-18 21:50:18 -0500678 self.assertEqual(result[1], b'')
679 self.assertEqual(result[2], b'HTTP/1.1 200 OK')
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000680
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000681 def test_with_continue_rejected(self):
682 usual_handler = self.handler # Save to avoid breaking any subsequent tests.
683 self.handler = RejectingSocketlessRequestHandler()
684 result = self.send_typical_request(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n')
685 self.assertEqual(result[0], b'HTTP/1.1 417 Expectation Failed\r\n')
686 self.verify_expected_headers(result[1:-1])
687 # The expect handler should short circuit the usual get method by
688 # returning false here, so get_called should be false
689 self.assertFalse(self.handler.get_called)
690 self.assertEqual(sum(r == b'Connection: close\r\n' for r in result[1:-1]), 1)
691 self.handler = usual_handler # Restore to avoid breaking any subsequent tests.
692
Antoine Pitrouc4924372010-12-16 16:48:36 +0000693 def test_request_length(self):
694 # Issue #10714: huge request lines are discarded, to avoid Denial
695 # of Service attacks.
696 result = self.send_typical_request(b'GET ' + b'x' * 65537)
697 self.assertEqual(result[0], b'HTTP/1.1 414 Request-URI Too Long\r\n')
698 self.assertFalse(self.handler.get_called)
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000699
Senthil Kumaran5466bf12010-12-18 16:55:23 +0000700 def test_header_length(self):
701 # Issue #6791: same for headers
702 result = self.send_typical_request(
703 b'GET / HTTP/1.1\r\nX-Foo: bar' + b'r' * 65537 + b'\r\n\r\n')
704 self.assertEqual(result[0], b'HTTP/1.1 400 Line too long\r\n')
705 self.assertFalse(self.handler.get_called)
706
Georg Brandl6fcac0d2010-08-02 18:56:54 +0000707class SimpleHTTPRequestHandlerTestCase(unittest.TestCase):
708 """ Test url parsing """
709 def setUp(self):
710 self.translated = os.getcwd()
711 self.translated = os.path.join(self.translated, 'filename')
712 self.handler = SocketlessRequestHandler()
713
714 def test_query_arguments(self):
715 path = self.handler.translate_path('/filename')
716 self.assertEqual(path, self.translated)
717 path = self.handler.translate_path('/filename?foo=bar')
718 self.assertEqual(path, self.translated)
719 path = self.handler.translate_path('/filename?a=b&spam=eggs#zot')
720 self.assertEqual(path, self.translated)
721
722 def test_start_with_double_slash(self):
723 path = self.handler.translate_path('//filename')
724 self.assertEqual(path, self.translated)
725 path = self.handler.translate_path('//filename?foo=bar')
726 self.assertEqual(path, self.translated)
727
728
Georg Brandlb533e262008-05-25 18:19:30 +0000729def test_main(verbose=None):
Georg Brandl6fcac0d2010-08-02 18:56:54 +0000730 cwd = os.getcwd()
Georg Brandlb533e262008-05-25 18:19:30 +0000731 try:
Georg Brandl6fcac0d2010-08-02 18:56:54 +0000732 support.run_unittest(
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000733 BaseHTTPRequestHandlerTestCase,
Georg Brandl6fcac0d2010-08-02 18:56:54 +0000734 BaseHTTPServerTestCase,
735 SimpleHTTPServerTestCase,
736 CGIHTTPServerTestCase,
737 SimpleHTTPRequestHandlerTestCase,
738 )
Georg Brandlb533e262008-05-25 18:19:30 +0000739 finally:
740 os.chdir(cwd)
741
742if __name__ == '__main__':
743 test_main()