[Python-bugs-list] [ python-Bugs-831271 ] httplib.HTTPConnection._send_request header parsing bug

SourceForge.net noreply at sourceforge.net
Sun Nov 2 11:51:57 EST 2003


Bugs item #831271, was opened at 2003-10-27 20:57
Message generated for change (Comment added) made by aleax
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=831271&group_id=5470

>Category: Python Library
Group: Python 2.3
Status: Open
>Resolution: Fixed
Priority: 5
Submitted By: Jason R. Coombs (jaraco)
>Assigned to: Alex Martelli (aleax)
Summary: httplib.HTTPConnection._send_request header parsing bug

Initial Comment:
The test on lines 730-731 of httplib.py as released with 
Python 2.3.2 doesn't do what it's intended to do.

Consider
>>> headers = { 'hoST': 'www.someplace.org' }
>>> 'Host' in ( headers or [k for k in headers.iterkeys() if 
k.lower() == 'host' ] )
False

This sample code demonstrates that the code in httplib 
at line 730 doesn't work as intended (it should return 
true for any dict who's keys include 'host' of any case).

Clearly the 'or' syntax has confused someone here, 
because the portion after the or (if executed) is always 
an empty list.  I recommend instead

        if 'host' in map( str.lower, headers.keys() ):

Or a better general solution might be to force all header 
keys to be case-insensitive strings by overriding str and 
dict to new case-insensitive versions, something like the 
attached.  This idea, however, is just a suggestion, and 
probably needs to be thought through more thoroughly.

----------------------------------------------------------------------

>Comment By: Alex Martelli (aleax)
Date: 2003-11-02 17:51

Message:
Logged In: YES 
user_id=60314

You're certainly right that lines 730-731 cannot be correct, for exactly the reason you specify; and that forcing case-insensitive header dicts may be a cool idea but it's too invasive for such a simple fix.  I've done some timing and I think the simplest and fastest way to check is:
if 'host' in [k.lower() for k in headers]:
accordingly, I have committed this change to the 2.3 branch in CVS.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=831271&group_id=5470



More information about the Python-bugs-list mailing list