a
    8öjêI  ã                   @   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 n4 eefy–   e d	¡ZY n eyª   d
ZY n0 ed Zdddddœeeeeee df e e!j" ddœ‡ fdd„Z#e$edœdd„ƒZ%e&ddœdd„Z'e&ddœdd„Z(d=e e) ddœdd„Z*e+ddœdd„Z,d dœd!d"„Z-edœd#d$„Z.ddœd%d&„Z/ee0e+e+e+f  dd'œd(d)„Z1e2e+e+e+dd*œd+d,„Z3e4ƒ fe5e4dd-œd.d/„Z6e4ƒ fe4d0d1œd2d3„Z7e8e+ e8e+ d4œd5d6„Z9e:e+e+ed7œd8d9„ƒZ;e<e+e e d:œd;d<„ƒZ=‡  Z>S )>r   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Úloop)r"   r#   r$   r%   Úreturnc                   s˜   t ƒ j|d� ttƒ| _ttƒ| _tƒ | _|| _	|| _
|d u rDg }n>t|tƒrZ| ¡ g}n(t|tƒrtt|ƒ ¡ 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(   úP/mnt/bigstor/www/usecases/.venv/lib/python3.9/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__©r&   c                 C   s   | j S ©N)r8   ©r<   r(   r(   r.   r#   y   s    zCookieJar.quote_cookie)Ú	file_pathr&   c                 C   sL   t  |¡}|jdd��"}t | j|tj¡ W d   ƒ n1 s>0    Y  d S )NÚwb©Úmode)ÚpathlibÚPathÚopenÚpickleÚdumpr2   ÚHIGHEST_PROTOCOL©r<   rB   Úfr(   r(   r.   Úsave}   s    
zCookieJar.savec                 C   sF   t  |¡}|jdd��}t |¡| _W d   ƒ n1 s80    Y  d S )NÚrbrD   )rF   rG   rH   rI   Úloadr2   rL   r(   r(   r.   rP   ‚   s    
zCookieJar.load©Ú	predicater&   c                    sv   ˆd u r>ˆj  ¡  ˆj ¡  ˆj ¡  ˆj ¡  ˆj ¡  d S t ¡ ‰‡ ‡‡‡fdd„ˆj ¡ D ƒ}|rrˆ |¡ d S )Nc                    sT   g | ]L\\}}}|  ¡ D ]6\}}|||f ‰ ˆjv rBˆjˆ  ˆksJˆ|ƒrˆ ‘qqS r(   )Úitemsr;   )r,   ÚdomainÚpathÚcookieÚnameÚmorsel©ÚkeyÚnowrR   r<   r(   r.   r/   ‘   s   
ùz#CookieJar.clear.<locals>.<listcomp>)	r:   Úclearr2   r4   r6   r;   ÚtimerS   Ú_delete_cookies)r<   rR   Úto_delr(   rY   r.   r\   ‡   s    




þ
zCookieJar.clear©rT   r&   c                    s   ˆ  ‡ ‡fdd„¡ d S )Nc                    s   ˆ  ˆ | d ¡S )NrT   )Ú_is_domain_match)Úx©rT   r<   r(   r.   Ú<lambda>Ÿ   ó    z(CookieJar.clear_domain.<locals>.<lambda>)r\   ©r<   rT   r(   rc   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@   )Úlenrj   )r,   rV   r(   r(   r.   Ú	<genexpr>¬   re   z$CookieJar.__len__.<locals>.<genexpr>)Úsumr2   rj   rA   r(   r(   r.   Ú__len__¦   s    zCookieJar.__len__c                    s²   t ˆ jƒ }sdS |tkrN|t ˆ jƒd krN‡ fdd„ˆ jD ƒˆ _t ˆ j¡ t ¡ }g }ˆ jr ˆ jd \}}||krxq t ˆ j¡ ˆ j |¡|krZ| 	|¡ qZ|r®ˆ  
|¡ dS )zRemove expired cookies.Né   c                    s(   g | ] }ˆ j  |d  ¡|d kr|‘qS )r   r   )r;   Úget)r,   ÚentryrA   r(   r.   r/   À   s   þz,CookieJar._do_expiration.<locals>.<listcomp>r   )rm   r:   Ú _MIN_SCHEDULED_COOKIE_EXPIRATIONr;   ÚheapqÚheapifyr]   Úheappoprr   Úappendr^   )r<   Zexpire_heap_lenr[   r_   ÚwhenÚ
cookie_keyr(   rA   r.   ri   ®   s*    ÿþ
þzCookieJar._do_expiration)r_   r&   c                 C   sd   |D ]Z\}}}| j  ||f¡ | j||f  |d ¡ | j||f  |d ¡ | j |||fd ¡ qd S r@   )r6   Údiscardr2   Úpopr4   r;   )r<   r_   rT   rU   rW   r(   r(   r.   r^   Ù   s
    zCookieJar._delete_cookies)ry   rT   rU   rW   r&   c                 C   s>   |||f}| j  |¡|krd S t | j||f¡ || j |< d S r@   )r;   rr   ru   Úheappushr:   )r<   ry   rT   rU   rW   rz   r(   r(   r.   Ú_expire_cookieà   s
    
zCookieJar._expire_cookie©ÚcookiesÚresponse_urlr&   c              	   C   s  |j }| jst|ƒrdS t|tƒr*| ¡ }|D �]Ô\}}t|tƒsXtƒ }|||< || }|d }|rz|d dkrzd}|d= |s¢|dur¢| j 	||f¡ | }|d< |rÆ|d dkrÆ|dd… }||d< |rØ|  
||¡sØq.|d }|rò|d d	k�r(|j}| d	¡�s
d	}nd	|d| d	¡…  }||d< | d	¡}|d
  }	�r�z0t|	ƒ}
