o
    ùT·j)  ã                   @  s  d dl mZ d dlmZmZmZmZmZmZ d dl	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
f B df ZeeeB eB eee
f B  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ÚCallableÚMappingÚMutableMapping)ÚAnyÚTypeVarN)Úcreate_collapsing_task_group)Ú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__© ú\/home/dinkstrade/pdmp-scanner/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   r2   Ú__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                 ƒ  sB  �ˆd dkrˆ  ˆ||¡I d H  d S tˆ|ƒ}|j‰t ¡ ‰d ‰ d‰d
‡ ‡‡‡‡‡‡‡‡f	dd	„}tjt ƒ \‰‰ˆ�Q ˆ�? tƒ 4 I d H š%‰ˆ ||¡I d H }|ˆˆ|ƒI d H  ˆ 	¡  ˆ 
¡  W d   ƒI d H  n1 I d H srw   Y  W d   ƒ n1 s�w   Y  W d   ƒ n1 s�w   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rA|d urAˆ ¡ I d H }W n tjy[   ˆd urWd‰ˆˆjpVˆj‚tdƒ‚w |d dksdJ ‚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)rC   Úresult©Ú
task_groupr!   r"   Úwrapw   s   €
z[BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnect.<locals>.wrap)rC   rD   r#   r   )Úis_setÚanyioÚcreate_task_groupÚ
start_soonÚwait)rJ   Úmessage)Úresponse_sentr2   rH   r"   Úreceive_or_disconnectq   s   €(ø
zMBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnectrP   r=   c                 “  sl   �z
ˆ  | ¡I d H  W n tjy   Y d S w | d dks)| d dkr2|  dd¡s4ˆ  ¡ I d H  d S d S d S )Nr$   úhttp.response.pathsendúhttp.response.bodyr)   F)r@   rL   ÚBrokenResourceErrorÚgetrO   )rP   )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_excrR   r   r   rX   rW   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$   rS   rT   zUnexpected message: r(   r*   r)   F)rV   )rP   r(   )Úrecv_streamr!   r"   Úbody_stream²   s   €ÿøzCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamÚstatus)Ústatus_codeÚcontentr]   Úheadersr3   )rP   r   r#   r=   )r#   r=   )r#   r`   )
rN   r   rV   rL   ÚEndOfStreamÚ	__cause__Ú__context__r,   Ú_StreamingResponseÚraw_headers)rB   r\   rP   r]   rb   Úresponse©	r[   Úexception_already_raisedra   rQ   r   r   rW   rI   r2   )rR   rX   r"   Ú	call_nextp   s,   €
	€
ò
z.BaseHTTPMiddleware.__call__.<locals>.call_next)rB   r   r#   r   )r:   r   r2   rL   ÚEventÚcreate_memory_object_streamr   r   r?   ÚsetÚclose)r   r   r   r@   rB   ro   rl   r!   rm   r"   Ú__call__e   s2   €
R
(ü€ÿ€ ÿzBaseHTTPMiddleware.__call__rB   r   ro   ÚRequestResponseEndpointr   c                 Ã  s   �t ƒ ‚r>   )ÚNotImplementedError)r   rB   ro   r!   r!   r"   r;   Ì   s   €zBaseHTTPMiddleware.dispatchr>   )r:   r   r;   r<   r#   r=   ©r   r   r   r   r@   r   r#   r=   )rB   r   ro   ru   r#   r   )r4   r5   r6   r   rt   r;   r!   r!   r!   r"   r9   `   s    
gr9   c                   @  s*   e Zd Z				dddd„Zddd„ZdS )rj   éÈ   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   r@   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   TFrT   r'   r*   )r]   rd   rk   r   Ú
isinstanceÚdictr�   )r   r   r   r@   Úshould_close_bodyr1   r!   r!   r"   rt   à   s,   €
ý
ÿ
úÿz_StreamingResponse.__call__)rx   NNN)re   ry   rd   rz   rf   r{   r|   r}   r]   r~   r#   r=   rw   )r4   r5   r6   r   rt   r!   r!   r!   r"   rj   Ð   s    úrj   )%Ú
__future__r   Úcollections.abcr   r   r   r   r   r   Útypingr	   r
   rL   Ústarlette._utilsr   Ústarlette.requestsr   r   Ústarlette.responsesr   Ústarlette.typesr   r   r   r   r   ru   ÚDispatchFunctionÚbytesÚstrr`   Ú
memoryviewry   r   r   r9   rj   r!   r!   r!   r"   Ú<module>   s      Lp