[3.9] bpo-5054: CGIHTTPRequestHandler.run_cgi() HTTP_ACCEPT improperl… · python/cpython@b630ca7
@@ -3,7 +3,7 @@
33Written by Cody A.W. Somerville <cody-somerville@ubuntu.com>,
44Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest.
55"""
6-6+from collections import OrderedDict
77from http.server import BaseHTTPRequestHandler, HTTPServer, \
88SimpleHTTPRequestHandler, CGIHTTPRequestHandler
99from http import server, HTTPStatus
@@ -19,7 +19,7 @@
1919import email.message
2020import email.utils
2121import html
22-import http.client
22+import http, http.client
2323import urllib.parse
2424import tempfile
2525import time
@@ -586,6 +586,15 @@ def test_html_escape_filename(self):
586586print(os.environ["%s"])
587587"""
588588589+cgi_file6 = """\
590+#!%s
591+import os
592+593+print("Content-type: text/plain")
594+print()
595+print(repr(os.environ))
596+"""
597+589598590599@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
591600 "This test can't be run reliably as root (issue #13308).")
@@ -664,6 +673,11 @@ def setUp(self):
664673file5.write(cgi_file1 % self.pythonexe)
665674os.chmod(self.file5_path, 0o777)
666675676+self.file6_path = os.path.join(self.cgi_dir, 'file6.py')
677+with open(self.file6_path, 'w', encoding='utf-8') as file6:
678+file6.write(cgi_file6 % self.pythonexe)
679+os.chmod(self.file6_path, 0o777)
680+667681os.chdir(self.parent_dir)
668682669683def tearDown(self):
@@ -683,6 +697,8 @@ def tearDown(self):
683697os.remove(self.file4_path)
684698if self.file5_path:
685699os.remove(self.file5_path)
700+if self.file6_path:
701+os.remove(self.file6_path)
686702os.rmdir(self.cgi_child_dir)
687703os.rmdir(self.cgi_dir)
688704os.rmdir(self.cgi_dir_in_sub_dir)
@@ -816,6 +832,23 @@ def test_cgi_path_in_sub_directories(self):
816832finally:
817833CGIHTTPRequestHandler.cgi_directories.remove('/sub/dir/cgi-bin')
818834835+def test_accept(self):
836+browser_accept = \
837+'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
838+tests = (
839+ ((('Accept', browser_accept),), browser_accept),
840+ ((), ''),
841+# Hack case to get two values for the one header
842+ ((('Accept', 'text/html'), ('ACCEPT', 'text/plain')),
843+'text/html,text/plain'),
844+ )
845+for headers, expected in tests:
846+headers = OrderedDict(headers)
847+with self.subTest(headers):
848+res = self.request('/cgi-bin/file6.py', 'GET', headers=headers)
849+self.assertEqual(http.HTTPStatus.OK, res.status)
850+expected = f"'HTTP_ACCEPT': {expected!r}"
851+self.assertIn(expected.encode('ascii'), res.read())
819852820853821854class SocketlessRequestHandler(SimpleHTTPRequestHandler):