o
    m=8jd                     @   s`  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlmZ d dlmZmZmZ d dlmZmZmZ d dlmZ d dlmZ d dlmZ dd	lmZ dd
lm Z m!Z! ddl"m#Z# ddl$m%Z%m&Z&m'Z' dZ(ee)df Z*dj+Z,dj+Z-dZ.e Z/e0dZ1G dd de	j2Z3G dd de Z4G dd de Z5dS )    N)defaultdict)IterableIteratorMapping)
BaseCookieMorselSimpleCookieMappingProxyType)Union)URL   ) preserve_morsel_with_coded_value)AbstractCookieJarClearCookiePredicate)is_ip_address)LooseCookiesPathLikeStrOrURL)	CookieJarDummyCookieJarzMorsel[str]z{}/{}z{1}.{0}d   )max-ageexpiresc                       sP   e Zd ZU dZeh dZeeeef  ed< dedede	f fddZ
  ZS )	_RestrictedCookieUnpicklera@  A restricted unpickler that only allows cookie-related types.

    This prevents arbitrary code execution when loading pickled cookie data
    from untrusted sources. Only types that are expected in a serialized
    CookieJar are permitted.

    Subclasses :class:`pickle._Unpickler` (the pure-Python implementation)
    rather than :class:`pickle.Unpickler` because the accelerated unpickler
    on some implementations (notably PyPy) does not dispatch through
    :meth:`find_class` overrides.

    See: https://docs.python.org/3/library/pickle.html#restricting-globals
    >   builtinssetr   dictr   tupler   	frozensetcollectionsr   http.cookiesr   r'   r   _ALLOWED_CLASSESmodulenamereturnc                    s4   ||f| j vrtd| d| dt ||S )NzForbidden class: .z. CookieJar.load() only allows cookie-related types for security. See https://docs.python.org/3/library/pickle.html#restricting-globals)r)   pickleUnpicklingErrorsuper
find_class)selfr*   r+   	__class__ X/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/aiohttp/cookiejar.pyr1   L   s
   z%_RestrictedCookieUnpickler.find_class)__name__
__module____qualname____doc__r#   r)   r!   str__annotations__typer1   __classcell__r5   r5   r3   r6   r   .   s   
 "r   c                       s  e Zd ZdZedZedZedZedej	Z
edZeejjjejjd d Zz
eee W n eyI   ed	ZY n eyS   d
ZY nw ed Zddddddededeee B dB dejdB ddf
 fddZ e!defddZ"e!defddZ#e!de$e%e&e&f e'f fddZ(e!de)e%e&e&f  fddZ*de+ddfdd Z,de+ddfd!d"Z-d#e.e&e.e&e.e&e&eB e/B f f f ddfd$d%Z0dLd&e1dB ddfd'd(Z2d)e&ddfd*d+Z3dMd-d.Z4defd/d0Z5dNd1d2Z6d3ee%e&e&e&f  ddfd4d5Z7d6e/d)e&d7e&d8e&ddf
d9d:Z8e9 fd;e:d<e9ddfd=d>Z;e9 fd?e9dd@fdAdBZ<dCe=e& de=e& fdDdEZ>e?d)e&dFe&defdGdHZ@eAdIe&dedB fdJdKZB  ZCS )Or   z/Implements cookie storage adhering to RFC 6265.z][\x09\x20-\x2F\x3B-\x40\x5B-\x60\x7B-\x7E]*(?P<token>[\x00-\x08\x0A-\x1F\d:a-zA-Z\x7F-\xFF]+)z(\d{1,2}):(\d{1,2}):(\d{1,2})z	(\d{1,2})zG(jan)|(feb)|(mar)|(apr)|(may)|(jun)|(jul)|(aug)|(sep)|(oct)|(nov)|(dec)z	(\d{2,4}))tzinfor   )	i           ;   rC   rD   rD   iFTN)unsafequote_cookietreat_as_secure_originlooprE   rF   rG   rH   r,   c                   s   t  j|d tt| _tt| _t | _|| _	|| _
|d u r"g }nt|tr-| g}nt|tr:t| g}ndd |D }|| _g | _i | _d S )NrH   c                 S   s*   g | ]}t |trt| n| qS r5   )
isinstancer;   r   origin).0urlr5   r5   r6   
<listcomp>   s    z&CookieJar.__init__.<locals>.<listcomp>)r0   __init__r   r   _cookiesr   _morsel_cacher   _host_only_cookies_unsafe_quote_cookierJ   r   rK   r;   _treat_as_secure_origin_expire_heap_expirations)r2   rE   rF   rG   rH   r3   r5   r6   rO   z   s*   


zCookieJar.__init__c                 C      | j S N)rS   r2   r5   r5   r6   rE         zCookieJar.unsafec                 C   rX   rY   )rT   rZ   r5   r5   r6   rF      r[   zCookieJar.quote_cookiec                 C   
   t | jS )z&Return the cookies stored in this jar.)r
   rP   rZ   r5   r5   r6   cookies      
zCookieJar.cookiesc                 C   r\   )z0Return the host-only cookies stored in this jar.)r#   rR   rZ   r5   r5   r6   host_only_cookies   r^   zCookieJar.host_only_cookies	file_pathc                 C   s  t |}i }| j D ]Z\\}}}| d| }i ||< | D ]D\}}|j|j|jd}	|jD ]}
|
tv r8q1||
 }|rB||	|
< q1||f| j	v rNd|	d< | j
|||f }dur_||	d< |	|| |< q!qt|ddd	d
 d}tj||dd W d   dS 1 sw   Y  dS )zSave cookies to a file using JSON format.

        :param file_path: Path to file where cookies will be serialized,
            :class:`str` or :class:`pathlib.Path` instance.
        |keyvaluecoded_valueT	host_onlyNexpires_timestampwutf-8c                 S   s   t | |dS )Ni  )osopen)pathflagsr5   r5   r6   <lambda>   s    z CookieJar.save.<locals>.<lambda>)modeencodingopener   )indent)pathlibPathrP   itemsrc   rd   re   	_reserved_RELATIVE_EXPIRY_ATTRSrR   rW   getrk   jsondump)r2   r`   datadomainrl   cookierc   r+   morselmorsel_dataattrattr_valexpfr5   r5   r6   save   s@   

