a
    i,MeÐG  ã                   @  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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   úR/var/www/webapp/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d/ddd!d"d#d$d%œd&d'„Zd0dd!d"dd$d(œd)d*„Zdd!d!d+œd,d-„ZdS )1Ú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__3   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_uriZ
defrag_urir   r   r   Ú_urlnorm?   s    zCacheController._urlnormc                 C  s
   |   |¡S )N)r7   )r0   r   r   r   r   Ú	cache_urlS   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)   r9   Zknown_directivesZ
cc_headersÚretvalZcc_directiveÚpartsZ	directiveÚtypÚrequiredr   r   r   Úparse_cache_controlW   sV    ó
þýz#CacheController.parse_cache_controlr   zHTTPResponse | None)Úrequestr   c                 C  sx   |j }|dusJ ‚| j |¡}|du r4t d¡ dS t| jtƒrN| j |¡}nd}| j 	|||¡}|du rtt 
d¡ |S )zO
        Load a cached response, or return None if it's not available.
        NzNo cache entry availablez1Cache entry deserialization failed, entry ignored)Úurlr"   rD   rH   rI   Ú
isinstancer	   Úget_bodyr$   ÚloadsÚwarning)r)   rR   r8   Z
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)rS   r8   rH   rI   rQ   r9   rY   rC   ÚstatusÚPERMANENT_REDIRECT_STATUSESr   r"   ÚdeleteÚtimer   ÚcalendarÚtimegmÚmaxrD   )r)   rR   r8   ÚccÚrespÚmsgr9   ÚnowÚ
time_tuplerZ   Zcurrent_ageZresp_ccZfreshness_lifetimeÚmax_ager\   Zexpire_timeZ	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 )Nr[   ZETagzIf-None-Matchzlast-modifiedzLast-ModifiedzIf-Modified-Since)rY   r   r9   )r)   rR   re   Znew_headersr9   r   r   r   Úconditional_headers  s    

z#CacheController.conditional_headersr   zbytes | Nonez
int | NoneÚNone)r8   rR   Ú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.
        ó    )r\   N)rT   r"   r	   Úsetr$   ÚdumpsÚset_body)r)   r8   rR   rm   rn   ro   r   r   r   Ú
_cache_set  s    ýýzCacheController._cache_set)rR   rm   rn   r%   r   c                 C  sø  |p| j }|j|vr(t d|j|¡ dS t|jƒ}d|v rft|d ƒ}|dusRJ ‚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 rd
}t d¡ |r6| j |¡r6t 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 |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.
        zStatus code %s not in %sNrZ   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"Ú*Zvaryr@   zResponse header has "Vary: *"r[   r\   i u zetag object cached for z secondszCaching due to etagzCaching permanent redirectrp   r:   z'Caching b/c date exists and max-age > 0z4Caching b/c of expires header. expires in {} seconds)r(   r]   rH   rI   r   r9   r   ra   rb   ÚisdigitrC   ÚlenrQ   rS   r8   r"   rD   r_   r#   rc   rt   r^   Úformat)r)   rR   rm   rn   r%   r(   Zresponse_headersrh   rZ   Zcc_reqrd   r8   Zno_storero   r\   ri   r   r   r   Úcache_response=  s²    

ÿÿÿþ
ýü












û



ÿÿûzCacheController.cache_response)rR   rm   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.
        Nru   c                   s"   i | ]\}}|  ¡ ˆ vr||“qS r   )r.   )Ú.0ÚkÚv©Zexcluded_headersr   r   Ú
<dictcomp>á  s   þz:CacheController.update_cached_response.<locals>.<dictcomp>r&   )rS   r8   rY   r9   ÚupdateÚitemsr]   rt   )r)   rR   rm   r8   Úcached_responser   r~   r   Úupdate_cached_responseÆ  s    	
	
þÿ	z&CacheController.update_cached_response)NTNN)NN)NN)rL   Ú
__module__Ú__qualname__Ú__doc__r*   Úclassmethodr7   r8   rQ   rY   rj   rk   rt   rz   rƒ   r   r   r   r   r    0   s,       û6n  ú"  û 
r    )"r†   Ú
__future__r   ra   ÚloggingÚrer`   Úemail.utilsr   Útypingr   r   r   Úpip._vendor.requests.structuresr   Úpip._vendor.cachecontrol.cacher   r	   Ú"pip._vendor.cachecontrol.serializer
   r   Úpip._vendor.requestsr   Úpip._vendor.urllib3r   r   Ú	getLoggerrL   rH   Úcompiler   r^   r   r    r   r   r   r   Ú<module>   s(   

