a
    Rf8                     @   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
mZ d dlmZmZmZ d dlmZ d dlmZmZ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!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.G dd de!Z/G dd de!Z0dS )    N)defaultdict)
BaseCookieMorselSimpleCookie)ceil)DefaultDictDictIterableIteratorListMappingOptionalSetTupleUnioncast)URL   )AbstractCookieJarClearCookiePredicate)is_ip_address)LooseCookiesPathLikeStrOrURL)	CookieJarDummyCookieJarMorsel[str]z{}/{}z{1}.{0}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$ddddZ%e$ddddZ&d5e 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.e)e)e)dd%d&d'Z/e0 fe1e0dd(d)d*Z2e0 fe0d+d,d-d.Z3e4e)e)ed/d0d1Z5e6e)e e d2d3d4Z7  Z8S )6r   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 | _|| _|| _|d u r:g }n>t	|t
rP| g}n(t	|trjt
| g}ndd |D }|| _tt | _i | _d S )Nr&   c                 S   s*   g | ]"}t |trt| n| qS  )
isinstancestrr   origin).0urlr)   r)   J/var/www/ai-form-bot/venv/lib/python3.9/site-packages/aiohttp/cookiejar.py
<listcomp>g   s   z&CookieJar.__init__.<locals>.<listcomp>)super__init__r   r   _cookiesset_host_only_cookies_unsafe_quote_cookier*   r   r,   r+   _treat_as_secure_originr   time_next_expiration_expirations)selfr#   r$   r%   r&   	__class__r)   r/   r2   Q   s&    

zCookieJar.__init__)	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dumpr3   HIGHEST_PROTOCOLr<   r?   fr)   r)   r/   saveo   s    
zCookieJar.savec                 C   sF   t |}|jdd}t|| _W d    n1 s80    Y  d S )NrbrA   )rC   rD   rE   rF   loadr3   rI   r)   r)   r/   rM   t   s    
zCookieJar.load	predicater'   c           
      C   s,  |d u r8t t | _| j  | j  | j  d S g }t }| j D ]R\\}}}| D ]<\}}|||f}	|	| jv r| j|	 |ks||rb||	 qbqN|D ]R\}}}| j	||f |||f}	|	| jv r| j|||f= | j||f 
|d  q| jr tg | j | jR  d n| j| _d S )Nr   )r   r9   r:   r3   clearr5   r;   itemsappenddiscardpopminvaluesSUB_MAX_TIMEMAX_TIME)
r<   rO   Zto_delnowdomainpathcookienamemorselkeyr)   r)   r/   rP   y   s8    





"zCookieJar.clearrZ   r'   c                    s     fdd d S )Nc                    s     | d S )NrZ   )_is_domain_matchxrZ   r<   r)   r/   <lambda>       z(CookieJar.clear_domain.<locals>.<lambda>rP   r<   rZ   r)   rd   r/   clear_domain   s    zCookieJar.clear_domainIterator[Morsel[str]]r'   c                 c   s*   |    | j D ]}| E d H  qd S N)_do_expirationr3   rV   )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 rl   )lenrV   )r-   r\   r)   r)   r/   	<genexpr>   rf   z$CookieJar.__len__.<locals>.<genexpr>)sumr3   rV   r<   r)   r)   r/   __len__   s    zCookieJar.__len__c                 C   s   |  dd  d S )Nc                 S   s   dS )NFr)   rb   r)   r)   r/   re      rf   z*CookieJar._do_expiration.<locals>.<lambda>rg   rs   r)   r)   r/   rm      s    zCookieJar._do_expiration)whenrZ   r[   r]   r'   c                 C   s"   t | j|| _|| j|||f< d S rl   )rU   r:   r;   )r<   ru   rZ   r[   r]   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 }|drtd}|d= |s|dur| j	
||f | }|d< |dr|dd }||d< |r| ||sq.|d }|r|ds|j}|dsd}nd|d|d  }||d< |d}|d }	|	rz0t|	}
tt |
 | j}| |||| W n ty|   d|d< Y n0 n8|d	 }|r| |}|r| |||| nd|d	< || j||f |< q.|   dS )
