
    kKd#/                        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j!                  Z"dS )    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                             dd          | _        | j        rH|d                             | j                  r(|d         t          | j                  d          | _        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	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          ?F:\djangOuth\env\Lib\site-packages\django/core/handlers/asgi.py__init__zASGIRequest.__init__&   s   
!&"":>>+r:: 	+f 8 89I J J 	+"6]3t/?+@+@+B+BCDNN"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 "    c                 6    t          | j        d                   S )Nr    )r   rB   rF   s    rK   GETzASGIRequest.GETi   s    >2333rM   c                 x    | j                             d          pt                                                      S )Nscheme)r3   r7   super_get_schemerF   	__class__s    rK   rT   zASGIRequest._get_schemem   s,    z~~h''@577+>+>+@+@@rM   c                 X    t          | d          s|                                  | j        S )N_post)hasattr_load_post_and_filesrX   rO   s    rK   	_get_postzASGIRequest._get_postp   s.    tW%% 	(%%'''zrM   c                     || _         d S N)rX   )rF   posts     rK   	_set_postzASGIRequest._set_postu   s    


rM   c                 X    t          | d          s|                                  | j        S )N_files)rY   rZ   ra   rO   s    rK   
_get_fileszASGIRequest._get_filesx   s.    tX&& 	(%%'''{rM   c                 R    t          | j                            dd                    S )NHTTP_COOKIEr   )r   rB   r7   rO   s    rK   COOKIESzASGIRequest.COOKIES   s     DIMM-<<===rM   c                 z    t                                                       | j                                         d S 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      s          A# A# A#F 4 4 _4A A A A A  
    
 8Iy))DHZ  E> > _>        rM   r   c                   n     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                 t    t                                                       |                     d           d S )NT)is_async)rS   rL   load_middlewarerU   s    rK   rL   zASGIHandler.__init__   s6    d+++++rM   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)rF   r3   receivesends       rK   __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                 x  K   	 |                      |           d{V }n# t          $ r Y dS w xY wt          |                     |                      t	          t
          j        j        d          | j        |           d{V  | 	                    ||          \  }}|2|
                                 |                     ||           d{V  dS |                     |           d{V }| j        |_        t          |t                    r| j        |_        |                     ||           d{V  dS )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5555555II 	 	 	FF	 	$0077888PmG384PPP>
 
 
 	
 	
 	
 	
 	
 	
 	
 #'"5"5eY"G"G?OO$$^T:::::::::F0099999999"&. h-- 	2"&/H  400000000000s     
..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modeTNrx   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9
 
 
		#GIIooooooGv"333!!!$&&&  000;;{E22 	 	qrM   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   )rF   r3   rG   s      rK   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)rS   handle_uncaught_exception	Exceptionr   r   DEBUG	traceback
format_exc)rF   r   resolverr   rV   s       rK   r   z%ASGIHandler.handle_uncaught_exception   s|    	7744WhQQQ 	 	 	**2.U	$&&&>U)     	s   "& :A#"A#c           	      P  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          |                                          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.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 	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-".+ 
 
 	
 	
 	
 	
 	
 	
 	
  	
   2 2 4 455       ") 
 
 
 
 
 
 
$$($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''
F14F1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)r:   r   )clsdatapositions      rK   r   zASGIHandler.chunk_bytes*  s        	*FT""X3> 99:CN*s4yy8    &H T""""""rM   c                 b    t           j        rt           j        S |                    dd          p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;  s1     % 	.--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      s        $$MJ, , , , ,4 4 41 1 1<  *K K K"
 
 
 
 
6E 6E 6Ep ' ' [' 0 0 0 0 0 0 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      s    



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