diff -r 66d0f6ef2a7f Lib/test/test_urllib2net.py --- a/Lib/test/test_urllib2net.py Thu Feb 28 18:03:16 2013 +0200 +++ b/Lib/test/test_urllib2net.py Sat Mar 02 22:05:02 2013 -0500 @@ -177,6 +177,22 @@ opener.open(request) self.assertEqual(request.get_header('User-agent'),'Test-Agent') + def test_headers_with_spaces(self): + url = "http://www.example.com" + with support.transient_internet(url): + opener = urllib.request.build_opener() + request = urllib.request.Request(url) + # testing trailing space to be removed + request.add_header('Footrail ', 'bartrail') + self.assertTrue(request.headers['Footrail'], 'bartrail') + request.add_unredirected_header('Footrailu ', 'bartrailu') + self.assertTrue(request.unredirected_hdrs['Footrailu'], 'bartrailu') + # testing leading space to be removed + request.add_header(' Foolead', 'barlead') + self.assertTrue(request.headers['Foolead'], 'barlead') + request.add_unredirected_header(' Fooleadu', 'barleadu') + self.assertTrue(request.unredirected_hdrs['Fooleadu'], 'barleadu') + def test_sites_no_connection_close(self): # Some sites do not send Connection: close header. # Verify that those work properly. (#issue12576) diff -r 66d0f6ef2a7f Lib/urllib/request.py --- a/Lib/urllib/request.py Thu Feb 28 18:03:16 2013 +0200 +++ b/Lib/urllib/request.py Sat Mar 02 22:05:02 2013 -0500 @@ -358,11 +358,11 @@ def add_header(self, key, val): # useful for something like authentication - self.headers[key.capitalize()] = val + self.headers[key.strip().capitalize()] = 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[key.strip().capitalize()] = val def has_header(self, header_name): return (header_name in self.headers or