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

Fix a bug for header field getting malformed when it extends over multiple lines #111

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
36 changes: 33 additions & 3 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,42 @@ def test_headers_raw_dict_none(self):
self.assertIsNone(headers_dict_to_raw(None))

def test_headers_raw_to_dict(self):
raw = b"Content-type: text/html\n\rAccept: gzip\n\r\
Cache-Control: no-cache\n\rCache-Control: no-store\n\n"
dct = {b'Content-type': [b'text/html'], b'Accept': [b'gzip'],
raw = b'\r\n'.join((b"Content-type: text/html",
b"Accept: gzip",
b"Cache-Control: no-cache",
b"Cache-Control: no-store"))
dct = {b'Content-type': [b'text/html'], b'Accept': [b'gzip'],
b'Cache-Control': [b'no-cache', b'no-store']}
self.assertEqual(headers_raw_to_dict(raw), dct)

def test_headers_raw_to_dict_multiline(self):
raw = b'\r\n'.join((b'Content-Type: multipart/related;',
b' type="application/xop+xml";',
b'\tboundary="example"',
b'Cache-Control: no-cache'))
# With strict=False, the header value that spans across
# multiple lines does not get parsed fully, and only the first
# line is retained.
dct = {b'Content-Type': [b'multipart/related;'],
b'Cache-Control': [b'no-cache']}
self.assertEqual(headers_raw_to_dict(raw), dct)

def test_headers_raw_to_dict_multiline_strict(self):
raw = b'\r\n'.join((b'Content-Type: multipart/related;',
b' type="application/xop+xml";',
b'\tboundary="example"',
b'Cache-Control: no-cache'))
# With strict=True, the header value that spans across
# multiple lines does get parsed fully.
dct = {
b'Content-Type': [
b'\r\n'.join((b'multipart/related;',
b' type="application/xop+xml";',
b'\tboundary="example"'))
],
b'Cache-Control': [b'no-cache']}
self.assertEqual(headers_raw_to_dict(raw, strict=True), dct)

def test_headers_dict_to_raw(self):
dct = OrderedDict([
(b'Content-type', b'text/html'),
Expand Down
23 changes: 21 additions & 2 deletions w3lib/http.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from base64 import urlsafe_b64encode


def headers_raw_to_dict(headers_raw):
def headers_raw_to_dict(headers_raw, strict=False):
r"""
Convert raw headers (single multi-line bytestring)
to a dictionary.

`strict` is a bool parameter controlling the multi-line parsing behavior.
If 'True', only the character sequence '\r\n' is considered as the line
delimiter, as per the HTTP specification (e.g., RFC 2616). If 'False'
(default), lines are delimited by 'str.splitlines()' and a wider range
of character(s) are considered as line boundaries.

For example:

>>> import w3lib.http
Expand All @@ -27,7 +33,20 @@ def headers_raw_to_dict(headers_raw):

if headers_raw is None:
return None
headers = headers_raw.splitlines()

if strict:
headers = []
for line in headers_raw.split(b'\r\n'):
if line.startswith(b' ') or line.startswith(b'\t'):
try:
headers[-1] += (b'\r\n' + line)
okomestudio marked this conversation as resolved.
Show resolved Hide resolved
except IndexError:
raise ValueError('Malformed raw headers')
else:
headers.append(line)
else:
headers = headers_raw.splitlines()

headers_tuples = [header.split(b':', 1) for header in headers]

result_dict = {}
Expand Down