
    PiN"                         d Z ddlZddlZddlmZ ddlmZmZmZ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  G d d	          Z G d
 d          Z G d d          ZdedefdZdS )zkAGSI Brotli middleware build on top of starlette.

Code is based on GZipMiddleware shipped with starlette.
    N)NoReturn)	MODE_FONTMODE_GENERIC	MODE_TEXT
Compressor)HeadersMutableHeaders)GZipResponder)ASGIAppMessageReceiveScopeSendc                       e Zd ZdZeZeZeZ	dS )ModezBrotli available modes.N)
__name__
__module____qualname____doc__r   genericr   textr   font     l/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/gradio/brotli_middleware.pyr   r      s#        !!GDDDDr   r   c                       e Zd ZdZ	 	 	 	 	 	 	 dd	ed
ededededededee         dz  ddfdZ	de
dededdfdZde
defdZde
defdZdS )BrotliMiddlewarez#Brotli middleware public interface.   r      r     TNappqualitymodelgwinlgblockminimum_sizegzip_fallbackexcluded_handlersreturnc	                     || _         || _        t          t          |          | _        || _        || _        || _        || _        |rd |D             | _	        dS g | _	        dS )ao  
        Arguments.

        mode: The compression mode can be:
            generic, text (*default*. Used for UTF-8 format text input)
            or font (for WOFF 2.0).
        quality: Controls the compression-speed vs compression-
            density tradeoff. The higher the quality, the slower the compression.
            Range is 0 to 11.
        lgwin: Base 2 logarithm of the sliding window size. Range
            is 10 to 24.
        lgblock: Base 2 logarithm of the maximum input block size.
            Range is 16 to 24. If set to 0, the value will be set based on the
            quality.
        minimum_size: Only compress responses that are bigger than this value in bytes.
        gzip_fallback: If True, uses gzip encoding if br is not in the Accept-Encoding header.
        excluded_handlers: List of handlers to be excluded from being compressed.
        c                 6    g | ]}t          j        |          S r   )recompile).0paths     r   
<listcomp>z-BrotliMiddleware.__init__.<locals>.<listcomp>@   s"    %U%U%U4bj&6&6%U%U%Ur   N)
r!   r"   getattrr   r#   r&   r$   r%   r'   r(   )	selfr!   r"   r#   r$   r%   r&   r'   r(   s	            r   __init__zBrotliMiddleware.__init__   st    : D$''	(
* 	(%U%UCT%U%U%UD"""%'D"""r   scopereceivesendc                 d  K   |                      |          s!|d         dk    s|                     |          s|                     |||           d {V S t          |          }d|                    dd          v rGt          | j        | j        | j        | j        | j	        | j
                  } ||||           d {V  d S | j        rGd|                    dd          v r/t          | j        | j
                  } ||||           d {V  d S |                     |||           d {V  d S )Ntypehttp)r4   brAccept-Encoding gzip)_is_handler_excluded_is_compressible_file_typer!   r   getBrotliResponderr"   r#   r$   r%   r&   r'   r
   )r2   r4   r5   r6   headersbr_respondergzip_responders          r   __call__zBrotliMiddleware.__call__D   s     %%e,,	8V}&&22599 ' %$777777777&&&7;;0"5555*	
