Skip to content

Commit fee0e19

Browse files
committed
python-ecosys/requests: Make response headers dict lowercase.
The HTTP response headers are case-insensitive. But the old implementation put them in a dictionary keyed by case-sensitive header name. The only way to correctly get a header would have been to iterate through the dictionary looking for it, which is slow and a lot of code and I doubt anyone bothered, I expect that existing code probably just did a case-sensitive match. That's fragile, server changes could break it. To fix that, lowercase all the header names before putting them in the dictionary. That way, clients can just access the dictionary with the lowercase header name and it will do the right thing. This is backward compatible with correctly-written existing code. But it's not compatible with existing code that was buggy anyway and did a case-sensitive match.
1 parent 63e3894 commit fee0e19

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

python-ecosys/requests/requests/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ def request(
204204
elif parse_headers is True:
205205
l = str(l, "utf-8")
206206
k, v = l.split(":", 1)
207-
resp_d[k] = v.strip()
207+
# Headers are case insensitive, so we lowercase them.
208+
# This avoids having to do a linear case-insensitive search
209+
# through the dictionary later.
210+
resp_d[k.lower()] = v.strip()
208211
else:
209212
parse_headers(l, resp_d)
210213
except OSError:

0 commit comments

Comments
 (0)