
    osjU4                    6   U d Z ddlmZ ddlZddl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Zej                  rIej                  dk\  r	ddlmZmZ n)ddlmZmZ  ej*                   e       j,                        ZdZd	ed
<   ndZ	 ddlmZmZ dZ G d d      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d      Z  G d d      Z! G d d      Z" G d  d!      Z#eeeeed"Z$d#ed$<   ee$jK                  d%       ese$jK                  d&       yy# e$ r 	 ddlZn# e$ r dZY nw xY wY &w xY w# e$ r< 	 ddlmZmZ  ej*                   e       j,                        ZdZn# e$ r Y nw xY wY w xY w)'zq
Handlers for Content-Encoding.

See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
    )annotationsN   )DecodingError)      )ZstdDecompressor	ZstdErrorFbool_zstandard_installedTc                      e Zd ZddZddZy)ContentDecoderc                    t               NNotImplementedErrorselfdatas     C/root/.hermes/venv/lib/python3.12/site-packages/httpx2/_decoders.pydecodezContentDecoder.decode=       !##    c                    t               r   r   r   s    r   flushzContentDecoder.flush@   r   r   Nr   bytesreturnr   r   r   )__name__
__module____qualname__r   r    r   r   r   r   <   s    $$r   r   c                       e Zd ZdZddZddZy)IdentityDecoderz 
    Handle unencoded data.
    c                    |S r   r#   r   s     r   r   zIdentityDecoder.decodeI   s    r   c                     yNr   r#   r   s    r   r   zIdentityDecoder.flushL   s    r   Nr   r   )r    r!   r"   __doc__r   r   r#   r   r   r%   r%   D   s    r   r%   c                  (    e Zd ZdZddZddZddZy)	DeflateDecoderzZ
    Handle 'deflate' decoding.

    See: https://stackoverflow.com/questions/1838699
    c                D    d| _         t        j                         | _        y )NT)first_attemptzlibdecompressobjdecompressorr   s    r   __init__zDeflateDecoder.__init__W   s    ! ..0r   c                B   | j                   }d| _         	 | j                  j                  |      S # t        j                  $ r[}|r?t        j
                  t        j                         | _        | j                  |      cY d }~S t        t        |            |d }~ww xY wNF)
r-   r0   
decompressr.   errorr/   	MAX_WBITSr   r   str)r   r   was_first_attemptexcs       r   r   zDeflateDecoder.decode[   s     .."	3$$//55zz 	3 $($6$6$G!{{4((C)s2		3s!   0 B;B>BBBc                    	 | j                   j                         S # t        j                  $ r}t	        t        |            |d }~ww xY wr   r0   r   r.   r5   r   r7   r   r9   s     r   r   zDeflateDecoder.flushf   A    	3$$**,,zz 	3C)s2	3    A	AA	Nr   Noner   r   r    r!   r"   r)   r1   r   r   r#   r   r   r+   r+   P   s    1	33r   r+   c                  (    e Zd ZdZddZddZddZy)	GZipDecoderzW
    Handle 'gzip' decoding.

    See: https://stackoverflow.com/questions/1838699
    c                Z    t        j                  t         j                  dz        | _        y )N   )r.   r/   r6   r0   r   s    r   r1   zGZipDecoder.__init__t   s     ..t~~/BCr   c                    	 | j                   j                  |      S # t        j                  $ r}t	        t        |            |d }~ww xY wr   )r0   r4   r.   r5   r   r7   r   r   r9   s      r   r   zGZipDecoder.decodew   sC    	3$$//55zz 	3C)s2	3s    A
AA
c                    	 | j                   j                         S # t        j                  $ r}t	        t        |            |d }~ww xY wr   r;   r<   s     r   r   zGZipDecoder.flush}   r=   r>   Nr?   r   r   rA   r#   r   r   rC   rC   m   s    D33r   rC   c                  (    e Zd ZdZddZddZddZy)	BrotliDecoderaT  
    Handle 'brotli' decoding.

    Requires `pip install brotlipy`. See: https://brotlipy.readthedocs.io/
        or   `pip install brotli`. See https://github.com/google/brotli
    Supports both 'brotlipy' and 'Brotli' packages since they share an import
    name. The top branches are for 'brotlipy' and bottom branches for 'Brotli'
    c                   t         t        d      d t        j                         | _        d| _        |  t        | j                  d      r| j                  j                  | _        y | j                  j                  | _        y )NzUsing 'BrotliDecoder', but neither of the 'brotlicffi' or 'brotli' packages have been installed. Make sure to install httpx using `pip install httpx[brotli]`.Fr4   )	brotliImportErrorDecompressorr0   	seen_datahasattrr4   _decompressprocessr   s    r   r1   zBrotliDecoder.__init__   sv    >P 	 #//14$$l3#00;;D  $0088Dr   c                    |syd| _         	 | j                  |      S # t        j                  $ r}t	        t        |            |d }~ww xY wNr   T)rO   rQ   rL   r5   r   r7   rG   s      r   r   zBrotliDecoder.decode   sJ    	3##D))|| 	3C)s2	3s    A
