codekingpro/portable-devtools
114k
1"""2 authlib.util.urls3 ~~~~~~~~~~~~~~~~~4 5 Wrapper functions for URL encoding and decoding.6"""7 8import re9from urllib.parse import quote as _quote10from urllib.parse import unquote as _unquote11from urllib.parse import urlencode as _urlencode12import urllib.parse as urlparse13 14from .encoding import to_unicode, to_bytes15 16always_safe = (17 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'18 'abcdefghijklmnopqrstuvwxyz'19 '0123456789_.-'20)21urlencoded = set(always_safe) | set('=&;:%+~,*@!()/?')22INVALID_HEX_PATTERN = re.compile(r'%[^0-9A-Fa-f]|%[0-9A-Fa-f][^0-9A-Fa-f]')23 24 25def url_encode(params):26 encoded = []27 for k, v in params:28 encoded.append((to_bytes(k), to_bytes(v)))29 return to_unicode(_urlencode(encoded))30 31 32def url_decode(query):33 """Decode a query string in x-www-form-urlencoded format into a sequence34 of two-element tuples.35 36 Unlike urlparse.parse_qsl(..., strict_parsing=True) urldecode will enforce37 correct formatting of the query string by validation. If validation fails38 a ValueError will be raised. urllib.parse_qsl will only raise errors if39 any of name-value pairs omits the equals sign.40 """41 # Check if query contains invalid characters42 if query and not set(query) <= urlencoded:43 error = ("Error trying to decode a non urlencoded string. "44 "Found invalid characters: %s "45 "in the string: '%s'. "46 "Please ensure the request/response body is "47 "x-www-form-urlencoded.")48 raise ValueError(error % (set(query) - urlencoded, query))49 50 # Check for correctly hex encoded values using a regular expression51 # All encoded values begin with % followed by two hex characters52 # correct = %00, %A0, %0A, %FF53 # invalid = %G0, %5H, %PO54 if INVALID_HEX_PATTERN.search(query):55 raise ValueError('Invalid hex encoding in query string.')56 57 # We encode to utf-8 prior to parsing because parse_qsl behaves58 # differently on unicode input in python 2 and 3.59 # Python 2.760 # >>> urlparse.parse_qsl(u'%E5%95%A6%E5%95%A6')61 # u'\xe5\x95\xa6\xe5\x95\xa6'62 # Python 2.7, non unicode input gives the same63 # >>> urlparse.parse_qsl('%E5%95%A6%E5%95%A6')64 # '\xe5\x95\xa6\xe5\x95\xa6'65 # but now we can decode it to unicode66 # >>> urlparse.parse_qsl('%E5%95%A6%E5%95%A6').decode('utf-8')67 # u'\u5566\u5566'68 # Python 3.3 however69 # >>> urllib.parse.parse_qsl(u'%E5%95%A6%E5%95%A6')70 # u'\u5566\u5566'71 72 # We want to allow queries such as "c2" whereas urlparse.parse_qsl73 # with the strict_parsing flag will not.74 params = urlparse.parse_qsl(query, keep_blank_values=True)75 76 # unicode all the things77 decoded = []78 for k, v in params:79 decoded.append((to_unicode(k), to_unicode(v)))80 return decoded81 82 83def add_params_to_qs(query, params):84 """Extend a query with a list of two-tuples."""85 if isinstance(params, dict):86 params = params.items()87 88 qs = urlparse.parse_qsl(query, keep_blank_values=True)89 qs.extend(params)90 return url_encode(qs)91 92 93def add_params_to_uri(uri, params, fragment=False):94 """Add a list of two-tuples to the uri query components."""95 sch, net, path, par, query, fra = urlparse.urlparse(uri)96 if fragment:97 fra = add_params_to_qs(fra, params)98 else:99 query = add_params_to_qs(query, params)100 return urlparse.urlunparse((sch, net, path, par, query, fra))101 102 103def quote(s, safe=b'/'):104 return to_unicode(_quote(to_bytes(s), safe))105 106 107def unquote(s):108 return to_unicode(_unquote(s))109 110 111def quote_url(s):112 return quote(s, b'~@#$&()*!+=:;,.?/\'')113 114 115def extract_params(raw):116 """Extract parameters and return them as a list of 2-tuples.117 118 Will successfully extract parameters from urlencoded query strings,119 dicts, or lists of 2-tuples. Empty strings/dicts/lists will return an120 empty list of parameters. Any other input will result in a return121 value of None.122 """123 if isinstance(raw, (list, tuple)):124 try:125 raw = dict(raw)126 except (TypeError, ValueError):127 return None128 129 if isinstance(raw, dict):130 params = []131 for k, v in raw.items():132 params.append((to_unicode(k), to_unicode(v)))133 return params134 135 if not raw:136 return None137 138 try:139 return url_decode(raw)140 except ValueError:141 return None142 143 144def is_valid_url(url):145 parsed = urlparse.urlparse(url)146 return parsed.scheme and parsed.hostname147 