o
    :ήc                     @   s   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	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dS )    N)Request)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   @/tmp/pip-target-vg8gfxp4/lib/python/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 }W n t jyW    d urS tdw |d dks`J 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_sent5   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_error9   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   r2   r1   r   r   coro@   s   .z<BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.corozNo response returned.r   zhttp.response.startc               	     s   4 I d H 0 2 z!3 d H W } | d dksJ |  dd}|r#|V  |  dds+ nq
6 W d   I d H  n1 I d H s=w   Y   d urH d S )Nr   zhttp.response.bodybody    	more_bodyF)get)r*   r7   )r5   r.   r   r   body_streamU   s   (	zCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamstatus)status_codecontentheaders)r   N)r#   create_memory_object_streamr   r(   r   EndOfStreamRuntimeErrorr%   AsyncGeneratorbytesr   raw_headers)r   r/   r6   r*   r;   responser+   r   r   r    )r5   r,   r.   r   r2   r1   r   	call_next   s.   
	


z.BaseHTTPMiddleware.__call__.<locals>.call_next)r   )r   r#   Eventr   r   r$   r   set)r   r   r   r   rH   r   rF   r   rG   r   __call__   s   J
.zBaseHTTPMiddleware.__call__r   rH   c                    s   t  r   )NotImplementedError)r   r   rH   r   r   r   r   n   s   zBaseHTTPMiddleware.dispatchr   )__name__
__module____qualname__r   r%   OptionalDispatchFunctionr   r   r   r	   rK   r   RequestResponseEndpointr   r   r   r   r   r   r      s"    
Wr   )r%   r#   starlette.requestsr   starlette.responsesr   r   starlette.typesr   r   r   r   r	   r&   r'   rR   rQ   TypeVarr
   r   r   r   r   r   <module>   s    