AA
c                    | j                   sy	 t        | j                  d      r| j                  j                          y# t        j
                  $ r}t        t        |            |d }~ww xY w)Nr   finish)rO   rP   r0   rV   rL   r5   r   r7   r<   s     r   r   zBrotliDecoder.flush   s]    ~~
	3t(((3 !!((*|| 	3C)s2	3s   0A   A-A((A-Nr?   r   r   rA   r#   r   r   rJ   rJ      s    9$33r   rJ   c                  (    e Zd ZdZddZddZddZy)	ZStandardDecoderzHandle 'zstd' RFC 8878 decoding.

    If running on Python 3.14+ or a distro that doesn't have the `compression.zstd` stdlib module, requires either:
    `pip install zstandard` or `pip install httpx2[zstd]`.
    c                T    t         st        d      d t               | _        d| _        y )NzXUsing 'ZStandardDecoder', ...Make sure to install httpx using `pip install httpx[zstd]`.F)r   rM   r   r0   rO   r   s    r   r1   zZStandardDecoder.__init__   s,    #j -.r   c                   |syd| _         t        j                         }	 | j                  j                  r(| j                  j
                  |z   }t               | _        |j                  | j                  j                  |             | j                  j                  r| j                  j
                  r|| j                  j
                  }t               | _        |j                  | j                  j                  |             | j                  j                  r| j                  j
                  r||j                         S # t        $ r}t        t        |            |d }~ww xY wrT   )rO   ioBytesIOr0   eofunused_datar   writer4   r	   r   r7   getvalue)r   r   outputr^   r9   s        r   r   zZStandardDecoder.decode   s   
	3  $$((44t;$4$6!LL**55d;<##''D,=,=,I,I"//;;$4$6!T..99+FG ##''D,=,=,I,I     	3C)s2	3s   DE   	E#	EE#c                `    | j                   sy| j                  j                  st        d      y)Nr   zZstandard data is incomplete)rO   r0   r]   r   r   s    r   r   zZStandardDecoder.flush   s*    ~~  $$ >??r   Nr?   r   r   rA   r#   r   r   rX   rX      s    !$r   rX   c                  8    e Zd ZU dZdZded<   d	dZd
dZddZy)MultiDecoderzE
    Handle the case where multiple encodings have been applied.
       ztyping.ClassVar[int]max_decode_linksc                    |D cg c]  }|t         v s| }}t        |      | j                  kD  rt        d| j                   d      t	        |      D cg c]  }t        |           c}| _        yc c}w c c}w )zk
        'encodings' should be the content codings in the order in which
        each was applied.
        zCannot apply more than z content encodings.N)SUPPORTED_DECODERSlenrf   r   reversedchildren)r   	encodingsencodingcodingscodings        r   r1   zMultiDecoder.__init__   s|    
 -6XEW9W8XXw<$///"9$:O:O9PPc deeZbcjZk.lPV/A&/I/K.l	 Y /ms   A4A4A9c                J    | j                   D ]  }|j                  |      } |S r   )rk   r   r   r   childs      r   r   zMultiDecoder.decode   s(    ]] 	&E<<%D	&r   c                p    d}| j                   D ]$  }|j                  |      |j                         z   }& |S r(   )rk   r   r   rq   s      r   r   zMultiDecoder.flush   s8    ]] 	6E<<%5D	6r   N)rl   ztyping.Sequence[str]r   r@   r   r   )	r    r!   r"   r)   rf   __annotations__r1   r   r   r#   r   r   rd   rd      s%     ./*.	m
