]> granicus.if.org Git - python/commitdiff
Issue #2776: fixed small issue when handling an URL with double slash
authorFacundo Batista <facundobatista@gmail.com>
Sat, 16 Aug 2008 14:44:07 +0000 (14:44 +0000)
committerFacundo Batista <facundobatista@gmail.com>
Sat, 16 Aug 2008 14:44:07 +0000 (14:44 +0000)
after a 302 response in the case of not going through a proxy.

Lib/test/test_urllib2.py
Lib/urllib2.py
Misc/NEWS

index 1e93fdbed8af396ed27988494b8901b97a1d1270..e28ee71a840da81c4f140406f51c22f7d5d3082d 100644 (file)
@@ -772,6 +772,32 @@ class HandlerTests(unittest.TestCase):
             self.assertEqual(req.unredirected_hdrs["Host"], "baz")
             self.assertEqual(req.unredirected_hdrs["Spam"], "foo")
 
+    def test_http_doubleslash(self):
+        # Checks that the presence of an unnecessary double slash in a url doesn't break anything
+        # Previously, a double slash directly after the host could cause incorrect parsing of the url
+        h = urllib2.AbstractHTTPHandler()
+        o = h.parent = MockOpener()
+
+        data = ""
+        ds_urls = [
+            "http://example.com/foo/bar/baz.html",
+            "http://example.com//foo/bar/baz.html",
+            "http://example.com/foo//bar/baz.html",
+            "http://example.com/foo/bar//baz.html",
+        ]
+
+        for ds_url in ds_urls:
+            ds_req = Request(ds_url, data)
+
+            # Check whether host is determined correctly if there is no proxy
+            np_ds_req = h.do_request_(ds_req)
+            self.assertEqual(np_ds_req.unredirected_hdrs["Host"],"example.com")
+
+            # Check whether host is determined correctly if there is a proxy
+            ds_req.set_proxy("someproxy:3128",None)
+            p_ds_req = h.do_request_(ds_req)
+            self.assertEqual(p_ds_req.unredirected_hdrs["Host"],"example.com")
+
     def test_errors(self):
         h = urllib2.HTTPErrorProcessor()
         o = h.parent = MockOpener()
index 156c6f8b0fd962937ed2a5dead69d5a235e44ea4..121685c0a8dae48c174023552f8ed901adfecb3f 100644 (file)
@@ -255,6 +255,9 @@ class Request:
         self.host, self.type = host, type
         self.__r_host = self.__original
 
+    def has_proxy(self):
+        return self.__r_host == self.__original
+
     def get_origin_req_host(self):
         return self.origin_req_host
 
@@ -1045,10 +1048,13 @@ class AbstractHTTPHandler(BaseHandler):
                 request.add_unredirected_header(
                     'Content-length', '%d' % len(data))
 
-        scheme, sel = splittype(request.get_selector())
-        sel_host, sel_path = splithost(sel)
+        sel_host = host
+        if request.has_proxy():
+            scheme, sel = splittype(request.get_selector())
+            sel_host, sel_path = splithost(sel)
+
         if not request.has_header('Host'):
-            request.add_unredirected_header('Host', sel_host or host)
+            request.add_unredirected_header('Host', sel_host)
         for name, value in self.parent.addheaders:
             name = name.capitalize()
             if not request.has_header(name):
index de572d1f38b9c0f84e3007d132e2ba9f15c218cc..0a16e8f89e2d19c58662b8a91f607c8b34e117a6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -48,6 +48,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #2776: fixed small issue when handling an URL with double slash
+  after a 302 response in the case of not going through a proxy.
+
 - Issue #2676: in the email package, content-type parsing was hanging on
   pathological input because of quadratic or exponential behaviour of a
   regular expression.