o
    h#                     @  s   d dl mZ 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 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)collapse_excgroups)ClientDisconnectRequest)AsyncContentStream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__ m/var/www/html/construction_image-detection-poc/venv/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   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)r3   r4   dispatch_func)r   r3   r4   r   r   r   r   ^   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(typing.Callable[[], typing.Awaitable[T]]r   r   c                   s   |  I d H } j   |S r7   )cancel_scopecancel)r<   result
task_groupr   r   wrapt   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)rC   message)response_sentr+   rA   r   receive_or_disconnectn   s   (
zMBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnectrI   r6   c                   s0   z  | I d H  W d S  tjy   Y d S w r7   )r9   rE   BrokenResourceError)rI   )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 r7   )r3   	Exception)exc)app_excrK   r   r   rN   rM   r   r   coro   s   "z<BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.coroinfor   http.response.debugTzNo response returned.http.response.start"typing.AsyncGenerator[bytes, None]c                   sR    2 z"3 d H W } | d dksJ |  dd}|r|V  |  dds% d S q6 d S )Nr   http.response.bodyr!   r#   r"   F)get)rI   r!   )recv_streamr   r   body_stream   s   zCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamstatus)status_codecontentrS   headersr,   )rI   r	   r   r6   )r   r6   )r   rV   )rG   r   rX   rE   EndOfStreamr%   _StreamingResponseraw_headers)r;   rR   rI   rS   rZ   response	rQ   exception_already_raisedrY   rJ   r   r   rM   rB   r+   )rK   rN   r   	call_nextm   s,   
		
z.BaseHTTPMiddleware.__call__.<locals>.call_next)r;   r   r   r   )r3   r   r+   rE   Eventcreate_memory_object_streamr   rF   r8   setclose)r   r   r   r9   r;   re   streamsrb   r   rc   r   __call__b   s8   
A
(  zBaseHTTPMiddleware.__call__r;   r   re   RequestResponseEndpointr   c                   s   t  r7   )NotImplementedError)r   r;   re   r   r   r   r4      s   zBaseHTTPMiddleware.dispatchr7   )r3   r   r4   r5   r   r6   r   r   r   r
   r9   r   r   r6   )r;   r   re   rl   r   r   )r-   r.   r/   r   rk   r4   r   r   r   r   r2   ]   s    
Wr2   c                   @  s*   e Zd Z				ddddZdddZdS )r`      Nr]   r   r\   intr^   typing.Mapping[str, str] | None
media_type
str | NonerS   &typing.Mapping[str, typing.Any] | Noner   r6   c                 C  s,   || _ || _|| _|| _| | d | _d S r7   )rS   body_iteratorr\   rr   init_headers
background)r   r]   r\   r^   rr   rS   r   r   r   r      s   

z_StreamingResponse.__init__r   r   r   r
   r9   r   c                   s   | j d u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rK|  I d H  d S d S )
NrT   )r   rS   rU   )r   r[   r^   rW   Tr    r#   F)rS   r\   ra   ru   rw   )r   r   r   r9   r*   r   r   r   rk      s    

z_StreamingResponse.__call__)ro   NNN)r]   r   r\   rp   r^   rq   rr   rs   rS   rt   r   r6   rn   )r-   r.   r/   r   rk   r   r   r   r   r`      s    r`   )
__future__r   typingrE   starlette._utilsr   starlette.requestsr   r   starlette.responsesr   r   starlette.typesr   r	   r
   r   r   Callable	Awaitablerl   DispatchFunctionTypeVarr   r   r2   r`   r   r   r   r   <module>   s    
L`