[Patches] [ python-Patches-649742 ] urllib2.Request's headers are case-sens.

SourceForge.net noreply@sourceforge.net
Sat, 10 May 2003 04:55:03 -0700


Patches item #649742, was opened at 2002-12-06 21:26
Message generated for change (Comment added) made by jjlee
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=649742&group_id=5470

Category: Library (Lib)
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: John J Lee (jjlee)
Assigned to: Brett Cannon (bcannon)
Summary: urllib2.Request's headers are case-sens.

Initial Comment:
urllib2.Request's headers are case-sensitive.

This is unfortunate if, for example, you add a content-type header
like so:

req = urllib2.Request("http://blah/", data,
                      headers={"Content-Type": "text/ugly"})

because, while urllib2.AbstractHTTPHandler is careful to check not to
add this header if it's already in the Request, it happens to use a
different case convention:

                if not req.headers.has_key('Content-type'):
                    h.putheader('Content-type',
                                'application/x-www-form-urlencoded')

so you get both headers:

Content-Type: text/ugly
Content-type: application/x-www-form-urlencoded

in essentially random order.  The documentation says:

"""Note that there cannot be more than one header with the same name,
and later calls will overwrite previous calls in case the key
collides.  Currently, this is no loss of functionality, since all
headers which have meaning when used more than once have a
(header-specific) way of gaining the same functionality using only one
header."""

RFC 2616 (section 4.2) says:

"""The order in which header fields with the same field-name are
received is therefore significant to the interpretation of the
combined field value, and thus a proxy MUST NOT change the order of
these field values when a message is forwarded."""

The patch fixes this by adding normalisation of header case to
urllib.Request.  With the patch, you'd get:

Content-type: text/ugly


John


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

>Comment By: John J Lee (jjlee)
Date: 2003-05-10 12:55

Message:
Logged In: YES 
user_id=261020

Patch is attached (no doc changes required). 
 

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

Comment By: Brett Cannon (bcannon)
Date: 2003-05-10 00:46

Message:
Logged In: YES 
user_id=357491

OK.  If you can rewrite the patch then using capitalize I will take a look and 
decide whether to apply it or not.

Also, if this will require changes to the docs please also include a patch for 
that.

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

Comment By: John J Lee (jjlee)
Date: 2003-05-09 13:41

Message:
Logged In: YES 
user_id=261020

Ooh, look at all those string methods I'd forgotten about. 
 
Yes, good idea, but name.capitalize() would be simpler and minutely 
more conservative (the module already uses that convention), hence 
better. 
 

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

Comment By: Brett Cannon (bcannon)
Date: 2003-05-09 02:38

Message:
Logged In: YES 
user_id=357491

Do you think this would also work, John, if instead of having 
normalise_header_case you did name.title()?

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

Comment By: John J Lee (jjlee)
Date: 2002-12-08 17:18

Message:
Logged In: YES 
user_id=261020

Here it is.

I swear I did check the box.  I clicked the button twice, though --
I guess SF doesn't like that.


John


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

Comment By: Martin v. Löwis (loewis)
Date: 2002-12-07 08:43

Message:
Logged In: YES 
user_id=21627

There's no uploaded file!  You have to check the
checkbox labeled "Check to Upload & Attach File"
when you upload a file.

Please try again.

(This is a SourceForge annoyance that we can do
nothing about. :-( )

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

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