blob: ad058b5efa4855250bb042b3e43be353b941db67 [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
Serhiy Storchakacb5bc402014-08-17 08:22:11 +030017import html
Georg Brandl24420152008-05-26 16:32:26 +000018import http.client
Georg Brandlb533e262008-05-25 18:19:30 +000019import tempfile
Senthil Kumaran0f476d42010-09-30 06:09:18 +000020from io import BytesIO
Georg Brandlb533e262008-05-25 18:19:30 +000021
22import unittest
23from test import support
Victor Stinner45df8202010-04-28 22:31:17 +000024threading = support.import_module('threading')
Georg Brandlb533e262008-05-25 18:19:30 +000025
Georg Brandlb533e262008-05-25 18:19:30 +000026class NoLogRequestHandler:
27 def log_message(self, *args):
28 # don't write log messages to stderr
29 pass
30
Barry Warsaw820c1202008-06-12 04:06:45 +000031 def read(self, n=None):
32 return ''
33
Georg Brandlb533e262008-05-25 18:19:30 +000034
35class TestServerThread(threading.Thread):
36 def __init__(self, test_object, request_handler):
37 threading.Thread.__init__(self)
38 self.request_handler = request_handler
39 self.test_object = test_object
Georg Brandlb533e262008-05-25 18:19:30 +000040
41 def run(self):
Antoine Pitroucb342182011-03-21 00:26:51 +010042 self.server = HTTPServer(('localhost', 0), self.request_handler)
43 self.test_object.HOST, self.test_object.PORT = self.server.socket.getsockname()
Antoine Pitrou08911bd2010-04-25 22:19:43 +000044 self.test_object.server_started.set()
45 self.test_object = None
Georg Brandlb533e262008-05-25 18:19:30 +000046 try:
Antoine Pitrou08911bd2010-04-25 22:19:43 +000047 self.server.serve_forever(0.05)
Georg Brandlb533e262008-05-25 18:19:30 +000048 finally:
49 self.server.server_close()
50
51 def stop(self):
52 self.server.shutdown()
53
54
55class BaseTestCase(unittest.TestCase):
56 def setUp(self):
Antoine Pitrou45ebeb82009-10-27 18:52:30 +000057 self._threads = support.threading_setup()
Nick Coghlan6ead5522009-10-18 13:19:33 +000058 os.environ = support.EnvironmentVarGuard()
Antoine Pitrou08911bd2010-04-25 22:19:43 +000059 self.server_started = threading.Event()
Georg Brandlb533e262008-05-25 18:19:30 +000060 self.thread = TestServerThread(self, self.request_handler)
61 self.thread.start()
Antoine Pitrou08911bd2010-04-25 22:19:43 +000062 self.server_started.wait()
Georg Brandlb533e262008-05-25 18:19:30 +000063
64 def tearDown(self):
Georg Brandlb533e262008-05-25 18:19:30 +000065 self.thread.stop()
Antoine Pitrouf7270822012-09-30 01:05:30 +020066 self.thread = None
Nick Coghlan6ead5522009-10-18 13:19:33 +000067 os.environ.__exit__()
Antoine Pitrou45ebeb82009-10-27 18:52:30 +000068 support.threading_cleanup(*self._threads)
Georg Brandlb533e262008-05-25 18:19:30 +000069
70 def request(self, uri, method='GET', body=None, headers={}):
Antoine Pitroucb342182011-03-21 00:26:51 +010071 self.connection = http.client.HTTPConnection(self.HOST, self.PORT)
Georg Brandlb533e262008-05-25 18:19:30 +000072 self.connection.request(method, uri, body, headers)
73 return self.connection.getresponse()
74
75
76class BaseHTTPServerTestCase(BaseTestCase):
77 class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler):
78 protocol_version = 'HTTP/1.1'
79 default_request_version = 'HTTP/1.1'
80
81 def do_TEST(self):
82 self.send_response(204)
83 self.send_header('Content-Type', 'text/html')
84 self.send_header('Connection', 'close')
85 self.end_headers()
86
87 def do_KEEP(self):
88 self.send_response(204)
89 self.send_header('Content-Type', 'text/html')
90 self.send_header('Connection', 'keep-alive')
91 self.end_headers()
92
93 def do_KEYERROR(self):
94 self.send_error(999)
95
Senthil Kumaran52d27202012-10-10 23:16:21 -070096 def do_NOTFOUND(self):
97 self.send_error(404)
98
Senthil Kumaran26886442013-03-15 07:53:21 -070099 def do_EXPLAINERROR(self):
100 self.send_error(999, "Short Message",
101 "This is a long \n explaination")
102
Georg Brandlb533e262008-05-25 18:19:30 +0000103 def do_CUSTOM(self):
104 self.send_response(999)
105 self.send_header('Content-Type', 'text/html')
106 self.send_header('Connection', 'close')
107 self.end_headers()
108
Armin Ronacher8d96d772011-01-22 13:13:05 +0000109 def do_LATINONEHEADER(self):
110 self.send_response(999)
111 self.send_header('X-Special', 'Dängerous Mind')
Armin Ronacher59531282011-01-22 13:44:22 +0000112 self.send_header('Connection', 'close')
Armin Ronacher8d96d772011-01-22 13:13:05 +0000113 self.end_headers()
Armin Ronacher59531282011-01-22 13:44:22 +0000114 body = self.headers['x-special-incoming'].encode('utf-8')
115 self.wfile.write(body)
Armin Ronacher8d96d772011-01-22 13:13:05 +0000116
Georg Brandlb533e262008-05-25 18:19:30 +0000117 def setUp(self):
118 BaseTestCase.setUp(self)
Antoine Pitroucb342182011-03-21 00:26:51 +0100119 self.con = http.client.HTTPConnection(self.HOST, self.PORT)
Georg Brandlb533e262008-05-25 18:19:30 +0000120 self.con.connect()
121
122 def test_command(self):
123 self.con.request('GET', '/')
124 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000125 self.assertEqual(res.status, 501)
Georg Brandlb533e262008-05-25 18:19:30 +0000126
127 def test_request_line_trimming(self):
128 self.con._http_vsn_str = 'HTTP/1.1\n'
R David Murray14199f92014-06-24 16:39:49 -0400129 self.con.putrequest('XYZBOGUS', '/')
Georg Brandlb533e262008-05-25 18:19:30 +0000130 self.con.endheaders()
131 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000132 self.assertEqual(res.status, 501)
Georg Brandlb533e262008-05-25 18:19:30 +0000133
134 def test_version_bogus(self):
135 self.con._http_vsn_str = 'FUBAR'
136 self.con.putrequest('GET', '/')
137 self.con.endheaders()
138 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000139 self.assertEqual(res.status, 400)
Georg Brandlb533e262008-05-25 18:19:30 +0000140
141 def test_version_digits(self):
142 self.con._http_vsn_str = 'HTTP/9.9.9'
143 self.con.putrequest('GET', '/')
144 self.con.endheaders()
145 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000146 self.assertEqual(res.status, 400)
Georg Brandlb533e262008-05-25 18:19:30 +0000147
148 def test_version_none_get(self):
149 self.con._http_vsn_str = ''
150 self.con.putrequest('GET', '/')
151 self.con.endheaders()
152 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000153 self.assertEqual(res.status, 501)
Georg Brandlb533e262008-05-25 18:19:30 +0000154
155 def test_version_none(self):
R David Murray14199f92014-06-24 16:39:49 -0400156 # Test that a valid method is rejected when not HTTP/1.x
Georg Brandlb533e262008-05-25 18:19:30 +0000157 self.con._http_vsn_str = ''
R David Murray14199f92014-06-24 16:39:49 -0400158 self.con.putrequest('CUSTOM', '/')
Georg Brandlb533e262008-05-25 18:19:30 +0000159 self.con.endheaders()
160 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000161 self.assertEqual(res.status, 400)
Georg Brandlb533e262008-05-25 18:19:30 +0000162
163 def test_version_invalid(self):
164 self.con._http_vsn = 99
165 self.con._http_vsn_str = 'HTTP/9.9'
166 self.con.putrequest('GET', '/')
167 self.con.endheaders()
168 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000169 self.assertEqual(res.status, 505)
Georg Brandlb533e262008-05-25 18:19:30 +0000170
171 def test_send_blank(self):
172 self.con._http_vsn_str = ''
173 self.con.putrequest('', '')
174 self.con.endheaders()
175 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000176 self.assertEqual(res.status, 400)
Georg Brandlb533e262008-05-25 18:19:30 +0000177
178 def test_header_close(self):
179 self.con.putrequest('GET', '/')
180 self.con.putheader('Connection', 'close')
181 self.con.endheaders()
182 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000183 self.assertEqual(res.status, 501)
Georg Brandlb533e262008-05-25 18:19:30 +0000184
185 def test_head_keep_alive(self):
186 self.con._http_vsn_str = 'HTTP/1.1'
187 self.con.putrequest('GET', '/')
188 self.con.putheader('Connection', 'keep-alive')
189 self.con.endheaders()
190 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000191 self.assertEqual(res.status, 501)
Georg Brandlb533e262008-05-25 18:19:30 +0000192
193 def test_handler(self):
194 self.con.request('TEST', '/')
195 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000196 self.assertEqual(res.status, 204)
Georg Brandlb533e262008-05-25 18:19:30 +0000197
198 def test_return_header_keep_alive(self):
199 self.con.request('KEEP', '/')
200 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000201 self.assertEqual(res.getheader('Connection'), 'keep-alive')
Georg Brandlb533e262008-05-25 18:19:30 +0000202 self.con.request('TEST', '/')
Brian Curtin61d0d602010-10-31 00:34:23 +0000203 self.addCleanup(self.con.close)
Georg Brandlb533e262008-05-25 18:19:30 +0000204
205 def test_internal_key_error(self):
206 self.con.request('KEYERROR', '/')
207 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000208 self.assertEqual(res.status, 999)
Georg Brandlb533e262008-05-25 18:19:30 +0000209
210 def test_return_custom_status(self):
211 self.con.request('CUSTOM', '/')
212 res = self.con.getresponse()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000213 self.assertEqual(res.status, 999)
Georg Brandlb533e262008-05-25 18:19:30 +0000214
Senthil Kumaran26886442013-03-15 07:53:21 -0700215 def test_return_explain_error(self):
216 self.con.request('EXPLAINERROR', '/')
217 res = self.con.getresponse()
218 self.assertEqual(res.status, 999)
219 self.assertTrue(int(res.getheader('Content-Length')))
220
Armin Ronacher8d96d772011-01-22 13:13:05 +0000221 def test_latin1_header(self):
Armin Ronacher59531282011-01-22 13:44:22 +0000222 self.con.request('LATINONEHEADER', '/', headers={
223 'X-Special-Incoming': 'Ärger mit Unicode'
224 })
Armin Ronacher8d96d772011-01-22 13:13:05 +0000225 res = self.con.getresponse()
226 self.assertEqual(res.getheader('X-Special'), 'Dängerous Mind')
Armin Ronacher59531282011-01-22 13:44:22 +0000227 self.assertEqual(res.read(), 'Ärger mit Unicode'.encode('utf-8'))
Armin Ronacher8d96d772011-01-22 13:13:05 +0000228
Senthil Kumaran52d27202012-10-10 23:16:21 -0700229 def test_error_content_length(self):
230 # Issue #16088: standard error responses should have a content-length
231 self.con.request('NOTFOUND', '/')
232 res = self.con.getresponse()
233 self.assertEqual(res.status, 404)
234 data = res.read()
Senthil Kumaran52d27202012-10-10 23:16:21 -0700235 self.assertEqual(int(res.getheader('Content-Length')), len(data))
236
Georg Brandlb533e262008-05-25 18:19:30 +0000237
238class SimpleHTTPServerTestCase(BaseTestCase):
239 class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler):
240 pass
241
242 def setUp(self):
243 BaseTestCase.setUp(self)
244 self.cwd = os.getcwd()
245 basetempdir = tempfile.gettempdir()
246 os.chdir(basetempdir)
247 self.data = b'We are the knights who say Ni!'
248 self.tempdir = tempfile.mkdtemp(dir=basetempdir)
249 self.tempdir_name = os.path.basename(self.tempdir)
Brett Cannon105df5d2010-10-29 23:43:42 +0000250 with open(os.path.join(self.tempdir, 'test'), 'wb') as temp:
251 temp.write(self.data)
Georg Brandlb533e262008-05-25 18:19:30 +0000252
253 def tearDown(self):
254 try:
255 os.chdir(self.cwd)
256 try:
257 shutil.rmtree(self.tempdir)
258 except:
259 pass
260 finally:
261 BaseTestCase.tearDown(self)
262
263 def check_status_and_reason(self, response, status, data=None):
264 body = response.read()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000265 self.assertTrue(response)
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000266 self.assertEqual(response.status, status)
267 self.assertIsNotNone(response.reason)
Georg Brandlb533e262008-05-25 18:19:30 +0000268 if data:
269 self.assertEqual(data, body)
Serhiy Storchakacb5bc402014-08-17 08:22:11 +0300270 return body
271
272 @unittest.skipUnless(support.TESTFN_UNDECODABLE,
273 'need support.TESTFN_UNDECODABLE')
274 def test_undecodable_filename(self):
Serhiy Storchakaa64ce5d2014-08-17 12:20:02 +0300275 enc = sys.getfilesystemencoding()
Serhiy Storchakacb5bc402014-08-17 08:22:11 +0300276 filename = os.fsdecode(support.TESTFN_UNDECODABLE) + '.txt'
277 with open(os.path.join(self.tempdir, filename), 'wb') as f:
278 f.write(support.TESTFN_UNDECODABLE)
279 response = self.request(self.tempdir_name + '/')
280 body = self.check_status_and_reason(response, 200)
281 quotedname = urllib.parse.quote(filename, errors='surrogatepass')
282 self.assertIn(('href="%s"' % quotedname)
Serhiy Storchakaa64ce5d2014-08-17 12:20:02 +0300283 .encode(enc, 'surrogateescape'), body)
Serhiy Storchakacb5bc402014-08-17 08:22:11 +0300284 self.assertIn(('>%s<' % html.escape(filename))
Serhiy Storchakaa64ce5d2014-08-17 12:20:02 +0300285 .encode(enc, 'surrogateescape'), body)
Serhiy Storchakacb5bc402014-08-17 08:22:11 +0300286 response = self.request(self.tempdir_name + '/' + quotedname)
287 self.check_status_and_reason(response, 200,
288 data=support.TESTFN_UNDECODABLE)
Georg Brandlb533e262008-05-25 18:19:30 +0000289
290 def test_get(self):
291 #constructs the path relative to the root directory of the HTTPServer
292 response = self.request(self.tempdir_name + '/test')
293 self.check_status_and_reason(response, 200, data=self.data)
Senthil Kumaran72c238e2013-09-13 00:21:18 -0700294 # check for trailing "/" which should return 404. See Issue17324
295 response = self.request(self.tempdir_name + '/test/')
296 self.check_status_and_reason(response, 404)
Georg Brandlb533e262008-05-25 18:19:30 +0000297 response = self.request(self.tempdir_name + '/')
298 self.check_status_and_reason(response, 200)
299 response = self.request(self.tempdir_name)
300 self.check_status_and_reason(response, 301)
301 response = self.request('/ThisDoesNotExist')
302 self.check_status_and_reason(response, 404)
303 response = self.request('/' + 'ThisDoesNotExist' + '/')
304 self.check_status_and_reason(response, 404)
Brett Cannon105df5d2010-10-29 23:43:42 +0000305 with open(os.path.join(self.tempdir_name, 'index.html'), 'w') as f:
306 response = self.request('/' + self.tempdir_name + '/')
307 self.check_status_and_reason(response, 200)
Charles-François Natalif7ed9fc2011-11-02 19:35:14 +0100308 # chmod() doesn't work as expected on Windows, and filesystem
309 # permissions are ignored by root on Unix.
310 if os.name == 'posix' and os.geteuid() != 0:
Brett Cannon105df5d2010-10-29 23:43:42 +0000311 os.chmod(self.tempdir, 0)
312 response = self.request(self.tempdir_name + '/')
313 self.check_status_and_reason(response, 404)
314 os.chmod(self.tempdir, 0o755)
Georg Brandlb533e262008-05-25 18:19:30 +0000315
316 def test_head(self):
317 response = self.request(
318 self.tempdir_name + '/test', method='HEAD')
319 self.check_status_and_reason(response, 200)
320 self.assertEqual(response.getheader('content-length'),
321 str(len(self.data)))
322 self.assertEqual(response.getheader('content-type'),
323 'application/octet-stream')
324
325 def test_invalid_requests(self):
326 response = self.request('/', method='FOO')
327 self.check_status_and_reason(response, 501)
328 # requests must be case sensitive,so this should fail too
329 response = self.request('/', method='get')
330 self.check_status_and_reason(response, 501)
331 response = self.request('/', method='GETs')
332 self.check_status_and_reason(response, 501)
333
334
335cgi_file1 = """\
336#!%s
337
338print("Content-type: text/html")
339print()
340print("Hello World")
341"""
342
343cgi_file2 = """\
344#!%s
345import cgi
346
347print("Content-type: text/html")
348print()
349
350form = cgi.FieldStorage()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000351print("%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"),
352 form.getfirst("bacon")))
Georg Brandlb533e262008-05-25 18:19:30 +0000353"""
354
Charles-François Natalif7ed9fc2011-11-02 19:35:14 +0100355
356@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
357 "This test can't be run reliably as root (issue #13308).")
Georg Brandlb533e262008-05-25 18:19:30 +0000358class CGIHTTPServerTestCase(BaseTestCase):
359 class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler):
360 pass
361
Antoine Pitroue768c392012-08-05 14:52:45 +0200362 linesep = os.linesep.encode('ascii')
363
Georg Brandlb533e262008-05-25 18:19:30 +0000364 def setUp(self):
365 BaseTestCase.setUp(self)
Victor Stinner0b0ca0c2010-10-17 19:46:36 +0000366 self.cwd = os.getcwd()
Georg Brandlb533e262008-05-25 18:19:30 +0000367 self.parent_dir = tempfile.mkdtemp()
368 self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin')
Ned Deily915a30f2014-07-12 22:06:26 -0700369 self.cgi_child_dir = os.path.join(self.cgi_dir, 'child-dir')
Georg Brandlb533e262008-05-25 18:19:30 +0000370 os.mkdir(self.cgi_dir)
Ned Deily915a30f2014-07-12 22:06:26 -0700371 os.mkdir(self.cgi_child_dir)
Benjamin Peterson35aca892013-10-30 12:48:59 -0400372 self.nocgi_path = None
Victor Stinner0b0ca0c2010-10-17 19:46:36 +0000373 self.file1_path = None
374 self.file2_path = None
Ned Deily915a30f2014-07-12 22:06:26 -0700375 self.file3_path = None
Georg Brandlb533e262008-05-25 18:19:30 +0000376
Florent Xiclunafd1b0932010-03-28 00:25:02 +0000377 # The shebang line should be pure ASCII: use symlink if possible.
378 # See issue #7668.
Brian Curtin3b4499c2010-12-28 14:31:47 +0000379 if support.can_symlink():
Florent Xiclunafd1b0932010-03-28 00:25:02 +0000380 self.pythonexe = os.path.join(self.parent_dir, 'python')
381 os.symlink(sys.executable, self.pythonexe)
382 else:
383 self.pythonexe = sys.executable
384
Victor Stinner3218c312010-10-17 20:13:36 +0000385 try:
386 # The python executable path is written as the first line of the
387 # CGI Python script. The encoding cookie cannot be used, and so the
388 # path should be encodable to the default script encoding (utf-8)
389 self.pythonexe.encode('utf-8')
390 except UnicodeEncodeError:
391 self.tearDown()
Serhiy Storchaka0b4591e2013-02-04 15:45:00 +0200392 self.skipTest("Python executable path is not encodable to utf-8")
Victor Stinner3218c312010-10-17 20:13:36 +0000393
Benjamin Peterson04e9de42013-10-30 12:43:09 -0400394 self.nocgi_path = os.path.join(self.parent_dir, 'nocgi.py')
395 with open(self.nocgi_path, 'w') as fp:
396 fp.write(cgi_file1 % self.pythonexe)
397 os.chmod(self.nocgi_path, 0o777)
398
Georg Brandlb533e262008-05-25 18:19:30 +0000399 self.file1_path = os.path.join(self.cgi_dir, 'file1.py')
Victor Stinner6fb45752010-10-17 20:17:41 +0000400 with open(self.file1_path, 'w', encoding='utf-8') as file1:
Florent Xiclunafd1b0932010-03-28 00:25:02 +0000401 file1.write(cgi_file1 % self.pythonexe)
Georg Brandlb533e262008-05-25 18:19:30 +0000402 os.chmod(self.file1_path, 0o777)
403
404 self.file2_path = os.path.join(self.cgi_dir, 'file2.py')
Victor Stinner6fb45752010-10-17 20:17:41 +0000405 with open(self.file2_path, 'w', encoding='utf-8') as file2:
Florent Xiclunafd1b0932010-03-28 00:25:02 +0000406 file2.write(cgi_file2 % self.pythonexe)
Georg Brandlb533e262008-05-25 18:19:30 +0000407 os.chmod(self.file2_path, 0o777)
408
Ned Deily915a30f2014-07-12 22:06:26 -0700409 self.file3_path = os.path.join(self.cgi_child_dir, 'file3.py')
410 with open(self.file3_path, 'w', encoding='utf-8') as file3:
411 file3.write(cgi_file1 % self.pythonexe)
412 os.chmod(self.file3_path, 0o777)
413
Georg Brandlb533e262008-05-25 18:19:30 +0000414 os.chdir(self.parent_dir)
415
416 def tearDown(self):
417 try:
418 os.chdir(self.cwd)
Florent Xiclunafd1b0932010-03-28 00:25:02 +0000419 if self.pythonexe != sys.executable:
420 os.remove(self.pythonexe)
Benjamin Peterson35aca892013-10-30 12:48:59 -0400421 if self.nocgi_path:
422 os.remove(self.nocgi_path)
Victor Stinner0b0ca0c2010-10-17 19:46:36 +0000423 if self.file1_path:
424 os.remove(self.file1_path)
425 if self.file2_path:
426 os.remove(self.file2_path)
Ned Deily915a30f2014-07-12 22:06:26 -0700427 if self.file3_path:
428 os.remove(self.file3_path)
429 os.rmdir(self.cgi_child_dir)
Georg Brandlb533e262008-05-25 18:19:30 +0000430 os.rmdir(self.cgi_dir)
431 os.rmdir(self.parent_dir)
432 finally:
433 BaseTestCase.tearDown(self)
434
Senthil Kumarand70846b2012-04-12 02:34:32 +0800435 def test_url_collapse_path(self):
436 # verify tail is the last portion and head is the rest on proper urls
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000437 test_vectors = {
Senthil Kumarand70846b2012-04-12 02:34:32 +0800438 '': '//',
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000439 '..': IndexError,
440 '/.//..': IndexError,
Senthil Kumarand70846b2012-04-12 02:34:32 +0800441 '/': '//',
442 '//': '//',
443 '/\\': '//\\',
444 '/.//': '//',
445 'cgi-bin/file1.py': '/cgi-bin/file1.py',
446 '/cgi-bin/file1.py': '/cgi-bin/file1.py',
447 'a': '//a',
448 '/a': '//a',
449 '//a': '//a',
450 './a': '//a',
451 './C:/': '/C:/',
452 '/a/b': '/a/b',
453 '/a/b/': '/a/b/',
454 '/a/b/.': '/a/b/',
455 '/a/b/c/..': '/a/b/',
456 '/a/b/c/../d': '/a/b/d',
457 '/a/b/c/../d/e/../f': '/a/b/d/f',
458 '/a/b/c/../d/e/../../f': '/a/b/f',
459 '/a/b/c/../d/e/.././././..//f': '/a/b/f',
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000460 '../a/b/c/../d/e/.././././..//f': IndexError,
Senthil Kumarand70846b2012-04-12 02:34:32 +0800461 '/a/b/c/../d/e/../../../f': '/a/f',
462 '/a/b/c/../d/e/../../../../f': '//f',
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000463 '/a/b/c/../d/e/../../../../../f': IndexError,
Senthil Kumarand70846b2012-04-12 02:34:32 +0800464 '/a/b/c/../d/e/../../../../f/..': '//',
465 '/a/b/c/../d/e/../../../../f/../.': '//',
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000466 }
467 for path, expected in test_vectors.items():
468 if isinstance(expected, type) and issubclass(expected, Exception):
469 self.assertRaises(expected,
Senthil Kumarand70846b2012-04-12 02:34:32 +0800470 server._url_collapse_path, path)
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000471 else:
Senthil Kumarand70846b2012-04-12 02:34:32 +0800472 actual = server._url_collapse_path(path)
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000473 self.assertEqual(expected, actual,
474 msg='path = %r\nGot: %r\nWanted: %r' %
475 (path, actual, expected))
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000476
Georg Brandlb533e262008-05-25 18:19:30 +0000477 def test_headers_and_content(self):
478 res = self.request('/cgi-bin/file1.py')
Antoine Pitroue768c392012-08-05 14:52:45 +0200479 self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000480 (res.read(), res.getheader('Content-type'), res.status))
Georg Brandlb533e262008-05-25 18:19:30 +0000481
Benjamin Peterson04e9de42013-10-30 12:43:09 -0400482 def test_issue19435(self):
483 res = self.request('///////////nocgi.py/../cgi-bin/nothere.sh')
484 self.assertEqual(res.status, 404)
485
Georg Brandlb533e262008-05-25 18:19:30 +0000486 def test_post(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000487 params = urllib.parse.urlencode(
488 {'spam' : 1, 'eggs' : 'python', 'bacon' : 123456})
Georg Brandlb533e262008-05-25 18:19:30 +0000489 headers = {'Content-type' : 'application/x-www-form-urlencoded'}
490 res = self.request('/cgi-bin/file2.py', 'POST', params, headers)
491
Antoine Pitroue768c392012-08-05 14:52:45 +0200492 self.assertEqual(res.read(), b'1, python, 123456' + self.linesep)
Georg Brandlb533e262008-05-25 18:19:30 +0000493
494 def test_invaliduri(self):
495 res = self.request('/cgi-bin/invalid')
496 res.read()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000497 self.assertEqual(res.status, 404)
Georg Brandlb533e262008-05-25 18:19:30 +0000498
499 def test_authorization(self):
500 headers = {b'Authorization' : b'Basic ' +
501 base64.b64encode(b'username:pass')}
502 res = self.request('/cgi-bin/file1.py', 'GET', headers=headers)
Antoine Pitroue768c392012-08-05 14:52:45 +0200503 self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000504 (res.read(), res.getheader('Content-type'), res.status))
Georg Brandlb533e262008-05-25 18:19:30 +0000505
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000506 def test_no_leading_slash(self):
507 # http://bugs.python.org/issue2254
508 res = self.request('cgi-bin/file1.py')
Antoine Pitroue768c392012-08-05 14:52:45 +0200509 self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000510 (res.read(), res.getheader('Content-type'), res.status))
511
Senthil Kumaran42713722010-10-03 17:55:45 +0000512 def test_os_environ_is_not_altered(self):
513 signature = "Test CGI Server"
514 os.environ['SERVER_SOFTWARE'] = signature
515 res = self.request('/cgi-bin/file1.py')
Antoine Pitroue768c392012-08-05 14:52:45 +0200516 self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
Senthil Kumaran42713722010-10-03 17:55:45 +0000517 (res.read(), res.getheader('Content-type'), res.status))
518 self.assertEqual(os.environ['SERVER_SOFTWARE'], signature)
519
Benjamin Peterson73b8b1c2014-06-14 18:36:29 -0700520 def test_urlquote_decoding_in_cgi_check(self):
521 res = self.request('/cgi-bin%2ffile1.py')
Benjamin Peterson314dc122014-06-16 23:15:50 -0700522 self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
Benjamin Peterson73b8b1c2014-06-14 18:36:29 -0700523 (res.read(), res.getheader('Content-type'), res.status))
524
Ned Deily915a30f2014-07-12 22:06:26 -0700525 def test_nested_cgi_path_issue21323(self):
526 res = self.request('/cgi-bin/child-dir/file3.py')
527 self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200),
528 (res.read(), res.getheader('Content-type'), res.status))
529
Georg Brandlb533e262008-05-25 18:19:30 +0000530
Georg Brandl6fcac0d2010-08-02 18:56:54 +0000531class SocketlessRequestHandler(SimpleHTTPRequestHandler):
532 def __init__(self):
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000533 self.get_called = False
534 self.protocol_version = "HTTP/1.1"
535
536 def do_GET(self):
537 self.get_called = True
538 self.send_response(200)
539 self.send_header('Content-Type', 'text/html')
540 self.end_headers()
541 self.wfile.write(b'<html><body>Data</body></html>\r\n')
542
543 def log_message(self, format, *args):
Georg Brandl6fcac0d2010-08-02 18:56:54 +0000544 pass
545
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000546class RejectingSocketlessRequestHandler(SocketlessRequestHandler):
547 def handle_expect_100(self):
548 self.send_error(417)
549 return False
550
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800551
552class AuditableBytesIO:
553
554 def __init__(self):
555 self.datas = []
556
557 def write(self, data):
558 self.datas.append(data)
559
560 def getData(self):
561 return b''.join(self.datas)
562
563 @property
564 def numWrites(self):
565 return len(self.datas)
566
567
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000568class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
Ezio Melotti3b3499b2011-03-16 11:35:38 +0200569 """Test the functionality of the BaseHTTPServer.
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000570
571 Test the support for the Expect 100-continue header.
572 """
573
574 HTTPResponseMatch = re.compile(b'HTTP/1.[0-9]+ 200 OK')
575
576 def setUp (self):
577 self.handler = SocketlessRequestHandler()
578
579 def send_typical_request(self, message):
580 input = BytesIO(message)
581 output = BytesIO()
582 self.handler.rfile = input
583 self.handler.wfile = output
584 self.handler.handle_one_request()
585 output.seek(0)
586 return output.readlines()
587
588 def verify_get_called(self):
589 self.assertTrue(self.handler.get_called)
590
591 def verify_expected_headers(self, headers):
592 for fieldName in b'Server: ', b'Date: ', b'Content-Type: ':
593 self.assertEqual(sum(h.startswith(fieldName) for h in headers), 1)
594
595 def verify_http_server_response(self, response):
596 match = self.HTTPResponseMatch.search(response)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200597 self.assertIsNotNone(match)
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000598
599 def test_http_1_1(self):
600 result = self.send_typical_request(b'GET / HTTP/1.1\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_http_1_0(self):
607 result = self.send_typical_request(b'GET / HTTP/1.0\r\n\r\n')
608 self.verify_http_server_response(result[0])
609 self.verify_expected_headers(result[1:-1])
610 self.verify_get_called()
611 self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n')
612
613 def test_http_0_9(self):
614 result = self.send_typical_request(b'GET / HTTP/0.9\r\n\r\n')
615 self.assertEqual(len(result), 1)
616 self.assertEqual(result[0], b'<html><body>Data</body></html>\r\n')
617 self.verify_get_called()
618
619 def test_with_continue_1_0(self):
620 result = self.send_typical_request(b'GET / HTTP/1.0\r\nExpect: 100-continue\r\n\r\n')
621 self.verify_http_server_response(result[0])
622 self.verify_expected_headers(result[1:-1])
623 self.verify_get_called()
624 self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n')
625
626 def test_with_continue_1_1(self):
627 result = self.send_typical_request(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n')
628 self.assertEqual(result[0], b'HTTP/1.1 100 Continue\r\n')
Benjamin Peterson04424232014-01-18 21:50:18 -0500629 self.assertEqual(result[1], b'\r\n')
630 self.assertEqual(result[2], b'HTTP/1.1 200 OK\r\n')
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000631 self.verify_expected_headers(result[2:-1])
632 self.verify_get_called()
633 self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n')
634
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800635 def test_header_buffering_of_send_error(self):
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000636
637 input = BytesIO(b'GET / HTTP/1.1\r\n\r\n')
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800638 output = AuditableBytesIO()
639 handler = SocketlessRequestHandler()
640 handler.rfile = input
641 handler.wfile = output
642 handler.request_version = 'HTTP/1.1'
643 handler.requestline = ''
644 handler.command = None
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000645
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800646 handler.send_error(418)
647 self.assertEqual(output.numWrites, 2)
648
649 def test_header_buffering_of_send_response_only(self):
650
651 input = BytesIO(b'GET / HTTP/1.1\r\n\r\n')
652 output = AuditableBytesIO()
653 handler = SocketlessRequestHandler()
654 handler.rfile = input
655 handler.wfile = output
656 handler.request_version = 'HTTP/1.1'
657
658 handler.send_response_only(418)
659 self.assertEqual(output.numWrites, 0)
660 handler.end_headers()
661 self.assertEqual(output.numWrites, 1)
662
663 def test_header_buffering_of_send_header(self):
664
665 input = BytesIO(b'GET / HTTP/1.1\r\n\r\n')
666 output = AuditableBytesIO()
667 handler = SocketlessRequestHandler()
668 handler.rfile = input
669 handler.wfile = output
670 handler.request_version = 'HTTP/1.1'
671
672 handler.send_header('Foo', 'foo')
673 handler.send_header('bar', 'bar')
674 self.assertEqual(output.numWrites, 0)
675 handler.end_headers()
676 self.assertEqual(output.getData(), b'Foo: foo\r\nbar: bar\r\n\r\n')
677 self.assertEqual(output.numWrites, 1)
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000678
679 def test_header_unbuffered_when_continue(self):
680
681 def _readAndReseek(f):
682 pos = f.tell()
683 f.seek(0)
684 data = f.read()
685 f.seek(pos)
686 return data
687
688 input = BytesIO(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n')
689 output = BytesIO()
690 self.handler.rfile = input
691 self.handler.wfile = output
692 self.handler.request_version = 'HTTP/1.1'
693
694 self.handler.handle_one_request()
695 self.assertNotEqual(_readAndReseek(output), b'')
696 result = _readAndReseek(output).split(b'\r\n')
697 self.assertEqual(result[0], b'HTTP/1.1 100 Continue')
Benjamin Peterson04424232014-01-18 21:50:18 -0500698 self.assertEqual(result[1], b'')
699 self.assertEqual(result[2], b'HTTP/1.1 200 OK')
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000700
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000701 def test_with_continue_rejected(self):
702 usual_handler = self.handler # Save to avoid breaking any subsequent tests.
703 self.handler = RejectingSocketlessRequestHandler()
704 result = self.send_typical_request(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n')
705 self.assertEqual(result[0], b'HTTP/1.1 417 Expectation Failed\r\n')
706 self.verify_expected_headers(result[1:-1])
707 # The expect handler should short circuit the usual get method by
708 # returning false here, so get_called should be false
709 self.assertFalse(self.handler.get_called)
710 self.assertEqual(sum(r == b'Connection: close\r\n' for r in result[1:-1]), 1)
711 self.handler = usual_handler # Restore to avoid breaking any subsequent tests.
712
Antoine Pitrouc4924372010-12-16 16:48:36 +0000713 def test_request_length(self):
714 # Issue #10714: huge request lines are discarded, to avoid Denial
715 # of Service attacks.
716 result = self.send_typical_request(b'GET ' + b'x' * 65537)
717 self.assertEqual(result[0], b'HTTP/1.1 414 Request-URI Too Long\r\n')
718 self.assertFalse(self.handler.get_called)
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000719
Senthil Kumaran5466bf12010-12-18 16:55:23 +0000720 def test_header_length(self):
721 # Issue #6791: same for headers
722 result = self.send_typical_request(
723 b'GET / HTTP/1.1\r\nX-Foo: bar' + b'r' * 65537 + b'\r\n\r\n')
724 self.assertEqual(result[0], b'HTTP/1.1 400 Line too long\r\n')
725 self.assertFalse(self.handler.get_called)
726
Georg Brandl6fcac0d2010-08-02 18:56:54 +0000727class SimpleHTTPRequestHandlerTestCase(unittest.TestCase):
728 """ Test url parsing """
729 def setUp(self):
730 self.translated = os.getcwd()
731 self.translated = os.path.join(self.translated, 'filename')
732 self.handler = SocketlessRequestHandler()
733
734 def test_query_arguments(self):
735 path = self.handler.translate_path('/filename')
736 self.assertEqual(path, self.translated)
737 path = self.handler.translate_path('/filename?foo=bar')
738 self.assertEqual(path, self.translated)
739 path = self.handler.translate_path('/filename?a=b&spam=eggs#zot')
740 self.assertEqual(path, self.translated)
741
742 def test_start_with_double_slash(self):
743 path = self.handler.translate_path('//filename')
744 self.assertEqual(path, self.translated)
745 path = self.handler.translate_path('//filename?foo=bar')
746 self.assertEqual(path, self.translated)
747
748
Georg Brandlb533e262008-05-25 18:19:30 +0000749def test_main(verbose=None):
Georg Brandl6fcac0d2010-08-02 18:56:54 +0000750 cwd = os.getcwd()
Georg Brandlb533e262008-05-25 18:19:30 +0000751 try:
Georg Brandl6fcac0d2010-08-02 18:56:54 +0000752 support.run_unittest(
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000753 BaseHTTPRequestHandlerTestCase,
Georg Brandl6fcac0d2010-08-02 18:56:54 +0000754 BaseHTTPServerTestCase,
755 SimpleHTTPServerTestCase,
756 CGIHTTPServerTestCase,
757 SimpleHTTPRequestHandlerTestCase,
758 )
Georg Brandlb533e262008-05-25 18:19:30 +0000759 finally:
760 os.chdir(cwd)
761
762if __name__ == '__main__':
763 test_main()