o
    i	/                     @  s  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mZ zddl	Z	W n e
y?   zddlZ	W n e
y<   dZ	Y nw Y nw zddlZW n e
yQ   dZY nw 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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edZe	du red edu red dS dS )zq
Handlers for Content-Encoding.

See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
    )annotationsN   )DecodingErrorc                   @  s    e Zd Zd	ddZd
ddZdS )ContentDecoderdatabytesreturnc                 C     t  NNotImplementedErrorselfr    r   I/home/kim/smarthome/.venv/lib/python3.10/site-packages/httpx/_decoders.pydecode%      zContentDecoder.decodec                 C  r	   r
   r   r   r   r   r   flush(   r   zContentDecoder.flushNr   r   r   r   r   r   )__name__
__module____qualname__r   r   r   r   r   r   r   $   s    
r   c                   @  s$   e Zd ZdZd
ddZdddZd	S )IdentityDecoderz 
    Handle unencoded data.
    r   r   r   c                 C  s   |S r
   r   r   r   r   r   r   1      zIdentityDecoder.decodec                 C  s   dS N    r   r   r   r   r   r   4   r   zIdentityDecoder.flushNr   r   )r   r   r   __doc__r   r   r   r   r   r   r   ,   s    
r   c                   @  .   e Zd ZdZdddZddd	Zdd
dZdS )DeflateDecoderzZ
    Handle 'deflate' decoding.

    See: https://stackoverflow.com/questions/1838699
    r   Nonec                 C  s   d| _ t | _d S NT)first_attemptzlibdecompressobjdecompressorr   r   r   r   __init__?   s   zDeflateDecoder.__init__r   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   C   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   N      zDeflateDecoder.flushNr   r!   r   r   r   r   r   r   r'   r   r   r   r   r   r   r    8   s
    

r    c                   @  r   )GZipDecoderzW
    Handle 'gzip' decoding.

    See: https://stackoverflow.com/questions/1838699
    r   r!   c                 C  s   t t jdB | _d S )N   )r$   r%   r+   r&   r   r   r   r   r'   \      zGZipDecoder.__init__r   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   _   s   zGZipDecoder.decodec              
   C  r.   r
   r/   r0   r   r   r   r   e   r1   zGZipDecoder.flushNr2   r   r   r3   r   r   r   r   r4   U   s
    

r4   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   r!   c                 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)   )	brotliImportErrorZDecompressorr&   	seen_datahasattrr)   _decompressprocessr   r   r   r   r'   v   s   
zBrotliDecoder.__init__r   r   c              
   C  sD   |sdS d| _ z| |W S  tjy! } ztt||d }~ww Nr   T)r;   r=   r9   r*   r   r,   r7   r   r   r   r      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)r;   r<   r&   r@   r9   r*   r   r,   r0   r   r   r   r      s   
zBrotliDecoder.flushNr2   r   r   r3   r   r   r   r   r8   l   s
    
	
	r8   c                   @  r   )ZStandardDecoderz
    Handle 'zstd' RFC 8878 decoding.

    Requires `pip install zstandard`.
    Can be installed as a dependency of httpx using `pip install httpx[zstd]`.
    r   r!   c                 C  s*   t d u r	tdd t   | _d| _d S )NzXUsing 'ZStandardDecoder', ...Make sure to install httpx using `pip install httpx[zstd]`.F)	zstandardr:   ZstdDecompressorr%   r&   r;   r   r   r   r   r'      s   
zZStandardDecoder.__init__r   r   c              
   C  s   t d usJ d| _t }zA|| j| | jjr@| jjrJ| jj}t 	 
 | _|| j| | jjrE| jjsW | S W | S W | S W | S  t jyb } ztt||d }~ww r"   )rB   r;   ioBytesIOwriter&   r)   eofunused_datarC   r%   Z	ZstdErrorr   r,   getvalue)r   r   outputrH   r-   r   r   r   r      s*   zZStandardDecoder.decodec                 C  s,   | j sdS | j }| jjstdt|S )Nr   zZstandard data is incomplete)r;   r&   r   rG   r   r   )r   retr   r   r   r      s   
zZStandardDecoder.flushNr2   r   r   r3   r   r   r   r   rA      s
    