tt ¡ |
 | jƒ}|  ||||¡ W n t�yŒ   d|d
< Y n0 n8|d  }�rÈ|  |¡ }�rÀ|  ||||¡ nd|d< ||f}| j|  |¡|kr.|| j| |< | j|  |d¡ q.|  ¡  dS )zUpdate cookies.NrT   r!   Ú.Ú r   r   rU   ú/zmax-ageÚexpires)Úraw_hostr7   r   r)   r   rS   r   r   r6   Úaddra   rU   Ú
startswithÚrfindÚrstripÚintÚminr]   ÚMAX_TIMEr~   Ú
ValueErrorÚ_parse_dater2   rr   r4   r|   ri   )r<   r€   r�   ÚhostnamerW   rV   ÚtmprT   rU   Úmax_ageZdelta_secondsZmax_age_expirationr…   Úexpire_timerZ   r(   r(   r.   Úupdate_cookiesè   s\    


zCookieJar.update_cookiesúBaseCookie[str]©Úrequest_urlr&   c                 C   sB  t ƒ }| js|S |  ¡  | js"|S t|ƒturPtjdt|ƒ› �tdd� t|ƒ}|jpXd}|j	dv}|r°| j
r°tƒ }t t¡� | ¡ }W d  ƒ n1 sœ0    Y  || j
v}d}| j|  ¡ D ]P}|j| j| v rî| j| |j ||j< qÂ|  |¡}|| j| |j< |||j< qÂt|ƒ�r2| j�s*|S |f}	nt t| d¡ƒt¡}	t |j d	¡t¡}
t |	|
¡}t|jƒ}|D ]Æ}|| jv�rŠ�qv| j|  ¡ D ] \}}|d
 }||f| jv �rÆ||k�rÆ�q˜t|d ƒ|k�rÜ�q˜|�rð|d �rð�q˜|| j| v �r| j| | ||< �q˜|  |¡}|| j| |< |||< �q˜�qv|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 rq   )Ú
