o
    ˜¨Êh.  ã                   @   sÐ   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 e  d
¡ZG dd„ deƒZG d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)Ú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rÐd}n|dkr×d}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© rG   úK/var/www/html/env/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>   ©rB   rG   rG   rH   ÚGETh   s   zASGIRequest.GETc                    s   | j  d¡p
tƒ  ¡ S )NÚscheme)r/   r3   ÚsuperÚ_get_schemerJ   ©Ú	__class__rG   rH   rN   l   s   zASGIRequest._get_schemec                 C   ó   t | dƒs	|  ¡  | jS )NÚ_post)ÚhasattrÚ_load_post_and_filesrR   rJ   rG   rG   rH   Ú	_get_posto   ó   
zASGIRequest._get_postc                 C   s
   || _ d S )N)rR   )rB   ÚpostrG   rG   rH   Ú	_set_postt   s   
zASGIRequest._set_postc                 C   rQ   )NÚ_files)rS   rT   rY   rJ   rG   rG   rH   Ú
_get_filesw   rV   zASGIRequest._get_filesc                 C   s   t | j dd¡ƒS )NÚHTTP_COOKIEr   )r   r>   r3   rJ   rG   rG   rH   ÚCOOKIES   s   zASGIRequest.COOKIES)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úbody_receive_timeoutrI   r   rK   rN   rU   rX   rZ   ÚpropertyÚPOSTÚFILESr\   Ú__classcell__rG   rG   rO   rH   r      s    C

r   c                       sp   e Zd ZdZeZdZ‡ fdd„Z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)rM   rI   Úload_middlewarerJ   rO   rG   rH   rI   ‹   s   
zASGIHandler.__init__c              	   Ã   sr   �|d dkrt d|d  ƒ‚tƒ 4 I dH š |  |||¡I dH  W d  ƒI dH  dS 1 I dH s2w   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)rB   r/   ÚreceiveÚsendrG   rG   rH   Ú__call__�   s   €
ÿ.ÿzASGIHandler.__call__c                 Ã   sê   �z
|   |¡I dH }W n
 ty   Y dS w zEt|  |¡ƒ ttjjdd�| j|d�I dH  |  	||¡\}}|du rJ|  
||¡I dH  W | ¡  dS |  |¡I dH }| j|_W | ¡  n| ¡  w t|tƒrj| j|_|  
||¡I dH  dS )zK
        Handles the ASGI request. Called via the __call__ method.
        NT©Úthread_sensitive)Úsenderr/   )Ú	read_bodyr   r   Úget_script_prefixr   r   Úrequest_startedrn   rP   Úcreate_requestÚsend_responseÚcloseÚget_response_asyncÚ_handler_classr;   r	   Ú
chunk_sizeÚ
block_size)rB   r/   rm   rn   rC   ÚrequestÚerror_responseÚresponserG   rG   rH   rl   �   s,   €ÿÿý

zASGIHandler.handlec                 Ã   sn   �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 an HTTP body from an ASGI connection.zw+b)Úmax_sizeÚmodeTNri   zhttp.disconnectÚbodyÚ	more_bodyFr   )	ÚtempfileÚSpooledTemporaryFiler   ÚFILE_UPLOAD_MAX_MEMORY_SIZErx   r   Úwriter3   Úseek)rB   rm   rC   ÚmessagerG   rG   rH   rs   ½   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   )rB   r/   rC   rG   rG   rH   rv   Ò   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)rM   Úhandle_uncaught_exceptionÚ	Exceptionr   r   ÚDEBUGÚ	tracebackÚ
format_exc)rB   r}   Úresolverr‹   rO   rG   rH   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)ri   r�   r(   Nzhttp.response.bodyT)ri   r‚   rƒ   ri   rp   )Úitemsr;   r?   ÚencodeÚappendr<   ÚcookiesÚvaluesÚoutputÚstriprŠ   Ú	streamingÚchunk_bytesÚcontentr   rx   )rB   r   rn   Úresponse_headersr›   rE   ÚcÚpartÚchunkr-   ÚlastrG   rG   rH   rw   ï   sJ   €



ÿý
ÿûÿÿýÿ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)r6   r{   )ÚclsÚdataÚpositionrG   rG   rH   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_NAMEr3   )rB   r/   rG   rG   rH   rt   5  s   zASGIHandler.get_script_prefix)r]   r^   r_   r`   r   rŽ   r{   rI   ro   rl   rs   rv   r”   rw   Úclassmethodr¤   rt   re   rG   rG   rO   rH   rf   „   s     5
rf   )!Ú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.functionalr   Ú	getLoggerr�   r   ÚBaseHandlerrf   rG   rG   rG   rH   Ú<module>   s    $	
i