a
    <BìjžJ  ã                   @  sî   d Z ddlm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 ddlmZ er´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dœdd„Z!G dd„ dƒZ"dS )z7
The httplib2 algorithms ported for use with requests.
é    )ÚannotationsN)Úparsedate_tz)ÚTYPE_CHECKINGÚ
CollectionÚMapping)ÚCaseInsensitiveDict)Ú	DictCacheÚSeparateBodyBaseCache)Ú
Serializer)ÚLiteral)ÚPreparedRequest)ÚHTTPResponse)Ú	BaseCachez9^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)é-  é4  Ústrztuple[str, str, str, str, str]©ÚuriÚreturnc                 C  s@   t  | ¡}|dusJ ‚| ¡ }|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)
    Né   é   é   é   é   )ÚURIÚmatchÚgroups)r   r   r   © r   úb/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pip/_vendor/cachecontrol/controller.pyÚ	parse_uri'   s    
r   c                   @  sÒ   e Zd ZdZd/dddddœd	d
„Zedddœdd„ƒZedddœdd„ƒZdddœdd„Zdddœdd„Z	dddœdd„Z
dddœdd „Zd0ddd!d"d#d$d%œd&d'„Zd1dd(d"dd$d)œd*d+„Zdd!d!d,œd-d.„ZdS )2ÚCacheControllerz4An interface to see if request should cached or not.NTzBaseCache | NoneÚboolzSerializer | NonezCollection[int] | None)ÚcacheÚcache_etagsÚ
serializerÚstatus_codesc                 C  s4   |d u rt ƒ n|| _|| _|p"tƒ | _|p,d| _d S )N)éÈ   éË   i,  r   r   )r   r"   r#   r
   r$   Úcacheable_status_codes)Úselfr"   r#   r$   r%   r   r   r   Ú__init__5   s    zCacheController.__init__r   r   c           	      C  sh   t |ƒ\}}}}}|r|s&td| ƒ‚| ¡ }| ¡ }|s>d}|rPd ||g¡pR|}|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_uriÚ
defrag_urir   r   r   Ú_urlnormA   s    zCacheController._urlnormc                 C  s
   |   |¡S )N)r8   )r0   r   r   r   r   Ú	cache_urlU   s    zCacheController.cache_urlzMapping[str, str]zdict[str, int | None])Úheadersr   c           
      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 ]Ì}| ¡ s^qP| d	d
¡}|d  ¡ }z|| \}}	W n" ty¨   t d|¡ Y qPY n0 |r²|	sºd ||< |rPz||d
  ¡ ƒ||< W qP tyø   |	rôt d|¡ Y qP t�y   t d||j	¡ Y qP0 qP|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)   r:   Úknown_directivesÚ
cc_headersÚretvalÚcc_directiveÚpartsÚ	directiveÚtypÚrequiredr   r   r   Úparse_cache_controlY   sV    ó
þýz#CacheController.parse_cache_controlr   zHTTPResponse | None)Úrequestr   c                 C  s†   d|j v rdS |j}|dus J ‚| j |¡}|du rBt d¡ dS t| jtƒr\| j |¡}nd}| j	 
|||¡}|du r‚t d¡ |S )zO
        Load a cached response, or return None if it's not available.
        ZRangeNzNo cache entry availablez1Cache entry deserialization failed, entry ignored)r:   Úurlr"   rE   rI   rJ   Ú
isinstancer	   Úget_bodyr$   ÚloadsÚwarning)r)   rW   r9   Ú
cache_dataZ	body_fileÚresultr   r   r   Ú_load_from_cache�   s    


z CacheController._load_from_cachezHTTPResponse | Literal[False]c                 C  s@  |j dusJ ‚|  |j ¡}t d|¡ |  |j¡}d|v rHt d¡ dS d|v rj|d dkrjt d¡ dS |  |¡}|s|dS t|jƒt	v rœd	}t |¡ |S t
|jƒ}|r²d
|vrÞd|vrÐt d¡ | j |¡ t d¡ dS t ¡ }t|d
 ƒ}|dusþJ ‚t |dd… ¡}	td||	 ƒ}
t d|
¡ |  |¡}d}| d¡}|du�r^|}t d|¡ nLd|v �rªt|d ƒ}|du�rªt |dd… ¡|	 }td|ƒ}t d|¡ | d¡}|du�rÎ|}t d|¡ | d¡}|du�rö|
|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.
        NzLooking up "%s" in the cacher=   z-Request header has "no-cache", cache bypassedFr;   r   z1Request header has "max_age" as 0, cache bypassedzQReturning cached permanent redirect response (ignoring date and etag information)ÚdateÚetagz(Purging cached response: no date or etagz!Ignoring cached response: no dater   z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)rX   r9   rI   rJ   rV   r:   r_   rD   ÚstatusÚPERMANENT_REDIRECT_STATUSESr   r"   ÚdeleteÚtimer   ÚcalendarÚtimegmÚmaxrE   )r)   rW   r9   ÚccÚrespÚmsgr:   ÚnowÚ
