
    ?wg	                        d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d d	lmZ d d
lmZ dgZ G d de      Z G d de      Zy)    )annotationsN)basename)urlparse)settings)finders)staticfiles_storage)FileResponse)
WhiteNoise)ensure_leading_trailing_slashWhiteNoiseMiddlewarec                      e Zd ZdZd Zy)WhiteNoiseFileResponsea  
    Wrap Django's FileResponse to prevent setting any default headers. For the
    most part these just duplicate work already done by WhiteNoise but in some
    cases (e.g. the content-disposition header introduced in Django 3.0) they
    are actively harmful.
    c                     y N )selfargskwargss      L/var/www/horilla/myenv/lib/python3.12/site-packages/whitenoise/middleware.pyset_headersz"WhiteNoiseFileResponse.set_headers   s        N)__name__
__module____qualname____doc__r   r   r   r   r   r      s    r   r   c                  `     e Zd ZdZdef fd	Zd Zed        Zd Z	 fdZ
d Zd	 Zd
 Z xZS )r   zh
    Wrap WhiteNoise to allow it to function as Django middleware, rather
    than WSGI middleware.
    Nc                   || _         	 |j                  }	 |j                  }	 |j
                  }	 |j                  }	 |j                  }	 |j                  }	 |j                  }		 |j                  }
t        | 1  d |||||||	|
	       	 |j                  | _        	 |j                  | _        t1        | j                         | _        |j2                  | _        | j4                  r'| j7                  | j4                  | j                          	 |j8                  }|r| j7                  |       | j                  r| j:                  s| j=                          y y y # t        $ r |j                  }Y Xw xY w# t        $ r |j                  rd}nd}Y lw xY w# t        $ r d}Y qw xY w# t        $ r d}Y vw xY w# t        $ r d }Y {w xY w# t        $ r d }Y w xY w# t        $ r d }	Y w xY w# t        $ r d }
Y w xY w# t        $ r |j                  | _        Y w xY w# t        $ r t#        |j$                  xs d      j&                  | _        |j(                  rS|j(                  j+                  d      }| j                   j-                  |      r| j                   t/        |      d  | _        Y w xY w# t        $ r d }Y w xY w)	Nr   <   Tzutf-8)	applicationautorefreshmax_ageallow_all_originscharset	mimetypesadd_headers_function
index_fileimmutable_file_test /)prefix)get_responseWHITENOISE_AUTOREFRESHAttributeErrorDEBUGWHITENOISE_MAX_AGEWHITENOISE_ALLOW_ALL_ORIGINSWHITENOISE_CHARSETWHITENOISE_MIMETYPESWHITENOISE_ADD_HEADERS_FUNCTIONWHITENOISE_INDEX_FILEWHITENOISE_IMMUTABLE_FILE_TESTsuper__init__WHITENOISE_USE_FINDERSuse_findersWHITENOISE_STATIC_PREFIXstatic_prefixr   
STATIC_URLpathFORCE_SCRIPT_NAMErstrip
startswithlenr   STATIC_ROOTstatic_root	add_filesWHITENOISE_ROOTr    add_files_from_finders)r   r+   r   r    r!   r"   r#   r$   r%   r&   r'   script_nameroot	__class__s                r   r7   zWhiteNoiseMiddleware.__init__$   s   (	) ( ? ?K	11G	% ( E E	11G	 55I	(#+#K#K 	!77J	'"*"I"I 	#/!5! 3 	 
	
	.'>>D	P!)!B!BD ;4;M;MN#//NN4++D4F4FNG	++D NN4 D$4$4'') %5Q  	)"..K	)  	~~		  	% $	%  	G	  	I	  	(#' 	(  	J	  	'"&	'"  	.'~~D	.
  	P!)(*=*=*C!D!I!ID))&88??D%%00=)-););C<L<N)OD&	P  	D	s   E E2 F F% F7 G	 G %G- 
G? H  J7 E/.E/2FFF"!F"%F43F47GG	GGG*)G*-G<;G<?HH BJ43J47KKc                    | j                   r| j                  |j                        }n%| j                  j	                  |j                        }|| j                  ||      S | j                  |      S r   )r    	find_file	path_infofilesgetserver+   )r   requeststatic_files      r   __call__zWhiteNoiseMiddleware.__call__t   s_    ..):):;K**..):):;K"::k733  ))r   c                    | j                  |j                  |j                        }t        |j                        }t        |j                  xs d|      }|d= |j                  D ]
  \  }}|||<    |S )Nr   )statuszcontent-type)r+   methodMETAintrT   r   fileheaders)rQ   rP   responserT   http_responsekeyvalues          r   rO   zWhiteNoiseMiddleware.serve}   sp    ++GNNGLLIX__%.x}}/B6R.)"** 	'JC!&M#	'r   c                   i }t        j                         D ]  }|j                  d       D ]y  \  }}t        |dd       xs dj	                  d      }dj                  | j                  ||rdnd|j                  dd      f      }|j                  ||j                  |             {  |j                         D ci c]  }|t        j                  |       }}|j                         D ]  \  }}| j                  |||        y c c}w )Nr*   r(   r)   \)
stat_cache)r   get_finderslistgetattrstripjoinr;   replace
setdefaultr=   valuesosstatitemsadd_file_to_dictionary)r   rM   finderr=   storager*   urlr`   s           r   rF   z+WhiteNoiseMiddleware.add_files_from_finders   s   ))+ 	:F!'T!2 :g!'8T:@bGGLgg**%2T3/	   gll4&89:	: 7<llnEddBGGDM)E
E 	JIC''Tj'I	J Fs   ;Dc              #     K   | j                   rL|j                  | j                        r1t        j                  |t        | j                        d        }|r| t        |   |      }|D ]  }|  y wr   )r9   r@   r;   r   findrA   r6   candidate_paths_for_url)r   ro   r=   pathsrI   s       r   rr   z,WhiteNoiseMiddleware.candidate_paths_for_url   sm     t/A/A B<<C(:(:$;$= >?D
/4 	DJ	s   A4A7c                    |j                  | j                        sy|t        | j                        d }| j                  |      }||k(  ry| j	                  |      }|rt        |      t        |      k(  ryy)z
        Determine whether given URL represents an immutable file (i.e. a
        file with a hash of its contents as part of its name) which can
        therefore be cached forever
        FNT)r@   r;   rA   get_name_without_hashget_static_urlr   )r   r=   ro   namename_without_hash
