o
    Œ1c	=  ã                   @   sˆ   d 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
 ddlmZ e e¡Ze d¡Zd	Zd
d„ ZG dd„ deƒZdS )z7
The httplib2 algorithms ported for use with requests.
é    N)Úparsedate_tz)ÚCaseInsensitiveDicté   )Ú	DictCache)Ú
Serializerz9^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)é-  é4  c                 C   s0   t  | ¡ ¡ }|d |d |d |d |d fS )z‡Parses a URI using the regex given in Appendix B of RFC 3986.

        (scheme, authority, path, query, fragment) = parse_uri(uri)
    r   é   é   é   é   )ÚURIÚmatchÚgroups)Úurir   © r   ú\/var/www/html/django/env/lib/python3.10/site-packages/pip/_vendor/cachecontrol/controller.pyÚ	parse_uri   s   "r   c                   @   s^   e Zd ZdZ	ddd„Zedd„ ƒZedd	„ ƒZd
d„ Zdd„ Z	dd„ Z
ddd„Zdd„ ZdS )ÚCacheControllerz9An interface to see if request should cached or not.
    NTc                 C   s4   |d u rt ƒ n|| _|| _|ptƒ | _|pd| _d S )N)éÈ   éË   i,  r   r   )r   ÚcacheÚcache_etagsr   Ú
serializerÚcacheable_status_codes)Úselfr   r   r   Ústatus_codesr   r   r   Ú__init__(   s   zCacheController.__init__c           	      C   sh   t |ƒ\}}}}}|r|std| ƒ‚| ¡ }| ¡ }|sd}|r(d ||g¡p)|}|d | | }|S )z4Normalize the URL to create a safe key for the cachez(Only absolute URIs are allowed. uri = %sú/ú?z://)r   Ú	ExceptionÚlowerÚjoin)	Úclsr   ÚschemeÚ	authorityÚpathÚqueryÚfragmentÚrequest_uriZ
defrag_urir   r   r   Ú_urlnorm0   s   zCacheController._urlnormc                 C   s
   |   |¡S ©N)r*   )r#   r   r   r   r   Ú	cache_urlD   s   
