Patch #1698 by Senthil: allow '@' in username when parsed by urlparse.py.

This commit is contained in:
Guido van Rossum 2008-01-05 01:21:57 +00:00
parent 3b83549ea0
commit ced4eb06e4
3 changed files with 24 additions and 4 deletions

View File

@ -254,6 +254,24 @@ class UrlParseTestCase(unittest.TestCase):
self.assertEqual(p.port, 80) self.assertEqual(p.port, 80)
self.assertEqual(p.geturl(), url) self.assertEqual(p.geturl(), url)
# Addressing issue1698, which suggests Username can contain
# "@" character. Though not RFC complaint, many ftp sites allow
# and requests email ids as usernames.
url = "http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag"
p = urlparse.urlsplit(url)
self.assertEqual(p.scheme, "http")
self.assertEqual(p.netloc, "User@example.com:Pass@www.python.org:080")
self.assertEqual(p.path, "/doc/")
self.assertEqual(p.query, "query=yes")
self.assertEqual(p.fragment, "frag")
self.assertEqual(p.username, "User@example.com")
self.assertEqual(p.password, "Pass")
self.assertEqual(p.hostname, "www.python.org")
self.assertEqual(p.port, 80)
self.assertEqual(p.geturl(), url)
def test_attributes_bad_port(self): def test_attributes_bad_port(self):
"""Check handling of non-integer ports.""" """Check handling of non-integer ports."""
p = urlparse.urlsplit("http://www.example.net:foo") p = urlparse.urlsplit("http://www.example.net:foo")

View File

@ -82,7 +82,7 @@ class BaseResult(tuple):
def username(self): def username(self):
netloc = self.netloc netloc = self.netloc
if "@" in netloc: if "@" in netloc:
userinfo = netloc.split("@", 1)[0] userinfo = netloc.rsplit("@", 1)[0]
if ":" in userinfo: if ":" in userinfo:
userinfo = userinfo.split(":", 1)[0] userinfo = userinfo.split(":", 1)[0]
return userinfo return userinfo
@ -92,7 +92,7 @@ class BaseResult(tuple):
def password(self): def password(self):
netloc = self.netloc netloc = self.netloc
if "@" in netloc: if "@" in netloc:
userinfo = netloc.split("@", 1)[0] userinfo = netloc.rsplit("@", 1)[0]
if ":" in userinfo: if ":" in userinfo:
return userinfo.split(":", 1)[1] return userinfo.split(":", 1)[1]
return None return None
@ -101,7 +101,7 @@ class BaseResult(tuple):
def hostname(self): def hostname(self):
netloc = self.netloc netloc = self.netloc
if "@" in netloc: if "@" in netloc:
netloc = netloc.split("@", 1)[1] netloc = netloc.rsplit("@", 1)[1]
if ":" in netloc: if ":" in netloc:
netloc = netloc.split(":", 1)[0] netloc = netloc.split(":", 1)[0]
return netloc.lower() or None return netloc.lower() or None
@ -110,7 +110,7 @@ class BaseResult(tuple):
def port(self): def port(self):
netloc = self.netloc netloc = self.netloc
if "@" in netloc: if "@" in netloc:
netloc = netloc.split("@", 1)[1] netloc = netloc.rsplit("@", 1)[1]
if ":" in netloc: if ":" in netloc:
port = netloc.split(":", 1)[1] port = netloc.split(":", 1)[1]
return int(port, 10) return int(port, 10)

View File

@ -342,6 +342,8 @@ Core and builtins
Library Library
------- -------
- Patch #1698: allow '@' in username parsed by urlparse.py.
- Issue #1735: TarFile.extractall() now correctly sets directory permissions - Issue #1735: TarFile.extractall() now correctly sets directory permissions
and times. and times.