"zCookieJar.savec                 C   s   t |}z$|jddd}t|}W d   n1 sw   Y  | | W dS  tjttfyV   |jdd}t	| | _
W d   Y dS 1 sNw   Y  Y dS w )a  Load cookies from a file.

        Tries to load JSON format first. Falls back to loading legacy
        pickle format (using a restricted unpickler) for backward
        compatibility with existing cookie files.

        Replaces the current jar contents; loaded cookies pass through the
        same acceptance rules as :meth:`update_cookies`.

        :param file_path: Path to file from where cookies will be
            imported, :class:`str` or :class:`pathlib.Path` instance.
        rri   )ro   rp   Nrb)ro   )rt   ru   rk   rz   load_load_json_dataJSONDecodeErrorUnicodeDecodeError
ValueErrorr   rP   )r2   r`   r   r|   r5   r5   r6   r      s   
&zCookieJar.loadr|   c                 C   s   |    | D ]k\}}|dd\}}| D ]Z\}}t }||d |d |d d |jD ]}	|	|v rA|	dvrA||	 ||	< q1|drKd|d	< |rTtjd
|dnt }
| 	||i|
 |d }durr| 
t|||| qq|   dS )z;Replace contents, routing cookies through update_cookies().ra   r   rc   rd   re   rb   rf    r}   https)schemehostrg   N)clearrv   splitr   __setstate__rw   ry   r   buildupdate_cookies_expire_cookiefloat_do_expiration)r2   r|   compound_keycookie_datar}   rl   r+   r   r   r   response_urlr   r5   r5   r6   r      s2   

zCookieJar._load_json_data	predicatec                    sz   d u rj   j  j  j  j  d S t  fddj D }|r;| d S d S )Nc                    sT   g | ]&\\}}}|  D ]\}}|||f  jv r!j  ks%|r qqS r5   )rv   rW   )rL   r}   rl   r~   r+   r   rc   nowr   r2   r5   r6   rN     s    
z#CookieJar.clear.<locals>.<listcomp>)	rV   r   rP   rQ   rR   rW   timerv   _delete_cookies)r2   r   to_delr5   r   r6   r     s   





zCookieJar.clearr}   c                    s     fdd d S )Nc                    s     | d S )Nr}   )_is_domain_match)xr}   r2   r5   r6   rn   -  s    z(CookieJar.clear_domain.<locals>.<lambda>)r   r2   r}   r5   r   r6   clear_domain,  s   zCookieJar.clear_domainIterator[Morsel[str]]c                 c   s,    |    | j D ]	}| E d H  q
d S rY   )r   rP   values)r2   valr5   r5   r6   __iter__/  s
   zCookieJar.__iter__c                 C   s   t dd | j D S )zReturn number of cookies.

        This function does not iterate self to avoid unnecessary expiration
        checks.
        c                 s   s    | ]	}t | V  qd S rY   )lenr   )rL   r~   r5   r5   r6   	<genexpr>:  s    z$CookieJar.__len__.<locals>.<genexpr>)sumrP   r   rZ   r5   r5   r6   __len__4  s   zCookieJar.__len__c                    s   t  j }s	dS |tkr'|t  jd kr' fdd jD  _t j t }g } jrR jd \}}||kr<nt j  j||krO|	|  js0|r[ 
| dS dS )zRemove expired cookies.Nrr   c                    s(   g | ]} j |d  |d kr|qS )r   r   )rW   ry   )rL   entryrZ   r5   r6   rN   N  s
    z,CookieJar._do_expiration.<locals>.<listcomp>r   )r   rV    _MIN_SCHEDULED_COOKIE_EXPIRATIONrW   heapqheapifyr   heappopry   appendr   )r2   expire_heap_lenr   r   when
cookie_keyr5   rZ   r6   r   <  s*   

zCookieJar._do_expirationr   c                 C   sd   |D ]-\}}}| j ||f | j||f |d  | j||f |d  | j|||fd  qd S rY   )rR   discardrP   poprQ   rW   )r2   r   r}   rl   r+   r5   r5   r6   r   g  s   zCookieJar._delete_cookiesr   rl   r+   c                 C   s>   |||f}| j ||krd S t| j||f || j |< d S rY   )rW   ry   r   heappushrV   )r2   r   r}   rl   r+   r   r5   r5   r6   r   n  s
   
zCookieJar._expire_cookier]   r   c              	   C   s  |j }| jst|rdS t|tr| }|D ]\}}t|ts+t }|||< || }|d }|r<|d dkr<d}|d= |sP|durP| j	||f | }|d< |rb|d dkrb|dd }||d< |rk| 
||skq|d }|rw|d d	kr|j}|d	sd	}nd	|d|d	  }||d< |d	}|d
  }	rzt|	}
tt |
 | j}| |||| W n' ty   d|d
< Y nw |d  }r| | }r| |||| nd|d< ||f}| j| ||kr|| j| |< | j| |d q|   dS )zUpdate cookies.Nr}   rD   r-   r   r   r   rl   /r   r   )raw_hostrS   r   rJ   r   rv   r   r   rR   addr   rl   
startswithrfindrstripintminr   MAX_TIMEr   r   _parse_daterP   ry   rQ   r   r   )r2   r]   r   hostnamer+   r~   tmpr}   rl   max_agedelta_secondsmax_age_expirationr   expire_timerc   r5   r5   r6   r   v  s`   



