[New-bugs-announce] [issue2275] urllib2 header capitalization

Hans-Peter Jansen report at bugs.python.org
Tue Mar 11 22:22:20 CET 2008


New submission from Hans-Peter Jansen <hpj at urpla.net>:

The urllib2 behavior related to headers is - hmm - improvable. 
It simply capitalize() the key, which leads to funny results like:
Accept-charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
while this is seemingly conforming to the specs, it's simply different 
to every other implementation of such things.. 

And we can do better. How about:
--- /usr/lib/python/urllib2.py  2008-01-10 19:03:55.000000000 +0100
+++ urllib2.py  2008-03-11 21:25:33.523890670 +0100
@@ -261,13 +261,16 @@ class Request:
     def is_unverifiable(self):
         return self.unverifiable

+    def _cap_header_key(self, key):
+        return '-'.join((ck.capitalize() for ck in key.split('-')))
+
     def add_header(self, key, val):
         # useful for something like authentication
-        self.headers[key.capitalize()] = val
+        self.headers[self._cap_header_key(key)] = val

     def add_unredirected_header(self, key, val):
         # will not be added to a redirected request
-        self.unredirected_hdrs[key.capitalize()] = val
+        self.unredirected_hdrs[self._cap_header_key(key)] = val

     def has_header(self, header_name):
         return (header_name in self.headers or

I'm not happy with the _cap_header_key name, but you get the idea.
The patch is optimized to operate with maximum locality. It's also 
attached.

I would be very grateful, if something similar could be applied.

Opinions?

----------
components: Library (Lib)
files: urllib2-cap-headers.diff
keywords: patch
messages: 63466
nosy: frispete
severity: minor
status: open
title: urllib2 header capitalization
type: behavior
versions: Python 2.5
Added file: http://bugs.python.org/file9658/urllib2-cap-headers.diff

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2275>
__________________________________


More information about the New-bugs-announce mailing list