
    kj5                        d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	Z	ddl
Z ej                  dd      Z edddd      aed!ddded	ed
dfd       Zed!deded	ed
efd       Zed!deded	ed
eez  fd       Z	 d!ddez  ez  ded	ed
dez  ez  fdZed!ddded	ed
dfd       Zed!deded	ed
eez  fd       Zed!deded	ed
efd       Z	 d!ddez  ez  d
dez  ez  fdZd Zded
efdZded
efdZded
efdZded
efdZded
efdZded
efdZded
efdZded
efdZeeeeeeedZeeeeeeedZdd gZy)"z1
Utility functions for decoding response bodies.
    N)BytesIO)overloadCachedDecodezencoded encoding errors decodedencodedencodingerrorsreturnc                      y N r   r   r   s      _/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/mitmproxy/net/encoding.pydecoder          JM    c                      y r   r   r   s      r   r   r      s    HKr   c                      y r   r   r   s      r   r   r      s    RUr   c                 N   | y|j                         }t        | t              xr= t        j                  | k(  xr( t        j
                  |k(  xr t        j                  |k(  }|rt        j                  S 	 	 t        |   |       }|dv rt        | |||      a|S # t        $ r t        j                  | ||      }Y 6w xY w# t        $ r  t        $ rS}t        dj!                  t#        |      j$                  t'        |       dd t'        |      t'        |                  d}~ww xY w)z
    Decode the given input object

    Returns:
        The decoded value

    Raises:
        ValueError, if decoding fails.
    Ngzipdeflate
deflaterawbrzstdz{} when decoding {} with {}: {}
   )lower
isinstancebytes_cacher   r   r   decodedcustom_decodeKeyErrorcodecsr   r   	TypeError	Exception
ValueErrorformattype__name__repr)r   r   r   cachedr    es         r   r   r   #   )    ~~H 	7E" 	$NNg%	$OOx'	$ MMV#	  ~~
	?#H-g6G FF!'8VWEF	  	?mmGXv>G	?
   
-44Q  Wcr"XQ	
 	

7   7B B?  B<9B? ;B<<B? ?D$ADD$r    c                      y r   r   r    r   r   s      r   encoder1   Q   r   r   c                      y r   r   r0   s      r   r1   r1   U   s    PSr   c                      y r   r   r0   s      r   r1   r1   Y   s    LOr   c                 N   | y|j                         }t        | t              xr= t        j                  | k(  xr( t        j
                  |k(  xr t        j                  |k(  }|rt        j                  S 	 	 t        |   |       }|dv rt        ||||       a|S # t        $ r t        j                  | ||      }Y 6w xY w# t        $ r  t        $ rS}t        dj!                  t#        |      j$                  t'        |       dd t'        |      t'        |                  d}~ww xY w)z
    Encode the given input object

    Returns:
        The encoded value

    Raises:
        ValueError, if encoding fails.
    Nr   z{} when encoding {} with {}: {}r   )r   r   r   r   r    r   r   r   custom_encoder"   r#   r1   r   r$   r%   r&   r'   r(   r)   r*   )r    r   r   r+   r   r,   s         r   r1   r1   ]   r-   r.   c                     | S )zb
    Returns content unchanged. Identity is the default value of
    Accept-Encoding headers.
    r   contents    r   identityr9      s	    
 Nr   r8   c                     | sy	 t        j                  d      }|j                  |       |j                         z   S # t         j                  $ r}t        d|       d}~ww xY w)z@Decode gzip or zlib-compressed data using zlib's auto-detection.r   /   zDecompression failed: N)zlibdecompressobj
decompressflusherrorr&   )r8   decompressorr,   s      r   decode_gziprB      se    7 ))"-&&w/,2D2D2FFF:: 71!5667s   6< A"AA"c                     t               }t        j                  |ddd      5 }|j                  |        d d d        |j	                         S # 1 sw Y   |j	                         S xY w)Nwbr      )fileobjmodemtimecompresslevel)r   r   GzipFilewritegetvalue)r8   sfs      r   encode_gziprO      sS    	A 
qt1A	F !	::<::<s   AA&c                 2    | syt        j                  |       S )Nr   )brotlir>   r7   s    r   decode_brotlirR      s    W%%r   c                 0    t        j                  | d      S )Nr   )quality)rQ   compressr7   s    r   encode_brotlirV      s    ??7A..r   c                     | syt        j                         }|j                  t        |       d      j	                         S )Nr   T)read_across_frames)r   ZstdDecompressorstream_readerr   readr8   zstd_ctxs     r   decode_zstdr^      s:    $$&H!!''"2t!LQQSSr   c                 P    t        j                  d      }|j                  |       S )NrE   level)r   ZstdCompressorrU   r\   s     r   encode_zstdrc      s#    ""+HW%%r   c                     | sy	 t        j                  |       S # t         j                  $ r t        j                  | d      cY S w xY w)a  
    Returns decompressed data for DEFLATE. Some servers may respond with
    compressed data without a zlib header or checksum. An undocumented
    feature of zlib permits the lenient decompression of data missing both
    values.

    http://bugs.python.org/issue5784
    r   i)r<   r>   r@   r7   s    r   decode_deflatere      sB     -w'':: -w,,-s    )AAc                 0    t        j                  | d      S )zP
    Returns compressed content, always including zlib header and checksum.
    rE   r`   )r<   rU   r7   s    r   encode_deflaterg      s    
 ==**r   )noner9   r   r   r   r   r   r1   r   )strict)__doc__r#   collectionsr   r<   ior   typingr   rQ   	zstandardr   
namedtupler   r   strr   r   r1   r9   rB   rO   rR   rV   r^   rc   re   rg   r!   r5   __all__r   r   r   <module>rr      s~           &{%%n6WX	dD$	- 
 MD MC M MD M 
 M 
 KC K3 K K3 K 
 K 
 UE US U# US5[ U 
 U ?G+
CZ%+
+.+
8;+
	CZ%+
\ 
 MD MC M MD M 
 M 
 SC S3 S S3; S 
 S 
 OE OS O# OU O 
 O 3;+
CZ%+
	CZ%+
\7 75 7 5 &5 &U &/5 /U /
T T5 T& &5 &-E -e -"+E +e +  
  
 X
r   