# HG changeset patch # Parent 7ae156f07a900114a97e585ad58132c007cb17c8 diff -r 7ae156f07a90 Lib/http/server.py --- a/Lib/http/server.py Sun Feb 15 00:45:57 2015 +0200 +++ b/Lib/http/server.py Sun Feb 15 04:25:15 2015 +0000 @@ -1061,12 +1061,7 @@ referer = self.headers.get('referer') if referer: env['HTTP_REFERER'] = referer - accept = [] - for line in self.headers.getallmatchingheaders('accept'): - if line[:1] in "\t\n\r ": - accept.append(line.strip()) - else: - accept = accept + line[7:].split(',') + accept = self.headers.get_all('accept', ()) env['HTTP_ACCEPT'] = ','.join(accept) ua = self.headers.get('user-agent') if ua: diff -r 7ae156f07a90 Lib/test/test_httpservers.py --- a/Lib/test/test_httpservers.py Sun Feb 15 00:45:57 2015 +0200 +++ b/Lib/test/test_httpservers.py Sun Feb 15 04:25:15 2015 +0000 @@ -15,9 +15,10 @@ import shutil import urllib.parse import html -import http.client +import http, http.client import tempfile from io import BytesIO +from collections import OrderedDict import unittest from test import support @@ -366,6 +367,15 @@ form.getfirst("bacon"))) """ +cgi_env = """\ +#!%s +import os + +print("Content-type: text/plain") +print() +print(repr(os.environ)) +""" + @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0, "This test can't be run reliably as root (issue #13308).") @@ -387,6 +397,7 @@ self.file1_path = None self.file2_path = None self.file3_path = None + self.env_path = None # The shebang line should be pure ASCII: use symlink if possible. # See issue #7668. @@ -425,6 +436,11 @@ file3.write(cgi_file1 % self.pythonexe) os.chmod(self.file3_path, 0o777) + self.env_path = os.path.join(self.cgi_dir, 'env.py') + with open(self.env_path, 'w', encoding='utf-8') as file: + file.write(cgi_env % self.pythonexe) + os.chmod(self.env_path, 0o777) + os.chdir(self.parent_dir) def tearDown(self): @@ -440,6 +456,8 @@ os.remove(self.file2_path) if self.file3_path: os.remove(self.file3_path) + if self.env_path: + os.remove(self.env_path) os.rmdir(self.cgi_child_dir) os.rmdir(self.cgi_dir) os.rmdir(self.parent_dir) @@ -541,6 +559,24 @@ self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200), (res.read(), res.getheader('Content-type'), res.status)) + def test_accept(self): + browser_accept = \ + 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' + tests = ( + ((('Accept', browser_accept),), browser_accept), + ((), ''), + # Hack case to get two values for the one header + ((('Accept', 'text/html'), ('ACCEPT', 'text/plain')), + 'text/html,text/plain'), + ) + for headers, expected in tests: + headers = OrderedDict(headers) + with self.subTest(headers): + res = self.request('/cgi-bin/env.py', 'GET', headers=headers) + self.assertEqual(http.HTTPStatus.OK, res.status) + expected = r"'HTTP_ACCEPT': " + repr(expected) + self.assertIn(expected.encode('ascii'), res.read()) + class SocketlessRequestHandler(SimpleHTTPRequestHandler): def __init__(self):