| 1 |
"""Unittests for the various HTTPServer modules. |
| 2 |
|
| 3 |
Written by Cody A.W. Somerville <[email protected]>, |
| 4 |
Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest. |
| 5 |
""" |
| 6 |
from collections import OrderedDict |
| 7 |
from http.server import BaseHTTPRequestHandler, HTTPServer, HTTPSServer, \ |
| 8 |
SimpleHTTPRequestHandler, CGIHTTPRequestHandler |
| 9 |
from http import server, HTTPStatus |
| 10 |
|
| 11 |
import contextlib |
| 12 |
import os |
| 13 |
import socket |
| 14 |
import sys |
| 15 |
import re |
| 16 |
import base64 |
| 17 |
import ntpath |
| 18 |
import pathlib |
| 19 |
import shutil |
| 20 |
import email.message |
| 21 |
import email.utils |
| 22 |
import html |
| 23 |
import http, http.client |
| 24 |
import urllib.parse |
| 25 |
import tempfile |
| 26 |
import time |
| 27 |
import datetime |
| 28 |
import threading |
| 29 |
from unittest import mock |
| 30 |
from io import BytesIO, StringIO |
| 31 |
|
| 32 |
import unittest |
| 33 |
from test import support |
| 34 |
from test.support import ( |
| 35 |
is_apple, import_helper, os_helper, requires_subprocess, threading_helper |
| 36 |
) |
| 37 |
|
| 38 |
try: |
| 39 |
import ssl |
| 40 |
except ImportError: |
| 41 |
ssl = None |
| 42 |
|
| 43 |
support.requires_working_socket(module=True) |
| 44 |
|
| 45 |
class NoLogRequestHandler: |
| 46 |
def log_message(self, *args): |
| 47 |
# don't write log messages to stderr |
| 48 |
pass |
| 49 |
|
| 50 |
def read(self, n=None): |
| 51 |
return '' |
| 52 |
|
| 53 |
|
| 54 |
class DummyRequestHandler(NoLogRequestHandler, SimpleHTTPRequestHandler): |
| 55 |
pass |
| 56 |
|
| 57 |
|
| 58 |
def create_https_server( |
| 59 |
certfile, |
| 60 |
keyfile=None, |
| 61 |
password=None, |
| 62 |
*, |
| 63 |
address=('localhost', 0), |
| 64 |
request_handler=DummyRequestHandler, |
| 65 |
): |
| 66 |
return HTTPSServer( |
| 67 |
address, request_handler, |
| 68 |
certfile=certfile, keyfile=keyfile, password=password |
| 69 |
) |
| 70 |
|
| 71 |
|
| 72 |
class TestSSLDisabled(unittest.TestCase): |
| 73 |
def test_https_server_raises_runtime_error(self): |
| 74 |
with import_helper.isolated_modules(): |
| 75 |
sys.modules['ssl'] = None |
| 76 |
certfile = certdata_file("keycert.pem") |
| 77 |
with self.assertRaises(RuntimeError): |
| 78 |
create_https_server(certfile) |
| 79 |
|
| 80 |
|
| 81 |
class TestServerThread(threading.Thread): |
| 82 |
def __init__(self, test_object, request_handler, tls=None): |
| 83 |
threading.Thread.__init__(self) |
| 84 |
self.request_handler = request_handler |
| 85 |
self.test_object = test_object |
| 86 |
self.tls = tls |
| 87 |
|
| 88 |
def run(self): |
| 89 |
if self.tls: |
| 90 |
certfile, keyfile, password = self.tls |
| 91 |
self.server = create_https_server( |
| 92 |
certfile, keyfile, password, |
| 93 |
request_handler=self.request_handler, |
| 94 |
) |
| 95 |
else: |
| 96 |
self.server = HTTPServer(('localhost', 0), self.request_handler) |
| 97 |
self.test_object.HOST, self.test_object.PORT = self.server.socket.getsockname() |
| 98 |
self.test_object.server_started.set() |
| 99 |
self.test_object = None |
| 100 |
try: |
| 101 |
self.server.serve_forever(0.05) |
| 102 |
finally: |
| 103 |
self.server.server_close() |
| 104 |
|
| 105 |
def stop(self): |
| 106 |
self.server.shutdown() |
| 107 |
self.join() |
| 108 |
|
| 109 |
|
| 110 |
class BaseTestCase(unittest.TestCase): |
| 111 |
|
| 112 |
# Optional tuple (certfile, keyfile, password) to use for HTTPS servers. |
| 113 |
tls = None |
| 114 |
|
| 115 |
def setUp(self): |
| 116 |
self._threads = threading_helper.threading_setup() |
| 117 |
os.environ = os_helper.EnvironmentVarGuard() |
| 118 |
self.server_started = threading.Event() |
| 119 |
self.thread = TestServerThread(self, self.request_handler, self.tls) |
| 120 |
self.thread.start() |
| 121 |
self.server_started.wait() |
| 122 |
|
| 123 |
def tearDown(self): |
| 124 |
self.thread.stop() |
| 125 |
self.thread = None |
| 126 |
os.environ.__exit__() |
| 127 |
threading_helper.threading_cleanup(*self._threads) |
| 128 |
|
| 129 |
def request(self, uri, method='GET', body=None, headers={}): |
| 130 |
self.connection = http.client.HTTPConnection(self.HOST, self.PORT) |
| 131 |
self.connection.request(method, uri, body, headers) |
| 132 |
return self.connection.getresponse() |
| 133 |
|
| 134 |
|
| 135 |
class BaseHTTPServerTestCase(BaseTestCase): |
| 136 |
class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler): |
| 137 |
protocol_version = 'HTTP/1.1' |
| 138 |
default_request_version = 'HTTP/1.1' |
| 139 |
|
| 140 |
def do_TEST(self): |
| 141 |
self.send_response(HTTPStatus.NO_CONTENT) |
| 142 |
self.send_header('Content-Type', 'text/html') |
| 143 |
self.send_header('Connection', 'close') |
| 144 |
self.end_headers() |
| 145 |
|
| 146 |
def do_KEEP(self): |
| 147 |
self.send_response(HTTPStatus.NO_CONTENT) |
| 148 |
self.send_header('Content-Type', 'text/html') |
| 149 |
self.send_header('Connection', 'keep-alive') |
| 150 |
self.end_headers() |
| 151 |
|
| 152 |
def do_KEYERROR(self): |
| 153 |
self.send_error(999) |
| 154 |
|
| 155 |
def do_NOTFOUND(self): |
| 156 |
self.send_error(HTTPStatus.NOT_FOUND) |
| 157 |
|
| 158 |
def do_EXPLAINERROR(self): |
| 159 |
self.send_error(999, "Short Message", |
| 160 |
"This is a long \n explanation") |
| 161 |
|
| 162 |
def do_CUSTOM(self): |
| 163 |
self.send_response(999) |
| 164 |
self.send_header('Content-Type', 'text/html') |
| 165 |
self.send_header('Connection', 'close') |
| 166 |
self.end_headers() |
| 167 |
|
| 168 |
def do_LATINONEHEADER(self): |
| 169 |
self.send_response(999) |
| 170 |
self.send_header('X-Special', 'Dängerous Mind') |
| 171 |
self.send_header('Connection', 'close') |
| 172 |
self.end_headers() |
| 173 |
body = self.headers['x-special-incoming'].encode('utf-8') |
| 174 |
self.wfile.write(body) |
| 175 |
|
| 176 |
def do_SEND_ERROR(self): |
| 177 |
self.send_error(int(self.path[1:])) |
| 178 |
|
| 179 |
def do_HEAD(self): |
| 180 |
self.send_error(int(self.path[1:])) |
| 181 |
|
| 182 |
def setUp(self): |
| 183 |
BaseTestCase.setUp(self) |
| 184 |
self.con = http.client.HTTPConnection(self.HOST, self.PORT) |
| 185 |
self.con.connect() |
| 186 |
|
| 187 |
def test_command(self): |
| 188 |
self.con.request('GET', '/') |
| 189 |
res = self.con.getresponse() |
| 190 |
self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
| 191 |
|
| 192 |
def test_request_line_trimming(self): |
| 193 |
self.con._http_vsn_str = 'HTTP/1.1\n' |
| 194 |
self.con.putrequest('XYZBOGUS', '/') |
| 195 |
self.con.endheaders() |
| 196 |
res = self.con.getresponse() |
| 197 |
self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
| 198 |
|
| 199 |
def test_version_bogus(self): |
| 200 |
self.con._http_vsn_str = 'FUBAR' |
| 201 |
self.con.putrequest('GET', '/') |
| 202 |
self.con.endheaders() |
| 203 |
res = self.con.getresponse() |
| 204 |
self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
| 205 |
|
| 206 |
def test_version_digits(self): |
| 207 |
self.con._http_vsn_str = 'HTTP/9.9.9' |
| 208 |
self.con.putrequest('GET', '/') |
| 209 |
self.con.endheaders() |
| 210 |
res = self.con.getresponse() |
| 211 |
self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
| 212 |
|
| 213 |
def test_version_signs_and_underscores(self): |
| 214 |
self.con._http_vsn_str = 'HTTP/-9_9_9.+9_9_9' |
| 215 |
self.con.putrequest('GET', '/') |
| 216 |
self.con.endheaders() |
| 217 |
res = self.con.getresponse() |
| 218 |
self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
| 219 |
|
| 220 |
def test_major_version_number_too_long(self): |
| 221 |
self.con._http_vsn_str = 'HTTP/909876543210.0' |
| 222 |
self.con.putrequest('GET', '/') |
| 223 |
self.con.endheaders() |
| 224 |
res = self.con.getresponse() |
| 225 |
self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
| 226 |
|
| 227 |
def test_minor_version_number_too_long(self): |
| 228 |
self.con._http_vsn_str = 'HTTP/1.909876543210' |
| 229 |
self.con.putrequest('GET', '/') |
| 230 |
self.con.endheaders() |
| 231 |
res = self.con.getresponse() |
| 232 |
self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
| 233 |
|
| 234 |
def test_version_none_get(self): |
| 235 |
self.con._http_vsn_str = '' |
| 236 |
self.con.putrequest('GET', '/') |
| 237 |
self.con.endheaders() |
| 238 |
res = self.con.getresponse() |
| 239 |
self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
| 240 |
|
| 241 |
def test_version_none(self): |
| 242 |
# Test that a valid method is rejected when not HTTP/1.x |
| 243 |
self.con._http_vsn_str = '' |
| 244 |
self.con.putrequest('CUSTOM', '/') |
| 245 |
self.con.endheaders() |
| 246 |
res = self.con.getresponse() |
| 247 |
self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
| 248 |
|
| 249 |
def test_version_invalid(self): |
| 250 |
self.con._http_vsn = 99 |
| 251 |
self.con._http_vsn_str = 'HTTP/9.9' |
| 252 |
self.con.putrequest('GET', '/') |
| 253 |
self.con.endheaders() |
| 254 |
res = self.con.getresponse() |
| 255 |
self.assertEqual(res.status, HTTPStatus.HTTP_VERSION_NOT_SUPPORTED) |
| 256 |
|
| 257 |
def test_send_blank(self): |
| 258 |
self.con._http_vsn_str = '' |
| 259 |
self.con.putrequest('', '') |
| 260 |
self.con.endheaders() |
| 261 |
res = self.con.getresponse() |
| 262 |
self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
| 263 |
|
| 264 |
def test_header_close(self): |
| 265 |
self.con.putrequest('GET', '/') |
| 266 |
self.con.putheader('Connection', 'close') |
| 267 |
self.con.endheaders() |
| 268 |
res = self.con.getresponse() |
| 269 |
self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
| 270 |
|
| 271 |
def test_header_keep_alive(self): |
| 272 |
self.con._http_vsn_str = 'HTTP/1.1' |
| 273 |
self.con.putrequest('GET', '/') |
| 274 |
self.con.putheader('Connection', 'keep-alive') |
| 275 |
self.con.endheaders() |
| 276 |
res = self.con.getresponse() |
| 277 |
self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
| 278 |
|
| 279 |
def test_handler(self): |
| 280 |
self.con.request('TEST', '/') |
| 281 |
res = self.con.getresponse() |
| 282 |
self.assertEqual(res.status, HTTPStatus.NO_CONTENT) |
| 283 |
|
| 284 |
def test_return_header_keep_alive(self): |
| 285 |
self.con.request('KEEP', '/') |
| 286 |
res = self.con.getresponse() |
| 287 |
self.assertEqual(res.getheader('Connection'), 'keep-alive') |
| 288 |
self.con.request('TEST', '/') |
| 289 |
self.addCleanup(self.con.close) |
| 290 |
|
| 291 |
def test_internal_key_error(self): |
| 292 |
self.con.request('KEYERROR', '/') |
| 293 |
res = self.con.getresponse() |
| 294 |
self.assertEqual(res.status, 999) |
| 295 |
|
| 296 |
def test_return_custom_status(self): |
| 297 |
self.con.request('CUSTOM', '/') |
| 298 |
res = self.con.getresponse() |
| 299 |
self.assertEqual(res.status, 999) |
| 300 |
|
| 301 |
def test_return_explain_error(self): |
| 302 |
self.con.request('EXPLAINERROR', '/') |
| 303 |
res = self.con.getresponse() |
| 304 |
self.assertEqual(res.status, 999) |
| 305 |
self.assertTrue(int(res.getheader('Content-Length'))) |
| 306 |
|
| 307 |
def test_latin1_header(self): |
| 308 |
self.con.request('LATINONEHEADER', '/', headers={ |
| 309 |
'X-Special-Incoming': 'Ärger mit Unicode' |
| 310 |
}) |
| 311 |
res = self.con.getresponse() |
| 312 |
self.assertEqual(res.getheader('X-Special'), 'Dängerous Mind') |
| 313 |
self.assertEqual(res.read(), 'Ärger mit Unicode'.encode('utf-8')) |
| 314 |
|
| 315 |
def test_error_content_length(self): |
| 316 |
# Issue #16088: standard error responses should have a content-length |
| 317 |
self.con.request('NOTFOUND', '/') |
| 318 |
res = self.con.getresponse() |
| 319 |
self.assertEqual(res.status, HTTPStatus.NOT_FOUND) |
| 320 |
|
| 321 |
data = res.read() |
| 322 |
self.assertEqual(int(res.getheader('Content-Length')), len(data)) |
| 323 |
|
| 324 |
def test_send_error(self): |
| 325 |
allow_transfer_encoding_codes = (HTTPStatus.NOT_MODIFIED, |
| 326 |
HTTPStatus.RESET_CONTENT) |
| 327 |
for code in (HTTPStatus.NO_CONTENT, HTTPStatus.NOT_MODIFIED, |
| 328 |
HTTPStatus.PROCESSING, HTTPStatus.RESET_CONTENT, |
| 329 |
HTTPStatus.SWITCHING_PROTOCOLS): |
| 330 |
self.con.request('SEND_ERROR', '/{}'.format(code)) |
| 331 |
res = self.con.getresponse() |
| 332 |
self.assertEqual(code, res.status) |
| 333 |
self.assertEqual(None, res.getheader('Content-Length')) |
| 334 |
self.assertEqual(None, res.getheader('Content-Type')) |
| 335 |
if code not in allow_transfer_encoding_codes: |
| 336 |
self.assertEqual(None, res.getheader('Transfer-Encoding')) |
| 337 |
|
| 338 |
data = res.read() |
| 339 |
self.assertEqual(b'', data) |
| 340 |
|
| 341 |
def test_head_via_send_error(self): |
| 342 |
allow_transfer_encoding_codes = (HTTPStatus.NOT_MODIFIED, |
| 343 |
HTTPStatus.RESET_CONTENT) |
| 344 |
for code in (HTTPStatus.OK, HTTPStatus.NO_CONTENT, |
| 345 |
HTTPStatus.NOT_MODIFIED, HTTPStatus.RESET_CONTENT, |
| 346 |
HTTPStatus.SWITCHING_PROTOCOLS): |
| 347 |
self.con.request('HEAD', '/{}'.format(code)) |
| 348 |
res = self.con.getresponse() |
| 349 |
self.assertEqual(code, res.status) |
| 350 |
if code == HTTPStatus.OK: |
| 351 |
self.assertTrue(int(res.getheader('Content-Length')) > 0) |
| 352 |
self.assertIn('text/html', res.getheader('Content-Type')) |
| 353 |
else: |
| 354 |
self.assertEqual(None, res.getheader('Content-Length')) |
| 355 |
self.assertEqual(None, res.getheader('Content-Type')) |
| 356 |
if code not in allow_transfer_encoding_codes: |
| 357 |
self.assertEqual(None, res.getheader('Transfer-Encoding')) |
| 358 |
|
| 359 |
data = res.read() |
| 360 |
self.assertEqual(b'', data) |
| 361 |
|
| 362 |
|
| 363 |
class HTTP09ServerTestCase(BaseTestCase): |
| 364 |
|
| 365 |
class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler): |
| 366 |
"""Request handler for HTTP/0.9 server.""" |
| 367 |
|
| 368 |
def do_GET(self): |
| 369 |
self.wfile.write(f'OK: here is {self.path}\r\n'.encode()) |
| 370 |
|
| 371 |
def setUp(self): |
| 372 |
super().setUp() |
| 373 |
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 374 |
self.sock = self.enterContext(self.sock) |
| 375 |
self.sock.connect((self.HOST, self.PORT)) |
| 376 |
|
| 377 |
def test_simple_get(self): |
| 378 |
self.sock.send(b'GET /index.html\r\n') |
| 379 |
res = self.sock.recv(1024) |
| 380 |
self.assertEqual(res, b"OK: here is /index.html\r\n") |
| 381 |
|
| 382 |
def test_invalid_request(self): |
| 383 |
self.sock.send(b'POST /index.html\r\n') |
| 384 |
res = self.sock.recv(1024) |
| 385 |
self.assertIn(b"Bad HTTP/0.9 request type ('POST')", res) |
| 386 |
|
| 387 |
def test_single_request(self): |
| 388 |
self.sock.send(b'GET /foo.html\r\n') |
| 389 |
res = self.sock.recv(1024) |
| 390 |
self.assertEqual(res, b"OK: here is /foo.html\r\n") |
| 391 |
|
| 392 |
# Ignore errors if the connection is already closed, |
| 393 |
# as this is the expected behavior of HTTP/0.9. |
| 394 |
with contextlib.suppress(OSError): |
| 395 |
self.sock.send(b'GET /bar.html\r\n') |
| 396 |
res = self.sock.recv(1024) |
| 397 |
# The server should not process our request. |
| 398 |
self.assertEqual(res, b'') |
| 399 |
|
| 400 |
|
| 401 |
def certdata_file(*path): |
| 402 |
return os.path.join(os.path.dirname(__file__), "certdata", *path) |
| 403 |
|
| 404 |
|
| 405 |
@unittest.skipIf(ssl is None, "requires ssl") |
| 406 |
class BaseHTTPSServerTestCase(BaseTestCase): |
| 407 |
CERTFILE = certdata_file("keycert.pem") |
| 408 |
ONLYCERT = certdata_file("ssl_cert.pem") |
| 409 |
ONLYKEY = certdata_file("ssl_key.pem") |
| 410 |
CERTFILE_PROTECTED = certdata_file("keycert.passwd.pem") |
| 411 |
ONLYKEY_PROTECTED = certdata_file("ssl_key.passwd.pem") |
| 412 |
EMPTYCERT = certdata_file("nullcert.pem") |
| 413 |
BADCERT = certdata_file("badcert.pem") |
| 414 |
KEY_PASSWORD = "somepass" |
| 415 |
BADPASSWORD = "badpass" |
| 416 |
|
| 417 |
tls = (ONLYCERT, ONLYKEY, None) # values by default |
| 418 |
|
| 419 |
request_handler = DummyRequestHandler |
| 420 |
|
| 421 |
def test_get(self): |
| 422 |
response = self.request('/') |
| 423 |
self.assertEqual(response.status, HTTPStatus.OK) |
| 424 |
|
| 425 |
def request(self, uri, method='GET', body=None, headers={}): |
| 426 |
context = ssl._create_unverified_context() |
| 427 |
self.connection = http.client.HTTPSConnection( |
| 428 |
self.HOST, self.PORT, context=context |
| 429 |
) |
| 430 |
self.connection.request(method, uri, body, headers) |
| 431 |
return self.connection.getresponse() |
| 432 |
|
| 433 |
def test_valid_certdata(self): |
| 434 |
valid_certdata= [ |
| 435 |
(self.CERTFILE, None, None), |
| 436 |
(self.CERTFILE, self.CERTFILE, None), |
| 437 |
(self.CERTFILE_PROTECTED, None, self.KEY_PASSWORD), |
| 438 |
(self.ONLYCERT, self.ONLYKEY_PROTECTED, self.KEY_PASSWORD), |
| 439 |
] |
| 440 |
for certfile, keyfile, password in valid_certdata: |
| 441 |
with self.subTest( |
| 442 |
certfile=certfile, keyfile=keyfile, password=password |
| 443 |
): |
| 444 |
server = create_https_server(certfile, keyfile, password) |
| 445 |
self.assertIsInstance(server, HTTPSServer) |
| 446 |
server.server_close() |
| 447 |
|
| 448 |
def test_invalid_certdata(self): |
| 449 |
invalid_certdata = [ |
| 450 |
(self.BADCERT, None, None), |
| 451 |
(self.EMPTYCERT, None, None), |
| 452 |
(self.ONLYCERT, None, None), |
| 453 |
(self.ONLYKEY, None, None), |
| 454 |
(self.ONLYKEY, self.ONLYCERT, None), |
| 455 |
(self.CERTFILE_PROTECTED, None, self.BADPASSWORD), |
| 456 |
# TODO: test the next case and add same case to test_ssl (We |
| 457 |
# specify a cert and a password-protected file, but no password): |
| 458 |
# (self.CERTFILE_PROTECTED, None, None), |
| 459 |
# see issue #132102 |
| 460 |
] |
| 461 |
for certfile, keyfile, password in invalid_certdata: |
| 462 |
with self.subTest( |
| 463 |
certfile=certfile, keyfile=keyfile, password=password |
| 464 |
): |
| 465 |
with self.assertRaises(ssl.SSLError): |
| 466 |
create_https_server(certfile, keyfile, password) |
| 467 |
|
| 468 |
|
| 469 |
class RequestHandlerLoggingTestCase(BaseTestCase): |
| 470 |
class request_handler(BaseHTTPRequestHandler): |
| 471 |
protocol_version = 'HTTP/1.1' |
| 472 |
default_request_version = 'HTTP/1.1' |
| 473 |
|
| 474 |
def do_GET(self): |
| 475 |
self.send_response(HTTPStatus.OK) |
| 476 |
self.end_headers() |
| 477 |
|
| 478 |
def do_ERROR(self): |
| 479 |
self.send_error(HTTPStatus.NOT_FOUND, 'File not found') |
| 480 |
|
| 481 |
def test_get(self): |
| 482 |
self.con = http.client.HTTPConnection(self.HOST, self.PORT) |
| 483 |
self.con.connect() |
| 484 |
|
| 485 |
with support.captured_stderr() as err: |
| 486 |
self.con.request('GET', '/') |
| 487 |
self.con.getresponse() |
| 488 |
|
| 489 |
self.assertEndsWith(err.getvalue(), '"GET / HTTP/1.1" 200 -\n') |
| 490 |
|
| 491 |
def test_err(self): |
| 492 |
self.con = http.client.HTTPConnection(self.HOST, self.PORT) |
| 493 |
self.con.connect() |
| 494 |
|
| 495 |
with support.captured_stderr() as err: |
| 496 |
self.con.request('ERROR', '/') |
| 497 |
self.con.getresponse() |
| 498 |
|
| 499 |
lines = err.getvalue().split('\n') |
| 500 |
self.assertEndsWith(lines[0], 'code 404, message File not found') |
| 501 |
self.assertEndsWith(lines[1], '"ERROR / HTTP/1.1" 404 -') |
| 502 |
|
| 503 |
|
| 504 |
class SimpleHTTPServerTestCase(BaseTestCase): |
| 505 |
class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler): |
| 506 |
pass |
| 507 |
|
| 508 |
def setUp(self): |
| 509 |
super().setUp() |
| 510 |
self.cwd = os.getcwd() |
| 511 |
basetempdir = tempfile.gettempdir() |
| 512 |
os.chdir(basetempdir) |
| 513 |
self.data = b'We are the knights who say Ni!' |
| 514 |
self.tempdir = tempfile.mkdtemp(dir=basetempdir) |
| 515 |
self.tempdir_name = os.path.basename(self.tempdir) |
| 516 |
self.base_url = '/' + self.tempdir_name |
| 517 |
tempname = os.path.join(self.tempdir, 'test') |
| 518 |
with open(tempname, 'wb') as temp: |
| 519 |
temp.write(self.data) |
| 520 |
temp.flush() |
| 521 |
mtime = os.stat(tempname).st_mtime |
| 522 |
# compute last modification datetime for browser cache tests |
| 523 |
last_modif = datetime.datetime.fromtimestamp(mtime, |
| 524 |
datetime.timezone.utc) |
| 525 |
self.last_modif_datetime = last_modif.replace(microsecond=0) |
| 526 |
self.last_modif_header = email.utils.formatdate( |
| 527 |
last_modif.timestamp(), usegmt=True) |
| 528 |
|
| 529 |
def tearDown(self): |
| 530 |
try: |
| 531 |
os.chdir(self.cwd) |
| 532 |
try: |
| 533 |
shutil.rmtree(self.tempdir) |
| 534 |
except: |
| 535 |
pass |
| 536 |
finally: |
| 537 |
super().tearDown() |
| 538 |
|
| 539 |
def check_status_and_reason(self, response, status, data=None): |
| 540 |
def close_conn(): |
| 541 |
"""Don't close reader yet so we can check if there was leftover |
| 542 |
buffered input""" |
| 543 |
nonlocal reader |
| 544 |
reader = response.fp |
| 545 |
response.fp = None |
| 546 |
reader = None |
| 547 |
response._close_conn = close_conn |
| 548 |
|
| 549 |
body = response.read() |
| 550 |
self.assertTrue(response) |
| 551 |
self.assertEqual(response.status, status) |
| 552 |
self.assertIsNotNone(response.reason) |
| 553 |
if data: |
| 554 |
self.assertEqual(data, body) |
| 555 |
# Ensure the server has not set up a persistent connection, and has |
| 556 |
# not sent any extra data |
| 557 |
self.assertEqual(response.version, 10) |
| 558 |
self.assertEqual(response.msg.get("Connection", "close"), "close") |
| 559 |
self.assertEqual(reader.read(30), b'', 'Connection should be closed') |
| 560 |
|
| 561 |
reader.close() |
| 562 |
return body |
| 563 |
|
| 564 |
def check_list_dir_dirname(self, dirname, quotedname=None): |
| 565 |
fullpath = os.path.join(self.tempdir, dirname) |
| 566 |
try: |
| 567 |
os.mkdir(os.path.join(self.tempdir, dirname)) |
| 568 |
except (OSError, UnicodeEncodeError): |
| 569 |
self.skipTest(f'Can not create directory {dirname!a} ' |
| 570 |
f'on current file system') |
| 571 |
|
| 572 |
if quotedname is None: |
| 573 |
quotedname = urllib.parse.quote(dirname, errors='surrogatepass') |
| 574 |
response = self.request(self.base_url + '/' + quotedname + '/') |
| 575 |
body = self.check_status_and_reason(response, HTTPStatus.OK) |
| 576 |
displaypath = html.escape(f'{self.base_url}/{dirname}/', quote=False) |
| 577 |
enc = sys.getfilesystemencoding() |
| 578 |
prefix = f'listing for {displaypath}</'.encode(enc, 'surrogateescape') |
| 579 |
self.assertIn(prefix + b'title>', body) |
| 580 |
self.assertIn(prefix + b'h1>', body) |
| 581 |
|
| 582 |
def check_list_dir_filename(self, filename): |
| 583 |
fullpath = os.path.join(self.tempdir, filename) |
| 584 |
content = ascii(fullpath).encode() + (os_helper.TESTFN_UNDECODABLE or b'\xff') |
| 585 |
try: |
| 586 |
with open(fullpath, 'wb') as f: |
| 587 |
f.write(content) |
| 588 |
except OSError: |
| 589 |
self.skipTest(f'Can not create file {filename!a} ' |
| 590 |
f'on current file system') |
| 591 |
|
| 592 |
response = self.request(self.base_url + '/') |
| 593 |
body = self.check_status_and_reason(response, HTTPStatus.OK) |
| 594 |
quotedname = urllib.parse.quote(filename, errors='surrogatepass') |
| 595 |
enc = response.headers.get_content_charset() |
| 596 |
self.assertIsNotNone(enc) |
| 597 |
self.assertIn((f'href="{quotedname}"').encode('ascii'), body) |
| 598 |
displayname = html.escape(filename, quote=False) |
| 599 |
self.assertIn(f'>{displayname}<'.encode(enc, 'surrogateescape'), body) |
| 600 |
|
| 601 |
response = self.request(self.base_url + '/' + quotedname) |
| 602 |
self.check_status_and_reason(response, HTTPStatus.OK, data=content) |
| 603 |
|
| 604 |
@unittest.skipUnless(os_helper.TESTFN_NONASCII, |
| 605 |
'need os_helper.TESTFN_NONASCII') |
| 606 |
def test_list_dir_nonascii_dirname(self): |
| 607 |
dirname = os_helper.TESTFN_NONASCII + '.dir' |
| 608 |
self.check_list_dir_dirname(dirname) |
| 609 |
|
| 610 |
@unittest.skipUnless(os_helper.TESTFN_NONASCII, |
| 611 |
'need os_helper.TESTFN_NONASCII') |
| 612 |
def test_list_dir_nonascii_filename(self): |
| 613 |
filename = os_helper.TESTFN_NONASCII + '.txt' |
| 614 |
self.check_list_dir_filename(filename) |
| 615 |
|
| 616 |
@unittest.skipIf(is_apple, |
| 617 |
'undecodable name cannot always be decoded on Apple platforms') |
| 618 |
@unittest.skipIf(sys.platform == 'win32', |
| 619 |
'undecodable name cannot be decoded on win32') |
| 620 |
@unittest.skipUnless(os_helper.TESTFN_UNDECODABLE, |
| 621 |
'need os_helper.TESTFN_UNDECODABLE') |
| 622 |
def test_list_dir_undecodable_dirname(self): |
| 623 |
dirname = os.fsdecode(os_helper.TESTFN_UNDECODABLE) + '.dir' |
| 624 |
self.check_list_dir_dirname(dirname) |
| 625 |
|
| 626 |
@unittest.skipIf(is_apple, |
| 627 |
'undecodable name cannot always be decoded on Apple platforms') |
| 628 |
@unittest.skipIf(sys.platform == 'win32', |
| 629 |
'undecodable name cannot be decoded on win32') |
| 630 |
@unittest.skipUnless(os_helper.TESTFN_UNDECODABLE, |
| 631 |
'need os_helper.TESTFN_UNDECODABLE') |
| 632 |
def test_list_dir_undecodable_filename(self): |
| 633 |
filename = os.fsdecode(os_helper.TESTFN_UNDECODABLE) + '.txt' |
| 634 |
self.check_list_dir_filename(filename) |
| 635 |
|
| 636 |
def test_list_dir_undecodable_dirname2(self): |
| 637 |
dirname = '\ufffd.dir' |
| 638 |
self.check_list_dir_dirname(dirname, quotedname='%ff.dir') |
| 639 |
|
| 640 |
@unittest.skipUnless(os_helper.TESTFN_UNENCODABLE, |
| 641 |
'need os_helper.TESTFN_UNENCODABLE') |
| 642 |
def test_list_dir_unencodable_dirname(self): |
| 643 |
dirname = os_helper.TESTFN_UNENCODABLE + '.dir' |
| 644 |
self.check_list_dir_dirname(dirname) |
| 645 |
|
| 646 |
@unittest.skipUnless(os_helper.TESTFN_UNENCODABLE, |
| 647 |
'need os_helper.TESTFN_UNENCODABLE') |
| 648 |
def test_list_dir_unencodable_filename(self): |
| 649 |
filename = os_helper.TESTFN_UNENCODABLE + '.txt' |
| 650 |
self.check_list_dir_filename(filename) |
| 651 |
|
| 652 |
def test_list_dir_escape_dirname(self): |
| 653 |
# Characters that need special treating in URL or HTML. |
| 654 |
for name in ('q?', 'f#', '&', '&', '<i>', '"dq"', "'sq'", |
| 655 |
'%A4', '%E2%82%AC'): |
| 656 |
with self.subTest(name=name): |
| 657 |
dirname = name + '.dir' |
| 658 |
self.check_list_dir_dirname(dirname, |
| 659 |
quotedname=urllib.parse.quote(dirname, safe='&<>\'"')) |
| 660 |
|
| 661 |
def test_list_dir_escape_filename(self): |
| 662 |
# Characters that need special treating in URL or HTML. |
| 663 |
for name in ('q?', 'f#', '&', '&', '<i>', '"dq"', "'sq'", |
| 664 |
'%A4', '%E2%82%AC'): |
| 665 |
with self.subTest(name=name): |
| 666 |
filename = name + '.txt' |
| 667 |
self.check_list_dir_filename(filename) |
| 668 |
os_helper.unlink(os.path.join(self.tempdir, filename)) |
| 669 |
|
| 670 |
def test_list_dir_with_query_and_fragment(self): |
| 671 |
prefix = f'listing for {self.base_url}/</'.encode('latin1') |
| 672 |
response = self.request(self.base_url + '/#123').read() |
| 673 |
self.assertIn(prefix + b'title>', response) |
| 674 |
self.assertIn(prefix + b'h1>', response) |
| 675 |
response = self.request(self.base_url + '/?x=123').read() |
| 676 |
self.assertIn(prefix + b'title>', response) |
| 677 |
self.assertIn(prefix + b'h1>', response) |
| 678 |
|
| 679 |
def test_get_dir_redirect_location_domain_injection_bug(self): |
| 680 |
"""Ensure //evil.co/..%2f../../X does not put //evil.co/ in Location. |
| 681 |
|
| 682 |
//netloc/ in a Location header is a redirect to a new host. |
| 683 |
https://github.com/python/cpython/issues/87389 |
| 684 |
|
| 685 |
This checks that a path resolving to a directory on our server cannot |
| 686 |
resolve into a redirect to another server. |
| 687 |
""" |
| 688 |
os.mkdir(os.path.join(self.tempdir, 'existing_directory')) |
| 689 |
url = f'/python.org/..%2f..%2f..%2f..%2f..%2f../%0a%0d/../{self.tempdir_name}/existing_directory' |
| 690 |
expected_location = f'{url}/' # /python.org.../ single slash single prefix, trailing slash |
| 691 |
# Canonicalizes to /tmp/tempdir_name/existing_directory which does |
| 692 |
# exist and is a dir, triggering the 301 redirect logic. |
| 693 |
response = self.request(url) |
| 694 |
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY) |
| 695 |
location = response.getheader('Location') |
| 696 |
self.assertEqual(location, expected_location, msg='non-attack failed!') |
| 697 |
|
| 698 |
# //python.org... multi-slash prefix, no trailing slash |
| 699 |
attack_url = f'/{url}' |
| 700 |
response = self.request(attack_url) |
| 701 |
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY) |
| 702 |
location = response.getheader('Location') |
| 703 |
self.assertNotStartsWith(location, '//') |
| 704 |
self.assertEqual(location, expected_location, |
| 705 |
msg='Expected Location header to start with a single / and ' |
| 706 |
'end with a / as this is a directory redirect.') |
| 707 |
|
| 708 |
# ///python.org... triple-slash prefix, no trailing slash |
| 709 |
attack3_url = f'//{url}' |
| 710 |
response = self.request(attack3_url) |
| 711 |
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY) |
| 712 |
self.assertEqual(response.getheader('Location'), expected_location) |
| 713 |
|
| 714 |
# If the second word in the http request (Request-URI for the http |
| 715 |
# method) is a full URI, we don't worry about it, as that'll be parsed |
| 716 |
# and reassembled as a full URI within BaseHTTPRequestHandler.send_head |
| 717 |
# so no errant scheme-less //netloc//evil.co/ domain mixup can happen. |
| 718 |
attack_scheme_netloc_2slash_url = f'https://pypi.org/{url}' |
| 719 |
expected_scheme_netloc_location = f'{attack_scheme_netloc_2slash_url}/' |
| 720 |
response = self.request(attack_scheme_netloc_2slash_url) |
| 721 |
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY) |
| 722 |
location = response.getheader('Location') |
| 723 |
# We're just ensuring that the scheme and domain make it through, if |
| 724 |
# there are or aren't multiple slashes at the start of the path that |
| 725 |
# follows that isn't important in this Location: header. |
| 726 |
self.assertStartsWith(location, 'https://pypi.org/') |
| 727 |
|
| 728 |
def test_get(self): |
| 729 |
#constructs the path relative to the root directory of the HTTPServer |
| 730 |
response = self.request(self.base_url + '/test') |
| 731 |
self.check_status_and_reason(response, HTTPStatus.OK, data=self.data) |
| 732 |
# check for trailing "/" which should return 404. See Issue17324 |
| 733 |
response = self.request(self.base_url + '/test/') |
| 734 |
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
| 735 |
response = self.request(self.base_url + '/test%2f') |
| 736 |
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
| 737 |
response = self.request(self.base_url + '/test%2F') |
| 738 |
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
| 739 |
response = self.request(self.base_url + '/') |
| 740 |
self.check_status_and_reason(response, HTTPStatus.OK) |
| 741 |
response = self.request(self.base_url + '%2f') |
| 742 |
self.check_status_and_reason(response, HTTPStatus.OK) |
| 743 |
response = self.request(self.base_url + '%2F') |
| 744 |
self.check_status_and_reason(response, HTTPStatus.OK) |
| 745 |
response = self.request(self.base_url) |
| 746 |
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY) |
| 747 |
self.assertEqual(response.getheader("Location"), self.base_url + "/") |
| 748 |
self.assertEqual(response.getheader("Content-Length"), "0") |
| 749 |
response = self.request(self.base_url + '/?hi=2') |
| 750 |
self.check_status_and_reason(response, HTTPStatus.OK) |
| 751 |
response = self.request(self.base_url + '?hi=1') |
| 752 |
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY) |
| 753 |
self.assertEqual(response.getheader("Location"), |
| 754 |
self.base_url + "/?hi=1") |
| 755 |
response = self.request('/ThisDoesNotExist') |
| 756 |
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
| 757 |
response = self.request('/' + 'ThisDoesNotExist' + '/') |
| 758 |
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
| 759 |
os.makedirs(os.path.join(self.tempdir, 'spam', 'index.html')) |
| 760 |
response = self.request(self.base_url + '/spam/') |
| 761 |
self.check_status_and_reason(response, HTTPStatus.OK) |
| 762 |
|
| 763 |
data = b"Dummy index file\r\n" |
| 764 |
with open(os.path.join(self.tempdir_name, 'index.html'), 'wb') as f: |
| 765 |
f.write(data) |
| 766 |
response = self.request(self.base_url + '/') |
| 767 |
self.check_status_and_reason(response, HTTPStatus.OK, data) |
| 768 |
|
| 769 |
# chmod() doesn't work as expected on Windows, and filesystem |
| 770 |
# permissions are ignored by root on Unix. |
| 771 |
if os.name == 'posix' and os.geteuid() != 0: |
| 772 |
os.chmod(self.tempdir, 0) |
| 773 |
try: |
| 774 |
response = self.request(self.base_url + '/') |
| 775 |
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
| 776 |
finally: |
| 777 |
os.chmod(self.tempdir, 0o755) |
| 778 |
|
| 779 |
def test_head(self): |
| 780 |
response = self.request( |
| 781 |
self.base_url + '/test', method='HEAD') |
| 782 |
self.check_status_and_reason(response, HTTPStatus.OK) |
| 783 |
self.assertEqual(response.getheader('content-length'), |
| 784 |
str(len(self.data))) |
| 785 |
self.assertEqual(response.getheader('content-type'), |
| 786 |
'application/octet-stream') |
| 787 |
|
| 788 |
def test_browser_cache(self): |
| 789 |
"""Check that when a request to /test is sent with the request header |
| 790 |
If-Modified-Since set to date of last modification, the server returns |
| 791 |
status code 304, not 200 |
| 792 |
""" |
| 793 |
headers = email.message.Message() |
| 794 |
headers['If-Modified-Since'] = self.last_modif_header |
| 795 |
response = self.request(self.base_url + '/test', headers=headers) |
| 796 |
self.check_status_and_reason(response, HTTPStatus.NOT_MODIFIED) |
| 797 |
|
| 798 |
# one hour after last modification : must return 304 |
| 799 |
new_dt = self.last_modif_datetime + datetime.timedelta(hours=1) |
| 800 |
headers = email.message.Message() |
| 801 |
headers['If-Modified-Since'] = email.utils.format_datetime(new_dt, |
| 802 |
usegmt=True) |
| 803 |
response = self.request(self.base_url + '/test', headers=headers) |
| 804 |
self.check_status_and_reason(response, HTTPStatus.NOT_MODIFIED) |
| 805 |
|
| 806 |
def test_browser_cache_file_changed(self): |
| 807 |
# with If-Modified-Since earlier than Last-Modified, must return 200 |
| 808 |
dt = self.last_modif_datetime |
| 809 |
# build datetime object : 365 days before last modification |
| 810 |
old_dt = dt - datetime.timedelta(days=365) |
| 811 |
headers = email.message.Message() |
| 812 |
headers['If-Modified-Since'] = email.utils.format_datetime(old_dt, |
| 813 |
usegmt=True) |
| 814 |
response = self.request(self.base_url + '/test', headers=headers) |
| 815 |
self.check_status_and_reason(response, HTTPStatus.OK) |
| 816 |
|
| 817 |
def test_browser_cache_with_If_None_Match_header(self): |
| 818 |
# if If-None-Match header is present, ignore If-Modified-Since |
| 819 |
|
| 820 |
headers = email.message.Message() |
| 821 |
headers['If-Modified-Since'] = self.last_modif_header |
| 822 |
headers['If-None-Match'] = "*" |
| 823 |
response = self.request(self.base_url + '/test', headers=headers) |
| 824 |
self.check_status_and_reason(response, HTTPStatus.OK) |
| 825 |
|
| 826 |
def test_invalid_requests(self): |
| 827 |
response = self.request('/', method='FOO') |
| 828 |
self.check_status_and_reason(response, HTTPStatus.NOT_IMPLEMENTED) |
| 829 |
# requests must be case sensitive,so this should fail too |
| 830 |
response = self.request('/', method='custom') |
| 831 |
self.check_status_and_reason(response, HTTPStatus.NOT_IMPLEMENTED) |
| 832 |
response = self.request('/', method='GETs') |
| 833 |
self.check_status_and_reason(response, HTTPStatus.NOT_IMPLEMENTED) |
| 834 |
|
| 835 |
def test_last_modified(self): |
| 836 |
"""Checks that the datetime returned in Last-Modified response header |
| 837 |
is the actual datetime of last modification, rounded to the second |
| 838 |
""" |
| 839 |
response = self.request(self.base_url + '/test') |
| 840 |
self.check_status_and_reason(response, HTTPStatus.OK, data=self.data) |
| 841 |
last_modif_header = response.headers['Last-modified'] |
| 842 |
self.assertEqual(last_modif_header, self.last_modif_header) |
| 843 |
|
| 844 |
def test_path_without_leading_slash(self): |
| 845 |
response = self.request(self.tempdir_name + '/test') |
| 846 |
self.check_status_and_reason(response, HTTPStatus.OK, data=self.data) |
| 847 |
response = self.request(self.tempdir_name + '/test/') |
| 848 |
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
| 849 |
response = self.request(self.tempdir_name + '/') |
| 850 |
self.check_status_and_reason(response, HTTPStatus.OK) |
| 851 |
response = self.request(self.tempdir_name) |
| 852 |
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY) |
| 853 |
self.assertEqual(response.getheader("Location"), |
| 854 |
self.tempdir_name + "/") |
| 855 |
response = self.request(self.tempdir_name + '/?hi=2') |
| 856 |
self.check_status_and_reason(response, HTTPStatus.OK) |
| 857 |
response = self.request(self.tempdir_name + '?hi=1') |
| 858 |
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY) |
| 859 |
self.assertEqual(response.getheader("Location"), |
| 860 |
self.tempdir_name + "/?hi=1") |
| 861 |
|
| 862 |
|
| 863 |
cgi_file1 = """\ |
| 864 |
#!%s |
| 865 |
|
| 866 |
print("Content-type: text/html") |
| 867 |
print() |
| 868 |
print("Hello World") |
| 869 |
""" |
| 870 |
|
| 871 |
cgi_file2 = """\ |
| 872 |
#!%s |
| 873 |
import os |
| 874 |
import sys |
| 875 |
import urllib.parse |
| 876 |
|
| 877 |
print("Content-type: text/html") |
| 878 |
print() |
| 879 |
|
| 880 |
content_length = int(os.environ["CONTENT_LENGTH"]) |
| 881 |
query_string = sys.stdin.buffer.read(content_length) |
| 882 |
params = {key.decode("utf-8"): val.decode("utf-8") |
| 883 |
for key, val in urllib.parse.parse_qsl(query_string)} |
| 884 |
|
| 885 |
print("%%s, %%s, %%s" %% (params["spam"], params["eggs"], params["bacon"])) |
| 886 |
""" |
| 887 |
|
| 888 |
cgi_file4 = """\ |
| 889 |
#!%s |
| 890 |
import os |
| 891 |
|
| 892 |
print("Content-type: text/html") |
| 893 |
print() |
| 894 |
|
| 895 |
print(os.environ["%s"]) |
| 896 |
""" |
| 897 |
|
| 898 |
cgi_file6 = """\ |
| 899 |
#!%s |
| 900 |
import os |
| 901 |
|
| 902 |
print("X-ambv: was here") |
| 903 |
print("Content-type: text/html") |
| 904 |
print() |
| 905 |
print("<pre>") |
| 906 |
for k, v in os.environ.items(): |
| 907 |
try: |
| 908 |
k.encode('ascii') |
| 909 |
v.encode('ascii') |
| 910 |
except UnicodeEncodeError: |
| 911 |
continue # see: BPO-44647 |
| 912 |
print(f"{k}={v}") |
| 913 |
print("</pre>") |
| 914 |
""" |
| 915 |
|
| 916 |
cgi_file7 = """\ |
| 917 |
#!%s |
| 918 |
import os |
| 919 |
import sys |
| 920 |
|
| 921 |
print("Content-type: text/plain") |
| 922 |
print() |
| 923 |
|
| 924 |
content_length = int(os.environ["CONTENT_LENGTH"]) |
| 925 |
body = sys.stdin.buffer.read(content_length) |
| 926 |
|
| 927 |
print(f"{content_length} {len(body)}") |
| 928 |
""" |
| 929 |
|
| 930 |
|
| 931 |
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0, |
| 932 |
"This test can't be run reliably as root (issue #13308).") |
| 933 |
@requires_subprocess() |
| 934 |
class CGIHTTPServerTestCase(BaseTestCase): |
| 935 |
class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler): |
| 936 |
_test_case_self = None # populated by each setUp() method call. |
| 937 |
|
| 938 |
def __init__(self, *args, **kwargs): |
| 939 |
with self._test_case_self.assertWarnsRegex( |
| 940 |
DeprecationWarning, |
| 941 |
r'http\.server\.CGIHTTPRequestHandler'): |
| 942 |
# This context also happens to catch and silence the |
| 943 |
# threading DeprecationWarning from os.fork(). |
| 944 |
super().__init__(*args, **kwargs) |
| 945 |
|
| 946 |
linesep = os.linesep.encode('ascii') |
| 947 |
|
| 948 |
def setUp(self): |
| 949 |
self.request_handler._test_case_self = self # practical, but yuck. |
| 950 |
BaseTestCase.setUp(self) |
| 951 |
self.cwd = os.getcwd() |
| 952 |
self.parent_dir = tempfile.mkdtemp() |
| 953 |
self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin') |
| 954 |
self.cgi_child_dir = os.path.join(self.cgi_dir, 'child-dir') |
| 955 |
self.sub_dir_1 = os.path.join(self.parent_dir, 'sub') |
| 956 |
self.sub_dir_2 = os.path.join(self.sub_dir_1, 'dir') |
| 957 |
self.cgi_dir_in_sub_dir = os.path.join(self.sub_dir_2, 'cgi-bin') |
| 958 |
os.mkdir(self.cgi_dir) |
| 959 |
os.mkdir(self.cgi_child_dir) |
| 960 |
os.mkdir(self.sub_dir_1) |
| 961 |
os.mkdir(self.sub_dir_2) |
| 962 |
os.mkdir(self.cgi_dir_in_sub_dir) |
| 963 |
self.nocgi_path = None |
| 964 |
self.file1_path = None |
| 965 |
self.file2_path = None |
| 966 |
self.file3_path = None |
| 967 |
self.file4_path = None |
| 968 |
self.file5_path = None |
| 969 |
self.file6_path = None |
| 970 |
self.file7_path = None |
| 971 |
|
| 972 |
# The shebang line should be pure ASCII: use symlink if possible. |
| 973 |
# See issue #7668. |
| 974 |
self._pythonexe_symlink = None |
| 975 |
if os_helper.can_symlink(): |
| 976 |
self.pythonexe = os.path.join(self.parent_dir, 'python') |
| 977 |
self._pythonexe_symlink = support.PythonSymlink(self.pythonexe).__enter__() |
| 978 |
else: |
| 979 |
self.pythonexe = sys.executable |
| 980 |
|
| 981 |
try: |
| 982 |
# The python executable path is written as the first line of the |
| 983 |
# CGI Python script. The encoding cookie cannot be used, and so the |
| 984 |
# path should be encodable to the default script encoding (utf-8) |
| 985 |
self.pythonexe.encode('utf-8') |
| 986 |
except UnicodeEncodeError: |
| 987 |
self.tearDown() |
| 988 |
self.skipTest("Python executable path is not encodable to utf-8") |
| 989 |
|
| 990 |
self.nocgi_path = os.path.join(self.parent_dir, 'nocgi.py') |
| 991 |
with open(self.nocgi_path, 'w', encoding='utf-8') as fp: |
| 992 |
fp.write(cgi_file1 % self.pythonexe) |
| 993 |
os.chmod(self.nocgi_path, 0o777) |
| 994 |
|
| 995 |
self.file1_path = os.path.join(self.cgi_dir, 'file1.py') |
| 996 |
with open(self.file1_path, 'w', encoding='utf-8') as file1: |
| 997 |
file1.write(cgi_file1 % self.pythonexe) |
| 998 |
os.chmod(self.file1_path, 0o777) |
| 999 |
|
| 1000 |
self.file2_path = os.path.join(self.cgi_dir, 'file2.py') |
| 1001 |
with open(self.file2_path, 'w', encoding='utf-8') as file2: |
| 1002 |
file2.write(cgi_file2 % self.pythonexe) |
| 1003 |
os.chmod(self.file2_path, 0o777) |
| 1004 |
|
| 1005 |
self.file3_path = os.path.join(self.cgi_child_dir, 'file3.py') |
| 1006 |
with open(self.file3_path, 'w', encoding='utf-8') as file3: |
| 1007 |
file3.write(cgi_file1 % self.pythonexe) |
| 1008 |
os.chmod(self.file3_path, 0o777) |
| 1009 |
|
| 1010 |
self.file4_path = os.path.join(self.cgi_dir, 'file4.py') |
| 1011 |
with open(self.file4_path, 'w', encoding='utf-8') as file4: |
| 1012 |
file4.write(cgi_file4 % (self.pythonexe, 'QUERY_STRING')) |
| 1013 |
os.chmod(self.file4_path, 0o777) |
| 1014 |
|
| 1015 |
self.file5_path = os.path.join(self.cgi_dir_in_sub_dir, 'file5.py') |
| 1016 |
with open(self.file5_path, 'w', encoding='utf-8') as file5: |
| 1017 |
file5.write(cgi_file1 % self.pythonexe) |
| 1018 |
os.chmod(self.file5_path, 0o777) |
| 1019 |
|
| 1020 |
self.file6_path = os.path.join(self.cgi_dir, 'file6.py') |
| 1021 |
with open(self.file6_path, 'w', encoding='utf-8') as file6: |
| 1022 |
file6.write(cgi_file6 % self.pythonexe) |
| 1023 |
os.chmod(self.file6_path, 0o777) |
| 1024 |
|
| 1025 |
self.file7_path = os.path.join(self.cgi_dir, 'file7.py') |
| 1026 |
with open(self.file7_path, 'w', encoding='utf-8') as file7: |
| 1027 |
file7.write(cgi_file7 % self.pythonexe) |
| 1028 |
os.chmod(self.file7_path, 0o777) |
| 1029 |
|
| 1030 |
os.chdir(self.parent_dir) |
| 1031 |
|
| 1032 |
def tearDown(self): |
| 1033 |
self.request_handler._test_case_self = None |
| 1034 |
try: |
| 1035 |
os.chdir(self.cwd) |
| 1036 |
if self._pythonexe_symlink: |
| 1037 |
self._pythonexe_symlink.__exit__(None, None, None) |
| 1038 |
if self.nocgi_path: |
| 1039 |
os.remove(self.nocgi_path) |
| 1040 |
if self.file1_path: |
| 1041 |
os.remove(self.file1_path) |
| 1042 |
if self.file2_path: |
| 1043 |
os.remove(self.file2_path) |
| 1044 |
if self.file3_path: |
| 1045 |
os.remove(self.file3_path) |
| 1046 |
if self.file4_path: |
| 1047 |
os.remove(self.file4_path) |
| 1048 |
if self.file5_path: |
| 1049 |
os.remove(self.file5_path) |
| 1050 |
if self.file6_path: |
| 1051 |
os.remove(self.file6_path) |
| 1052 |
if self.file7_path: |
| 1053 |
os.remove(self.file7_path) |
| 1054 |
os.rmdir(self.cgi_child_dir) |
| 1055 |
os.rmdir(self.cgi_dir) |
| 1056 |
os.rmdir(self.cgi_dir_in_sub_dir) |
| 1057 |
os.rmdir(self.sub_dir_2) |
| 1058 |
os.rmdir(self.sub_dir_1) |
| 1059 |
# The 'gmon.out' file can be written in the current working |
| 1060 |
# directory if C-level code profiling with gprof is enabled. |
| 1061 |
os_helper.unlink(os.path.join(self.parent_dir, 'gmon.out')) |
| 1062 |
os.rmdir(self.parent_dir) |
| 1063 |
finally: |
| 1064 |
BaseTestCase.tearDown(self) |
| 1065 |
|
| 1066 |
def test_url_collapse_path(self): |
| 1067 |
# verify tail is the last portion and head is the rest on proper urls |
| 1068 |
test_vectors = { |
| 1069 |
'': '//', |
| 1070 |
'..': IndexError, |
| 1071 |
'/.//..': IndexError, |
| 1072 |
'/': '//', |
| 1073 |
'//': '//', |
| 1074 |
'/\\': '//\\', |
| 1075 |
'/.//': '//', |
| 1076 |
'cgi-bin/file1.py': '/cgi-bin/file1.py', |
| 1077 |
'/cgi-bin/file1.py': '/cgi-bin/file1.py', |
| 1078 |
'a': '//a', |
| 1079 |
'/a': '//a', |
| 1080 |
'//a': '//a', |
| 1081 |
'./a': '//a', |
| 1082 |
'./C:/': '/C:/', |
| 1083 |
'/a/b': '/a/b', |
| 1084 |
'/a/b/': '/a/b/', |
| 1085 |
'/a/b/.': '/a/b/', |
| 1086 |
'/a/b/c/..': '/a/b/', |
| 1087 |
'/a/b/c/../d': '/a/b/d', |
| 1088 |
'/a/b/c/../d/e/../f': '/a/b/d/f', |
| 1089 |
'/a/b/c/../d/e/../../f': '/a/b/f', |
| 1090 |
'/a/b/c/../d/e/.././././..//f': '/a/b/f', |
| 1091 |
'../a/b/c/../d/e/.././././..//f': IndexError, |
| 1092 |
'/a/b/c/../d/e/../../../f': '/a/f', |
| 1093 |
'/a/b/c/../d/e/../../../../f': '//f', |
| 1094 |
'/a/b/c/../d/e/../../../../../f': IndexError, |
| 1095 |
'/a/b/c/../d/e/../../../../f/..': '//', |
| 1096 |
'/a/b/c/../d/e/../../../../f/../.': '//', |
| 1097 |
} |
| 1098 |
for path, expected in test_vectors.items(): |
| 1099 |
if isinstance(expected, type) and issubclass(expected, Exception): |
| 1100 |
self.assertRaises(expected, |
| 1101 |
server._url_collapse_path, path) |
| 1102 |
else: |
| 1103 |
actual = server._url_collapse_path(path) |
| 1104 |
self.assertEqual(expected, actual, |
| 1105 |
msg='path = %r\nGot: %r\nWanted: %r' % |
| 1106 |
(path, actual, expected)) |
| 1107 |
|
| 1108 |
def test_headers_and_content(self): |
| 1109 |
res = self.request('/cgi-bin/file1.py') |
| 1110 |
self.assertEqual( |
| 1111 |
(res.read(), res.getheader('Content-type'), res.status), |
| 1112 |
(b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK)) |
| 1113 |
|
| 1114 |
def test_issue19435(self): |
| 1115 |
res = self.request('///////////nocgi.py/../cgi-bin/nothere.sh') |
| 1116 |
self.assertEqual(res.status, HTTPStatus.NOT_FOUND) |
| 1117 |
|
| 1118 |
def test_post(self): |
| 1119 |
params = urllib.parse.urlencode( |
| 1120 |
{'spam' : 1, 'eggs' : 'python', 'bacon' : 123456}) |
| 1121 |
headers = {'Content-type' : 'application/x-www-form-urlencoded'} |
| 1122 |
res = self.request('/cgi-bin/file2.py', 'POST', params, headers) |
| 1123 |
|
| 1124 |
self.assertEqual(res.read(), b'1, python, 123456' + self.linesep) |
| 1125 |
|
| 1126 |
def test_large_content_length(self): |
| 1127 |
for w in range(15, 25): |
| 1128 |
size = 1 << w |
| 1129 |
body = b'X' * size |
| 1130 |
headers = {'Content-Length' : str(size)} |
| 1131 |
res = self.request('/cgi-bin/file7.py', 'POST', body, headers) |
| 1132 |
self.assertEqual(res.read(), b'%d %d' % (size, size) + self.linesep) |
| 1133 |
|
| 1134 |
def test_large_content_length_truncated(self): |
| 1135 |
with support.swap_attr(self.request_handler, 'timeout', 0.001): |
| 1136 |
for w in range(18, 65): |
| 1137 |
size = 1 << w |
| 1138 |
headers = {'Content-Length' : str(size)} |
| 1139 |
res = self.request('/cgi-bin/file1.py', 'POST', b'x', headers) |
| 1140 |
self.assertEqual(res.read(), b'Hello World' + self.linesep) |
| 1141 |
|
| 1142 |
def test_invaliduri(self): |
| 1143 |
res = self.request('/cgi-bin/invalid') |
| 1144 |
res.read() |
| 1145 |
self.assertEqual(res.status, HTTPStatus.NOT_FOUND) |
| 1146 |
|
| 1147 |
def test_authorization(self): |
| 1148 |
headers = {b'Authorization' : b'Basic ' + |
| 1149 |
base64.b64encode(b'username:pass')} |
| 1150 |
res = self.request('/cgi-bin/file1.py', 'GET', headers=headers) |
| 1151 |
self.assertEqual( |
| 1152 |
(b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 1153 |
(res.read(), res.getheader('Content-type'), res.status)) |
| 1154 |
|
| 1155 |
def test_no_leading_slash(self): |
| 1156 |
# http://bugs.python.org/issue2254 |
| 1157 |
res = self.request('cgi-bin/file1.py') |
| 1158 |
self.assertEqual( |
| 1159 |
(b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 1160 |
(res.read(), res.getheader('Content-type'), res.status)) |
| 1161 |
|
| 1162 |
def test_os_environ_is_not_altered(self): |
| 1163 |
signature = "Test CGI Server" |
| 1164 |
os.environ['SERVER_SOFTWARE'] = signature |
| 1165 |
res = self.request('/cgi-bin/file1.py') |
| 1166 |
self.assertEqual( |
| 1167 |
(b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 1168 |
(res.read(), res.getheader('Content-type'), res.status)) |
| 1169 |
self.assertEqual(os.environ['SERVER_SOFTWARE'], signature) |
| 1170 |
|
| 1171 |
def test_urlquote_decoding_in_cgi_check(self): |
| 1172 |
res = self.request('/cgi-bin%2ffile1.py') |
| 1173 |
self.assertEqual( |
| 1174 |
(b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 1175 |
(res.read(), res.getheader('Content-type'), res.status)) |
| 1176 |
|
| 1177 |
def test_nested_cgi_path_issue21323(self): |
| 1178 |
res = self.request('/cgi-bin/child-dir/file3.py') |
| 1179 |
self.assertEqual( |
| 1180 |
(b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 1181 |
(res.read(), res.getheader('Content-type'), res.status)) |
| 1182 |
|
| 1183 |
def test_query_with_multiple_question_mark(self): |
| 1184 |
res = self.request('/cgi-bin/file4.py?a=b?c=d') |
| 1185 |
self.assertEqual( |
| 1186 |
(b'a=b?c=d' + self.linesep, 'text/html', HTTPStatus.OK), |
| 1187 |
(res.read(), res.getheader('Content-type'), res.status)) |
| 1188 |
|
| 1189 |
def test_query_with_continuous_slashes(self): |
| 1190 |
res = self.request('/cgi-bin/file4.py?k=aa%2F%2Fbb&//q//p//=//a//b//') |
| 1191 |
self.assertEqual( |
| 1192 |
(b'k=aa%2F%2Fbb&//q//p//=//a//b//' + self.linesep, |
| 1193 |
'text/html', HTTPStatus.OK), |
| 1194 |
(res.read(), res.getheader('Content-type'), res.status)) |
| 1195 |
|
| 1196 |
def test_cgi_path_in_sub_directories(self): |
| 1197 |
try: |
| 1198 |
CGIHTTPRequestHandler.cgi_directories.append('/sub/dir/cgi-bin') |
| 1199 |
res = self.request('/sub/dir/cgi-bin/file5.py') |
| 1200 |
self.assertEqual( |
| 1201 |
(b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 1202 |
(res.read(), res.getheader('Content-type'), res.status)) |
| 1203 |
finally: |
| 1204 |
CGIHTTPRequestHandler.cgi_directories.remove('/sub/dir/cgi-bin') |
| 1205 |
|
| 1206 |
def test_accept(self): |
| 1207 |
browser_accept = \ |
| 1208 |
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' |
| 1209 |
tests = ( |
| 1210 |
((('Accept', browser_accept),), browser_accept), |
| 1211 |
((), ''), |
| 1212 |
# Hack case to get two values for the one header |
| 1213 |
((('Accept', 'text/html'), ('ACCEPT', 'text/plain')), |
| 1214 |
'text/html,text/plain'), |
| 1215 |
) |
| 1216 |
for headers, expected in tests: |
| 1217 |
headers = OrderedDict(headers) |
| 1218 |
with self.subTest(headers): |
| 1219 |
res = self.request('/cgi-bin/file6.py', 'GET', headers=headers) |
| 1220 |
self.assertEqual(http.HTTPStatus.OK, res.status) |
| 1221 |
expected = f"HTTP_ACCEPT={expected}".encode('ascii') |
| 1222 |
self.assertIn(expected, res.read()) |
| 1223 |
|
| 1224 |
|
| 1225 |
class SocketlessRequestHandler(SimpleHTTPRequestHandler): |
| 1226 |
def __init__(self, directory=None): |
| 1227 |
request = mock.Mock() |
| 1228 |
request.makefile.return_value = BytesIO() |
| 1229 |
super().__init__(request, None, None, directory=directory) |
| 1230 |
|
| 1231 |
self.get_called = False |
| 1232 |
self.protocol_version = "HTTP/1.1" |
| 1233 |
|
| 1234 |
def do_GET(self): |
| 1235 |
self.get_called = True |
| 1236 |
self.send_response(HTTPStatus.OK) |
| 1237 |
self.send_header('Content-Type', 'text/html') |
| 1238 |
self.end_headers() |
| 1239 |
self.wfile.write(b'<html><body>Data</body></html>\r\n') |
| 1240 |
|
| 1241 |
def log_message(self, format, *args): |
| 1242 |
pass |
| 1243 |
|
| 1244 |
class RejectingSocketlessRequestHandler(SocketlessRequestHandler): |
| 1245 |
def handle_expect_100(self): |
| 1246 |
self.send_error(HTTPStatus.EXPECTATION_FAILED) |
| 1247 |
return False |
| 1248 |
|
| 1249 |
|
| 1250 |
class AuditableBytesIO: |
| 1251 |
|
| 1252 |
def __init__(self): |
| 1253 |
self.datas = [] |
| 1254 |
|
| 1255 |
def write(self, data): |
| 1256 |
self.datas.append(data) |
| 1257 |
|
| 1258 |
def getData(self): |
| 1259 |
return b''.join(self.datas) |
| 1260 |
|
| 1261 |
@property |
| 1262 |
def numWrites(self): |
| 1263 |
return len(self.datas) |
| 1264 |
|
| 1265 |
|
| 1266 |
class BaseHTTPRequestHandlerTestCase(unittest.TestCase): |
| 1267 |
"""Test the functionality of the BaseHTTPServer. |
| 1268 |
|
| 1269 |
Test the support for the Expect 100-continue header. |
| 1270 |
""" |
| 1271 |
|
| 1272 |
HTTPResponseMatch = re.compile(b'HTTP/1.[0-9]+ 200 OK') |
| 1273 |
|
| 1274 |
def setUp (self): |
| 1275 |
self.handler = SocketlessRequestHandler() |
| 1276 |
|
| 1277 |
def send_typical_request(self, message): |
| 1278 |
input = BytesIO(message) |
| 1279 |
output = BytesIO() |
| 1280 |
self.handler.rfile = input |
| 1281 |
self.handler.wfile = output |
| 1282 |
self.handler.handle_one_request() |
| 1283 |
output.seek(0) |
| 1284 |
return output.readlines() |
| 1285 |
|
| 1286 |
def verify_get_called(self): |
| 1287 |
self.assertTrue(self.handler.get_called) |
| 1288 |
|
| 1289 |
def verify_expected_headers(self, headers): |
| 1290 |
for fieldName in b'Server: ', b'Date: ', b'Content-Type: ': |
| 1291 |
self.assertEqual(sum(h.startswith(fieldName) for h in headers), 1) |
| 1292 |
|
| 1293 |
def verify_http_server_response(self, response): |
| 1294 |
match = self.HTTPResponseMatch.search(response) |
| 1295 |
self.assertIsNotNone(match) |
| 1296 |
|
| 1297 |
def test_unprintable_not_logged(self): |
| 1298 |
# We call the method from the class directly as our Socketless |
| 1299 |
# Handler subclass overrode it... nice for everything BUT this test. |
| 1300 |
self.handler.client_address = ('127.0.0.1', 1337) |
| 1301 |
log_message = BaseHTTPRequestHandler.log_message |
| 1302 |
with mock.patch.object(sys, 'stderr', StringIO()) as fake_stderr: |
| 1303 |
log_message(self.handler, '/foo') |
| 1304 |
log_message(self.handler, '/\033bar\000\033') |
| 1305 |
log_message(self.handler, '/spam %s.', 'a') |
| 1306 |
log_message(self.handler, '/spam %s.', '\033\x7f\x9f\xa0beans') |
| 1307 |
log_message(self.handler, '"GET /foo\\b"ar\007 HTTP/1.0"') |
| 1308 |
stderr = fake_stderr.getvalue() |
| 1309 |
self.assertNotIn('\033', stderr) # non-printable chars are caught. |
| 1310 |
self.assertNotIn('\000', stderr) # non-printable chars are caught. |
| 1311 |
lines = stderr.splitlines() |
| 1312 |
self.assertIn('/foo', lines[0]) |
| 1313 |
self.assertIn(r'/\x1bbar\x00\x1b', lines[1]) |
| 1314 |
self.assertIn('/spam a.', lines[2]) |
| 1315 |
self.assertIn('/spam \\x1b\\x7f\\x9f\xa0beans.', lines[3]) |
| 1316 |
self.assertIn(r'"GET /foo\\b"ar\x07 HTTP/1.0"', lines[4]) |
| 1317 |
|
| 1318 |
def test_http_1_1(self): |
| 1319 |
result = self.send_typical_request(b'GET / HTTP/1.1\r\n\r\n') |
| 1320 |
self.verify_http_server_response(result[0]) |
| 1321 |
self.verify_expected_headers(result[1:-1]) |
| 1322 |
self.verify_get_called() |
| 1323 |
self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
| 1324 |
self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1') |
| 1325 |
self.assertEqual(self.handler.command, 'GET') |
| 1326 |
self.assertEqual(self.handler.path, '/') |
| 1327 |
self.assertEqual(self.handler.request_version, 'HTTP/1.1') |
| 1328 |
self.assertSequenceEqual(self.handler.headers.items(), ()) |
| 1329 |
|
| 1330 |
def test_http_1_0(self): |
| 1331 |
result = self.send_typical_request(b'GET / HTTP/1.0\r\n\r\n') |
| 1332 |
self.verify_http_server_response(result[0]) |
| 1333 |
self.verify_expected_headers(result[1:-1]) |
| 1334 |
self.verify_get_called() |
| 1335 |
self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
| 1336 |
self.assertEqual(self.handler.requestline, 'GET / HTTP/1.0') |
| 1337 |
self.assertEqual(self.handler.command, 'GET') |
| 1338 |
self.assertEqual(self.handler.path, '/') |
| 1339 |
self.assertEqual(self.handler.request_version, 'HTTP/1.0') |
| 1340 |
self.assertSequenceEqual(self.handler.headers.items(), ()) |
| 1341 |
|
| 1342 |
def test_http_0_9(self): |
| 1343 |
result = self.send_typical_request(b'GET / HTTP/0.9\r\n\r\n') |
| 1344 |
self.assertEqual(len(result), 1) |
| 1345 |
self.assertEqual(result[0], b'<html><body>Data</body></html>\r\n') |
| 1346 |
self.verify_get_called() |
| 1347 |
|
| 1348 |
def test_extra_space(self): |
| 1349 |
result = self.send_typical_request( |
| 1350 |
b'GET /spaced out HTTP/1.1\r\n' |
| 1351 |
b'Host: dummy\r\n' |
| 1352 |
b'\r\n' |
| 1353 |
) |
| 1354 |
self.assertStartsWith(result[0], b'HTTP/1.1 400 ') |
| 1355 |
self.verify_expected_headers(result[1:result.index(b'\r\n')]) |
| 1356 |
self.assertFalse(self.handler.get_called) |
| 1357 |
|
| 1358 |
def test_with_continue_1_0(self): |
| 1359 |
result = self.send_typical_request(b'GET / HTTP/1.0\r\nExpect: 100-continue\r\n\r\n') |
| 1360 |
self.verify_http_server_response(result[0]) |
| 1361 |
self.verify_expected_headers(result[1:-1]) |
| 1362 |
self.verify_get_called() |
| 1363 |
self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
| 1364 |
self.assertEqual(self.handler.requestline, 'GET / HTTP/1.0') |
| 1365 |
self.assertEqual(self.handler.command, 'GET') |
| 1366 |
self.assertEqual(self.handler.path, '/') |
| 1367 |
self.assertEqual(self.handler.request_version, 'HTTP/1.0') |
| 1368 |
headers = (("Expect", "100-continue"),) |
| 1369 |
self.assertSequenceEqual(self.handler.headers.items(), headers) |
| 1370 |
|
| 1371 |
def test_with_continue_1_1(self): |
| 1372 |
result = self.send_typical_request(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n') |
| 1373 |
self.assertEqual(result[0], b'HTTP/1.1 100 Continue\r\n') |
| 1374 |
self.assertEqual(result[1], b'\r\n') |
| 1375 |
self.assertEqual(result[2], b'HTTP/1.1 200 OK\r\n') |
| 1376 |
self.verify_expected_headers(result[2:-1]) |
| 1377 |
self.verify_get_called() |
| 1378 |
self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
| 1379 |
self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1') |
| 1380 |
self.assertEqual(self.handler.command, 'GET') |
| 1381 |
self.assertEqual(self.handler.path, '/') |
| 1382 |
self.assertEqual(self.handler.request_version, 'HTTP/1.1') |
| 1383 |
headers = (("Expect", "100-continue"),) |
| 1384 |
self.assertSequenceEqual(self.handler.headers.items(), headers) |
| 1385 |
|
| 1386 |
def test_header_buffering_of_send_error(self): |
| 1387 |
|
| 1388 |
input = BytesIO(b'GET / HTTP/1.1\r\n\r\n') |
| 1389 |
output = AuditableBytesIO() |
| 1390 |
handler = SocketlessRequestHandler() |
| 1391 |
handler.rfile = input |
| 1392 |
handler.wfile = output |
| 1393 |
handler.request_version = 'HTTP/1.1' |
| 1394 |
handler.requestline = '' |
| 1395 |
handler.command = None |
| 1396 |
|
| 1397 |
handler.send_error(418) |
| 1398 |
self.assertEqual(output.numWrites, 2) |
| 1399 |
|
| 1400 |
def test_header_buffering_of_send_response_only(self): |
| 1401 |
|
| 1402 |
input = BytesIO(b'GET / HTTP/1.1\r\n\r\n') |
| 1403 |
output = AuditableBytesIO() |
| 1404 |
handler = SocketlessRequestHandler() |
| 1405 |
handler.rfile = input |
| 1406 |
handler.wfile = output |
| 1407 |
handler.request_version = 'HTTP/1.1' |
| 1408 |
|
| 1409 |
handler.send_response_only(418) |
| 1410 |
self.assertEqual(output.numWrites, 0) |
| 1411 |
handler.end_headers() |
| 1412 |
self.assertEqual(output.numWrites, 1) |
| 1413 |
|
| 1414 |
def test_header_buffering_of_send_header(self): |
| 1415 |
|
| 1416 |
input = BytesIO(b'GET / HTTP/1.1\r\n\r\n') |
| 1417 |
output = AuditableBytesIO() |
| 1418 |
handler = SocketlessRequestHandler() |
| 1419 |
handler.rfile = input |
| 1420 |
handler.wfile = output |
| 1421 |
handler.request_version = 'HTTP/1.1' |
| 1422 |
|
| 1423 |
handler.send_header('Foo', 'foo') |
| 1424 |
handler.send_header('bar', 'bar') |
| 1425 |
self.assertEqual(output.numWrites, 0) |
| 1426 |
handler.end_headers() |
| 1427 |
self.assertEqual(output.getData(), b'Foo: foo\r\nbar: bar\r\n\r\n') |
| 1428 |
self.assertEqual(output.numWrites, 1) |
| 1429 |
|
| 1430 |
def test_header_unbuffered_when_continue(self): |
| 1431 |
|
| 1432 |
def _readAndReseek(f): |
| 1433 |
pos = f.tell() |
| 1434 |
f.seek(0) |
| 1435 |
data = f.read() |
| 1436 |
f.seek(pos) |
| 1437 |
return data |
| 1438 |
|
| 1439 |
input = BytesIO(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n') |
| 1440 |
output = BytesIO() |
| 1441 |
self.handler.rfile = input |
| 1442 |
self.handler.wfile = output |
| 1443 |
self.handler.request_version = 'HTTP/1.1' |
| 1444 |
|
| 1445 |
self.handler.handle_one_request() |
| 1446 |
self.assertNotEqual(_readAndReseek(output), b'') |
| 1447 |
result = _readAndReseek(output).split(b'\r\n') |
| 1448 |
self.assertEqual(result[0], b'HTTP/1.1 100 Continue') |
| 1449 |
self.assertEqual(result[1], b'') |
| 1450 |
self.assertEqual(result[2], b'HTTP/1.1 200 OK') |
| 1451 |
|
| 1452 |
def test_with_continue_rejected(self): |
| 1453 |
usual_handler = self.handler # Save to avoid breaking any subsequent tests. |
| 1454 |
self.handler = RejectingSocketlessRequestHandler() |
| 1455 |
result = self.send_typical_request(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n') |
| 1456 |
self.assertEqual(result[0], b'HTTP/1.1 417 Expectation Failed\r\n') |
| 1457 |
self.verify_expected_headers(result[1:-1]) |
| 1458 |
# The expect handler should short circuit the usual get method by |
| 1459 |
# returning false here, so get_called should be false |
| 1460 |
self.assertFalse(self.handler.get_called) |
| 1461 |
self.assertEqual(sum(r == b'Connection: close\r\n' for r in result[1:-1]), 1) |
| 1462 |
self.handler = usual_handler # Restore to avoid breaking any subsequent tests. |
| 1463 |
|
| 1464 |
def test_request_length(self): |
| 1465 |
# Issue #10714: huge request lines are discarded, to avoid Denial |
| 1466 |
# of Service attacks. |
| 1467 |
result = self.send_typical_request(b'GET ' + b'x' * 65537) |
| 1468 |
self.assertEqual(result[0], b'HTTP/1.1 414 URI Too Long\r\n') |
| 1469 |
self.assertFalse(self.handler.get_called) |
| 1470 |
self.assertIsInstance(self.handler.requestline, str) |
| 1471 |
|
| 1472 |
def test_header_length(self): |
| 1473 |
# Issue #6791: same for headers |
| 1474 |
result = self.send_typical_request( |
| 1475 |
b'GET / HTTP/1.1\r\nX-Foo: bar' + b'r' * 65537 + b'\r\n\r\n') |
| 1476 |
self.assertEqual(result[0], b'HTTP/1.1 431 Line too long\r\n') |
| 1477 |
self.assertFalse(self.handler.get_called) |
| 1478 |
self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1') |
| 1479 |
|
| 1480 |
def test_too_many_headers(self): |
| 1481 |
result = self.send_typical_request( |
| 1482 |
b'GET / HTTP/1.1\r\n' + b'X-Foo: bar\r\n' * 101 + b'\r\n') |
| 1483 |
self.assertEqual(result[0], b'HTTP/1.1 431 Too many headers\r\n') |
| 1484 |
self.assertFalse(self.handler.get_called) |
| 1485 |
self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1') |
| 1486 |
|
| 1487 |
def test_html_escape_on_error(self): |
| 1488 |
result = self.send_typical_request( |
| 1489 |
b'<script>alert("hello")</script> / HTTP/1.1') |
| 1490 |
result = b''.join(result) |
| 1491 |
text = '<script>alert("hello")</script>' |
| 1492 |
self.assertIn(html.escape(text, quote=False).encode('ascii'), result) |
| 1493 |
|
| 1494 |
def test_close_connection(self): |
| 1495 |
# handle_one_request() should be repeatedly called until |
| 1496 |
# it sets close_connection |
| 1497 |
def handle_one_request(): |
| 1498 |
self.handler.close_connection = next(close_values) |
| 1499 |
self.handler.handle_one_request = handle_one_request |
| 1500 |
|
| 1501 |
close_values = iter((True,)) |
| 1502 |
self.handler.handle() |
| 1503 |
self.assertRaises(StopIteration, next, close_values) |
| 1504 |
|
| 1505 |
close_values = iter((False, False, True)) |
| 1506 |
self.handler.handle() |
| 1507 |
self.assertRaises(StopIteration, next, close_values) |
| 1508 |
|
| 1509 |
def test_date_time_string(self): |
| 1510 |
now = time.time() |
| 1511 |
# this is the old code that formats the timestamp |
| 1512 |
year, month, day, hh, mm, ss, wd, y, z = time.gmtime(now) |
| 1513 |
expected = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % ( |
| 1514 |
self.handler.weekdayname[wd], |
| 1515 |
day, |
| 1516 |
self.handler.monthname[month], |
| 1517 |
year, hh, mm, ss |
| 1518 |
) |
| 1519 |
self.assertEqual(self.handler.date_time_string(timestamp=now), expected) |
| 1520 |
|
| 1521 |
|
| 1522 |
class SimpleHTTPRequestHandlerTestCase(unittest.TestCase): |
| 1523 |
""" Test url parsing """ |
| 1524 |
def setUp(self): |
| 1525 |
self.translated_1 = os.path.join(os.getcwd(), 'filename') |
| 1526 |
self.translated_2 = os.path.join('foo', 'filename') |
| 1527 |
self.translated_3 = os.path.join('bar', 'filename') |
| 1528 |
self.handler_1 = SocketlessRequestHandler() |
| 1529 |
self.handler_2 = SocketlessRequestHandler(directory='foo') |
| 1530 |
self.handler_3 = SocketlessRequestHandler(directory=pathlib.PurePath('bar')) |
| 1531 |
|
| 1532 |
def test_query_arguments(self): |
| 1533 |
path = self.handler_1.translate_path('/filename') |
| 1534 |
self.assertEqual(path, self.translated_1) |
| 1535 |
path = self.handler_2.translate_path('/filename') |
| 1536 |
self.assertEqual(path, self.translated_2) |
| 1537 |
path = self.handler_3.translate_path('/filename') |
| 1538 |
self.assertEqual(path, self.translated_3) |
| 1539 |
|
| 1540 |
path = self.handler_1.translate_path('/filename?foo=bar') |
| 1541 |
self.assertEqual(path, self.translated_1) |
| 1542 |
path = self.handler_2.translate_path('/filename?foo=bar') |
| 1543 |
self.assertEqual(path, self.translated_2) |
| 1544 |
path = self.handler_3.translate_path('/filename?foo=bar') |
| 1545 |
self.assertEqual(path, self.translated_3) |
| 1546 |
|
| 1547 |
path = self.handler_1.translate_path('/filename?a=b&spam=eggs#zot') |
| 1548 |
self.assertEqual(path, self.translated_1) |
| 1549 |
path = self.handler_2.translate_path('/filename?a=b&spam=eggs#zot') |
| 1550 |
self.assertEqual(path, self.translated_2) |
| 1551 |
path = self.handler_3.translate_path('/filename?a=b&spam=eggs#zot') |
| 1552 |
self.assertEqual(path, self.translated_3) |
| 1553 |
|
| 1554 |
def test_start_with_double_slash(self): |
| 1555 |
path = self.handler_1.translate_path('//filename') |
| 1556 |
self.assertEqual(path, self.translated_1) |
| 1557 |
path = self.handler_2.translate_path('//filename') |
| 1558 |
self.assertEqual(path, self.translated_2) |
| 1559 |
path = self.handler_3.translate_path('//filename') |
| 1560 |
self.assertEqual(path, self.translated_3) |
| 1561 |
|
| 1562 |
path = self.handler_1.translate_path('//filename?foo=bar') |
| 1563 |
self.assertEqual(path, self.translated_1) |
| 1564 |
path = self.handler_2.translate_path('//filename?foo=bar') |
| 1565 |
self.assertEqual(path, self.translated_2) |
| 1566 |
path = self.handler_3.translate_path('//filename?foo=bar') |
| 1567 |
self.assertEqual(path, self.translated_3) |
| 1568 |
|
| 1569 |
def test_windows_colon(self): |
| 1570 |
with support.swap_attr(server.os, 'path', ntpath): |
| 1571 |
path = self.handler_1.translate_path('c:c:c:foo/filename') |
| 1572 |
path = path.replace(ntpath.sep, os.sep) |
| 1573 |
self.assertEqual(path, self.translated_1) |
| 1574 |
path = self.handler_2.translate_path('c:c:c:foo/filename') |
| 1575 |
path = path.replace(ntpath.sep, os.sep) |
| 1576 |
self.assertEqual(path, self.translated_2) |
| 1577 |
path = self.handler_3.translate_path('c:c:c:foo/filename') |
| 1578 |
path = path.replace(ntpath.sep, os.sep) |
| 1579 |
self.assertEqual(path, self.translated_3) |
| 1580 |
|
| 1581 |
path = self.handler_1.translate_path('\\c:../filename') |
| 1582 |
path = path.replace(ntpath.sep, os.sep) |
| 1583 |
self.assertEqual(path, self.translated_1) |
| 1584 |
path = self.handler_2.translate_path('\\c:../filename') |
| 1585 |
path = path.replace(ntpath.sep, os.sep) |
| 1586 |
self.assertEqual(path, self.translated_2) |
| 1587 |
path = self.handler_3.translate_path('\\c:../filename') |
| 1588 |
path = path.replace(ntpath.sep, os.sep) |
| 1589 |
self.assertEqual(path, self.translated_3) |
| 1590 |
|
| 1591 |
path = self.handler_1.translate_path('c:\\c:..\\foo/filename') |
| 1592 |
path = path.replace(ntpath.sep, os.sep) |
| 1593 |
self.assertEqual(path, self.translated_1) |
| 1594 |
path = self.handler_2.translate_path('c:\\c:..\\foo/filename') |
| 1595 |
path = path.replace(ntpath.sep, os.sep) |
| 1596 |
self.assertEqual(path, self.translated_2) |
| 1597 |
path = self.handler_3.translate_path('c:\\c:..\\foo/filename') |
| 1598 |
path = path.replace(ntpath.sep, os.sep) |
| 1599 |
self.assertEqual(path, self.translated_3) |
| 1600 |
|
| 1601 |
path = self.handler_1.translate_path('c:c:foo\\c:c:bar/filename') |
| 1602 |
path = path.replace(ntpath.sep, os.sep) |
| 1603 |
self.assertEqual(path, self.translated_1) |
| 1604 |
path = self.handler_2.translate_path('c:c:foo\\c:c:bar/filename') |
| 1605 |
path = path.replace(ntpath.sep, os.sep) |
| 1606 |
self.assertEqual(path, self.translated_2) |
| 1607 |
path = self.handler_3.translate_path('c:c:foo\\c:c:bar/filename') |
| 1608 |
path = path.replace(ntpath.sep, os.sep) |
| 1609 |
self.assertEqual(path, self.translated_3) |
| 1610 |
|
| 1611 |
|
| 1612 |
class MiscTestCase(unittest.TestCase): |
| 1613 |
def test_all(self): |
| 1614 |
expected = [] |
| 1615 |
denylist = {'executable', 'nobody_uid', 'test'} |
| 1616 |
for name in dir(server): |
| 1617 |
if name.startswith('_') or name in denylist: |
| 1618 |
continue |
| 1619 |
module_object = getattr(server, name) |
| 1620 |
if getattr(module_object, '__module__', None) == 'http.server': |
| 1621 |
expected.append(name) |
| 1622 |
self.assertCountEqual(server.__all__, expected) |
| 1623 |
|
| 1624 |
|
| 1625 |
class ScriptTestCase(unittest.TestCase): |
| 1626 |
|
| 1627 |
def mock_server_class(self): |
| 1628 |
return mock.MagicMock( |
| 1629 |
return_value=mock.MagicMock( |
| 1630 |
__enter__=mock.MagicMock( |
| 1631 |
return_value=mock.MagicMock( |
| 1632 |
socket=mock.MagicMock( |
| 1633 |
getsockname=lambda: ('', 0), |
| 1634 |
), |
| 1635 |
), |
| 1636 |
), |
| 1637 |
), |
| 1638 |
) |
| 1639 |
|
| 1640 |
@mock.patch('builtins.print') |
| 1641 |
def test_server_test_unspec(self, _): |
| 1642 |
mock_server = self.mock_server_class() |
| 1643 |
server.test(ServerClass=mock_server, bind=None) |
| 1644 |
self.assertIn( |
| 1645 |
mock_server.address_family, |
| 1646 |
(socket.AF_INET6, socket.AF_INET), |
| 1647 |
) |
| 1648 |
|
| 1649 |
@mock.patch('builtins.print') |
| 1650 |
def test_server_test_localhost(self, _): |
| 1651 |
mock_server = self.mock_server_class() |
| 1652 |
server.test(ServerClass=mock_server, bind="localhost") |
| 1653 |
self.assertIn( |
| 1654 |
mock_server.address_family, |
| 1655 |
(socket.AF_INET6, socket.AF_INET), |
| 1656 |
) |
| 1657 |
|
| 1658 |
ipv6_addrs = ( |
| 1659 |
"::", |
| 1660 |
"2001:0db8:85a3:0000:0000:8a2e:0370:7334", |
| 1661 |
"::1", |
| 1662 |
) |
| 1663 |
|
| 1664 |
ipv4_addrs = ( |
| 1665 |
"0.0.0.0", |
| 1666 |
"8.8.8.8", |
| 1667 |
"127.0.0.1", |
| 1668 |
) |
| 1669 |
|
| 1670 |
@mock.patch('builtins.print') |
| 1671 |
def test_server_test_ipv6(self, _): |
| 1672 |
for bind in self.ipv6_addrs: |
| 1673 |
mock_server = self.mock_server_class() |
| 1674 |
server.test(ServerClass=mock_server, bind=bind) |
| 1675 |
self.assertEqual(mock_server.address_family, socket.AF_INET6) |
| 1676 |
|
| 1677 |
@mock.patch('builtins.print') |
| 1678 |
def test_server_test_ipv4(self, _): |
| 1679 |
for bind in self.ipv4_addrs: |
| 1680 |
mock_server = self.mock_server_class() |
| 1681 |
server.test(ServerClass=mock_server, bind=bind) |
| 1682 |
self.assertEqual(mock_server.address_family, socket.AF_INET) |
| 1683 |
|
| 1684 |
|
| 1685 |
def setUpModule(): |
| 1686 |
unittest.addModuleCleanup(os.chdir, os.getcwd()) |
| 1687 |
|
| 1688 |
|
| 1689 |
if __name__ == '__main__': |
| 1690 |
unittest.main() |