
    h@                     4   S r SSKrSSKJr  SSKJr  SSKJr  SSKJ	r	  SSK
JrJr  SSKJrJrJrJr  SS	KJr  SS
KJr  SSKJr  SSKJr  \" S5      rS rS rS rS rS"S jrS#S jr S r!S r"S r#S r$S"S jr%S r&S r'S r(S r)S r*S r+S$S jr,S#S  jr-S! r.g)%a  
This module contains helper functions for controlling caching. It does so by
managing the "Vary" header of responses. It includes functions to patch the
header of response objects directly and decorators that change functions to do
that header-patching themselves.

For information on the Vary header, see RFC 9110 Section 12.5.5.

Essentially, the "Vary" HTTP header defines which headers a cache should take
into account when building its cache key. Requests with the same path but
different header content for headers named in "Vary" need to get different
cache keys to prevent delivery of wrong content.

An example: i18n middleware would need to distinguish caches by the
"Accept-language" header.
    N)defaultdict)md5)settings)caches)HttpResponseHttpResponseNotModified)	http_dateparse_etagsparse_http_date_safe
quote_etag)log_response)_lazy_re_compile)get_current_timezone_name)get_languagez\s*,\s*c           
      ^   S nS n[        [        5      nU R                  S5      (       aR  [        R	                  U R
                  S   5       H,  nU" U5      u  pgUS:X  a  XF   R                  U5        M(  XtU'   M.     SU;   a$  SU;   a  [        [        US   5      US   5      US'   SU;   a
  SU;   a  US	 OSU;   a	  SU;   a  US	 UR                  5        H6  u  pUR                  S	S
5      nUS:X  a  XF   R                  U	5        M2  XU'   M8     / n
UR                  5        Hc  u  pk[        U[        5      (       a2  SU;   a  S1nU
R                  U Vs/ s H
  os" Xg5      PM     sn5        ML  U
R                  U" Xk5      5        Me     SR                  U
5      nX@R
                  S'   gs  snf )a  
Patch the Cache-Control header by adding all keyword arguments to it.
The transformation is as follows:

* All keyword parameter names are turned to lowercase, and underscores
  are converted to hyphens.
* If the value of a parameter is True (exactly True, not just a
  true value), only the parameter name is added to the header.
* All other parameters are added with their value, after applying
  str() to it.
c                     U R                  SS5      n[        U5      S:  a  US   R                  5       US   4$ US   R                  5       S4$ )N=   r   Tsplitlenlowersts     D/var/www/html/env/lib/python3.13/site-packages/django/utils/cache.pydictitem%patch_cache_control.<locals>.dictitem/   sJ    GGCOq6A:aDJJL!A$''aDJJL$''    c                  :    U S   SL a  U S   $ U S   < SU S   < 3$ )Nr   Tr   r    )r   s    r   	dictvalue&patch_cache_control.<locals>.dictvalue6   s)    Q44<Q4KdAaD))r   Cache-Controlzno-cachemax-agemax_ageprivatepublic_-T, N)r   setgetcc_delim_rer   headersaddminintitemsreplace
isinstanceextendappendjoin)responsekwargsr   r"   ccfield	directivevaluekv
directivesvaluess               r   patch_cache_controlrC   "   s   (* 
S	B||O$$ &&x'7'7'HIE'IJ&!!%( %9 J B9.ByM 2F94EFy B8v-yM	RI/xLIIc3'	
"Ma yM  JXXZ	fc""v~Ouy:OPi	:; ( 
:	B(*_%	 Ps   F*
c                     U R                  S5      (       d  g[        S [        R                  U R                  S   5       5       5      n [        US   5      $ ! [        [        [        4 a     gf = f)z{
Return the max-age from the response Cache-Control header as an integer,
or None if it wasn't found or wasn't an integer.
r$   Nc              3   8   #    U  H  n[        U5      v   M     g 7fN)	_to_tuple).0els     r   	<genexpr>get_max_age.<locals>.<genexpr>n   s       T"	" Ts   r%   )	
has_headerdictr.   r   r/   r2   
ValueError	TypeErrorKeyError)r9   r;   s     r   get_max_agerQ   g   sr    
 //	  + 1 1(2B2B?2S T 
B2i=!!	8, s   A A10A1c                     U R                   (       dI  U R                  (       a8  [        [        U R                  SS9R	                  5       5      U R
                  S'   U $ )NFusedforsecurityETag)	streamingcontentr   r   	hexdigestr/   r9   s    r   set_response_etagrZ   w   sH    ("2"2#-  %8BBD$
  Or   c                 D    [        SS9n[        SU R                  UU S9  U$ )Ni  )statuszPrecondition Failed: %s)r9   request)r   r   path)r]   r9   s     r   _precondition_failedr_      s+    3'H!	 Or   c                     [        5       nU(       a=  S H&  nX1;   d  M
  UR                  U   UR                  U'   M(     UR                  Ul        U$ )N)r$   zContent-LocationDaterU   ExpireszLast-ModifiedVary)r   r/   cookies)r]   r9   new_responseheaders       r   _not_modifiedrg      sR    *,L
F !/7/?/?/G$$V,
"  (//r   c                     U(       a  SUR                   s=::  a  S:  d   U$   U$ [        U R                  R                  SS5      5      nU R                  R                  S5      nU=(       a    [	        U5      n[        U R                  R                  SS5      5      nU R                  R                  S5      nU=(       a    [	        U5      nU(       a  [        X5      (       d  [        U 5      $ U(       d"  U(       a  [        X%5      (       d  [        U 5      $ U(       a6  [        X5      (       d&  U R                  S;   a  [        X5      $ [        U 5      $ U(       d2  U(       a+  [        X'5      (       d  U R                  S;   a  [        X5      $ U$ )	N   i,  HTTP_IF_MATCH HTTP_IF_UNMODIFIED_SINCEHTTP_IF_NONE_MATCHHTTP_IF_MODIFIED_SINCE)GETHEAD)status_coder
   METAr-   r   _if_match_passesr_   _if_unmodified_since_passes_if_none_match_passesmethodrg   _if_modified_since_passes)r]   etaglast_modifiedr9   if_match_etagsif_unmodified_sinceif_none_match_etagsif_modified_sinces           r   get_conditional_responser~      sR    4 4:s: ; !!1!1/2!FGN!,,**+EF- 2F3 &gll&6&67KR&PQ(()AB)U.BCT.U
 .tDD#G,, +MOO#G,, #8#S#S>>_, 33'00  )-KKNNo-W// Or   c                 X    U (       d  gUS/:X  a  gU R                  S5      (       a  gX;   $ )zE
Test the If-Match comparison as defined in RFC 9110 Section 13.1.1.
F*TW/)
startswithtarget_etagetagss     r   rs   rs      s7     	3% 				%	% ##r   c                     U =(       a    X:*  $ )zP
Test the If-Unmodified-Since comparison as defined in RFC 9110 Section
13.1.4.
r!   )ry   r{   s     r   rt   rt      s    
 A]AAr   c                 ^    U (       d  gUS/:X  a  gU R                  S5      n S U 5       nX;  $ )zJ
Test the If-None-Match comparison as defined in RFC 9110 Section 13.1.2.
Tr   Fr   c              3   B   #    U  H  oR                  S 5      v   M     g7f)r   Nstrip)rH   rx   s     r   rJ   (_if_none_match_passes.<locals>.<genexpr>  s     4edD!!es   r   r   s     r   ru   ru      s=     	3%  "''-4e4''r   c                 &    U (       + =(       d    X:  $ )zN
Test the If-Modified-Since comparison as defined in RFC 9110 Section
13.1.3.
r!   )ry   r}   s     r   rw   rw     s    
 A AAr   c                     Uc  [         R                  nUS:  a  SnU R                  S5      (       d.  [        [        R                  " 5       U-   5      U R
                  S'   [        XS9  g)z
Add HTTP caching headers to the given HttpResponse: Expires and
Cache-Control.

Each header is only added if it isn't already set.

cache_timeout is in seconds. The CACHE_MIDDLEWARE_SECONDS setting is used
by default.
Nr   rb   )r&   )r   CACHE_MIDDLEWARE_SECONDSrL   r	   timer/   rC   )r9   cache_timeouts     r   patch_response_headersr     s[      99qy))&/		m0K&L#8r   c                 2    [        U SS9  [        U SSSSS9  g)zK
Add headers to a response to indicate that a page should never be cached.
)r   T)no_cacheno_storemust_revalidater'   N)r   rC   rY   s    r   add_never_cache_headersr   !  s!     8264$dr   c                    U R                  S5      (       a#  [        R                  U R                  S   5      nO/ nU Vs1 s H  o3R	                  5       iM     nnU Vs/ s H  nUR	                  5       U;  d  M  UPM     nnX&-  nSU;   a  SU R                  S'   gSR                  U5      U R                  S'   gs  snf s  snf )a  
Add (or update) the "Vary" header in the given HttpResponse object.
newheaders is a list of header names that should be in "Vary". If headers
contains an asterisk, then "Vary" header will consist of a single asterisk
'*'. Otherwise, existing headers in "Vary" aren't removed.
rc   r   r+   N)rL   r.   r   r/   r   r8   )r9   
newheadersvary_headersrf   existing_headers	newheaderadditional_headerss          r   patch_vary_headersr   +  s     6"""(()9)9&)AB5AB\6\B $#I??$44 	#  
 &L
l#& #'99\#:  Cs    B<C;Cc                     U R                  S5      (       d  g[        R                  U R                  S   5      nU Vs1 s H  o3R	                  5       iM     nnUR	                  5       U;   $ s  snf )zJ
Check to see if the response has a given header name in its Vary header.
rc   F)rL   r.   r   r/   r   )r9   header_queryr   rf   r   s        r   has_vary_headerr   G  sg     v&&$$X%5%5f%=>L5AB\6\B#333 Cs   A,c                     [         R                  (       a  US[        U S[        5       5      -  -  n[         R                  (       a  US[        5       -  -  nU$ )zCIf necessary, add the current locale or time zone to the cache key.z.%sLANGUAGE_CODE)r   USE_I18Ngetattrr   USE_TZr   )r]   	cache_keys     r   _i18n_cache_key_suffixr   R  sG     	UWWo|~NNN	U6888	r   c           	      d   [        SS9nU HB  nU R                  R                  U5      nUc  M#  UR                  UR	                  5       5        MD     [        U R                  5       R	                  S5      SS9nSU< SU< SUR                  5       < SUR                  5       < 3n[        X5      $ )z=Return a cache key from the headers given in the header list.FrS   asciiz"views.decorators.cache.cache_page..)r   rr   r-   updateencodebuild_absolute_urirX   r   )	r]   rv   
headerlist
key_prefixctxrf   r>   urlr   s	            r   _generate_cache_keyr   ^  s    
e
$C  (JJu||~&  g((*11':E
RC	I "'55r   c                     [        UR                  5       R                  S5      SS9nSU < SUR                  5       < 3n[	        X5      $ )z(Return a cache key for the header cache.r   FrS   z$views.decorators.cache.cache_header.r   )r   r   r   rX   r   )r   r]   r   r   s       r   _generate_cache_header_keyr   o  sB    
g((*11':E
RCI "'55r   c                     Uc  [         R                  n[        X5      nUc  [        [         R                     nUR                  U5      nUb  [        XXQ5      $ g)aI  
Return a cache key based on the request URL and query. It can be used
in the request phase because it pulls the list of headers to take into
account from the global URL registry and uses those to build a cache key
to check against.

If there isn't a headerlist stored, return None, indicating that the page
needs to be rebuilt.
N)r   CACHE_MIDDLEWARE_KEY_PREFIXr   r   CACHE_MIDDLEWARE_ALIASr-   r   )r]   r   rv   cacher   r   s         r   get_cache_keyr   y  sX     99
*:?I}x6679%J"7JKKr   c                    Uc  [         R                  nUc  [         R                  n[        X05      nUc  [        [         R
                     nUR                  S5      (       a  [         R                  n/ n[        R                  UR                  S   5       HF  nUR                  5       R                  SS5      nUS:w  d	  U(       a  M2  UR                  SU-   5        MH     UR                  5         UR                  XWU5        [!        X R"                  Xs5      $ UR                  U/ U5        [!        X R"                  / U5      $ )a  
Learn what headers to take into account for some request URL from the
response object. Store those headers in a global URL registry so that
later access to that URL will know what headers to take into account
without building the response object itself. The headers are named in the
Vary header of the response, but we want to prevent response generation.

The list of headers to use for cache key generation is stored in the same
cache as the pages themselves. If the cache ages some data out of the
cache, this just means that we have to build the response once to get at
the Vary header and so at the list of headers to use for the cache key.
rc   r*   r)   ACCEPT_LANGUAGEHTTP_)r   r   r   r   r   r   rL   r   r.   r   r/   upperr4   r7   sortr,   r   rv   )	r]   r9   r   r   r   r   is_accept_language_redundantr   rf   s	            r   learn_cache_keyr     s    99
 99*:?I}x6676""'/'8'8$
 
!''(8(8(@AF\\^++C5F**2N2N!!'F"23 B 			)7"7NNJSS 			)R/"7NNB
KKr   c                     U R                  SS5      n[        U5      S:X  a  US   R                  5       US   4$ US   R                  5       S4$ )Nr   r      r   Tr   r   s     r   rG   rG     sJ    	QA
1v{tzz|QqT!!Q4::<r   rF   )NNN)Nro   N)/__doc__r   collectionsr   hashlibr   django.confr   django.core.cacher   django.httpr   r   django.utils.httpr	   r
   r   r   django.utils.logr   django.utils.regex_helperr   django.utils.timezoner   django.utils.translationr   r.   rC   rQ   rZ   r_   rg   r~   rs   rt   ru   rw   r   r   r   r   r   r   r   r   r   rG   r!   r   r   <module>r      s   "  #    $ = V V ) 6 ; 1z*B+J 4/d$*B(&B9&;84	6"6,&LRr   