3 æÈ[þGã @súdZddlZddlZddlZddlmZddlmZmZm Z m Z m Z y ddl Z Wne k rlddlZ YnXGdd„deƒZGdd „d eƒZd d „Zd d „Zddd„ZGdd„deƒZGdd„deje ƒZdd„Zdd„Zdd„Zd dd„Zdd„ZdS)!z­ requests.cookies ~~~~~~~~~~~~~~~~ Compatibility code to be able to use `cookielib.CookieJar` with requests. requests.utils imports from here, so be careful with imports. éNé)Úto_native_string)Ú cookielibÚurlparseÚ urlunparseÚMorselÚMutableMappingc@sŽeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z ddd„Z dd„Z dd„Z dd„Zedd„ƒZedd„ƒZedd„ƒZdS) Ú MockRequestaòWraps a `requests.Request` to mimic a `urllib2.Request`. The code in `cookielib.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. cCs ||_i|_t|jjƒj|_dS)N)Ú_rÚ _new_headersrÚurlÚschemeÚtype)ÚselfÚrequest©rú/usr/lib/python3.6/cookies.pyÚ__init__%szMockRequest.__init__cCs|jS)N)r)rrrrÚget_type*szMockRequest.get_typecCst|jjƒjS)N)rr r Znetloc)rrrrÚget_host-szMockRequest.get_hostcCs|jƒS)N)r)rrrrÚget_origin_req_host0szMockRequest.get_origin_req_hostcCsT|jjjdƒs|jjSt|jjddd�}t|jjƒ}t|j||j|j |j |j gƒS)NZHostzutf-8)Úencoding) r ÚheadersÚgetr rrrr ÚpathZparamsZqueryZfragment)rÚhostZparsedrrrÚ get_full_url3s zMockRequest.get_full_urlcCsdS)NTr)rrrrÚis_unverifiableAszMockRequest.is_unverifiablecCs||jjkp||jkS)N)r rr )rÚnamerrrÚ has_headerDszMockRequest.has_headerNcCs|jjj||jj||ƒƒS)N)r rrr )rrÚdefaultrrrÚ get_headerGszMockRequest.get_headercCs tdƒ‚dS)zMcookielib has no legitimate use for this method; add it back if you find one.z=Cookie headers should be added with add_unredirected_header()N)ÚNotImplementedError)rÚkeyÚvalrrrÚ add_headerJszMockRequest.add_headercCs||j|<dS)N)r )rrÚvaluerrrÚadd_unredirected_headerNsz#MockRequest.add_unredirected_headercCs|jS)N)r )rrrrÚget_new_headersQszMockRequest.get_new_headerscCs|jƒS)N)r)rrrrÚ unverifiableTszMockRequest.unverifiablecCs|jƒS)N)r)rrrrÚorigin_req_hostXszMockRequest.origin_req_hostcCs|jƒS)N)r)rrrrr\szMockRequest.host)N)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrrrrrrr!r%r'r(Úpropertyr)r*rrrrrr s    r c@s(eZdZdZdd„Zdd„Zdd„ZdS) Ú MockResponsezÀWraps a `httplib.HTTPMessage` to mimic a `urllib.addinfourl`. ...what? Basically, expose the parsed HTTP headers from the server response the way `cookielib` expects to see them. cCs ||_dS)z†Make a MockResponse for `cookielib` to read. :param headers: a httplib.HTTPMessage or analogous carrying the headers N)Ú_headers)rrrrrrhszMockResponse.__init__cCs|jS)N)r1)rrrrÚinfooszMockResponse.infocCs|jj|ƒdS)N)r1Ú getheaders)rrrrrr3rszMockResponse.getheadersN)r+r,r-r.rr2r3rrrrr0asr0cCs8t|dƒo|jsdSt|ƒ}t|jjƒ}|j||ƒdS)zìExtract the cookies from the response into a CookieJar. :param jar: cookielib.CookieJar (not necessarily a RequestsCookieJar) :param request: our own requests.Request object :param response: urllib3.HTTPResponse object Ú_original_responseN)Úhasattrr4r r0ÚmsgZextract_cookies)ÚjarrZresponseZreqÚresrrrÚextract_cookies_to_jarvs   r9cCs t|ƒ}|j|ƒ|jƒjdƒS)zj Produce an appropriate Cookie header string to be sent with `request`, or None. :rtype: str ÚCookie)r Zadd_cookie_headerr(r)r7rÚrrrrÚget_cookie_header‡s r<cCs‚g}xV|D]N}|j|krq |dk r.||jkr.q |dk rB||jkrBq |j|j|j|jfƒq Wx |D]\}}}|j|||ƒqbWdS)zkUnsets a cookie by name, by default over all domains and paths. Wraps CookieJar.clear(), is O(n). N)rÚdomainrÚappendÚclear)Ú cookiejarrr=rZ clearablesÚcookierrrÚremove_cookie_by_name’s  rBc@seZdZdZdS)ÚCookieConflictErrorz£There 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.rrrrrC¥srCcsèeZdZdZd3dd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z dd„Zd4dd„Z‡fdd„Zdd„Zdd „Zd!d"„Z‡fd#d$„Z‡fd%d&„Zd5d'd(„Zd6d)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Z‡ZS)7ÚRequestsCookieJara±Compatibility class; is a cookielib.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). Nc Cs(y|j|||ƒStk r"|SXdS)zãDict-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). N)Ú_find_no_duplicatesÚKeyError)rrr r=rrrrr½szRequestsCookieJar.getcKsX|dkr(t|||jdƒ|jdƒd�dSt|tƒr)rÚdomainsrArrrÚ list_domainss  zRequestsCookieJar.list_domainscCs0g}x&t|ƒD]}|j|kr|j|jƒqW|S)z0Utility method to list all the paths in the jar.)rNrr>)rÚpathsrArrrÚ list_pathss  zRequestsCookieJar.list_pathscCs>g}x4t|ƒD](}|jdk r*|j|kr*dS|j|jƒqWdS)zvReturns True if there are multiple domains in the jar. Returns False otherwise. :rtype: bool NTF)rNr=r>)rrVrArrrÚmultiple_domainss z"RequestsCookieJar.multiple_domainscCsJi}x@t|ƒD]4}|dks$|j|kr|dks6|j|kr|j||j<qW|S)z¾Takes 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 N)rNr=rr&r)rr=rZ dictionaryrArrrÚget_dict+s zRequestsCookieJar.get_dictc s*ytt|ƒj|ƒStk r$dSXdS)NT)ÚsuperrDÚ __contains__rC)rr)Ú __class__rrr];szRequestsCookieJar.__contains__cCs |j|ƒS)zÿDict-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). )rE)rrrrrÚ __getitem__AszRequestsCookieJar.__getitem__cCs|j||ƒdS)zÓDict-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)rM)rrr&rrrÚ __setitem__JszRequestsCookieJar.__setitem__cCst||ƒdS)zlDeletes a cookie given a name. Wraps ``cookielib.CookieJar``'s ``remove_cookie_by_name()``. N)rB)rrrrrÚ __delitem__QszRequestsCookieJar.__delitem__csLt|jdƒr4|jjdƒr4|jjdƒr4|jjddƒ|_tt|ƒj|f|ž|ŽS)NÚ startswithú"z\"Ú)r5r&rbÚendswithÚreplacer\rDrJ)rrAÚargsrK)r^rrrJWs$zRequestsCookieJar.set_cookiecs@t|tjƒr,x.|D]}|jtj|ƒƒqWntt|ƒj|ƒdS)zAUpdates this jar with cookies from another CookieJar or dict-likeN)rGrÚ CookieJarrJÚcopyr\rDÚupdate)rÚotherrA)r^rrrj\s  zRequestsCookieJar.updatecCs\xDt|ƒD]8}|j|kr |dks*|j|kr |dks<|j|kr |jSq Wtd|||fƒ‚dS)aÔRequests 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 Nzname=%r, domain=%r, path=%r)rNrr=rr&rF)rrr=rrArrrÚ_findds   zRequestsCookieJar._findcCs|d}xXt|ƒD]L}|j|kr|dks.|j|kr|dks@|j|kr|dk rTtd|ƒ‚|j}qW|rf|Std|||fƒ‚dS)aùBoth ``__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, %rzname=%r, domain=%r, path=%r)rNrr=rrCr&rF)rrr=rZtoReturnrArrrrExs    z%RequestsCookieJar._find_no_duplicatescCs|jjƒ}|jdƒ|S)z4Unlike a normal CookieJar, this class is pickleable.Ú _cookies_lock)Ú__dict__riÚpop)rÚstaterrrÚ __getstate__‘s  zRequestsCookieJar.__getstate__cCs$|jj|ƒd|jkr tjƒ|_dS)z4Unlike a normal CookieJar, this class is pickleable.rmN)rnrjÚ threadingÚRLockrm)rrprrrÚ __setstate__˜s  zRequestsCookieJar.__setstate__cCs"tƒ}|j|jƒƒ|j|ƒ|S)z(Return a copy of this RequestsCookieJar.)rDZ set_policyÚ get_policyrj)rZnew_cjrrrrižs zRequestsCookieJar.copycCs|jS)z&Return the CookiePolicy instance used.)Z_policy)rrrrru¥szRequestsCookieJar.get_policy)NNN)NN)NN)NN)r+r,r-r.rrMrOrQrRrSrTrUrWrYrZr[r]r_r`rarJrjrlrErqrtriruÚ __classcell__rr)r^rrD«s2            rDcCsR|dkr dSt|dƒr|jƒStj|ƒ}|jƒx|D]}|jtj|ƒƒq6W|S)Nri)r5rir?rJ)r7Znew_jarrArrrÚ_copy_cookie_jarªs   rwcKs¤d||ddddddddddiddœ }t|ƒt|ƒ}|rLd }t|t|ƒƒ‚|j|ƒt|d ƒ|d <t|d ƒ|d <|d jdƒ|d<t|dƒ|d<tjf|ŽS)zÊMake 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"). rNrdú/FTÚHttpOnly) Úversionrr&Úportr=rÚsecureÚexpiresÚdiscardÚcommentÚ comment_urlÚrestÚrfc2109z4create_cookie() got unexpected keyword arguments: %sr{Zport_specifiedr=Zdomain_specifiedÚ.Zdomain_initial_dotrZpath_specified)rMÚ TypeErrorrPrjÚboolrbrr:)rr&rKÚresultZbadargsÚerrrrrrI¹s. rIcCsÄd}|drPyttjƒt|dƒƒ}Wqrtk rLtd|dƒ‚YqrXn"|drrd}tjtj|d|ƒƒ}t|dt|dƒd|d||j |d dd |d idt|d ƒ|j |d p¾dd� S)zBConvert a Morsel object into a Cookie containing the one k/v pair.Nzmax-agezmax-age: %s must be integerr}z%a, %d-%b-%Y %H:%M:%S GMTrFr=rryZhttponlyr|rzr) rr€r~r=r}rrr{r�r‚r|r&rz) ÚintÚtimeÚ ValueErrorr„ÚcalendarZtimegmZstrptimerIr…r#r&)Zmorselr}Z time_templaterrrrHÝs0   rHTcCsV|dkrtƒ}|dk rRdd„|Dƒ}x,|D]$}|s:||kr*|jt|||ƒƒq*W|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 NcSsg|] }|j‘qSr)r)Ú.0rArrrú sz'cookiejar_from_dict..)rDrJrI)Z cookie_dictr@Ú overwriteZnames_from_jarrrrrÚcookiejar_from_dictüs   r�c Cszt|tjƒstdƒ‚t|tƒr.t||dd�}nHt|tjƒrvy|j|ƒWn,tk rtx|D]}|j|ƒq^WYnX|S)zÒAdd 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)r@rŽ) rGrrhrŠÚdictr�rjÚAttributeErrorrJ)r@ZcookiesZ cookie_in_jarrrrÚ merge_cookiess    r’)NN)NT)r.rir‰r‹Z_internal_utilsrÚcompatrrrrrrrÚ ImportErrorZdummy_threadingÚobjectr r0r9r<rBÚ RuntimeErrorrCrhrDrwrIrHr�r’rrrrÚ s.  H $