o
    lin(                     @  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                      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streamZ_wrapped_rc_stream)selfr   r   	__class__ `/home/app/PaddleOCR-VL/.venv_paddleocr/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   Z_is_disconnectedr   RuntimeErrorgetattrr&   Z_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   r0   __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)r8   r9   dispatch_func)r   r8   r9   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<   )Zcancel_scopecancel)rA   result
task_groupr!   r"   wrapw   s   
z[BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnect.<locals>.wrap)rA   rB   r#   r   )is_setanyiocreate_task_group
start_soonwait)rG   message)response_sentr0   rE   r"   receive_or_disconnectq   s   (
zMBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnectrM   r;   c                   s0   z  | I d H  W d S  tjy   Y d S w r<   )r>   rI   ZBrokenResourceError)rM   )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<   )r8   	Exception)exc)app_excrO   r   r   rQ   rP   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)rM   r(   )recv_streamr!   r"   body_stream   s   zCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamstatus)status_codecontentrV   headersr1   )rM   r   r#   r;   )r#   r;   )r#   rY   )
rK   r   r[   rI   ZEndOfStream	__cause____context__r+   _StreamingResponseraw_headers)r@   rU   rM   rV   r]   response	rT   Zexception_already_raisedr\   rN   r   r   rP   rF   r0   )rO   rQ   r"   	call_nextp   s,   
	

z.BaseHTTPMiddleware.__call__.<locals>.call_next)r@   r   r#   r   )r8   r   r0   rI   EventZcreate_memory_object_streamr   rJ   r=   setclose)r   r   r   r>   r@   rh   streamsrf   r!   rg   r"   __call__e   s8   
M
(  zBaseHTTPMiddleware.__call__r@   r   rh   RequestResponseEndpointr   c                   s   t  r<   )NotImplementedError)r   r@   rh   r!   r!   r"   r9      s   zBaseHTTPMiddleware.dispatchr<   )r8   r   r9   r:   r#   r;   r   r   r   r   r>   r   r#   r;   )r@   r   rh   rn   r#   r   )r2   r3   r4   r   rm   r9   r!   r!   r!   r"   r7   `   s    
cr7   c                   @  s*   e Zd Z				ddddZdddZdS )rd      Nr`   AsyncContentStreamr_   intra   Mapping[str, str] | None
media_type
str | NonerV   Mapping[str, Any] | Noner#   r;   c                 C  s,   || _ || _|| _|| _| | d | _d S r<   )rV   body_iteratorr_   ru   Zinit_headers
background)r   r`   r_   ra   ru   rV   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 )
NrW   )r$   rV   rX   )r$   r^   ra   TFrZ   r'   r*   )rV   r_   re   rx   
isinstancedictry   )r   r   r   r>   Zshould_close_bodyr/   r!   r!   r"   rm      s,   


z_StreamingResponse.__call__)rq   NNN)r`   rr   r_   rs   ra   rt   ru   rv   rV   rw   r#   r;   rp   )r2   r3   r4   r   rm   r!   r!   r!   r"   rd      s    rd   )&
__future__r   collections.abcr   r   r   r   r   typingr   r	   r
   r   rI   Zstarlette._utilsr   Zstarlette.requestsr   r   Zstarlette.responsesr   Zstarlette.typesr   r   r   r   r   rn   ZDispatchFunctionbytesstrrY   
memoryviewrr   r   r   r7   rd   r!   r!   r!   r"   <module>   s     Ll