a
    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e	edddZdS )    N)NoReturn)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   U/var/www/html/assistant/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                    s^  |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  nr|dkr.|d
d}|dd}| j||d|d
< | |I d H  n,|dkrZ| | j I d H  | |I d H  d S )Nr   zhttp.response.startr$   )rawzcontent-encodingzcontent-typer    zhttp.response.bodyTbody    	more_bodyF)r7   r   zContent-EncodingzContent-Lengthzhttp.response.pathsend)r,   r   r.   r!   
startswithDEFAULT_EXCLUDED_CONTENT_TYPESr/   r-   r   lenr   apply_compressionr   Zadd_vary_headerr*   str)r   r3   Zmessage_typer$   r5   r7   r   r   r   r0   0   sZ    






z'IdentityResponder.send_with_compressionr5   r7   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   r5   r7   r   r   r   r;   m   s    z#IdentityResponder.apply_compression)r&   r'   r(   r<   __annotations__r   r)   r   r   r   r	   r%   r   r0   bytesboolr;   r   r   r   r   r#       s
   
	=r#   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   z   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   )rH   rJ   rE   r%   r1   rK   r   r   r%      s    zGZipResponder.__call__r=   c                C   s>   | j | |s| j   | j }| jd | j  |S )Nr   )rJ   writecloserH   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@   rA   r;   __classcell__r   r   rK   r   r"   w   s   r"   r2   c                    s   t dd S )Nzsend awaitable not set)RuntimeError)r3   r   r   r   r+      s    r+   )r   rF   typingr   Zstarlette.datastructuresr   r   Zstarlette.typesr   r   r   r   r	   r9   r
   r#   r"   r+   r   r   r   r   <module>   s   W