]> granicus.if.org Git - python/commitdiff
Fixes Issue 1401. When redirected, a possible POST get converted
authorFacundo Batista <facundobatista@gmail.com>
Thu, 7 Feb 2008 19:06:52 +0000 (19:06 +0000)
committerFacundo Batista <facundobatista@gmail.com>
Thu, 7 Feb 2008 19:06:52 +0000 (19:06 +0000)
to GET, so it loses its payload. So, it also must lose the
headers related to the payload (if it has no content any more,
it shouldn't indicate content length and type).

Lib/test/test_urllib2.py
Lib/urllib2.py

index 90e177173374933f376fa1d206618c14b42d57ee..a35cbfae842f34320cae7864915d0a44f34594d9 100644 (file)
@@ -822,6 +822,8 @@ class HandlerTests(unittest.TestCase):
                 method = getattr(h, "http_error_%s" % code)
                 req = Request(from_url, data)
                 req.add_header("Nonsense", "viking=withhold")
+                if data is not None:
+                    req.add_header("Content-Length", str(len(data)))
                 req.add_unredirected_header("Spam", "spam")
                 try:
                     method(req, MockFile(), code, "Blah",
@@ -834,6 +836,13 @@ class HandlerTests(unittest.TestCase):
                     self.assertEqual(o.req.get_method(), "GET")
                 except AttributeError:
                     self.assert_(not o.req.has_data())
+
+                # now it's a GET, there should not be headers regarding content
+                # (possibly dragged from before being a POST)
+                headers = [x.lower() for x in o.req.headers]
+                self.assertTrue("content-length" not in headers)
+                self.assertTrue("content-type" not in headers)
+
                 self.assertEqual(o.req.headers["Nonsense"],
                                  "viking=withhold")
                 self.assert_("Spam" not in o.req.headers)
index 8bf08848b155adc20c93bfeb20a1fc8afd8bf83e..d5a539d9db03035fc3a8ba2afe294297907097d0 100644 (file)
@@ -534,8 +534,11 @@ class HTTPRedirectHandler(BaseHandler):
             # do the same.
             # be conciliant with URIs containing a space
             newurl = newurl.replace(' ', '%20')
+            newheaders = dict((k,v) for k,v in req.headers.items()
+                              if k.lower() not in ("content-length", "content-type")
+                             )
             return Request(newurl,
-                           headers=req.headers,
+                           headers=newheaders,
                            origin_req_host=req.get_origin_req_host(),
                            unverifiable=True)
         else: