o
    	+i](                     @  s  d dl mZ d dlmZmZmZmZmZmZ d dl	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
f B df ZeeeB eB eee
f B  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CallableMappingMutableMapping)AnyTypeVarN)collapse_excgroups)ClientDisconnectRequest)Response)ASGIAppMessageReceiveScopeSendTc                      s,   e Zd ZdZd fddZ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.
    scoper   receiver   c                   s(   t  || d| _d| _|  | _d S )NF)super__init___wrapped_rcv_disconnected_wrapped_rcv_consumedstream_wrapped_rc_stream)selfr   r   	__class__ V/var/www/html/psymed-ai/venv/lib/python3.10/site-packages/starlette/middleware/base.pyr      s   z_CachedRequest.__init__returnr   c                   s   | j rddiS | jr0| jrd| _ ddiS |  I d H }|d dkr+td|d  d| _ |S t| dd d urBd| _d| jddS | jrNd| _dd	ddS z|  }|	 I d H }| j| _d|| j dW S  t
yv   d| _ ddi Y S w )
Ntypehttp.disconnectTzUnexpected message received: _bodyzhttp.requestFr$   body	more_body    )r   r   _is_disconnectedr   RuntimeErrorgetattrr&   _stream_consumedr   	__anext__r   )r   msgr   chunkr!   r!   r"   wrapped_receive"   sH   z_CachedRequest.wrapped_receive)r   r   r   r   r#   r   )__name__
__module____qualname____doc__r   r2   __classcell__r!   r!   r   r"   r      s    r   c                   @  s,   e Zd Zdddd	ZdddZdddZdS )BaseHTTPMiddlewareNappr   dispatchDispatchFunction | Noner#   Nonec                 C  s$   || _ |d u r| j| _d S || _d S N)r:   r;   dispatch_func)r   r:   r;   r!   r!   r"   r   a   s   zBaseHTTPMiddleware.__init__r   r   r   r   sendr   c                   sj  d dkr ||I d H  d S t|}|jt d  dd
 f	dd	}t }|\e S t @ t 4 I d H %||I d H }||I d H  	  
  W d   I d H  n1 I d H sww   Y  W d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y   d urs d S d S )Nr$   httpFrequestr   r#   r   c                   s   d
fdd df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yZ   d urVdjpUjtdw |d dkscJ dfdd}t|d | |d}|d |_	|S )Nr#   r   c               	     s     r	ddiS t 4 I d H   d	 fdd}  | j | I d H }W d   I d H  n1 I d H s7w   Y    rDddiS |S )
Nr$   r%   funcCallable[[], Awaitable[T]]r#   r   c                   s   |  I d H } j   |S r>   )cancel_scopecancel)rC   result
task_groupr!   r"   wrapw   s   
z[BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnect.<locals>.wrap)rC   rD   r#   r   )is_setanyiocreate_task_group
start_soonwait)rJ   message)response_sentr2   rH   r"   receive_or_disconnectq   s   (
zMBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnectrP   r=   c                   s0   z  | I d H  W d S  tjy   Y d S w r>   )r@   rL   BrokenResourceError)rP   )send_streamr!   r"   send_no_error   s   zEBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.send_no_errorc                    s~   2 z I d H  W n ty$ }  z|  W Y d } ~ nd } ~ ww W d    d S W d    d S 1 s8w   Y  d S r>   )r:   	Exception)exc)app_excrR   r   r   rU   rT   r!   r"   coro   s   "z<BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.coroinfor$   http.response.debugTzNo response returned.http.response.startBodyStreamGeneratorc                   st    2 z33 d H W } | d dkr| V   d S | d dks"J d|  |  dd}|r-|V  |  dds6 d S q6 d S )	Nr$   zhttp.response.pathsendhttp.response.bodyzUnexpected message: r(   r*   r)   F)get)rP   r(   )recv_streamr!   r"   body_stream   s   zCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamstatus)status_codecontentrZ   headersr3   )rP   r   r#   r=   )r#   r=   )r#   r]   )
rN   r   r_   rL   EndOfStream	__cause____context__r,   _StreamingResponseraw_headers)rB   rY   rP   rZ   ra   response	rX   exception_already_raisedr`   rQ   r   r   rT   rI   r2   )rR   rU   r"   	call_nextp   s,   
	

z.BaseHTTPMiddleware.__call__.<locals>.call_next)rB   r   r#   r   )r:   r   r2   rL   Eventcreate_memory_object_streamr   rM   r?   setclose)r   r   r   r@   rB   rn   streamsrk   r!   rl   r"   __call__e   s8   
M
(  zBaseHTTPMiddleware.__call__rB   r   rn   RequestResponseEndpointr   c                   s   t  r>   )NotImplementedError)r   rB   rn   r!   r!   r"   r;      s   zBaseHTTPMiddleware.dispatchr>   )r:   r   r;   r<   r#   r=   r   r   r   r   r@   r   r#   r=   )rB   r   rn   ru   r#   r   )r4   r5   r6   r   rt   r;   r!   r!   r!   r"   r9   `   s    
cr9   c                   @  s*   e Zd Z				ddddZdddZdS )ri      Nrd   AsyncContentStreamrc   intre   Mapping[str, str] | None
media_type
str | NonerZ   Mapping[str, Any] | Noner#   r=   c                 C  s,   || _ || _|| _|| _| | d | _d S r>   )rZ   body_iteratorrc   r|   init_headers
background)r   rd   rc   re   r|   rZ   r!   r!   r"   r      s   

z_StreamingResponse.__init__r   r   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r8d}||I d H  q#|d|ddI d H  q#6 |rR|dd	ddI d H  | jr^|  I d H  d S d S )
Nr[   )r$   rZ   r\   )r$   rb   re   TFr^   r'   r*   )rZ   rc   rj   r   
isinstancedictr   )r   r   r   r@   should_close_bodyr1   r!   r!   r"   rt      s,   


z_StreamingResponse.__call__)rx   NNN)rd   ry   rc   rz   re   r{   r|   r}   rZ   r~   r#   r=   rw   )r4   r5   r6   r   rt   r!   r!   r!   r"   ri      s    ri   )%
__future__r   collections.abcr   r   r   r   r   r   typingr	   r
   rL   starlette._utilsr   starlette.requestsr   r   starlette.responsesr   starlette.typesr   r   r   r   r   ru   DispatchFunctionbytesstrr]   
memoryviewry   r   r   r9   ri   r!   r!   r!   r"   <module>   s      Ll