U
    Rh#                     @  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__ ]/var/www/html/timesheet/backend/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   _is_disconnectedr   RuntimeErrorgetattrr!   _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)r5   r6   dispatch_func)r   r5   r6   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 r7   )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)rC   message)response_sentr-   rA   r   receive_or_disconnectr   s    zMBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnectr4   c                     s     I d H     d S r7   )rH   closer   )recv_streamrJ   r   r   "close_recv_stream_on_response_sent   s    zZBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.close_recv_stream_on_response_sent)rI   r   c                   s2   z  | I d H  W n tjk
r,   Y d S X d S r7   )r:   rE   BrokenResourceError)rI   )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 r7   )r5   	Exception)exc)app_excrK   r   r   rQ   rP   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)rI   r#   )rT   rM   r   r   body_stream   s    zCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamstatus)status_codecontentrV   headers)
rE   create_memory_object_streamrG   r   r[   EndOfStreamr'   rZ   _StreamingResponseraw_headers)r<   rN   rU   rI   rV   r\   responserJ   r   r   rB   r-   )rT   rK   rM   rQ   rP   r   	call_nextl   s,    	


z.BaseHTTPMiddleware.__call__.<locals>.call_next)	r5   r   r-   rE   Eventr   rF   r8   set)r   r   r   r:   r<   rg   re   r   rf   r   __call__c   s    
MzBaseHTTPMiddleware.__call__r   RequestResponseEndpointr	   )r<   rg   r   c                   s
   t  d S r7   )NotImplementedError)r   r<   rg   r   r   r   r6      s    zBaseHTTPMiddleware.dispatch)N)r.   r/   r0   r   rj   r6   r   r   r   r   r3   ^   s   \r3   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 )rc      Nr   intztyping.Mapping[str, str] | Nonez
str | Nonez&typing.Mapping[str, typing.Any] | Noner4   )r_   r^   r`   
media_typerV   r   c                 C  s,   || _ || _|| _|| _| | d | _d S r7   )rV   body_iteratorr^   ro   init_headers
background)r   r_   r^   r`   ro   rV   r   r   r   r      s    
z_StreamingResponse.__init__r   r   r   r9   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 )
NrW   )r   rV   rX   )r   r]   r`   rY   Tr"   r%   F)rV   r^   rd   rp   rr   )r   r   r   r:   r,   r   r   r   rj      s    

z_StreamingResponse.__call__)rm   NNN)r.   r/   r0   r   rj   r   r   r   r   rc      s       rc   )
__future__r   typingrE   Z	anyio.abcr   r   starlette._utilsr   starlette.requestsr   r   starlette.responsesr   r	   starlette.typesr
   r   r   r   r   Callable	Awaitablerk   ZDispatchFunctionTypeVarr   r   r3   rc   r   r   r   r   <module>   s   
Le