o
    e&                     @   s   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 G dd dZ	G dd	 d	e	Z
G d
d de	ZG dd de	ZG dd de	ZG dd de	ZG dd dZG dd dZG dd dZG dd dZe
eeedZedu r{ed dS dS )zq
Handlers for Content-Encoding.

See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
    N   )brotli)DecodingErrorc                   @   s,   e Zd ZdedefddZdefddZdS )ContentDecoderdatareturnc                 C      t  NNotImplementedErrorselfr    r   ?D:\Projects\ConvertPro\env\Lib\site-packages\httpx/_decoders.pydecode      zContentDecoder.decodec                 C   r   r	   r
   r   r   r   r   flush   r   zContentDecoder.flushN)__name__
__module____qualname__bytesr   r   r   r   r   r   r      s    r   c                   @   s0   e Zd ZdZdedefddZdefddZdS )	IdentityDecoderz 
    Handle unencoded data.
    r   r   c                 C   s   |S r	   r   r   r   r   r   r         zIdentityDecoder.decodec                 C   s   dS N    r   r   r   r   r   r      r   zIdentityDecoder.flushN)r   r   r   __doc__r   r   r   r   r   r   r   r      s    r   c                   @   :   e Zd ZdZdddZdedefddZdefd	d
ZdS )DeflateDecoderzZ
    Handle 'deflate' decoding.

    See: https://stackoverflow.com/questions/1838699
    r   Nc                 C   s   d| _ t | _d S )NT)first_attemptzlibdecompressobjdecompressorr   r   r   r   __init__*   s   zDeflateDecoder.__init__r   c              
   C   sn   | j }d| _ z| j|W S  tjy6 } z|r+ttj | _| |W  Y d }~S tt	||d }~ww NF)
r   r"   
decompressr    errorr!   	MAX_WBITSr   r   str)r   r   Zwas_first_attemptexcr   r   r   r   .   s   zDeflateDecoder.decodec              
   C   6   z| j  W S  tjy } ztt||d }~ww r	   r"   r   r    r&   r   r(   r   r)   r   r   r   r   9      zDeflateDecoder.flushr   Nr   r   r   r   r#   r   r   r   r   r   r   r   r   #   s
    
r   c                   @   r   )GZipDecoderzW
    Handle 'gzip' decoding.

    See: https://stackoverflow.com/questions/1838699
    r   Nc                 C   s   t t jdB | _d S )N   )r    r!   r'   r"   r   r   r   r   r#   G      zGZipDecoder.__init__r   c              
   C   s8   z| j |W S  tjy } ztt||d }~ww r	   )r"   r%   r    r&   r   r(   r   r   r)   r   r   r   r   J   s   zGZipDecoder.decodec              
   C   r*   r	   r+   r,   r   r   r   r   P   r-   zGZipDecoder.flushr.   r/   r   r   r   r   r0   @   s
    
r0   c                   @   r   )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'
    r   Nc                 C   sN   t d u r	tdd t  | _d| _|  t| jdr | jj| _d S | jj| _d S )NzUsing 'BrotliDecoder', but neither of the 'brotlicffi' or 'brotli' packages have been installed. Make sure to install httpx using `pip install httpx[brotli]`.Fr%   )	r   ImportErrorZDecompressorr"   	seen_datahasattrr%   _decompressprocessr   r   r   r   r#   a   s   
zBrotliDecoder.__init__r   c              
   C   sD   |sdS d| _ z| |W S  tjy! } ztt||d }~ww Nr   T)r6   r8   r   r&   r   r(   r3   r   r   r   r   s   s   zBrotliDecoder.decodec              
   C   sP   | j sdS zt| jdr| j  W dS  tjy' } ztt||d }~ww )Nr   finish)r6   r7   r"   r;   r   r&   r   r(   r,   r   r   r   r   |   s   
zBrotliDecoder.flushr.   r/   r   r   r   r   r4   W   s
    
		r4   c                   @   sH   e Zd ZdZdeje ddfddZdedefdd	Z	defd
dZ
dS )MultiDecoderzE
    Handle the case where multiple encodings have been applied.
    childrenr   Nc                 C   s   t t|| _dS )zm
        'children' should be a sequence of decoders in the order in which
        each was applied.
        N)listreversedr=   )r   r=   r   r   r   r#      s   zMultiDecoder.__init__r   c                 C   s   | j D ]}||}q|S r	   )r=   r   r   r   childr   r   r   r      s   