zCacheController.cache_urlc           
      C   s  t dft dft dfddddddddt dfdœ}| d| dd¡¡}i }| d¡D ]c}| ¡ s/q(| d	d
¡}|d  ¡ }z|| \}}	W n tyR   t d|¡ Y q(w |rW|	s[d ||< |r‹z||d
  ¡ ƒ||< W q( tyz   |	rxt d|¡ Y q( tyŠ   t d||j	¡ Y q(w q(|S )NTF)NF)úmax-agez	max-staleú	min-freshúno-cacheúno-storezno-transformzonly-if-cachedzmust-revalidateÚpublicÚprivatezproxy-revalidatezs-maxagezcache-controlzCache-ControlÚ ú,ú=r   r   z,Ignoring unknown cache-control directive: %sz-Missing value for cache-control directive: %sz8Invalid value for cache-control directive %s, must be %s)
ÚintÚgetÚsplitÚstripÚKeyErrorÚloggerÚdebugÚ
IndexErrorÚ
ValueErrorÚ__name__)
r   ÚheadersZknown_directivesZ
cc_headersÚretvalZcc_directiveÚpartsZ	directiveÚtypÚrequiredr   r   r   Úparse_cache_controlH   s^   óþþ€ýÿ÷z#CacheController.parse_cache_controlc                 C   s*  |   |j¡}t d|¡ |  |j¡}d|v rt d¡ dS d|v r.|d dkr.t d¡ dS | j |¡}|du r?t d	¡ dS | j 	||¡}|sOt 
d
¡ dS t|jƒtv r_d}t |¡ |S t|jƒ}|rjd|vr€d|vryt d¡ | j |¡ t d¡ dS t ¡ }t t|d ƒ¡}	td||	 ƒ}
t d|
¡ |  |¡}d}d|v r°|d }t d|¡ n d|v rÐt|d ƒ}|durÐt |¡|	 }td|ƒ}t d|¡ d|v rÞ|d }t d|¡ d|v rð|d }|
|7 }
t d|
¡ ||
krt d¡ t d||
¡ |S d|vrt d¡ | j |¡ dS )ze
        Return a cached response if it exists in the cache, otherwise
        return False.
        zLooking up "%s" in the cacher/   z-Request header has "no-cache", cache bypassedFr-   r   z1Request header has "max_age" as 0, cache bypassedNzNo cache entry availablez1Cache entry deserialization failed, entry ignoredzQReturning cached permanent redirect response (ignoring date and etag information)ÚdateÚetagz(Purging cached response: no date or etagz!Ignoring cached response: no datezCurrent age based on date: %iz#Freshness lifetime from max-age: %iÚexpiresz#Freshness lifetime from expires: %iz+Freshness lifetime from request max-age: %ir.   z'Adjusted current age from min-fresh: %iz2The response is "fresh", returning cached responsez%i > %iz4The cached response is "stale" with no etag, purging)r,   Úurlr;   r<   rE   r@   r   r7   r   ÚloadsÚwarningr6   ÚstatusÚPERMANENT_REDIRECT_STATUSESr   ÚdeleteÚtimeÚcalendarÚtimegmr   Úmax)r   Úrequestr,   ÚccZ
cache_dataÚrespÚmsgr@   ÚnowrF   Zcurrent_ageZresp_ccZfreshness_lifetimerH   Zexpire_timeZ	min_freshr   r   r   Úcached_request~   sz   



ÿ





ÿ



zCacheController.cached_requestc                 C   s`   |   |j¡}| j || j |¡¡}i }|r.t|jƒ}d|v r$|d |d< d|v r.|d |d< |S )NrG   ZETagzIf-None-Matchzlast-modifiedzLast-ModifiedzIf-Modified-Since)r,   rI   r   rJ   r   r7   r   r@   )r   rS   r,   rU   Znew_headersr@   r   r   r   Úconditional_headersî   s   
z#CacheController.conditional_headersc                 C   sÐ  |p| j }|j|vrt d|j|¡ dS t|jƒ}d|v r't t|d ƒ¡}nd}|durCd|v rC|d  	¡ rCt
|d ƒt|ƒkrCdS |  |j¡}|  |¡}	|  |j¡}
t d|
¡ d}d|	v rgd	}t d
¡ d|v rrd	}t d¡ |r…| j |
¡r…t d¡ | j |
¡ |r‰dS d| dd¡v r˜t d¡ dS | jrÚd|v rÚd}| d¡r·t|d ƒ}|dur·t |¡| }t|dƒ}t d |¡¡ t d¡ | jj|
| j |||¡|d dS t
|jƒtv rõt d¡ | j |
| j ||d¡¡ dS d|v rbt t|d ƒ¡}d|	v r)|	d dkr)t d¡ |	d }| jj|
| j |||¡|d dS d|v rd|d rft|d ƒ}|durFt |¡| }nd}t d |¡¡ | jj|
| jj|||d|d dS dS dS dS )zc
        Algorithm for caching requests.

        This assumes a requests Response object.
        zStatus code %s not in %sNrF   r   úcontent-lengthz&Updating cache with response from "%s"Fr0   TzResponse header has "no-store"zRequest header has "no-store"z0Purging existing cache entry to honor "no-store"Ú*Zvaryr3   zResponse header has "Vary: *"rG   rH   i u z"etag object cached for {0} secondszCaching due to etag)rH   zCaching permanent redirectó    r-   z'Caching b/c date exists and max-age > 0z5Caching b/c of expires header. expires in {0} seconds)Úbody)r   rL   r;   r<   r   r@   rP   rQ   r   Úisdigitr6   ÚlenrE   r,   rI   r   r7   rN   r   rR   ÚformatÚsetr   ÚdumpsrM   )r   rS   Úresponser]   r   r   Zresponse_headersrF   Zcc_reqrT   r,   Zno_storeZexpires_timerH   r   r   r   Úcache_responseþ   s¨   

ÿ
ÿþ
ýü








ý



ý


ÿÿ
ýåzCacheController.cache_responsec                    sv   |   |j¡}| j || j |¡¡}|s|S dg‰ |j t‡ fdd„|j 	¡ D ƒƒ¡ d|_
| j || j ||¡¡ |S )zéOn a 304 we will get a new set of headers that we want to
        update our cached value with, assuming we have one.

        This should only ever be called when we've sent an ETag and
        gotten a 304 as the response.
        rZ   c                 3   s(    | ]\}}|  ¡ ˆ vr||fV  qd S r+   )r!   )Ú.0ÚkÚv©Zexcluded_headersr   r   Ú	<genexpr>’  s   € 
ý
ÿz9CacheController.update_cached_response.<locals>.<genexpr>r   )r,   rI   r   rJ   r   r7   r@   ÚupdateÚdictÚitemsrL   ra   rb   )r   rS   rc   r,   Úcached_responser   rh   r   Úupdate_cached_responsey  s   	þÿ	z&CacheController.update_cached_response)NTNN)NN)r?   Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr*   r,   rE   rX   rY   rd   rn   r   r   r   r   r   $   s    
ÿ

6p
{r   )rq   ÚloggingÚrerP   rO   Úemail.utilsr   Úpip._vendor.requests.structuresr   r   r   Ú	serializer   Ú	getLoggerr?   r;   Úcompiler   rM   r   Úobjectr   r   r   r   r   Ú<module>   s   

	