
    vh                         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      Z G d d	      Z G d
 de      Zde	defdZy)    N)NoReturn)HeadersMutableHeaders)ASGIAppMessageReceiveScopeSend)ztext/event-streamc            	       >    e Zd ZddedededdfdZdeded	eddfd
Z	y)GZipMiddlewareappminimum_sizecompresslevelreturnNc                 .    || _         || _        || _        y N)r   r   r   )selfr   r   r   s       Q/var/www/html/luna/venv/lib/python3.12/site-packages/starlette/middleware/gzip.py__init__zGZipMiddleware.__init__   s    (*    scopereceivesendc                 Z  K   |d   dk7  r| j                  |||       d {    y t        |      }d|j                  dd      v r-t        | j                   | j                  | j
                        }n t        | j                   | j                        } ||||       d {    y 7 7 w)Ntypehttp)r   gzipAccept-Encoding )r   )r   r   getGZipResponderr   r   IdentityResponder)r   r   r   r   headers	responders         r   __call__zGZipMiddleware.__call__   s     =F"((5'4000&W[[!2B77%dhh0A0AQUQcQcdI)$((D4E4EFIw--- 1 	.s"   B+B'A?B+!B)"B+)B+)i  	   )
__name__
__module____qualname__r   intr   r	   r   r
   r%    r   r   r   r      sE    +G +3 +S +Y] +
.E .G .4 .D .r   r   c                   h    e Zd ZU eed<   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ededefdZy)r"   content_encodingr   r   r   Nc                 n    || _         || _        t        | _        i | _        d| _        d| _        d| _        y )NF)r   r   unattached_sendr   initial_messagestartedcontent_encoding_setcontent_type_is_excluded)r   r   r   s      r   r   zIdentityResponder.__init__#   s7    ()	(*$)!(-%r   r   r   r   c                 h   K   || _         | j                  ||| j                         d {    y 7 wr   )r   r   send_with_compression)r   r   r   r   s       r   r%   zIdentityResponder.__call__,   s)     	hhugt'A'ABBBs   (202messagec                   K   |d   }|dk(  rT|| _         t        | j                   d         }d|v | _        |j                  dd      j	                  t
              | _        y |dk(  rh| j                  s| j                  rP| j                  s*d	| _        | j                  | j                          d {    | j                  |       d {    y |dk(  r| j                  sd	| _        |j                  d
d      }|j                  dd      }t        |      | j                  k  r?|s=| j                  | j                          d {    | j                  |       d {    y |s| j                  |d      }t        | j                   d         }|j                  d       ||d
   k7  r+| j                  |d<   t        t        |            |d<   ||d
<   | j                  | j                          d {    | j                  |       d {    y | j                  |d	      }t        | j                   d         }|j                  d       ||d
   k7  r| j                  |d<   |d= ||d
<   | j                  | j                          d {    | j                  |       d {    y |dk(  rT|j                  d
d      }|j                  dd      }| j                  ||      |d
<   | j                  |       d {    y |dk(  r=| j                  | j                          d {    | j                  |       d {    y y 7 7 7 	7 7 `7 J7 7 7 ^7 77  w)Nr   zhttp.response.startr#   )rawzcontent-encodingzcontent-typer   zhttp.response.bodyTbodyr   	more_bodyF)r:   r   zContent-EncodingzContent-Lengthzhttp.response.pathsend)r0   r   r2   r    
startswithDEFAULT_EXCLUDED_CONTENT_TYPESr3   r1   r   lenr   apply_compressionr   add_vary_headerr-   str)r   r6   message_typer#   r9   r:   s         r   r5   z'IdentityResponder.send_with_compression0   s     v00 $+D $"6"6y"ABG(:g(ED%,3KK,K,V,VWu,vD)11t7P7PTXTqTq<<#ii 4 4555))G$$$11$,,DL;;vs+DK7I4y4,,,Yii 4 4555ii(((--de-D(T-A-A)-LM''(9:76?*262G2GG./03CIG,-&*GFOii 4 4555ii((( --dd-C(T-A-A)-LM''(9:76?*262G2GG./ 01&*GFOii 4 4555ii(((11;;vs+DK7I"44TY4OGFO))G$$$55))D00111))G$$$ 6Y 6$ 6( 6( 6( % 2$s   B-M#/M0M#M
	A;M#MM#MBM#3M4M#MA?M#MM#%M&AM#>M?(M#'M(M# M!M#
M#M#M#M#M#M#M#M#M#!M#r9   r:   c                    |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   r9   r:   s      r   r>   z#IdentityResponder.apply_compressionm   s	     r   )r'   r(   r)   r@   __annotations__r   r*   r   r	   r   r
   r%   r   r5   bytesboolr>   r+   r   r   r"   r"       sv    .G .3 .4 .CE CG C4 CD C;%7 ;%t ;%ze 4 E r   r"   c            	       f     e Zd ZdZddedededdf fdZded	ed
e	ddf fdZ
dededefdZ xZS )r!   r   r   r   r   r   Nc                     t         |   ||       t        j                         | _        t        j                  d| j                  |      | _        y )Nwb)modefileobjr   )superr   ioBytesIOgzip_bufferr   GzipFile	gzip_file)r   r   r   r   	__class__s       r   r   zGZipResponder.__init__z   s;    l+::<D$:J:JZghr   r   r   r   c                    K   | j                   5  | j                  5  t        |   |||       d {    d d d        d d d        y 7 # 1 sw Y   xY w# 1 sw Y   y xY wwr   )rO   rQ   rL   r%   )r   r   r   r   rR   s       r   r%   zGZipResponder.__call__   s_      	9t~~ 	9'"5'4888	9 	9 	98	9 	9 	9 	9sA   A"AA
AA
A	A"A

A	AAA"r9   r:   c                   | j                   j                  |       |s| j                   j                          | j                  j	                         }| j                  j                  d       | j                  j                          |S )Nr   )rQ   writecloserO   getvalueseektruncaterC   s      r   r>   zGZipResponder.apply_compression   sf    T"NN  "((*a !!#r   )r&   )r'   r(   r)   r-   r   r*   r   r	   r   r
   r%   rE   rF   r>   __classcell__)rR   s   @r   r!   r!   w   sk    iG i3 is iSW i9E 9G 94 9D 9	e 	4 	E 	r   r!   r6   r   c                     K   t        d      w)Nzsend awaitable not set)RuntimeError)r6   s    r   r/   r/      s     
/
00s   )r   rM   typingr   starlette.datastructuresr   r   starlette.typesr   r   r   r	   r
   r<   r   r"   r!   r/   r+   r   r   <module>r`      sT     	  < B B!7 . .*T Tn% 217 1x 1r   