static_urls         r   r'   z(WhiteNoiseMiddleware.immutable_file_test   sz     ~~d0013t))*,- 66t<$$(():;
 (:.(3-?r   c                    t         j                  j                  |      \  }}t         j                  j                  |      d   }||z   S )aW  
        Removes the version hash from a filename e.g, transforms
        'css/application.f3ea4bcc2.css' into 'css/application.css'

        Note: this is specific to the naming scheme used by Django's
        CachedStaticFilesStorage. You may have to override this if
        you are using a different static files versioning system
        r   )ri   r=   splitext)r   filenamename_with_hashextrw   s        r   ru   z*WhiteNoiseMiddleware.get_name_without_hash   s@     !gg..x8ww/2czr   c                L    	 t        j                  |      S # t        $ r Y y w xY wr   )r   ro   
ValueError)r   rw   s     r   rv   z#WhiteNoiseMiddleware.get_static_url   s*    	&**400 		s    	##)r   r   r   r   r   r7   rR   staticmethodrO   rF   rr   r'   ru   rv   __classcell__)rI   s   @r   r   r      sI    
 %)8 N*`*  J&(r   )
__future__r   ri   	posixpathr   urllib.parser   django.confr   django.contrib.staticfilesr   "django.contrib.staticfiles.storager   django.httpr	   whitenoise.baser
   whitenoise.string_utilsr   __all__r   r   r   r   r   <module>r      sE    " 	  !   . B $ & A!
"	\ 	k: kr   