stacklevelrƒ   )ÚhttpsÚwssN)rƒ   rƒ   r‚   r„   rT   rU   Úsecure) r   r2   ri   Útyper   ÚwarningsÚwarnÚDeprecationWarningr†   Úschemer9   Ú
contextlibÚsuppressrŽ   r+   rj   rZ   r4   Ú_build_morselr   r7   Ú	itertoolsÚ
accumulateÚreversedÚsplitÚ_FORMAT_DOMAIN_REVERSEDrU   Ú_FORMAT_PATHÚproductrm   rS   r6   )r<   r—   Úfilteredr�   Zis_not_secureZrequest_originrZ   ÚcZmrsl_valÚdomainsÚpathsÚpairsZpath_lenÚprW   rV   rT   r(   r(   r.   Úfilter_cookies2  st    ÿü


&


ÿ

zCookieJar.filter_cookies)rV   r&   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   ú")rZ   ÚvalueÚcoded_value)	r8   r´   r   r   Ú_SIMPLE_COOKIEÚvalue_encoder³   Ú__setstate__rZ   )r<   rV   rX   r³   r´   r(   r(   r.   r£   Š  s    
zCookieJar._build_morsel)rT   r�   r&   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‚   )Úendswithrm   r   )rT   r�   Znon_matchingr(   r(   r.   ra   š  s    

zCookieJar._is_domain_match)Údate_strr&   c                 C   s¼  |sdS d}d}d}d}d } }}d}	d}
d}| j  |¡D ]¸}| d¡}|s|| j |¡}|r|d}dd„ | ¡ D ƒ\}}}q<|s¢| j |¡}|r¢d}t| ¡ ƒ}	q<|sÐ| j |¡}|rÐd}|j	dusÈJ ‚|j	}
q<|s<| j
 |¡}|r<d}t| ¡ ƒ}q<d|  k�rd	k�rn n
|d
7 }n$d|  k�r4dk�r@n n|d7 }d||||fv �rVdS d|	  k�rndk�stn dS |dk �sœ|dk�sœ|dk�sœ|dk�r d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.   rn   Â  re   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¹   Z
found_timeZ	found_dayZfound_monthZ
found_yearÚhourÚminuteÚsecondÚdayÚmonthÚyearZtoken_matchrº   Z
time_matchZ	day_matchZmonth_matchZ
year_matchr(   r(   r.   r�   ª  s^    

(zCookieJar._parse_date)N)?Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚreÚcompiler¿   rÂ   rÅ   ÚIrÆ   rÈ   r‹   ÚdatetimeÚmaxÚreplaceÚtimezoneÚutcÚ	timestampr�   rÉ   rÊ   r]   ÚgmtimeÚOSErrorrŽ   ÚOverflowErrorZSUB_MAX_TIMEÚboolr   r   r   r   ÚasyncioÚAbstractEventLoopr1   Úpropertyr#   r   rN   rP   r   r\   r*   rg   rl   rp   ri   r   r^   Úfloatr~   r   r   r”   r±   r   r£   Ústaticmethodra   Úclassmethodr�   Ú__classcell__r(   r(   r=   r.   r   3   sb   ÿ

þ
ÿ
úù!+JXr   c                       s°   e Zd ZdZddœeej ddœ‡ fdd„Zddœd	d
„Ze	dœdd„Z
eedœdd„ƒZdee ddœdd„Zeddœdd„Zeƒ feeddœdd„Zeddœ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__rh   r?   c                 c   s   d S r@   r(   rA   r(   r(   r.   rl   õ  s    zDummyCookieJar.__iter__c                 C   s   dS )Nr   r(   rA   r(   r(   r.   rp   ù  s    zDummyCookieJar.__len__c                 C   s   dS )NTr(   rA   r(   r(   r.   r#   ü  s    zDummyCookieJar.quote_cookierQ   c                 C   s   d S r@   r(   )r<   rR   r(   r(   r.   r\      s    zDummyCookieJar.clearr`   c                 C   s   d S r@   r(   rf   r(   r(   r.   rg     s    zDummyCookieJar.clear_domainr   c                 C   s   d S r@   r(   )r<   r€   r�   r(   r(   r.   r”     s    zDummyCookieJar.update_cookiesr•   r–   c                 C   s   t ƒ S r@   )r   )r<   r—   r(   r(   r.   r±   	  s    zDummyCookieJar.filter_cookies)N)rÒ   rÓ   rÔ   rÕ   r   rã   rä   r1   rl   r‹   rp   rå   râ   r#   r   r\   r*   rg   r   r   r”   r±   ré   r(   r(   r=   r.   r   ë  s    r   )5rã   rÉ   r¡   rÙ   ru   r¤   ÚosrF   rI   rÖ   r]   r�   Úcollectionsr   Úcollections.abcr   Úhttp.cookiesr   r   r   Útypingr   r   r	   r
   r   r   r   r   r   Zyarlr   Z_cookie_helpersr   Úabcr   r   Zhelpersr   Ztypedefsr   r   r   Ú__all__r*   Z
CookieItemÚformatr©   r¨   rt   rµ   r   r   r(   r(   r(   r.   Ú<module>   s>   ,   ;