U ��.e�G�@s�dZddlZddlZddlZddlmZddlmZmZm Z m Z m Z z 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.8/site-packages/pip/_vendor/requests/cookies.py�__init__%szMockRequest.__init__cCs|jSr )r�rrrr�get_type*szMockRequest.get_typecCst|jj�jSr )rr r Znetlocrrrr�get_host-szMockRequest.get_hostcCs|��Sr �rrrrr�get_origin_req_host0szMockRequest.get_origin_req_hostcCsT|jj�d�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�NTrrrrr�is_unverifiableAszMockRequest.is_unverifiablecCs||jjkp||jkSr )r rr �r�namerrr� has_headerDszMockRequest.has_headerNcCs|jj�||j�||��Sr )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|<dSr �r �rr#�valuerrr�add_unredirected_headerNsz#MockRequest.add_unredirected_headercCs|jSr r+rrrr�get_new_headersQszMockRequest.get_new_headerscCs|��Sr )r!rrrr� unverifiableTszMockRequest.unverifiablecCs|��Sr )rrrrr�origin_req_hostXszMockRequest.origin_req_hostcCs|��Sr rrrrrr\szMockRequest.host)N)�__name__� __module__� __qualname__�__doc__rrrrrr!r$r&r*r.r/�propertyr0r1rrrrrr 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|jSr r8rrrr�infooszMockResponse.infocCs|j�|�dSr )r9� getheadersr"rrrr;rszMockResponse.getheadersN)r2r3r4r5rr:r;rrrrr7asr7cCs8t|d�r|jsdSt|�}t|jj�}|�||�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)�hasattrr<r r7�msgZextract_cookies)�jarrZresponseZreq�resrrr�extract_cookies_to_jarvs � rAcCs t|�}|�|�|���d�S)zj Produce an appropriate Cookie header string to be sent with `request`, or None. :rtype: str �Cookie)r Zadd_cookie_headerr/r)r?r�rrrr�get_cookie_header�s rDcCszg}|D]N}|j|krq|dk r,||jkr,q|dk r@||jkr@q|�|j|j|jf�q|D]\}}}|�|||�q\dS)zkUnsets a cookie by name, by default over all domains and paths. Wraps CookieJar.clear(), is O(n). N)r#�domainr�append�clear)� cookiejarr#rErZ clearables�cookierrr�remove_cookie_by_name�s rJc@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)r2r3r4r5rrrrrK�srKcs�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). NcCs.z|�|||�WStk r(|YSXdS)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)rr#r%rErrrrr�szRequestsCookieJar.getcKsX|dkr(t|||�d�|�d�d�dSt|t�r|j|kr |dk rRtd|��|j}q |rb|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, %rrw)rVr#rErrKr-rN)rr#rErZtoReturnrIrrrrMxs    z%RequestsCookieJar._find_no_duplicatescCs|j��}|�d�|S)�4Unlike a normal CookieJar, this class is pickleable.� _cookies_lock)�__dict__rt�pop�r�staterrr� __getstate__�s  zRequestsCookieJar.__getstate__cCs$|j�|�d|jkr t��|_dS)ryrzN)r{ru� threading�RLockrzr}rrr� __setstate__�s  zRequestsCookieJar.__setstate__cCs"t�}|�|���|�|�|S)z(Return a copy of this RequestsCookieJar.)rLZ set_policy� get_policyru)rZnew_cjrrrrt�s zRequestsCookieJar.copycCs|jS)z&Return the CookiePolicy instance used.)Z_policyrrrrr��szRequestsCookieJar.get_policy)NNN)NN)NN)NN)r2r3r4r5rrUrXrZr[r\r]r^rarcrdrergrjrkrlrRrurxrMrr�rtr�� __classcell__rrrhrrL�s2            rLcCsN|dkr dSt|d�r|��St�|�}|��|D]}|�t�|��q4|S)Nrt)r=rtrGrR)r?Znew_jarrIrrr�_copy_cookie_jar�s  r�cKs�d||ddddddddddidd� }t|�t|�}|rLd }t|t|���|�|�t|d �|d <t|d �|d <|d �d�|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"). rNro�/FT�HttpOnly) �versionr#r-�portrEr�secure�expires�discard�comment� comment_url�rest�rfc2109z4create_cookie() got unexpected keyword arguments: %sr�Zport_specifiedrEZdomain_specified�.Zdomain_initial_dotrZpath_specified)rU� TypeErrorrYru�boolrmrrB)r#r-rS�resultZbadargs�errrrrrQ�s0� rQcCs�d}|drPztt��t|d��}Wqrtk rLtd|d��YqrXn"|drrd}t�t�|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 GMTr�FrErr�Zhttponlyr�r�r) r�r�r�rEr�r#rr�r�r�r�r-r�) �int�time� ValueErrorr��calendarZtimegm�strptimerQr�r(r-)Zmorselr�Z time_templaterrrrP�s4�    �rPTcCsR|dkrt�}|dk rNdd�|D�}|D]$}|s8||kr(|�t|||��q(|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#)�.0rIrrr� sz'cookiejar_from_dict..)rLrRrQ)Z cookie_dictrH� overwriteZnames_from_jarr#rrr�cookiejar_from_dict�s  r�cCsvt|tj�std��t|t�r.t||dd�}nDt|tj�rrz|�|�Wn(tk rp|D]}|�|�q\YnX|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)rHr�) rOrrsr��dictr�ru�AttributeErrorrR)rHZcookiesZ cookie_in_jarrrr� merge_cookiess  � r�)NN)NT)r5rtr�r�Z_internal_utilsr�compatrrrrrr�� ImportErrorZdummy_threading�objectr r7rArDrJ� RuntimeErrorrKrsrLr�rQrPr�r�rrrr�s.   H $