a
    ׮'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rd| 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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_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-lengthZCONTENT_LENGTHzcontent-typeZ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 r9   U/var/www/html/genifoncierenv/lib/python3.9/site-packages/django/core/handlers/asgi.py__init__&   s`    




	





zASGIRequest.__init__c                 C   s   t | jd S )Nr   )r   r2   r5   r9   r9   r:   GETi   s    zASGIRequest.GETc                    s   | j dpt  S )Nscheme)r'   r(   super_get_schemer<   	__class__r9   r:   r@   m   s    zASGIRequest._get_schemec                 C   s   t | ds|   | jS )N_post)hasattr_load_post_and_filesrC   r<   r9   r9   r:   	_get_postp   s    
zASGIRequest._get_postc                 C   s
   || _ d S N)rC   )r5   postr9   r9   r:   	_set_postu   s    zASGIRequest._set_postc                 C   s   t | ds|   | jS )N_files)rD   rE   rJ   r<   r9   r9   r:   
_get_filesx   s    
zASGIRequest._get_filesc                 C   s   t | jddS )NZHTTP_COOKIEr   )r   r2   r(   r<   r9   r9   r:   COOKIES   s    zASGIRequest.COOKIESc                    s   t    | j  d S rG   )r?   closer4   r<   rA   r9   r:   rM      s    
zASGIRequest.close)__name__
__module____qualname____doc__Zbody_receive_timeoutr;   r   r=   r@   rF   rI   rK   propertyPOSTZFILESrL   rM   __classcell__r9   r9   rA   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)r?   r;   Zload_middlewarer<   rA   r9   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)r5   r'   receivesendr9   r9   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\   rB   create_requestrM   send_responseZget_response_asyncZ_handler_classr/   r	   
chunk_size
block_size)r5   r'   r[   r\   r6   requestZerror_responseresponser9   r9   r:   rZ      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modeNrW   zhttp.disconnectbody	more_bodyFr   )	tempfileSpooledTemporaryFiler   ZFILE_UPLOAD_MAX_MEMORY_SIZErM   r   writer(   seek)r5   r[   r6   messager9   r9   r:   r_      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sysrq   r   r   r   )r5   r'   r6   r9   r9   r:   ra      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)r?   handle_uncaught_exception	Exceptionr   r   DEBUG	traceback
format_exc)r5   re   resolverrq   rA   r9   r:   rz      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  qq6 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)rW   rs   r"   Nzhttp.response.bodyT)rW   ri   rj   rW   r^   )itemsr/   r3   encodeappendr0   cookiesvaluesoutputstriprp   Z	streamingr   	__aiter__chunk_bytescontentr   rM   )r5   rf   r\   response_headersr   r8   cr   partchunkr%   lastr9   r9   r:   rb      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+   rc   )clsdatapositionr9   r9   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(   )r5   r'   r9   r9   r:   r`   ;  s    zASGIHandler.get_script_prefix)rN   rO   rP   rQ   r   rt   rc   r;   r]   rZ   r_   ra   rz   rb   classmethodr   r`   rT   r9   r9   rA   r:   rU      s   8
rU   )#loggingrx   rk   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   	getLoggerrv   r   BaseHandlerrU   r9   r9   r9   r:   <module>   s   $	
m