a
    Lcòf#/  ã                   @   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 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)Úaclosing)Úcached_propertyzdjango.requestc                       sz   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‡ f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rX|d  | j¡rX|d t| jƒd … | _n
|d | _| jrŒd| j 	d¡| j 
ddd¡f | _n
|d | _| j d  ¡ | _| j  d	d¡}t|tƒrÆ| ¡ }| 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¡�rb| j d d | jd< t| j d d ƒ| jd< nd| jd< d| jd< | j  dg ¡D ]z\}}| 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_STRINGZSCRIPT_NAMEZ	PATH_INFOzwsgi.multithreadzwsgi.multiprocessÚclientr   ZREMOTE_ADDRZREMOTE_HOSTZREMOTE_PORTÚserverZSERVER_NAMEZSERVER_PORTÚunknownÚ0ÚheadersÚlatin1zcontent-lengthÚCONTENT_LENGTHzcontent-typeÚCONTENT_TYPEzHTTP_%sú-Ú_ú,)ÚscopeZ_post_parse_errorZ_read_startedZresolver_matchÚgetÚscript_nameÚ
startswithÚlenZ	path_infoÚrstripÚreplacer   Úupperr   Ú
isinstanceÚbytesÚdecodeÚMETAÚstrZ_set_content_type_paramsÚ_stream)Úselfr)   Ú	body_filer   ÚnameÚvalueZcorrected_name© r;   úV/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/django/core/handlers/asgi.pyÚ__init__&   s`    

þ


ù	





zASGIRequest.__init__c                 C   s   t | jd ƒS )Nr   )r   r4   ©r7   r;   r;   r<   ÚGETi   s    zASGIRequest.GETc                    s   | j  d¡ptƒ  ¡ S )NÚscheme)r)   r*   ÚsuperÚ_get_schemer>   ©Ú	__class__r;   r<   rB   m   s    zASGIRequest._get_schemec                 C   s   t | dƒs|  ¡  | jS )NÚ_post)ÚhasattrÚ_load_post_and_filesrE   r>   r;   r;   r<   Ú	_get_postp   s    
zASGIRequest._get_postc                 C   s
   || _ d S ©N)rE   )r7   Úpostr;   r;   r<   Ú	_set_postu   s    zASGIRequest._set_postc                 C   s   t | dƒs|  ¡  | jS )NÚ_files)rF   rG   rL   r>   r;   r;   r<   Ú
_get_filesx   s    
zASGIRequest._get_filesc                 C   s   t | j dd¡ƒS )NZHTTP_COOKIEr   )r   r4   r*   r>   r;   r;   r<   ÚCOOKIES€   s    zASGIRequest.COOKIESc                    s   t ƒ  ¡  | j ¡  d S rI   )rA   Úcloser6   r>   rC   r;   r<   rO   „   s    
zASGIRequest.close)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zbody_receive_timeoutr=   r   r?   rB   rH   rK   rM   ÚpropertyÚPOSTZFILESrN   rO   Ú__classcell__r;   r;   rC   r<   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)rA   r=   Zload_middlewarer>   rC   r;   r<   r=   �   s    
zASGIHandler.__init__c              	   Ã   sn   |d dkrt d|d  ƒ‚tƒ 4 I dH š* |  |||¡I dH  W d  ƒI dH  qj1 I dH s`0    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)r7   r)   ÚreceiveÚsendr;   r;   r<   Ú__call__”   s    
ÿzASGIHandler.__call__c                 Ã   sÎ   z|   |¡I dH }W n ty(   Y dS 0 t|  |¡ƒ ttjjdd�| j|d�I dH  |  	||¡\}}|du rŽ| 
¡  |  ||¡I dH  dS |  |¡I dH }| j|_t|tƒr¸| j|_|  ||¡I dH  dS )zK
        Handles the ASGI request. Called via the __call__ method.
        NT©Zthread_sensitive)Zsenderr)   )Ú	read_bodyr   r   Úget_script_prefixr   r   Zrequest_startedr^   rD   Úcreate_requestrO   Úsend_responseZget_response_asyncZ_handler_classr1   r	   Ú
