$+JhZdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z ddlmZmZddlmZddlmZddlmZmZmZmZdd lmZmZmZmZmZmZdd lm Z!dd lm"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(dd l)m*Z*dd l+m,Z,m-Z-m.Z.m/Z/ddl0m1Z1dZ2ejfZ4dddZ5djmejndeddZ8ejrdk(rdZ:dZ"dZ;dZdZ?ejd ZAd!ZBd"ZCd#ZDd$ZEdHd%ZFd&ZGd'ZHd(ZId)ZJd*ZKd+ZLd,ZMd-ZNeOd.ZPd/ZQd0ZRd1ZSd2ZTd3ZUd4ZVejd5ZWd6ZXdId7ZYd8ZZdJd9Z[dKd:Z\d;Z]d<Z^d=jd>Z`e`d?zZae`d@zZbdAZcdBZddCZedDZfdEZgdFZhdGZiy)Lz requests.utils ~~~~~~~~~~~~~~ This module provides utility functions that are used within Requests that are also useful for external consumption. N) OrderedDict) make_headers parse_url)certs __version__)_HEADER_VALIDATORS_BYTE_HEADER_VALIDATORS_STRHEADER_VALIDATORSto_native_string)Mapping basestringbytes getproxiesgetproxies_environment integer_types)parse_http_list) proxy_bypassproxy_bypass_environmentquotestrunquoteurlparse urlunparsecookiejar_from_dict)FileModeWarning InvalidHeader InvalidURLUnrewindableBodyError)CaseInsensitiveDict)z.netrc_netrcPi)httphttpsz, z,\s*T)accept_encodingzaccept-encodingwin32c ddl} |j|jd}t |j |dd}|j |dd}|r|sy|jd}|D]j}|dk(rd|vry |jdd }|jd d }|jd d}tj||tjsjy y#t$rYywxYw#t tf$rYywxYw)NrFz;Software\Microsoft\Windows\CurrentVersion\Internet Settings ProxyEnable ProxyOverride;z.Tz\.*z.*?) winreg ImportErrorOpenKeyHKEY_CURRENT_USERint QueryValueExOSError ValueErrorsplitreplacerematchI)hostr0internetSettings proxyEnable proxyOverridetests 0/usr/lib/python3/dist-packages/requests/utils.pyproxy_bypass_registryrCLs   %~~((N   f112BMRSTUVK"//0@/RSTUM- &++C0 ! Dy d?<<U+D<<U+D<<T*DxxdBDD) ?  $  s#CAC+ C('C(+C=<C=cBtr t|St|S)zReturn True, if the host should be bypassed. Checks proxy settings gathered from the environment, if specified, or the registry. )rrrC)r=s rBrrps " #+D1 1(. .c>t|dr|j}|S)z/Returns an internal sequence dictionary update.items)hasattrrG)ds rBdict_to_sequencerJ|sq' GGI HrEcd}d}t|dr t|}n~t|dr |j}net|drY |j}tj|j }d|j vrtjdt t|drW |j}t|dr:|8 |jdd |j}|j|xsd|d}t!d||z S#tjtf$rYwxYw#t$rd}Y?wxYw#t$r||}YRwxYw) Nr__len__lenfilenoba%Requests has determined the content-length for this request using the binary size of the file: however, the file has been opened in text mode (i.e. without the 'b' flag in the mode). This may lead to an incorrect content-length. In Requests 3.0, support will be removed for files in text mode.tellseek)rHrMrNosfstatst_sizemodewarningswarnrioUnsupportedOperationAttributeErrorrPrQr6max)o total_lengthcurrent_positionrNs rB super_lenr`s]Lq)1v E uu H  XXZF88F+33L!&&  2$ q& % vvx q&!l&: %FF1aL#$668LFF+0q1 q,!11 22e''8    X%#$L%% 0 '#/  0s6D(E7D0D-,D-0 D>=D> EEctjjd}||f}n dtD} ddlm}m}d}|D]E} tj j|}tj j|sC|}n|yt|} | j} ||j| } | r| drdnd} | | | dfSy#t$rYywxYw#|tf$r|rYywxYw#ttf$rYywxYw)z;Returns the Requests tuple auth for a given url from netrc.NETRCNc3&K|] }d| yw)z~/N).0fs rB z!get_netrc_auth..s9Rs89sr)NetrcParseErrornetrcrrR)rSenvironget NETRC_FILESrirhpath expanduserKeyErrorexistsrhostnameauthenticatorsr6r1r[) url raise_errors netrc_filenetrc_locationsrhri netrc_pathrflocrir=r#login_is rBget_netrc_authr{s+(J%-9[9' 0  A gg((+ww~~c"      c]{{ :&55d;F%ay!aw33'   . )    (   s_C4C#C4C4 C4#+C  CC4CC4 C1.C40C11C44DDct|dd}|rBt|tr1|ddk7r(|ddk7rtjj |Syyyy)z0Tries to guess the filename of the given object.nameNr<>)getattr isinstancerrSrmbasename)objr}s rBguess_filenamersT 3 %D 4,aCDHPSOww%%ET,trEcRtjj|r|Stjj|\}}|rytjj|sZtjj|\}}|sn5dj ||g}|r tjj|sZt j |s|St j|}||jvr|Stj}tjj ||jdd}tjj|s6t|5}|j|j|ddd|S|S#1swY|SxYw)zReplace nonexistent paths that look like they refer to a member of a zip archive with the location of an extracted copy of the target, or else just return the provided path unchanged. /rN)rSrmrpr8joinzipfile is_zipfileZipFilenamelisttempfile gettempdir atomic_openwriteread)rmarchivememberprefixzip_filetmpextracted_path file_handlers rBextract_zipped_pathsrsE  ww~~d ggmmD)OGV "''..1''--0 66*+ "''..1   g & w'H X&&((     CWW\\#v||C'8'<=N 77>>. )  ( 6L   x}}V4 5 6 > 6 s /!FF&c#JKtjtjj |\}} tj |d5}|dddtj ||y#1swY xYw#t$rtj|wxYww)z-Write a file to the disk in an atomic fashion)dirwbN) rmkstemprSrmdirnamefdopenr9 BaseExceptionremove)filenametmp_descriptortmp_name tmp_handlers rBrr&s (//BGGOOH4MNNH YY~t ,     8X&    ( s47B#BA4B3B#4A=9B B  B#ct|yt|ttttfr t dt |S)aTake an object and test to see if it can be represented as a dictionary. Unless it can not be represented as such, return an OrderedDict, e.g., :: >>> from_key_val_list([('key', 'val')]) OrderedDict([('key', 'val')]) >>> from_key_val_list('string') Traceback (most recent call last): ... ValueError: cannot encode objects that are not 2-tuples >>> from_key_val_list({'key': 'val'}) OrderedDict([('key', 'val')]) :rtype: OrderedDict N+cannot encode objects that are not 2-tuples)rrrboolr4r7rvalues rBfrom_key_val_listr3s6$ }%#udC01FGG u rEc|yt|ttttfr t dt|t r|j}t|S)aTake an object and test to see if it can be represented as a dictionary. If it can be, return a list of tuples, e.g., :: >>> to_key_val_list([('key', 'val')]) [('key', 'val')] >>> to_key_val_list({'key': 'val'}) [('key', 'val')] >>> to_key_val_list('string') Traceback (most recent call last): ... ValueError: cannot encode objects that are not 2-tuples :rtype: list Nr) rrrrr4r7rrGlistrs rBto_key_val_listrNsJ" }%#udC01FGG%!  ;rEcg}t|D]5}|dd|ddcxk(rdk(rnnt|dd}|j|7|S)aParse lists as described by RFC 2068 Section 2. In particular, parse comma-separated lists where the elements of the list may include quoted-strings. A quoted-string could contain a comma. A non-quoted string could have quotes in the middle. Quotes are removed automatically after parsing. It basically works like :func:`parse_set_header` just that items may appear multiple times and case sensitivity is preserved. The return value is a standard :class:`list`: >>> parse_list_header('token, "quoted value"') ['token', 'quoted value'] To create a header from the :class:`list` again, use the :func:`dump_header` function. :param value: a string with a list header. :return: :class:`list` :rtype: list Nrr")_parse_list_headerunquote_header_valueappend)rresultitems rBparse_list_headerrlsY.F"5) 8tBCy 'C ''Qr 3D d MrEci}t|D]H}d|vrd||< |jdd\}}|dd|ddcxk(rdk(rnnt|dd}|||<J|S)a^Parse lists of key, value pairs as described by RFC 2068 Section 2 and convert them into a python dict: >>> d = parse_dict_header('foo="is a fish", bar="as well"') >>> type(d) is dict True >>> sorted(d.items()) [('bar', 'as well'), ('foo', 'is a fish')] If there is no value for a key it will be `None`: >>> parse_dict_header('key_without_value') {'key_without_value': None} To create a header from the :class:`dict` again, use the :func:`dump_header` function. :param value: a string with a dict header. :return: :class:`dict` :rtype: dict =Nrrr)rr8r)rrrr}s rBparse_dict_headerrs},F"5) d?F4L jja( e !9bc )c )(q5Et  MrEc|rF|d|dcxk(rdk(r5n|S|dd}|r|dddk7r"|jddjd dS|S) zUnquotes a header value. (Reversal of :func:`quote_header_value`). This does not use the real unquoting but what browsers are actually using for quoting. :param value: the header value to unquote. :rtype: str rrrrNrRz\\\z\")r9)r is_filenames rBrrsd qU2Y-#- La eBQi61==.66ucB B LrEcJi}|D]}|j||j<|S)zReturns a key/value dictionary from a CookieJar. :param cj: CookieJar object to extract cookies from. :rtype: dict )rr})cj cookie_dictcookies rBdict_from_cookiejarrs2K0#)<< FKK 0 rEct||S)zReturns a CookieJar from a key/value dictionary. :param cj: CookieJar to insert cookies into. :param cookie_dict: Dict of key/values to insert into CookieJar. :rtype: CookieJar r)rrs rBadd_dict_to_cookiejarrs {B //rEc^tjdttjdtj }tjdtj }tjd}|j ||j |z|j |zS)zlReturns encodings from given content string. :param content: bytestring to extract encodings from. zIn requests 3.0, get_encodings_from_content will be removed. For more information, please see the discussion on issue #2266. (This warning should only appear once.)z!])flagsz+]z$^<\?xml.*?encoding=["\']*(.+?)["\'>])rWrXDeprecationWarningr:compiler<findall)content charset_re pragma_rexml_res rBget_encodings_from_contentrs  MM 1  @MJ IQSQUQUVI ZZ? @F 7#   G $ % .. ! "rEcH|jd}|dj|dd}}i}d}|D]m}|j}|s|d}}|jd} | dk7r+|d| j|}|| dzdj|}|||j<o||fS) zReturns content type and parameters from given header :param header: string :return: tuple containing content type and dictionary of parameters r,rrNz"' Trr)r8stripfindlower) headertokens content_typeparams params_dictitems_to_stripparamkeyrindex_of_equalss rB_parse_content_type_headerrs\\# F!!9??,fQRj&LKN-  C#jjoO"$,_-33NCo134::>J',K $-  $$rEc|jd}|syt|\}}d|vr|djdSd|vryd|vryy) z}Returns encodings from given HTTP Header Dict. :param headers: dictionary to extract encoding from. :rtype: str z content-typeNcharsetz'"textz ISO-8859-1zapplication/jsonutf-8)rkrr)headersrrs rBget_encoding_from_headersrs`;;~.L 5lCL&Fi &&u-- \)*rEc#K|j |Ed{ytj|jd}|D]}|j|}|s||jdd}|r|yy7ew)zStream decodes an iterator.Nr9errorsrET)final)encodingcodecsgetincrementaldecoderdecode)iteratorrdecoderchunkrvs rBstream_decode_response_unicoder-s zz6f**1::6iHG ^^E " H 4 (B   sA<A:AA<"A<c#Kd}||dkr t|}|t|kr||||z||z }|t|kryyw)z Iterate over slices of a string.rN)rM)string slice_lengthposs rB iter_slicesr>sW C|q06{ F S3-.. | F s AAAc>tjdtg}t|j}|r t |j |S t |j |dS#t$r|j|Y5wxYw#t$r|j cYSwxYw)zReturns the requested content back in unicode. :param r: Response object to get unicode content from. Tried: 1. charset from content-type 2. fall back and replace all unicode characters :rtype: str zIn requests 3.0, get_unicode_from_response will be removed. For more information, please see the discussion on issue #2266. (This warning should only appear once.)r9r) rWrXrrrrr UnicodeErrorr TypeError)rtried_encodingsrs rBget_unicode_from_responserHs MM 1  O)3H -qyy(+ + 199hy99  -  " "8 , - yys#A$ B$BBBBzBABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~c|jd}tdt|D]l}||dd}t|dk(rI|jr9 t t |d}|tvr|||ddz||<Vd||||<bd||||<nd j|S#t $rtd|dwxYw) zUn-escape any percent-escape sequences in a URI that are unreserved characters. This leaves all reserved, illegal and non-ASCII bytes encoded. :rtype: str %rrrRz"Invalid percent-escape sequence: ''N) r8rangerMisalnumchrr4r7r UNRESERVED_SETr)uripartsihcs rBunquote_unreservedrus IIcNE 1c%j ! & !HQqM q6Q;199; LAr ON"uQx|+auQxj>a58*~E!H & 775> L #EaS!JKK Ls B''Ccpd}d} tt||S#t$rt||cYSwxYw)zRe-quote the given URI. This function passes the given URI through an unquote/quote cycle to ensure that it is fully and consistently quoted. :rtype: str z!#$%&'()*+,/:;=?@[]~z!#$&'()*+,/:;=?@[]~)safe)rrr )rsafe_with_percentsafe_without_percents rB requote_urir sJ/0 5',3DEE 5S344 5s 55c rtjdtj|d}|j d\}}tjdtjt t |d}tjdtj|d|z}||z||zk(S)zThis function allows you to check if an IP belongs to a network subnet Example: returns True if ip = 192.168.1.1 and net = 192.168.1.0/24 returns False if ip = 192.168.1.1 and net = 192.168.100.0/24 :rtype: bool z=Lrr)structunpacksocket inet_atonr8dotted_netmaskr4)ipnetipaddrnetaddrbitsnetmasknetworks rBaddress_in_networkrs]]4!1!1"!5 6q 9FIIcNMGTmmD&"2"2>#d)3L"MNqQGmmD&"2"27";I)r inet_ntoarpack)maskrs rBrrs7 b4i1, ,D   FKKd3 44rEcN tj|y#t$rYywxYw)z :rtype: bool FT)rrr6) string_ips rBis_ipv4_addressr#s.#  s  $$c |jddk(rR t|jdd}|dks|dkDry t j |jddyy#t$rYywxYw#t $rYywxYw)zV Very simple check of the cidr format in no_proxy variable. :rtype: bool rrFrrT)countr4r8r7rrr6)string_networkr s rB is_valid_cidrr's C A% ~++C034D !8tby    ^11#6q9 :     s"A('A7( A43A47 BBc#8K|du}|r2tjj|}|tj|< d|r(tj|=y|tj|<yy#|r(tj|=w|tj|<wwxYww)zSet the environment variable 'env_name' to 'value' Save previous value, yield, and then restore the previous value stored in the environment variable 'env_name'. If 'value' is None, do nothingN)rSrjrk)env_namer value_changed old_values rB set_environr,s%MJJNN8, $ 81  JJx('0 8$ = JJx('0 8$ s9BA++B+,BBcd}|}||d}t|}|jy|rd|jddjdD}t |jr=|D]7}t |rt |j|s%y|j|k(s7yn_|j}|jr|d |jz }|D]0}|jj|s|j|s0ytd|5 t|j}dddryy #ttjf$rd }Y)wxYw#1swY.xYw) zL Returns whether we should bypass proxies or not. :rtype: bool ctjj|xs-tjj|jSN)rSrjrkupper)rs rB get_proxyz(should_bypass_proxies..get_proxys-zz~~c"AbjjnnSYY[&AArENno_proxyTc3&K|] }|s| ywr/rd)rer=s rBrgz(should_bypass_proxies..sRTTDRs r,:F)rrqr9r8r#r'rportendswithr,rrrgaierror) rsr2r1 no_proxy_argparsedproxy_iphost_with_portr=bypasss rBshould_bypass_proxiesr?sZB LZ( c]F SX%5%5c2%>%D%DS%IR 6?? +$  *)&//8D#__0  $__N{{Afkk]"33  ??++D1^5L5LT5R  Z . !&//2F  6??+ F  s*ED22E EEEEc4t||riStS)zA Return a dict of environment proxies. :rtype: dict r2)r?r)rsr2s rBget_environ_proxiesrB2s S84 |rEc.|xsi}t|}|j+|j|j|jdS|jdz|jz|jd|jzdg}d}|D]}||vs||}|S|S)zSelect a proxy for the url, if applicable. :param url: The url being for the request :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs Nallz://zall://)rrqrkscheme)rsproxiesurlparts proxy_keysproxy proxy_keys rB select_proxyrK>s mG}H {{8??GKK,>?? %("3"338$$$ J E  I&E  L  LrEc8||ni}|j}t|j}|jd}|j }|rOt ||sBt ||}|j||jd}|r|j|||S)aThis method takes proxy information from a request and configuration input to resolve a mapping of target proxies. This will consider settings such a NO_PROXY to strip proxy configurations. :param request: Request or PreparedRequest :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs :param trust_env: Boolean declaring whether to trust environment configs :rtype: dict r2rArD)rsrrErkcopyr?rB setdefault) requestrF trust_envrsrEr2 new_proxiesenviron_proxiesrIs rBresolve_proxiesrSXs!,g"G ++C c] ! !F{{:&H,,.K.sXF-cHE##FO,?,?,FG   " "65 1 rEc|dtS)zO Return a string representing the default user agent. :rtype: str rr)r}s rBdefault_user_agentrUss V1[M ""rEc:tttdddS)z9 :rtype: requests.structures.CaseInsensitiveDict z*/*z keep-alive)z User-AgentzAccept-EncodingAccept Connection)r"rUDEFAULT_ACCEPT_ENCODINGrdrErBdefault_headersrZ|s% ,.6&   rEcg}d}|j|}|s|Stjd|D]} |jdd\}}d|jdi}|jdD]:} |jd\}}|j|||j|<<|j ||S#t$r|d}}YwxYw#t$rY4wxYw) zReturn a list of parsed link headers proxies. i.e. Link: ; rel=front; type="image/jpeg",; rel=back;type="image/jpeg" :rtype: list z '"z, * '"r)rr:r8r7r) rlinks replace_charsvalrsrlinkrrs rBparse_header_linksr`s EM KK &E  xx& "))C+KCsyy*+\\#& HE "[[- U.3[[-GD=) *  H  T!$ L "rC "  s#B?1C? CC CCasciirRc|dd}|tjtjfvry|ddtjk(ry|ddtjtj fvry|j t}|dk(ry |dk(r |dddtk(ry |d ddtk(ry |dk(r|ddtk(ry |d dtk(ryy)z :rtype: str Nzutf-32rcz utf-8-sigrRzutf-16rrz utf-16-berz utf-16-lez utf-32-bez utf-32-le) r BOM_UTF32_LE BOM_UTF32_BEBOM_UTF8 BOM_UTF16_LE BOM_UTF16_BEr%_null_null2_null3)datasample nullcounts rBguess_json_utfrqs"1XF &%%v':':;; bqzV__$ bqzf))6+>+>?? U#IA~A~ #A#;&  !$Q$<6 !A~ "1:  !":  rEct|}|\}}}}}}} |j} | s|| }} |rdj|| g} ||}|d}t|| |d|| fS)zGiven a URL that may or may not have a scheme, prepend the given scheme. Does not replace a present scheme with the one provided as an argument. :rtype: str @r)rnetlocrr) rs new_schemer;rEauthr=r7rmqueryfragmentrts rBprepend_scheme_if_neededrys{ s^F6<3FD$dE8 ]]F V 4.) ~ | vvtRA BBrEct|} t|jt|jf}|S#tt f$rd}Y|SwxYw)z{Given a url with authentication components, extract them into a tuple of username,password. :rtype: (str,str) )rr)rrusernamepasswordr[r)rsr;rvs rBget_auth_from_urlr}sU c]F('&//*BC K I & Ks*9A AcB|\}}t||dt||dy)zVerifies that header parts don't contain leading whitespace reserved characters, or return characters. :param header: tuple, in the format (name, value). rrN)_validate_header_part)rr}rs rBcheck_header_validityrs% KD%&$*&%+rEc t|tr t|}n7t|tr t|}nt d|d|dt ||j|s|dk(rdnd}t d|d|y) Nz Header part (z) from z# must be of type str or bytes, not rr}rzSInvalid leading whitespace, reserved character(s), or returncharacter(s) in header z: )rrr rr rtyper;)r header_partheader_validator_index validator header_kinds rBrr s+s#*+AB K '+,BC K?'&:115k1B0C E  ??; ' 6! ;f &&1]"[O E   (rEc~t|\}}}}}}|s||}}|jddd}t|||||dfS)zW Given a url remove the fragment and the authentication part. :rtype: str rsrrr)rrsplitr)rsrErtrmrrwrxs rB urldefragauthr sU 5=SM1FFD&% V ]]3 "2 &F vvtVUB? @@rEct|jdd}|.t|jtr ||jyt d#t $r t dwxYw)zfMove file pointer back to its recorded starting position so it can be read again on redirect. rQNz;An error occurred when rewinding request body for redirect.z+Unable to rewind request body for redirect.)rbodyr_body_positionrr6r!)prepared_request body_seeks rB rewind_bodyr1st(--vtrs #0$ :) ," $c* )) BHHWl489JKL <<7"H / ?3D0 f&"J   6<@F2 02%22"$PM 05, 550 11*9x 46# "L  g  @C:  , &A"SrE