a
    c	g5"                     @  s   d dl m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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)ObjectReceiveStreamObjectSendStream)collapse_excgroups)BackgroundTask)ClientDisconnectRequest)ContentStreamResponseStreamingResponse)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__ \/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/starlette/middleware/base.pyr      s    z_CachedRequest.__init__r   returnc                   s   | j rddiS | jrX| jr(d| _ ddiS |  I d H }|d dkrTtd|d  |S t| dd d ur|d| _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#   sB    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)r4   r5   dispatch_func)r   r4   r5   r   r   r   r   a   s    zBaseHTTPMiddleware.__init__r   r   r   )r   r   sendr    c              
     s   d dkr$ ||I d H  d S t|}|jt  ddd fdd}t x t 4 I d H B||I d H }||I d H     W d   I d H  q1 I d H s0    Y  W d    n1 s0    Y  d S )Nr!   httpr   r
   )requestr    c                   s,  d  t  \dd	fd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 r6   )Zcancel_scopecancel)r;   result
task_groupr   r   wrapz   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_disconnectt   s    8zMBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnectr3   c                     s     I d H     d S r6   )rE   closer   )recv_streamrG   r   r   "close_recv_stream_on_response_sent   s    zZBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.close_recv_stream_on_response_sent)rF   r    c                   s0   z  | I d H  W n tjy*   Y d S 0 d S r6   )r8   rB   ZBrokenResourceError)rF   )send_streamr   r   send_no_error   s    zEBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.send_no_errorc                    s~   4 I d H X z I d H  W n( tyN }  z|  W Y d } ~ n
d } ~ 0 0 W d   I d H  qz1 I d H sp0    Y  d S r6   )r4   	Exception)exc)app_excrH   r   r   rM   rL   r   r   coro   s
    z<BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.coroinfor!   http.response.debugzNo response returned.zhttp.response.startz"typing.AsyncGenerator[bytes, None]c               	    s   4 I d H ` 2 zB3 d H W } | d dks.J |  dd}|rD|V  |  dds qXq6 W d   I d H  q1 I d H sx0    Y   d ur d S )Nr!   zhttp.response.bodyr$   r&   r%   F)get)rF   r$   )rP   rJ   r   r   body_stream   s    2zCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamstatus)status_codecontentrR   headers)	rB   Zcreate_memory_object_streamrD   r   rT   ZEndOfStreamr'   _StreamingResponseraw_headers)r:   rK   rQ   rF   rR   rU   responserG   r   r   r?   r,   )rP   rH   rJ   rM   rL   r   	call_nextn   s0    	


z.BaseHTTPMiddleware.__call__.<locals>.call_next)	r4   r   r,   rB   Eventr   rC   r7   set)r   r   r   r8   r:   r^   r\   r   r]   r   __call__e   s    
OzBaseHTTPMiddleware.__call__r   RequestResponseEndpointr
   )r:   r^   r    c                   s
   t  d S r6   )NotImplementedError)r   r:   r^   r   r   r   r5      s    zBaseHTTPMiddleware.dispatch)N)r-   r.   r/   r   ra   r5   r   r   r   r   r2   `   s   ^r2   c                	      sD   e Zd Zdddddddd	d
 fddZdd	d fddZ  ZS )rZ      Nr	   intztyping.Mapping[str, str] | Nonez
str | NonezBackgroundTask | Nonez&typing.Mapping[str, typing.Any] | Noner3   )rX   rW   rY   
media_type
backgroundrR   r    c                   s   || _ t ||||| d S r6   )_infor   r   )r   rX   rW   rY   rf   rg   rR   r   r   r   r      s    	z_StreamingResponse.__init__r   )r8   r    c                   s.   | j r|d| j dI d H  t |I d H S )NrS   )r!   rR   )rh   r   stream_response)r   r8   r   r   r   ri      s    z"_StreamingResponse.stream_response)rd   NNNN)r-   r.   r/   r   ri   r1   r   r   r   r   rZ      s         rZ   )!
__future__r   typingrB   Z	anyio.abcr   r   Zstarlette._utilsr   Zstarlette.backgroundr   Zstarlette.requestsr   r   Zstarlette.responsesr	   r
   r   Zstarlette.typesr   r   r   r   r   Callable	Awaitablerb   ZDispatchFunctionTypeVarr   r   r2   rZ   r   r   r   r   <module>   s    
Ki