Commit 40e6a451 authored by Gregory P. Smith's avatar Gregory P. Smith

Merged revisions 82985 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r82985 | gregory.p.smith | 2010-07-19 16:17:22 -0700 (Mon, 19 Jul 2010) | 3 lines

  Fixes Issue #3704: http.cookiejar was not properly handling URLs with a / in
  the parameters.  (This is jjlee's issue3704.patch ported to py3k)
........
parent b1d09829
...@@ -603,17 +603,14 @@ def eff_request_host(request): ...@@ -603,17 +603,14 @@ def eff_request_host(request):
return req_host, erhn return req_host, erhn
def request_path(request): 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() url = request.get_full_url()
path, parameters, query, frag = urllib.parse.urlparse(url)[2:] parts = urllib.parse.urlsplit(url)
if parameters: path = escape_path(parts.path)
path = "%s;%s" % (path, parameters) if not path.startswith("/"):
path = escape_path(path)
req_path = urllib.parse.urlunparse(("", "", path, "", query, frag))
if not req_path.startswith("/"):
# fix bad RFC 2396 absoluteURI # fix bad RFC 2396 absoluteURI
req_path = "/"+req_path path = "/" + path
return req_path return path
def request_port(request): def request_port(request):
host = request.get_host() host = request.get_host()
......
"""Tests for http/cookiejar.py.""" """Tests for http/cookiejar.py."""
import re, os, time, urllib.request import os
from unittest import TestCase import re
import test.support
import time
import unittest
import urllib.request
from test import support from http.cookiejar import (time2isoz, http2time, time2netscape,
parse_ns_headers, join_header_words, split_header_words, Cookie,
CookieJar, DefaultCookiePolicy, LWPCookieJar, MozillaCookieJar,
LoadError, lwp_cookie_str, DEFAULT_HTTP_PORT, escape_path,
reach, is_HDN, domain_match, user_domain_match, request_path,
request_port, request_host)
from http.cookiejar import time2isoz, http2time, time2netscape, \
parse_ns_headers, join_header_words, split_header_words, Cookie, \
CookieJar, DefaultCookiePolicy, LWPCookieJar, MozillaCookieJar, \
LoadError, lwp_cookie_str, DEFAULT_HTTP_PORT, escape_path, \
reach, is_HDN, domain_match, user_domain_match, request_path, \
request_port, request_host
class DateTimeTests(TestCase): class DateTimeTests(unittest.TestCase):
def test_time2isoz(self): def test_time2isoz(self):
base = 1019227000 base = 1019227000
...@@ -96,7 +99,7 @@ class DateTimeTests(TestCase): ...@@ -96,7 +99,7 @@ class DateTimeTests(TestCase):
) )
class HeaderTests(TestCase): class HeaderTests(unittest.TestCase):
def test_parse_ns_headers(self): def test_parse_ns_headers(self):
# quotes should be stripped # quotes should be stripped
expected = [[('foo', 'bar'), ('expires', 2209069412), ('version', '0')]] expected = [[('foo', 'bar'), ('expires', 2209069412), ('version', '0')]]
...@@ -217,10 +220,10 @@ def _interact(cookiejar, url, set_cookie_hdrs, hdr_name): ...@@ -217,10 +220,10 @@ def _interact(cookiejar, url, set_cookie_hdrs, hdr_name):
return cookie_hdr return cookie_hdr
class FileCookieJarTests(TestCase): class FileCookieJarTests(unittest.TestCase):
def test_lwp_valueless_cookie(self): def test_lwp_valueless_cookie(self):
# cookies with no value should be saved and loaded consistently # cookies with no value should be saved and loaded consistently
filename = support.TESTFN filename = test.support.TESTFN
c = LWPCookieJar() c = LWPCookieJar()
interact_netscape(c, "http://www.acme.com/", 'boo') interact_netscape(c, "http://www.acme.com/", 'boo')
self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None) self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
...@@ -235,7 +238,7 @@ class FileCookieJarTests(TestCase): ...@@ -235,7 +238,7 @@ class FileCookieJarTests(TestCase):
def test_bad_magic(self): def test_bad_magic(self):
# IOErrors (eg. file doesn't exist) are allowed to propagate # IOErrors (eg. file doesn't exist) are allowed to propagate
filename = support.TESTFN filename = test.support.TESTFN
for cookiejar_class in LWPCookieJar, MozillaCookieJar: for cookiejar_class in LWPCookieJar, MozillaCookieJar:
c = cookiejar_class() c = cookiejar_class()
try: try:
...@@ -258,7 +261,7 @@ class FileCookieJarTests(TestCase): ...@@ -258,7 +261,7 @@ class FileCookieJarTests(TestCase):
try: os.unlink(filename) try: os.unlink(filename)
except OSError: pass except OSError: pass
class CookieTests(TestCase): class CookieTests(unittest.TestCase):
# XXX # XXX
# Get rid of string comparisons where not actually testing str / repr. # Get rid of string comparisons where not actually testing str / repr.
# .clear() etc. # .clear() etc.
...@@ -338,7 +341,7 @@ class CookieTests(TestCase): ...@@ -338,7 +341,7 @@ class CookieTests(TestCase):
def test_missing_value(self): def test_missing_value(self):
# missing = sign in Cookie: header is regarded by Mozilla as a missing # missing = sign in Cookie: header is regarded by Mozilla as a missing
# name, and by http.cookiejar as a missing value # name, and by http.cookiejar as a missing value
filename = support.TESTFN filename = test.support.TESTFN
c = MozillaCookieJar(filename) c = MozillaCookieJar(filename)
interact_netscape(c, "http://www.acme.com/", 'eggs') interact_netscape(c, "http://www.acme.com/", 'eggs')
interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/') interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')
...@@ -522,6 +525,16 @@ class CookieTests(TestCase): ...@@ -522,6 +525,16 @@ class CookieTests(TestCase):
interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"') interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"')
self.assertTrue("/blah/rhubarb" in c._cookies["www.acme.com"]) self.assertTrue("/blah/rhubarb" in c._cookies["www.acme.com"])
def test_default_path_with_query(self):
cj = 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): def test_escape_path(self):
cases = [ cases = [
# quoted safe # quoted safe
...@@ -550,16 +563,15 @@ class CookieTests(TestCase): ...@@ -550,16 +563,15 @@ class CookieTests(TestCase):
def test_request_path(self): def test_request_path(self):
# with parameters # with parameters
req = urllib.request.Request( req = urllib.request.Request(
"http://www.example.com/rheum/rhaponicum;" "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") "foo=bar;sing=song?apples=pears&spam=eggs#ni")
self.assertEquals(request_path(req),
"/rheum/rhaponticum;foo=bar;sing=song")
# without parameters # without parameters
req = urllib.request.Request( req = urllib.request.Request(
"http://www.example.com/rheum/rhaponicum?" "http://www.example.com/rheum/rhaponticum?"
"apples=pears&spam=eggs#ni")
self.assertEquals(request_path(req), "/rheum/rhaponicum?"
"apples=pears&spam=eggs#ni") "apples=pears&spam=eggs#ni")
self.assertEquals(request_path(req), "/rheum/rhaponticum")
# missing final slash # missing final slash
req = urllib.request.Request("http://www.example.com") req = urllib.request.Request("http://www.example.com")
self.assertEquals(request_path(req), "/") self.assertEquals(request_path(req), "/")
...@@ -1033,7 +1045,7 @@ class CookieTests(TestCase): ...@@ -1033,7 +1045,7 @@ class CookieTests(TestCase):
self.assertTrue(cookie.expires is None) self.assertTrue(cookie.expires is None)
class LWPCookieTests(TestCase): class LWPCookieTests(unittest.TestCase):
# Tests taken from libwww-perl, with a few modifications and additions. # Tests taken from libwww-perl, with a few modifications and additions.
def test_netscape_example_1(self): def test_netscape_example_1(self):
...@@ -1423,7 +1435,7 @@ class LWPCookieTests(TestCase): ...@@ -1423,7 +1435,7 @@ class LWPCookieTests(TestCase):
self.assertEquals(len(c), 6) self.assertEquals(len(c), 6)
# save and restore # save and restore
filename = support.TESTFN filename = test.support.TESTFN
try: try:
c.save(filename, ignore_discard=True) c.save(filename, ignore_discard=True)
...@@ -1462,7 +1474,7 @@ class LWPCookieTests(TestCase): ...@@ -1462,7 +1474,7 @@ class LWPCookieTests(TestCase):
# Save / load Mozilla/Netscape cookie file format. # Save / load Mozilla/Netscape cookie file format.
year_plus_one = time.localtime()[0] + 1 year_plus_one = time.localtime()[0] + 1
filename = support.TESTFN filename = test.support.TESTFN
c = MozillaCookieJar(filename, c = MozillaCookieJar(filename,
policy=DefaultCookiePolicy(rfc2965=True)) policy=DefaultCookiePolicy(rfc2965=True))
...@@ -1624,7 +1636,7 @@ class LWPCookieTests(TestCase): ...@@ -1624,7 +1636,7 @@ class LWPCookieTests(TestCase):
def test_main(verbose=None): def test_main(verbose=None):
support.run_unittest( test.support.run_unittest(
DateTimeTests, DateTimeTests,
HeaderTests, HeaderTests,
CookieTests, CookieTests,
......
...@@ -78,6 +78,9 @@ C-API ...@@ -78,6 +78,9 @@ C-API
Library Library
------- -------
- Issue #3704: http.cookiejar was not properly handling URLs with a / in the
parameters.
- Issue #4629: getopt raises an error if an argument ends with = whereas getopt - Issue #4629: getopt raises an error if an argument ends with = whereas getopt
doesn't except a value (eg. --help= is rejected if getopt uses ['help='] long doesn't except a value (eg. --help= is rejected if getopt uses ['help='] long
options). options).
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment