
    .PhUJ                        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j        e          Z ej        d          ZdZ ddZ! G 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  uristrreturntuple[str, str, str, str, str]c                    t                               |           }|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   s      W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/cachecontrol/controller.py	parse_urir    '   sQ    
 IIcNNE\\^^F1Ivay&)VAYq	BB    c                      e Zd ZdZ	 	 	 	 d+d,dZed-d            Zed-d            Zd.dZd/dZ	d0dZ
d1dZ	 	 d2d3d&Z	 	 d2d4d)Zd5d*ZdS )6CacheControllerz4An interface to see if request should cached or not.NTcacheBaseCache | Nonecache_etagsbool
serializerSerializer | Nonestatus_codesCollection[int] | Nonec                ~    |t                      n|| _        || _        |pt                      | _        |pd| _        d S )N)      i,  r   r   )r	   r$   r&   r   r(   cacheable_status_codes)selfr$   r&   r(   r*   s        r   __init__zCacheController.__init__5   sC     %*MY[[[u
&$4
&2&O6O###r!   r   r   r   c                    t          |          \  }}}}}|r|st          d|z            |                                }|                                }|sd}|rd                    ||g          p|}|dz   |z   |z   }|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_uris	            r   _urlnormzCacheController._urlnormA   s     6?s^^2D% 	NY 	NFLMMMOO%%	 	D 7$ 7 7?4e^i/+=
r!   c                ,    |                      |          S )N)r@   )r8   r   s     r   	cache_urlzCacheController.cache_urlU   s    ||C   r!   headersMapping[str, str]dict[str, int | None]c                   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|                    d	d
          }|d                                         }	 ||         \  }}	n+# t          $ r t
                              d|           Y |w xY w|r|	sd ||<   |r	  ||d
                                                   ||<   # t          $ r  |	rt
                              d|           Y t          $ r% t
                              d||j	                   Y 	w xY w|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__)
r0   rC   known_directives
cc_headersretvalcc_directiveparts	directivetyprequireds
             r   parse_cache_controlz#CacheController.parse_cache_controlY   s    T{ut%%)+,#$ -d
 
  [['++or2R2RSS
(*&,,S11 	 	L%%''  &&sA..Ea((I 0 ;XX   KYWWW  )h )$(y! (+E!HNN,<,<(=(=F9%%!    N%   "   LLU!      s*   6C%C*)C*9&D  'E7	*E76E7requestr   HTTPResponse | Nonec                   d|j         v rdS |j        }|J | j                            |          }|t                              d           dS t          | j        t                    r| j                            |          }nd}| j	        
                    |||          }|t                              d           |S )zO
        Load a cached response, or return None if it's not available.
        RangeNzNo cache entry availablez1Cache entry deserialization failed, entry ignored)rC   urlr$   rQ   rU   rV   
isinstancer
   get_bodyr(   loadswarning)r0   rc   rB   
cache_data	body_fileresults         r   _load_from_cachez CacheController._load_from_cache   s     go%%4K	$$$Z^^I..
LL34444dj"788 	
++I66III&&w
IFF>NNNOOOr!   HTTPResponse | Literal[False]c                   |j         J |                     |j                   }t                              d|           |                     |j                  }d|v rt                              d           dS d|v r(|d         dk    rt                              d           dS |                     |          }|sdS t          |j                  t          v rd	}t                              |           |S t          |j                  }|rd
|vrTd|vr4t                              d           | j                            |           t                              d           dS t          j                    }t          |d
                   }|J t          j        |dd                   }	t#          d||	z
            }
t                              d|
           |                     |          }d}|                    d          }||}t                              d|           ned|v rat          |d                   }|Jt          j        |dd                   |	z
  }t#          d|          }t                              d|           |                    d          }||}t                              d|           |                    d          }| |
|z  }
t                              d|
           ||
k    r8t                              d           t                              d||
           |S d|vr4t                              d           | j                            |           dS )ze
        Return a cached response if it exists in the cache, otherwise
        return False.
        NzLooking up "%s" in the cacherI   z-Request header has "no-cache", cache bypassedFrG   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: %irH   z'Adjusted current age from min-fresh: %iz2The response is "fresh", returning cached responsez%i > %iz4The cached response is "stale" with no etag, purging)rg   rB   rU   rV   rb   rC   ro   rP   statusPERMANENT_REDIRECT_STATUSESr   r$   deletetimer   calendartimegmmaxrQ   )r0   rc   rB   ccrespmsgrC   now
time_tuplerr   current_ageresp_ccfreshness_lifetimemax_agert   expire_time	min_freshs                    r   cached_requestzCacheController.cached_request   s~   
 {&&&NN7;//	3Y???%%go66 LLHIII5??r)}11LLLMMM5 $$W-- 	5 t{:::7  LLK,?,M,M 	&//W$$ GHHH
!!),,,LL<===5ikk!'&/22
%%%z"1"~..!S4Z((4kBBB **733  ++i((!(LL>@RSSSS '!!"79#566G"&ogbqbk::TA%(K%8%8"BDVWWW &&##!(LL=?Q   FF;''	 9$KLLBKPPP ++LLMNNNLL$6DDDK   LLOPPPJi((( ur!   dict[str, str]c                    |                      |          }i }|r2t          |j                  }d|v r|d         |d<   d|v r|d         |d<   |S )Nrs   ETagzIf-None-Matchzlast-modifiedzLast-ModifiedzIf-Modified-Since)ro   r   rC   )r0   rc   r}   new_headersrC   s        r   conditional_headersz#CacheController.conditional_headers  sl    $$W-- 	L0CDL0Q0QG  /6vO,'))3:?3K/0r!   rB   responser   bodybytes | Noneexpires_time
int | NoneNonec                X   t          | j        t                    rX| j                            || j                            ||d          |           || j                            ||           dS dS | j                            || j                            |||          |           dS )z.
        Store the data in the cache.
        r!   )rt   N)rh   r$   r
   setr(   dumpsset_body)r0   rB   rc   r   r   r   s         r   
