o
    ^"<f+                     @   s   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 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 d
ZG dd deZG dd dejZdS )    N)sync_to_async)settings)signals)RequestAbortedRequestDataTooBig)base)FileResponseHttpRequestHttpResponseHttpResponseBadRequestHttpResponseServerError	QueryDictparse_cookie)set_script_prefix)cached_propertyzdjango.requestc                       sn   e Zd ZdZdZdd Zedd Z fddZd	d
 Z	dd Z
dd Zee	e
ZeeZedd Z  ZS )ASGIRequestzv
    Custom request subclass that decodes from an ASGI-standard request dict
    and wraps request body handling.
    <   c                 C   s  || _ d| _d| _d | _| j dd| _| jr,|d | jr,|d t| jd  | _n|d | _| jrFd| j	d| j
dddf | _n|d | _| j d  | _| j d	d}t|trc| }| j|| j| jd
d
d| _| j dr| j d d | jd< | jd | jd< | j d d | jd< | j dr| j d d | jd< t| j d d | jd< n
d| jd< d| jd< | j dg D ]9\}}|d}|dkrd}n|dkrd}n
d| 
dd }|d}|| jv r| j| d | }|| j|< q| | j || _d | _d S ) NF	root_path pathz%s/%s/   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)selfr.   	body_filer   namevaluecorrected_name rF   P/var/www/html/kck/venv/lib/python3.10/site-packages/django/core/handlers/asgi.py__init__   s`   




	





zASGIRequest.__init__c                 C   s   t | jd S )Nr   )r   r=   rA   rF   rF   rG   GETb   s   zASGIRequest.GETc                    s   | j dp
t  S )Nscheme)r.   r2   super_get_schemerI   	__class__rF   rG   rM   f   s   zASGIRequest._get_schemec                 C      t | ds	|   | jS )N_post)hasattr_load_post_and_filesrQ   rI   rF   rF   rG   	_get_posti      
zASGIRequest._get_postc                 C   s
   || _ d S )N)rQ   )rA   postrF   rF   rG   	_set_postn   s   
zASGIRequest._set_postc                 C   rP   )N_files)rR   rS   rX   rI   rF   rF   rG   
_get_filesq   rU   zASGIRequest._get_filesc                 C   s   t | jddS )NHTTP_COOKIEr   )r   r=   r2   rI   rF   rF   rG   COOKIESy   s   zASGIRequest.COOKIES)__name__
__module____qualname____doc__body_receive_timeoutrH   r   rJ   rM   rT   rW   rY   propertyPOSTFILESr[   __classcell__rF   rF   rN   rG   r      s    C

r   c                       sh   e Zd ZdZeZdZ fddZdd Zdd Z	d	d
 Z
 fddZdd Zedd Zdd Z  ZS )ASGIHandlerzHandler for ASGI requests.i   c                    s   t    | jdd d S )NT)is_async)rL   rH   load_middlewarerI   rN   rF   rG   rH      s   
zASGIHandler.__init__c                    s   |d dkrt d|d  z
| |I dH }W n
 ty#   Y dS w t| | ttjjdd| j	|dI dH  | 
||\}}|du rR| ||I dH  dS | |I dH }| j	|_t|trg| j|_| ||I dH  dS )zV
        Async entrypoint - parses the request and hands off to get_response.
        typehttpz5Django can only handle ASGI/HTTP connections, not %s.NTthread_sensitive)senderr.   )
ValueError	read_bodyr   r   get_script_prefixr   r   request_startedsendrO   create_requestsend_responseget_response_async_handler_classr:   r   
chunk_size
block_size)rA   r.   receiverq   rB   requesterror_responseresponserF   rF   rG   __call__   s.    
zASGIHandler.__call__c                    sf   t jtjdd}	 | I dH }|d dkrt d|v r$||d  |dd	s+nq
|d
 |S )z*Reads a HTTP body from an ASGI connection.zw+b)max_sizemodeTNrh   zhttp.disconnectbody	more_bodyFr   )tempfileSpooledTemporaryFiler   FILE_UPLOAD_MAX_MEMORY_SIZEr   writer2   seek)rA   rx   rB   messagerF   rF   rG   rn      s   
zASGIHandler.read_bodyc                 C   sh   z	|  ||dfW S  ty#   tjdt ddid dt f Y S  ty3   dtdddf Y S 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
   )rA   r.   rB   rF   rF   rG   rr      s   zASGIHandler.create_requestc                    s@   z	t  |||W S  ty   ttjrt nddd Y S w )z#Last-chance handler for exceptions.zInternal Server Errorz
text/plain)content_type)rL   handle_uncaught_exception	Exceptionr   r   DEBUG	traceback
format_exc)rA   ry   resolverr   rN   rF   rG   r      s   
z%ASGIHandler.handle_uncaught_exceptionc                    s4  g }|  D ]#\}}t|tr|d}t|tr|d}|t|t|f q|j D ]}|d|jddd	 f q0|d|j
|dI dH  |jrv|D ]}| |D ]\}}	|d	|d
dI dH  q[qT|dd	iI dH  n| |jD ]\}}
|d	||
 dI dH  q|t|jd
d I dH  dS )z)Encode and send a response out over ASGI.asciir(   s
   Set-Cookier   )headerzhttp.response.start)rh   r   r'   Nzhttp.response.bodyT)rh   r   r   rh   rj   )itemsr:   r>   encodeappendr;   cookiesvaluesoutputstripr   	streamingchunk_bytescontentr   close)rA   r{   rq   response_headersr   rD   cpartchunkr,   lastrF   rF   rG   rs      sD   



	zASGIHandler.send_responsec                 c   sh    d}|s|dfV  dS |t |k r2|||| j  || j t |kfV  || j7 }|t |k sdS dS )z
        Chunks some data up so it can be sent in reasonable size messages.
        Yields (chunk, last_chunk) tuples.
        r   TN)r5   rv   )clsdatapositionrF   rF   rG   r     s   

zASGIHandler.chunk_bytesc                 C   s   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_NAMEr2   )rA   r.   rF   rF   rG   ro     s   zASGIHandler.get_script_prefix)r\   r]   r^   r_   r   r   rv   rH   r|   rn   rr   r   rs   classmethodr   ro   rd   rF   rF   rN   rG   re   ~   s    "/
re   ) loggingr   r   r   asgiref.syncr   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   BaseHandlerre   rF   rF   rF   rG   <module>   s    $
h