! L ,ugt444444444F 	&GKK8I2,N,N"N"N*48T5FGGN .666666666Fhhugt,,,,,,,,,,,r   c                 p    |                     dd          t          fd| j        D                       S )Nr/   r<   c              3   B   K   | ]}|                               V  d S N)search)r.   patternhandlers     r   	<genexpr>z8BrotliMiddleware._is_handler_excluded.<locals>.<genexpr>`   s/      QQw7>>'**QQQQQQr   )r@   anyr(   )r2   r4   rK   s     @r   r>   z%BrotliMiddleware._is_handler_excluded]   s<    ))FB''QQQQ$:PQQQQQQr   c                     |                     dd          }h d}d|v r4d|                    d          d                                         z   }||v S dS )z>Check if the requested file has a compressible file extension.r/   r<   >   .js.md.css.csv.htm.svg.tsv.txt.xml.html.json.F)r@   splitlower)r2   r4   r/   compressible_extensions	extensions        r   r?   z+BrotliMiddleware._is_compressible_file_typed   sh    yy$$#
 #
 #
 $;;djjoob177999I 777ur   )r   r   r   r   r    TN)r   r   r   r   r   intstrboollistr3   r   r   r   rE   r>   r?   r   r   r   r   r      s&       --
 ".2'( '('( '( 	'(
 '( '( '( '(  9t+'( 
'( '( '( '(R-E -G -4 -D - - - -2R% RD R R R R $      r   r   c                   d    e Zd ZdZdededededededd	fd
Zdede	de
dd	fdZdedd	fdZd Zd	S )rA   zBrotli Interface.r!   r"   r#   r$   r%   r&   r)   Nc                 $   || _         || _        || _        || _        || _        || _        t          | _        i | _        d| _	        d| _
        t          | j        | j        | j        | j                  | _        t          j                    | _        d S )NF)r"   r#   r$   r%   )r!   r"   r#   r$   r%   r&   unattached_sendr6   initial_messagestartedcontent_encoding_setr   br_fileioBytesIO	br_buffer)r2   r!   r"   r#   r$   r%   r&   s          r   r3   zBrotliResponder.__init__~   s     	
()	(*$)!!Lty
DL
 
 
 r   r4   r5   r6   c                 \   K   || _         |                     ||| j                   d {V  d S rH   )r6   r!   send_with_brotli)r2   r4   r5   r6   s       r   rE   zBrotliResponder.__call__   s>      	hhugt'<===========r   messagec                   K   |d         }|dk    r-|| _         t          | j         d                   }d|v | _        dS |dk    rR| j        rK| j        s'd| _        |                     | j                    d{V  |                     |           d{V  dS |dk    rd| j        s\d| _        |                    d	d
          }|                    dd          }t          |          | j        k     r?|s=|                     | j                    d{V  |                     |           d{V  dS |s|                     |          | j	        
                                z   }t          | j         d                   }d|d<   t          t          |                    |d<   |                    d           ||d	<   |                     | j                    d{V  |                     |           d{V  dS t          | j         d                   }d|d<   |                    d           |d= | j                            |                     |          | j	                                        z              | j                                        |d	<   | j                            d           | j                                         |                     | j                    d{V  |                     |           d{V  dS |dk    rd|                    d	d
          }|                    dd          }| j                            |                     |          | j	                                        z              |s| j                            | j	        
                                           | j                                        |d	<   | j                                         |                     |           d{V  dS | j                                        |d	<   | j                            d           | j                                         |                     |           d{V  dS dS )zApply compression using brotli.r8   zhttp.response.startrB   )rawzcontent-encodingzhttp.response.bodyTNbodyr   	more_bodyFr:   zContent-EncodingzContent-Lengthr;   r   )rg   r   ri   rh   r6   r@   lenr&   _processrj   finishr	   ra   add_vary_headerrm   writeflushgetvalueseektruncateclose)r2   rp   message_typerB   rs   rt   s         r   ro   z BrotliResponder.send_with_brotli   s     v000 $+D $"6y"ABBBG(:g(ED%%%111d6O1< 6#ii 4555555555))G$$$$$$$$$$$111$,1DL;;vs++DK77I4yy4,,,Y,ii 4555555555ii((((((((((( )}}T**T\-@-@-B-BB(T-A)-LMMM.2*+,/D		NN()''(9:::"&ii 4555555555ii((((((((((( )T-A)-LMMM.2*+''(9:::,-$$T]]4%8%84<;M;M;O;O%OPPP"&."9"9";";##A&&&'')))ii 4555555555ii(((((((((((111;;vs++DK77IN  t!4!4t|7I7I7K7K!KLLL $$T\%8%8%:%:;;;"&."9"9";";$$&&&ii((((((((("n5577GFON"""N##%%%))G$$$$$$$$$$$ 21r   c                     t          | j        d          r| j                            |          S | j                            |          S )aZ  Workaround to support both brotli and brotlipy

        Before the official Google brotli repository offered a Python version,
        there was a separate package to connect to brotli. These APIs are nearly
        identical except that the official Google API has Compressor.process
        while the brotlipy API has Compress.compress
        process)hasattrrj   r   compress)r2   rs   s     r   rv   zBrotliResponder._process   sD     4<++ 	.<''---|$$T***r   )r   r   r   r   r   r`   r   r3   r   r   r   rE   r   ro   rv   r   r   r   rA   rA   {   s        && & 	&
 & & & 
& & & &0>E >G >4 >D > > > ><%g <%$ <% <% <% <%|+ + + + +r   rA   _r)   c                 $   K   t          d          )Nzsend awaitable not set)RuntimeError)r   s    r   rf   rf      s      
/
0
00r   )r   rk   r,   typingr   brotlir   r   r   r   starlette.datastructuresr   r	   starlette.middleware.gzipr
   starlette.typesr   r   r   r   r   r   r   rA   rf   r   r   r   <module>r      sc   
 
			 				       A A A A A A A A A A A A < < < < < < < < 3 3 3 3 3 3 B B B B B B B B B B B B B B       ` ` ` ` ` ` ` `Fh+ h+ h+ h+ h+ h+ h+ h+V1W 1 1 1 1 1 1 1r   