
    Hwg#/                        d dl Z d dlZd dlZd dl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 d d
lmZ  e j<                  d      Z G d de      Z  G d dejB                        Z"y)    N)ThreadSensitiveContextsync_to_async)settings)signals)RequestAbortedRequestDataTooBig)base)FileResponseHttpRequestHttpResponseHttpResponseBadRequestHttpResponseServerError	QueryDictparse_cookie)set_script_prefix)aclosing)cached_propertyzdjango.requestc                        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 | _        | j                   j	                  dd      | _        | j
                  r?|d   j                  | j
                        r!|d   t        | j
                        d  | _        n
|d   | _        | j
                  rA| j
                  j                  d      d| j                  j                  ddd      | _        n
|d   | _        | j                   d   j                         | _        | j                   j	                  dd      }t        |t              r|j!                         }| j                  || j
                  | j                  d	d	d
| _        | j                   j	                  d      rZ| j                   d   d   | j"                  d<   | j"                  d   | j"                  d<   | j                   d   d   | j"                  d<   | j                   j	                  d      rH| j                   d   d   | j"                  d<   t%        | j                   d   d         | j"                  d<   nd| j"                  d<   d| j"                  d<   | j                   j	                  dg       D ]  \  }}|j!                  d      }|dk(  rd}n+|dk(  rd}n#d|j                         j                  dd      z  }|j!                  d      }|| j"                  v r| j"                  |   dz   |z   }|| j"                  |<    | j'                  | j"                         || _        d | _        y )NF	root_path path/   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-_,)scope_post_parse_error_read_startedresolver_matchgetscript_name
startswithlen	path_inforstripreplacer   upperr   
isinstancebytesdecodeMETAstr_set_content_type_params_stream)selfr3   	body_filer   namevaluecorrected_names          P/var/www/horilla/myenv/lib/python3.12/site-packages/django/core/handlers/asgi.py__init__zASGIRequest.__init__&   s   
!&""::>>+r:f 8 89I9I J"6]3t/?/?+@+BCDN"6]DN   '',&&sB2DI
 fDIjj*002zz~~nb9lE*'..0L"kk(++ $!%
	 ::>>(#'+zz(';A'>DIIm$'+yy'?DIIm$'+zz(';A'>DIIm$::>>(#'+zz(';A'>DIIm$'*4::h+?+B'CDIIm$'0DIIm$'*DIIm$::>>)R8 	.KD%;;x(D''!1'!/!*TZZ\-A-A#s-K!K LL*E*		.1C7%?(-DIIn%	. 	%%dii0 "    c                 2    t        | j                  d         S )Nr    )r   rB   rF   s    rK   GETzASGIRequest.GETi   s    >233rM   c                 Z    | j                   j                  d      xs t        |          S )Nscheme)r3   r7   super_get_schemerF   	__class__s    rK   rT   zASGIRequest._get_schemem   s#    zz~~h'@57+>+@@rM   c                 R    t        | d      s| j                          | j                  S )N_post)hasattr_load_post_and_filesrX   rO   s    rK   	_get_postzASGIRequest._get_postp   s"    tW%%%'zzrM   c                     || _         y N)rX   )rF   posts     rK   	_set_postzASGIRequest._set_postu   s	    
rM   c                 R    t        | d      s| j                          | j                  S )N_files)rY   rZ   ra   rO   s    rK   
_get_fileszASGIRequest._get_filesx   s"    tX&%%'{{rM   c                 L    t        | j                  j                  dd            S )NHTTP_COOKIEr   )r   rB   r7   rO   s    rK   COOKIESzASGIRequest.COOKIES   s    DIIMM-<==rM   c                 V    t         |           | j                  j                          y r]   )rS   closerE   rU   s    rK   rg   zASGIRequest.close   s    rM   )__name__
__module____qualname____doc__body_receive_timeoutrL   r   rP   rT   r[   r_   rb   propertyPOSTFILESre   rg   __classcell__rV   s   @rK   r   r      st     A#F 4 4A

 Iy)DZ E> > rM   r   c                   h     e Zd ZdZeZdZ fdZd Zd Z	d Z
