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
|
|
|
|
|
|
|
# Standard/builtin Python modules
|
|
|
|
import string
|
2000-04-10 14:02:46 -03:00
|
|
|
from string import join, split, rfind
|
1994-09-12 07:36:35 -03:00
|
|
|
|
|
|
|
# A classification of schemes ('' means apply by default)
|
|
|
|
uses_relative = ['ftp', 'http', 'gopher', 'nntp', 'wais', 'file',
|
1997-01-02 14:18:27 -04:00
|
|
|
'https', 'shttp',
|
2000-04-14 11:01:34 -03:00
|
|
|
'prospero', 'rtsp', 'rtspu', '']
|
1994-09-12 07:36:35 -03:00
|
|
|
uses_netloc = ['ftp', 'http', 'gopher', 'nntp', 'telnet', 'wais',
|
1999-02-22 11:38:46 -04:00
|
|
|
'file',
|
1997-01-02 14:18:27 -04:00
|
|
|
'https', 'shttp', 'snews',
|
2000-04-14 11:01:34 -03:00
|
|
|
'prospero', 'rtsp', 'rtspu', '']
|
1997-01-02 14:18:27 -04:00
|
|
|
non_hierarchical = ['gopher', 'hdl', 'mailto', 'news', 'telnet', 'wais',
|
|
|
|
'snews',
|
|
|
|
]
|
|
|
|
uses_params = ['ftp', 'hdl', 'prospero', 'http',
|
2000-04-14 11:01:34 -03:00
|
|
|
'https', 'shttp', 'rtsp', 'rtspu',
|
1997-01-02 14:18:27 -04:00
|
|
|
'']
|
|
|
|
uses_query = ['http', 'wais',
|
|
|
|
'https', 'shttp',
|
2000-04-14 11:01:34 -03:00
|
|
|
'gopher', 'rtsp', 'rtspu',
|
1997-01-02 14:18:27 -04:00
|
|
|
'']
|
1996-05-28 20:10:02 -03:00
|
|
|
uses_fragment = ['ftp', 'hdl', 'http', 'gopher', 'news', 'nntp', 'wais',
|
1997-01-02 14:18:27 -04:00
|
|
|
'https', 'shttp', 'snews',
|
1994-09-12 07:36:35 -03:00
|
|
|
'file', 'prospero', '']
|
|
|
|
|
|
|
|
# Characters valid in scheme names
|
|
|
|
scheme_chars = string.letters + string.digits + '+-.'
|
|
|
|
|
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():
|
2000-04-10 14:02:46 -03:00
|
|
|
"""Clear the parse cache."""
|
|
|
|
global _parse_cache
|
|
|
|
_parse_cache = {}
|
1996-05-28 20:54:24 -03:00
|
|
|
|
|
|
|
|
1998-08-25 16:45:24 -03:00
|
|
|
def urlparse(url, scheme = '', allow_fragments = 1):
|
2000-02-04 11:28:42 -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."""
|
1998-08-25 16:45:24 -03:00
|
|
|
key = url, scheme, allow_fragments
|
1998-09-02 18:53:16 -03:00
|
|
|
cached = _parse_cache.get(key, None)
|
|
|
|
if cached:
|
|
|
|
return cached
|
1996-12-27 11:26:15 -04:00
|
|
|
if len(_parse_cache) >= MAX_CACHE_SIZE: # avoid runaway growth
|
2000-04-10 14:02:46 -03:00
|
|
|
clear_cache()
|
1998-09-02 18:53:16 -03:00
|
|
|
find = string.find
|
1996-05-28 20:54:24 -03:00
|
|
|
netloc = path = params = query = fragment = ''
|
1998-09-02 18:53:16 -03:00
|
|
|
i = find(url, ':')
|
1994-09-12 07:36:35 -03:00
|
|
|
if i > 0:
|
1999-01-06 18:13:09 -04:00
|
|
|
if url[:i] == 'http': # optimize the common case
|
1998-09-02 18:53:16 -03:00
|
|
|
scheme = string.lower(url[:i])
|
|
|
|
url = url[i+1:]
|
|
|
|
if url[:2] == '//':
|
|
|
|
i = find(url, '/', 2)
|
|
|
|
if i < 0:
|
|
|
|
i = len(url)
|
|
|
|
netloc = url[2:i]
|
|
|
|
url = url[i:]
|
|
|
|
if allow_fragments:
|
|
|
|
i = string.rfind(url, '#')
|
|
|
|
if i >= 0:
|
|
|
|
fragment = url[i+1:]
|
1999-01-06 18:13:09 -04:00
|
|
|
url = url[:i]
|
1998-09-02 18:53:16 -03:00
|
|
|
i = find(url, '?')
|
|
|
|
if i >= 0:
|
|
|
|
query = url[i+1:]
|
1999-01-06 18:13:09 -04:00
|
|
|
url = url[:i]
|
1998-09-02 18:53:16 -03:00
|
|
|
i = find(url, ';')
|
|
|
|
if i >= 0:
|
|
|
|
params = url[i+1:]
|
1999-01-06 18:13:09 -04:00
|
|
|
url = url[:i]
|
1998-09-02 18:53:16 -03:00
|
|
|
tuple = scheme, netloc, url, params, query, fragment
|
|
|
|
_parse_cache[key] = tuple
|
|
|
|
return tuple
|
1994-09-12 07:36:35 -03:00
|
|
|
for c in url[:i]:
|
|
|
|
if c not in scheme_chars:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
scheme, url = string.lower(url[:i]), url[i+1:]
|
|
|
|
if scheme in uses_netloc:
|
|
|
|
if url[:2] == '//':
|
1998-09-02 18:53:16 -03:00
|
|
|
i = find(url, '/', 2)
|
1994-09-12 07:36:35 -03:00
|
|
|
if i < 0:
|
|
|
|
i = len(url)
|
|
|
|
netloc, url = url[2:i], url[i:]
|
1998-08-25 16:45:24 -03:00
|
|
|
if allow_fragments and scheme in uses_fragment:
|
1994-09-12 07:36:35 -03:00
|
|
|
i = string.rfind(url, '#')
|
|
|
|
if i >= 0:
|
|
|
|
url, fragment = url[:i], url[i+1:]
|
|
|
|
if scheme in uses_query:
|
1998-09-02 18:53:16 -03:00
|
|
|
i = find(url, '?')
|
1994-09-12 07:36:35 -03:00
|
|
|
if i >= 0:
|
|
|
|
url, query = url[:i], url[i+1:]
|
|
|
|
if scheme in uses_params:
|
1998-09-02 18:53:16 -03:00
|
|
|
i = find(url, ';')
|
1994-09-12 07:36:35 -03:00
|
|
|
if i >= 0:
|
|
|
|
url, params = url[:i], url[i+1:]
|
1996-05-28 20:54:24 -03:00
|
|
|
tuple = scheme, netloc, url, params, query, fragment
|
|
|
|
_parse_cache[key] = tuple
|
|
|
|
return tuple
|
1994-09-12 07:36:35 -03:00
|
|
|
|
|
|
|
def urlunparse((scheme, netloc, url, params, query, fragment)):
|
2000-02-04 11:28:42 -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)."""
|
1999-03-18 11:10:44 -04:00
|
|
|
if netloc or (scheme in uses_netloc and url[:2] == '//'):
|
1995-08-04 01:29:32 -03:00
|
|
|
if url[:1] != '/': url = '/' + url
|
1999-03-18 11:10:44 -04:00
|
|
|
url = '//' + (netloc or '') + url
|
1994-09-12 07:36:35 -03:00
|
|
|
if scheme:
|
|
|
|
url = scheme + ':' + url
|
|
|
|
if params:
|
|
|
|
url = url + ';' + params
|
|
|
|
if query:
|
|
|
|
url = url + '?' + query
|
|
|
|
if fragment:
|
|
|
|
url = url + '#' + fragment
|
|
|
|
return url
|
|
|
|
|
1998-08-25 16:45:24 -03:00
|
|
|
def urljoin(base, url, allow_fragments = 1):
|
2000-02-04 11:28:42 -04:00
|
|
|
"""Join a base URL and a possibly relative URL to form an absolute
|
|
|
|
interpretation of the latter."""
|
1994-09-12 07:36:35 -03:00
|
|
|
if not base:
|
|
|
|
return url
|
|
|
|
bscheme, bnetloc, bpath, bparams, bquery, bfragment = \
|
1998-08-25 16:45:24 -03:00
|
|
|
urlparse(base, '', allow_fragments)
|
1994-09-12 07:36:35 -03:00
|
|
|
scheme, netloc, path, params, query, fragment = \
|
1998-08-25 16:45:24 -03:00
|
|
|
urlparse(url, bscheme, allow_fragments)
|
1994-09-12 07:36:35 -03:00
|
|
|
if scheme != bscheme or scheme not in uses_relative:
|
|
|
|
return urlunparse((scheme, netloc, path,
|
|
|
|
params, query, fragment))
|
|
|
|
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))
|
|
|
|
if not path:
|
1996-05-28 20:54:24 -03:00
|
|
|
return urlunparse((scheme, netloc, bpath,
|
|
|
|
params, query or bquery, fragment))
|
2000-04-10 14:02:46 -03:00
|
|
|
segments = split(bpath, '/')[:-1] + split(path, '/')
|
|
|
|
# XXX The stuff below is bogus in various ways...
|
1994-09-12 07:36:35 -03:00
|
|
|
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]:
|
|
|
|
del segments[i-1:i+1]
|
|
|
|
break
|
|
|
|
i = i+1
|
|
|
|
else:
|
|
|
|
break
|
1997-12-03 18:38:56 -04:00
|
|
|
if len(segments) == 2 and segments[1] == '..' and segments[0] == '':
|
|
|
|
segments[-1] = ''
|
|
|
|
elif len(segments) >= 2 and segments[-1] == '..':
|
1994-09-12 07:36:35 -03:00
|
|
|
segments[-2:] = ['']
|
2000-04-10 14:02:46 -03:00
|
|
|
return urlunparse((scheme, netloc, join(segments, '/'),
|
1994-09-12 07:36:35 -03:00
|
|
|
params, query, fragment))
|
|
|
|
|
1996-05-28 20:54:24 -03:00
|
|
|
def urldefrag(url):
|
2000-04-10 14:02:46 -03:00
|
|
|
"""Removes any existing fragment from URL.
|
1996-05-28 20:54:24 -03:00
|
|
|
|
2000-04-10 14:02:46 -03:00
|
|
|
Returns a tuple of the defragmented URL and the fragment. If
|
|
|
|
the URL contained no fragments, the second element is the
|
|
|
|
empty string.
|
|
|
|
"""
|
|
|
|
s, n, p, a, q, frag = urlparse(url)
|
|
|
|
defrag = urlunparse((s, n, p, a, q, ''))
|
|
|
|
return defrag, frag
|
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
|
|
|
"""
|
1998-12-21 14:24:09 -04:00
|
|
|
# XXX The result for //g is actually http://g/; is this a problem?
|
1994-09-12 07:36:35 -03:00
|
|
|
|
|
|
|
def test():
|
|
|
|
import sys
|
|
|
|
base = ''
|
|
|
|
if sys.argv[1:]:
|
|
|
|
fn = sys.argv[1]
|
|
|
|
if fn == '-':
|
|
|
|
fp = sys.stdin
|
|
|
|
else:
|
|
|
|
fp = open(fn)
|
|
|
|
else:
|
|
|
|
import StringIO
|
|
|
|
fp = StringIO.StringIO(test_input)
|
|
|
|
while 1:
|
|
|
|
line = fp.readline()
|
|
|
|
if not line: break
|
|
|
|
words = string.split(line)
|
|
|
|
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], '!!!!!!!!!!'
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test()
|