o
    j                     @  s   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZmZ dZdZG d	d
 d
Ze ZG dd deZG dd deZG dd dZG dd dZdddZdS )    )annotations)cast)Headers)HTTPException)PlainTextResponse)ASGIAppMessageReceiveScopeSendzstarlette.max_body_sizezstarlette._body_limit_responderc                   @  s   e Zd ZdZdS )_Missing N)__name__
__module____qualname__	__slots__r   r   r   ]/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/starlette/middleware/body_limit.pyr      s    r   c                      s   e Zd Zd fddZ  ZS )_RequestBodyTooLargereturnNonec                   s   t  jddd d S )N  Content Too Large)status_codedetail)super__init__self	__class__r   r   r      s   z_RequestBodyTooLarge.__init__)r   r   )r   r   r   r   __classcell__r   r   r   r   r      s    r   c                   @  s   e Zd ZdS )_RequestBodyLimitResponseSentN)r   r   r   r   r   r   r   r!      s    r!   c                   @  s$   e Zd ZdZddd	ZdddZdS )RequestBodyLimitMiddlewarez-Limit the total size of an HTTP request body.appr   max_body_sizeintr   r   c                 C  s   || _ || _d S N)r#   r$   r   r#   r$   r   r   r   r   !   s   
z#RequestBodyLimitMiddleware.__init__scoper
   receiver	   sendr   c                   sF   |d dkr|  |||I d H S t| j | j}||||I d H  d S )Ntypehttp)r#   RequestBodyLimitResponderr$   )r   r(   r)   r*   	responderr   r   r   __call__%   s
   z#RequestBodyLimitMiddleware.__call__Nr#   r   r$   r%   r   r   r(   r
   r)   r	   r*   r   r   r   )r   r   r   __doc__r   r/   r   r   r   r   r"      s    
r"   c                   @  s^   e Zd ZdddZedd
dZed ddZed!ddZd"ddZd#ddZ	d$ddZ
dS )%r-   r#   r   r$   r%   r   r   c                 C  s4   || _ || _d | _d | _d | _d | _d| _d| _d S )Nr   F)r#   r$   _scope_receive_sendcontent_length
total_sizeresponse_startedr'   r   r   r   r   .   s   
z"RequestBodyLimitResponder.__init__r
   c                 C     | j d usJ | j S r&   )r3   r   r   r   r   r(   8      zRequestBodyLimitResponder.scoper	   c                 C  r9   r&   )r4   r   r   r   r   r)   =   r:   z!RequestBodyLimitResponder.receiver   c                 C  r9   r&   )r5   r   r   r   r   r*   B   r:   zRequestBodyLimitResponder.sendr(   r)   r*   c              
     sX  t ttB |tt}| j|t< t td B |t}|d ur5| j|_|j	|jkr+t
| |||I d H S || _|| _|| _t|| _| |t< zMz| || j| jI d H  W n$ t
yr   | jra tddd}||||I d H  Y n	 tyz   Y nw W |td  t|tr|td  d S ||t< d S |td  t|tr|td  w ||t< w )Nr   r   r   )r   r%   r   getMAX_BODY_SIZE_SCOPE_KEY_MISSINGr$   r-   _BODY_LIMIT_RESPONDER_SCOPE_KEYr7   r   r#   r3   r4   r5   _get_content_lengthr6   receive_with_limitsend_with_limitr8   r   r!   pop
isinstance)r   r(   r)   r*   previous_scope_limitactive_responderresponser   r   r   r/   G   sB   



z"RequestBodyLimitResponder.__call__r   c                   sd   | j d ur| j | jkrt|  I d H }|d dkr0|  jt|dd7  _| j| jkr0t|S )Nr+   zhttp.requestbody    )r6   r$   r   r)   r7   lenr<   )r   messager   r   r   rA   h   s   z,RequestBodyLimitResponder.receive_with_limitrK   c                   sf   |d dkr)d| _ | jd ur)| j| jkr)tddd}|| j| j| jI d H  t| |I d H  d S )Nr+   zhttp.response.startTr   r   r;   )r8   r6   r$   r   r(   r)   r*   r!   )r   rK   rG   r   r   r   rB   s   s   z)RequestBodyLimitResponder.send_with_limitNr0   )r   r
   )r   r	   )r   r   r1   )r   r   )rK   r   r   r   )r   r   r   r   propertyr(   r)   r*   r/   rA   rB   r   r   r   r   r-   -   s    



!r-   r(   r
   r   
int | Nonec                 C  s<   t | dd}|d u rd S zt|W S  ty   Y d S w )N)r(   zcontent-length)r   r<   r%   
ValueError)r(   r6   r   r   r   r@   }   s   
r@   N)r(   r
   r   rM   )
__future__r   typingr   starlette.datastructuresr   starlette.exceptionsr   starlette.responsesr   starlette.typesr   r   r	   r
   r   r=   r?   r   r>   r   	Exceptionr!   r"   r-   r@   r   r   r   r   <module>   s    P