fHdZddlZddlZddlZddlmZddlmZmZm Z m Z m Z  ddl Z GddZGdd Zd Zd Zdd ZGd deZGdde j,eZdZdZdZddZdZy#e $rddlZ Y[wxYw)z requests.cookies ~~~~~~~~~~~~~~~~ Compatibility code to be able to use `http.cookiejar.CookieJar` with requests. requests.utils imports from here, so be careful with imports. N)to_native_string)MorselMutableMapping cookieliburlparse urlunparseceZdZdZdZdZdZdZdZdZ dZ dd Z d Z d Z d ZedZedZedZy ) MockRequestaWraps a `requests.Request` to mimic a `urllib2.Request`. The code in `http.cookiejar.CookieJar` expects this interface in order to correctly manage cookie policies, i.e., determine whether a cookie can be set, given the domains of the request and the cookie. The original request object is read-only. The client is responsible for collecting the new headers via `get_new_headers()` and interpreting them appropriately. You probably want `get_cookie_header`, defined below. c|||_i|_t|jjj|_yN)_r _new_headersrurlschemetype)selfrequests Y/var/lib/jenkins/workspace/mettalog/venv/lib/python3.12/site-packages/requests/cookies.py__init__zMockRequest.__init__#s+TWW[[)00 c|jSr )rrs rget_typezMockRequest.get_type(s yyrcTt|jjjSr )rrrnetlocrs rget_hostzMockRequest.get_host+s $+++rc"|jSr rrs rget_origin_req_hostzMockRequest.get_origin_req_host.s}}rc|jjjds|jjSt |jjdd}t |jj}t |j||j|j|j|jgS)NHostzutf-8)encoding) rheadersgetrrrr rpathparamsqueryfragment)rhostparseds r get_full_urlzMockRequest.get_full_url1sww""6*77;;  7'J$''++&        rcyNTrs ris_unverifiablezMockRequest.is_unverifiableEsrcR||jjvxs||jvSr )rr$rrnames r has_headerzMockRequest.has_headerHs%tww&C$$2C2C*CCrNc|jjj||jj||Sr )rr$r%r)rr3defaults r get_headerzMockRequest.get_headerKs0ww""4):):)>)>tW)MNNrctd)zMcookiejar has no legitimate use for this method; add it back if you find one.z=Cookie headers should be added with add_unredirected_header())NotImplementedError)rkeyvals r add_headerzMockRequest.add_headerNs! K  rc"||j|<yr rrr3values radd_unredirected_headerz#MockRequest.add_unredirected_headerTs"'$rc|jSr r>rs rget_new_headerszMockRequest.get_new_headersWs   rc"|jSr )r0rs r unverifiablezMockRequest.unverifiableZs##%%rc"|jSr )r rs rorigin_req_hostzMockRequest.origin_req_host^s''))rc"|jSr rrs rr*zMockRequest.hostbs}}rr )__name__ __module__ __qualname____doc__rrrr r,r0r4r7r<rArCpropertyrErGr*r/rrr r s| 1 , (DO (!&&**rr c"eZdZdZdZdZdZy) MockResponsezWraps a `httplib.HTTPMessage` to mimic a `urllib.addinfourl`. ...what? Basically, expose the parsed HTTP headers from the server response the way `http.cookiejar` expects to see them. c||_y)zMake a MockResponse for `cookiejar` to read. :param headers: a httplib.HTTPMessage or analogous carrying the headers N_headers)rr$s rrzMockResponse.__init__ns   rc|jSr rQrs rinfozMockResponse.infous }}rc:|jj|yr )rR getheadersr2s rrVzMockResponse.getheadersxs   &rN)rIrJrKrLrrTrVr/rrrOrOgs  'rrOct|dr |jsyt|}t|jj}|j ||y)zExtract the cookies from the response into a CookieJar. :param jar: http.cookiejar.CookieJar (not necessarily a RequestsCookieJar) :param request: our own requests.Request object :param response: urllib3.HTTPResponse object _original_responseN)hasattrrXr rOmsgextract_cookies)jarrresponsereqress rextract_cookies_to_jarr`|sJ H2 38S8S g C x2266 7CS!rcxt|}|j||jjdS)zj Produce an appropriate Cookie header string to be sent with `request`, or None. :rtype: str Cookie)r add_cookie_headerrCr%)r\rrs rget_cookie_headerres6 GA!    " "8 ,,rcg}|D]h}|j|k7r|||jk7r%|||jk7r7|j|j|j|jfj|D]\}}}|j |||y)zkUnsets a cookie by name, by default over all domains and paths. Wraps CookieJar.clear(), is O(n). N)r3domainr&appendclear) cookiejarr3rgr& clearablescookies rremove_cookie_by_namerms J ;;$    &FMM"9    3 6==&++v{{CD)dd+)rceZdZdZy)CookieConflictErrorzThere are two cookies that meet the criteria specified in the cookie jar. Use .get and .set and include domain and path args in order to be more specific. N)rIrJrKrLr/rrrorosrroceZdZdZddZdZdZdZdZdZ dZ d Z d Z d Z d Zdd ZfdZdZdZdZfdZfdZddZddZdZdZdZdZxZS)RequestsCookieJaraCompatibility class; is a http.cookiejar.CookieJar, but exposes a dict interface. This is the CookieJar we create by default for requests and sessions that don't specify one, since some clients may expect response.cookies and session.cookies to support dict operations. Requests does not use the dict interface internally; it's just for compatibility with external client code. All requests code should work out of the box with externally provided instances of ``CookieJar``, e.g. ``LWPCookieJar`` and ``FileCookieJar``. Unlike a regular CookieJar, this class is pickleable. .. warning:: dictionary operations that are normally O(1) may be O(n). cL |j|||S#t$r|cYSwxYw)zDict-like get() that also supports optional domain and path args in order to resolve naming collisions from using one cookie jar over multiple domains. .. warning:: operation is O(n), not O(1). )_find_no_duplicatesKeyError)rr3r6rgr&s rr%zRequestsCookieJar.gets0 ++D&$? ? N s  ##c |.t|||jd|jdyt|tr t |}n t ||fi|}|j ||S)zDict-like set() that also supports optional domain and path args in order to resolve naming collisions from using one cookie jar over multiple domains. Nrgr&)rgr&)rmr% isinstancermorsel_to_cookie create_cookie set_cookie)rr3r@kwargscs rsetzRequestsCookieJar.setsg = !d6::h#7fjj>P   eV $ 'AdE4V4A rc#HKt|D]}|jyw)zDict-like iterkeys() that returns an iterator of names of cookies from the jar. .. seealso:: itervalues() and iteritems(). N)iterr3rrls riterkeyszRequestsCookieJar.iterkeyss 4jF++ ! "c4t|jS)zDict-like keys() that returns a list of names of cookies from the jar. .. seealso:: values() and items(). )listrrs rkeyszRequestsCookieJar.keyss DMMO$$rc#HKt|D]}|jyw)zDict-like itervalues() that returns an iterator of values of cookies from the jar. .. seealso:: iterkeys() and iteritems(). N)r~r@rs r itervalueszRequestsCookieJar.itervaluess 4jF,, !rc4t|jS)zDict-like values() that returns a list of values of cookies from the jar. .. seealso:: keys() and items(). )rrrs rvalueszRequestsCookieJar.valuess DOO%&&rc#`Kt|D]}|j|jfyw)zDict-like iteritems() that returns an iterator of name-value tuples from the jar. .. seealso:: iterkeys() and itervalues(). N)r~r3r@rs r iteritemszRequestsCookieJar.iteritemss) 4jF++v||+ +!s,.c4t|jS)zDict-like items() that returns a list of name-value tuples from the jar. Allows client-code to call ``dict(RequestsCookieJar)`` and get a vanilla python dict of key value pairs. .. seealso:: keys() and values(). )rrrs ritemszRequestsCookieJar.items sDNN$%%rc~g}t|D],}|j|vs|j|j.|S)z2Utility method to list all the domains in the jar.r~rgrhrdomainsrls r list_domainszRequestsCookieJar.list_domainss84jF}}G+v}}-!rc~g}t|D],}|j|vs|j|j.|S)z0Utility method to list all the paths in the jar.)r~r&rh)rpathsrls r list_pathszRequestsCookieJar.list_pathss84jF{{%' V[[)! rcg}t|D]9}|j|j|vry|j|j;y)zvReturns True if there are multiple domains in the jar. Returns False otherwise. :rtype: bool TFrrs rmultiple_domainsz"RequestsCookieJar.multiple_domains%sD 4jF}}(V]]g-E NN6== )!rci}t|D]?}||j|k(s||j|k(s'|j||j<A|S)zTakes as an argument an optional domain and path and returns a plain old Python dict of name-value pairs of cookies that meet the requirements. :rtype: dict )r~rgr&r@r3)rrgr& dictionaryrls rget_dictzRequestsCookieJar.get_dict2sQ 4jF&--6"9  t 3*0,, 6;;' ! rcB t||S#t$rYywxYwr.)super __contains__ro)rr3 __class__s rrzRequestsCookieJar.__contains__As* 7'- -"  s  c$|j|S)zDict-like __getitem__() for compatibility with client code. Throws exception if there are more than one cookie with name. In that case, use the more explicit get() method instead. .. warning:: operation is O(n), not O(1). )rsr2s r __getitem__zRequestsCookieJar.__getitem__Gs''--rc(|j||y)zDict-like __setitem__ for compatibility with client code. Throws exception if there is already a cookie of that name in the jar. In that case, use the more explicit set() method instead. N)r|r?s r __setitem__zRequestsCookieJar.__setitem__Ps urct||y)zqDeletes a cookie given a name. Wraps ``http.cookiejar.CookieJar``'s ``remove_cookie_by_name()``. N)rmr2s r __delitem__zRequestsCookieJar.__delitem__Ws dD)rct|jdrW|jjdr<|jjdr!|jj dd|_t ||g|i|S)N startswith"z\")rYr@rendswithreplacerry)rrlargsrzrs rryzRequestsCookieJar.set_cookie]sg FLL, / '', %%c*!<<//r:FLw!&:4:6::rct|tjr,|D]&}|jt j|(yt ||y)zAUpdates this jar with cookies from another CookieJar or dict-likeN)rvr CookieJarrycopyrupdate)rotherrlrs rrzRequestsCookieJar.updatefs@ eY00 1 & 12  GN5 !rct|D]C}|j|k(s||j|k(s%||j|k(s7|jcSt d|d|d|)aRequests uses this method internally to get cookie values. If there are conflicting cookies, _find arbitrarily chooses one. See _find_no_duplicates if you want an exception thrown if there are conflicting cookies. :param name: a string containing name of cookie :param domain: (optional) string containing domain of cookie :param path: (optional) string containing path of cookie :return: cookie.value name= , domain=, path=)r~r3rgr&r@rt)rr3rgr&rls r_findzRequestsCookieJar._findnsh4jF{{d">V]]f%<|v{{d':%||+ ! thizIJJrcd}t|D]R}|j|k(s||j|k(s%||j|k(s7|t d||j }T|r|St d|d|d|)aBoth ``__get_item__`` and ``get`` call this function: it's never used elsewhere in Requests. :param name: a string containing name of cookie :param domain: (optional) string containing domain of cookie :param path: (optional) string containing path of cookie :raises KeyError: if cookie is not found :raises CookieConflictError: if there are multiple cookies that match name and optionally domain and path :return: cookie.value Nz&There are multiple cookies with name, rrr)r~r3rgr&ror@rt)rr3rgr&toReturnrls rrsz%RequestsCookieJar._find_no_duplicatess4jF{{d">V]]f%<|v{{d':#/"5"H Q#$*<<! OthizIJJrc\|jj}|jd|S)4Unlike a normal CookieJar, this class is pickleable. _cookies_lock)__dict__rpoprstates r __getstate__zRequestsCookieJar.__getstate__s% ""$ /" rc|jj|d|jvrtj|_yy)rrN)rr threadingRLockrrs r __setstate__zRequestsCookieJar.__setstate__s4 U# $-- /!*!2D  0rczt}|j|j|j||S)z(Return a copy of this RequestsCookieJar.)rq set_policy get_policyr)rnew_cjs rrzRequestsCookieJar.copys0"$$//+, d rc|jS)z&Return the CookiePolicy instance used.)_policyrs rrzRequestsCookieJar.get_policys ||r)NNNNN)rIrJrKrLr%r|rrrrrrrrrrrrrrryrrrsrrrr __classcell__)rs@rrqrqs" &%',&   .* ;"K(K:3 rrqc|yt|dr|jStj|}|j|D]&}|jtj|(|S)Nr)rYrriry)r\new_jarrls r_copy_cookie_jarrs\ {sFxxziinG MMO499V,- Nrc fd||ddddddddddidd }t|t|z }|rtd t||j|t |d |d <t |d |d <|d j d|d<t |d|d<t jdi|S)zMake a cookie from underspecified parameters. By default, the pair of `name` and `value` will be set for the domain '' and sent on every request (this is sometimes called a "supercookie"). rNr/FTHttpOnly) versionr3r@portrgr&secureexpiresdiscardcomment comment_urlrestrfc2109z2create_cookie() got unexpected keyword arguments: rport_specifiedrgdomain_specified.domain_initial_dotr&path_specifiedr/)r| TypeErrorrrboolrrrb)r3r@rzresultbadargss rrxrxsT"F &kCK'G@g P   MM&#F6N3F !%fX&6!7F #)(#3#>#>s#CF  #F6N3F     %f %%rcd}|dr. ttjt|dz}n3|dr.d}t j tj |d|}t|dt|dd|d ||j|d dd |d idt|d |j|dxsd S#t$rtd|ddwxYw)zBConvert a Morsel object into a Cookie containing the one k/v pair.Nzmax-agez max-age: z must be integerrz%a, %d-%b-%Y %H:%M:%S GMTrFrgr&rhttponlyrrr) rrrrgrr3r&rrrrr@r) inttime ValueErrorrcalendartimegmstrptimerxrr:r@)morselr time_templates rrwrwsG i M$))+F9,=(>>?G  3 //$--y0A="QR y! *+h ZZ F^ &, -F8$%lly!&Q   Miy(9'::JKL L Ms ,CCc| t}|E|Dcgc]}|j}}|D]'}|s||vs |jt|||)|Scc}w)aCReturns a CookieJar from a key/value dictionary. :param cookie_dict: Dict of key/values to insert into CookieJar. :param cookiejar: (optional) A cookiejar to add the cookies to. :param overwrite: (optional) If False, will not replace cookies already in the jar with new ones. :rtype: CookieJar )rqr3ryrx) cookie_dictrj overwriterlnames_from_jarr3s rcookiejar_from_dictr sj%' 4=>&&++>>DT7$$]4T9J%KL   ?sAc<t|tjs tdt|trt ||d}|St|tjr |j ||S|S#t$r|D]}|j|Y|SwxYw)zAdd cookies to cookiejar and returns a merged CookieJar. :param cookiejar: CookieJar object to add the cookies to. :param cookies: Dictionary or CookieJar object to be added. :rtype: CookieJar z!You can only merge into CookieJarF)rjr) rvrrrdictrrAttributeErrorry)rjcookies cookie_in_jars r merge_cookiesrs i!4!4 5<=='4 '9PUV   GY00 1 4   W % 9  4!( $$]3")   4s!A66!BBrr.)rLrrr_internal_utilsrcompatrrrrr r ImportErrordummy_threadingr rOr`rerm RuntimeErrorrorrqrrxrwrrr/rrrs -KK( MM`''* " -,&, E ++^EP "&J:*W('(sA88 BB