Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-105622: Parse HTTP headers with default email policy #105918

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Doc/library/http.client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ This module provides the following function:
.. function:: parse_headers(fp)

Parse the headers from a file pointer *fp* representing a HTTP
request/response. The file has to be a :class:`BufferedIOBase` reader
(i.e. not text) and must provide a valid :rfc:`2822` style header.
request/response according to the the default email policy
:data:`email.policy.default`. The file has to be a :class:`BufferedIOBase`
reader (i.e. not text) and must provide a valid :rfc:`2822` style header.

This function returns an instance of :class:`http.client.HTTPMessage`
that holds the header fields, but no payload
Expand Down
4 changes: 3 additions & 1 deletion Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"""

import email.parser
import email.policy
import email.message
import errno
import http
Expand Down Expand Up @@ -233,7 +234,8 @@ def _parse_header_lines(header_lines, _class=HTTPMessage):

"""
hstring = b''.join(header_lines).decode('iso-8859-1')
return email.parser.Parser(_class=_class).parsestr(hstring)
return email.parser.Parser(_class=_class,
policy=email.policy.default).parsestr(hstring)

def parse_headers(fp, _class=HTTPMessage):
"""Parses only RFC2822 headers from a file pointer."""
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_docxmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_valid_get_response(self):
response = self.client.getresponse()

self.assertEqual(response.status, 200)
self.assertEqual(response.getheader("Content-type"), "text/html; charset=UTF-8")
self.assertEqual(response.getheader("Content-type"), "text/html; charset=\"UTF-8\"")

# Server raises an exception if we don't start to read the data
response.read()
Expand All @@ -103,7 +103,7 @@ def test_get_css(self):
response = self.client.getresponse()

self.assertEqual(response.status, 200)
self.assertEqual(response.getheader("Content-type"), "text/css; charset=UTF-8")
self.assertEqual(response.getheader("Content-type"), "text/css; charset=\"UTF-8\"")

# Server raises an exception if we don't start to read the data
response.read()
Expand Down
10 changes: 4 additions & 6 deletions Lib/test/test_urllibnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,10 @@ def test_header(self):

def test_data_header(self):
with self.urlretrieve(self.logo) as (file_location, fileheaders):
datevalue = fileheaders.get('Date')
dateformat = '%a, %d %b %Y %H:%M:%S GMT'
try:
time.strptime(datevalue, dateformat)
except ValueError:
self.fail('Date value not in %r format' % dateformat)
date = fileheaders.get('Date')
self.assertIsInstance(date, email.headerregistry.DateHeader,
"Date is not an instance of email.headerregistery.DateHeader")
self.assertIsNotNone(date.datetime, "Date is not in RFC 5322 format")

def test_reporthook(self):
records = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Update :func:`http.client.parse_headers` to use the default email policy
:data:`email.policy.default` when parsing headers. Previously no email
policy was specified which is not reccomended.