7,j-T dZddlmZddlZddlZddlZddlmZmZddl m Z m Z m Z ddl mZmZmZmZddlmZdd lmZdd lmZmZmZmZer dd lmZdd lmZddl Z Gd dZ!GddZ"dd?d!Z%Gd"d#e&Z'Gd$d%e ee(e(dzfZ)d@d'Z*dAd+Z+dBd.Z,ed/e 0Z-e dCdDd6Z.e dEdFd7Z. dCdGd8Z.dHd;Z/dS)Iz requests.cookies ~~~~~~~~~~~~~~~~ Compatibility code to be able to use `http.cookiejar.CookieJar` with requests. requests.utils imports from here, so be careful with imports. ) annotationsN)IteratorMutableMapping)Cookie CookieJar CookiePolicy) TYPE_CHECKINGAnyTypeVaroverload)to_native_string) is_prepared)Morsel cookieliburlparse urlunparse)SupportsKeysAndGetItem)PreparedRequestceZdZUdZded<ddZd d Zd d Zd d Zd d Z d!dZ d"dZ d#d$dZ d%dZ d&dZd'dZed!dZed dZed dZdS)( MockRequestaWraps a `requests.PreparedRequest` 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. strtyperequestrreturnNonect|sJ||_i|_t|jjj|_dSN) _is_prepared_r _new_headersrurlschemer)selfrs @C:\PYTHON\GemmaClient\venv\Lib\site-packages\requests/cookies.py__init__zMockRequest.__init__-s?G$$$$$,.TW[))0 c|jSr)rr$s r%get_typezMockRequest.get_type3s yr'c>t|jjjSr)rr r"netlocr)s r%get_hostzMockRequest.get_host6s $$++r'c*|Srr-r)s r%get_origin_req_hostzMockRequest.get_origin_req_host9s}}r'c(|jjds |jjSt |jjdd}t |jj}t |j||j|j |j |j gS)NHostzutf-8)encoding) r headersgetr"rrrr#pathparamsqueryfragment)r$hostparseds r% get_full_urlzMockRequest.get_full_url<sw""6** 7;  7'JJJ$'+&&         r'boolcdSNTr)s r%is_unverifiablezMockRequest.is_unverifiablePstr'namec0||jjvp||jvSr)r r4r!r$rBs r% has_headerzMockRequest.has_headerSstw&C$$2C*CCr'Ndefault str | Nonect|jj||j||Sr)r r4r5r!)r$rBrFs r% get_headerzMockRequest.get_headerVs/w""4):)>)>tW)M)MNNNr'keyvalc td)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)r$rJrKs r% add_headerzMockRequest.add_headerYs! K   r'valuec||j|<dSrr!r$rBrOs r%add_unredirected_headerz#MockRequest.add_unredirected_header_s"'$r'dict[str, str]c|jSrrQr)s r%get_new_headerszMockRequest.get_new_headersbs   r'c*|Sr)rAr)s r% unverifiablezMockRequest.unverifiablees##%%%r'c*|Sr)r0r)s r%origin_req_hostzMockRequest.origin_req_hostis'')))r'c*|Srr/r)s r%r:zMockRequest.hostms}}r')rrrr)rrrr=)rBrrr=r)rBrrFrGrrG)rJrrKrrr)rBrrOrrr)rrT)__name__ __module__ __qualname____doc____annotations__r&r*r-r0r<rArErIrNrSrVpropertyrXrZr:r@r'r%rrsu  III1111 ,,,,    (DDDDOOOOO    ((((!!!!&&&X&***X*Xr'rc*eZdZdZd dZd dZdd Zd S) 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. r4r rrc||_dS)zMake a MockResponse for `cookiejar` to read. :param headers: a httplib.HTTPMessage or analogous carrying the headers N_headers)r$r4s r%r&zMockResponse.__init__ys   r'c|jSrrfr)s r%infozMockResponse.infos }r'rBrc:|j|dSr)rg getheadersrDs r%rkzMockResponse.getheaderss   &&&&&r'N)r4r rr)rr )rBrrr )r]r^r_r`r&rirkr@r'r%rdrdrsZ     ''''''r'rdjarrrrresponser rrct|dr|jsdSt|}t|jj}|||dS)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)hasattrrorrdmsgextract_cookies)rlrrmreqress r%extract_cookies_to_jarrusb H2 3 38S g  C x26 7 7CS!!!!!r'rGct|}|||dS)zj Produce an appropriate Cookie header string to be sent with `request`, or None. :rtype: str r)radd_cookie_headerrVr5)rlrrs r%get_cookie_headerrysE GA!     " "8 , ,,r' cookiejarrBrdomainr6cg}|D]Q}|j|kr| ||jkr| ||jkr*||j|j|jfR|D]\}}}||||dS)zkUnsets a cookie by name, by default over all domains and paths. Wraps CookieJar.clear(), is O(n). N)rBr{r6appendclear)rzrBr{r6 clearablescookies r%remove_cookie_by_namers.0JEE ;$     &FM"9"9    3 3 6=&+v{CDDDD(,,dd++++,,r'ceZdZdZdS)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)r]r^r_r`r@r'r%rrsr'rceZdZUdZded< ddZd?dZd@dZdAdZ dBdZ dCdZ dDdZ d@dZ d@d ZdEd"Z dFdGd$ZdHfd& ZdIfd( ZdJd)ZdKd+ZdLd,ZdMfd0 ZdNfd3 Z dFdOd4Z dFdPd5ZdQd7ZdRd9ZdSd:ZdTd;ZxZS)URequestsCookieJaraCompatibility 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). r_policyNrBrrFrGr{r6rcV ||||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)r$rBrFr{r6s r%r5zRequestsCookieJar.getsC ++D&$?? ?   NNN s  ((rO#str | Morsel[dict[str, str]] | Nonekwargsr Cookie | Nonec |;t|||d|ddSt|trt |}nt ||fi|}|||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. Nr{r6)r{r6)rr5 isinstancermorsel_to_cookie create_cookie set_cookie)r$rBrOrcs r%setzRequestsCookieJar.sets = !d6::h#7#7fjj>P>P     F eV $ $ 5 ''AAdE44V44A r' Iterator[str]c#@Kt|D] }|jV dS)zDict-like iterkeys() that returns an iterator of names of cookies from the jar. .. seealso:: itervalues() and iteritems(). N)iterrBr$rs r%iterkeyszRequestsCookieJar.iterkeyss8 4jj  F+      r' list[str]cDt|S)zDict-like keys() that returns a list of names of cookies from the jar. .. seealso:: values() and items(). )listrr)s r%keyszRequestsCookieJar.keyss DMMOO$$$r'Iterator[str | None]c#@Kt|D] }|jV dS)zDict-like itervalues() that returns an iterator of values of cookies from the jar. .. seealso:: iterkeys() and iteritems(). N)rrOrs r% itervalueszRequestsCookieJar.itervalues s8 4jj  F,      r'list[str | None]cDt|S)zDict-like values() that returns a list of values of cookies from the jar. .. seealso:: keys() and items(). )rrr)s r%valueszRequestsCookieJar.valuess DOO%%&&&r' Iterator[tuple[str, str | None]]c#NKt|D]}|j|jfVdS)zDict-like iteritems() that returns an iterator of name-value tuples from the jar. .. seealso:: iterkeys() and itervalues(). N)rrBrOrs r% iteritemszRequestsCookieJar.iteritemss? 4jj , ,F+v|+ + + + + , ,r'list[tuple[str, str | None]]cDt|S)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(). )rrr)s r%itemszRequestsCookieJar.items%sDNN$$%%%r'ctg}t|D]%}|j|vr||j&|S)z2Utility method to list all the domains in the jar.rr{r}r$domainsrs r% list_domainszRequestsCookieJar.list_domains.sC4jj . .F}G++v}---r'ctg}t|D]%}|j|vr||j&|S)z0Utility method to list all the paths in the jar.)rr6r})r$pathsrs r% list_pathszRequestsCookieJar.list_paths6sC4jj * *F{%'' V[))) r'r=cg}t|D]/}|j |j|vrdS||j0dS)zvReturns True if there are multiple domains in the jar. Returns False otherwise. :rtype: bool NTFrrs r%multiple_domainsz"RequestsCookieJar.multiple_domains>sU  4jj * *F}(V]g-E-Ett NN6= ) ) ) )ur'dict[str, str | None]ci}t|D]+}| |j|kr| |j|kr|j||j<,|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 )rr{r6rOrB)r$r{r6 dictionaryrs r%get_dictzRequestsCookieJar.get_dictKsU-/ 4jj 7 7F&-6"9"9  t 3 3*0, 6;'r'Iterator[Cookie]cDtS)zDRequestCookieJar's __iter__ comes from CookieJar not MutableMapping.)super__iter__)r$ __class__s r%rzRequestsCookieJar.__iter__\sww!!!r'objectcj t|S#t$rYdSwxYwr?)r __contains__r)r$rBrs r%rzRequestsCookieJar.__contains__`sB 77''-- -"   44 s $ 22c,||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). )rrDs r% __getitem__zRequestsCookieJar.__getitem__fs''---r'rc2|||dS)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)rrRs r% __setitem__zRequestsCookieJar.__setitem__os ur'c&t||dS)zqDeletes a cookie given a name. Wraps ``http.cookiejar.CookieJar``'s ``remove_cookie_by_name()``. N)rrDs r% __delitem__zRequestsCookieJar.__delitem__xs dD)))))r'rrargsc|jx}E|dr0|dr|dd|_t j|g|Ri|S)N"z\")rO startswithendswithreplacerr)r$rrrrOrs r%rzRequestsCookieJar.set_cookie~sxl "U /  %% 0s## 0!==33FL!uww!&:4:::6:::r'other,CookieJar | SupportsKeysAndGetItem[str, str]ct|tjr.|D])}|t j|*dSt |dS)zAUpdates this jar with cookies from another CookieJar or dict-likeN)rrrrcopyrupdate)r$rrrs r%rzRequestsCookieJar.updatesp eY0 1 1 " 3 3 & 1 12222 3 3 GGNN5 ! ! ! ! !r'ct|D]0}|j|kr#| |j|kr| |j|kr |jcS1t 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 Nname= , domain=, path=)rrBr{r6rOr)r$rBr{r6rs r%_findzRequestsCookieJar._finds~4jj , ,F{d"">V]f%<%<|v{d':':%|+++ItIIIIIIJJJr'cd}t|D]B}|j|kr5| |j|kr(| |j|kr|t d||j}C||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)rrBr{r6rrOr)r$rBr{r6toReturnrs r%rz%RequestsCookieJar._find_no_duplicatess4jj 0 0F{d"">V]f%<%<|v{d':':#/"5 Q Q Q##$*<  OItIIIIIIJJJr'dict[str, Any]cb|j}|d|S)4Unlike a normal CookieJar, this class is pickleable. _cookies_lock)__dict__rpopr$states r% __getstate__zRequestsCookieJar.__getstate__s, ""$$ /""" r'rc|j|d|jvrtj|_dSdS)rrN)rr threadingRLockrrs r% __setstate__zRequestsCookieJar.__setstate__sC U### $- / /!*!2!2D    0 /r'ct}|||||S)z(Return a copy of this RequestsCookieJar.)r set_policy get_policyr)r$new_cjs r%rzRequestsCookieJar.copysB"$$$//++,,, d r'c|jS)z&Return the CookiePolicy instance used.)rr)s r%rzRequestsCookieJar.get_policys |r')NNN) rBrrFrGr{rGr6rGrrG)rBrrOrrr rr)rr)rr)rr)rr)rr)rrr\NN)r{rGr6rGrr)rr)rBrrr=)rBrrrG)rBrrOrrr)rBrrr)rrrr rr rr)rrrr)rBrr{rGr6rGrrG)rBrr{rGr6rGrr)rr)rrrr)rr)rr)r]r^r_r`rar5rrrrrrrrrrrrrrrrrrrrrrrr __classcell__)rs@r%rrs" #! $*%%%%'''',,,,&&&&    =A""""""" ....**** ;;;;;;""""""HLKKKKK.HLKKKKK>3333 r'rCookieJar | Nonec|dSt|ddx}r |Stj|}||D])}|tj|*|S)Nr)getattrrr~r)rl copy_methodnew_jarrs r%_copy_cookie_jarrs {tc64000{{}}innG MMOOO..49V,,---- Nr'rOrrc d||ddddddddddidd }t|t|z }|rtd t|||t |d |d <t |d |d <|d 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) versionrBrOportr{r6secureexpiresdiscardcomment comment_urlrestrfc2109z2create_cookie() got unexpected keyword arguments: rport_specifiedr{domain_specified.domain_initial_dotr6path_specifiedr@)r TypeErrorrrr=rrr)rBrOrresultbadargss r%rrsT"F &kkCKK'G  Pg P P    MM&#F6N33F !%fX&6!7!7F #)(#3#>#>s#C#CF  #F6N33F    % %f % %%r'morsel Morsel[Any]cd}|dr_ ttjt|dz}n^#t$rtd|ddwxYw|dr/d}t jtj|d|}t|dt|dd|d ||j |d dd |d idt|d |j |dpd S)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 GMTrFr{r6rhttponlyrrr) rrrr{rrBr6rrrrrOr) inttime ValueErrorrcalendartimegmstrptimerr=rJrO)rr time_templates r%rrs3G iS M$)++F9,=(>(>>??GG M M MKy(9KKKLL L M  S3 /$-y0A="Q"QRR y! *++h Z F^ &, -F8$%%ly!&Q   s 6A$A' _CookieJarT)boundT cookie_dictdict[str, str] | None overwriter=cdSrr@rrzrs r%cookiejar_from_dictr3s r'cdSrr@rs r%rr;s #r'c|t}|@d|D}|D]1}|s||vr)|t|||2|S)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 Ncg|] }|j Sr@)rB).0rs r% z'cookiejar_from_dict..Ts>>>&&+>>>r')rrr)rrzrnames_from_jarrBs r%rrCs|%'' >>I>>> M MD MT77$$]4T9J%K%KLLL r'cookies!dict[str, str] | CookieJar | NonecNt|tjstdt|trt ||d}nSt|tjr9t |ddx}r ||n|D]}|||S)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)rzrrN)rrrr dictrrr)rzr update_method cookie_in_jars r% merge_cookiesr%\s i!4 5 5><==='4  4'9PUVVV GY0 1 14#Ix>> >= 4 M' " " " "!( 4 4 $$]3333 r')rlrrrrmr rr)rlrrrrrGr) rzrrBrr{rGr6rGrr)rlrrr)rBrrOrrr rr)rrrrr?)rrrzrrr=rr)T)rrrzrrr=rr)rrrzrrr=rr)rzrrr rr)0r` __future__rr rr collections.abcrrhttp.cookiejarrrrtypingr r r r _internal_utilsr_typesrrcompatrrrr _typeshedrmodelsrrrrdruryr RuntimeErrorrrrrrrrrr%r@r'r%r0s#""""" 44444444::::::::::888888888888------//////;;;;;;;;;;;;(000000''''''PPPPPPPPf''''''''*""""$----TX,,,,,*, ]]]]] >#sTz/#B]]]@    "&"&"&"&J:gm9555     #'2r'