rA   c                   @  s.   e Zd ZdZdddZdd
dZdddZdS )MultiDecoderzE
    Handle the case where multiple encodings have been applied.
    childrentyping.Sequence[ContentDecoder]r   r!   c                 C  s   t t|| _dS )zm
        'children' should be a sequence of decoders in the order in which
        each was applied.
        N)listreversedrM   )r   rM   r   r   r   r'      s   zMultiDecoder.__init__r   r   c                 C  s   | j D ]}||}q|S r
   )rM   r   r   r   childr   r   r   r      s   
zMultiDecoder.decodec                 C  s&   d}| j D ]}|||  }q|S r   )rM   r   r   rQ   r   r   r   r      s   
zMultiDecoder.flushN)rM   rN   r   r!   r   r   r3   r   r   r   r   rL      s
    

rL   c                   @  0   e Zd ZdZddddZdddZdddZdS )ByteChunkerz>
    Handles returning byte content in fixed-size chunks.
    N
chunk_size
int | Noner   r!   c                 C     t  | _|| _d S r
   )rD   rE   _buffer_chunk_sizer   rU   r   r   r   r'         

zByteChunker.__init__contentr   list[bytes]c                       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   rY   .0ir   valuer   r   
<listcomp>       z&ByteChunker.decode.<locals>.<listcomp>r   	rY   rX   rF   tellrI   rangelenseektruncater   r\   chunksr   rd   r   r      "   



zByteChunker.decodec                 C  .   | j  }| j d | j   |r|gS g S Nr   rX   rI   rm   rn   rd   r   r   r   r        

zByteChunker.flushr
   rU   rV   r   r!   )r\   r   r   r]   )r   r]   r3   r   r   r   r   rT      
    
rT   c                   @  rS   )TextChunkerz>
    Handles returning text content in fixed-size chunks.
    NrU   rV   r   r!   c                 C  rW   r
   )rD   StringIOrX   rY   rZ   r   r   r   r'     r[   zTextChunker.__init__r\   r,   	list[str]c                   r^   )Nc                   r_   r   r`   ra   rd   r   r   rf     rg   z&TextChunker.decode.<locals>.<listcomp>r   rh   ri   ro   r   rd   r   r     rq   zTextChunker.decodec                 C  rr   rs   rt   rd   r   r   r   r   +  ru   zTextChunker.flushr
   rv   )r\   r,   r   rz   r   rz   r3   r   r   r   r   rx     rw   rx   c                   @  s0   e Zd ZdZddddZdddZdddZdS )TextDecoderz8
    Handles incrementally decoding bytes into text
    utf-8encodingr,   r   r!   c                 C  s   t |dd| _d S )Nreplace)errors)codecsgetincrementaldecoderdecoder)r   r~   r   r   r   r'   7  r6   zTextDecoder.__init__r   r   c                 C  s   | j |S r
   r   r   r   r   r   r   r   :  s   zTextDecoder.decodec                 C  s   | j ddS r?   r   r   r   r   r   r   =  s   zTextDecoder.flushN)r}   )r~   r,   r   r!   )r   r   r   r,   )r   r,   r3   r   r   r   r   r|   2  s
    
r|   c                   @  s.   e Zd ZdZdddZdd	d
Zd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   r!   c                 C  s   g | _ d| _d S r(   )buffertrailing_crr   r   r   r   r'   I  s   
zLineDecoder.__init__textr,   rz   c                 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   
  FTrh   r   r    )r   endswith
splitlinesrl   r   appendjoinpop)r   r   ZNEWLINE_CHARSZtrailing_newlinelinesr   r   r   r   M  s(   
"zLineDecoder.decodec                 C  s.   | j s| jsg S d| j g}g | _ d| _|S )Nr   F)r   r   r   )r   r   r   r   r   r   s  s   zLineDecoder.flushNr2   )r   r,   r   rz   r{   r3   r   r   r   r   r   A  s
    

&r   )identitygzipdeflatebrzstdr   r   )r   
__future__r   r   rD   typingr$   _exceptionsr   r9   r:   Z
brotlicffirB   r   r   r    r4   r8   rA   rL   rT   rx   r|   r   ZSUPPORTED_DECODERSr   r   r   r   r   <module>   sX    
5*''=	