zCookieJar.update_cookiesrequest_urlBaseCookie[str]c                 C   s"  t  }| js|S |   | js|S t|tur(tjdt| tdd t|}|jp,d}|j	dv}|rX| j
rXt }tt | }W d   n1 sNw   Y  || j
v}d}| j|  D ](}|j| j| v rw| j| |j ||j< qa| |}|| j| |j< |||j< qat|r| js|S |f}	ntt|dt}	t|jd	t}
t|	|
}t|j}|D ]U}|| jvrq| j|  D ]E\}}|d
 }||f| jv r||krqt|d |krq|r|d rq|| j| v r| j| | ||< q| |}|| j| |< |||< qq|S )z8Returns this jar's cookies filtered by their attributes.zQfilter_cookies expects yarl.URL instances only,and will stop working in 4.x, got rr   )
stacklevelr   )r   wssN)r   r   r-   r   r}   rl   secure) r   rP   r   r=   r   warningswarnDeprecationWarningr   r   rU   
contextlibsuppressr   rK   r   rc   rQ   _build_morselr   rS   	itertools
accumulatereversedr   _FORMAT_DOMAIN_REVERSEDrl   _FORMAT_PATHproductr   rv   rR   )r2   r   filteredr   is_not_securerequest_originrc   cmrsl_valdomainspathspairspath_lenpr+   r~   r}   r5   r5   r6   filter_cookies  sx   









