o
    FhI                     @   sJ  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 d dlmZmZmZ d dlmZmZmZmZmZmZmZmZ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.Z0dZ1e Z2G dd de#Z3G dd de#Z4dS )    N)defaultdict)Mapping)
BaseCookieMorselSimpleCookie)	DefaultDictDictIterableIteratorListOptionalSetTupleUnion)URL   ) preserve_morsel_with_coded_value)AbstractCookieJarClearCookiePredicate)is_ip_address)LooseCookiesPathLikeStrOrURL)	CookieJarDummyCookieJarzMorsel[str]z{}/{}z{1}.{0}d   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efyK   ed	ZY n eyU   d
ZY nw ed Zddddddededeeee df de e!j" ddf
 fddZ#e$defddZ%de&ddfddZ'de&ddfddZ(dCde e) ddfddZ*d e+ddfd!d"Z,dDd$d%Z-defd&d'Z.dEd(d)Z/d*ee0e+e+e+f  ddfd+d,Z1d-e2d e+d.e+d/e+ddf
d0d1Z3e4 fd2e5d3e4ddfd4d5Z6e4 fd6e4dd7fd8d9Z7d:e8e+ de8e+ fd;d<Z9e:d e+d=e+defd>d?Z;e<d@e+de e fdAdBZ=  Z>S )Fr   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           ;   r    r!   r!   iFTN)unsafequote_cookietreat_as_secure_originloopr"   r#   r$   r%   returnc                   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r%   c                 S   s*   g | ]}t |trt| n| qS  )
isinstancestrr   origin).0urlr(   r(   D/var/www/html/venv/lib/python3.10/site-packages/aiohttp/cookiejar.py
<listcomp>q   s    z&CookieJar.__init__.<locals>.<listcomp>)super__init__r   r   _cookiesdict_morsel_cacheset_host_only_cookies_unsafe_quote_cookier)   r   r+   r*   _treat_as_secure_origin_expire_heap_expirations)selfr"   r#   r$   r%   	__class__r(   r.   r1   X   s*   