zUpdate cookies.NrZ   . r   r[   /zmax-ageexpires)raw_hostr6   r   r*   r   rQ   r   r   endswithr5   add
startswithra   r[   rfindrstripintrU   r9   rX   rv   
ValueError_parse_dater3   rm   )r<   rx   ry   hostnamer]   r\   tmprZ   r[   max_ageZdelta_secondsZmax_age_expirationr}   expire_timer)   r)   r/   update_cookies   s\    






zCookieJar.update_cookiesBaseCookie[str]request_urlr'   c                    s   j rt nt } js|S     js.|S t|}|jp>d}|jdv}|r jrt }t	
t | }W d   n1 s0    Y  | jv} jd  D ]}|j||j< qt|rЈ js|S |f}ntt|dt}t|jdt}t||}	tj fdd|	D }
t|j}|
D ]}|j}|d	 }||f jv r^||kr^q.t|d
 |krtq.|r|d rq.td||jt  }|!|j|j|j" |||< q.|S )z8Returns this jar's cookies filtered by their attributes.r{   )httpswssN)r{   r{   rz   r|   c                 3   s   | ]} j |  V  qd S rl   )r3   rV   )r-   prs   r)   r/   rq   "  s   z+CookieJar.filter_cookies.<locals>.<genexpr>rZ   r[   securer   )#r7   r   r   r3   rm   r   r~   schemer8   
contextlibsuppressr   r,   rV   valuer_   r   r6   	itertools
accumulatereversedsplit_FORMAT_DOMAIN_REVERSEDr[   _FORMAT_PATHproductchainfrom_iterablerp   r5   r   getr   r4   coded_value)r<   r   filteredr   Zis_not_secureZrequest_origincdomainspathspairsrx   Zpath_lenr\   r]   rZ   Zmrsl_valr)   rs   r/   filter_cookies   sX    


&


zCookieJar.filter_cookies)rZ   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.TFNrz   )r   rp   r   )rZ   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|}|rd}t| }	q<|s| j|}|rd}|j	dusJ |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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 rl   )r   )r-   sr)   r)   r/   rq   e  rf   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   M  s^    

(zCookieJar._parse_date)N)9__name__
__module____qualname____doc__recompiler   r   r   Ir   r   r   datetimemaxreplacetimezoneutc	timestamprX   r   r   r9   gmtimeOSErrorr   OverflowErrorrW   boolr   r   r   r   asyncioAbstractEventLoopr2   r   rK   rM   r   rP   r+   ri   ro   rt   rm   floatrv   r   r   r   r   staticmethodra   classmethodr   __classcell__r)   r)   r=   r/   r   ,   sZ   



IE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
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(   )r1   r2   )r<   r&   r=   r)   r/   r2     s    zDummyCookieJar.__init__rj   rk   c                 c   s   d S rl   r)   rs   r)   r)   r/   ro     s    zDummyCookieJar.__iter__c                 C   s   dS )Nr   r)   rs   r)   r)   r/   rt     s    zDummyCookieJar.__len__rN   c                 C   s   d S rl   r)   )r<   rO   r)   r)   r/   rP     s    zDummyCookieJar.clearr`   c                 C   s   d S rl   r)   rh   r)   r)   r/   ri     s    zDummyCookieJar.clear_domainrw   c                 C   s   d S rl   r)   )r<   rx   ry   r)   r)   r/   r     s    zDummyCookieJar.update_cookiesr   r   c                 C   s   t  S rl   )r   )r<   r   r)   r)   r/   r     s    zDummyCookieJar.filter_cookies)N)r   r   r   r   r   r   r   r2   ro   r   rt   r   rP   r+   ri   r   r   r   r   r   r)   r)   r=   r/   r     s    r   )1r   r   r   r   r   osrC   rF   r   r9   collectionsr   http.cookiesr   r   r   mathr   typingr   r   r	   r
   r   r   r   r   r   r   r   Zyarlr   abcr   r   Zhelpersr   Ztypedefsr   r   r   __all__r+   Z
CookieItemformatr   r   r   r   r)   r)   r)   r/   <module>   s2   4  d