o
     d                     @   s   d dl Z d dlZd dlmZ d dl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ZG d	d
 d
e	ZdS )    N)BackgroundTask)Request)ContentStreamResponseStreamingResponse)ASGIAppMessageReceiveScopeSendTc                   @   s\   e Zd Z	ddedeje ddfddZdede	d	e
ddfd
dZdededefddZdS )BaseHTTPMiddlewareNappdispatchreturnc                 C   s$   || _ |d u r| j| _d S || _d S N)r   r   dispatch_func)selfr   r    r   D/usr/local/lib/python3.10/dist-packages/starlette/middleware/base.py__init__   s   zBaseHTTPMiddleware.__init__scopereceivesendc              	      s   d dkr ||I d H  d S t  dtdtf fdd}t 4 I d H (t|d}||I d H }|||I d H     W d   I d H  d S 1 I d H s[w   Y  d S )Ntypehttprequestr   c                    s.  d  t  \dtffdddfdd}dtdd f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yn    d urj tdw |d dkswJ dtj	t
d f f fdd}t|d | |d}|d |_|S )Nr   c               	      s     r	ddiS t 4 I d H - dtjg tjt f dtf fdd}  | j | j	I d H }W d   I d H  n1 I d H sDw   Y    rQddiS |S )Nr   zhttp.disconnectfuncr   c                    s   |  I d H } j   |S r   )cancel_scopecancel)r   result
task_groupr   r   wrap)   s   
z[BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnect.<locals>.wrap)
is_setanyiocreate_task_grouptypingCallable	Awaitabler   
start_soonwaitr   )r#   message)r   response_sentr!   r   receive_or_disconnect#   s   &(
zMBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnectc                      s     I d H     d S r   )r+   closer   )recv_streamr-   r   r   "close_recv_stream_on_response_sent6   s   zZBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.close_recv_stream_on_response_sentr,   c                    s0   z  | I d H  W d S  tjy   Y d S w r   )r   r%   BrokenResourceError)r,   )send_streamr   r   send_no_error:   s   zEBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.send_no_errorc                     s   4 I d H 8 z I d H  W n ty( }  z|  W Y d } ~ nd } ~ ww W d   I d H  d S W d   I d H  d S 1 I d H sEw   Y  d S r   )r   	Exception)exc)app_excr.   r   r   r4   r3   r   r   coroA   s   .z<BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.coroinfor   http.response.debugzNo response returned.zhttp.response.startc               	     s   4 I d H ( 2 z3 d H W } | d dksJ |  dd}|r#|V  q
6 W d   I d H  n1 I d H s5w   Y   d ur@ d S )Nr   zhttp.response.bodybody    )get)r,   r;   )r7   r0   r   r   body_streamY   s   (zCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamstatus)status_codecontentr9   headers)r   N)r%   create_memory_object_streamr   r*   r   r=   EndOfStreamRuntimeErrorr'   AsyncGeneratorbytes_StreamingResponseraw_headers)r   r1   r8   r,   r9   r>   responser-   r   r   r"   )r7   r.   r0   r   r4   r3   r   	call_next   s6   
	

z.BaseHTTPMiddleware.__call__.<locals>.call_next)r   )r   r%   Eventr   r   r&   r   set)r   r   r   r   rL   r   rJ   r   rK   r   __call__   s   K
.zBaseHTTPMiddleware.__call__r   rL   c                    s   t  r   )NotImplementedError)r   r   rL   r   r   r   r   p   s   zBaseHTTPMiddleware.dispatchr   )__name__
__module____qualname__r   r'   OptionalDispatchFunctionr   r
   r	   r   rO   r   RequestResponseEndpointr   r   r   r   r   r   r      s"    
Xr   c                       s   e Zd Z					ddededejejeef  deje deje	 dejejeej
f  d	df fd
dZded	df fddZ  ZS )rH      NrA   r@   rB   
media_type
backgroundr9   r   c                    s   || _ t ||||| d S r   )_infosuperr   )r   rA   r@   rB   rX   rY   r9   	__class__r   r   r   w   s   	z_StreamingResponse.__init__r   c                    s0   | j r|d| j dI d H  t |I d H S )Nr:   )r   r9   )rZ   r[   stream_response)r   r   r\   r   r   r^      s   z"_StreamingResponse.stream_response)rW   NNNN)rQ   rR   rS   r   intr'   rT   Mappingstrr   Anyr   r   r^   __classcell__r   r   r\   r   rH   v   s,    rH   )r'   r%   starlette.backgroundr   starlette.requestsr   starlette.responsesr   r   r   starlette.typesr   r   r	   r
   r   r(   r)   rV   rU   TypeVarr   r   rH   r   r   r   r   <module>   s    
e