zCookieJar.__init__c                 C   s   | j S N)r8   r<   r(   r(   r.   r#   y   s   zCookieJar.quote_cookie	file_pathc                 C   sN   t |}|jdd}t| j|tj W d    d S 1 s w   Y  d S )Nwbmode)pathlibPathopenpickledumpr2   HIGHEST_PROTOCOLr<   rA   fr(   r(   r.   save}   s   
"zCookieJar.savec                 C   sH   t |}|jdd}t|| _W d    d S 1 sw   Y  d S )NrbrC   )rE   rF   rG   rH   loadr2   rK   r(   r(   r.   rO      s   
"zCookieJar.load	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 r(   )itemsr;   )r,   domainpathcookienamemorselkeynowrP   r<   r(   r.   r/      s    
z#CookieJar.clear.<locals>.<listcomp>)	r:   clearr2   r4   r6   r;   timerQ   _delete_cookies)r<   rP   to_delr(   rW   r.   rZ      s   





zCookieJar.clearrR   c                    s     fdd d S )Nc                    s     | d S )NrR   )_is_domain_match)xrR   r<   r(   r.   <lambda>   s    z(CookieJar.clear_domain.<locals>.<lambda>)rZ   r<   rR   r(   r`   r.   clear_domain   s   zCookieJar.clear_domainIterator[Morsel[str]]c                 c   s,    |    | j D ]	}| E d H  q
d S r?   )_do_expirationr2   values)r<   valr(   r(   r.   __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 r?   )lenrf   )r,   rT   r(   r(   r.   	<genexpr>   s    z$CookieJar.__len__.<locals>.<genexpr>)sumr2   rf   r@   r(   r(   r.   __len__   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.N   c                    s(   g | ]} j |d  |d kr|qS )r   r   )r;   get)r,   entryr@   r(   r.   r/      s
    z,CookieJar._do_expiration.<locals>.<listcomp>r   )ri   r:    _MIN_SCHEDULED_COOKIE_EXPIRATIONr;   heapqheapifyr[   heappoprn   appendr\   )r<   expire_heap_lenrY   r]   when
cookie_keyr(   r@   r.   re      s*   

zCookieJar._do_expirationr]   c                 C   sd   |D ]-\}}}| j ||f | j||f |d  | j||f |d  | j|||fd  qd S r?   )r6   discardr2   popr4   r;   )r<   r]   rR   rS   rU   r(   r(   r.   r\      s   zCookieJar._delete_cookiesrv   rS   rU   c                 C   s>   |||f}| j ||krd S t| j||f || j |< d S r?   )r;   rn   rq   heappushr:   )r<   rv   rR   rS   rU   rw   r(   r(   r.   _expire_cookie   s
   
zCookieJar._expire_cookiecookiesresponse_urlc              	   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.NrR   r!   . r   r   rS   /zmax-ageexpires)raw_hostr7   r   r)   r   rQ   r   r   r6   addr^   rS   
startswithrfindrstripintminr[   MAX_TIMEr{   
ValueError_parse_dater2   rn   r4   ry   re   )r<   r|   r}   hostnamerU   rT   tmprR   rS   max_agedelta_secondsmax_age_expirationr   expire_timerX   r(   r(   r.   update_cookies   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 rm   )
stacklevelr   )httpswssN)r   r   r~   r   rR   rS   secure) r   r2   re   typer   warningswarnDeprecationWarningr   schemer9   
contextlibsuppressr   r+   rf   rX   r4   _build_morselr   r7   	itertools
accumulatereversedsplit_FORMAT_DOMAIN_REVERSEDrS   _FORMAT_PATHproductri   rQ   r6   )r<   r   filteredr   is_not_securerequest_originrX   cmrsl_valdomainspathspairspath_lenprU   rT   rR   r(   r(   r.   filter_cookies2  sx   









zCookieJar.filter_cookiesrT   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   ")rX   valuecoded_value)	r8   r   r   r   _SIMPLE_COOKIEvalue_encoder   __setstate__rX   )r<   rT   rV   r   r   r(   r(   r.   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~   )endswithri   r   )rR   r   non_matchingr(   r(   r.   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 r?   )r   )r,   sr(   r(   r.   rj     s    z(CookieJar._parse_date.<locals>.<genexpr>F   c   il  E   i  r   r   iA  r   r    r!   )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_matchr(   r(   r.   r     sd   

 zCookieJar._parse_dater?   r&   rd   )r&   N)?__name__
__module____qualname____doc__recompiler   r   r   Ir   r   r   datetimemaxreplacetimezoneutc	timestampr   r   r   r[   gmtimeOSErrorr   OverflowErrorSUB_MAX_TIMEboolr   r   r   r   asyncioAbstractEventLoopr1   propertyr#   r   rM   rO   r   rZ   r*   rc   rh   rl   re   r   r\   floatr{   r   r   r   r   r   r   staticmethodr^   classmethodr   __classcell__r(   r(   r=   r.   r   3   sn    


!

 +JX r   c                       s   e Zd ZdZdddeej ddf fddZdd	d
Zde	fddZ
edefddZddee 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.

    Nr'   r%   r&   c                   s   t  j|d d S )Nr'   )r0   r1   )r<   r%   r=   r(   r.   r1     s   zDummyCookieJar.__init__rd   c                 c   s    d S r?   r(   r@   r(   r(   r.   rh     s   zDummyCookieJar.__iter__c                 C      dS )Nr   r(   r@   r(   r(   r.   rl        zDummyCookieJar.__len__c                 C   r   )NTr(   r@   r(   r(   r.   r#     s   zDummyCookieJar.quote_cookierP   c                 C      d S r?   r(   )r<   rP   r(   r(   r.   rZ      r   zDummyCookieJar.clearrR   c                 C   r   r?   r(   rb   r(   r(   r.   rc     r   zDummyCookieJar.clear_domainr|   r}   c                 C   r   r?   r(   )r<   r|   r}   r(   r(   r.   r     r   zDummyCookieJar.update_cookiesr   r   c                 C   s   t  S r?   )r   )r<   r   r(   r(   r.   r   	  s   zDummyCookieJar.filter_cookiesr   r?   )r   r   r   r   r   r   r   r1   rh   r   rl   r   r   r#   r   rZ   r*   rc   r   r   r   r   r   r(   r(   r=   r.   r     s    "
r   )5r   r   r   r   rq   r   osrE   rH   r   r[   r   collectionsr   collections.abcr   http.cookiesr   r   r   typingr   r   r	   r
   r   r   r   r   r   yarlr   _cookie_helpersr   abcr   r   helpersr   typedefsr   r   r   __all__r*   
CookieItemformatr   r   rp   r   r   r   r(   r(   r(   r.   <module>   s@    ,   ;