U
    EZh#                     @  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
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
eZG dd dZG dd deZdS )    )annotationsN)ObjectReceiveStreamObjectSendStream)collapse_excgroups)ClientDisconnectRequest)AsyncContentStreamResponse)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__ `/home/aprabhat/apps/x.techxrdev.in/venv/lib/python3.8/site-packages/starlette/middleware/base.pyr      s    z_CachedRequest.__init__r   returnc                   s   | j rddiS | jr^| jr(d| _ ddiS |  I d H }|d dkrTtd|d  d| _ |S t| dd d k	rd| _d| jddS | jrd| _dd	ddS z0|  }|	 I d H }| j| _d|| j dW S  t
k
r   d| _ ddi Y S X 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    sD    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 kr| jn|| _d S N)r3   r4   dispatch_func)r   r3   r4   r   r   r   r   _   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 T t 4 I d H 2||I d H }||I d H     W 5 Q I d H R X W 5 Q R X 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 k	r I d H }W n* t jk
r    d k	rڈ tdY nX |d dkstdd fdd}t|d | |d}|d |_	|S )Nr   r   c               
     sv     rddiS t 4 I d H 6 ddd fdd}  | j | I d H }W 5 Q I d H R X   rrddiS |S )Nr   r    z(typing.Callable[[], typing.Awaitable[T]]r   )funcr   c                   s   |  I d H } j   |S r5   )Zcancel_scopecancel)r;   result
task_groupr   r   wrapx   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_disconnectr   s    zMBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnectr2   c                     s     I d H     d S r5   )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                   s2   z  | I d H  W n tjk
r,   Y d S X d S r5   )r8   rB   ZBrokenResourceError)rF   )send_streamr   r   send_no_error   s    zEBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.send_no_errorc                    sb   4 I d H F z I d H  W n& tk
rL }  z|  W 5 d } ~ X Y nX W 5 Q I d H R X d S r5   )r3   	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.http.response.startz"typing.AsyncGenerator[bytes, None]c               
    sx   4 I d H P 2 zB3 d H W } | d dks.t | dd}|rD|V  | dds qXq6 W 5 Q I d H R X  d k	rt d S )Nr   http.response.bodyr#   r%   r$   F)AssertionErrorget)rF   r#   )rP   rJ   r   r   body_stream   s    zCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamstatus)status_codecontentrR   headers)
rB   Zcreate_memory_object_streamrD   r   rW   ZEndOfStreamr&   rV   _StreamingResponseraw_headers)r:   rK   rQ   rF   rR   rX   responserG   r   r   r?   r+   )rP   rH   rJ   rM   rL   r   	call_nextl   s,    	


z.BaseHTTPMiddleware.__call__.<locals>.call_next)	r3   r   r+   rB   Eventr   rC   r6   set)r   r   r   r8   r:   ra   r_   r   r`   r   __call__c   s    
MzBaseHTTPMiddleware.__call__r   RequestResponseEndpointr	   )r:   ra   r   c                   s
   t  d S r5   )NotImplementedError)r   r:   ra   r   r   r   r4      s    zBaseHTTPMiddleware.dispatch)N)r,   r-   r.   r   rd   r4   r   r   r   r   r1   ^   s   \r1   c                   @  s:   e Zd Zdddddddd	d
dZdddddddZdS )r]      Nr   intztyping.Mapping[str, str] | Nonez
str | Nonez&typing.Mapping[str, typing.Any] | Noner2   )r[   rZ   r\   
media_typerR   r   c                 C  s,   || _ || _|| _|| _| | d | _d S r5   )rR   body_iteratorrZ   ri   Zinit_headers
background)r   r[   rZ   r\   ri   rR   r   r   r   r      s    
z_StreamingResponse.__init__r   r   r   r7   c                   s   | j d k	r |d| j dI d H  |d| j| jdI d H  | j2 z"3 d H W }|d|ddI d H  q@6 |ddd	dI d H  | jr|  I d H  d S )
NrS   )r   rR   rT   )r   rY   r\   rU   Tr"   r%   F)rR   rZ   r^   rj   rk   )r   r   r   r8   r*   r   r   r   rd      s    

z_StreamingResponse.__call__)rg   NNN)r,   r-   r.   r   rd   r   r   r   r   r]      s       r]   )
__future__r   typingrB   Z	anyio.abcr   r   Zstarlette._utilsr   Zstarlette.requestsr   r   Zstarlette.responsesr   r	   Zstarlette.typesr
   r   r   r   r   Callable	Awaitablere   ZDispatchFunctionTypeVarr   r   r1   r]   r   r   r   r   <module>   s   
Le