time_tupler`   Úcurrent_ageÚresp_ccÚfreshness_lifetimeÚmax_agerb   Úexpire_timeÚ	min_freshr   r   r   Úcached_request©   sz    


ÿ











ÿ





zCacheController.cached_requestzdict[str, str]c                 C  sH   |   |¡}i }|rDt|jƒ}d|v r0|d |d< d|v rD|d |d< |S )Nra   ÚETagzIf-None-Matchzlast-modifiedzLast-ModifiedzIf-Modified-Since)r_   r   r:   )r)   rW   rk   Únew_headersr:   r   r   r   Úconditional_headers  s    

z#CacheController.conditional_headersr   zbytes | Nonez
int | NoneÚNone)r9   rW   ÚresponseÚbodyÚexpires_timer   c                 C  sd   t | jtƒrB| jj|| j ||d¡|d� |dur`| j ||¡ n| jj|| j |||¡|d� dS )z.
        Store the data in the cache.
        ó    )rb   N)rY   r"   r	   Úsetr$   ÚdumpsZset_body)r)   r9   rW   rz   r{   r|   r   r   r   Ú
_cache_set&  s    ýýzCacheController._cache_setz2HTTPResponse | weakref.ReferenceType[HTTPResponse])rW   Úresponse_or_refr{   r%   r   c                 C  s  t |tjƒr |ƒ }|du r$dS n|}|p,| j}|j|vrLt d|j|¡ dS t|jƒ}d|v rŠt	|d ƒ}|dusvJ ‚t
 |dd… ¡}	nd}	|durÂd|v rÂ|d  ¡ rÂt|d ƒt|ƒkrÂdS |  |j¡}
|  |¡}|jdusæJ ‚|  |j¡}t d|¡ d}d	|v �rd
}t d¡ d	|
v �r2d
}t d¡ |�r\| j |¡�r\t d¡ | j |¡ |�rfdS d| dd¡v �r†t d¡ dS | j�rd|v �rd}| d¡�rÔt	|d ƒ}|du�rÔt
 |dd… ¡|	 }t|dƒ}t d|› d�¡ t d¡ |  |||||¡ �n
t|jƒtv �r<t d¡ |  |||d¡ nÞd|v �rt	|d ƒ}|du�s`J ‚t
 |dd… ¡}	| d¡}|du�r²|dk�r²t d¡ |}|  |||||¡ nhd|v �r|d �rt	|d ƒ}|du�rôt
 |dd… ¡|	 }nd}t d |¡¡ |  |||||¡ dS )zc
        Algorithm for caching requests.

        This assumes a requests Response object.
        NzStatus code %s not in %sr`   r   r   úcontent-lengthz&Updating cache with response from "%s"Fr>   TzResponse header has "no-store"zRequest header has "no-store"z0Purging existing cache entry to honor "no-store"Ú*ÚvaryrA   zResponse header has "Vary: *"ra   rb   i u zetag object cached for z secondszCaching due to etagzCaching permanent redirectr}   r;   z'Caching b/c date exists and max-age > 0z4Caching b/c of expires header. expires in {} seconds)rY   ÚweakrefÚReferenceTyper(   rc   rI   rJ   r   r:   r   rg   rh   ÚisdigitrD   ÚlenrV   rX   r9   r"   rE   re   r#   ri   r€   rd   Úformat)r)   rW   r�   r{   r%   rz   r(   Úresponse_headersrn   r`   Úcc_reqrj   r9   Úno_storer|   rb   rr   r   r   r   Úcache_responseD  s¼    

ÿÿÿþ
ýü













û



ÿÿûzCacheController.cache_response)rW   rz   r   c                   sj   |j dusJ ‚|  |j ¡}|  |¡}|s,|S dg‰ |j ‡ fdd„|j ¡ D ƒ¡ d|_|  |||¡ |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.
        Nr‚   c                   s"   i | ]\}}|  ¡ ˆ vr||“qS r   )r.   )Ú.0ÚkÚv©Úexcluded_headersr   r   Ú
<dictcomp>ò  s   þz:CacheController.update_cached_response.<locals>.<dictcomp>r&   )rX   r9   r_   r:   ÚupdateÚitemsrc   r€   )r)   rW   rz   r9   Úcached_responser   r‘   r   Úupdate_cached_response×  s    	
	
þÿ	z&CacheController.update_cached_response)NTNN)NN)NN)rM   Ú
__module__Ú__qualname__Ú__doc__r*   Úclassmethodr8   r9   rV   r_   ru   rx   r€   r�   r—   r   r   r   r   r    2   s,       û6n  ú"  û r    )#rš   Ú
__future__r   rg   ÚloggingÚrerf   r…   Úemail.utilsr   Útypingr   r   r   Zpip._vendor.requests.structuresr   Zpip._vendor.cachecontrol.cacher   r	   Z"pip._vendor.cachecontrol.serializer
   r   Zpip._vendor.requestsr   Zpip._vendor.urllib3r   r   Ú	getLoggerrM   rI   Úcompiler   rd   r   r    r   r   r   r   Ú<module>   s*   

