2008-05-18 06:12:20 -03:00
|
|
|
"""Unittests for the various HTTPServer modules.
|
|
|
|
|
|
|
|
Written by Cody A.W. Somerville <cody-somerville@ubuntu.com>,
|
|
|
|
Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2010-09-30 03:40:56 -03:00
|
|
|
import re
|
2008-05-18 06:12:20 -03:00
|
|
|
import base64
|
|
|
|
import shutil
|
|
|
|
import urllib
|
|
|
|
import httplib
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
2012-04-11 15:23:23 -03:00
|
|
|
import CGIHTTPServer
|
2010-09-30 03:40:56 -03:00
|
|
|
|
|
|
|
|
2012-04-11 15:23:23 -03:00
|
|
|
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
|
|
|
from SimpleHTTPServer import SimpleHTTPRequestHandler
|
|
|
|
from CGIHTTPServer import CGIHTTPRequestHandler
|
|
|
|
from StringIO import StringIO
|
2008-05-18 06:12:20 -03:00
|
|
|
from test import test_support
|
2012-04-11 15:23:23 -03:00
|
|
|
|
|
|
|
|
2010-04-27 20:55:59 -03:00
|
|
|
threading = test_support.import_module('threading')
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
|
|
|
|
class NoLogRequestHandler:
|
|
|
|
def log_message(self, *args):
|
|
|
|
# don't write log messages to stderr
|
|
|
|
pass
|
|
|
|
|
2010-12-16 13:18:49 -04:00
|
|
|
class SocketlessRequestHandler(SimpleHTTPRequestHandler):
|
2010-09-30 03:40:56 -03:00
|
|
|
def __init__(self):
|
|
|
|
self.get_called = False
|
|
|
|
self.protocol_version = "HTTP/1.1"
|
|
|
|
|
|
|
|
def do_GET(self):
|
|
|
|
self.get_called = True
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header('Content-Type', 'text/html')
|
|
|
|
self.end_headers()
|
2010-12-16 13:18:49 -04:00
|
|
|
self.wfile.write(b'<html><body>Data</body></html>\r\n')
|
2010-09-30 03:40:56 -03:00
|
|
|
|
2012-04-11 15:23:23 -03:00
|
|
|
def log_message(self, fmt, *args):
|
2010-09-30 03:40:56 -03:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2008-05-18 06:12:20 -03:00
|
|
|
class TestServerThread(threading.Thread):
|
|
|
|
def __init__(self, test_object, request_handler):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.request_handler = request_handler
|
|
|
|
self.test_object = test_object
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.server = HTTPServer(('', 0), self.request_handler)
|
|
|
|
self.test_object.PORT = self.server.socket.getsockname()[1]
|
2010-04-25 18:15:50 -03:00
|
|
|
self.test_object.server_started.set()
|
|
|
|
self.test_object = None
|
2008-05-18 06:12:20 -03:00
|
|
|
try:
|
2010-04-25 18:15:50 -03:00
|
|
|
self.server.serve_forever(0.05)
|
2008-05-18 06:12:20 -03:00
|
|
|
finally:
|
|
|
|
self.server.server_close()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.server.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
class BaseTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2009-10-27 15:50:52 -03:00
|
|
|
self._threads = test_support.threading_setup()
|
2009-10-17 12:23:08 -03:00
|
|
|
os.environ = test_support.EnvironmentVarGuard()
|
2010-04-25 18:15:50 -03:00
|
|
|
self.server_started = threading.Event()
|
2008-05-18 06:12:20 -03:00
|
|
|
self.thread = TestServerThread(self, self.request_handler)
|
|
|
|
self.thread.start()
|
2010-04-25 18:15:50 -03:00
|
|
|
self.server_started.wait()
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.thread.stop()
|
2009-10-17 12:23:08 -03:00
|
|
|
os.environ.__exit__()
|
2009-10-27 15:50:52 -03:00
|
|
|
test_support.threading_cleanup(*self._threads)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def request(self, uri, method='GET', body=None, headers={}):
|
|
|
|
self.connection = httplib.HTTPConnection('localhost', self.PORT)
|
|
|
|
self.connection.request(method, uri, body, headers)
|
|
|
|
return self.connection.getresponse()
|
|
|
|
|
2010-09-30 03:40:56 -03:00
|
|
|
class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
|
2011-03-16 07:34:31 -03:00
|
|
|
"""Test the functionality of the BaseHTTPServer focussing on
|
2010-09-30 03:40:56 -03:00
|
|
|
BaseHTTPRequestHandler.
|
|
|
|
"""
|
|
|
|
|
|
|
|
HTTPResponseMatch = re.compile('HTTP/1.[0-9]+ 200 OK')
|
|
|
|
|
|
|
|
def setUp (self):
|
|
|
|
self.handler = SocketlessRequestHandler()
|
|
|
|
|
|
|
|
def send_typical_request(self, message):
|
2012-04-11 15:23:23 -03:00
|
|
|
input_msg = StringIO(message)
|
2010-09-30 03:40:56 -03:00
|
|
|
output = StringIO()
|
2012-04-11 15:23:23 -03:00
|
|
|
self.handler.rfile = input_msg
|
2010-09-30 03:40:56 -03:00
|
|
|
self.handler.wfile = output
|
|
|
|
self.handler.handle_one_request()
|
|
|
|
output.seek(0)
|
|
|
|
return output.readlines()
|
|
|
|
|
|
|
|
def verify_get_called(self):
|
|
|
|
self.assertTrue(self.handler.get_called)
|
|
|
|
|
|
|
|
def verify_expected_headers(self, headers):
|
|
|
|
for fieldName in 'Server: ', 'Date: ', 'Content-Type: ':
|
|
|
|
self.assertEqual(sum(h.startswith(fieldName) for h in headers), 1)
|
|
|
|
|
|
|
|
def verify_http_server_response(self, response):
|
|
|
|
match = self.HTTPResponseMatch.search(response)
|
2014-02-08 08:49:55 -04:00
|
|
|
self.assertIsNotNone(match)
|
2010-09-30 03:40:56 -03:00
|
|
|
|
|
|
|
def test_http_1_1(self):
|
|
|
|
result = self.send_typical_request('GET / HTTP/1.1\r\n\r\n')
|
|
|
|
self.verify_http_server_response(result[0])
|
|
|
|
self.verify_expected_headers(result[1:-1])
|
|
|
|
self.verify_get_called()
|
|
|
|
self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n')
|
|
|
|
|
|
|
|
def test_http_1_0(self):
|
|
|
|
result = self.send_typical_request('GET / HTTP/1.0\r\n\r\n')
|
|
|
|
self.verify_http_server_response(result[0])
|
|
|
|
self.verify_expected_headers(result[1:-1])
|
|
|
|
self.verify_get_called()
|
|
|
|
self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n')
|
|
|
|
|
|
|
|
def test_http_0_9(self):
|
|
|
|
result = self.send_typical_request('GET / HTTP/0.9\r\n\r\n')
|
|
|
|
self.assertEqual(len(result), 1)
|
|
|
|
self.assertEqual(result[0], '<html><body>Data</body></html>\r\n')
|
|
|
|
self.verify_get_called()
|
|
|
|
|
|
|
|
def test_with_continue_1_0(self):
|
|
|
|
result = self.send_typical_request('GET / HTTP/1.0\r\nExpect: 100-continue\r\n\r\n')
|
|
|
|
self.verify_http_server_response(result[0])
|
|
|
|
self.verify_expected_headers(result[1:-1])
|
|
|
|
self.verify_get_called()
|
|
|
|
self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n')
|
|
|
|
|
2010-12-16 13:18:49 -04:00
|
|
|
def test_request_length(self):
|
|
|
|
# Issue #10714: huge request lines are discarded, to avoid Denial
|
|
|
|
# of Service attacks.
|
|
|
|
result = self.send_typical_request(b'GET ' + b'x' * 65537)
|
|
|
|
self.assertEqual(result[0], b'HTTP/1.1 414 Request-URI Too Long\r\n')
|
|
|
|
self.assertFalse(self.handler.get_called)
|
|
|
|
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
class BaseHTTPServerTestCase(BaseTestCase):
|
|
|
|
class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler):
|
|
|
|
protocol_version = 'HTTP/1.1'
|
|
|
|
default_request_version = 'HTTP/1.1'
|
|
|
|
|
|
|
|
def do_TEST(self):
|
|
|
|
self.send_response(204)
|
|
|
|
self.send_header('Content-Type', 'text/html')
|
|
|
|
self.send_header('Connection', 'close')
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def do_KEEP(self):
|
|
|
|
self.send_response(204)
|
|
|
|
self.send_header('Content-Type', 'text/html')
|
|
|
|
self.send_header('Connection', 'keep-alive')
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def do_KEYERROR(self):
|
|
|
|
self.send_error(999)
|
|
|
|
|
|
|
|
def do_CUSTOM(self):
|
|
|
|
self.send_response(999)
|
|
|
|
self.send_header('Content-Type', 'text/html')
|
|
|
|
self.send_header('Connection', 'close')
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
BaseTestCase.setUp(self)
|
|
|
|
self.con = httplib.HTTPConnection('localhost', self.PORT)
|
|
|
|
self.con.connect()
|
|
|
|
|
|
|
|
def test_command(self):
|
|
|
|
self.con.request('GET', '/')
|
|
|
|
res = self.con.getresponse()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.status, 501)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_request_line_trimming(self):
|
|
|
|
self.con._http_vsn_str = 'HTTP/1.1\n'
|
2014-06-24 17:49:24 -03:00
|
|
|
self.con.putrequest('XYZBOGUS', '/')
|
2008-05-18 06:12:20 -03:00
|
|
|
self.con.endheaders()
|
|
|
|
res = self.con.getresponse()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.status, 501)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_version_bogus(self):
|
|
|
|
self.con._http_vsn_str = 'FUBAR'
|
|
|
|
self.con.putrequest('GET', '/')
|
|
|
|
self.con.endheaders()
|
|
|
|
res = self.con.getresponse()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.status, 400)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_version_digits(self):
|
|
|
|
self.con._http_vsn_str = 'HTTP/9.9.9'
|
|
|
|
self.con.putrequest('GET', '/')
|
|
|
|
self.con.endheaders()
|
|
|
|
res = self.con.getresponse()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.status, 400)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_version_none_get(self):
|
|
|
|
self.con._http_vsn_str = ''
|
|
|
|
self.con.putrequest('GET', '/')
|
|
|
|
self.con.endheaders()
|
|
|
|
res = self.con.getresponse()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.status, 501)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_version_none(self):
|
2014-06-24 17:49:24 -03:00
|
|
|
# Test that a valid method is rejected when not HTTP/1.x
|
2008-05-18 06:12:20 -03:00
|
|
|
self.con._http_vsn_str = ''
|
2014-06-24 17:49:24 -03:00
|
|
|
self.con.putrequest('CUSTOM', '/')
|
2008-05-18 06:12:20 -03:00
|
|
|
self.con.endheaders()
|
|
|
|
res = self.con.getresponse()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.status, 400)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_version_invalid(self):
|
|
|
|
self.con._http_vsn = 99
|
|
|
|
self.con._http_vsn_str = 'HTTP/9.9'
|
|
|
|
self.con.putrequest('GET', '/')
|
|
|
|
self.con.endheaders()
|
|
|
|
res = self.con.getresponse()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.status, 505)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_send_blank(self):
|
|
|
|
self.con._http_vsn_str = ''
|
|
|
|
self.con.putrequest('', '')
|
|
|
|
self.con.endheaders()
|
|
|
|
res = self.con.getresponse()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.status, 400)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_header_close(self):
|
|
|
|
self.con.putrequest('GET', '/')
|
|
|
|
self.con.putheader('Connection', 'close')
|
|
|
|
self.con.endheaders()
|
|
|
|
res = self.con.getresponse()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.status, 501)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_head_keep_alive(self):
|
|
|
|
self.con._http_vsn_str = 'HTTP/1.1'
|
|
|
|
self.con.putrequest('GET', '/')
|
|
|
|
self.con.putheader('Connection', 'keep-alive')
|
|
|
|
self.con.endheaders()
|
|
|
|
res = self.con.getresponse()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.status, 501)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_handler(self):
|
|
|
|
self.con.request('TEST', '/')
|
|
|
|
res = self.con.getresponse()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.status, 204)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_return_header_keep_alive(self):
|
|
|
|
self.con.request('KEEP', '/')
|
|
|
|
res = self.con.getresponse()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.getheader('Connection'), 'keep-alive')
|
2008-05-18 06:12:20 -03:00
|
|
|
self.con.request('TEST', '/')
|
2010-10-30 21:36:01 -03:00
|
|
|
self.addCleanup(self.con.close)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_internal_key_error(self):
|
|
|
|
self.con.request('KEYERROR', '/')
|
|
|
|
res = self.con.getresponse()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.status, 999)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_return_custom_status(self):
|
|
|
|
self.con.request('CUSTOM', '/')
|
|
|
|
res = self.con.getresponse()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.status, 999)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
|
|
|
|
class SimpleHTTPServerTestCase(BaseTestCase):
|
|
|
|
class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
BaseTestCase.setUp(self)
|
2008-05-20 03:47:31 -03:00
|
|
|
self.cwd = os.getcwd()
|
|
|
|
basetempdir = tempfile.gettempdir()
|
|
|
|
os.chdir(basetempdir)
|
2008-05-18 06:12:20 -03:00
|
|
|
self.data = 'We are the knights who say Ni!'
|
2008-05-20 03:47:31 -03:00
|
|
|
self.tempdir = tempfile.mkdtemp(dir=basetempdir)
|
2008-05-18 06:12:20 -03:00
|
|
|
self.tempdir_name = os.path.basename(self.tempdir)
|
2008-05-20 03:47:31 -03:00
|
|
|
temp = open(os.path.join(self.tempdir, 'test'), 'wb')
|
|
|
|
temp.write(self.data)
|
|
|
|
temp.close()
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
try:
|
2008-05-20 03:47:31 -03:00
|
|
|
os.chdir(self.cwd)
|
2008-05-18 06:12:20 -03:00
|
|
|
try:
|
|
|
|
shutil.rmtree(self.tempdir)
|
2012-04-11 15:23:23 -03:00
|
|
|
except OSError:
|
2008-05-18 06:12:20 -03:00
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
BaseTestCase.tearDown(self)
|
|
|
|
|
|
|
|
def check_status_and_reason(self, response, status, data=None):
|
|
|
|
body = response.read()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(response)
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(response.status, status)
|
|
|
|
self.assertIsNotNone(response.reason)
|
2008-05-18 06:12:20 -03:00
|
|
|
if data:
|
|
|
|
self.assertEqual(data, body)
|
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
#constructs the path relative to the root directory of the HTTPServer
|
2008-05-20 03:47:31 -03:00
|
|
|
response = self.request(self.tempdir_name + '/test')
|
2008-05-18 06:12:20 -03:00
|
|
|
self.check_status_and_reason(response, 200, data=self.data)
|
2013-09-13 04:18:55 -03:00
|
|
|
# check for trailing "/" which should return 404. See Issue17324
|
|
|
|
response = self.request(self.tempdir_name + '/test/')
|
|
|
|
self.check_status_and_reason(response, 404)
|
2008-05-18 06:12:20 -03:00
|
|
|
response = self.request(self.tempdir_name + '/')
|
|
|
|
self.check_status_and_reason(response, 200)
|
|
|
|
response = self.request(self.tempdir_name)
|
|
|
|
self.check_status_and_reason(response, 301)
|
|
|
|
response = self.request('/ThisDoesNotExist')
|
|
|
|
self.check_status_and_reason(response, 404)
|
|
|
|
response = self.request('/' + 'ThisDoesNotExist' + '/')
|
|
|
|
self.check_status_and_reason(response, 404)
|
2014-04-04 14:59:33 -03:00
|
|
|
with open(os.path.join(self.tempdir_name, 'index.html'), 'w') as fp:
|
|
|
|
response = self.request('/' + self.tempdir_name + '/')
|
|
|
|
self.check_status_and_reason(response, 200)
|
|
|
|
# chmod() doesn't work as expected on Windows, and filesystem
|
|
|
|
# permissions are ignored by root on Unix.
|
|
|
|
if os.name == 'posix' and os.geteuid() != 0:
|
|
|
|
os.chmod(self.tempdir, 0)
|
|
|
|
response = self.request(self.tempdir_name + '/')
|
|
|
|
self.check_status_and_reason(response, 404)
|
|
|
|
os.chmod(self.tempdir, 0755)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_head(self):
|
|
|
|
response = self.request(
|
2008-05-20 03:47:31 -03:00
|
|
|
self.tempdir_name + '/test', method='HEAD')
|
2008-05-18 06:12:20 -03:00
|
|
|
self.check_status_and_reason(response, 200)
|
|
|
|
self.assertEqual(response.getheader('content-length'),
|
|
|
|
str(len(self.data)))
|
|
|
|
self.assertEqual(response.getheader('content-type'),
|
|
|
|
'application/octet-stream')
|
|
|
|
|
|
|
|
def test_invalid_requests(self):
|
|
|
|
response = self.request('/', method='FOO')
|
|
|
|
self.check_status_and_reason(response, 501)
|
|
|
|
# requests must be case sensitive,so this should fail too
|
2014-10-18 18:10:02 -03:00
|
|
|
response = self.request('/', method='custom')
|
2008-05-18 06:12:20 -03:00
|
|
|
self.check_status_and_reason(response, 501)
|
|
|
|
response = self.request('/', method='GETs')
|
|
|
|
self.check_status_and_reason(response, 501)
|
|
|
|
|
|
|
|
|
|
|
|
cgi_file1 = """\
|
|
|
|
#!%s
|
|
|
|
|
|
|
|
print "Content-type: text/html"
|
|
|
|
print
|
|
|
|
print "Hello World"
|
|
|
|
"""
|
|
|
|
|
|
|
|
cgi_file2 = """\
|
|
|
|
#!%s
|
|
|
|
import cgi
|
|
|
|
|
|
|
|
print "Content-type: text/html"
|
|
|
|
print
|
|
|
|
|
|
|
|
form = cgi.FieldStorage()
|
2010-03-19 15:34:55 -03:00
|
|
|
print "%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"),
|
|
|
|
form.getfirst("bacon"))
|
2008-05-18 06:12:20 -03:00
|
|
|
"""
|
|
|
|
|
2011-11-02 15:32:54 -03:00
|
|
|
|
|
|
|
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
|
|
|
|
"This test can't be run reliably as root (issue #13308).")
|
2008-05-18 06:12:20 -03:00
|
|
|
class CGIHTTPServerTestCase(BaseTestCase):
|
|
|
|
class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
BaseTestCase.setUp(self)
|
|
|
|
self.parent_dir = tempfile.mkdtemp()
|
|
|
|
self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin')
|
2014-07-13 02:01:15 -03:00
|
|
|
self.cgi_child_dir = os.path.join(self.cgi_dir, 'child-dir')
|
2008-05-18 06:12:20 -03:00
|
|
|
os.mkdir(self.cgi_dir)
|
2014-07-13 02:01:15 -03:00
|
|
|
os.mkdir(self.cgi_child_dir)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
2010-03-22 14:18:18 -03:00
|
|
|
# The shebang line should be pure ASCII: use symlink if possible.
|
|
|
|
# See issue #7668.
|
|
|
|
if hasattr(os, 'symlink'):
|
|
|
|
self.pythonexe = os.path.join(self.parent_dir, 'python')
|
|
|
|
os.symlink(sys.executable, self.pythonexe)
|
|
|
|
else:
|
|
|
|
self.pythonexe = sys.executable
|
|
|
|
|
2013-10-30 13:43:09 -03:00
|
|
|
self.nocgi_path = os.path.join(self.parent_dir, 'nocgi.py')
|
|
|
|
with open(self.nocgi_path, 'w') as fp:
|
|
|
|
fp.write(cgi_file1 % self.pythonexe)
|
|
|
|
os.chmod(self.nocgi_path, 0777)
|
|
|
|
|
2008-05-18 06:12:20 -03:00
|
|
|
self.file1_path = os.path.join(self.cgi_dir, 'file1.py')
|
|
|
|
with open(self.file1_path, 'w') as file1:
|
2010-03-22 14:18:18 -03:00
|
|
|
file1.write(cgi_file1 % self.pythonexe)
|
2008-05-18 06:12:20 -03:00
|
|
|
os.chmod(self.file1_path, 0777)
|
|
|
|
|
|
|
|
self.file2_path = os.path.join(self.cgi_dir, 'file2.py')
|
|
|
|
with open(self.file2_path, 'w') as file2:
|
2010-03-22 14:18:18 -03:00
|
|
|
file2.write(cgi_file2 % self.pythonexe)
|
2008-05-18 06:12:20 -03:00
|
|
|
os.chmod(self.file2_path, 0777)
|
|
|
|
|
2014-07-13 02:01:15 -03:00
|
|
|
self.file3_path = os.path.join(self.cgi_child_dir, 'file3.py')
|
|
|
|
with open(self.file3_path, 'w') as file3:
|
|
|
|
file3.write(cgi_file1 % self.pythonexe)
|
|
|
|
os.chmod(self.file3_path, 0777)
|
|
|
|
|
2008-05-20 03:47:31 -03:00
|
|
|
self.cwd = os.getcwd()
|
2008-05-18 06:12:20 -03:00
|
|
|
os.chdir(self.parent_dir)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
try:
|
2008-05-20 03:47:31 -03:00
|
|
|
os.chdir(self.cwd)
|
2010-03-22 14:18:18 -03:00
|
|
|
if self.pythonexe != sys.executable:
|
|
|
|
os.remove(self.pythonexe)
|
2013-10-30 13:43:09 -03:00
|
|
|
os.remove(self.nocgi_path)
|
2008-05-18 06:12:20 -03:00
|
|
|
os.remove(self.file1_path)
|
|
|
|
os.remove(self.file2_path)
|
2014-07-13 02:01:15 -03:00
|
|
|
os.remove(self.file3_path)
|
|
|
|
os.rmdir(self.cgi_child_dir)
|
2008-05-18 06:12:20 -03:00
|
|
|
os.rmdir(self.cgi_dir)
|
|
|
|
os.rmdir(self.parent_dir)
|
|
|
|
finally:
|
|
|
|
BaseTestCase.tearDown(self)
|
|
|
|
|
2012-04-11 15:23:23 -03:00
|
|
|
def test_url_collapse_path(self):
|
2012-04-10 16:07:57 -03:00
|
|
|
# verify tail is the last portion and head is the rest on proper urls
|
2009-04-06 03:33:26 -03:00
|
|
|
test_vectors = {
|
2012-04-11 15:23:23 -03:00
|
|
|
'': '//',
|
2009-04-06 03:33:26 -03:00
|
|
|
'..': IndexError,
|
|
|
|
'/.//..': IndexError,
|
2012-04-11 15:23:23 -03:00
|
|
|
'/': '//',
|
|
|
|
'//': '//',
|
|
|
|
'/\\': '//\\',
|
|
|
|
'/.//': '//',
|
|
|
|
'cgi-bin/file1.py': '/cgi-bin/file1.py',
|
|
|
|
'/cgi-bin/file1.py': '/cgi-bin/file1.py',
|
|
|
|
'a': '//a',
|
|
|
|
'/a': '//a',
|
|
|
|
'//a': '//a',
|
|
|
|
'./a': '//a',
|
|
|
|
'./C:/': '/C:/',
|
|
|
|
'/a/b': '/a/b',
|
|
|
|
'/a/b/': '/a/b/',
|
|
|
|
'/a/b/.': '/a/b/',
|
|
|
|
'/a/b/c/..': '/a/b/',
|
|
|
|
'/a/b/c/../d': '/a/b/d',
|
|
|
|
'/a/b/c/../d/e/../f': '/a/b/d/f',
|
|
|
|
'/a/b/c/../d/e/../../f': '/a/b/f',
|
|
|
|
'/a/b/c/../d/e/.././././..//f': '/a/b/f',
|
2009-04-06 03:33:26 -03:00
|
|
|
'../a/b/c/../d/e/.././././..//f': IndexError,
|
2012-04-11 15:23:23 -03:00
|
|
|
'/a/b/c/../d/e/../../../f': '/a/f',
|
|
|
|
'/a/b/c/../d/e/../../../../f': '//f',
|
2009-04-06 03:33:26 -03:00
|
|
|
'/a/b/c/../d/e/../../../../../f': IndexError,
|
2012-04-11 15:23:23 -03:00
|
|
|
'/a/b/c/../d/e/../../../../f/..': '//',
|
|
|
|
'/a/b/c/../d/e/../../../../f/../.': '//',
|
2009-04-06 03:33:26 -03:00
|
|
|
}
|
|
|
|
for path, expected in test_vectors.iteritems():
|
|
|
|
if isinstance(expected, type) and issubclass(expected, Exception):
|
|
|
|
self.assertRaises(expected,
|
2012-04-11 15:23:23 -03:00
|
|
|
CGIHTTPServer._url_collapse_path, path)
|
2009-04-06 03:33:26 -03:00
|
|
|
else:
|
2012-04-11 15:23:23 -03:00
|
|
|
actual = CGIHTTPServer._url_collapse_path(path)
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(expected, actual,
|
|
|
|
msg='path = %r\nGot: %r\nWanted: %r' %
|
|
|
|
(path, actual, expected))
|
2009-04-06 03:33:26 -03:00
|
|
|
|
2008-05-18 06:12:20 -03:00
|
|
|
def test_headers_and_content(self):
|
|
|
|
res = self.request('/cgi-bin/file1.py')
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(('Hello World\n', 'text/html', 200),
|
|
|
|
(res.read(), res.getheader('Content-type'), res.status))
|
2008-05-18 06:12:20 -03:00
|
|
|
|
2013-10-30 13:43:09 -03:00
|
|
|
def test_issue19435(self):
|
|
|
|
res = self.request('///////////nocgi.py/../cgi-bin/nothere.sh')
|
|
|
|
self.assertEqual(res.status, 404)
|
|
|
|
|
2008-05-18 06:12:20 -03:00
|
|
|
def test_post(self):
|
|
|
|
params = urllib.urlencode({'spam' : 1, 'eggs' : 'python', 'bacon' : 123456})
|
|
|
|
headers = {'Content-type' : 'application/x-www-form-urlencoded'}
|
|
|
|
res = self.request('/cgi-bin/file2.py', 'POST', params, headers)
|
|
|
|
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.read(), '1, python, 123456\n')
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_invaliduri(self):
|
|
|
|
res = self.request('/cgi-bin/invalid')
|
|
|
|
res.read()
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(res.status, 404)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
|
|
|
def test_authorization(self):
|
2010-03-19 15:34:55 -03:00
|
|
|
headers = {'Authorization' : 'Basic %s' %
|
|
|
|
base64.b64encode('username:pass')}
|
2008-05-18 06:12:20 -03:00
|
|
|
res = self.request('/cgi-bin/file1.py', 'GET', headers=headers)
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(('Hello World\n', 'text/html', 200),
|
|
|
|
(res.read(), res.getheader('Content-type'), res.status))
|
2008-05-18 06:12:20 -03:00
|
|
|
|
2009-04-06 03:33:26 -03:00
|
|
|
def test_no_leading_slash(self):
|
|
|
|
# http://bugs.python.org/issue2254
|
|
|
|
res = self.request('cgi-bin/file1.py')
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertEqual(('Hello World\n', 'text/html', 200),
|
2009-04-06 03:33:26 -03:00
|
|
|
(res.read(), res.getheader('Content-type'), res.status))
|
|
|
|
|
2010-10-03 15:16:52 -03:00
|
|
|
def test_os_environ_is_not_altered(self):
|
|
|
|
signature = "Test CGI Server"
|
|
|
|
os.environ['SERVER_SOFTWARE'] = signature
|
|
|
|
res = self.request('/cgi-bin/file1.py')
|
|
|
|
self.assertEqual((b'Hello World\n', 'text/html', 200),
|
|
|
|
(res.read(), res.getheader('Content-type'), res.status))
|
|
|
|
self.assertEqual(os.environ['SERVER_SOFTWARE'], signature)
|
2008-05-18 06:12:20 -03:00
|
|
|
|
2014-06-14 22:36:29 -03:00
|
|
|
def test_urlquote_decoding_in_cgi_check(self):
|
|
|
|
res = self.request('/cgi-bin%2ffile1.py')
|
|
|
|
self.assertEqual((b'Hello World\n', 'text/html', 200),
|
|
|
|
(res.read(), res.getheader('Content-type'), res.status))
|
|
|
|
|
2014-07-13 02:01:15 -03:00
|
|
|
def test_nested_cgi_path_issue21323(self):
|
|
|
|
res = self.request('/cgi-bin/child-dir/file3.py')
|
|
|
|
self.assertEqual((b'Hello World\n', 'text/html', 200),
|
|
|
|
(res.read(), res.getheader('Content-type'), res.status))
|
|
|
|
|
2010-12-16 13:11:34 -04:00
|
|
|
|
|
|
|
class SimpleHTTPRequestHandlerTestCase(unittest.TestCase):
|
|
|
|
""" Test url parsing """
|
|
|
|
def setUp(self):
|
|
|
|
self.translated = os.getcwd()
|
|
|
|
self.translated = os.path.join(self.translated, 'filename')
|
|
|
|
self.handler = SocketlessRequestHandler()
|
|
|
|
|
|
|
|
def test_query_arguments(self):
|
|
|
|
path = self.handler.translate_path('/filename')
|
|
|
|
self.assertEqual(path, self.translated)
|
|
|
|
path = self.handler.translate_path('/filename?foo=bar')
|
|
|
|
self.assertEqual(path, self.translated)
|
|
|
|
path = self.handler.translate_path('/filename?a=b&spam=eggs#zot')
|
|
|
|
self.assertEqual(path, self.translated)
|
|
|
|
|
|
|
|
def test_start_with_double_slash(self):
|
|
|
|
path = self.handler.translate_path('//filename')
|
|
|
|
self.assertEqual(path, self.translated)
|
|
|
|
path = self.handler.translate_path('//filename?foo=bar')
|
|
|
|
self.assertEqual(path, self.translated)
|
|
|
|
|
|
|
|
|
2008-05-18 06:12:20 -03:00
|
|
|
def test_main(verbose=None):
|
|
|
|
try:
|
|
|
|
cwd = os.getcwd()
|
2010-09-30 03:40:56 -03:00
|
|
|
test_support.run_unittest(BaseHTTPRequestHandlerTestCase,
|
2010-12-16 13:11:34 -04:00
|
|
|
SimpleHTTPRequestHandlerTestCase,
|
2010-09-30 03:40:56 -03:00
|
|
|
BaseHTTPServerTestCase,
|
|
|
|
SimpleHTTPServerTestCase,
|
|
|
|
CGIHTTPServerTestCase
|
|
|
|
)
|
2008-05-18 06:12:20 -03:00
|
|
|
finally:
|
|
|
|
os.chdir(cwd)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|