o
    ђh%                     @  s$  d dl mZ d dlmZmZmZmZmZ d dlm	Z	m
Z
mZmZ d dlZd dlmZ d dlmZmZ d dlmZ d dlmZmZmZmZmZ e
egee f Ze
eegee f Zeeeeee	f f df Zeeeee eee	f f  Z!ed	Z"G d
d deZ#G dd dZ$G dd deZ%dS )    )annotations)AsyncGeneratorAsyncIterable	AwaitableMappingMutableMapping)AnyCallableTypeVarUnionN)collapse_excgroups)ClientDisconnectRequest)Response)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__ R/var/www/html/govbot/env/lib/python3.10/site-packages/starlette/middleware/base.pyr      s   z_CachedRequest.__init__returnr   c                   s   | j rddiS | jr0| jrd| _ ddiS |  I d H }|d dkr+td|d  d| _ |S t| dd d urBd| _d| jddS | jrNd| _dd	ddS z|  }|	 I d H }| j| _d|| j dW S  t
yv   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"   sH   z_CachedRequest.wrapped_receive)r   r   r   r   r$   r   )__name__
__module____qualname____doc__r   r3   __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)r;   r<   dispatch_func)r   r;   r<   r"   r"   r#   r   a   s   zBaseHTTPMiddleware.__init__r   r   r   r   sendr   c                   sj  d dkr ||I d H  d S t|}|jt d  dd
 f	dd	}t }|\e S t @ t 4 I d H %||I d H }||I d H  	  
  W d   I d H  n1 I d H sww   Y  W d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y   d urs d S d S )Nr%   httpFrequestr   r$   r   c                   s   d
fdd d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yU   d urQdtdw |d dks^J 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Callable[[], Awaitable[T]]r$   r   c                   s   |  I d H } j   |S r?   )cancel_scopecancel)rD   result
task_groupr"   r#   wrapw   s   
z[BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnect.<locals>.wrap)rD   rE   r$   r   )is_setanyiocreate_task_group
start_soonwait)rK   message)response_sentr3   rI   r#   receive_or_disconnectq   s   (
zMBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnectrQ   r>   c                   s0   z  | I d H  W d S  tjy   Y d S w r?   )rA   rM   BrokenResourceError)rQ   )send_streamr"   r#   send_no_error   s   zEBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.send_no_errorc                    s~   2 z I d H  W n ty$ }  z|  W Y d } ~ nd } ~ ww W d    d S W d    d S 1 s8w   Y  d S r?   )r;   	Exception)exc)app_excrS   r   r   rV   rU   r"   r#   coro   s   "z<BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.coroinfor%   http.response.debugTzNo response returned.http.response.startBodyStreamGeneratorc                   st    2 z33 d H W } | d dkr| V   d S | d dks"J d|  |  dd}|r-|V  |  dds6 d S q6 d S )	Nr%   zhttp.response.pathsendhttp.response.bodyzUnexpected message: r)   r+   r*   F)get)rQ   r)   )recv_streamr"   r#   body_stream   s   zCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamstatus)status_codecontentr[   headersr4   )rQ   r   r$   r>   )r$   r>   )r$   r^   )rO   r   r`   rM   EndOfStreamr-   _StreamingResponseraw_headers)rC   rZ   rQ   r[   rb   response	rY   exception_already_raisedra   rR   r   r   rU   rJ   r3   )rS   rV   r#   	call_nextp   s,   
	
z.BaseHTTPMiddleware.__call__.<locals>.call_next)rC   r   r$   r   )r;   r   r3   rM   Eventcreate_memory_object_streamr   rN   r@   setclose)r   r   r   rA   rC   rm   streamsrj   r"   rk   r#   __call__e   s8   
D
(  zBaseHTTPMiddleware.__call__rC   r   rm   RequestResponseEndpointr   c                   s   t  r?   )NotImplementedError)r   rC   rm   r"   r"   r#   r<      s   zBaseHTTPMiddleware.dispatchr?   )r;   r   r<   r=   r$   r>   r   r   r   r   rA   r   r$   r>   )rC   r   rm   rt   r$   r   )r5   r6   r7   r   rs   r<   r"   r"   r"   r#   r:   `   s    
Zr:   c                   @  s*   e Zd Z				ddddZdddZdS )rh      Nre   AsyncContentStreamrd   intrf   Mapping[str, str] | None
media_type
str | Noner[   Mapping[str, Any] | Noner$   r>   c                 C  s,   || _ || _|| _|| _| | d | _d S r?   )r[   body_iteratorrd   r{   init_headers
background)r   re   rd   rf   r{   r[   r"   r"   r#   r      s   

z_StreamingResponse.__init__r   r   r   r   rA   r   c                   s   | j d ur|d| j dI d H  |d| j| jdI d H  d}| j2 z 3 d H W }t|tr8d}||I d H  q#|d|ddI d H  q#6 |rR|dd	ddI d H  | jr^|  I d H  d S d S )
Nr\   )r%   r[   r]   )r%   rc   rf   TFr_   r(   r+   )r[   rd   ri   r~   
isinstancedictr   )r   r   r   rA   should_close_bodyr2   r"   r"   r#   rs      s,   


z_StreamingResponse.__call__)rw   NNN)re   rx   rd   ry   rf   rz   r{   r|   r[   r}   r$   r>   rv   )r5   r6   r7   r   rs   r"   r"   r"   r#   rh      s    rh   )&
__future__r   collections.abcr   r   r   r   r   typingr   r	   r
   r   rM   starlette._utilsr   starlette.requestsr   r   starlette.responsesr   starlette.typesr   r   r   r   r   rt   DispatchFunctionbytesstrr^   
memoryviewrx   r   r   r:   rh   r"   r"   r"   r#   <module>   s     Lc