B -_ @sNdZddlZddlZddlZddddddd d d d d ddddddddddgZddddddddd d!d"d#d$d%d&d'd(d)d*gZdddddd+dddd"d d!d,d#d$d%d-d'd(d&d.d/d0d)d*gZddd1d#ddd d!d$d%d2d3d"d&d4gZdd1d5d6d+ddd,d2d3g Zddddd d!d"dd$d%d2d3g Z ddd1ddd6ddd d!d,dd#g Z d7Z d8Z iZ d9d:Zd;ZdZeefd?d@ZeefdAdBZdCdDZGdEdFdFeZGdGdHdHeZGdIdJdJeZGdKdLdLeeZGdMdNdNeeZddOlmZeddPZeddQZeddRZdSe_dTej_dUej _dVe_dWej!_dXej"_dYej#_dZej$_d[ej _d\e_ej!jej!_ej"jej"_ej#jej#_d]ej%_ej$jej$_ej jej _eZ&Gd^ddeeZ'Gd_ddeeZ(Gd`ddeeZ)GdaddeeZ*GdbddeeZ+GdcddeeZ,dddeZ-e-[-ddgdZ.dhdiZ/ddjdkZ0dldmZ1ddndZ2dodZ3dpdZ4ddqdZ5drdZ6dsZ7da8dtdZ9e:duZ;ddxdZdd|dZ?e@d}ZAeBeAZCiZDGd~ddejEZFddd ZGddd ZHdddZIdydddeHfdd ZJddZKddZLdaMddZNdaOddZPddZQddZRdaSddZTdddZUddZVddZWddZXddZYdS)acParse (absolute and relative) URLs. urlparse module is based upon the following RFC specifications. RFC 3986 (STD66): "Uniform Resource Identifiers" by T. Berners-Lee, R. Fielding and L. Masinter, January 2005. RFC 2732 : "Format for Literal IPv6 Addresses in URL's by R.Hinden, B.Carpenter and L.Masinter, December 1999. RFC 2396: "Uniform Resource Identifiers (URI)": Generic Syntax by T. Berners-Lee, R. Fielding, and L. Masinter, August 1998. RFC 2368: "The mailto URL scheme", by P.Hoffman , L Masinter, J. Zawinski, July 1998. RFC 1808: "Relative Uniform Resource Locators", by R. Fielding, UC Irvine, June 1995. RFC 1738: "Uniform Resource Locators (URL)" by T. Berners-Lee, L. Masinter, M. McCahill, December 1994 RFC 3986 is considered the current standard and any future changes to urlparse module should conform with it. The urlparse module is currently not entirely compliant with this RFC due to defacto scenarios for parsing, and for backward compatibility purposes, some parsing quirks from older RFCs are retained. The testcases in test_urlparse.py provides a good indicator of parsing behavior. Nurlparse urlunparseurljoin urldefragurlsplit urlunsplit urlencodeparse_qs parse_qslquote quote_plusquote_from_bytesunquote unquote_plusunquote_to_bytes DefragResult ParseResult SplitResultDefragResultBytesParseResultBytesSplitResultBytesZftphttpZgopherZnntpZimapZwaisfileZhttpsZshttpZmmsZprosperoZrtspZrtspuZsftpZsvnzsvn+sshZwsZwssZtelnetZsnewsZrsyncZnfsZgitzgit+sshZhdlZsipZsipsZtelZmailtoZnewszAabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-.cCsttdS)z,Clear the parse cache and the quoters cache.N) _parse_cacheclear _safe_quotersrr"/usr/lib/python3.7/urllib/parse.py clear_cacheSsr asciistrictcCs|S)Nr)objrrr_noopbsr$cCs |||S)N)encode)r#encodingerrorsrrr_encode_resultesr(cstfdd|DS)Nc3s"|]}|r|ndVqdS)rN)decode).0x)r&r'rr ksz_decode_args..)tuple)argsr&r'r)r&r'r _decode_argsisr/cGsZt|dt}x.|ddD]}|rt|t|krtdqW|rL|tfSt|tfS)Nrz$Cannot mix str and non-str arguments) isinstancestr TypeErrorr$r/r()r.Z str_inputargrrr _coerce_argsms  r5c@seZdZdZdZdddZdS) _ResultMixinStrz>Standard approach to encoding parsed results from str to bytesrr!r"cs|jfdd|DS)Nc3s|]}|VqdS)N)r%)r*r+)r&r'rrr,sz)_ResultMixinStr.encode..)_encoded_counterpart)selfr&r'r)r&r'rr%sz_ResultMixinStr.encodeN)r!r")__name__ __module__ __qualname____doc__ __slots__r%rrrrr6~sr6c@seZdZdZdZdddZdS) _ResultMixinBytesz>Standard approach to decoding parsed results from bytes to strrr!r"cs|jfdd|DS)Nc3s|]}|VqdS)N)r))r*r+)r&r'rrr,sz+_ResultMixinBytes.decode..)_decoded_counterpart)r8r&r'r)r&r'rr)sz_ResultMixinBytes.decodeN)r!r")r9r:r;r<r=r)rrrrr>sr>c@sDeZdZdZdZeddZeddZeddZed d Z d S) _NetlocResultMixinBasezHShared methods for the parsed result objects containing a netloc elementrcCs |jdS)Nr) _userinfo)r8rrrusernamesz_NetlocResultMixinBase.usernamecCs |jdS)Nr0)rA)r8rrrpasswordsz_NetlocResultMixinBase.passwordcCsD|jd}|sdSt|tr dnd}||\}}}|||S)Nr%%) _hostinfor1r2 partitionlower)r8hostnameZ separatorZpercentZzonerrrrIs  z_NetlocResultMixinBase.hostnamecCs>|jd}|dk r:t|d}d|kr0dks:ntd|S)Nr0 rizPort out of range 0-65535)rFint ValueError)r8portrrrrMs   z_NetlocResultMixinBase.portN) r9r:r;r<r=propertyrBrCrIrMrrrrr@s    r@c@s(eZdZdZeddZeddZdS)_NetlocResultMixinStrrcCsD|j}|d\}}}|r4|d\}}}|ssc@seZdZdZddZdS)rrcCst|S)N)r)r8rrrrfEszParseResult.geturlN)r9r:r;r=rfrrrrrCsc@seZdZdZddZdS)rrcCs |jr|jd|jS|jSdS)N#)rdre)r8rrrrfKszDefragResultBytes.geturlN)r9r:r;r=rfrrrrrIsc@seZdZdZddZdS)rrcCst|S)N)r)r8rrrrfSszSplitResultBytes.geturlN)r9r:r;r=rfrrrrrQsc@seZdZdZddZdS)rrcCst|S)N)r)r8rrrrfXszParseResultBytes.geturlN)r9r:r;r=rfrrrrrVscCs8ttfttfttff}x|D]\}}||_||_qWdS)N)rrrrrrr7r?)Z _result_pairsZ_decodedZ_encodedrrr_fix_result_transcoding\s  rhTc Csft||\}}}t|||}|\}}}}}|tkrHd|krHt|\}}nd}t||||||} || S)a#Parse a URL into 6 components: :///;?# Return a 6-tuple: (scheme, netloc, path, params, query, fragment). Note that we don't break the components up in smaller bits (e.g. netloc is a single string) and we don't expand % escapes.;r)r5r uses_params _splitparamsr) reschemeallow_fragments_coerce_resultZ splitresultrRqueryrdparamsresultrrrris cCsRd|kr,|d|d}|dkr6|dfSn |d}|d|||ddfS)N/rirrr0)findrfind)reirrrrkys   rkcCsLt|}x*dD]"}|||}|dkrt||}qW|||||dfS)Nz/?#r)lenrsmin)restartdelimcZwdelimrrr _splitnetlocs   r{cCs|r |rdSddl}|dd}|dd}|dd}|dd}|d|}||kr`dSx(dD] }||krftd |d d qfWdS) NrrPrrQrc?ZNFKCz/?#@:znetloc 'z' contains invalid z#characters under NFKC normalization)isascii unicodedatareplaceZ normalizerL)rRr~nZnetloc2rzrrr _checknetlocs        rc CsTt||\}}}t|}|||t|t|f}t|d}|rF||StttkrXtd}}}|d} | dkr|d| dkr>|| dd}|dddkrt |d\}}d |krd |ksd |krd |krt d |rd |kr| d d\}}d |kr| d d\}}t |t d||||} | t|<|| Sxb|d| D]} | tkrLPqLW|| dd} | rtdd| Dr|d| | }}|dddkrt |d\}}d |krd |ksd |krd |krt d |rd |kr| d d\}}d |kr,| d d\}}t |t |||||} | t|<|| S)aParse a URL into 5 components: :///?# Return a 5-tuple: (scheme, netloc, path, query, fragment). Note that we don't break the components up in smaller bits (e.g. netloc is a single string) and we don't expand % escapes.NrrQrrr0z//rXrYzInvalid IPv6 URLrcr|css|]}|dkVqdS) 0123456789Nr)r*rzrrrr,szurlsplit..)r5booltypergetrvMAX_CACHE_SIZEr rsr{rLsplitrr scheme_charsanyrH) rerlrmrnkeycachedrRrordruvrzrestrrrrsZ         cCs<t|\}}}}}}}|r&d||f}|t|||||fS)zPut a parsed URL back together again. This may result in a slightly different, but equivalent URL, if the URL that was parsed originally had redundant delimiters, e.g. a ? with an empty query (the draft states that these are equivalent).z%s;%s)r5r) componentsrlrRrerprordrnrrrrs cCst|\}}}}}}|s4|r`|tkr`|dddkr`|rP|dddkrPd|}d|pXd|}|rp|d|}|r|d|}|r|d |}||S) akCombine the elements of a tuple as returned by urlsplit() into a complete URL as a string. The data argument can be any five-item iterable. This may result in a slightly different, but equivalent URL, if the URL that was parsed originally had unnecessary delimiters (for example, a ? with an empty query; the RFC states that these are equivalent).Nrz//r0rrrrQr|rc)r5 uses_netloc)rrlrRrerordrnrrrrs    c Cs|s|S|s|St||\}}}t|d|\}}}}}} t|||\} } } } }}| |ks`| tkrh||S| tkr| r|t| | | | ||fS|} | s| s|} |} |s|}|t| | | | ||fS|d}|ddkr|d=| dddkr| d}n(|| d}td|dd|dd<g}xX|D]P}|dkr^y |Wntk rZYnXn|dkrnq*n | |q*W|ddkr| d|t| | d |pd| ||fS) zaJoin a base URL and a possibly relative URL to form an absolute interpretation of the latter.rrrNr0z...)rz..) r5r uses_relativerrrfilterpop IndexErrorappendjoin)baserermrnZbschemeZbnetlocZbpathZbparamsZbqueryZ bfragmentrlrRpathrprordZ base_partsZsegmentsZ resolved_pathZsegrrrrsT           c CsTt|\}}d|kr>t|\}}}}}}t|||||df}nd}|}|t||S)zRemoves any existing fragment from URL. Returns a tuple of the defragmented URL and the fragment. If the URL contained no fragments, the second element is the empty string. rcr)r5rrr) rernsrpaqZfragZdefragrrrr8s Z0123456789ABCDEFabcdefc Cs|s|jdSt|tr"|d}|d}t|dkr<|S|dg}|j}tdkrbddtDaxb|ddD]R}y(|t|dd ||d dWqptk r|d||YqpXqpWd |S) z,unquote_to_bytes('abc%20def') -> b'abc def'.zutf-8rEr0rNcSs.i|]&}tD]}t||||q qSr)_hexdigbytesfromhexr%)r*rbrrr ^sz$unquote_to_bytes..r) rr1r2r%rvr _hextobyterKeyErrorr)stringbitsresritemrrrrKs*     z([-]+)utf-8rcCsd|kr|j|S|dkrd}|dkr*d}t|}|dg}|j}x@tdt|dD],}|t|||||||dqVWd|S) aReplace %xx escapes by their single-character equivalent. The optional encoding and errors parameters specify how to decode percent-encoded sequences into Unicode characters, as accepted by the bytes.decode() method. By default, percent-encoded sequences are decoded with UTF-8, and invalid sequences are replaced by a placeholder character. unquote('abc%20def') -> 'abc def'. rDNzutf-8rrr0rr)r_asciirerrangervrr)r)rr&r'rrrrurrrrks   Fc CsPi}t||||||d}x2|D]*\}} ||kr>||| q| g||<qW|S)aParse a query given as a string argument. Arguments: qs: percent-encoded query string to be parsed keep_blank_values: flag indicating whether blank values in percent-encoded queries should be treated as blank strings. A true value indicates that blanks should be retained as blank strings. The default false value indicates that blank values are to be ignored and treated as if they were not included. strict_parsing: flag indicating what to do with parsing errors. If false (the default), errors are silently ignored. If true, errors raise a ValueError exception. encoding and errors: specify how to decode percent-encoded sequences into Unicode characters, as accepted by the bytes.decode() method. max_num_fields: int. If set, then throws a ValueError if there are more than n fields read by parse_qsl(). Returns a dictionary. )r&r'max_num_fields)r r) qskeep_blank_valuesstrict_parsingr&r'rZ parsed_resultpairsnamevaluerrrr scCst|\}}|dk rszparse_qsl..=rzbad query field: %rrr+ )r&r')r5countrLrrvrrr)rrrr&r'rrn num_fieldsrrZ name_valueZnvrrrrrr s4     cCs|dd}t|||S)zLike unquote(), but also replace plus signs by spaces, as required for unquoting HTML form values. unquote_plus('%7e/abc+def') -> '~/abc def' rr)rr)rr&r'rrrrs sBABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-~c@s(eZdZdZddZddZddZdS) QuoterzA mapping from bytes (in range(0,256)) to strings. String values are percent-encoded byte values, unless the key < 128, and in the "safe" set (either the specified safe set, or default set). cCst||_dS)zsafe: bytes object.N) _ALWAYS_SAFEunionsafe)r8rrrr__init__szQuoter.__init__cCsd|jjt|fS)Nz<%s %r>) __class__r9dict)r8rrr__repr__szQuoter.__repr__cCs(||jkrt|nd|}|||<|S)Nz%{:02X})rchrformat)r8rrrrr __missing__szQuoter.__missing__N)r9r:r;r<rrrrrrrrsrrrcCsbt|tr8|s|S|dkrd}|dkr*d}|||}n |dk rHtd|dk rXtdt||S)a<quote('abc def') -> 'abc%20def' Each part of a URL, e.g. the path info, the query, etc., has a different set of reserved characters that must be quoted. RFC 3986 Uniform Resource Identifiers (URI): Generic Syntax lists the following reserved characters. reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," | "~" Each of these characters is reserved in some component of a URL, but not necessarily in all of them. Python 3.7 updates from using RFC 2396 to RFC 3986 to quote URL strings. Now, "~" is included in the set of reserved characters. By default, the quote function is intended for quoting the path section of a URL. Thus, it will not encode '/'. This character is reserved, but in typical usage the quote function is being called on a path where the existing slash characters are used as reserved characters. string and safe may be either str or bytes objects. encoding and errors must not be specified if string is a bytes object. The optional encoding and errors parameters specify how to deal with non-ASCII characters, as accepted by the str.encode method. By default, encoding='utf-8' (characters are encoded with UTF-8), and errors='strict' (unsupported characters raise a UnicodeEncodeError). Nzutf-8r"z,quote() doesn't support 'encoding' for bytesz*quote() doesn't support 'errors' for bytes)r1r2r%r3r )rrr&r'rrrr s cCsdt|trd|ks$t|tr2d|kr2t||||St|trBd}nd}t|||||}|ddS)zLike quote(), but also replace ' ' with '+', as required for quoting HTML form values. Plus signs in the original string are escaped unless they are included in safe. It also does not have safe default to '/'. r r)r1r2rr r)rrr&r'Zspacerrrr =s cst|ttfstd|sdSt|tr6|dd}ntdd|D}|t|s^|Sy t |Wn&t k rt |j t |<YnXd fdd|DS)zLike quote(), but accepts a bytes object rather than a str, and does not perform string-to-bytes encoding. It always returns an ASCII string. quote_from_bytes(b'abc def?') -> 'abc%20def%3f' z!quote_from_bytes() expected bytesrr!ignorecSsg|]}|dkr|qS)r)r*rzrrrr[sz$quote_from_bytes..csg|] }|qSrr)r*char)quoterrrrbs)r1r bytearrayr3r2r%rstrip_ALWAYS_SAFE_BYTESr)rrr __getitem__r)Zbsrr)rrr Ns  c Cst|dr|}nPyt|r0t|dts0tWn0tk rbt\}}}td|YnXg} |sxr|D]j\} } t| t r|| |} n|t | |||} t| t r|| |} n|t | |||} | | d| qrWn,x(|D]\} } t| t r|| |} n|t | |||} t| t rJ|| |} | | d| qt| t rx|| |||} | | d| qy t| } Wn:tk r|t | |||} | | d| YqXxJ| D]B} t| t r|| |} n|t | |||} | | d| qWqWd | S)a^Encode a dict or sequence of two-element tuples into a URL query string. If any values in the query arg are sequences and doseq is true, each sequence element is converted to a separate parameter. If the query arg is a sequence of two-element tuples, the order of the parameters in the output will match the order of parameters in the input. The components of a query arg may each be either a string or a bytes type. The safe, encoding, and errors parameters are passed down to the function specified by quote_via (encoding and errors only if a component is a str). itemsrz1not a valid non-string sequence or mapping objectrr) hasattrrrvr1r-r3sysexc_infowith_tracebackrr2rr)roZdoseqrr&r'Z quote_viaZtyZvatblkrr+ZeltrrrrdsP                cCsJt|trFy|d}Wn(tk rDtdt|dYnX|S)zto_bytes(u"URL") --> 'URL'.ASCIIzURL z contains non-ASCII characters)r1r2r%r) UnicodeErrorrepr)rerrrto_bytess  rcCs`t|}|dddkr<|dddkr<|dd}|dddkr\|dd}|S)z8unwrap('') --> 'type://host/path'.Nr0zURL:)r2strip)rerrrunwraps   rcCsDtdkrtdtjat|}|r<|\}}||fSd|fS)z:splittype('type:opaquestring') --> 'type', 'opaquestring'.Nz ([^/:]+):(.*)) _typeprogrecompileDOTALLmatchgroupsrH)rerrldatarrr splittypes   rcCsXtdkrtdtjat|}|rP|\}}|rH|ddkrHd|}||fSd|fS)z;splithost('//host[:port]/path') --> 'host[:port]', '/path'.Nz//([^/#?]*)(.*)rrr) _hostprogrrrrr)rerZ host_portrrrr splithosts  rcCs |d\}}}|r|nd|fS)zJsplituser('user[:passwd]@host[:port]') --> 'user[:passwd]', 'host[:port]'.rPN)rS)hostuserryrrr splitusersrcCs |d\}}}||r|ndfS)z/splitpasswd('user:passwd') -> 'user', 'passwd'.rQN)rG)rryZpasswdrrr splitpasswdsrcCsDtdkrtdtjat|}|r<|\}}|r<||fS|dfS)z*splitport('host:port') --> 'host', 'port'.Nz(.*):([0-9]*)$) _portprogrrrrr)rrrMrrr splitports  rrcCsT|d\}}}|s|}n2|rLy t|}Wntk rBd}YnX||fS||fS)zSplit host and port, returning numeric port. Return given default port if no ':' found; defaults to -1. Return numerical port if a valid number are found after ':'. Return None if ':' but not a valid number.rQN)rSrKrL)rZdefportryrMZnportrrr splitnports  rcCs$|d\}}}|r||fS|dfS)z/splitquery('/path?query') --> '/path', 'query'.r|N)rS)rerryrorrr splitquerysrcCs$|d\}}}|r||fS|dfS)z)splittag('/path#tag') --> '/path', 'tag'.rcN)rS)rerrytagrrrsplittagsrcCs|d}|d|ddfS)zksplitattr('/path;attr1=value1;attr2=value2;...') -> '/path', ['attr1=value1', 'attr2=value2', ...].rirr0N)r)reZwordsrrr splitattrs rcCs |d\}}}||r|ndfS)z-splitvalue('attr=value') --> 'attr', 'value'.rN)rG)attrryrrrr splitvalue"sr)rT)r)rT)T)rr)FFrrN)FFrrN)rr)rrNN)rNN)rr)r)Zr<rr collections__all__rrrjZnon_hierarchicalZ uses_queryZ uses_fragmentrrrr Z_implicit_encodingZ_implicit_errorsr$r(r/r5objectr6r>r@rOr]rbZ_DefragResultBaseZ_SplitResultBaseZ_ParseResultBasererdrlrRrrorpZ ResultBaserrrrrrrhrrkr{rrrrrrrrrrrrr r r frozensetrrrr defaultdictrr r r rrrrrrrrrrrrrrrrrrrrs                !           9  E   & ;  /   O