fSZdZddlZddlZddlZddlZddlZddlZddlZddlZddl 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}td|}|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 ValueErrorsplitfilterreplacerematchI)hostr0internetSettings proxyEnable proxyOverridetests W/var/lib/jenkins/workspace/mettalog/venv/lib/python3.12/site-packages/requests/utils.pyproxy_bypass_registryrDLs    %~~((N   f112BMRSTUVK"//0@/RSTUM- &++C0 t]3 !Dy d?<<U+D<<U+D<<T*DxxdBDD)"C  $  s#C(AC7( C43C47D D cBtr t|St|S)zReturn True, if the host should be bypassed. Checks proxy settings gathered from the environment, if specified, or the registry. )rrrD)r>s rCrrrs " #+D1 1(. .c>t|dr|j}|S)z/Returns an internal sequence dictionary update.items)hasattrrH)ds rCdict_to_sequencerK~sq' GGI HrFcd}d}t|tr|jd}t|dr t |}n~t|dr |j}net|drY |j }t j|j}d|jvrtjdt t|drW |j!}t|d r:|8 |j#dd |j!}|j#|xsd|d}t'd||z S#tjtf$rYwxYw#t$$rd}Y?wxYw#t$$r||}YRwxYw) Nrutf-8__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) isinstancerencoderIrOrPosfstatst_sizemodewarningswarnrioUnsupportedOperationAttributeErrorrRrSr6max)o total_lengthcurrent_positionrPs rC super_lenrdsuL!S HHW q)1v E uu H  XXZF88F+33L!&&  2$ q& % vvx q&!l&: %FF1aL#$668LFF+0q1 q,!11 22e''8    X%#$L%% 0 '#/  0s6$D2 E"(7E2E E EE" E21E2c|tjjd}||f}n dtD} ddlm}m}d}|D]E} tj j|}tj j|sC|}n|yt|} d} t|tr| jd} | jj| d} ||j!| } | r| drdnd} | | | d fSy#t$rYywxYw#|t"f$r|rYywxYw#t$t&f$rYywxYw) z;Returns the Requests tuple auth for a given url from netrc.NETRCNc3&K|] }d| yw)z~/N).0fs rC z!get_netrc_auth..s9Rs89sr)NetrcParseErrornetrc:asciirrT)rWenvironget NETRC_FILESrmrlpath expanduserKeyErrorexistsrrUrdecodenetlocr8authenticatorsr6r1r_)url raise_errors netrc_filenetrc_locationsrlrm netrc_pathrjlocrisplitstrr>r#login_is rCget_netrc_authrsS(J%-9[9- 0  A gg((+ww~~c" !    c] c3 w/Hyyx(+ :&55d;F%ay!aw333   : )    (   s`D)D#D)D) A D)+D DD)DD)D&#D)%D&&D))D;:D;ct|dd}|rBt|tr1|ddk7r(|ddk7rtjj |Syyyy)z0Tries to guess the filename of the given object.nameNr<>)getattrrUrrWrsbasename)objrs rCguess_filenamersT 3 %D 4,aCDHPSOww%%ET,trFcRtjj|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)rWrsrvr8joinzipfile is_zipfileZipFilenamelisttempfile gettempdir atomic_openwriteread)rsarchivememberprefixzip_filetmpextracted_path file_handlers rCextract_zipped_pathsr sE  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) rmkstemprWrsdirnamefdopenr: BaseExceptionremove)filenametmp_descriptortmp_name tmp_handlers rCrr1s (//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)rUrrboolr4r7rvalues rCfrom_key_val_listr>s6$ }%#udC01FGG u rFc|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) rUrrrr4r7rrHlistrs rCto_key_val_listrYsJ" }%#udC01FGG%!  ;rFcg}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 rCparse_list_headerrwsT.F"5) 8tBCy 'C ''Qr 3D d* MrFci}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)rrrrs rCparse_dict_headerrsx,F"5) d?F4L jja( e !9bc )c )(q5Et * MrFc|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 rrrrNrTz\\\z\")r:)r is_filenames rCrrsd qU2Y-#- La eBQi61==.66ucB B LrFcX|Dcic]}|j|j}}|Scc}w)zReturns a key/value dictionary from a CookieJar. :param cj: CookieJar object to extract cookies from. :rtype: dict )rr)cjcookie cookie_dicts rCdict_from_cookiejarrs/<>>6;; ,>K> ?s'ct||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 rCadd_dict_to_cookiejarrs {B //rFc^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=["\']*(.+?)["\'>])r[r\DeprecationWarningr;compiler=findall)content charset_re pragma_rexml_res rCget_encodings_from_contentrs  MM 1  @MJ IQSQUQUVI ZZ? @F 7#   G $ % .. ! "rFcH|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 rC_parse_content_type_headerrs\\# F!!9??,fQRj&LKN  C#jjoO"$,_-33NCo134::>J',K $  $$rFc|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/jsonrM)rqrr)headersrrs rCget_encoding_from_headersrs`;;~.L 5lCL&Fi &&u-- \)*rFc#K|j |Ed{ytj|jd}|D]}|j|}|s||jdd}|r|yy7ew)zStream decodes an iterator.Nr:errorsrFT)final)encodingcodecsgetincrementaldecoderrw)iteratorrdecoderchunkrvs rCstream_decode_response_unicoder4s 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)rO)string slice_lengthposs rC iter_slicesrEsW 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.)r:r) r[r\rrrrr UnicodeErrorr TypeError)rtried_encodingsrs rCget_unicode_from_responserOs 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 %rrrTz"Invalid percent-escape sequence: ''N) r8rangerOisalnumchrr4r7r UNRESERVED_SETr)uripartsihcs rCunquote_unreservedr |s 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)rr r )rsafe_with_percentsafe_without_percents rC requote_urirsJ/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 rCaddress_in_networkr s]]4!1!1"!5 6q 9FIIcNMGTmmD&"2"2>#d)3L"MNqQGmmD&"2"27";I)r inet_ntoarpack)maskrs rCrrs7 b4i1, ,D   FKKd3 44rFcN tj|y#t$rYywxYw)z :rtype: bool FT)rrr6) string_ips rCis_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 rrFr"rT)countr4r8r7rrr6)string_networkr%s rC 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)rWrprq)env_namer value_changed old_values rC set_environr1s%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)rWrprqupper)rs rC get_proxyz(should_bypass_proxies..get_proxys-zz~~c"AbjjnnSYY[&AArFNno_proxyTc3&K|] }|s| ywr4rh)rir>s rCrkz(should_bypass_proxies..sRTTDRs r,:F)rhostnamer:r8r(r,r portendswithr1rrrgaierror) rzr7r6 no_proxy_argparsedproxy_iphost_with_portr>bypasss rCshould_bypass_proxiesrEsPB 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 r7)rEr)rzr7s rCget_environ_proxiesrH:s S84 |rFc.|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://)rr<rqscheme)rzproxiesurlparts proxy_keysproxy proxy_keys rC select_proxyrQFs mG}H {{8??GKK,>?? %("3"338$$$ J E  I&E  L  LrFc8||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 as 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 r7rGrJ)rzrrKrqcopyrErH setdefault) requestrL trust_envrzrKr7 new_proxiesenviron_proxiesrOs rCresolve_proxiesrY`s!,g"G ++C c] ! !F{{:&H,,.K.sXF-cHE##FO,?,?,FG   " "65 1 rFc|dtS)zO Return a string representing the default user agent. :rtype: str rr)rs rCdefault_user_agentr[{s V1[M ""rFc:tttdddS)z9 :rtype: requests.structures.CaseInsensitiveDict z*/*z keep-alive)z User-AgentzAccept-EncodingAccept Connection)r"r[DEFAULT_ACCEPT_ENCODINGrhrFrCdefault_headersr`s% ,.6&   rFcg}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_charsvalrzrlinkrrs rCparse_header_linksrfs EM KK &E  xx& "))C+KCsyy*+\\#&E "[[- U.3[[-GD=) * '  T!'$ L "rC "  s#B?1C? CC CCrorTc|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-32rhz utf-8-sigrTzutf-16rrMz 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 rCguess_json_utfrvs"1XF &%%v':':;; bqzV__$ bqzf))6+>+>?? U#IA~A~ #A#;&  !$Q$<6 !A~ "1:  !":  rFct|}|\}}}}}}} |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)rrxrr) rz new_schemerArKauthr>r=rsqueryfragmentrxs rCprepend_scheme_if_neededr}s{ s^F6<3FD$dE8 ]]F V 4.) ~ | vvtRA BBrFct|} 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)rzrArzs rCget_auth_from_urlrsU 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)rrrs rCcheck_header_validityr s% KD%&$*&%+rFc 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 rrrzTInvalid leading whitespace, reserved character(s), or return character(s) in header z: )rUrr rr rtyper<)r header_partheader_validator_index validator header_kinds rCrrs+s#*+AB K '+,BC K?'&:115k1B0C E  ??; ' 6! ;f &&1]"[O E   (rFc~t|\}}}}}}|s||}}|jddd}t|||||dfS)zW Given a url remove the fragment and the authentication part. :rtype: str rxrrr)rrsplitr)rzrKrxrsrr{r|s rC urldefragauthr(sU 5=SM1FFD&% V ]]3 "2 &F vvtVUB? @@rFct|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. rSNz;An error occurred when rewinding request body for redirect.z+Unable to rewind request body for redirect.)rbodyrU_body_positionrr6r!)prepared_request body_seeks rC rewind_bodyr9st(--vtrs #0$ :) ," $c* )) BHHWl489JKL <<7$L / B3J6 r&"J   6<@F202%22"$PM 05, 550 11*:z 46# "L  g  @C:  , &A"SrF