
    af5                     "   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	 d dl
mZ d dlmZ d dlmZmZ d dlmZ d dlmZmZmZmZmZmZmZ d d	lmZ d d
lmZ  ej        d          Z d Z! G d de          Z" G d dej#                  Z$dS )    N)aclosing)ThreadSensitiveContextsync_to_async)settings)signals)RequestAbortedRequestDataTooBig)base)FileResponseHttpRequestHttpResponseHttpResponseBadRequestHttpResponseServerError	QueryDictparse_cookie)set_script_prefix)cached_propertyzdjango.requestc                 b    t           j        rt           j        S |                     dd          pdS )zM
    Return the script prefix to use from either the scope or a setting.
    	root_path )r   FORCE_SCRIPT_NAMEget)scopes    K/var/www/html/env/lib/python3.11/site-packages/django/core/handlers/asgi.pyget_script_prefixr      s1     ! *))99["%%++    c                        e Zd ZdZdZd Zed             Z fdZd Z	d Z
d Z ee	e
          Z ee          Zed	             Z fd
Z xZS )ASGIRequestzv
    Custom request subclass that decodes from an ASGI-standard request dict
    and wraps request body handling.
    <   c                    || _         d| _        d| _        d | _        t	          |          | _        | j        r&|d                             | j                  | _        n|d         | _        | j        r?| j                            d          d| j        	                    ddd          | _
        n|d         | _
        | j         d                                         | _        | j                             dd          }t          |t                    r|                                }| j        || j        | j        ddd	| _        | j                             d
          rK| j         d
         d         | j        d<   | j        d         | j        d<   | j         d
         d         | j        d<   | j                             d          rD| j         d         d         | j        d<   t%          | j         d         d                   | j        d<   nd| j        d<   d| j        d<   | j                             dg           D ]\  }}|                    d          }|dk    rd}n4|dk    rd}n+d|                                	                    dd          z  }|                    d          }|| j        v r| j        |         dz   |z   }|| j        |<   |                     | j                   || _        d | _        d S )NFpath/r      methodquery_stringT)REQUEST_METHODQUERY_STRINGSCRIPT_NAME	PATH_INFOzwsgi.multithreadzwsgi.multiprocessclientr   REMOTE_ADDRREMOTE_HOSTREMOTE_PORTserverSERVER_NAMESERVER_PORTunknown0headerslatin1zcontent-lengthCONTENT_LENGTHzcontent-typeCONTENT_TYPEzHTTP_%s-_,)r   _post_parse_error_read_startedresolver_matchr   script_nameremoveprefix	path_inforstripreplacer!   upperr$   r   
