| 1 |
"""RFC 3986 URI parsing and relative reference resolution / absolutization. |
| 2 |
|
| 3 |
(aka splitting and joining) |
| 4 |
|
| 5 |
Copyright 2006 John J. Lee <[email protected]> |
| 6 |
|
| 7 |
This code is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the BSD or ZPL 2.1 licenses (see the file LICENSE |
| 9 |
included with the distribution). |
| 10 |
|
| 11 |
""" |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
from __future__ import absolute_import |
| 16 |
import re |
| 17 |
|
| 18 |
from .polyglot import quote |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
BAD_URI_CHARS_RE = re.compile(r"[^A-Za-z0-9\-_.~!*'();:@&=+$,/?%#[\]]") |
| 30 |
|
| 31 |
|
| 32 |
def clean_url(url, encoding='utf-8'): |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
is_unicode = not isinstance(url, bytes) |
| 41 |
if not is_unicode: |
| 42 |
url = url.decode(encoding, "replace") |
| 43 |
url = url.strip() |
| 44 |
|
| 45 |
|
| 46 |
ans = quote(url.encode(encoding), "!*'();:@&=+$,/?%#[]~") |
| 47 |
if is_unicode and isinstance(ans, bytes): |
| 48 |
ans = ans.decode(encoding) |
| 49 |
return ans |
| 50 |
|
| 51 |
|
| 52 |
def is_clean_uri(uri): |
| 53 |
""" |
| 54 |
>>> is_clean_uri("ABC!") |
| 55 |
True |
| 56 |
>>> is_clean_uri(u"ABC!") |
| 57 |
True |
| 58 |
>>> is_clean_uri("ABC|") |
| 59 |
False |
| 60 |
>>> is_clean_uri(u"ABC|") |
| 61 |
False |
| 62 |
>>> is_clean_uri("http://example.com/0") |
| 63 |
True |
| 64 |
>>> is_clean_uri(u"http://example.com/0") |
| 65 |
True |
| 66 |
""" |
| 67 |
|
| 68 |
|
| 69 |
return not bool(BAD_URI_CHARS_RE.search(uri)) |
| 70 |
|
| 71 |
|
| 72 |
SPLIT_MATCH = re.compile( |
| 73 |
r"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?").match |
| 74 |
|
| 75 |
|
| 76 |
def urlsplit(absolute_uri): |
| 77 |
"""Return scheme, authority, path, query, fragment.""" |
| 78 |
match = SPLIT_MATCH(absolute_uri) |
| 79 |
if match: |
| 80 |
g = match.groups() |
| 81 |
return g[1], g[3], g[4], g[6], g[8] |
| 82 |
|
| 83 |
|
| 84 |
def urlunsplit(parts): |
| 85 |
scheme, authority, path, query, fragment = parts |
| 86 |
r = [] |
| 87 |
append = r.append |
| 88 |
if scheme is not None: |
| 89 |
append(scheme) |
| 90 |
append(":") |
| 91 |
if authority is not None: |
| 92 |
append("//") |
| 93 |
append(authority) |
| 94 |
append(path) |
| 95 |
if query is not None: |
| 96 |
append("?") |
| 97 |
append(query) |
| 98 |
if fragment is not None: |
| 99 |
append("#") |
| 100 |
append(fragment) |
| 101 |
return "".join(r) |
| 102 |
|
| 103 |
|
| 104 |
def urljoin(base_uri, uri_reference): |
| 105 |
"""Join a base URI with a URI reference and return the resulting URI. |
| 106 |
|
| 107 |
See RFC 3986. |
| 108 |
""" |
| 109 |
return urlunsplit(urljoin_parts(urlsplit(base_uri), |
| 110 |
urlsplit(uri_reference))) |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
def urljoin_parts(base_parts, reference_parts): |
| 143 |
scheme, authority, path, query, fragment = base_parts |
| 144 |
rscheme, rauthority, rpath, rquery, rfragment = reference_parts |
| 145 |
|
| 146 |
if rscheme == scheme: |
| 147 |
rscheme = None |
| 148 |
|
| 149 |
if rscheme is not None: |
| 150 |
tscheme, tauthority, tpath, tquery = ( |
| 151 |
rscheme, rauthority, remove_dot_segments(rpath), rquery) |
| 152 |
else: |
| 153 |
if rauthority is not None: |
| 154 |
tauthority, tpath, tquery = ( |
| 155 |
rauthority, remove_dot_segments(rpath), rquery) |
| 156 |
else: |
| 157 |
if rpath == "": |
| 158 |
tpath = path |
| 159 |
if rquery is not None: |
| 160 |
tquery = rquery |
| 161 |
else: |
| 162 |
tquery = query |
| 163 |
else: |
| 164 |
if rpath.startswith("/"): |
| 165 |
tpath = remove_dot_segments(rpath) |
| 166 |
else: |
| 167 |
tpath = merge(authority, path, rpath) |
| 168 |
tpath = remove_dot_segments(tpath) |
| 169 |
tquery = rquery |
| 170 |
tauthority = authority |
| 171 |
tscheme = scheme |
| 172 |
tfragment = rfragment |
| 173 |
return (tscheme, tauthority, tpath, tquery, tfragment) |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
def remove_dot_segments(path): |
| 196 |
r = [] |
| 197 |
while path: |
| 198 |
|
| 199 |
if path.startswith("../"): |
| 200 |
path = path[3:] |
| 201 |
continue |
| 202 |
if path.startswith("./"): |
| 203 |
path = path[2:] |
| 204 |
continue |
| 205 |
|
| 206 |
if path.startswith("/./"): |
| 207 |
path = path[2:] |
| 208 |
continue |
| 209 |
if path == "/.": |
| 210 |
path = "/" |
| 211 |
continue |
| 212 |
|
| 213 |
if path.startswith("/../"): |
| 214 |
path = path[3:] |
| 215 |
if r: |
| 216 |
r.pop() |
| 217 |
continue |
| 218 |
if path == "/..": |
| 219 |
path = "/" |
| 220 |
if r: |
| 221 |
r.pop() |
| 222 |
continue |
| 223 |
|
| 224 |
if path == ".": |
| 225 |
path = path[1:] |
| 226 |
continue |
| 227 |
if path == "..": |
| 228 |
path = path[2:] |
| 229 |
continue |
| 230 |
|
| 231 |
start = 0 |
| 232 |
if path.startswith("/"): |
| 233 |
start = 1 |
| 234 |
ii = path.find("/", start) |
| 235 |
if ii < 0: |
| 236 |
ii = None |
| 237 |
r.append(path[:ii]) |
| 238 |
if ii is None: |
| 239 |
break |
| 240 |
path = path[ii:] |
| 241 |
return "".join(r) |
| 242 |
|
| 243 |
|
| 244 |
def merge(base_authority, base_path, ref_path): |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
if base_path == "": |
| 250 |
return "/" + ref_path |
| 251 |
ii = base_path.rfind("/") |
| 252 |
if ii >= 0: |
| 253 |
return base_path[:ii + 1] + ref_path |
| 254 |
return ref_path |
| 255 |
|
| 256 |
|
| 257 |
if __name__ == "__main__": |
| 258 |
import doctest |
| 259 |
doctest.testmod() |