a
    ǏPfG                     @  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
rd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 )zParses 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   d/var/www/html/python-backend/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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_uri
defrag_urir   r   r   _urlnorm?   s    zCacheController._urlnormc                 C  s
   |  |S )N)r8   )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|	sd ||< |rPz||d
  ||< W qP ty   |	rt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_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"   rE   rI   rJ   
isinstancer	   get_bodyr$   loadswarning)r)   rW   r9   
cache_data	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 rd	}t| |S t
|j}|rd
|vrd|vrtd | j| td dS t }t|d
 }|dusJ t|dd }	td||	 }
td|
 | |}d}|d}|dur^|}td| nLd|v rt|d }|durt|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_tuplera   current_ageresp_ccfreshness_lifetimemax_agerc   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 )Nrb   ETagzIf-None-Matchzlast-modifiedzLast-ModifiedzIf-Modified-Since)r`   r   r:   )r)   rW   rl   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.
            )rc   N)rY   r"   r	   setr$   dumpsset_body)r)   r9   rW   r{   r|   r}   r   r   r   
_cache_set  s    zCacheController._cache_set)rW   r{   r|   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rd|v r|d 	 rt
|d t|krdS | |j}	| |}
|jdusJ | |j}td| d}d	|
v rd
}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rd|v rd}|drt|d }|durt|dd | }t|d}td| d td | ||||| n
t
|jtv rtd | |||d nd|v rt|d }|dus:J t|dd }|
d}|dur|dkrtd |}| ||||| nhd|v r|d rt|d }|durt|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 %sNra   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: *"rb   rc   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)r(   rd   rI   rJ   r   r:   r   rh   ri   isdigitrD   lenrV   rX   r9   r"   rE   rf   r#   rj   r   re   format)r)   rW   r{   r|   r%   r(   response_headersro   ra   cc_reqrk   r9   no_storer}   rc   rs   r   r   r   cache_response=  s    



















zCacheController.cache_response)rW   r{   r   c                   sj   |j dusJ | |j }| |}|s,|S dg |j fdd|j D  d|_| ||| |S )zOn 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itemsrd   r   )r)   rW   r{   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`   rv   ry   r   r   r   r   r   r   r   r    0   s,       6n  "   
r    )"r   
__future__r   rh   loggingrerg   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   re   r   r    r   r   r   r   <module>   s(   

