a
    ^gm"                     @  s   d dl mZ d dlZd dlZd dlmZ d dlmZmZ d dl	m
Z
mZ d dlmZmZmZmZmZ ejegeje f Zejeegeje f ZedZG dd	 d	eZG d
d dZG dd deZdS )    )annotationsN)collapse_excgroups)ClientDisconnectRequest)AsyncContentStream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__ g/var/www/html/cobodadashboardai.evdpl.com/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   r)   __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)r1   r2   dispatch_func)r   r1   r2   r   r   r   r   ^   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 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 s&0    Y  W d    n1 sF0    Y  d S )Nr   httpr   r   )requestr   c                   s  d  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r 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(typing.Callable[[], typing.Awaitable[T]]r   )funcr   c                   s   |  I d H } j   |S r3   )Zcancel_scopecancel)r9   result
task_groupr   r   wrapt   s    
z[BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnect.<locals>.wrap)is_setanyiocreate_task_group
start_soonwait)r>   message)response_sentr)   r<   r   receive_or_disconnectn   s    8zMBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnectr0   )rD   r   c                   s0   z  | I d H  W n tjy*   Y d S 0 d S r3   )r6   r@   ZBrokenResourceError)rD   )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 r3   )r1   	Exception)exc)app_excrF   r   r   rH   rG   r   r   coro   s
    z<BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.coroinfor   http.response.debugzNo response returned.http.response.startz"typing.AsyncGenerator[bytes, None]c                   sZ   2 zB3 d H W } | d dks J |  dd}|r6|V  |  dds qJq6  d urV d S )Nr   http.response.bodyr!   r#   r"   F)get)rD   r!   )rK   recv_streamr   r   body_stream   s    zCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamstatus)status_codecontentrM   headers)rB   r   rQ   r@   ZEndOfStreamr$   _StreamingResponseraw_headers)r8   rL   rD   rM   rS   responserR   rE   r   r   rG   r=   r)   )rK   rF   rH   r   	call_nextk   s&    	

z.BaseHTTPMiddleware.__call__.<locals>.call_next)r1   r   r)   r@   EventZcreate_memory_object_streamr   rA   r4   setclose)r   r   r   r6   r8   r\   streamsrZ   r   r[   r   __call__b   s    
 DzBaseHTTPMiddleware.__call__r   RequestResponseEndpointr   )r8   r\   r   c                   s
   t  d S r3   )NotImplementedError)r   r8   r\   r   r   r   r2      s    zBaseHTTPMiddleware.dispatch)N)r*   r+   r,   r   ra   r2   r   r   r   r   r/   ]   s   Vr/   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 )rX      Nr   intztyping.Mapping[str, str] | Nonez
str | Nonez&typing.Mapping[str, typing.Any] | Noner0   )rV   rU   rW   
media_typerM   r   c                 C  s,   || _ || _|| _|| _| | d | _d S r3   )rM   body_iteratorrU   rf   Zinit_headers
background)r   rV   rU   rW   rf   rM   r   r   r   r      s    
z_StreamingResponse.__init__r   r
   r   r5   c                   s   | j d ur |d| j dI d H  |d| j| jdI d H  | j2 z"3 d H W }|d|ddI d H  q@6 |ddd	dI d H  | jr|  I d H  d S )
NrN   )r   rM   rO   )r   rT   rW   rP   Tr    r#   F)rM   rU   rY   rg   rh   )r   r   r   r6   r(   r   r   r   ra      s    

z_StreamingResponse.__call__)rd   NNN)r*   r+   r,   r   ra   r   r   r   r   rX      s       rX   )
__future__r   typingr@   Zstarlette._utilsr   Zstarlette.requestsr   r   Zstarlette.responsesr   r   Zstarlette.typesr   r	   r
   r   r   Callable	Awaitablerb   ZDispatchFunctionTypeVarr   r   r/   rX   r   r   r   r   <module>   s   
L_