[Python-3000-checkins] r65711 - in python/branches/py3k/Lib: test/test_urllib2.py urllib/request.py

facundo.batista python-3000-checkins at python.org
Sat Aug 16 16:44:33 CEST 2008


Author: facundo.batista
Date: Sat Aug 16 16:44:32 2008
New Revision: 65711

Log:

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.


Modified:
   python/branches/py3k/Lib/test/test_urllib2.py
   python/branches/py3k/Lib/urllib/request.py

Modified: python/branches/py3k/Lib/test/test_urllib2.py
==============================================================================
--- python/branches/py3k/Lib/test/test_urllib2.py	(original)
+++ python/branches/py3k/Lib/test/test_urllib2.py	Sat Aug 16 16:44:32 2008
@@ -770,6 +770,34 @@
             self.assertEqual(req.unredirected_hdrs["Host"], "baz")
             self.assertEqual(req.unredirected_hdrs["Spam"], "foo")
 
+    def test_http_doubleslash(self):
+        # Checks the presence of any unnecessary double slash in url does not
+        # break anything. Previously, a double slash directly after the host
+        # could could cause incorrect parsing.
+        h = urllib.request.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 = urllib.request.HTTPErrorProcessor()
         o = h.parent = MockOpener()

Modified: python/branches/py3k/Lib/urllib/request.py
==============================================================================
--- python/branches/py3k/Lib/urllib/request.py	(original)
+++ python/branches/py3k/Lib/urllib/request.py	Sat Aug 16 16:44:32 2008
@@ -231,6 +231,9 @@
         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
 
@@ -1010,10 +1013,12 @@
                 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):


More information about the Python-3000-checkins mailing list