isinstancebytesdecodeMETAstr_set_content_type_params_stream)selfr   	body_filer%   namevaluecorrected_names          r   __init__zASGIRequest.__init__0   s   
!&"",U33 	+"6]778HIIDNN"6]DN  	& '',,,,&&sB222DII
 fDIj*0022z~~nb99lE** 	1'..00L"k(+ $!%
 
	 :>>(## 	?'+z(';A'>DIm$'+y'?DIm$'+z(';A'>DIm$:>>(## 	+'+z(';A'>DIm$'*4:h+?+B'C'CDIm$$'0DIm$'*DIm$:>>)R88 	. 	.KD%;;x((D'''!1''!/!*TZZ\\-A-A#s-K-K!K LL**E**	.1C7%?(-DIn%%%%di000 "r   c                 6    t          | j        d                   S )Nr'   )r   rF   rJ   s    r   GETzASGIRequest.GETs   s    >2333r   c                 x    | j                             d          pt                                                      S )Nscheme)r   r   super_get_schemerJ   	__class__s    r   rV   zASGIRequest._get_schemew   s,    z~~h''@577+>+>+@+@@r   c                 X    t          | d          s|                                  | j        S )N_post)hasattr_load_post_and_filesrZ   rQ   s    r   	_get_postzASGIRequest._get_postz   s.    tW%% 	(%%'''zr   c                     || _         d S N)rZ   )rJ   posts     r   	_set_postzASGIRequest._set_post   s    


r   c                 X    t          | d          s|                                  | j        S )N_files)r[   r\   rc   rQ   s    r   
_get_fileszASGIRequest._get_files   s.    tX&& 	(%%'''{r   c                 R    t          | j                            dd                    S )NHTTP_COOKIEr   )r   rF   r   rQ   s    r   COOKIESzASGIRequest.COOKIES   s     DIMM-<<===r   c                 z    t                                                       | j                                         d S r_   )rU   closerI   rW   s    r   ri   zASGIRequest.close   s.    r   )__name__
__module____qualname____doc__body_receive_timeoutrO   r   rR   rV   r]   ra   rd   propertyPOSTFILESrg   ri   __classcell__rX   s   @r   r   r   &   s          A# A# A#F 4 4 _4A A A A A  
    
 8Iy))DHZ  E> > _>        r   r   c                   t     e Zd ZdZeZdZ fdZd Zd Z	d Z
d Zd Zd	 Z fd
Zd Zed             Z xZS )ASGIHandlerzHandler for ASGI requests.i   c                 t    t                                                       |                     d           d S )NT)is_async)rU   rO   load_middlewarerW   s    r   rO   zASGIHandler.__init__   s6    d+++++r   c                    K   |d         dk    rt          d|d         z            t                      4 d{V  |                     |||           d{V  ddd          d{V  dS # 1 d{V swxY w Y   dS )zV
        Async entrypoint - parses the request and hands off to get_response.
        typehttpz5Django can only handle ASGI/HTTP connections, not %s.N)
ValueErrorr   handle)rJ   r   receivesends       r   __call__zASGIHandler.__call__   s*      =F""G%PV-W   *++ 	4 	4 	4 	4 	4 	4 	4 	4++eWd333333333	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4s   A,,
A69A6c                    K   	                       |           d{V }n# t          $ r Y dS w xY wt          t          |                     t          j                             j        |           d{V                       ||          \  }}|2|	                                  
                    ||           d{V  dS  fd}t          j                             |                    t          j         |||                    g}t          j        |t          j                   d{V  |D ]}	|	                                rD	 |	                                 ,# t          $ r Y 8t$          $ r |	                                  w xY w|	                                 	 |	 d{V  x# t          j        $ r Y w xY w|	                                 dS )zK
        Handles the ASGI request. Called via the __call__ method.
        N)senderr   c                 z   K                        |            d {V }                    ||           d {V  d S r_   )run_get_responsesend_response)requestr   responserJ   s      r   process_requestz+ASGIHandler.handle.<locals>.process_request   sZ      !227;;;;;;;;H$$Xt44444444444r   )return_when)	read_bodyr   r   r   r   request_startedasendrX   create_requestri   r   asynciocreate_tasklisten_for_disconnectwaitFIRST_COMPLETEDdoneresultAssertionErrorcancelCancelledError)
rJ   r   r~   r   rK   r   error_responser   taskstasks
   `         r   r}   zASGIHandler.handle   sV     
	"nnW55555555II 	 	 	FF	 	+E22333%++4>+OOOOOOOOO"&"5"5eY"G"G?OO$$^T:::::::::F	5 	5 	5 	5 	5  : :7 C CDD > >??
 l5g.EFFFFFFFFFF 	 	Dyy{{ KKMMMM%   D%   OO%%%
 JJJJJJJJ-   D 	s0   ! 
//E%%
F1F(F11GGc                    K    |             d{V }|d         dk    rt                      J d|d         z              )z&Listen for disconnect from the client.Nrz   http.disconnectFz+Invalid ASGI message after request body: %s)r   )rJ   r~   messages      r   r   z!ASGIHandler.listen_for_disconnect   sP      		//////6?/// """UCgfoUUUur   c                    K   |                      |           d{V }| j        |_        t          |t                    r| j        |_        |S )zGet async response.N)get_response_asyncrX   _handler_classrC   r   
chunk_size
block_size)rJ   r   r   s      r   r   zASGIHandler.run_get_response   sZ       0099999999"&. h-- 	2"&/Hr   c                 `  K   t          j        t          j        d          }	  |             d{V }|d         dk    r"|                                 t                      d|v r|                    |d                    |                    dd	          snu|                    d
           |S )z+Reads an HTTP body from an ASGI connection.zw+b)max_sizemodeTNrz   r   body	more_bodyFr   )	tempfileSpooledTemporaryFiler   FILE_UPLOAD_MAX_MEMORY_SIZEri   r   writer   seek)rJ   r~   rK   r   s       r   r   zASGIHandler.read_body   s       19
 
 
		#GIIooooooGv"333!!!$&&&  000;;{E22 	 	qr   c                    	 |                      ||          dfS # t          $ rC t                              dt	          j                    ddi           dt                      fcY S t          $ r dt          dd          fcY S w xY w)	z
        Create the Request object and returns either (request, None) or
        (None, response) if there is an error response.
        Nz Bad Request (UnicodeDecodeError)status_codei  )exc_infoextraz413 Payload too largei  )status)	request_classUnicodeDecodeErrorloggerwarningsysr   r   r	   r   )rJ   r   rK   s      r   r   zASGIHandler.create_request
  s    

	K%%eY77==! 	2 	2 	2NN2$c*    
 /111111  	K 	K 	K&=cJJJJJJJ	Ks    A
B&BBc                     	 t                                          |||          S # t          $ r3 t          t          j        rt          j                    ndd          cY S w xY w)z#Last-chance handler for exceptions.zInternal Server Errorz
text/plain)content_type)rU   handle_uncaught_exception	Exceptionr   r   DEBUG	traceback
format_exc)rJ   r   resolverr   rX   s       r   r   z%ASGIHandler.handle_uncaught_exception  s|    	7744WhQQQ 	 	 	**2.U	$&&&>U)     	s   "& :A#"A#c           	      F  K   g }|                                 D ]\  }}t          |t                    r|                    d          }t          |t                    r|                    d          }|                    t          |          t          |          f           |j                                        D ]R}|                    d|                    d                              d          	                                f           S |d|j
        |d           d{V  |j        rt          t          |                    4 d{V }|2 3 d{V }|                     |          D ]\  }	}
 |d	|	d
d           d{V  86 	 ddd          d{V  n# 1 d{V swxY w Y    |dd	i           d{V  n6|                     |j                  D ]\  }	} |d	|	| d           d{V   t!          |j        d
                       d{V  dS )z)Encode and send a response out over ASGI.asciir4   s
   Set-Cookier   )headerzhttp.response.start)rz   r   r3   Nzhttp.response.bodyT)rz   r   r   rz   )thread_sensitive)itemsrC   rG   encodeappendrD   cookiesvaluesoutputstripr   	streamingr   aiterchunk_bytescontentr   ri   )rJ   r   r   response_headersr   rM   cr   partchunkr8   lasts               r   r   zASGIHandler.send_response'  s      %^^-- 	C 	CMFE&#&& 0w//%%% /X..##U6]]E%LL$ABBBB!((** 	 	A## 3 3 : :7 C C I I K KL    d-".+ 
 
 	
 	
 	
 	
 	
 	
 	
  	
  h00       G") 
 
 
 
 
 
 
$$($4$4T$:$: 	 	q"d(<(- .2          	 #*'                           $ 456666666666  $//0@AA  td 4 %)-X           CmHNTBBBDDDDDDDDDDDs   F"F3F""
F,/F,c              #      K   d}|s|dfV  dS |t          |          k     rN|||| j        z            || j        z   t          |          k    fV  || j        z  }|t          |          k     LdS dS )z
        Chunks some data up so it can be sent in reasonable size messages.
        Yields (chunk, last_chunk) tuples.
        r   TN)lenr   )clsdatapositions      r   r   zASGIHandler.chunk_bytes_  s        	*FT""X3> 99:CN*s4yy8    &H T""""""r   )rj   rk   rl   rm   r   r   r   rO   r   r}   r   r   r   r   r   r   classmethodr   rr   rs   s   @r   ru   ru      s        $$MJ, , , , ,4 4 44 4 4lV V V	 	 	  *K K K"
 
 
 
 
6E 6E 6Ep ' ' [' ' ' ' 'r   ru   )%r   loggingr   r   r   
contextlibr   asgiref.syncr   r   django.confr   django.corer   django.core.exceptionsr   r	   django.core.handlersr
   django.httpr   r   r   r   r   r   r   django.urlsr   django.utils.functionalr   	getLoggerr   r   r   BaseHandlerru    r   r   <module>r      s     



            > > > > > > > >                   D D D D D D D D % % % % % %                  * ) ) ) ) ) 3 3 3 3 3 3		+	,	,, , ,j j j j j+ j j jZ[' [' [' [' ['$" [' [' [' [' ['r   