zMultiDecoder.decodec                 C   s&   d}| j D ]}|||  }q|S r   )r=   r   r   r@   r   r   r   r      s   
zMultiDecoder.flush)r   r   r   r   typingSequencer   r#   r   r   r   r   r   r   r   r<      s
    r<   c                   @   V   e Zd ZdZddeje ddfddZdedej	e fdd	Z
dej	e fd
dZdS )ByteChunkerz>
    Handles returning byte content in fixed-size chunks.
    N
chunk_sizer   c                 C      t  | _|| _d S r	   )ioBytesIO_buffer_chunk_sizer   rF   r   r   r   r#         

zByteChunker.__init__contentc                    s    j d u r|r
|gS g S  j|  j  j kr_ j  fddtdt j D }t|d  j krF jd  j  |S  jd  j|d   j  |d d S g S )Nc                       g | ]}|| j   qS r   rK   .0ir   valuer   r   
<listcomp>       z&ByteChunker.decode.<locals>.<listcomp>r   	rK   rJ   writetellgetvaluerangelenseektruncater   rN   chunksr   rT   r   r      s"   



zByteChunker.decodec                 C   .   | j  }| j d | j   |r|gS g S Nr   rJ   r\   r_   r`   rT   r   r   r   r         

zByteChunker.flushr	   )r   r   r   r   rB   Optionalintr#   r   Listr   r   r   r   r   r   rE      
    rE   c                   @   rD   )TextChunkerz>
    Handles returning text content in fixed-size chunks.
    NrF   r   c                 C   rG   r	   )rH   StringIOrJ   rK   rL   r   r   r   r#      rM   zTextChunker.__init__rN   c                    s    j d u r|gS  j|  j  j kr[ j  fddtdt j D }t|d  j krB jd  j  |S  jd  j|d   j  |d d S g S )Nc                    rO   r   rP   rQ   rT   r   r   rV      rW   z&TextChunker.decode.<locals>.<listcomp>r   rX   rY   ra   r   rT   r   r      s"   



zTextChunker.decodec                 C   rc   rd   re   rT   r   r   r   r      rf   zTextChunker.flushr	   )r   r   r   r   rB   rg   rh   r#   r(   ri   r   r   r   r   r   r   rk      rj   rk   c                   @   s@   e Zd ZdZddefddZdedefdd	Zdefd
dZdS )TextDecoderz8
    Handles incrementally decoding bytes into text
    utf-8encodingc                 C   s   t |dd| _d S )Nreplace)errors)codecsgetincrementaldecoderdecoder)r   ro   r   r   r   r#      r2   zTextDecoder.__init__r   r   c                 C   s   | j |S r	   rt   r   r   r   r   r   r      s   zTextDecoder.decodec                 C   s   | j ddS r:   ru   r   r   r   r   r      s   zTextDecoder.flushN)rn   )	r   r   r   r   r(   r#   r   r   r   r   r   r   r   rm      s
    rm   c                   @   sF   e Zd ZdZdddZdedeje fddZdeje fd	d
Z	dS )LineDecoderz
    Handles incrementally reading lines from text.

    Has the same behaviour as the stdllib splitlines, but handling the input iteratively.
    r   Nc                 C   s   g | _ d| _d S r$   )buffertrailing_crr   r   r   r   r#   	  s   
zLineDecoder.__init__textc                 C   s   d}| j rd| }d| _ |drd| _ |d d }|sg S |d |v }| }t|dkr:|s:| j|d  g S | jrQd| j|d  g|dd   }g | _|sY| g| _|S )	Nu   
  FTrX   r   r    )rx   endswith
splitlinesr^   rw   appendjoinpop)r   ry   ZNEWLINE_CHARSZtrailing_newlinelinesr   r   r   r     s(   
"zLineDecoder.decodec                 C   s.   | j s| jsg S d| j g}g | _ d| _|S )Nr{   F)rw   rx   r   )r   r   r   r   r   r   1  s   zLineDecoder.flushr.   )
r   r   r   r   r#   r(   rB   ri   r   r   r   r   r   r   rv     s
    
$rv   )identitygzipdeflatebrr   )r   rr   rH   rB   r    Z_compatr   _exceptionsr   r   r   r   r0   r4   r<   rE   rk   rm   rv   ZSUPPORTED_DECODERSr   r   r   r   r   <module>   s2    5'':