3 ]R&@sddlmZddlZddlmZddlmZddlmZm Z ddl m Z m Z ddl mZdd lmZmZmZmZd d d d dddgZd#ZejdZejejBdddhBZedhBZZGdddedeZddZd$ddZdd Z d!d"Z!dS)%)absolute_importN) namedtuple)LocationParseError)sixrfc3986)RFC3986ExceptionValidationError) Validator) abnf_regexp normalizerscompatmiscschemeauthhostportpathqueryfragmenthttphttpsz^(?:[a-zA-Z][a-zA-Z0-9+\-]*:|/):@/?cs^eZdZdZfZdfdd ZeddZeddZed d Z ed d Z d dZ Z S)Urlz Data structure for representing an HTTP URL. Used as a return value for :func:`parse_url`. Both the scheme and host are normalized as they are both case-insensitive according to RFC 3986. Nc sF|r|jd rd|}|dk r(|j}tt|j||||||||S)Nr) startswithlowersuperr__new__)clsrrrrrrr) __class__3/tmp/pip-build-el9acr48/urllib3/urllib3/util/url.pyr !s z Url.__new__cCs|jS)z@For backwards-compatibility with urlparse. We're nice like that.)r)selfr#r#r$hostname*sz Url.hostnamecCs&|jpd}|jdk r"|d|j7}|S)z)Absolute path including the query string.rNr)rr)r%urir#r#r$ request_uri/s  zUrl.request_uricCs|jrd|j|jfS|jS)z(Network location including host and portz%s:%d)rr)r%r#r#r$netloc9sz Url.netlocc Cs|\}}}}}}}d}|dk r*||d7}|dk r>||d7}|dk rN||7}|dk rf|dt|7}|dk rv||7}|dk r|d|7}|dk r|d|7}|S)a Convert self into a url This function should more or less round-trip with :func:`.parse_url`. The returned url may not be exactly the same as the url inputted to :func:`.parse_url`, but it should be equivalent by the RFC (e.g., urls with a blank port will have : removed). Example: :: >>> U = parse_url('http://google.com/mail/') >>> U.url 'http://google.com/mail/' >>> Url('http', 'username:password', 'host.com', 80, ... '/path', 'query', 'fragment').url 'http://username:password@host.com:80/path?query#fragment' Nz://rrr#)str) r%rrrrrrrurlr#r#r$r-@s"    zUrl.urlcCs|jS)N)r-)r%r#r#r$__str__hsz Url.__str__)NNNNNNN) __name__ __module__ __qualname____doc__ __slots__r propertyr&r(r)r-r. __classcell__r#r#)r"r$rs   (rcCszd}d}x8|D]0}|j|}|dkr&q|dks6||kr|}|}qW|dksR|dkr\|ddfS|d|||dd|fS)a .. deprecated:: 1.25 Given a string and an iterable of delimiters, split on the first found delimiter. Return two split parts and the matched delimiter. If not found, then the first part is the full input string. Example:: >>> split_first('foo/bar?baz', '?/=') ('foo', 'bar?baz', '/') >>> split_first('foo/bar?baz', '123') ('foo/bar?baz', '', None) Scales linearly with number of delims. Not ideal for large number of delims. Nrr*)find)sdelimsmin_idx min_delimdidxr#r#r$ split_firstls   r>utf-8c Cs|dkr |Sttjjtj||}|jdd}||jdk}t}xrt dt|D]`}|||d}t |} |r||dks| dkr|j |kr|j |qT|j dj | jjqTW|j |S) zPercent-encodes a URI component without reapplying onto an already percent-encoded component. Based on rfc3986.normalizers.encode_component() Nzutf-8 surrogatepass%rr6z%{0:02x})lenr ZPERCENT_MATCHERfindallr to_strencodecount bytearrayrangeorddecodeextendformatupper) component allowed_charsencodingZpercent_encodingsZ uri_bytesZis_percent_encodedZencoded_componentiZbyteZbyte_ordr#r#r$_encode_invalid_charss    rSc s|s tSt|tj tj|s*d|}dd}yhtjjt j |j }t j |d|dt|dtt|dtt|dt}|jd k }|j|d }Wn$ttfk rtjt|d SX|r|jd krt||jd ks|jjtkr|j}t}y|j|jj|Wn"t k r4tjt|d SX|j!}|sd|j"d k sZ|j#d k r`d }nd }fd d }t||j||j$||j%|j&d k rt'|j&nd ||||j"||j#dS)a Given a url, return a parsed :class:`.Url` namedtuple. Best-effort is performed to parse incomplete urls. Fields not provided will be None. This parser is RFC 3986 compliant. :param str url: URL to parse into a :class:`.Url` namedtuple. Partly backwards-compatible with :mod:`urlparse`. Example:: >>> parse_url('http://google.com/mail/') Url(scheme='http', host='google.com', port=None, path='/mail/', ...) >>> parse_url('google.com:80') Url(scheme=None, host='google.com', port=80, path=None, ...) >>> parse_url('/foo?bar') Url(scheme=None, host=None, port=None, path='/foo', query='bar', ...) z//cSs||rxtdd|Drxy ddl}Wntk r>tdYnXy|j|jdddS|jk rvtd|YnX|S)NcSsg|]}t|dkqS)rB)rJ).0xr#r#r$ sz2parse_url..idna_encode..rz-Unable to parse URL without the 'idna' moduleT)strict std3_rulesz#Name '%s' is not a valid IDNA label)anyidna ImportErrorrrFr IDNAError)namerZr#r#r$ idna_encodes zparse_url..idna_encoder authorityrrrN)Z idna_encoderr*cs.|dkr dS r*t|tj r*|jdS|S)Nzutf-8) isinstancer binary_typerF)rU) is_stringr#r$ to_input_type s  z parse_url..to_input_type)rrrrrrr)(rr`rra SCHEME_REGEXsearchrZ IRI_MATCHERmatchr rE groupdictrZ IRIReferencerS PATH_CHARS QUERY_CHARSFRAGMENT_CHARSr_rF ValueErrorr raise_fromrrrNORMALIZABLE_SCHEMES normalizer Zcheck_validity_ofZCOMPONENT_NAMESvalidater rrruserinforrint) r-r^Z split_iriZiri_refZ has_authorityZuri_refZ validatorrrcr#)rbr$ parse_urlsV          rrcCst|}|jpd|j|jfS)z4 Deprecated. Use :func:`parse_url` instead. r)rrrr&r)r-pr#r#r$get_hostsrt)rrN)r?)" __future__rre collectionsr exceptionsrpackagesrrZpackages.rfc3986.exceptionsrr Zpackages.rfc3986.validatorsr Zpackages.rfc3986r r r r url_attrsrmcompilerdZUNRESERVED_CHARS_SETZSUB_DELIMITERS_SETrhrirjrr>rSrrrtr#r#r#r$s"     S# n