d Z fdZd	 Zed
        Zd Z xZS )ASGIHandlerzHandler for ASGI requests.i   c                 F    t         |           | j                  d       y )NT)is_async)rS   rL   load_middlewarerU   s    rK   rL   zASGIHandler.__init__   s    d+rM   c                    K   |d   dk7  rt        d|d   z        t               4 d{    | j                  |||       d{    ddd      d{    y7 07 7 	# 1 d{  7  sw Y   yxY ww)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)rF   r3   receivesends       rK   __call__zASGIHandler.__call__   s      =F"G%PV-W  *+ 	4 	4++eWd333	4 	4 	43	4 	4 	4 	4sV   (A6AA6A!AA!
A6AA6A!A6!A3'A*(A3/A6c                 t  K   	 | j                  |       d{   }t        | j                  |              t	        t
        j                  j                  d      | j                  |       d{    | j                  ||      \  }}|+|j                          | j                  ||       d{    y| j                  |       d{   }| j                  |_        t        |t              r| j                   |_        | j                  ||       d{    y7 # t        $ r Y yw xY w7 7 7 g7 w)zK
        Handles the ASGI request. Called via the __call__ method.
        NTthread_sensitive)senderr3   )	read_bodyr   r   get_script_prefixr   r   request_startedr}   rV   create_requestrg   send_responseget_response_async_handler_classr?   r
   
chunk_size
block_size)rF   r3   r|   r}   rG   requesterror_responseresponses           rK   r{   zASGIHandler.handle   s    
	"nnW55I 	$0078PmG33884P>>
 	
 	
 #'"5"5eY"G?OO$$^T:::0099"&.. h-"&//H  4000- 6 			
 ; : 	1s|   D8D! DD! AD81D02A D82D23D8D4AD8D6D8D! !	D-*D8,D--D82D84D86D8c                 :  K   t        j                  t        j                  d      }	  |        d{   }|d   dk(  r|j	                          t               d|v r|j                  |d          |j                  dd      sn]|j                  d	       |S 7 ew)
z+Reads an HTTP body from an ASGI connection.zw+b)max_sizemodeNrx   zhttp.disconnectbody	more_bodyFr   )	tempfileSpooledTemporaryFiler   FILE_UPLOAD_MAX_MEMORY_SIZErg   r   writer7   seek)rF   r|   rG   messages       rK   r   zASGIHandler.read_body   s      1199
	 #IoGv"33!$&& 0;;{E2  	q &s   1BBA&Bc                     	 | j                  ||      dfS # t        $ r; t        j                  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   )rF   r3   rG   s      rK   r   zASGIHandler.create_request   s    

	K%%eY7==! 	2NN2$c*  
 /111  	K&=cJJJ	Ks    AA3A32A3c                     	 t         |   |||      S # t        $ r4 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)rS   handle_uncaught_exception	Exceptionr   r   DEBUG	traceback
format_exc)rF   r   resolverr   rV   s       rK   r   z%ASGIHandler.handle_uncaught_exception   sP    	74WhQQ 	**2..	$$&>U) 	s    :AAc           	        K   g }|j                         D ]l  \  }}t        |t              r|j                  d      }t        |t              r|j                  d      }|j	                  t        |      t        |      f       n |j                  j                         D ]B  }|j	                  d|j                  d      j                  d      j                         f       D  |d|j                  |d       d{    |j                  r[t        |j                               4 d{   }|2 3 d{   }| j                  |      D ]  \  }	}
 |d	|	d
d       d{     7| j                  |j                        D ]  \  }	} |d	|	| d       d{      t!        |j"                  d
              d{    y7 7 7 7 j6 ddd      d{  7   n# 1 d{  7  sw Y   nxY w |dd	i       d{  7   i7 o7 Kw)z)Encode and send a response out over ASGI.asciir-   s
   Set-Cookier   )headerzhttp.response.start)rx   r   r,   Nzhttp.response.bodyT)rx   r   r   rx   r   )itemsr?   rC   encodeappendr@   cookiesvaluesoutputstripr   	streamingr   	__aiter__chunk_bytescontentr   rg   )rF   r   r}   response_headersr   rI   cr   partchunkr1   lasts               rK   r   zASGIHandler.send_response   s)     %^^- 	CMFE&#&w/%%X.##U6]E%L$AB	C !!((* 	A## 3 : :7 C I I KL	
 -"..+
 	
 	
 
   2 2 45  ") 
 
$$($4$4T$: 	q"(<(- .2  	  $//0@0@A t 4 %)-X   CmHNNTBDDDM	

 #*      45666
 	Es   C:H<G=-H*G+H.G 1G5G	6G9'G  G
!G (1HH	%H?H HH	GG G HGH G2&G)'G2.HHHHc              #      K   d}|s|df y|t        |      k  rN|||| j                  z    || j                  z   t        |      k\  f || j                  z  }|t        |      k  rMyyw)z
        Chunks some data up so it can be sent in reasonable size messages.
        Yields (chunk, last_chunk) tuples.
        r   TN)r:   r   )clsdatapositions      rK   r   zASGIHandler.chunk_bytes*  sy      *T"X3>> 9:CNN*s4y8  &H T"s   A&A+)A+c                 n    t         j                  rt         j                  S |j                  dd      xs dS )zU
        Return the script prefix to use from either the scope or a setting.
        r   r   )r   FORCE_SCRIPT_NAMEr7   )rF   r3   s     rK   r   zASGIHandler.get_script_prefix;  s/     %%---yyb)/R/rM   )rh   ri   rj   rk   r   r   r   rL   r~   r{   r   r   r   r   classmethodr   r   rp   rq   s   @rK   rs   rs      sO    $MJ,41<*K"
6Ep ' ' 0rM   rs   )#loggingr   r   r   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.asyncior   django.utils.functionalr   	getLoggerr   r   BaseHandlerrs    rM   rK   <module>r      sp     
   >    D %   * ) 3			+	,j+ jZx0$"" x0rM   