chunk_sizeÚ
block_size)r7   r)   r]   r^   r8   ÚrequestZerror_responseÚresponser;   r;   r<   r\   ¢   s$    ÿ
zASGIHandler.handlec                 Ã   sj   t jtjdd�}|ƒ I dH }|d dkr6| ¡  tƒ ‚d|v rL| |d ¡ | dd¡sq\q| d	¡ |S )
z+Reads an HTTP body from an ASGI connection.zw+b)Úmax_sizeÚmodeNrY   zhttp.disconnectÚbodyÚ	more_bodyFr   )	ÚtempfileÚSpooledTemporaryFiler   ZFILE_UPLOAD_MAX_MEMORY_SIZErO   r   Úwriter*   Úseek)r7   r]   r8   Úmessager;   r;   r<   ra   À   s    ÿ
zASGIHandler.read_bodyc                 C   sl   z|   ||¡dfW S  tyF   tjdt ¡ ddid� dtƒ f Y S  tyf   dtddd�f Y S 0 dS )	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Úsysrs   r   r   r   )r7   r)   r8   r;   r;   r<   rc   Õ   s    ýzASGIHandler.create_requestc                    sD   zt ƒ  |||¡W S  ty>   ttjr0t ¡ nddd� Y S 0 dS )z#Last-chance handler for exceptions.zInternal Server Errorz
text/plain)Úcontent_typeN)rA   Úhandle_uncaught_exceptionÚ	Exceptionr   r   ÚDEBUGÚ	tracebackÚ
format_exc)r7   rg   Úresolverrs   rC   r;   r<   r|   æ   s    þz%ASGIHandler.handle_uncaught_exceptionc              	   Ã   s„  g }|  ¡ D ]F\}}t|tƒr(| d¡}t|tƒr<| d¡}| t|ƒt|ƒf¡ q|j ¡ D ]$}| d|jdd� d¡ 	¡ f¡ q^|d|j
|dœƒI dH  |j�r:t| ¡ ƒ4 I dH šV}|2 z63 dH W }|  |¡D ]\}	}
|d	|	d
dœƒI dH  qÔq¾6 W d  ƒI dH  �q&1 I dH �s0    Y  |dd	iƒI dH  n0|  |j¡D ]"\}	}|d	|	| dœƒI dH  �qFt|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)rY   ru   r"   Nzhttp.response.bodyT)rY   rk   rl   rY   r`   )Úitemsr1   r5   ÚencodeÚappendr2   ÚcookiesÚvaluesÚoutputÚstriprr   Z	streamingr   Ú	__aiter__Úchunk_bytesÚcontentr   rO   )r7   rh   r^   Úresponse_headersrƒ   r:   Úcr�   ÚpartÚchunkr'   Úlastr;   r;   r<   rd   ò   sH    



ÿýÿ
ûÿ>
ýÿzASGIHandler.send_responsec                 c   sX   d}|s|dfV  dS |t |ƒk rT|||| j … || j t |ƒkfV  || j7 }qdS )z
        Chunks some data up so it can be sent in reasonable size messages.
        Yields (chunk, last_chunk) tuples.
        r   TN)r-   re   )ÚclsÚdataÚpositionr;   r;   r<   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   ZFORCE_SCRIPT_NAMEr*   )r7   r)   r;   r;   r<   rb   ;  s    zASGIHandler.get_script_prefix)rP   rQ   rR   rS   r   rv   re   r=   r_   r\   ra   rc   r|   rd   ÚclassmethodrŒ   rb   rV   r;   r;   rC   r<   rW   ‰   s   8
rW   )#Úloggingrz   rm   r   Zasgiref.syncr   r   Zdjango.confr   Zdjango.corer   Zdjango.core.exceptionsr   r   Zdjango.core.handlersr   Zdjango.httpr	   r
   r   r   r   r   r   Zdjango.urlsr   Zdjango.utils.asyncior   Zdjango.utils.functionalr   Ú	getLoggerrx   r   ÚBaseHandlerrW   r;   r;   r;   r<   Ú<module>   s   $	
m