a
    &gA                     @   s   d dl Z d dlZd dl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eejdddZdS )    N)HeadersMutableHeaders)ASGIAppMessageReceiveScopeSend)ztext/event-streamc                   @   s6   e Zd Zd
eeeddddZeeedddd	Z	dS )GZipMiddleware  	   Nappminimum_sizecompresslevelreturnc                 C   s   || _ || _|| _d S N)r   r   r   selfr   r   r    r   W/var/www/html/texto_audio/venv/lib/python3.9/site-packages/starlette/middleware/gzip.py__init__   s    zGZipMiddleware.__init__scopereceivesendr   c                    sx   |d dkr$|  |||I d H  d S t|d}d|ddv rTt| 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   headersZ	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                   @   s^   e Zd ZU eed< eeddddZee	e
ddddZedd	d
dZeeedddZdS )r"   content_encodingN)r   r   r   c                 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   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__messager   c                    s0  |d }|dkrH|| _ t| j d d}d|v | _|ddt| _n|dkr| js\| jr| jszd	| _| | j I d H  | |I d H  n|dkr| jsd	| _|d
d}|dd}t	|| j
k r|s| | j I d H  | |I d H  n|sv| j|dd}t| j d d}|d ||d
 krR| j|d< tt	||d< ||d
< | | j I d H  | |I d H  np| j|d	d}t| j d d}|d ||d
 kr| j|d< |d= ||d
< | | j I d H  | |I d H  nD|dkr,|d
d}|dd}| j||d|d
< | |I d H  d S )Nr   zhttp.response.startr#   )rawzcontent-encodingzcontent-typer   zhttp.response.bodyTbody    	more_bodyF)r6   r   zContent-EncodingzContent-Length)r+   r   r-   r    
startswithDEFAULT_EXCLUDED_CONTENT_TYPESr.   r,   r   lenr   apply_compressionr   Zadd_vary_headerr)   str)r   r2   Zmessage_typer#   r4   r6   r   r   r   r/   0   sT    





z'IdentityResponder.send_with_compressionr4   r6   r   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   r4   r6   r   r   r   r:   i   s    z#IdentityResponder.apply_compression)r%   r&   r'   r;   __annotations__r   r(   r   r   r   r   r$   r   r/   bytesboolr:   r   r   r   r   r"       s
   
	9r"   c                       sX   e Zd ZdZdeeedd fddZeee	dd fdd	Z
eeed
ddZ  ZS )r!   r   r   Nr   c                    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   v   s    
zGZipResponder.__init__r   c              	      sf   | j L | j& t |||I d H  W d    n1 s:0    Y  W d    n1 sX0    Y  d S r   )rG   rI   rD   r$   r0   rJ   r   r   r$   |   s    zGZipResponder.__call__r<   c                C   s>   | j | |s| j   | j }| jd | j  |S )Nr   )rI   writecloserG   getvalueseektruncater=   r   r   r   r:      s    


zGZipResponder.apply_compression)r   )r%   r&   r'   r)   r   r(   r   r   r   r   r$   r?   r@   r:   __classcell__r   r   rJ   r   r!   s   s   r!   r1   c                    s   t dd S )Nzsend awaitable not set)RuntimeError)r2   r   r   r   r*      s    r*   )r   rE   typingZstarlette.datastructuresr   r   Zstarlette.typesr   r   r   r   r   r8   r	   r"   r!   NoReturnr*   r   r   r   r   <module>   s   S