Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Parse (absolute and relative) URLs. |
| 2 | |
| 3 | See RFC 1808: "Relative Uniform Resource Locators", by R. Fielding, |
| 4 | UC Irvine, June 1995. |
| 5 | """ |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 6 | |
| 7 | # Standard/builtin Python modules |
| 8 | import string |
Guido van Rossum | a25d7dd | 2000-04-10 17:02:46 +0000 | [diff] [blame] | 9 | from string import join, split, rfind |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 10 | |
| 11 | # A classification of schemes ('' means apply by default) |
| 12 | uses_relative = ['ftp', 'http', 'gopher', 'nntp', 'wais', 'file', |
Guido van Rossum | b02092a | 1997-01-02 18:18:27 +0000 | [diff] [blame] | 13 | 'https', 'shttp', |
Fred Drake | 0556501 | 2000-04-14 14:01:34 +0000 | [diff] [blame] | 14 | 'prospero', 'rtsp', 'rtspu', ''] |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 15 | uses_netloc = ['ftp', 'http', 'gopher', 'nntp', 'telnet', 'wais', |
Guido van Rossum | 974e32d | 1999-02-22 15:38:46 +0000 | [diff] [blame] | 16 | 'file', |
Guido van Rossum | b02092a | 1997-01-02 18:18:27 +0000 | [diff] [blame] | 17 | 'https', 'shttp', 'snews', |
Fred Drake | 0556501 | 2000-04-14 14:01:34 +0000 | [diff] [blame] | 18 | 'prospero', 'rtsp', 'rtspu', ''] |
Guido van Rossum | b02092a | 1997-01-02 18:18:27 +0000 | [diff] [blame] | 19 | non_hierarchical = ['gopher', 'hdl', 'mailto', 'news', 'telnet', 'wais', |
| 20 | 'snews', |
| 21 | ] |
| 22 | uses_params = ['ftp', 'hdl', 'prospero', 'http', |
Fred Drake | 0556501 | 2000-04-14 14:01:34 +0000 | [diff] [blame] | 23 | 'https', 'shttp', 'rtsp', 'rtspu', |
Guido van Rossum | b02092a | 1997-01-02 18:18:27 +0000 | [diff] [blame] | 24 | ''] |
| 25 | uses_query = ['http', 'wais', |
| 26 | 'https', 'shttp', |
Fred Drake | 0556501 | 2000-04-14 14:01:34 +0000 | [diff] [blame] | 27 | 'gopher', 'rtsp', 'rtspu', |
Guido van Rossum | b02092a | 1997-01-02 18:18:27 +0000 | [diff] [blame] | 28 | ''] |
Guido van Rossum | 5feb54c | 1996-05-28 23:10:02 +0000 | [diff] [blame] | 29 | uses_fragment = ['ftp', 'hdl', 'http', 'gopher', 'news', 'nntp', 'wais', |
Guido van Rossum | b02092a | 1997-01-02 18:18:27 +0000 | [diff] [blame] | 30 | 'https', 'shttp', 'snews', |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 31 | 'file', 'prospero', ''] |
| 32 | |
| 33 | # Characters valid in scheme names |
| 34 | scheme_chars = string.letters + string.digits + '+-.' |
| 35 | |
Guido van Rossum | 7449540 | 1997-07-14 19:08:15 +0000 | [diff] [blame] | 36 | MAX_CACHE_SIZE = 20 |
Guido van Rossum | 3fd32ec | 1996-05-28 23:54:24 +0000 | [diff] [blame] | 37 | _parse_cache = {} |
| 38 | |
| 39 | def clear_cache(): |
Guido van Rossum | a25d7dd | 2000-04-10 17:02:46 +0000 | [diff] [blame] | 40 | """Clear the parse cache.""" |
| 41 | global _parse_cache |
| 42 | _parse_cache = {} |
Guido van Rossum | 3fd32ec | 1996-05-28 23:54:24 +0000 | [diff] [blame] | 43 | |
| 44 | |
Jeremy Hylton | 4722da6 | 1998-08-25 19:45:24 +0000 | [diff] [blame] | 45 | def urlparse(url, scheme = '', allow_fragments = 1): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 46 | """Parse a URL into 6 components: |
| 47 | <scheme>://<netloc>/<path>;<params>?<query>#<fragment> |
| 48 | Return a 6-tuple: (scheme, netloc, path, params, query, fragment). |
| 49 | Note that we don't break the components up in smaller bits |
| 50 | (e.g. netloc is a single string) and we don't expand % escapes.""" |
Jeremy Hylton | 4722da6 | 1998-08-25 19:45:24 +0000 | [diff] [blame] | 51 | key = url, scheme, allow_fragments |
Jeremy Hylton | b85c847 | 1998-09-02 21:53:16 +0000 | [diff] [blame] | 52 | cached = _parse_cache.get(key, None) |
| 53 | if cached: |
| 54 | return cached |
Guido van Rossum | 671dc20 | 1996-12-27 15:26:15 +0000 | [diff] [blame] | 55 | if len(_parse_cache) >= MAX_CACHE_SIZE: # avoid runaway growth |
Guido van Rossum | a25d7dd | 2000-04-10 17:02:46 +0000 | [diff] [blame] | 56 | clear_cache() |
Jeremy Hylton | b85c847 | 1998-09-02 21:53:16 +0000 | [diff] [blame] | 57 | find = string.find |
Guido van Rossum | 3fd32ec | 1996-05-28 23:54:24 +0000 | [diff] [blame] | 58 | netloc = path = params = query = fragment = '' |
Jeremy Hylton | b85c847 | 1998-09-02 21:53:16 +0000 | [diff] [blame] | 59 | i = find(url, ':') |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 60 | if i > 0: |
Andrew M. Kuchling | 5c35520 | 1999-01-06 22:13:09 +0000 | [diff] [blame] | 61 | if url[:i] == 'http': # optimize the common case |
Jeremy Hylton | b85c847 | 1998-09-02 21:53:16 +0000 | [diff] [blame] | 62 | scheme = string.lower(url[:i]) |
| 63 | url = url[i+1:] |
| 64 | if url[:2] == '//': |
| 65 | i = find(url, '/', 2) |
| 66 | if i < 0: |
| 67 | i = len(url) |
| 68 | netloc = url[2:i] |
| 69 | url = url[i:] |
| 70 | if allow_fragments: |
| 71 | i = string.rfind(url, '#') |
| 72 | if i >= 0: |
Jeremy Hylton | b85c847 | 1998-09-02 21:53:16 +0000 | [diff] [blame] | 73 | fragment = url[i+1:] |
Andrew M. Kuchling | 5c35520 | 1999-01-06 22:13:09 +0000 | [diff] [blame] | 74 | url = url[:i] |
Jeremy Hylton | b85c847 | 1998-09-02 21:53:16 +0000 | [diff] [blame] | 75 | i = find(url, '?') |
| 76 | if i >= 0: |
Jeremy Hylton | b85c847 | 1998-09-02 21:53:16 +0000 | [diff] [blame] | 77 | query = url[i+1:] |
Andrew M. Kuchling | 5c35520 | 1999-01-06 22:13:09 +0000 | [diff] [blame] | 78 | url = url[:i] |
Jeremy Hylton | b85c847 | 1998-09-02 21:53:16 +0000 | [diff] [blame] | 79 | i = find(url, ';') |
| 80 | if i >= 0: |
Jeremy Hylton | b85c847 | 1998-09-02 21:53:16 +0000 | [diff] [blame] | 81 | params = url[i+1:] |
Andrew M. Kuchling | 5c35520 | 1999-01-06 22:13:09 +0000 | [diff] [blame] | 82 | url = url[:i] |
Jeremy Hylton | b85c847 | 1998-09-02 21:53:16 +0000 | [diff] [blame] | 83 | tuple = scheme, netloc, url, params, query, fragment |
| 84 | _parse_cache[key] = tuple |
| 85 | return tuple |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 86 | for c in url[:i]: |
| 87 | if c not in scheme_chars: |
| 88 | break |
| 89 | else: |
| 90 | scheme, url = string.lower(url[:i]), url[i+1:] |
| 91 | if scheme in uses_netloc: |
| 92 | if url[:2] == '//': |
Jeremy Hylton | b85c847 | 1998-09-02 21:53:16 +0000 | [diff] [blame] | 93 | i = find(url, '/', 2) |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 94 | if i < 0: |
| 95 | i = len(url) |
| 96 | netloc, url = url[2:i], url[i:] |
Jeremy Hylton | 4722da6 | 1998-08-25 19:45:24 +0000 | [diff] [blame] | 97 | if allow_fragments and scheme in uses_fragment: |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 98 | i = string.rfind(url, '#') |
| 99 | if i >= 0: |
| 100 | url, fragment = url[:i], url[i+1:] |
| 101 | if scheme in uses_query: |
Jeremy Hylton | b85c847 | 1998-09-02 21:53:16 +0000 | [diff] [blame] | 102 | i = find(url, '?') |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 103 | if i >= 0: |
| 104 | url, query = url[:i], url[i+1:] |
| 105 | if scheme in uses_params: |
Jeremy Hylton | b85c847 | 1998-09-02 21:53:16 +0000 | [diff] [blame] | 106 | i = find(url, ';') |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 107 | if i >= 0: |
| 108 | url, params = url[:i], url[i+1:] |
Guido van Rossum | 3fd32ec | 1996-05-28 23:54:24 +0000 | [diff] [blame] | 109 | tuple = scheme, netloc, url, params, query, fragment |
| 110 | _parse_cache[key] = tuple |
| 111 | return tuple |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 113 | def urlunparse((scheme, netloc, url, params, query, fragment)): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 114 | """Put a parsed URL back together again. This may result in a |
| 115 | slightly different, but equivalent URL, if the URL that was parsed |
| 116 | originally had redundant delimiters, e.g. a ? with an empty query |
| 117 | (the draft states that these are equivalent).""" |
Guido van Rossum | f3963b1 | 1999-03-18 15:10:44 +0000 | [diff] [blame] | 118 | if netloc or (scheme in uses_netloc and url[:2] == '//'): |
Guido van Rossum | fb1a0cd | 1995-08-04 04:29:32 +0000 | [diff] [blame] | 119 | if url[:1] != '/': url = '/' + url |
Guido van Rossum | f3963b1 | 1999-03-18 15:10:44 +0000 | [diff] [blame] | 120 | url = '//' + (netloc or '') + url |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 121 | if scheme: |
| 122 | url = scheme + ':' + url |
| 123 | if params: |
| 124 | url = url + ';' + params |
| 125 | if query: |
| 126 | url = url + '?' + query |
| 127 | if fragment: |
| 128 | url = url + '#' + fragment |
| 129 | return url |
| 130 | |
Jeremy Hylton | 4722da6 | 1998-08-25 19:45:24 +0000 | [diff] [blame] | 131 | def urljoin(base, url, allow_fragments = 1): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 132 | """Join a base URL and a possibly relative URL to form an absolute |
| 133 | interpretation of the latter.""" |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 134 | if not base: |
| 135 | return url |
| 136 | bscheme, bnetloc, bpath, bparams, bquery, bfragment = \ |
Jeremy Hylton | 4722da6 | 1998-08-25 19:45:24 +0000 | [diff] [blame] | 137 | urlparse(base, '', allow_fragments) |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 138 | scheme, netloc, path, params, query, fragment = \ |
Jeremy Hylton | 4722da6 | 1998-08-25 19:45:24 +0000 | [diff] [blame] | 139 | urlparse(url, bscheme, allow_fragments) |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 140 | if scheme != bscheme or scheme not in uses_relative: |
| 141 | return urlunparse((scheme, netloc, path, |
| 142 | params, query, fragment)) |
| 143 | if scheme in uses_netloc: |
| 144 | if netloc: |
| 145 | return urlunparse((scheme, netloc, path, |
| 146 | params, query, fragment)) |
| 147 | netloc = bnetloc |
| 148 | if path[:1] == '/': |
| 149 | return urlunparse((scheme, netloc, path, |
| 150 | params, query, fragment)) |
| 151 | if not path: |
Guido van Rossum | 3fd32ec | 1996-05-28 23:54:24 +0000 | [diff] [blame] | 152 | return urlunparse((scheme, netloc, bpath, |
| 153 | params, query or bquery, fragment)) |
Guido van Rossum | a25d7dd | 2000-04-10 17:02:46 +0000 | [diff] [blame] | 154 | segments = split(bpath, '/')[:-1] + split(path, '/') |
| 155 | # XXX The stuff below is bogus in various ways... |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 156 | if segments[-1] == '.': |
| 157 | segments[-1] = '' |
| 158 | while '.' in segments: |
| 159 | segments.remove('.') |
| 160 | while 1: |
| 161 | i = 1 |
| 162 | n = len(segments) - 1 |
| 163 | while i < n: |
| 164 | if segments[i] == '..' and segments[i-1]: |
| 165 | del segments[i-1:i+1] |
| 166 | break |
| 167 | i = i+1 |
| 168 | else: |
| 169 | break |
Guido van Rossum | e612be5 | 1997-12-03 22:38:56 +0000 | [diff] [blame] | 170 | if len(segments) == 2 and segments[1] == '..' and segments[0] == '': |
| 171 | segments[-1] = '' |
| 172 | elif len(segments) >= 2 and segments[-1] == '..': |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 173 | segments[-2:] = [''] |
Guido van Rossum | a25d7dd | 2000-04-10 17:02:46 +0000 | [diff] [blame] | 174 | return urlunparse((scheme, netloc, join(segments, '/'), |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 175 | params, query, fragment)) |
| 176 | |
Guido van Rossum | 3fd32ec | 1996-05-28 23:54:24 +0000 | [diff] [blame] | 177 | def urldefrag(url): |
Guido van Rossum | a25d7dd | 2000-04-10 17:02:46 +0000 | [diff] [blame] | 178 | """Removes any existing fragment from URL. |
Guido van Rossum | 3fd32ec | 1996-05-28 23:54:24 +0000 | [diff] [blame] | 179 | |
Guido van Rossum | a25d7dd | 2000-04-10 17:02:46 +0000 | [diff] [blame] | 180 | Returns a tuple of the defragmented URL and the fragment. If |
| 181 | the URL contained no fragments, the second element is the |
| 182 | empty string. |
| 183 | """ |
| 184 | s, n, p, a, q, frag = urlparse(url) |
| 185 | defrag = urlunparse((s, n, p, a, q, '')) |
| 186 | return defrag, frag |
Guido van Rossum | 3fd32ec | 1996-05-28 23:54:24 +0000 | [diff] [blame] | 187 | |
| 188 | |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 189 | test_input = """ |
| 190 | http://a/b/c/d |
| 191 | |
| 192 | g:h = <URL:g:h> |
| 193 | http:g = <URL:http://a/b/c/g> |
| 194 | http: = <URL:http://a/b/c/d> |
| 195 | g = <URL:http://a/b/c/g> |
| 196 | ./g = <URL:http://a/b/c/g> |
| 197 | g/ = <URL:http://a/b/c/g/> |
| 198 | /g = <URL:http://a/g> |
| 199 | //g = <URL:http://g> |
| 200 | ?y = <URL:http://a/b/c/d?y> |
| 201 | g?y = <URL:http://a/b/c/g?y> |
| 202 | g?y/./x = <URL:http://a/b/c/g?y/./x> |
| 203 | . = <URL:http://a/b/c/> |
| 204 | ./ = <URL:http://a/b/c/> |
| 205 | .. = <URL:http://a/b/> |
| 206 | ../ = <URL:http://a/b/> |
| 207 | ../g = <URL:http://a/b/g> |
| 208 | ../.. = <URL:http://a/> |
| 209 | ../../g = <URL:http://a/g> |
| 210 | ../../../g = <URL:http://a/../g> |
| 211 | ./../g = <URL:http://a/b/g> |
| 212 | ./g/. = <URL:http://a/b/c/g/> |
| 213 | /./g = <URL:http://a/./g> |
| 214 | g/./h = <URL:http://a/b/c/g/h> |
| 215 | g/../h = <URL:http://a/b/c/h> |
| 216 | http:g = <URL:http://a/b/c/g> |
| 217 | http: = <URL:http://a/b/c/d> |
Andrew M. Kuchling | 5c35520 | 1999-01-06 22:13:09 +0000 | [diff] [blame] | 218 | http:?y = <URL:http://a/b/c/d?y> |
| 219 | http:g?y = <URL:http://a/b/c/g?y> |
| 220 | http:g?y/./x = <URL:http://a/b/c/g?y/./x> |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 221 | """ |
Guido van Rossum | c08cc50 | 1998-12-21 18:24:09 +0000 | [diff] [blame] | 222 | # XXX The result for //g is actually http://g/; is this a problem? |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 223 | |
| 224 | def test(): |
| 225 | import sys |
| 226 | base = '' |
| 227 | if sys.argv[1:]: |
| 228 | fn = sys.argv[1] |
| 229 | if fn == '-': |
| 230 | fp = sys.stdin |
| 231 | else: |
| 232 | fp = open(fn) |
| 233 | else: |
| 234 | import StringIO |
| 235 | fp = StringIO.StringIO(test_input) |
| 236 | while 1: |
| 237 | line = fp.readline() |
| 238 | if not line: break |
| 239 | words = string.split(line) |
| 240 | if not words: |
| 241 | continue |
| 242 | url = words[0] |
| 243 | parts = urlparse(url) |
| 244 | print '%-10s : %s' % (url, parts) |
| 245 | abs = urljoin(base, url) |
| 246 | if not base: |
| 247 | base = abs |
| 248 | wrapped = '<URL:%s>' % abs |
| 249 | print '%-10s = %s' % (url, wrapped) |
| 250 | if len(words) == 3 and words[1] == '=': |
| 251 | if wrapped != words[2]: |
| 252 | print 'EXPECTED', words[2], '!!!!!!!!!!' |
| 253 | |
| 254 | if __name__ == '__main__': |
| 255 | test() |