zCookieJar.filter_cookiesr~   c                 C   sb   | j r|jr|jd dkrt|S t }| j r t|j\}}n|j }}||j||d |S )z<Build a morsel for sending, respecting quote_cookie setting.r   "rb   )	rT   re   r   r   _SIMPLE_COOKIEvalue_encoderd   r   rc   )r2   r~   r   rd   re   r5   r5   r6   r     s   
zCookieJar._build_morselr   c                 C   sD   || krdS | | sdS |dt|   }| dsdS t| S )z0Implements domain matching adhering to RFC 6265.TFNr-   )endswithr   r   )r}   r   non_matchingr5   r5   r6   r   (  s   


zCookieJar._is_domain_matchdate_strc                 C   s  |sdS d}d}d}d}d } }}d}	d}
d}| j |D ]\}|d}|s>| j|}|r>d}dd | D \}}}q|sQ| j|}|rQd}t| }	q|sh| j|}|rhd}|j	dusdJ |j	}
q|sz| j
|}|rzd}t| }qd|  krd	krn n|d
7 }nd|  krdkrn n|d7 }d||||fv rdS d|	  krdksdS  dS |dk s|dks|dks|dkrdS t||
|	|||dddf	S )z4Implements date string parsing adhering to RFC 6265.NFr   tokenTc                 s   s    | ]}t |V  qd S rY   )r   )rL   sr5   r5   r6   r   P  s    z(CookieJar._parse_date.<locals>.<genexpr>F   c   il  E   i  r   rA   iA  rB   rC   rD   )DATE_TOKENS_REfinditergroupDATE_HMS_TIME_REmatchgroupsDATE_DAY_OF_MONTH_REr   DATE_MONTH_RE	lastindexDATE_YEAR_REcalendartimegm)clsr   
found_time	found_dayfound_month
found_yearhourminuteseconddaymonthyeartoken_matchr   
time_match	day_matchmonth_match
year_matchr5   r5   r6   r   8  sd   

 zCookieJar._parse_daterY   r,   r   )r,   N)Dr7   r8   r9   r:   recompiler   r   r   Ir   r   r   datetimemaxreplacetimezoneutc	timestampr   r   r   r   gmtimeOSErrorOverflowErrorSUB_MAX_TIMEboolr   listasyncioAbstractEventLooprO   propertyrE   rF   r
   r!   r;   r   r]   r#   r_   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   staticmethodr   classmethodr   r>   r5   r5   r3   r6   r   V   s    


! *"
&

 +JX r   c                       s
  e Zd ZdZdddejdB ddf fddZd#d	d
ZdefddZ	e
defddZe
defddZe
deeeef ef fddZe
deeeef  fddZd$dedB ddfddZdeddfddZe fdededdfddZdedd fd!d"Z  ZS )%r   zxImplements a dummy cookie storage.

    It can be used with the ClientSession when no cookie processing is needed.

    NrI   rH   r,   c                   s   t  j|d d S )NrI   )r0   rO   )r2   rH   r3   r5   r6   rO     s   zDummyCookieJar.__init__r   c                 c   s    d S rY   r5   rZ   r5   r5   r6   r     s   zDummyCookieJar.__iter__c                 C      dS )Nr   r5   rZ   r5   r5   r6   r        zDummyCookieJar.__len__c                 C   r#  )NFr5   rZ   r5   r5   r6   rE        zDummyCookieJar.unsafec                 C   r#  )NTr5   rZ   r5   r5   r6   rF     r%  zDummyCookieJar.quote_cookiec                 C   s   t i S )zReturn an empty mapping.r	   rZ   r5   r5   r6   r]     s   zDummyCookieJar.cookiesc                 C      t  S )zReturn an empty frozenset.)r#   rZ   r5   r5   r6   r_     s   z DummyCookieJar.host_only_cookiesr   c                 C      d S rY   r5   )r2   r   r5   r5   r6   r     r$  zDummyCookieJar.clearr}   c                 C   r'  rY   r5   r   r5   r5   r6   r     r$  zDummyCookieJar.clear_domainr]   r   c                 C   r'  rY   r5   )r2   r]   r   r5   r5   r6   r     r$  zDummyCookieJar.update_cookiesr   r   c                 C   r&  rY   )r   )r2   r   r5   r5   r6   r     s   zDummyCookieJar.filter_cookiesr  rY   )r7   r8   r9   r:   r  r  rO   r   r   r   r   r  rE   rF   r
   r!   r;   r   r]   r#   r_   r   r   r   r   r   r   r   r>   r5   r5   r3   r6   r   y  s"    "
 r   )6r  r   r   r  r   r   rz   rj   rt   r.   r  r   r   r%   r   collections.abcr   r   r   http.cookiesr   r   r   typesr
   typingr   yarlr   _cookie_helpersr   abcr   r   helpersr   typedefsr   r   r   __all__r;   
CookieItemformatr   r   r   r   r#   rx   
_Unpicklerr   r   r   r5   r5   r5   r6   <module>   sJ    (    '