a
    nJh6                     @   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	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*G dd de Z+G dd de Z,d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]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&d8e 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Z5e4e)e)ed2d3d4Z6e7e)e e d5d6d7Z8  Z9S )9r   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)   C/var/www/shaz/venv/lib/python3.9/site-packages/aiohttp/cookiejar.py
<listcomp>b   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   L   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/   savej   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   o   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   t   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 | D S )Nc                 s   s   | ]
}d V  qdS )r   Nr)   )r-   ir)   r)   r/   	<genexpr>   rf   z$CookieJar.__len__.<locals>.<genexpr>)sum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 }	|	rvz0t|	}
tt |
 | j}| |||| W n tyr   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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   sZ    





zCookieJar.update_cookiesBaseCookie[str]request_urlr'   c           
      C   sb  | 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}t| dd dD ]}|j}|d }|s|j||< q| jst|rq||f| jv r||krqn| ||sq| |j|d sq|r*|d	 r*qtd
||jt }	|	|j|j|j |	||< q|S )z8Returns this jar's cookies filtered by their attributes.r{   )httpswssNc                 S   s   t | d S )Nr[   )len)cr)   r)   r/   re     rf   z*CookieJar.filter_cookies.<locals>.<lambda>)r_   rZ   r[   securer   )r7   r   r   r3   rm   r   r~   schemer8   
contextlibsuppressr   r,   sortedr_   valuer6   r   r5   ra   _is_path_matchr[   r   getr   r4   coded_value)
r<   r   filteredr   Zis_not_secureZrequest_originr\   r]   rZ   Zmrsl_valr)   r)   r/   filter_cookies   sH    


&


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   r   r   )rZ   r   non_matchingr)   r)   r/   ra   $  s    

zCookieJar._is_domain_match)req_pathcookie_pathr'   c                 C   sP   |  dsd} | |krdS |  |s(dS |dr6dS | t|d }| dS )z.Implements path matching adhering to RFC 6265.r|   TFN)r   r   r   )r   r   r   r)   r)   r/   r   4  s    


zCookieJar._is_path_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   _  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   G  s^    

(zCookieJar._parse_date)N):__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   r   classmethodr   __classcell__r)   r)   r=   r/   r   '   s^   



H7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   )-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
CookieItemr   r   r)   r)   r)   r/   <module>   s,   4  c