[wwwsearch-commits] r21906 - in wwwsearch/ClientCookie/branch/1.1.1-seek-fix: ClientCookie test

jjlee at codespeak.net jjlee at codespeak.net
Tue Jan 10 23:13:07 CET 2006


Author: jjlee
Date: Tue Jan 10 23:13:05 2006
New Revision: 21906

Modified:
   wwwsearch/ClientCookie/branch/1.1.1-seek-fix/ClientCookie/_urllib2_support.py
   wwwsearch/ClientCookie/branch/1.1.1-seek-fix/test/test_urllib2.py
Log:
Fix Refresh handling in case without explicit URL

Modified: wwwsearch/ClientCookie/branch/1.1.1-seek-fix/ClientCookie/_urllib2_support.py
==============================================================================
--- wwwsearch/ClientCookie/branch/1.1.1-seek-fix/ClientCookie/_urllib2_support.py	(original)
+++ wwwsearch/ClientCookie/branch/1.1.1-seek-fix/ClientCookie/_urllib2_support.py	Tue Jan 10 23:13:05 2006
@@ -18,6 +18,7 @@
 from _Util import isstringlike, startswith, getheaders
 from _HeadersUtil import is_html
 from _Debug import getLogger
+debug = getLogger("ClientCookie.cookies").debug
 
 try: True
 except NameError:
@@ -499,21 +500,25 @@
 
             if code == 200 and hdrs.has_key("refresh"):
                 refresh = getheaders(hdrs, "refresh")[0]
-                i = string.find(refresh, ";")
-                if i != -1:
-                    pause, newurl_spec = refresh[:i], refresh[i+1:]
-                    i = string.find(newurl_spec, "=")
-                    if i != -1:
-                        pause = int(pause)
-                        if (self.max_time is None) or (pause <= self.max_time):
-                            if pause != 0 and self.honor_time:
-                                time.sleep(pause)
-                            newurl = newurl_spec[i+1:]
-                            hdrs["location"] = newurl
-                            # hardcoded http is NOT a bug
-                            response = self.parent.error(
-                                "http", request, response,
-                                "refresh", msg, hdrs)
+                ii = string.find(refresh, ";")
+                if ii != -1:
+                    pause, newurl_spec = int(refresh[:ii]), refresh[ii+1:]
+                    jj = string.find(newurl_spec, "=")
+                    if jj != -1:
+                        key, newurl = newurl_spec[:jj], newurl_spec[jj+1:]
+                    if key.strip() != "url":
+                        debug("bad Refresh header: %r" % refresh)
+                        return response
+                else:
+                    pause, newurl = int(refresh), response.geturl()
+                if (self.max_time is None) or (pause <= self.max_time):
+                    if pause != 0 and self.honor_time:
+                        time.sleep(pause)
+                    hdrs["location"] = newurl
+                    # hardcoded http is NOT a bug
+                    response = self.parent.error(
+                        "http", request, response,
+                        "refresh", msg, hdrs)
 
             return response
 

Modified: wwwsearch/ClientCookie/branch/1.1.1-seek-fix/test/test_urllib2.py
==============================================================================
--- wwwsearch/ClientCookie/branch/1.1.1-seek-fix/test/test_urllib2.py	(original)
+++ wwwsearch/ClientCookie/branch/1.1.1-seek-fix/test/test_urllib2.py	Tue Jan 10 23:13:05 2006
@@ -685,16 +685,17 @@
         self.assert_(headers["Foo"] == "Bar")
 
     def test_refresh(self):
-        # XXX processor constructor optional args
-        h = HTTPRefreshProcessor()
-        o = h.parent = MockOpener()
+        # XXX test processor constructor optional args
+        h = HTTPRefreshProcessor(max_time=None, honor_time=False)
 
-        req = Request("http://example.com/")
-        headers = MockHeaders({"refresh": '0; url="http://example.com/foo/"'})
-        r = MockResponse(200, "OK", headers, "")
-        newr = h.http_response(req, r)
-        self.assert_(o.proto == "http")
-        self.assert_(o.args == (req, r, "refresh", "OK", headers))
+        for val in ['0; url="http://example.com/foo/"', "2"]:
+            o = h.parent = MockOpener()
+            req = Request("http://example.com/")
+            headers = MockHeaders({"refresh": val})
+            r = MockResponse(200, "OK", headers, "")
+            newr = h.http_response(req, r)
+            self.assertEqual(o.proto, "http")
+            self.assertEqual(o.args, (req, r, "refresh", "OK", headers))
 
     def test_redirect(self):
         from_url = "http://example.com/a.html"


More information about the wwwsearch-commits mailing list