o
    f5"                     @  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
 d dlmZ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eZG dd dZG dd deZ dS )    )annotationsN)ObjectReceiveStreamObjectSendStream)collapse_excgroups)BackgroundTask)ClientDisconnectRequest)ContentStreamResponseStreamingResponse)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stream_wrapped_rc_stream)selfr   r   	__class__ Q/var/www/NoticeGen/venv/lib/python3.10/site-packages/starlette/middleware/base.pyr      s   z_CachedRequest.__init__returnr   c                   s   | j rddiS | jr-| jrd| _ ddiS |  I d H }|d dkr+td|d  |S t| dd d ur?d| _d| jddS | jrKd| _dd	ddS z|  }|	 I d H }| j| _d|| j dW S  t
ys   d| _ ddi Y S w )
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#   sF   z_CachedRequest.wrapped_receive)r   r   r   r   r    r   )__name__
__module____qualname____doc__r   r.   __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)r6   r7   dispatch_func)r   r6   r7   r   r   r   r   a   s   zBaseHTTPMiddleware.__init__r   r   r   r   sendr   c              
     s   d dkr ||I d H  d S t|}|jt  d	 fdd}t E t 4 I d H !||I d H }||I d H     W d   I d H  n1 I d H s]w   Y  W d    d S W d    d S 1 suw   Y  d S )
Nr!   httprequestr   r    r
   c                   s  d  t  \d	fdddfdd}dfd	d
d fdd}| | z  I d H }|dd }|d dkrU|d urU I d H }W n t jyh    d urd tdw |d dksqJ 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(typing.Callable[[], typing.Awaitable[T]]r    r   c                   s   |  I d H } j   |S r:   )cancel_scopecancel)r?   result
task_groupr   r   wrapz   s   
z[BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnect.<locals>.wrap)r?   r@   r    r   )is_setanyiocreate_task_group
start_soonwait)rF   message)response_sentr.   rD   r   receive_or_disconnectt   s   (
zMBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnectr9   c                     s     I d H     d S r:   )rK   closer   )recv_streamrM   r   r   "close_recv_stream_on_response_sent   s   zZBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.close_recv_stream_on_response_sentrL   c                   s0   z  | I d H  W d S  tjy   Y d S w r:   )r<   rH   BrokenResourceError)rL   )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:   )r6   	Exception)exc)app_excrN   r   r   rT   rS   r   r   coro   s   .z<BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.coroinfor!   http.response.debugzNo response returned.zhttp.response.start"typing.AsyncGenerator[bytes, None]c               	    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.bodyr$   r&   r%   F)get)rL   r$   )rW   rP   r   r   body_stream   s   (	zCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamstatus)status_codecontentrY   headersr/   )r    r9   )rL   r   r    r9   )r    r[   )	rH   create_memory_object_streamrJ   r   r\   EndOfStreamr(   _StreamingResponseraw_headers)r>   rQ   rX   rL   rY   r]   responserM   r   r   rE   r.   )rW   rN   rP   rT   rS   r   	call_nextn   s6   
	

z.BaseHTTPMiddleware.__call__.<locals>.call_next)r>   r   r    r
   )	r6   r   r.   rH   Eventr   rI   r;   set)r   r   r   r<   r>   rh   rf   r   rg   r   __call__e   s"   
O
*"zBaseHTTPMiddleware.__call__r>   r   rh   RequestResponseEndpointr
   c                   s   t  r:   )NotImplementedError)r   r>   rh   r   r   r   r7      s   zBaseHTTPMiddleware.dispatchr:   )r6   r   r7   r8   r    r9   )r   r   r   r   r<   r   r    r9   )r>   r   rh   rl   r    r
   )r0   r1   r2   r   rk   r7   r   r   r   r   r5   `   s    
^r5   c                      s8   e Zd Z					dd fddZd fddZ  ZS )rd      Nr`   r	   r_   intra   typing.Mapping[str, str] | None
media_type
str | None
backgroundBackgroundTask | NonerY   &typing.Mapping[str, typing.Any] | Noner    r9   c                   s   || _ t ||||| d S r:   )_infor   r   )r   r`   r_   ra   rq   rs   rY   r   r   r   r      s   	z_StreamingResponse.__init__r<   r   c                   s0   | j r|d| j dI d H  t |I d H S )NrZ   )r!   rY   )rv   r   stream_response)r   r<   r   r   r   rw      s   z"_StreamingResponse.stream_response)rn   NNNN)r`   r	   r_   ro   ra   rp   rq   rr   rs   rt   rY   ru   r    r9   )r<   r   r    r9   )r0   r1   r2   r   rw   r4   r   r   r   r   rd      s    rd   )!
__future__r   typingrH   	anyio.abcr   r   starlette._utilsr   starlette.backgroundr   starlette.requestsr   r   starlette.responsesr	   r
   r   starlette.typesr   r   r   r   r   Callable	Awaitablerl   DispatchFunctionTypeVarr   r   r5   rd   r   r   r   r   <module>   s"    
Ki