o
    é’OjV(  ã                   @  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__© ú_/var/www/html/pharmsmart-cdr-gen/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 ¡ }|\‰‰ˆ�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r@|d ur@ˆ ¡ I d H }W n tjyZ   ˆd urVd‰ˆˆjpUˆj‚tdƒ‚w |d dkscJ ‚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                 “  s0   �zˆ   | ¡I d H  W d S  tjy   Y d S w r>   )r@   rL   ÚBrokenResourceError)rP   )Ú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   rU   rT   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)rP   r(   )Úrecv_streamr!   r"   Úbody_stream­   s   €ÿøzCBaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.body_streamÚstatus)Ústatus_codeÚcontentrZ   Úheadersr3   )rP   r   r#   r=   )r#   r=   )r#   r]   )
rN   r   r_   rL   ÚEndOfStreamÚ	__cause__Ú__context__r,   Ú_StreamingResponseÚraw_headers)rB   rY   rP   rZ   ra   Úresponse©	rX   Úexception_already_raisedr`   rQ   r   r   rT   rI   r2   )rR   rU   r"   Ú	call_nextp   s,   €
	€
ò
z.BaseHTTPMiddleware.__call__.<locals>.call_next)rB   r   r#   r   )
r:   r   r2   rL   ÚEventÚcreate_memory_object_streamr   r?   ÚsetÚclose)r   r   r   r@   rB   rn   Ústreamsrk   r!   rl   r"   Ú__call__e   s4   €
M
(ü€ÿ€ ÿzBaseHTTPMiddleware.__call__rB   r   rn   ÚRequestResponseEndpointr   c                 Ã  s   �t ƒ ‚r>   )ÚNotImplementedError)r   rB   rn   r!   r!   r"   r;   È   s   €zBaseHTTPMiddleware.dispatchr>   )r:   r   r;   r<   r#   r=   ©r   r   r   r   r@   r   r#   r=   )rB   r   rn   ru   r#   r   )r4   r5   r6   r   rt   r;   r!   r!   r!   r"   r9   `   s    
cr9   c                   @  s*   e Zd Z				dddd„Zddd„ZdS )ri   éÈ   Nrd   ÚAsyncContentStreamrc   Úintre   úMapping[str, str] | NoneÚ
media_typeú
str | NonerZ   úMapping[str, Any] | Noner#   r=   c                 C  s,   || _ || _|| _|| _|  |¡ d | _d S r>   )rZ   Úbody_iteratorrc   r|   Úinit_headersÚ
background)r   rd   rc   re   r|   rZ   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$   rZ   r\   )r$   rb   re   TFr^   r'   r*   )rZ   rc   rj   r   Ú
isinstanceÚdictr�   )r   r   r   r@   Úshould_close_bodyr1   r!   r!   r"   rt   Ü   s,   €
ý
ÿ
úÿz_StreamingResponse.__call__)rx   NNN)rd   ry   rc   rz   re   r{   r|   r}   rZ   r~   r#   r=   rw   )r4   r5   r6   r   rt   r!   r!   r!   r"   ri   Ì   s    úri   )%Ú
__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   ri   r!   r!   r!   r"   Ú<module>   s      Ll