Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1 | """Parse (absolute and relative) URLs. |
| 2 | |
Senthil Kumaran | fd41e08 | 2010-04-17 14:44:14 +0000 | [diff] [blame] | 3 | urlparse module is based upon the following RFC specifications. |
| 4 | |
| 5 | RFC 3986 (STD66): "Uniform Resource Identifiers" by T. Berners-Lee, R. Fielding |
| 6 | and L. Masinter, January 2005. |
| 7 | |
| 8 | RFC 2732 : "Format for Literal IPv6 Addresses in URL's by R.Hinden, B.Carpenter |
| 9 | and L.Masinter, December 1999. |
| 10 | |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 11 | RFC 2396: "Uniform Resource Identifiers (URI)": Generic Syntax by T. |
Senthil Kumaran | fd41e08 | 2010-04-17 14:44:14 +0000 | [diff] [blame] | 12 | Berners-Lee, R. Fielding, and L. Masinter, August 1998. |
| 13 | |
David Malcolm | ee25568 | 2010-12-02 16:41:00 +0000 | [diff] [blame] | 14 | RFC 2368: "The mailto URL scheme", by P.Hoffman , L Masinter, J. Zawinski, July 1998. |
Senthil Kumaran | fd41e08 | 2010-04-17 14:44:14 +0000 | [diff] [blame] | 15 | |
| 16 | RFC 1808: "Relative Uniform Resource Locators", by R. Fielding, UC Irvine, June |
| 17 | 1995. |
| 18 | |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 19 | RFC 1738: "Uniform Resource Locators (URL)" by T. Berners-Lee, L. Masinter, M. |
Senthil Kumaran | fd41e08 | 2010-04-17 14:44:14 +0000 | [diff] [blame] | 20 | McCahill, December 1994 |
| 21 | |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 22 | RFC 3986 is considered the current standard and any future changes to |
| 23 | urlparse module should conform with it. The urlparse module is |
| 24 | currently not entirely compliant with this RFC due to defacto |
| 25 | scenarios for parsing, and for backward compatibility purposes, some |
| 26 | parsing quirks from older RFCs are retained. The testcases in |
Senthil Kumaran | fd41e08 | 2010-04-17 14:44:14 +0000 | [diff] [blame] | 27 | test_urlparse.py provides a good indicator of parsing behavior. |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 28 | """ |
| 29 | |
Serhiy Storchaka | 8ea4616 | 2013-03-14 21:31:37 +0200 | [diff] [blame] | 30 | import re |
Facundo Batista | 2ac5de2 | 2008-07-07 18:24:11 +0000 | [diff] [blame] | 31 | import sys |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 32 | import collections |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 33 | import warnings |
Facundo Batista | 2ac5de2 | 2008-07-07 18:24:11 +0000 | [diff] [blame] | 34 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 35 | __all__ = ["urlparse", "urlunparse", "urljoin", "urldefrag", |
Senthil Kumaran | 0256b2a | 2010-10-25 16:36:20 +0000 | [diff] [blame] | 36 | "urlsplit", "urlunsplit", "urlencode", "parse_qs", |
| 37 | "parse_qsl", "quote", "quote_plus", "quote_from_bytes", |
Serhiy Storchaka | 1515450 | 2015-04-07 19:09:01 +0300 | [diff] [blame] | 38 | "unquote", "unquote_plus", "unquote_to_bytes", |
| 39 | "DefragResult", "ParseResult", "SplitResult", |
| 40 | "DefragResultBytes", "ParseResultBytes", "SplitResultBytes"] |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 41 | |
Senthil Kumaran | 906f533 | 2017-05-17 21:48:59 -0700 | [diff] [blame] | 42 | # A classification of schemes. |
| 43 | # The empty string classifies URLs with no scheme specified, |
| 44 | # being the default value returned by “urlsplit” and “urlparse”. |
| 45 | |
| 46 | uses_relative = ['', 'ftp', 'http', 'gopher', 'nntp', 'imap', |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 47 | 'wais', 'file', 'https', 'shttp', 'mms', |
Senthil Kumaran | 906f533 | 2017-05-17 21:48:59 -0700 | [diff] [blame] | 48 | 'prospero', 'rtsp', 'rtspu', 'sftp', |
Berker Peksag | f676748 | 2016-09-16 14:43:58 +0300 | [diff] [blame] | 49 | 'svn', 'svn+ssh', 'ws', 'wss'] |
Senthil Kumaran | 906f533 | 2017-05-17 21:48:59 -0700 | [diff] [blame] | 50 | |
| 51 | uses_netloc = ['', 'ftp', 'http', 'gopher', 'nntp', 'telnet', |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 52 | 'imap', 'wais', 'file', 'mms', 'https', 'shttp', |
Senthil Kumaran | 906f533 | 2017-05-17 21:48:59 -0700 | [diff] [blame] | 53 | 'snews', 'prospero', 'rtsp', 'rtspu', 'rsync', |
Berker Peksag | f676748 | 2016-09-16 14:43:58 +0300 | [diff] [blame] | 54 | 'svn', 'svn+ssh', 'sftp', 'nfs', 'git', 'git+ssh', |
| 55 | 'ws', 'wss'] |
Senthil Kumaran | 906f533 | 2017-05-17 21:48:59 -0700 | [diff] [blame] | 56 | |
| 57 | uses_params = ['', 'ftp', 'hdl', 'prospero', 'http', 'imap', |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 58 | 'https', 'shttp', 'rtsp', 'rtspu', 'sip', 'sips', |
Senthil Kumaran | 906f533 | 2017-05-17 21:48:59 -0700 | [diff] [blame] | 59 | 'mms', 'sftp', 'tel'] |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 60 | |
Georg Brandl | a61b09f | 2012-08-24 18:15:29 +0200 | [diff] [blame] | 61 | # These are not actually used anymore, but should stay for backwards |
| 62 | # compatibility. (They are undocumented, but have a public-looking name.) |
Senthil Kumaran | 906f533 | 2017-05-17 21:48:59 -0700 | [diff] [blame] | 63 | |
Georg Brandl | a61b09f | 2012-08-24 18:15:29 +0200 | [diff] [blame] | 64 | non_hierarchical = ['gopher', 'hdl', 'mailto', 'news', |
| 65 | 'telnet', 'wais', 'imap', 'snews', 'sip', 'sips'] |
Senthil Kumaran | 906f533 | 2017-05-17 21:48:59 -0700 | [diff] [blame] | 66 | |
| 67 | uses_query = ['', 'http', 'wais', 'imap', 'https', 'shttp', 'mms', |
| 68 | 'gopher', 'rtsp', 'rtspu', 'sip', 'sips'] |
| 69 | |
| 70 | uses_fragment = ['', 'ftp', 'hdl', 'http', 'gopher', 'news', |
Georg Brandl | a61b09f | 2012-08-24 18:15:29 +0200 | [diff] [blame] | 71 | 'nntp', 'wais', 'https', 'shttp', 'snews', |
Senthil Kumaran | 906f533 | 2017-05-17 21:48:59 -0700 | [diff] [blame] | 72 | 'file', 'prospero'] |
Georg Brandl | a61b09f | 2012-08-24 18:15:29 +0200 | [diff] [blame] | 73 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 74 | # Characters valid in scheme names |
| 75 | scheme_chars = ('abcdefghijklmnopqrstuvwxyz' |
| 76 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 77 | '0123456789' |
| 78 | '+-.') |
| 79 | |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 80 | # XXX: Consider replacing with functools.lru_cache |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 81 | MAX_CACHE_SIZE = 20 |
| 82 | _parse_cache = {} |
| 83 | |
| 84 | def clear_cache(): |
Antoine Pitrou | 2df5fc7 | 2009-12-08 19:38:17 +0000 | [diff] [blame] | 85 | """Clear the parse cache and the quoters cache.""" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 86 | _parse_cache.clear() |
Antoine Pitrou | 2df5fc7 | 2009-12-08 19:38:17 +0000 | [diff] [blame] | 87 | _safe_quoters.clear() |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 88 | |
| 89 | |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 90 | # Helpers for bytes handling |
| 91 | # For 3.2, we deliberately require applications that |
| 92 | # handle improperly quoted URLs to do their own |
| 93 | # decoding and encoding. If valid use cases are |
| 94 | # presented, we may relax this by using latin-1 |
| 95 | # decoding internally for 3.3 |
| 96 | _implicit_encoding = 'ascii' |
| 97 | _implicit_errors = 'strict' |
| 98 | |
| 99 | def _noop(obj): |
| 100 | return obj |
| 101 | |
| 102 | def _encode_result(obj, encoding=_implicit_encoding, |
| 103 | errors=_implicit_errors): |
| 104 | return obj.encode(encoding, errors) |
| 105 | |
| 106 | def _decode_args(args, encoding=_implicit_encoding, |
| 107 | errors=_implicit_errors): |
| 108 | return tuple(x.decode(encoding, errors) if x else '' for x in args) |
| 109 | |
| 110 | def _coerce_args(*args): |
| 111 | # Invokes decode if necessary to create str args |
| 112 | # and returns the coerced inputs along with |
| 113 | # an appropriate result coercion function |
| 114 | # - noop for str inputs |
| 115 | # - encoding function otherwise |
| 116 | str_input = isinstance(args[0], str) |
| 117 | for arg in args[1:]: |
| 118 | # We special-case the empty string to support the |
| 119 | # "scheme=''" default argument to some functions |
| 120 | if arg and isinstance(arg, str) != str_input: |
| 121 | raise TypeError("Cannot mix str and non-str arguments") |
| 122 | if str_input: |
| 123 | return args + (_noop,) |
| 124 | return _decode_args(args) + (_encode_result,) |
| 125 | |
| 126 | # Result objects are more helpful than simple tuples |
| 127 | class _ResultMixinStr(object): |
| 128 | """Standard approach to encoding parsed results from str to bytes""" |
| 129 | __slots__ = () |
| 130 | |
| 131 | def encode(self, encoding='ascii', errors='strict'): |
| 132 | return self._encoded_counterpart(*(x.encode(encoding, errors) for x in self)) |
| 133 | |
| 134 | |
| 135 | class _ResultMixinBytes(object): |
| 136 | """Standard approach to decoding parsed results from bytes to str""" |
| 137 | __slots__ = () |
| 138 | |
| 139 | def decode(self, encoding='ascii', errors='strict'): |
| 140 | return self._decoded_counterpart(*(x.decode(encoding, errors) for x in self)) |
| 141 | |
| 142 | |
| 143 | class _NetlocResultMixinBase(object): |
| 144 | """Shared methods for the parsed result objects containing a netloc element""" |
| 145 | __slots__ = () |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 146 | |
| 147 | @property |
| 148 | def username(self): |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 149 | return self._userinfo[0] |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 150 | |
| 151 | @property |
| 152 | def password(self): |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 153 | return self._userinfo[1] |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 154 | |
| 155 | @property |
| 156 | def hostname(self): |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 157 | hostname = self._hostinfo[0] |
| 158 | if not hostname: |
Коренберг Марк | fbd6051 | 2017-12-21 17:16:17 +0500 | [diff] [blame] | 159 | return None |
| 160 | # Scoped IPv6 address may have zone info, which must not be lowercased |
| 161 | # like http://[fe80::822a:a8ff:fe49:470c%tESt]:1234/keys |
| 162 | separator = '%' if isinstance(hostname, str) else b'%' |
| 163 | hostname, percent, zone = hostname.partition(separator) |
| 164 | return hostname.lower() + percent + zone |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 165 | |
| 166 | @property |
| 167 | def port(self): |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 168 | port = self._hostinfo[1] |
| 169 | if port is not None: |
Matt Eaton | 2cb4661 | 2018-03-20 01:41:37 -0500 | [diff] [blame] | 170 | try: |
| 171 | port = int(port, 10) |
| 172 | except ValueError: |
| 173 | message = f'Port could not be cast to integer value as {port!r}' |
| 174 | raise ValueError(message) from None |
Senthil Kumaran | 2fc5a50 | 2012-05-24 21:56:17 +0800 | [diff] [blame] | 175 | if not ( 0 <= port <= 65535): |
Robert Collins | dfa95c9 | 2015-08-10 09:53:30 +1200 | [diff] [blame] | 176 | raise ValueError("Port out of range 0-65535") |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 177 | return port |
| 178 | |
| 179 | |
| 180 | class _NetlocResultMixinStr(_NetlocResultMixinBase, _ResultMixinStr): |
| 181 | __slots__ = () |
| 182 | |
| 183 | @property |
| 184 | def _userinfo(self): |
| 185 | netloc = self.netloc |
| 186 | userinfo, have_info, hostinfo = netloc.rpartition('@') |
| 187 | if have_info: |
| 188 | username, have_password, password = userinfo.partition(':') |
| 189 | if not have_password: |
| 190 | password = None |
Senthil Kumaran | ad02d23 | 2010-04-16 03:02:13 +0000 | [diff] [blame] | 191 | else: |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 192 | username = password = None |
| 193 | return username, password |
| 194 | |
| 195 | @property |
| 196 | def _hostinfo(self): |
| 197 | netloc = self.netloc |
| 198 | _, _, hostinfo = netloc.rpartition('@') |
| 199 | _, have_open_br, bracketed = hostinfo.partition('[') |
| 200 | if have_open_br: |
| 201 | hostname, _, port = bracketed.partition(']') |
Serhiy Storchaka | ff97b08 | 2014-01-18 18:30:33 +0200 | [diff] [blame] | 202 | _, _, port = port.partition(':') |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 203 | else: |
Serhiy Storchaka | ff97b08 | 2014-01-18 18:30:33 +0200 | [diff] [blame] | 204 | hostname, _, port = hostinfo.partition(':') |
| 205 | if not port: |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 206 | port = None |
| 207 | return hostname, port |
| 208 | |
| 209 | |
| 210 | class _NetlocResultMixinBytes(_NetlocResultMixinBase, _ResultMixinBytes): |
| 211 | __slots__ = () |
| 212 | |
| 213 | @property |
| 214 | def _userinfo(self): |
| 215 | netloc = self.netloc |
| 216 | userinfo, have_info, hostinfo = netloc.rpartition(b'@') |
| 217 | if have_info: |
| 218 | username, have_password, password = userinfo.partition(b':') |
| 219 | if not have_password: |
| 220 | password = None |
| 221 | else: |
| 222 | username = password = None |
| 223 | return username, password |
| 224 | |
| 225 | @property |
| 226 | def _hostinfo(self): |
| 227 | netloc = self.netloc |
| 228 | _, _, hostinfo = netloc.rpartition(b'@') |
| 229 | _, have_open_br, bracketed = hostinfo.partition(b'[') |
| 230 | if have_open_br: |
| 231 | hostname, _, port = bracketed.partition(b']') |
Serhiy Storchaka | ff97b08 | 2014-01-18 18:30:33 +0200 | [diff] [blame] | 232 | _, _, port = port.partition(b':') |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 233 | else: |
Serhiy Storchaka | ff97b08 | 2014-01-18 18:30:33 +0200 | [diff] [blame] | 234 | hostname, _, port = hostinfo.partition(b':') |
| 235 | if not port: |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 236 | port = None |
| 237 | return hostname, port |
| 238 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 239 | |
| 240 | from collections import namedtuple |
| 241 | |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 242 | _DefragResultBase = namedtuple('DefragResult', 'url fragment') |
Senthil Kumaran | 86f7109 | 2016-01-14 00:11:39 -0800 | [diff] [blame] | 243 | _SplitResultBase = namedtuple( |
| 244 | 'SplitResult', 'scheme netloc path query fragment') |
| 245 | _ParseResultBase = namedtuple( |
| 246 | 'ParseResult', 'scheme netloc path params query fragment') |
| 247 | |
| 248 | _DefragResultBase.__doc__ = """ |
| 249 | DefragResult(url, fragment) |
| 250 | |
| 251 | A 2-tuple that contains the url without fragment identifier and the fragment |
| 252 | identifier as a separate argument. |
| 253 | """ |
| 254 | |
| 255 | _DefragResultBase.url.__doc__ = """The URL with no fragment identifier.""" |
| 256 | |
| 257 | _DefragResultBase.fragment.__doc__ = """ |
| 258 | Fragment identifier separated from URL, that allows indirect identification of a |
| 259 | secondary resource by reference to a primary resource and additional identifying |
| 260 | information. |
| 261 | """ |
| 262 | |
| 263 | _SplitResultBase.__doc__ = """ |
| 264 | SplitResult(scheme, netloc, path, query, fragment) |
| 265 | |
| 266 | A 5-tuple that contains the different components of a URL. Similar to |
| 267 | ParseResult, but does not split params. |
| 268 | """ |
| 269 | |
| 270 | _SplitResultBase.scheme.__doc__ = """Specifies URL scheme for the request.""" |
| 271 | |
| 272 | _SplitResultBase.netloc.__doc__ = """ |
| 273 | Network location where the request is made to. |
| 274 | """ |
| 275 | |
| 276 | _SplitResultBase.path.__doc__ = """ |
| 277 | The hierarchical path, such as the path to a file to download. |
| 278 | """ |
| 279 | |
| 280 | _SplitResultBase.query.__doc__ = """ |
| 281 | The query component, that contains non-hierarchical data, that along with data |
| 282 | in path component, identifies a resource in the scope of URI's scheme and |
| 283 | network location. |
| 284 | """ |
| 285 | |
| 286 | _SplitResultBase.fragment.__doc__ = """ |
| 287 | Fragment identifier, that allows indirect identification of a secondary resource |
| 288 | by reference to a primary resource and additional identifying information. |
| 289 | """ |
| 290 | |
| 291 | _ParseResultBase.__doc__ = """ |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 292 | ParseResult(scheme, netloc, path, params, query, fragment) |
Senthil Kumaran | 86f7109 | 2016-01-14 00:11:39 -0800 | [diff] [blame] | 293 | |
| 294 | A 6-tuple that contains components of a parsed URL. |
| 295 | """ |
| 296 | |
| 297 | _ParseResultBase.scheme.__doc__ = _SplitResultBase.scheme.__doc__ |
| 298 | _ParseResultBase.netloc.__doc__ = _SplitResultBase.netloc.__doc__ |
| 299 | _ParseResultBase.path.__doc__ = _SplitResultBase.path.__doc__ |
| 300 | _ParseResultBase.params.__doc__ = """ |
| 301 | Parameters for last path element used to dereference the URI in order to provide |
| 302 | access to perform some operation on the resource. |
| 303 | """ |
| 304 | |
| 305 | _ParseResultBase.query.__doc__ = _SplitResultBase.query.__doc__ |
| 306 | _ParseResultBase.fragment.__doc__ = _SplitResultBase.fragment.__doc__ |
| 307 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 308 | |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 309 | # For backwards compatibility, alias _NetlocResultMixinStr |
| 310 | # ResultBase is no longer part of the documented API, but it is |
| 311 | # retained since deprecating it isn't worth the hassle |
| 312 | ResultBase = _NetlocResultMixinStr |
| 313 | |
| 314 | # Structured result objects for string data |
| 315 | class DefragResult(_DefragResultBase, _ResultMixinStr): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 316 | __slots__ = () |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 317 | def geturl(self): |
| 318 | if self.fragment: |
| 319 | return self.url + '#' + self.fragment |
| 320 | else: |
| 321 | return self.url |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 322 | |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 323 | class SplitResult(_SplitResultBase, _NetlocResultMixinStr): |
| 324 | __slots__ = () |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 325 | def geturl(self): |
| 326 | return urlunsplit(self) |
| 327 | |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 328 | class ParseResult(_ParseResultBase, _NetlocResultMixinStr): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 329 | __slots__ = () |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 330 | def geturl(self): |
| 331 | return urlunparse(self) |
| 332 | |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 333 | # Structured result objects for bytes data |
| 334 | class DefragResultBytes(_DefragResultBase, _ResultMixinBytes): |
| 335 | __slots__ = () |
| 336 | def geturl(self): |
| 337 | if self.fragment: |
| 338 | return self.url + b'#' + self.fragment |
| 339 | else: |
| 340 | return self.url |
| 341 | |
| 342 | class SplitResultBytes(_SplitResultBase, _NetlocResultMixinBytes): |
| 343 | __slots__ = () |
| 344 | def geturl(self): |
| 345 | return urlunsplit(self) |
| 346 | |
| 347 | class ParseResultBytes(_ParseResultBase, _NetlocResultMixinBytes): |
| 348 | __slots__ = () |
| 349 | def geturl(self): |
| 350 | return urlunparse(self) |
| 351 | |
| 352 | # Set up the encode/decode result pairs |
| 353 | def _fix_result_transcoding(): |
| 354 | _result_pairs = ( |
| 355 | (DefragResult, DefragResultBytes), |
| 356 | (SplitResult, SplitResultBytes), |
| 357 | (ParseResult, ParseResultBytes), |
| 358 | ) |
| 359 | for _decoded, _encoded in _result_pairs: |
| 360 | _decoded._encoded_counterpart = _encoded |
| 361 | _encoded._decoded_counterpart = _decoded |
| 362 | |
| 363 | _fix_result_transcoding() |
| 364 | del _fix_result_transcoding |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 365 | |
| 366 | def urlparse(url, scheme='', allow_fragments=True): |
| 367 | """Parse a URL into 6 components: |
| 368 | <scheme>://<netloc>/<path>;<params>?<query>#<fragment> |
| 369 | Return a 6-tuple: (scheme, netloc, path, params, query, fragment). |
| 370 | Note that we don't break the components up in smaller bits |
| 371 | (e.g. netloc is a single string) and we don't expand % escapes.""" |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 372 | url, scheme, _coerce_result = _coerce_args(url, scheme) |
Senthil Kumaran | eda29f4 | 2012-06-29 11:08:20 -0700 | [diff] [blame] | 373 | splitresult = urlsplit(url, scheme, allow_fragments) |
| 374 | scheme, netloc, url, query, fragment = splitresult |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 375 | if scheme in uses_params and ';' in url: |
| 376 | url, params = _splitparams(url) |
| 377 | else: |
| 378 | params = '' |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 379 | result = ParseResult(scheme, netloc, url, params, query, fragment) |
| 380 | return _coerce_result(result) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 381 | |
| 382 | def _splitparams(url): |
| 383 | if '/' in url: |
| 384 | i = url.find(';', url.rfind('/')) |
| 385 | if i < 0: |
| 386 | return url, '' |
| 387 | else: |
| 388 | i = url.find(';') |
| 389 | return url[:i], url[i+1:] |
| 390 | |
| 391 | def _splitnetloc(url, start=0): |
| 392 | delim = len(url) # position of end of domain part of url, default is end |
| 393 | for c in '/?#': # look for delimiters; the order is NOT important |
| 394 | wdelim = url.find(c, start) # find first of this delim |
| 395 | if wdelim >= 0: # if found |
| 396 | delim = min(delim, wdelim) # use earliest delim position |
| 397 | return url[start:delim], url[delim:] # return (domain, rest) |
| 398 | |
| 399 | def urlsplit(url, scheme='', allow_fragments=True): |
| 400 | """Parse a URL into 5 components: |
| 401 | <scheme>://<netloc>/<path>?<query>#<fragment> |
| 402 | Return a 5-tuple: (scheme, netloc, path, query, fragment). |
| 403 | Note that we don't break the components up in smaller bits |
| 404 | (e.g. netloc is a single string) and we don't expand % escapes.""" |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 405 | url, scheme, _coerce_result = _coerce_args(url, scheme) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 406 | allow_fragments = bool(allow_fragments) |
| 407 | key = url, scheme, allow_fragments, type(url), type(scheme) |
| 408 | cached = _parse_cache.get(key, None) |
| 409 | if cached: |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 410 | return _coerce_result(cached) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 411 | if len(_parse_cache) >= MAX_CACHE_SIZE: # avoid runaway growth |
| 412 | clear_cache() |
| 413 | netloc = query = fragment = '' |
| 414 | i = url.find(':') |
| 415 | if i > 0: |
| 416 | if url[:i] == 'http': # optimize the common case |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 417 | url = url[i+1:] |
| 418 | if url[:2] == '//': |
| 419 | netloc, url = _splitnetloc(url, 2) |
Senthil Kumaran | 7a1e09f | 2010-04-22 12:19:46 +0000 | [diff] [blame] | 420 | if (('[' in netloc and ']' not in netloc) or |
| 421 | (']' in netloc and '[' not in netloc)): |
| 422 | raise ValueError("Invalid IPv6 URL") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 423 | if allow_fragments and '#' in url: |
| 424 | url, fragment = url.split('#', 1) |
| 425 | if '?' in url: |
| 426 | url, query = url.split('?', 1) |
Oren Milman | 8df44ee | 2017-09-03 07:51:39 +0300 | [diff] [blame] | 427 | v = SplitResult('http', netloc, url, query, fragment) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 428 | _parse_cache[key] = v |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 429 | return _coerce_result(v) |
Senthil Kumaran | 397eb44 | 2011-04-15 18:20:24 +0800 | [diff] [blame] | 430 | for c in url[:i]: |
| 431 | if c not in scheme_chars: |
| 432 | break |
| 433 | else: |
Ezio Melotti | 6709b7d | 2012-05-19 17:15:19 +0300 | [diff] [blame] | 434 | # make sure "url" is not actually a port number (in which case |
| 435 | # "scheme" is really part of the path) |
| 436 | rest = url[i+1:] |
| 437 | if not rest or any(c not in '0123456789' for c in rest): |
| 438 | # not a port number |
| 439 | scheme, url = url[:i].lower(), rest |
Senthil Kumaran | 397eb44 | 2011-04-15 18:20:24 +0800 | [diff] [blame] | 440 | |
Senthil Kumaran | 6be85c5 | 2010-02-19 07:42:50 +0000 | [diff] [blame] | 441 | if url[:2] == '//': |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 442 | netloc, url = _splitnetloc(url, 2) |
Senthil Kumaran | 7a1e09f | 2010-04-22 12:19:46 +0000 | [diff] [blame] | 443 | if (('[' in netloc and ']' not in netloc) or |
| 444 | (']' in netloc and '[' not in netloc)): |
| 445 | raise ValueError("Invalid IPv6 URL") |
Senthil Kumaran | 1be320e | 2012-05-19 08:12:00 +0800 | [diff] [blame] | 446 | if allow_fragments and '#' in url: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 447 | url, fragment = url.split('#', 1) |
Senthil Kumaran | 1be320e | 2012-05-19 08:12:00 +0800 | [diff] [blame] | 448 | if '?' in url: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 449 | url, query = url.split('?', 1) |
| 450 | v = SplitResult(scheme, netloc, url, query, fragment) |
| 451 | _parse_cache[key] = v |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 452 | return _coerce_result(v) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 453 | |
| 454 | def urlunparse(components): |
| 455 | """Put a parsed URL back together again. This may result in a |
| 456 | slightly different, but equivalent URL, if the URL that was parsed |
| 457 | originally had redundant delimiters, e.g. a ? with an empty query |
| 458 | (the draft states that these are equivalent).""" |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 459 | scheme, netloc, url, params, query, fragment, _coerce_result = ( |
| 460 | _coerce_args(*components)) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 461 | if params: |
| 462 | url = "%s;%s" % (url, params) |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 463 | return _coerce_result(urlunsplit((scheme, netloc, url, query, fragment))) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 464 | |
| 465 | def urlunsplit(components): |
Senthil Kumaran | 8749a63 | 2010-06-28 14:08:00 +0000 | [diff] [blame] | 466 | """Combine the elements of a tuple as returned by urlsplit() into a |
| 467 | complete URL as a string. The data argument can be any five-item iterable. |
| 468 | This may result in a slightly different, but equivalent URL, if the URL that |
| 469 | was parsed originally had unnecessary delimiters (for example, a ? with an |
| 470 | empty query; the RFC states that these are equivalent).""" |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 471 | scheme, netloc, url, query, fragment, _coerce_result = ( |
| 472 | _coerce_args(*components)) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 473 | if netloc or (scheme and scheme in uses_netloc and url[:2] != '//'): |
| 474 | if url and url[:1] != '/': url = '/' + url |
| 475 | url = '//' + (netloc or '') + url |
| 476 | if scheme: |
| 477 | url = scheme + ':' + url |
| 478 | if query: |
| 479 | url = url + '?' + query |
| 480 | if fragment: |
| 481 | url = url + '#' + fragment |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 482 | return _coerce_result(url) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 483 | |
| 484 | def urljoin(base, url, allow_fragments=True): |
| 485 | """Join a base URL and a possibly relative URL to form an absolute |
| 486 | interpretation of the latter.""" |
| 487 | if not base: |
| 488 | return url |
| 489 | if not url: |
| 490 | return base |
Antoine Pitrou | 55ac5b3 | 2014-08-21 19:16:17 -0400 | [diff] [blame] | 491 | |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 492 | base, url, _coerce_result = _coerce_args(base, url) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 493 | bscheme, bnetloc, bpath, bparams, bquery, bfragment = \ |
| 494 | urlparse(base, '', allow_fragments) |
| 495 | scheme, netloc, path, params, query, fragment = \ |
| 496 | urlparse(url, bscheme, allow_fragments) |
Antoine Pitrou | 55ac5b3 | 2014-08-21 19:16:17 -0400 | [diff] [blame] | 497 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 498 | if scheme != bscheme or scheme not in uses_relative: |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 499 | return _coerce_result(url) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 500 | if scheme in uses_netloc: |
| 501 | if netloc: |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 502 | return _coerce_result(urlunparse((scheme, netloc, path, |
| 503 | params, query, fragment))) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 504 | netloc = bnetloc |
Antoine Pitrou | 55ac5b3 | 2014-08-21 19:16:17 -0400 | [diff] [blame] | 505 | |
Senthil Kumaran | dca5b86 | 2010-12-17 04:48:45 +0000 | [diff] [blame] | 506 | if not path and not params: |
Facundo Batista | 23e3856 | 2008-08-14 16:55:14 +0000 | [diff] [blame] | 507 | path = bpath |
Senthil Kumaran | dca5b86 | 2010-12-17 04:48:45 +0000 | [diff] [blame] | 508 | params = bparams |
Facundo Batista | 23e3856 | 2008-08-14 16:55:14 +0000 | [diff] [blame] | 509 | if not query: |
| 510 | query = bquery |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 511 | return _coerce_result(urlunparse((scheme, netloc, path, |
| 512 | params, query, fragment))) |
Antoine Pitrou | 55ac5b3 | 2014-08-21 19:16:17 -0400 | [diff] [blame] | 513 | |
| 514 | base_parts = bpath.split('/') |
| 515 | if base_parts[-1] != '': |
| 516 | # the last item is not a directory, so will not be taken into account |
| 517 | # in resolving the relative path |
| 518 | del base_parts[-1] |
| 519 | |
| 520 | # for rfc3986, ignore all base path should the first character be root. |
| 521 | if path[:1] == '/': |
| 522 | segments = path.split('/') |
| 523 | else: |
| 524 | segments = base_parts + path.split('/') |
Senthil Kumaran | a66e388 | 2014-09-22 15:49:16 +0800 | [diff] [blame] | 525 | # filter out elements that would cause redundant slashes on re-joining |
| 526 | # the resolved_path |
Berker Peksag | 20416f7 | 2015-04-16 02:31:14 +0300 | [diff] [blame] | 527 | segments[1:-1] = filter(None, segments[1:-1]) |
Antoine Pitrou | 55ac5b3 | 2014-08-21 19:16:17 -0400 | [diff] [blame] | 528 | |
| 529 | resolved_path = [] |
| 530 | |
| 531 | for seg in segments: |
| 532 | if seg == '..': |
| 533 | try: |
| 534 | resolved_path.pop() |
| 535 | except IndexError: |
| 536 | # ignore any .. segments that would otherwise cause an IndexError |
| 537 | # when popped from resolved_path if resolving for rfc3986 |
| 538 | pass |
| 539 | elif seg == '.': |
| 540 | continue |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 541 | else: |
Antoine Pitrou | 55ac5b3 | 2014-08-21 19:16:17 -0400 | [diff] [blame] | 542 | resolved_path.append(seg) |
| 543 | |
| 544 | if segments[-1] in ('.', '..'): |
| 545 | # do some post-processing here. if the last segment was a relative dir, |
| 546 | # then we need to append the trailing '/' |
| 547 | resolved_path.append('') |
| 548 | |
| 549 | return _coerce_result(urlunparse((scheme, netloc, '/'.join( |
Senthil Kumaran | a66e388 | 2014-09-22 15:49:16 +0800 | [diff] [blame] | 550 | resolved_path) or '/', params, query, fragment))) |
Antoine Pitrou | 55ac5b3 | 2014-08-21 19:16:17 -0400 | [diff] [blame] | 551 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 552 | |
| 553 | def urldefrag(url): |
| 554 | """Removes any existing fragment from URL. |
| 555 | |
| 556 | Returns a tuple of the defragmented URL and the fragment. If |
| 557 | the URL contained no fragments, the second element is the |
| 558 | empty string. |
| 559 | """ |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 560 | url, _coerce_result = _coerce_args(url) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 561 | if '#' in url: |
| 562 | s, n, p, a, q, frag = urlparse(url) |
| 563 | defrag = urlunparse((s, n, p, a, q, '')) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 564 | else: |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 565 | frag = '' |
| 566 | defrag = url |
| 567 | return _coerce_result(DefragResult(defrag, frag)) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 568 | |
Serhiy Storchaka | 8ea4616 | 2013-03-14 21:31:37 +0200 | [diff] [blame] | 569 | _hexdig = '0123456789ABCDEFabcdef' |
Victor Stinner | d6a91a7 | 2014-03-17 22:38:41 +0100 | [diff] [blame] | 570 | _hextobyte = None |
Serhiy Storchaka | 8ea4616 | 2013-03-14 21:31:37 +0200 | [diff] [blame] | 571 | |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 572 | def unquote_to_bytes(string): |
| 573 | """unquote_to_bytes('abc%20def') -> b'abc def'.""" |
| 574 | # Note: strings are encoded as UTF-8. This is only an issue if it contains |
| 575 | # unescaped non-ASCII characters, which URIs should not. |
Florent Xicluna | 82a3f8a | 2010-08-14 18:30:35 +0000 | [diff] [blame] | 576 | if not string: |
| 577 | # Is it a string-like object? |
| 578 | string.split |
Florent Xicluna | c7b8e86 | 2010-05-17 17:33:07 +0000 | [diff] [blame] | 579 | return b'' |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 580 | if isinstance(string, str): |
| 581 | string = string.encode('utf-8') |
Serhiy Storchaka | 8ea4616 | 2013-03-14 21:31:37 +0200 | [diff] [blame] | 582 | bits = string.split(b'%') |
| 583 | if len(bits) == 1: |
Florent Xicluna | c7b8e86 | 2010-05-17 17:33:07 +0000 | [diff] [blame] | 584 | return string |
Serhiy Storchaka | 8ea4616 | 2013-03-14 21:31:37 +0200 | [diff] [blame] | 585 | res = [bits[0]] |
| 586 | append = res.append |
Victor Stinner | d6a91a7 | 2014-03-17 22:38:41 +0100 | [diff] [blame] | 587 | # Delay the initialization of the table to not waste memory |
| 588 | # if the function is never called |
| 589 | global _hextobyte |
| 590 | if _hextobyte is None: |
Serhiy Storchaka | 8cbd3df | 2016-12-21 12:59:28 +0200 | [diff] [blame] | 591 | _hextobyte = {(a + b).encode(): bytes.fromhex(a + b) |
Victor Stinner | d6a91a7 | 2014-03-17 22:38:41 +0100 | [diff] [blame] | 592 | for a in _hexdig for b in _hexdig} |
Serhiy Storchaka | 8ea4616 | 2013-03-14 21:31:37 +0200 | [diff] [blame] | 593 | for item in bits[1:]: |
Guido van Rossum | df9f1ec | 2008-08-06 19:31:34 +0000 | [diff] [blame] | 594 | try: |
Serhiy Storchaka | 8ea4616 | 2013-03-14 21:31:37 +0200 | [diff] [blame] | 595 | append(_hextobyte[item[:2]]) |
| 596 | append(item[2:]) |
| 597 | except KeyError: |
| 598 | append(b'%') |
| 599 | append(item) |
| 600 | return b''.join(res) |
| 601 | |
| 602 | _asciire = re.compile('([\x00-\x7f]+)') |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 603 | |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 604 | def unquote(string, encoding='utf-8', errors='replace'): |
| 605 | """Replace %xx escapes by their single-character equivalent. The optional |
| 606 | encoding and errors parameters specify how to decode percent-encoded |
| 607 | sequences into Unicode characters, as accepted by the bytes.decode() |
| 608 | method. |
| 609 | By default, percent-encoded sequences are decoded with UTF-8, and invalid |
| 610 | sequences are replaced by a placeholder character. |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 611 | |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 612 | unquote('abc%20def') -> 'abc def'. |
| 613 | """ |
Serhiy Storchaka | 8ea4616 | 2013-03-14 21:31:37 +0200 | [diff] [blame] | 614 | if '%' not in string: |
| 615 | string.split |
Florent Xicluna | c7b8e86 | 2010-05-17 17:33:07 +0000 | [diff] [blame] | 616 | return string |
| 617 | if encoding is None: |
| 618 | encoding = 'utf-8' |
| 619 | if errors is None: |
| 620 | errors = 'replace' |
Serhiy Storchaka | 8ea4616 | 2013-03-14 21:31:37 +0200 | [diff] [blame] | 621 | bits = _asciire.split(string) |
| 622 | res = [bits[0]] |
| 623 | append = res.append |
| 624 | for i in range(1, len(bits), 2): |
| 625 | append(unquote_to_bytes(bits[i]).decode(encoding, errors)) |
| 626 | append(bits[i + 1]) |
| 627 | return ''.join(res) |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 628 | |
Senthil Kumaran | 257b980 | 2017-04-04 21:19:43 -0700 | [diff] [blame] | 629 | |
Victor Stinner | ac71c54 | 2011-01-14 12:52:12 +0000 | [diff] [blame] | 630 | def parse_qs(qs, keep_blank_values=False, strict_parsing=False, |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 631 | encoding='utf-8', errors='replace', max_num_fields=None): |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 632 | """Parse a query given as a string argument. |
| 633 | |
| 634 | Arguments: |
| 635 | |
Senthil Kumaran | 30e86a4 | 2010-08-09 20:01:35 +0000 | [diff] [blame] | 636 | qs: percent-encoded query string to be parsed |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 637 | |
| 638 | keep_blank_values: flag indicating whether blank values in |
Senthil Kumaran | 30e86a4 | 2010-08-09 20:01:35 +0000 | [diff] [blame] | 639 | percent-encoded queries should be treated as blank strings. |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 640 | A true value indicates that blanks should be retained as |
| 641 | blank strings. The default false value indicates that |
| 642 | blank values are to be ignored and treated as if they were |
| 643 | not included. |
| 644 | |
| 645 | strict_parsing: flag indicating what to do with parsing errors. |
| 646 | If false (the default), errors are silently ignored. |
| 647 | If true, errors raise a ValueError exception. |
Victor Stinner | ac71c54 | 2011-01-14 12:52:12 +0000 | [diff] [blame] | 648 | |
| 649 | encoding and errors: specify how to decode percent-encoded sequences |
| 650 | into Unicode characters, as accepted by the bytes.decode() method. |
Senthil Kumaran | 257b980 | 2017-04-04 21:19:43 -0700 | [diff] [blame] | 651 | |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 652 | max_num_fields: int. If set, then throws a ValueError if there |
| 653 | are more than n fields read by parse_qsl(). |
| 654 | |
Senthil Kumaran | 257b980 | 2017-04-04 21:19:43 -0700 | [diff] [blame] | 655 | Returns a dictionary. |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 656 | """ |
Senthil Kumaran | eda29f4 | 2012-06-29 11:08:20 -0700 | [diff] [blame] | 657 | parsed_result = {} |
Victor Stinner | ac71c54 | 2011-01-14 12:52:12 +0000 | [diff] [blame] | 658 | pairs = parse_qsl(qs, keep_blank_values, strict_parsing, |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 659 | encoding=encoding, errors=errors, |
| 660 | max_num_fields=max_num_fields) |
Victor Stinner | ac71c54 | 2011-01-14 12:52:12 +0000 | [diff] [blame] | 661 | for name, value in pairs: |
Senthil Kumaran | eda29f4 | 2012-06-29 11:08:20 -0700 | [diff] [blame] | 662 | if name in parsed_result: |
| 663 | parsed_result[name].append(value) |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 664 | else: |
Senthil Kumaran | eda29f4 | 2012-06-29 11:08:20 -0700 | [diff] [blame] | 665 | parsed_result[name] = [value] |
| 666 | return parsed_result |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 667 | |
Senthil Kumaran | 257b980 | 2017-04-04 21:19:43 -0700 | [diff] [blame] | 668 | |
Victor Stinner | ac71c54 | 2011-01-14 12:52:12 +0000 | [diff] [blame] | 669 | def parse_qsl(qs, keep_blank_values=False, strict_parsing=False, |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 670 | encoding='utf-8', errors='replace', max_num_fields=None): |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 671 | """Parse a query given as a string argument. |
| 672 | |
Senthil Kumaran | 257b980 | 2017-04-04 21:19:43 -0700 | [diff] [blame] | 673 | Arguments: |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 674 | |
Senthil Kumaran | 257b980 | 2017-04-04 21:19:43 -0700 | [diff] [blame] | 675 | qs: percent-encoded query string to be parsed |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 676 | |
Senthil Kumaran | 257b980 | 2017-04-04 21:19:43 -0700 | [diff] [blame] | 677 | keep_blank_values: flag indicating whether blank values in |
| 678 | percent-encoded queries should be treated as blank strings. |
| 679 | A true value indicates that blanks should be retained as blank |
| 680 | strings. The default false value indicates that blank values |
| 681 | are to be ignored and treated as if they were not included. |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 682 | |
Senthil Kumaran | 257b980 | 2017-04-04 21:19:43 -0700 | [diff] [blame] | 683 | strict_parsing: flag indicating what to do with parsing errors. If |
| 684 | false (the default), errors are silently ignored. If true, |
| 685 | errors raise a ValueError exception. |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 686 | |
Senthil Kumaran | 257b980 | 2017-04-04 21:19:43 -0700 | [diff] [blame] | 687 | encoding and errors: specify how to decode percent-encoded sequences |
| 688 | into Unicode characters, as accepted by the bytes.decode() method. |
Victor Stinner | ac71c54 | 2011-01-14 12:52:12 +0000 | [diff] [blame] | 689 | |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 690 | max_num_fields: int. If set, then throws a ValueError |
| 691 | if there are more than n fields read by parse_qsl(). |
| 692 | |
Senthil Kumaran | 257b980 | 2017-04-04 21:19:43 -0700 | [diff] [blame] | 693 | Returns a list, as G-d intended. |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 694 | """ |
Nick Coghlan | 9fc443c | 2010-11-30 15:48:08 +0000 | [diff] [blame] | 695 | qs, _coerce_result = _coerce_args(qs) |
matthewbelisle-wf | 2091448 | 2018-10-19 05:52:59 -0500 | [diff] [blame] | 696 | |
| 697 | # If max_num_fields is defined then check that the number of fields |
| 698 | # is less than max_num_fields. This prevents a memory exhaustion DOS |
| 699 | # attack via post bodies with many fields. |
| 700 | if max_num_fields is not None: |
| 701 | num_fields = 1 + qs.count('&') + qs.count(';') |
| 702 | if max_num_fields < num_fields: |
| 703 | raise ValueError('Max number of fields exceeded') |
| 704 | |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 705 | pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')] |
| 706 | r = [] |
| 707 | for name_value in pairs: |
| 708 | if not name_value and not strict_parsing: |
| 709 | continue |
| 710 | nv = name_value.split('=', 1) |
| 711 | if len(nv) != 2: |
| 712 | if strict_parsing: |
| 713 | raise ValueError("bad query field: %r" % (name_value,)) |
| 714 | # Handle case of a control-name with no equal sign |
| 715 | if keep_blank_values: |
| 716 | nv.append('') |
| 717 | else: |
| 718 | continue |
| 719 | if len(nv[1]) or keep_blank_values: |
Victor Stinner | ac71c54 | 2011-01-14 12:52:12 +0000 | [diff] [blame] | 720 | name = nv[0].replace('+', ' ') |
| 721 | name = unquote(name, encoding=encoding, errors=errors) |
| 722 | name = _coerce_result(name) |
| 723 | value = nv[1].replace('+', ' ') |
| 724 | value = unquote(value, encoding=encoding, errors=errors) |
| 725 | value = _coerce_result(value) |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 726 | r.append((name, value)) |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 727 | return r |
| 728 | |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 729 | def unquote_plus(string, encoding='utf-8', errors='replace'): |
| 730 | """Like unquote(), but also replace plus signs by spaces, as required for |
| 731 | unquoting HTML form values. |
| 732 | |
| 733 | unquote_plus('%7e/abc+def') -> '~/abc def' |
| 734 | """ |
| 735 | string = string.replace('+', ' ') |
| 736 | return unquote(string, encoding, errors) |
| 737 | |
| 738 | _ALWAYS_SAFE = frozenset(b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 739 | b'abcdefghijklmnopqrstuvwxyz' |
| 740 | b'0123456789' |
Ratnadeep Debnath | 21024f0 | 2017-02-25 14:30:28 +0530 | [diff] [blame] | 741 | b'_.-~') |
Florent Xicluna | c7b8e86 | 2010-05-17 17:33:07 +0000 | [diff] [blame] | 742 | _ALWAYS_SAFE_BYTES = bytes(_ALWAYS_SAFE) |
| 743 | _safe_quoters = {} |
Guido van Rossum | df9f1ec | 2008-08-06 19:31:34 +0000 | [diff] [blame] | 744 | |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 745 | class Quoter(collections.defaultdict): |
| 746 | """A mapping from bytes (in range(0,256)) to strings. |
| 747 | |
| 748 | String values are percent-encoded byte values, unless the key < 128, and |
| 749 | in the "safe" set (either the specified safe set, or default set). |
| 750 | """ |
| 751 | # Keeps a cache internally, using defaultdict, for efficiency (lookups |
| 752 | # of cached keys don't call Python code at all). |
Guido van Rossum | df9f1ec | 2008-08-06 19:31:34 +0000 | [diff] [blame] | 753 | def __init__(self, safe): |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 754 | """safe: bytes object.""" |
Florent Xicluna | c7b8e86 | 2010-05-17 17:33:07 +0000 | [diff] [blame] | 755 | self.safe = _ALWAYS_SAFE.union(safe) |
Guido van Rossum | df9f1ec | 2008-08-06 19:31:34 +0000 | [diff] [blame] | 756 | |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 757 | def __repr__(self): |
| 758 | # Without this, will just display as a defaultdict |
Serhiy Storchaka | 465e60e | 2014-07-25 23:36:00 +0300 | [diff] [blame] | 759 | return "<%s %r>" % (self.__class__.__name__, dict(self)) |
Guido van Rossum | df9f1ec | 2008-08-06 19:31:34 +0000 | [diff] [blame] | 760 | |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 761 | def __missing__(self, b): |
| 762 | # Handle a cache miss. Store quoted string in cache and return. |
Florent Xicluna | c7b8e86 | 2010-05-17 17:33:07 +0000 | [diff] [blame] | 763 | res = chr(b) if b in self.safe else '%{:02X}'.format(b) |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 764 | self[b] = res |
| 765 | return res |
| 766 | |
| 767 | def quote(string, safe='/', encoding=None, errors=None): |
Guido van Rossum | df9f1ec | 2008-08-06 19:31:34 +0000 | [diff] [blame] | 768 | """quote('abc def') -> 'abc%20def' |
| 769 | |
| 770 | Each part of a URL, e.g. the path info, the query, etc., has a |
| 771 | different set of reserved characters that must be quoted. |
| 772 | |
Ratnadeep Debnath | 21024f0 | 2017-02-25 14:30:28 +0530 | [diff] [blame] | 773 | RFC 3986 Uniform Resource Identifiers (URI): Generic Syntax lists |
Guido van Rossum | df9f1ec | 2008-08-06 19:31:34 +0000 | [diff] [blame] | 774 | the following reserved characters. |
| 775 | |
| 776 | reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | |
Ratnadeep Debnath | 21024f0 | 2017-02-25 14:30:28 +0530 | [diff] [blame] | 777 | "$" | "," | "~" |
Guido van Rossum | df9f1ec | 2008-08-06 19:31:34 +0000 | [diff] [blame] | 778 | |
| 779 | Each of these characters is reserved in some component of a URL, |
| 780 | but not necessarily in all of them. |
| 781 | |
Ratnadeep Debnath | 21024f0 | 2017-02-25 14:30:28 +0530 | [diff] [blame] | 782 | Python 3.7 updates from using RFC 2396 to RFC 3986 to quote URL strings. |
| 783 | Now, "~" is included in the set of reserved characters. |
| 784 | |
Guido van Rossum | df9f1ec | 2008-08-06 19:31:34 +0000 | [diff] [blame] | 785 | By default, the quote function is intended for quoting the path |
| 786 | section of a URL. Thus, it will not encode '/'. This character |
| 787 | is reserved, but in typical usage the quote function is being |
| 788 | called on a path where the existing slash characters are used as |
| 789 | reserved characters. |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 790 | |
R David Murray | 8c4e112 | 2014-12-24 21:23:18 -0500 | [diff] [blame] | 791 | string and safe may be either str or bytes objects. encoding and errors |
| 792 | must not be specified if string is a bytes object. |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 793 | |
| 794 | The optional encoding and errors parameters specify how to deal with |
| 795 | non-ASCII characters, as accepted by the str.encode method. |
| 796 | By default, encoding='utf-8' (characters are encoded with UTF-8), and |
| 797 | errors='strict' (unsupported characters raise a UnicodeEncodeError). |
Guido van Rossum | df9f1ec | 2008-08-06 19:31:34 +0000 | [diff] [blame] | 798 | """ |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 799 | if isinstance(string, str): |
Florent Xicluna | c7b8e86 | 2010-05-17 17:33:07 +0000 | [diff] [blame] | 800 | if not string: |
| 801 | return string |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 802 | if encoding is None: |
| 803 | encoding = 'utf-8' |
| 804 | if errors is None: |
| 805 | errors = 'strict' |
| 806 | string = string.encode(encoding, errors) |
| 807 | else: |
| 808 | if encoding is not None: |
| 809 | raise TypeError("quote() doesn't support 'encoding' for bytes") |
| 810 | if errors is not None: |
| 811 | raise TypeError("quote() doesn't support 'errors' for bytes") |
| 812 | return quote_from_bytes(string, safe) |
| 813 | |
| 814 | def quote_plus(string, safe='', encoding=None, errors=None): |
| 815 | """Like quote(), but also replace ' ' with '+', as required for quoting |
| 816 | HTML form values. Plus signs in the original string are escaped unless |
| 817 | they are included in safe. It also does not have safe default to '/'. |
| 818 | """ |
Jeremy Hylton | f819886 | 2009-03-26 16:55:08 +0000 | [diff] [blame] | 819 | # Check if ' ' in string, where string may either be a str or bytes. If |
| 820 | # there are no spaces, the regular quote will produce the right answer. |
| 821 | if ((isinstance(string, str) and ' ' not in string) or |
| 822 | (isinstance(string, bytes) and b' ' not in string)): |
| 823 | return quote(string, safe, encoding, errors) |
| 824 | if isinstance(safe, str): |
| 825 | space = ' ' |
| 826 | else: |
| 827 | space = b' ' |
Georg Brandl | faf4149 | 2009-05-26 18:31:11 +0000 | [diff] [blame] | 828 | string = quote(string, safe + space, encoding, errors) |
Jeremy Hylton | f819886 | 2009-03-26 16:55:08 +0000 | [diff] [blame] | 829 | return string.replace(' ', '+') |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 830 | |
| 831 | def quote_from_bytes(bs, safe='/'): |
| 832 | """Like quote(), but accepts a bytes object rather than a str, and does |
| 833 | not perform string-to-bytes encoding. It always returns an ASCII string. |
Senthil Kumaran | ffa4b2c | 2012-05-26 09:53:32 +0800 | [diff] [blame] | 834 | quote_from_bytes(b'abc def\x3f') -> 'abc%20def%3f' |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 835 | """ |
Florent Xicluna | c7b8e86 | 2010-05-17 17:33:07 +0000 | [diff] [blame] | 836 | if not isinstance(bs, (bytes, bytearray)): |
| 837 | raise TypeError("quote_from_bytes() expected bytes") |
| 838 | if not bs: |
| 839 | return '' |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 840 | if isinstance(safe, str): |
| 841 | # Normalize 'safe' by converting to bytes and removing non-ASCII chars |
| 842 | safe = safe.encode('ascii', 'ignore') |
Florent Xicluna | c7b8e86 | 2010-05-17 17:33:07 +0000 | [diff] [blame] | 843 | else: |
| 844 | safe = bytes([c for c in safe if c < 128]) |
| 845 | if not bs.rstrip(_ALWAYS_SAFE_BYTES + safe): |
| 846 | return bs.decode() |
Guido van Rossum | df9f1ec | 2008-08-06 19:31:34 +0000 | [diff] [blame] | 847 | try: |
Florent Xicluna | c7b8e86 | 2010-05-17 17:33:07 +0000 | [diff] [blame] | 848 | quoter = _safe_quoters[safe] |
Guido van Rossum | df9f1ec | 2008-08-06 19:31:34 +0000 | [diff] [blame] | 849 | except KeyError: |
Florent Xicluna | c7b8e86 | 2010-05-17 17:33:07 +0000 | [diff] [blame] | 850 | _safe_quoters[safe] = quoter = Quoter(safe).__getitem__ |
| 851 | return ''.join([quoter(char) for char in bs]) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 852 | |
R David Murray | c17686f | 2015-05-17 20:44:50 -0400 | [diff] [blame] | 853 | def urlencode(query, doseq=False, safe='', encoding=None, errors=None, |
| 854 | quote_via=quote_plus): |
Senthil Kumaran | 324ae385 | 2013-09-05 21:42:38 -0700 | [diff] [blame] | 855 | """Encode a dict or sequence of two-element tuples into a URL query string. |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 856 | |
| 857 | If any values in the query arg are sequences and doseq is true, each |
| 858 | sequence element is converted to a separate parameter. |
| 859 | |
| 860 | If the query arg is a sequence of two-element tuples, the order of the |
| 861 | parameters in the output will match the order of parameters in the |
| 862 | input. |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 863 | |
Senthil Kumaran | 324ae385 | 2013-09-05 21:42:38 -0700 | [diff] [blame] | 864 | The components of a query arg may each be either a string or a bytes type. |
R David Murray | 8c4e112 | 2014-12-24 21:23:18 -0500 | [diff] [blame] | 865 | |
R David Murray | c17686f | 2015-05-17 20:44:50 -0400 | [diff] [blame] | 866 | The safe, encoding, and errors parameters are passed down to the function |
| 867 | specified by quote_via (encoding and errors only if a component is a str). |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 868 | """ |
| 869 | |
Jeremy Hylton | a4de60a | 2009-03-26 14:49:26 +0000 | [diff] [blame] | 870 | if hasattr(query, "items"): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 871 | query = query.items() |
| 872 | else: |
Jeremy Hylton | 230feba | 2009-03-26 16:56:59 +0000 | [diff] [blame] | 873 | # It's a bother at times that strings and string-like objects are |
| 874 | # sequences. |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 875 | try: |
| 876 | # non-sequence items should not work with len() |
| 877 | # non-empty strings will fail this |
| 878 | if len(query) and not isinstance(query[0], tuple): |
| 879 | raise TypeError |
Jeremy Hylton | 230feba | 2009-03-26 16:56:59 +0000 | [diff] [blame] | 880 | # Zero-length sequences of all types will get here and succeed, |
| 881 | # but that's a minor nit. Since the original implementation |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 882 | # allowed empty dicts that type of behavior probably should be |
| 883 | # preserved for consistency |
| 884 | except TypeError: |
Jeremy Hylton | a4de60a | 2009-03-26 14:49:26 +0000 | [diff] [blame] | 885 | ty, va, tb = sys.exc_info() |
| 886 | raise TypeError("not a valid non-string sequence " |
| 887 | "or mapping object").with_traceback(tb) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 888 | |
| 889 | l = [] |
| 890 | if not doseq: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 891 | for k, v in query: |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 892 | if isinstance(k, bytes): |
R David Murray | c17686f | 2015-05-17 20:44:50 -0400 | [diff] [blame] | 893 | k = quote_via(k, safe) |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 894 | else: |
R David Murray | c17686f | 2015-05-17 20:44:50 -0400 | [diff] [blame] | 895 | k = quote_via(str(k), safe, encoding, errors) |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 896 | |
| 897 | if isinstance(v, bytes): |
R David Murray | c17686f | 2015-05-17 20:44:50 -0400 | [diff] [blame] | 898 | v = quote_via(v, safe) |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 899 | else: |
R David Murray | c17686f | 2015-05-17 20:44:50 -0400 | [diff] [blame] | 900 | v = quote_via(str(v), safe, encoding, errors) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 901 | l.append(k + '=' + v) |
| 902 | else: |
| 903 | for k, v in query: |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 904 | if isinstance(k, bytes): |
R David Murray | c17686f | 2015-05-17 20:44:50 -0400 | [diff] [blame] | 905 | k = quote_via(k, safe) |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 906 | else: |
R David Murray | c17686f | 2015-05-17 20:44:50 -0400 | [diff] [blame] | 907 | k = quote_via(str(k), safe, encoding, errors) |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 908 | |
| 909 | if isinstance(v, bytes): |
R David Murray | c17686f | 2015-05-17 20:44:50 -0400 | [diff] [blame] | 910 | v = quote_via(v, safe) |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 911 | l.append(k + '=' + v) |
| 912 | elif isinstance(v, str): |
R David Murray | c17686f | 2015-05-17 20:44:50 -0400 | [diff] [blame] | 913 | v = quote_via(v, safe, encoding, errors) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 914 | l.append(k + '=' + v) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 915 | else: |
| 916 | try: |
Jeremy Hylton | 230feba | 2009-03-26 16:56:59 +0000 | [diff] [blame] | 917 | # Is this a sufficient test for sequence-ness? |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 918 | x = len(v) |
| 919 | except TypeError: |
| 920 | # not a sequence |
R David Murray | c17686f | 2015-05-17 20:44:50 -0400 | [diff] [blame] | 921 | v = quote_via(str(v), safe, encoding, errors) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 922 | l.append(k + '=' + v) |
| 923 | else: |
| 924 | # loop over the sequence |
| 925 | for elt in v: |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 926 | if isinstance(elt, bytes): |
R David Murray | c17686f | 2015-05-17 20:44:50 -0400 | [diff] [blame] | 927 | elt = quote_via(elt, safe) |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 928 | else: |
R David Murray | c17686f | 2015-05-17 20:44:50 -0400 | [diff] [blame] | 929 | elt = quote_via(str(elt), safe, encoding, errors) |
Senthil Kumaran | df022da | 2010-07-03 17:48:22 +0000 | [diff] [blame] | 930 | l.append(k + '=' + elt) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 931 | return '&'.join(l) |
| 932 | |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 933 | |
Georg Brandl | 13e8946 | 2008-07-01 19:56:00 +0000 | [diff] [blame] | 934 | def to_bytes(url): |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 935 | warnings.warn("urllib.parse.to_bytes() is deprecated as of 3.8", |
| 936 | DeprecationWarning, stacklevel=2) |
| 937 | return _to_bytes(url) |
| 938 | |
| 939 | |
| 940 | def _to_bytes(url): |
Georg Brandl | 13e8946 | 2008-07-01 19:56:00 +0000 | [diff] [blame] | 941 | """to_bytes(u"URL") --> 'URL'.""" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 942 | # Most URL schemes require ASCII. If that changes, the conversion |
| 943 | # can be relaxed. |
Georg Brandl | 13e8946 | 2008-07-01 19:56:00 +0000 | [diff] [blame] | 944 | # XXX get rid of to_bytes() |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 945 | if isinstance(url, str): |
| 946 | try: |
| 947 | url = url.encode("ASCII").decode() |
| 948 | except UnicodeError: |
| 949 | raise UnicodeError("URL " + repr(url) + |
| 950 | " contains non-ASCII characters") |
| 951 | return url |
| 952 | |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 953 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 954 | def unwrap(url): |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 955 | warnings.warn("urllib.parse.unwrap() is deprecated as of 3.8", |
| 956 | DeprecationWarning, stacklevel=2) |
| 957 | return _unwrap(url) |
| 958 | |
| 959 | |
| 960 | def _unwrap(url): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 961 | """unwrap('<URL:type://host/path>') --> 'type://host/path'.""" |
| 962 | url = str(url).strip() |
| 963 | if url[:1] == '<' and url[-1:] == '>': |
| 964 | url = url[1:-1].strip() |
| 965 | if url[:4] == 'URL:': url = url[4:].strip() |
| 966 | return url |
| 967 | |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 968 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 969 | def splittype(url): |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 970 | warnings.warn("urllib.parse.splittype() is deprecated as of 3.8, " |
| 971 | "use urllib.parse.urlparse() instead", |
| 972 | DeprecationWarning, stacklevel=2) |
| 973 | return _splittype(url) |
| 974 | |
| 975 | |
| 976 | _typeprog = None |
| 977 | def _splittype(url): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 978 | """splittype('type:opaquestring') --> 'type', 'opaquestring'.""" |
| 979 | global _typeprog |
| 980 | if _typeprog is None: |
Serhiy Storchaka | 44eceb6 | 2015-03-03 20:21:35 +0200 | [diff] [blame] | 981 | _typeprog = re.compile('([^/:]+):(.*)', re.DOTALL) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 982 | |
| 983 | match = _typeprog.match(url) |
| 984 | if match: |
Serhiy Storchaka | 44eceb6 | 2015-03-03 20:21:35 +0200 | [diff] [blame] | 985 | scheme, data = match.groups() |
| 986 | return scheme.lower(), data |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 987 | return None, url |
| 988 | |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 989 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 990 | def splithost(url): |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 991 | warnings.warn("urllib.parse.splithost() is deprecated as of 3.8, " |
| 992 | "use urllib.parse.urlparse() instead", |
| 993 | DeprecationWarning, stacklevel=2) |
| 994 | return _splithost(url) |
| 995 | |
| 996 | |
| 997 | _hostprog = None |
| 998 | def _splithost(url): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 999 | """splithost('//host[:port]/path') --> 'host[:port]', '/path'.""" |
| 1000 | global _hostprog |
| 1001 | if _hostprog is None: |
postmasters | 90e01e5 | 2017-06-20 06:02:44 -0700 | [diff] [blame] | 1002 | _hostprog = re.compile('//([^/#?]*)(.*)', re.DOTALL) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1003 | |
| 1004 | match = _hostprog.match(url) |
Senthil Kumaran | c295862 | 2010-11-22 04:48:26 +0000 | [diff] [blame] | 1005 | if match: |
Serhiy Storchaka | 44eceb6 | 2015-03-03 20:21:35 +0200 | [diff] [blame] | 1006 | host_port, path = match.groups() |
| 1007 | if path and path[0] != '/': |
Senthil Kumaran | c295862 | 2010-11-22 04:48:26 +0000 | [diff] [blame] | 1008 | path = '/' + path |
| 1009 | return host_port, path |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1010 | return None, url |
| 1011 | |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1012 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1013 | def splituser(host): |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1014 | warnings.warn("urllib.parse.splituser() is deprecated as of 3.8, " |
| 1015 | "use urllib.parse.urlparse() instead", |
| 1016 | DeprecationWarning, stacklevel=2) |
| 1017 | return _splituser(host) |
| 1018 | |
| 1019 | |
| 1020 | def _splituser(host): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1021 | """splituser('user[:passwd]@host[:port]') --> 'user[:passwd]', 'host[:port]'.""" |
Serhiy Storchaka | 44eceb6 | 2015-03-03 20:21:35 +0200 | [diff] [blame] | 1022 | user, delim, host = host.rpartition('@') |
| 1023 | return (user if delim else None), host |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1024 | |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1025 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1026 | def splitpasswd(user): |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1027 | warnings.warn("urllib.parse.splitpasswd() is deprecated as of 3.8, " |
| 1028 | "use urllib.parse.urlparse() instead", |
| 1029 | DeprecationWarning, stacklevel=2) |
| 1030 | return _splitpasswd(user) |
| 1031 | |
| 1032 | |
| 1033 | def _splitpasswd(user): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1034 | """splitpasswd('user:passwd') -> 'user', 'passwd'.""" |
Serhiy Storchaka | 44eceb6 | 2015-03-03 20:21:35 +0200 | [diff] [blame] | 1035 | user, delim, passwd = user.partition(':') |
| 1036 | return user, (passwd if delim else None) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1037 | |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1038 | |
| 1039 | def splitport(host): |
| 1040 | warnings.warn("urllib.parse.splitport() is deprecated as of 3.8, " |
| 1041 | "use urllib.parse.urlparse() instead", |
| 1042 | DeprecationWarning, stacklevel=2) |
| 1043 | return _splitport(host) |
| 1044 | |
| 1045 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1046 | # splittag('/path#tag') --> '/path', 'tag' |
| 1047 | _portprog = None |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1048 | def _splitport(host): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1049 | """splitport('host:port') --> 'host', 'port'.""" |
| 1050 | global _portprog |
| 1051 | if _portprog is None: |
Serhiy Storchaka | 44eceb6 | 2015-03-03 20:21:35 +0200 | [diff] [blame] | 1052 | _portprog = re.compile('(.*):([0-9]*)$', re.DOTALL) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1053 | |
| 1054 | match = _portprog.match(host) |
Serhiy Storchaka | ff97b08 | 2014-01-18 18:30:33 +0200 | [diff] [blame] | 1055 | if match: |
| 1056 | host, port = match.groups() |
| 1057 | if port: |
| 1058 | return host, port |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1059 | return host, None |
| 1060 | |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1061 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1062 | def splitnport(host, defport=-1): |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1063 | warnings.warn("urllib.parse.splitnport() is deprecated as of 3.8, " |
| 1064 | "use urllib.parse.urlparse() instead", |
| 1065 | DeprecationWarning, stacklevel=2) |
| 1066 | return _splitnport(host, defport) |
| 1067 | |
| 1068 | |
| 1069 | def _splitnport(host, defport=-1): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1070 | """Split host and port, returning numeric port. |
| 1071 | Return given default port if no ':' found; defaults to -1. |
| 1072 | Return numerical port if a valid number are found after ':'. |
| 1073 | Return None if ':' but not a valid number.""" |
Serhiy Storchaka | 44eceb6 | 2015-03-03 20:21:35 +0200 | [diff] [blame] | 1074 | host, delim, port = host.rpartition(':') |
| 1075 | if not delim: |
| 1076 | host = port |
| 1077 | elif port: |
| 1078 | try: |
| 1079 | nport = int(port) |
| 1080 | except ValueError: |
| 1081 | nport = None |
| 1082 | return host, nport |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1083 | return host, defport |
| 1084 | |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1085 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1086 | def splitquery(url): |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1087 | warnings.warn("urllib.parse.splitquery() is deprecated as of 3.8, " |
| 1088 | "use urllib.parse.urlparse() instead", |
| 1089 | DeprecationWarning, stacklevel=2) |
| 1090 | return _splitquery(url) |
| 1091 | |
| 1092 | |
| 1093 | def _splitquery(url): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1094 | """splitquery('/path?query') --> '/path', 'query'.""" |
Serhiy Storchaka | 44eceb6 | 2015-03-03 20:21:35 +0200 | [diff] [blame] | 1095 | path, delim, query = url.rpartition('?') |
| 1096 | if delim: |
| 1097 | return path, query |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1098 | return url, None |
| 1099 | |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1100 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1101 | def splittag(url): |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1102 | warnings.warn("urllib.parse.splittag() is deprecated as of 3.8, " |
| 1103 | "use urllib.parse.urlparse() instead", |
| 1104 | DeprecationWarning, stacklevel=2) |
| 1105 | return _splittag(url) |
| 1106 | |
| 1107 | |
| 1108 | def _splittag(url): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1109 | """splittag('/path#tag') --> '/path', 'tag'.""" |
Serhiy Storchaka | 44eceb6 | 2015-03-03 20:21:35 +0200 | [diff] [blame] | 1110 | path, delim, tag = url.rpartition('#') |
| 1111 | if delim: |
| 1112 | return path, tag |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1113 | return url, None |
| 1114 | |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1115 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1116 | def splitattr(url): |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1117 | warnings.warn("urllib.parse.splitattr() is deprecated as of 3.8, " |
| 1118 | "use urllib.parse.urlparse() instead", |
| 1119 | DeprecationWarning, stacklevel=2) |
| 1120 | return _splitattr(url) |
| 1121 | |
| 1122 | |
| 1123 | def _splitattr(url): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1124 | """splitattr('/path;attr1=value1;attr2=value2;...') -> |
| 1125 | '/path', ['attr1=value1', 'attr2=value2', ...].""" |
| 1126 | words = url.split(';') |
| 1127 | return words[0], words[1:] |
| 1128 | |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1129 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1130 | def splitvalue(attr): |
Cheryl Sabella | 0250de4 | 2018-04-25 16:51:54 -0700 | [diff] [blame] | 1131 | warnings.warn("urllib.parse.splitvalue() is deprecated as of 3.8, " |
| 1132 | "use urllib.parse.parse_qsl() instead", |
| 1133 | DeprecationWarning, stacklevel=2) |
| 1134 | return _splitvalue(attr) |
| 1135 | |
| 1136 | |
| 1137 | def _splitvalue(attr): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1138 | """splitvalue('attr=value') --> 'attr', 'value'.""" |
Serhiy Storchaka | 44eceb6 | 2015-03-03 20:21:35 +0200 | [diff] [blame] | 1139 | attr, delim, value = attr.partition('=') |
| 1140 | return attr, (value if delim else None) |