a
    h%                     @  s$  d dl mZ d dlmZmZmZmZmZ d dlm	Z	m
Z
mZmZ d dl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 e
egee f Ze
eegee f Zeeeeee	f f df Zeeeee eee	f f  Z!ed	Z"G d
d deZ#G dd dZ$G dd deZ%dS )    )annotations)AsyncGeneratorAsyncIterable	AwaitableMappingMutableMapping)AnyCallableTypeVarUnionN)collapse_excgroups)ClientDisconnectRequest)Response)ASGIAppMessageReceiveScopeSendTc                      s6   e Zd ZdZddd fddZddd	d
Z  ZS )_CachedRequesta  
    If the user calls Request.body() from their dispatch function
    we cache the entire request body in memory and pass that to downstream middlewares,
    but if they call Request.stream() then all we do is send an
    empty body so that downstream things don't hang forever.
    r   r   )scopereceivec                   s(   t  || d| _d| _|  | _d S )NF)super__init___wrapped_rcv_disconnected_wrapped_rcv_consumedstreamZ_wrapped_rc_stream)selfr   r   	__class__ U/var/www/html/assistant/venv/lib/python3.9/site-packages/starlette/middleware/base.pyr      s    z_CachedRequest.__init__r   returnc                   s   | j rddiS | jr^| jr(d| _ ddiS |  I d H }|d dkrTtd|d  d| _ |S t| dd d urd| _d| jddS | jrd| _dd	ddS z0|  }|	 I d H }| j| _d|| j dW S  t
y   d| _ ddi Y S 0 d S )
Ntypehttp.disconnectTzUnexpected message received: _bodyzhttp.requestFr%   body	more_body    )r   r   Z_is_disconnectedr   RuntimeErrorgetattrr'   Z_stream_consumedr   	__anext__r   )r   msgr   chunkr!   r!   r"   wrapped_receive"   sD    z_CachedRequest.wrapped_receive)__name__
__module____qualname____doc__r   r1   __classcell__r!   r!   r   r"   r      s   r   c                   @  sF   e Zd ZdddddddZdd	d
ddddZddddddZdS )BaseHTTPMiddlewareNr   zDispatchFunction | NoneNone)appdispatchr$   c                 C  s   || _ |d u r| jn|| _d S N)r9   r:   dispatch_func)r   r9   r:   r!   r!   r"   r   a   s    zBaseHTTPMiddleware.__init__r   r   r   r   r   sendr$   c                   st  d dkr$ ||I d H  d S t|}|jt d  dddd f	dd}t }|\  t  t 4 I d H J||I d H }||I d H  	  
  W d   I d H  q1 I d H s0    Y  W d    n1 s0    Y  W d    n1 s20    Y  W d    n1 sR0    Y   d urpsp d S )	Nr%   httpFr   r   )requestr$   c                   s   dd
fdd dddfdddd fd	d
}	 | z@ I d H }|dd }|d dkr|d ur I d H }W n, tjy   d urdtdY n0 |d dksJ ddfdd}t|d | |d}|d |_|S )Nr   r#   c               	     s     rddiS t 4 I d H F ddd fdd}  | j | I d H }W d   I d H  q|1 I d H sr0    Y    rddiS |S )Nr%   r&   zCallable[[], Awaitable[T]]r   )funcr$   c                   s   |  I d H } j   |S r;   )Zcancel_scopecancel)rA   result
task_groupr!   r"   wrapw   s    
z[BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnect.<locals>.wrap)is_setanyiocreate_task_group
start_soonwait)rF   message)response_sentr1   rD   r"   receive_or_disconnectq   s    8zMBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnectr8   )rL   r$   c                   s0   z  | I d H  W n tjy*   Y d S 0 d S r;   )r>   rH   ZBrokenResourceError)rL   )send_streamr!   r"   send_no_error   s    zEBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.send_no_errorc                    sj   R z I d H  W n( tyF }  z|  W Y d } ~ n
d } ~ 0 0 W d    n1 s\0    Y  d S r;   )r9   	Exception)exc)app_excrN   r   r   rP   rO   r!   r"   coro   s
    z<BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.coroinfor%   http.response.debugTzNo response returned.http.response.startBodyStreamGeneratorc                   sn    2 zb3 d H W } | d dkr&| V   qj| d dks@J d|  |  dd}|rV|V  |  dds qjq6 d S )	Nr%   zhttp.response.pathsendhttp.response.bodyzUnexpected message: r)   r+   r*   F)get)rL   r)   )recv_streamr!   r"   body_stream   s    zCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamstatus)status_codecontentrU   headers)rJ   r   rZ   rH   ZEndOfStreamr,   _StreamingResponseraw_headers)r@   rT   rL   rU   r\   response	rS   Zexception_already_raisedr[   rM   r   r   rO   rE   r1   )rN   rP   r"   	call_nextp   s&    	

z.BaseHTTPMiddleware.__call__.<locals>.call_next)r9   r   r1   rH   EventZcreate_memory_object_streamr   rI   r<   setclose)r   r   r   r>   r@   re   Zstreamsrc   r!   rd   r"   __call__e   s&    
$DzBaseHTTPMiddleware.__call__r   RequestResponseEndpointr   )r@   re   r$   c                   s
   t  d S r;   )NotImplementedError)r   r@   re   r!   r!   r"   r:      s    zBaseHTTPMiddleware.dispatch)N)r2   r3   r4   r   ri   r:   r!   r!   r!   r"   r7   `   s   Zr7   c                   @  s:   e Zd Zdddddddd	d
dZdddddddZdS )ra      NAsyncContentStreamintzMapping[str, str] | Nonez
str | NonezMapping[str, Any] | Noner8   )r_   r^   r`   
media_typerU   r$   c                 C  s,   || _ || _|| _|| _| | d | _d S r;   )rU   body_iteratorr^   ro   Zinit_headers
background)r   r_   r^   r`   ro   rU   r!   r!   r"   r      s    
z_StreamingResponse.__init__r   r   r   r=   c                   s   | j d ur |d| j dI d H  |d| j| jdI d H  d}| j2 z@3 d H W }t|trnd}||I d H  qD|d|ddI d H  qD6 |r|dd	ddI d H  | jr|  I d H  d S )
NrV   )r%   rU   rW   )r%   r]   r`   TFrY   r(   r+   )rU   r^   rb   rp   
isinstancedictrq   )r   r   r   r>   Zshould_close_bodyr0   r!   r!   r"   ri      s&    


z_StreamingResponse.__call__)rl   NNN)r2   r3   r4   r   ri   r!   r!   r!   r"   ra      s       ra   )&
__future__r   collections.abcr   r   r   r   r   typingr   r	   r
   r   rH   Zstarlette._utilsr   Zstarlette.requestsr   r   Zstarlette.responsesr   Zstarlette.typesr   r   r   r   r   rj   ZDispatchFunctionbytesstrrX   
memoryviewrm   r   r   r7   ra   r!   r!   r!   r"   <module>   s   Lc