[Python-checkins] r83145 - in python/branches/release27-maint: Lib/cookielib.py Lib/test/test_cookielib.py Misc/NEWS

gregory.p.smith python-checkins at python.org
Sun Jul 25 21:11:36 CEST 2010


Author: gregory.p.smith
Date: Sun Jul 25 21:11:36 2010
New Revision: 83145

Log:
Fixes issue #3704: cookielib was not properly handling URLs with a / in the
parameters.


Modified:
   python/branches/release27-maint/Lib/cookielib.py
   python/branches/release27-maint/Lib/test/test_cookielib.py
   python/branches/release27-maint/Misc/NEWS

Modified: python/branches/release27-maint/Lib/cookielib.py
==============================================================================
--- python/branches/release27-maint/Lib/cookielib.py	(original)
+++ python/branches/release27-maint/Lib/cookielib.py	Sun Jul 25 21:11:36 2010
@@ -607,19 +607,14 @@
     return req_host, erhn
 
 def request_path(request):
-    """request-URI, as defined by RFC 2965."""
+    """Path component of request-URI, as defined by RFC 2965."""
     url = request.get_full_url()
-    #scheme, netloc, path, parameters, query, frag = urlparse.urlparse(url)
-    #req_path = escape_path("".join(urlparse.urlparse(url)[2:]))
-    path, parameters, query, frag = urlparse.urlparse(url)[2:]
-    if parameters:
-        path = "%s;%s" % (path, parameters)
-    path = escape_path(path)
-    req_path = urlparse.urlunparse(("", "", path, "", query, frag))
-    if not req_path.startswith("/"):
+    parts = urlparse.urlsplit(url)
+    path = escape_path(parts.path)
+    if not path.startswith("/"):
         # fix bad RFC 2396 absoluteURI
-        req_path = "/"+req_path
-    return req_path
+        path = "/" + path
+    return path
 
 def request_port(request):
     host = request.get_host()

Modified: python/branches/release27-maint/Lib/test/test_cookielib.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_cookielib.py	(original)
+++ python/branches/release27-maint/Lib/test/test_cookielib.py	Sun Jul 25 21:11:36 2010
@@ -1,11 +1,16 @@
 # -*- coding: latin-1 -*-
 """Tests for cookielib.py."""
 
-import re, os, time
+import cookielib
+import os
+import re
+import time
+
 from unittest import TestCase
 
 from test import test_support
 
+
 class DateTimeTests(TestCase):
 
     def test_time2isoz(self):
@@ -563,6 +568,16 @@
         interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"')
         self.assertIn("/blah/rhubarb", c._cookies["www.acme.com"])
 
+    def test_default_path_with_query(self):
+        cj = cookielib.CookieJar()
+        uri = "http://example.com/?spam/eggs"
+        value = 'eggs="bar"'
+        interact_netscape(cj, uri, value)
+        # default path does not include query, so is "/", not "/?spam"
+        self.assertIn("/", cj._cookies["example.com"])
+        # cookie is sent back to the same URI
+        self.assertEquals(interact_netscape(cj, uri), value)
+
     def test_escape_path(self):
         from cookielib import escape_path
         cases = [
@@ -591,15 +606,14 @@
         from urllib2 import Request
         from cookielib import request_path
         # with parameters
-        req = Request("http://www.example.com/rheum/rhaponicum;"
+        req = Request("http://www.example.com/rheum/rhaponticum;"
                       "foo=bar;sing=song?apples=pears&spam=eggs#ni")
-        self.assertEquals(request_path(req), "/rheum/rhaponicum;"
-                     "foo=bar;sing=song?apples=pears&spam=eggs#ni")
+        self.assertEquals(request_path(req),
+                          "/rheum/rhaponticum;foo=bar;sing=song")
         # without parameters
-        req = Request("http://www.example.com/rheum/rhaponicum?"
+        req = Request("http://www.example.com/rheum/rhaponticum?"
                       "apples=pears&spam=eggs#ni")
-        self.assertEquals(request_path(req), "/rheum/rhaponicum?"
-                     "apples=pears&spam=eggs#ni")
+        self.assertEquals(request_path(req), "/rheum/rhaponticum")
         # missing final slash
         req = Request("http://www.example.com")
         self.assertEquals(request_path(req), "/")

Modified: python/branches/release27-maint/Misc/NEWS
==============================================================================
--- python/branches/release27-maint/Misc/NEWS	(original)
+++ python/branches/release27-maint/Misc/NEWS	Sun Jul 25 21:11:36 2010
@@ -18,6 +18,9 @@
 Library
 -------
 
+- Issue #3704: cookielib was not properly handling URLs with a / in the
+  parameters.
+
 - Issue #9032: XML-RPC client retries the request on EPIPE error. The EPIPE
   error occurs when the server closes the socket and the client sends a big
   XML-RPC request.


More information about the Python-checkins mailing list