o
    ђh                     @   s   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	m
Z
mZmZ dZG dd dZG dd	 d	ZG d
d deZde	defddZdS )    N)NoReturn)HeadersMutableHeaders)ASGIAppMessageReceiveScopeSend)ztext/event-streamc                	   @   sB   e Zd Zddedededdfdd	Zd
edededdfddZ	dS )GZipMiddleware  	   appminimum_sizecompresslevelreturnNc                 C   s   || _ || _|| _d S N)r   r   r   selfr   r   r    r   R/var/www/html/govbot/env/lib/python3.10/site-packages/starlette/middleware/gzip.py__init__   s   
zGZipMiddleware.__init__scopereceivesendc                    sz   |d dkr|  |||I d H  d S t|d}d|ddv r+t| j | j| jd}nt| j | j}||||I d H  d S )Ntypehttp)r   gzipAccept-Encoding )r   )r   r   getGZipResponderr   r   IdentityResponder)r   r   r   r   headers	responderr   r   r   __call__   s   
zGZipMiddleware.__call__)r   r   )
__name__
__module____qualname__r   intr   r   r   r	   r$   r   r   r   r   r
      s    r
   c                   @   sn   e Zd ZU eed< dededdfddZded	e	d
e
ddfddZdeddfddZdededefddZdS )r!   content_encodingr   r   r   Nc                 C   s.   || _ || _t| _i | _d| _d| _d| _d S )NF)r   r   unattached_sendr   initial_messagestartedcontent_encoding_setcontent_type_is_excluded)r   r   r   r   r   r   r   #   s   
zIdentityResponder.__init__r   r   r   c                    s"   || _ | ||| jI d H  d S r   )r   r   send_with_compressionr   r   r   r   r   r   r   r$   ,   s   zIdentityResponder.__call__messagec                    sb  |d }|dkr%|| _ t| j d d}d|v | _|ddt| _d S |dkrH| js/| jrH| js>d	| _| | j I d H  | |I d H  d S |dkr| jsd	| _|d
d}|dd}t	|| j
k rz|sz| | j I d H  | |I d H  d S |s| j|dd}t| j d d}|d ||d
 kr| j|d< tt	||d< ||d
< | | j I d H  | |I d H  d S | j|d	d}t| j d d}|d ||d
 kr| j|d< |d= ||d
< | | j I d H  | |I d H  d S |dkr|d
d}|dd}| j||d|d
< | |I d H  d S |dkr/| | j I d H  | |I d H  d S d S )Nr   zhttp.response.startr"   )rawzcontent-encodingzcontent-typer   zhttp.response.bodyTbody    	more_bodyF)r5   r   zContent-EncodingzContent-Lengthzhttp.response.pathsend)r+   r   r-   r   
startswithDEFAULT_EXCLUDED_CONTENT_TYPESr.   r,   r   lenr   apply_compressionr   add_vary_headerr)   str)r   r1   message_typer"   r3   r5   r   r   r   r/   0   s^   






z'IdentityResponder.send_with_compressionr3   r5   c                C   s   |S )zApply compression on the response body.

        If more_body is False, any compression file should be closed. If it
        isn't, it won't be closed automatically until all background tasks
        complete.
        r   r   r3   r5   r   r   r   r9   m   s   z#IdentityResponder.apply_compression)r%   r&   r'   r;   __annotations__r   r(   r   r   r   r	   r$   r   r/   bytesboolr9   r   r   r   r   r!       s   
 	=r!   c                	       sh   e Zd ZdZddedededdf fdd	Zd
edede	ddf fddZ
dededefddZ  ZS )r    r   r   r   r   r   r   Nc                    s0   t  || t | _tjd| j|d| _d S )Nwb)modefileobjr   )superr   ioBytesIOgzip_bufferr   GzipFile	gzip_filer   	__class__r   r   r   z   s   
zGZipResponder.__init__r   r   r   c              	      sz   | j / | j t |||I d H  W d    n1 sw   Y  W d    d S W d    d S 1 s6w   Y  d S r   )rG   rI   rD   r$   r0   rJ   r   r   r$      s   PzGZipResponder.__call__r3   r5   c                C   s>   | j | |s| j   | j }| jd | j  |S )Nr   )rI   writecloserG   getvalueseektruncater=   r   r   r   r9      s   


zGZipResponder.apply_compression)r   )r%   r&   r'   r)   r   r(   r   r   r   r	   r$   r?   r@   r9   __classcell__r   r   rJ   r   r    w   s
     r    r1   r   c                    s
   t d)Nzsend awaitable not set)RuntimeError)r1   r   r   r   r*      s   r*   )r   rE   typingr   starlette.datastructuresr   r   starlette.typesr   r   r   r   r	   r7   r
   r!   r    r*   r   r   r   r   <module>   s    W