r   rd   c                  *    e Zd ZdZdddZddZd	dZy)
ByteChunkerz>
    Handles returning byte content in fixed-size chunks.
    Nc                D    t        j                         | _        || _        y r   )r[   r\   _buffer_chunk_sizer   
chunk_sizes     r   r1   zByteChunker.__init__  s    zz|%r   c                   | j                   |r|gS g S | j                  j                  |       | j                  j                         | j                   k\  r| j                  j	                         }t        dt        |      | j                         D cg c]  }|||| j                   z     }}t        |d         | j                   k(  r7| j                  j                  d       | j                  j                          |S | j                  j                  d       | j                  j                  |d          | j                  j                          |d d S g S c c}w Nr   	ry   rx   r_   tellr`   rangeri   seektruncater   contentvalueichunkss        r   r   zByteChunker.decode  !   # 'G9/R/7#<<$"2"22LL))+E?DQE
TXTdTd?ef!eAD$4$4 45fFf6":$"2"22!!!$%%'!!!$""6":.%%'cr{"I g   Ec                    | j                   j                         }| j                   j                  d       | j                   j                          |r|gS g S Nr   rx   r`   r   r   r   r   s     r   r   zByteChunker.flush  E    %%'!w'R'r   r   r{   z
int | Noner   r@   )r   r   r   list[bytes])r   r   rA   r#   r   r   rv   rv         &((r   rv   c                  *    e Zd ZdZdddZddZd	dZy)
TextChunkerz>
    Handles returning text content in fixed-size chunks.
    Nc                D    t        j                         | _        || _        y r   )r[   StringIOrx   ry   rz   s     r   r1   zTextChunker.__init__+  s    {{}%r   c                   | j                   |r|gS g S | j                  j                  |       | j                  j                         | j                   k\  r| j                  j	                         }t        dt        |      | j                         D cg c]  }|||| j                   z     }}t        |d         | j                   k(  r7| j                  j                  d       | j                  j                          |S | j                  j                  d       | j                  j                  |d          | j                  j                          |d d S g S c c}w r}   r   r   s        r   r   zTextChunker.decode/  r   r   c                    | j                   j                         }| j                   j                  d       | j                   j                          |r|gS g S r   r   r   s     r   r   zTextChunker.flushC  r   r   r   r   )r   r7   r   	list[str]r   r   rA   r#   r   r   r   r   &  r   r   r   c                  *    e Zd ZdZdddZddZd	dZy)
TextDecoderz8
    Handles incrementally decoding bytes into text
    c                F     t        j                  |      d      | _        y )Nreplace)errors)codecsgetincrementaldecoderdecoder)r   rm   s     r   r1   zTextDecoder.__init__O  s    =v33H=YOr   c                8    | j                   j                  |      S r   r   r   r   s     r   r   zTextDecoder.decodeR  s    ||""4((r   c                :    | j                   j                  dd      S rT   r   r   s    r   r   zTextDecoder.flushU  s    ||""3--r   N)zutf-8)rm   r7   r   r@   )r   r   r   r7   )r   r7   rA   r#   r   r   r   r   J  s    P).r   r   c                  (    e Zd ZdZddZddZddZy)	LineDecoderz
    Handles incrementally reading lines from text.

    Has the same behaviour as the stdllib splitlines,
    but handling the input iteratively.
    c                     g | _         d| _        y r3   )buffertrailing_crr   s    r   r1   zLineDecoder.__init__a  s    !#!&r   c                   d}| j                   rd|z   }d| _         |j                  d      rd| _         |d d }|sg S |d   |v }|j                         }t        |      dk(  r"|s | j                  j                  |d          g S | j                  r/dj                  | j                        |d   z   g|dd  z   }g | _        |s|j                         g| _        |S )	Nu   
  FTr~   r   r    )r   endswith
splitlinesri   r   appendjoinpop)r   textNEWLINE_CHARStrailing_newlineliness        r   r   zLineDecoder.decodee  s    B $;D$D==#D9D I8}4!u:?#3KKuQx(I;; WWT[[)E!H45ab	AEDK !99;-DKr   c                    | j                   s| j                  sg S dj                  | j                         g}g | _         d| _        |S )Nr   F)r   r   r   )r   r   s     r   r   zLineDecoder.flush  s?    {{4#3#3I%& r   Nr?   )r   r7   r   r   r   rA   r#   r   r   r   r   Y  s    '$Lr   r   )identitygzipdeflatebrzstdzdict[str, type[ContentDecoder]]rh   r   r   )&r)   
__future__r   r   	functoolsr[   systypingr.   _exceptionsr   rL   rM   
brotlicffiTYPE_CHECKINGversion_infocompression.zstdr   r	   	zstandard_ZstdDecompressorpartialr/   r   rt   r   r%   r+   rC   rJ   rX   rd   rv   r   r   r   rh   r   r#   r   r   <module>r      s   #   	 
   &	 

7"@@N,9,,->-@-N-NO!&$& @#$ $	n 	3^ 3:3. 3.23N 23j(~ (V> <!( !(H!( !(H. .9 9z  
7 3  
>4 6" U   	$ 8  	R0y001B1D1R1RS#'  		sk   D8 
E 8E>EEE
EEEEF+F	F	FFFFF