_cache_setzCacheController._cache_set&  s     dj"788 	 JNN%%gx==$     
##It44444   JNN%%gx>>$      r!   response_or_ref2HTTPResponse | weakref.ReferenceType[HTTPResponse]c                   t          |t          j                  r |            }|dS n|}|p| j        }|j        |vr#t
                              d|j        |           dS t          |j                  }d|v r6t          |d                   }|J t          j        |dd                   }	nd}	|Fd|v rB|d                                         r(t          |d                   t          |          k    rdS |                     |j                  }
|                     |          }|j        J |                     |j                  }t
                              d|           d}d	|v rd
}t
                              d           d	|
v rd
}t
                              d           |rN| j                            |          r4t
                              d           | j                            |           |rdS d|                    dd          v rt
                              d           dS | j        rd|v rd}|                    d          r6t          |d                   }|t          j        |dd                   |	z
  }t-          |d          }t
                              d| d           t
                              d           |                     |||||           dS t          |j                  t0          v r4t
                              d           |                     |||d           dS d|v rt          |d                   }|J t          j        |dd                   }	|                    d          }|=|dk    r7t
                              d           |}|                     |||||           dS d|v r|d         rt          |d                   }| t          j        |dd                   |	z
  }nd}t
                              d                    |                     |                     |||||           dS dS dS dS )zc
        Algorithm for caching requests.

        This assumes a requests Response object.
        NzStatus code %s not in %srr   r   r   content-lengthz&Updating cache with response from "%s"FrJ   TzResponse header has "no-store"zRequest header has "no-store"z0Purging existing cache entry to honor "no-store"*varyrM   zResponse header has "Vary: *"rs   rt   i u zetag object cached for z secondszCaching due to etagzCaching permanent redirectr!   rG   z'Caching b/c date exists and max-age > 0z4Caching b/c of expires header. expires in {} seconds)rh   weakrefReferenceTyper/   ru   rU   rV   r   rC   r   ry   rz   isdigitrP   lenrb   rg   rB   r$   rQ   rw   r&   r{   r   rv   format)r0   rc   r   r   r*   r   r/   response_headersr   rr   cc_reqr|   rB   no_storer   rt   r   s                    r   cache_responsezCacheController.cache_responseD  s    ow'<== 	'&((H 	   'H ".!L1L?"888LL*HO=S   F5H6
 6
 %%%%&6v&>??J)))?:bqb>22DDD  $444 !12::<< 5$%56773t99DDF))'/::%%&677{&&&NN7;//	=yIII HLL9:::HLL8999 	)
y11 	)LLKLLLJi((( 	F "&&vr2222LL8999F  <	*: : :L##I.. G&'7	'BCC&#+?72A2;#?#?$#FL|Z88LLLI<IIIJJJLL.///OOIw$MMMMM !!%@@@LL5666OOIw#>>>>>
 '''%&6v&>??J)))?:bqb>22DffY''G"w{{FGGG&      ...#I. *+;I+FGGG*'/wrr{'C'Cd'J'+LLNUU(   
 OO! $    A ('& /. r!   c                <   |j         J |                     |j                   }|                     |          }|s|S dg|j                            f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                H    i | ]\  }}|                                 v||S  )r6   ).0kvexcluded_headerss      r   
<dictcomp>z:CacheController.update_cached_response.<locals>.<dictcomp>  s=       Aq7799$444 1444r!   r-   )rg   rB   ro   rC   updateitemsru   r   )r0   rc   r   rB   cached_responser   s        @r   update_cached_responsez&CacheController.update_cached_response  s     {&&&NN7;//	//88 	O --&&   $,2244  	
 	
 	
 "% 		7O<<<r!   )NTNN)r$   r%   r&   r'   r(   r)   r*   r+   )r   r   r   r   )rC   rD   r   rE   )rc   r   r   rd   )rc   r   r   rp   )rc   r   r   r   )NN)rB   r   rc   r   r   r   r   r   r   r   r   r   )
rc   r   r   r   r   r   r*   r+   r   r   )rc   r   r   r   r   r   )rY   
__module____qualname____doc__r1   classmethodr@   rB   rb   ro   r   r   r   r   r   r   r!   r   r#   r#   2   s9       >> #' (,/3
P 
P 
P 
P 
P    [& ! ! ! [!4 4 4 4l   4l l l l\   ( "#'    D "/3Q Q Q Q Qf( ( ( ( ( (r!   r#   )r   r   r   r   )#r   
__future__r   ry   loggingrerx   r   email.utilsr   typingr   r   r   requests.structuresr   cachecontrol.cacher	   r
   cachecontrol.serializer   r   requestsr   urllib3r   r   	getLoggerrY   rU   compiler   rv   r    r#   r   r!   r   <module>r      s  
  # " " " " "   				   $ $ $ $ $ $ 5 5 5 5 5 5 5 5 5 5 3 3 3 3 3 3 ? ? ? ? ? ? ? ? - - - - - - -(((((($$$$$$,,,,,,		8	$	$bjMNN( C C C CM M M M M M M M M Mr!   