Skip to content

Commit e2b7feb

Browse files
authored
Merge pull request #85 from dmippolitov/master
[MRG+1] fix headers_raw_to_dict multiple headers with one name issue
2 parents f46b4c4 + c3e61bc commit e2b7feb

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

tests/test_http.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ def test_headers_raw_dict_none(self):
1919
self.assertIsNone(headers_dict_to_raw(None))
2020

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

2628
def test_headers_dict_to_raw(self):

w3lib/http.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,21 @@ def headers_raw_to_dict(headers_raw):
2929
return None
3030
headers = headers_raw.splitlines()
3131
headers_tuples = [header.split(b':', 1) for header in headers]
32-
return dict([
33-
(header_item[0].strip(), [header_item[1].strip()])
34-
for header_item in headers_tuples
35-
if len(header_item) == 2
36-
])
32+
33+
result_dict = {}
34+
for header_item in headers_tuples:
35+
if not len(header_item) == 2:
36+
continue
37+
38+
item_key = header_item[0].strip()
39+
item_value = header_item[1].strip()
40+
41+
if item_key in result_dict:
42+
result_dict[item_key].append(item_value)
43+
else:
44+
result_dict[item_key] = [item_value]
45+
46+
return result_dict
3747

3848

3949
def headers_dict_to_raw(headers_dict):

0 commit comments

Comments
 (0)