2000-02-04 11:28:42 -04:00
|
|
|
"""Parse (absolute and relative) URLs.
|
|
|
|
|
|
|
|
See RFC 1808: "Relative Uniform Resource Locators", by R. Fielding,
|
|
|
|
UC Irvine, June 1995.
|
|
|
|
"""
|
1994-09-12 07:36:35 -03:00
|
|
|
|
2002-10-16 18:21:39 -03:00
|
|
|
__all__ = ["urlparse", "urlunparse", "urljoin", "urldefrag",
|
|
|
|
"urlsplit", "urlunsplit"]
|
2001-03-01 00:27:19 -04:00
|
|
|
|
1994-09-12 07:36:35 -03:00
|
|
|
# A classification of schemes ('' means apply by default)
|
2004-05-07 02:50:35 -03:00
|
|
|
uses_relative = ['ftp', 'http', 'gopher', 'nntp', 'imap',
|
2006-01-20 13:24:23 -04:00
|
|
|
'wais', 'file', 'https', 'shttp', 'mms',
|
|
|
|
'prospero', 'rtsp', 'rtspu', '', 'sftp']
|
2004-05-07 02:50:35 -03:00
|
|
|
uses_netloc = ['ftp', 'http', 'gopher', 'nntp', 'telnet',
|
2006-01-20 13:24:23 -04:00
|
|
|
'imap', 'wais', 'file', 'mms', 'https', 'shttp',
|
|
|
|
'snews', 'prospero', 'rtsp', 'rtspu', 'rsync', '',
|
|
|
|
'svn', 'svn+ssh', 'sftp']
|
2004-05-07 02:50:35 -03:00
|
|
|
non_hierarchical = ['gopher', 'hdl', 'mailto', 'news',
|
2006-04-01 02:11:07 -04:00
|
|
|
'telnet', 'wais', 'imap', 'snews', 'sip', 'sips']
|
2004-05-07 02:50:35 -03:00
|
|
|
uses_params = ['ftp', 'hdl', 'prospero', 'http', 'imap',
|
2006-04-01 02:11:07 -04:00
|
|
|
'https', 'shttp', 'rtsp', 'rtspu', 'sip', 'sips',
|
2006-01-20 13:24:23 -04:00
|
|
|
'mms', '', 'sftp']
|
2004-05-07 02:50:35 -03:00
|
|
|
uses_query = ['http', 'wais', 'imap', 'https', 'shttp', 'mms',
|
2006-04-01 02:11:07 -04:00
|
|
|
'gopher', 'rtsp', 'rtspu', 'sip', 'sips', '']
|
2004-05-07 02:50:35 -03:00
|
|
|
uses_fragment = ['ftp', 'hdl', 'http', 'gopher', 'news',
|
2006-01-20 13:24:23 -04:00
|
|
|
'nntp', 'wais', 'https', 'shttp', 'snews',
|
|
|
|
'file', 'prospero', '']
|
1994-09-12 07:36:35 -03:00
|
|
|
|
|
|
|
# Characters valid in scheme names
|
2000-12-19 12:48:13 -04:00
|
|
|
scheme_chars = ('abcdefghijklmnopqrstuvwxyz'
|
|
|
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
|
|
'0123456789'
|
|
|
|
'+-.')
|
1994-09-12 07:36:35 -03:00
|
|
|
|
1997-07-14 16:08:15 -03:00
|
|
|
MAX_CACHE_SIZE = 20
|
1996-05-28 20:54:24 -03:00
|
|
|
_parse_cache = {}
|
|
|
|
|
|
|
|
def clear_cache():
|
2001-01-14 23:34:38 -04:00
|
|
|
"""Clear the parse cache."""
|
|
|
|
global _parse_cache
|
|
|
|
_parse_cache = {}
|
1996-05-28 20:54:24 -03:00
|
|
|
|
|
|
|
|
2006-04-01 18:14:43 -04:00
|
|
|
class BaseResult(tuple):
|
|
|
|
"""Base class for the parsed result objects.
|
|
|
|
|
|
|
|
This provides the attributes shared by the two derived result
|
|
|
|
objects as read-only properties. The derived classes are
|
|
|
|
responsible for checking the right number of arguments were
|
|
|
|
supplied to the constructor.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
# Attributes that access the basic components of the URL:
|
|
|
|
|
|
|
|
@property
|
|
|
|
def scheme(self):
|
|
|
|
return self[0]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def netloc(self):
|
|
|
|
return self[1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
return self[2]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def query(self):
|
|
|
|
return self[-2]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def fragment(self):
|
|
|
|
return self[-1]
|
|
|
|
|
|
|
|
# Additional attributes that provide access to parsed-out portions
|
|
|
|
# of the netloc:
|
|
|
|
|
|
|
|
@property
|
|
|
|
def username(self):
|
|
|
|
netloc = self.netloc
|
|
|
|
if "@" in netloc:
|
|
|
|
userinfo = netloc.split("@", 1)[0]
|
|
|
|
if ":" in userinfo:
|
|
|
|
userinfo = userinfo.split(":", 1)[0]
|
|
|
|
return userinfo
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def password(self):
|
|
|
|
netloc = self.netloc
|
|
|
|
if "@" in netloc:
|
|
|
|
userinfo = netloc.split("@", 1)[0]
|
|
|
|
if ":" in userinfo:
|
|
|
|
return userinfo.split(":", 1)[1]
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def hostname(self):
|
|
|
|
netloc = self.netloc
|
|
|
|
if "@" in netloc:
|
|
|
|
netloc = netloc.split("@", 1)[1]
|
|
|
|
if ":" in netloc:
|
|
|
|
netloc = netloc.split(":", 1)[0]
|
|
|
|
return netloc.lower() or None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def port(self):
|
|
|
|
netloc = self.netloc
|
|
|
|
if "@" in netloc:
|
|
|
|
netloc = netloc.split("@", 1)[1]
|
|
|
|
if ":" in netloc:
|
|
|
|
port = netloc.split(":", 1)[1]
|
|
|
|
return int(port, 10)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class SplitResult(BaseResult):
|
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def __new__(cls, scheme, netloc, path, query, fragment):
|
|
|
|
return BaseResult.__new__(
|
|
|
|
cls, (scheme, netloc, path, query, fragment))
|
|
|
|
|
|
|
|
def geturl(self):
|
|
|
|
return urlunsplit(self)
|
|
|
|
|
|
|
|
|
|
|
|
class ParseResult(BaseResult):
|
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def __new__(cls, scheme, netloc, path, params, query, fragment):
|
|
|
|
return BaseResult.__new__(
|
|
|
|
cls, (scheme, netloc, path, params, query, fragment))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def params(self):
|
|
|
|
return self[3]
|
|
|
|
|
|
|
|
def geturl(self):
|
|
|
|
return urlunparse(self)
|
|
|
|
|
|
|
|
|
|
|
|
def urlparse(url, scheme='', allow_fragments=True):
|
2001-01-14 23:34:38 -04:00
|
|
|
"""Parse a URL into 6 components:
|
|
|
|
<scheme>://<netloc>/<path>;<params>?<query>#<fragment>
|
|
|
|
Return a 6-tuple: (scheme, netloc, path, params, query, fragment).
|
|
|
|
Note that we don't break the components up in smaller bits
|
|
|
|
(e.g. netloc is a single string) and we don't expand % escapes."""
|
2001-11-15 22:52:57 -04:00
|
|
|
tuple = urlsplit(url, scheme, allow_fragments)
|
|
|
|
scheme, netloc, url, query, fragment = tuple
|
|
|
|
if scheme in uses_params and ';' in url:
|
|
|
|
url, params = _splitparams(url)
|
|
|
|
else:
|
|
|
|
params = ''
|
2006-04-01 18:14:43 -04:00
|
|
|
return ParseResult(scheme, netloc, url, params, query, fragment)
|
2001-11-15 22:52:57 -04:00
|
|
|
|
|
|
|
def _splitparams(url):
|
|
|
|
if '/' in url:
|
|
|
|
i = url.find(';', url.rfind('/'))
|
|
|
|
if i < 0:
|
|
|
|
return url, ''
|
|
|
|
else:
|
|
|
|
i = url.find(';')
|
|
|
|
return url[:i], url[i+1:]
|
|
|
|
|
2005-01-09 11:29:10 -04:00
|
|
|
def _splitnetloc(url, start=0):
|
|
|
|
for c in '/?#': # the order is important!
|
|
|
|
delim = url.find(c, start)
|
|
|
|
if delim >= 0:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
delim = len(url)
|
|
|
|
return url[start:delim], url[delim:]
|
|
|
|
|
2006-04-01 18:14:43 -04:00
|
|
|
def urlsplit(url, scheme='', allow_fragments=True):
|
2001-11-15 22:52:57 -04:00
|
|
|
"""Parse a URL into 5 components:
|
|
|
|
<scheme>://<netloc>/<path>?<query>#<fragment>
|
|
|
|
Return a 5-tuple: (scheme, netloc, path, query, fragment).
|
|
|
|
Note that we don't break the components up in smaller bits
|
|
|
|
(e.g. netloc is a single string) and we don't expand % escapes."""
|
2006-04-01 18:14:43 -04:00
|
|
|
allow_fragments = bool(allow_fragments)
|
2007-12-13 14:18:47 -04:00
|
|
|
key = url, scheme, allow_fragments, type(url), type(scheme)
|
2001-01-14 23:34:38 -04:00
|
|
|
cached = _parse_cache.get(key, None)
|
|
|
|
if cached:
|
|
|
|
return cached
|
|
|
|
if len(_parse_cache) >= MAX_CACHE_SIZE: # avoid runaway growth
|
|
|
|
clear_cache()
|
2001-11-15 22:52:57 -04:00
|
|
|
netloc = query = fragment = ''
|
2001-01-14 23:34:38 -04:00
|
|
|
i = url.find(':')
|
|
|
|
if i > 0:
|
|
|
|
if url[:i] == 'http': # optimize the common case
|
|
|
|
scheme = url[:i].lower()
|
|
|
|
url = url[i+1:]
|
|
|
|
if url[:2] == '//':
|
2005-01-09 11:29:10 -04:00
|
|
|
netloc, url = _splitnetloc(url, 2)
|
2001-11-15 22:52:57 -04:00
|
|
|
if allow_fragments and '#' in url:
|
|
|
|
url, fragment = url.split('#', 1)
|
|
|
|
if '?' in url:
|
|
|
|
url, query = url.split('?', 1)
|
2006-04-01 18:14:43 -04:00
|
|
|
v = SplitResult(scheme, netloc, url, query, fragment)
|
|
|
|
_parse_cache[key] = v
|
|
|
|
return v
|
2001-01-14 23:34:38 -04:00
|
|
|
for c in url[:i]:
|
|
|
|
if c not in scheme_chars:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
scheme, url = url[:i].lower(), url[i+1:]
|
2005-01-09 11:29:10 -04:00
|
|
|
if scheme in uses_netloc and url[:2] == '//':
|
|
|
|
netloc, url = _splitnetloc(url, 2)
|
2001-11-15 22:52:57 -04:00
|
|
|
if allow_fragments and scheme in uses_fragment and '#' in url:
|
|
|
|
url, fragment = url.split('#', 1)
|
|
|
|
if scheme in uses_query and '?' in url:
|
|
|
|
url, query = url.split('?', 1)
|
2006-04-01 18:14:43 -04:00
|
|
|
v = SplitResult(scheme, netloc, url, query, fragment)
|
|
|
|
_parse_cache[key] = v
|
|
|
|
return v
|
1994-09-12 07:36:35 -03:00
|
|
|
|
|
|
|
def urlunparse((scheme, netloc, url, params, query, fragment)):
|
2001-01-14 23:34:38 -04:00
|
|
|
"""Put a parsed URL back together again. This may result in a
|
|
|
|
slightly different, but equivalent URL, if the URL that was parsed
|
|
|
|
originally had redundant delimiters, e.g. a ? with an empty query
|
|
|
|
(the draft states that these are equivalent)."""
|
2001-11-15 22:52:57 -04:00
|
|
|
if params:
|
|
|
|
url = "%s;%s" % (url, params)
|
|
|
|
return urlunsplit((scheme, netloc, url, query, fragment))
|
|
|
|
|
|
|
|
def urlunsplit((scheme, netloc, url, query, fragment)):
|
2002-10-14 16:59:54 -03:00
|
|
|
if netloc or (scheme and scheme in uses_netloc and url[:2] != '//'):
|
2001-01-14 23:34:38 -04:00
|
|
|
if url and url[:1] != '/': url = '/' + url
|
|
|
|
url = '//' + (netloc or '') + url
|
|
|
|
if scheme:
|
|
|
|
url = scheme + ':' + url
|
|
|
|
if query:
|
|
|
|
url = url + '?' + query
|
|
|
|
if fragment:
|
|
|
|
url = url + '#' + fragment
|
|
|
|
return url
|
1994-09-12 07:36:35 -03:00
|
|
|
|
2006-04-01 18:14:43 -04:00
|
|
|
def urljoin(base, url, allow_fragments=True):
|
2001-01-14 23:34:38 -04:00
|
|
|
"""Join a base URL and a possibly relative URL to form an absolute
|
|
|
|
interpretation of the latter."""
|
|
|
|
if not base:
|
|
|
|
return url
|
|
|
|
if not url:
|
|
|
|
return base
|
|
|
|
bscheme, bnetloc, bpath, bparams, bquery, bfragment = \
|
|
|
|
urlparse(base, '', allow_fragments)
|
|
|
|
scheme, netloc, path, params, query, fragment = \
|
|
|
|
urlparse(url, bscheme, allow_fragments)
|
|
|
|
if scheme != bscheme or scheme not in uses_relative:
|
|
|
|
return url
|
|
|
|
if scheme in uses_netloc:
|
|
|
|
if netloc:
|
|
|
|
return urlunparse((scheme, netloc, path,
|
|
|
|
params, query, fragment))
|
|
|
|
netloc = bnetloc
|
|
|
|
if path[:1] == '/':
|
|
|
|
return urlunparse((scheme, netloc, path,
|
|
|
|
params, query, fragment))
|
2003-10-12 01:29:10 -03:00
|
|
|
if not (path or params or query):
|
2001-01-14 23:34:38 -04:00
|
|
|
return urlunparse((scheme, netloc, bpath,
|
2003-10-12 01:29:10 -03:00
|
|
|
bparams, bquery, fragment))
|
2001-01-14 23:34:38 -04:00
|
|
|
segments = bpath.split('/')[:-1] + path.split('/')
|
|
|
|
# XXX The stuff below is bogus in various ways...
|
|
|
|
if segments[-1] == '.':
|
|
|
|
segments[-1] = ''
|
|
|
|
while '.' in segments:
|
|
|
|
segments.remove('.')
|
|
|
|
while 1:
|
|
|
|
i = 1
|
|
|
|
n = len(segments) - 1
|
|
|
|
while i < n:
|
|
|
|
if (segments[i] == '..'
|
|
|
|
and segments[i-1] not in ('', '..')):
|
|
|
|
del segments[i-1:i+1]
|
|
|
|
break
|
|
|
|
i = i+1
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
if segments == ['', '..']:
|
|
|
|
segments[-1] = ''
|
|
|
|
elif len(segments) >= 2 and segments[-1] == '..':
|
|
|
|
segments[-2:] = ['']
|
|
|
|
return urlunparse((scheme, netloc, '/'.join(segments),
|
|
|
|
params, query, fragment))
|
1994-09-12 07:36:35 -03:00
|
|
|
|
1996-05-28 20:54:24 -03:00
|
|
|
def urldefrag(url):
|
2001-01-14 23:34:38 -04:00
|
|
|
"""Removes any existing fragment from URL.
|
1996-05-28 20:54:24 -03:00
|
|
|
|
2001-01-14 23:34:38 -04:00
|
|
|
Returns a tuple of the defragmented URL and the fragment. If
|
|
|
|
the URL contained no fragments, the second element is the
|
|
|
|
empty string.
|
|
|
|
"""
|
2001-11-15 22:52:57 -04:00
|
|
|
if '#' in url:
|
|
|
|
s, n, p, a, q, frag = urlparse(url)
|
|
|
|
defrag = urlunparse((s, n, p, a, q, ''))
|
|
|
|
return defrag, frag
|
|
|
|
else:
|
|
|
|
return url, ''
|
1996-05-28 20:54:24 -03:00
|
|
|
|
|
|
|
|
1994-09-12 07:36:35 -03:00
|
|
|
test_input = """
|
|
|
|
http://a/b/c/d
|
|
|
|
|
|
|
|
g:h = <URL:g:h>
|
|
|
|
http:g = <URL:http://a/b/c/g>
|
|
|
|
http: = <URL:http://a/b/c/d>
|
|
|
|
g = <URL:http://a/b/c/g>
|
|
|
|
./g = <URL:http://a/b/c/g>
|
|
|
|
g/ = <URL:http://a/b/c/g/>
|
|
|
|
/g = <URL:http://a/g>
|
|
|
|
//g = <URL:http://g>
|
|
|
|
?y = <URL:http://a/b/c/d?y>
|
|
|
|
g?y = <URL:http://a/b/c/g?y>
|
|
|
|
g?y/./x = <URL:http://a/b/c/g?y/./x>
|
|
|
|
. = <URL:http://a/b/c/>
|
|
|
|
./ = <URL:http://a/b/c/>
|
|
|
|
.. = <URL:http://a/b/>
|
|
|
|
../ = <URL:http://a/b/>
|
|
|
|
../g = <URL:http://a/b/g>
|
|
|
|
../.. = <URL:http://a/>
|
|
|
|
../../g = <URL:http://a/g>
|
|
|
|
../../../g = <URL:http://a/../g>
|
|
|
|
./../g = <URL:http://a/b/g>
|
|
|
|
./g/. = <URL:http://a/b/c/g/>
|
|
|
|
/./g = <URL:http://a/./g>
|
|
|
|
g/./h = <URL:http://a/b/c/g/h>
|
|
|
|
g/../h = <URL:http://a/b/c/h>
|
|
|
|
http:g = <URL:http://a/b/c/g>
|
|
|
|
http: = <URL:http://a/b/c/d>
|
1999-01-06 18:13:09 -04:00
|
|
|
http:?y = <URL:http://a/b/c/d?y>
|
|
|
|
http:g?y = <URL:http://a/b/c/g?y>
|
|
|
|
http:g?y/./x = <URL:http://a/b/c/g?y/./x>
|
1994-09-12 07:36:35 -03:00
|
|
|
"""
|
|
|
|
|
|
|
|
def test():
|
2001-01-14 23:34:38 -04:00
|
|
|
import sys
|
|
|
|
base = ''
|
|
|
|
if sys.argv[1:]:
|
|
|
|
fn = sys.argv[1]
|
|
|
|
if fn == '-':
|
|
|
|
fp = sys.stdin
|
|
|
|
else:
|
|
|
|
fp = open(fn)
|
|
|
|
else:
|
2004-12-31 15:15:26 -04:00
|
|
|
try:
|
|
|
|
from cStringIO import StringIO
|
|
|
|
except ImportError:
|
|
|
|
from StringIO import StringIO
|
|
|
|
fp = StringIO(test_input)
|
2001-01-14 23:34:38 -04:00
|
|
|
while 1:
|
|
|
|
line = fp.readline()
|
|
|
|
if not line: break
|
|
|
|
words = line.split()
|
|
|
|
if not words:
|
|
|
|
continue
|
|
|
|
url = words[0]
|
|
|
|
parts = urlparse(url)
|
|
|
|
print '%-10s : %s' % (url, parts)
|
|
|
|
abs = urljoin(base, url)
|
|
|
|
if not base:
|
|
|
|
base = abs
|
|
|
|
wrapped = '<URL:%s>' % abs
|
|
|
|
print '%-10s = %s' % (url, wrapped)
|
|
|
|
if len(words) == 3 and words[1] == '=':
|
|
|
|
if wrapped != words[2]:
|
|
|
|
print 'EXPECTED', words[2], '!!!!!!!!!!'
|
1994-09-12 07:36:35 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2001-01-14 23:34:38 -04:00
|
|
|
test()
|