U
    dd&                     @   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kred 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eedddZedddZdS )ContentDecoderdatareturnc                 C   s
   t  d S NNotImplementedErrorselfr    r   3/tmp/pip-unpacked-wheel-fkuvgui5/httpx/_decoders.pydecode   s    zContentDecoder.decoder   c                 C   s
   t  d S r	   r
   r   r   r   r   flush   s    zContentDecoder.flushN)__name__
__module____qualname__bytesr   r   r   r   r   r   r      s   r   c                   @   s.   e Zd ZdZeedddZedddZdS )	IdentityDecoderz 
    Handle unencoded data.
    r   c                 C   s   |S r	   r   r   r   r   r   r      s    zIdentityDecoder.decoder   c                 C   s   dS N    r   r   r   r   r   r      s    zIdentityDecoder.flushN)r   r   r   __doc__r   r   r   r   r   r   r   r      s   r   c                   @   s<   e Zd ZdZddddZeedddZedd	d
ZdS )DeflateDecoderzZ
    Handle 'deflate' decoding.

    See: https://stackoverflow.com/questions/1838699
    Nr   c                 C   s   d| _ t | _d S )NT)first_attemptzlibdecompressobjdecompressorr   r   r   r   __init__*   s    zDeflateDecoder.__init__r   c              
   C   sx   | j }d| _ z| j|W S  tjk
rr } z8|rTttj | _| | W Y S tt	||W 5 d }~X Y nX d S NF)
r   r    
decompressr   errorr   	MAX_WBITSr   r   str)r   r   Zwas_first_attemptexcr   r   r   r   .   s    zDeflateDecoder.decodec              
   C   sD   z| j  W S  tjk
r> } ztt||W 5 d }~X Y nX d S r	   r    r   r   r$   r   r&   r   r'   r   r   r   r   9   s    zDeflateDecoder.flushr   r   r   r   r!   r   r   r   r   r   r   r   r   #   s   r   c                   @   s<   e Zd ZdZddddZeedddZedd	d
ZdS )GZipDecoderzW
    Handle 'gzip' decoding.

    See: https://stackoverflow.com/questions/1838699
    Nr   c                 C   s   t t jdB | _d S )N   )r   r   r%   r    r   r   r   r   r!   G   s    zGZipDecoder.__init__r   c              
   C   sF   z| j |W S  tjk
r@ } ztt||W 5 d }~X Y nX d S r	   )r    r#   r   r$   r   r&   r   r   r'   r   r   r   r   J   s    zGZipDecoder.decodec              
   C   sD   z| j  W S  tjk
r> } ztt||W 5 d }~X Y nX d S r	   r(   r)   r   r   r   r   P   s    zGZipDecoder.flushr*   r   r   r   r   r+   @   s   r+   c                   @   s<   e Zd ZdZddddZeedddZedd	d
ZdS )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'
    Nr   c                 C   sL   t d krtdd t  | _d| _|  t| jdr>| jj| _n
| 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   sR   |sdS d| _ z| |W S  tjk
rL } ztt||W 5 d }~X Y nX d S Nr   T)r0   r2   r   r$   r   r&   r-   r   r   r   r   s   s    zBrotliDecoder.decodec              
   C   s^   | j s
dS zt| jdr"| j  W dS  tjk
rX } ztt||W 5 d }~X Y nX d S )Nr   finish)r0   r1   r    r5   r   r$   r   r&   r)   r   r   r   r   |   s    
zBrotliDecoder.flushr*   r   r   r   r   r.   W   s   		r.   c                   @   sD   e Zd ZdZeje ddddZeedddZ	ed	d
dZ
dS )MultiDecoderzE
    Handle the case where multiple encodings have been applied.
    N)childrenr   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reversedr7   )r   r7   r   r   r   r!      s    zMultiDecoder.__init__r   c                 C   s   | j D ]}||}q|S r	   )r7   r   r   r   childr   r   r   r      s    
zMultiDecoder.decoder   c                 C   s&   d}| j D ]}|||  }q
|S r   )r7   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   r6      s   r6   c                   @   sR   e Zd ZdZdeje ddddZeej	e dddZ
ej	e d	d
dZdS )ByteChunkerz>
    Handles returning byte content in fixed-size chunks.
    N
chunk_sizer   c                 C   s   t  | _|| _d S r	   )ioBytesIO_buffer_chunk_sizer   r@   r   r   r   r!      s    
zByteChunker.__init__contentr   c                    s    j d kr|r|gS g S  j|  j  j kr j  fddtdt j D }t|d  j kr jd  j  |S  jd  j|d   j  |d d S ng S d S )Nc                    s   g | ]}|| j   qS r   rD   .0ir   valuer   r   
<listcomp>   s   z&ByteChunker.decode.<locals>.<listcomp>r   	rD   rC   writetellgetvaluerangelenseektruncater   rG   chunksr   rL   r   r      s"    



zByteChunker.decoder   c                 C   s.   | j  }| j d | j   |r*|gS g S Nr   rC   rS   rV   rW   rL   r   r   r   r      s    

zByteChunker.flush)N)r   r   r   r   r<   Optionalintr!   r   Listr   r   r   r   r   r   r>      s   r>   c                   @   sR   e Zd ZdZdeje ddddZeej	e dddZ
ej	e d	d
dZdS )TextChunkerz>
    Handles returning text content in fixed-size chunks.
    Nr?   c                 C   s   t  | _|| _d S r	   )rA   StringIOrC   rD   rE   r   r   r   r!      s    
zTextChunker.__init__rF   c                    s    j d kr|gS  j|  j  j kr j  fddtdt j D }t|d  j kr jd  j  |S  jd  j|d   j  |d d S ng S d S )Nc                    s   g | ]}|| j   qS r   rH   rI   rL   r   r   rN      s   z&TextChunker.decode.<locals>.<listcomp>r   rO   rP   rX   r   rL   r   r      s"    



zTextChunker.decoder   c                 C   s.   | j  }| j d | j   |r*|gS g S rZ   r[   rL   r   r   r   r      s    

zTextChunker.flush)N)r   r   r   r   r<   r\   r]   r!   r&   r^   r   r   r   r   r   r   r_      s   r_   c                   @   s>   e Zd ZdZdedddZeedddZed	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   rc   r   r   r   r!      s    zTextDecoder.__init__r   c                 C   s   | j |S r	   rh   r   r   r   r   r   r      s    zTextDecoder.decoder   c                 C   s   | j ddS r4   ri   r   r   r   r   r      s    zTextDecoder.flushN)rb   )	r   r   r   r   r&   r!   r   r   r   r   r   r   r   ra      s   ra   c                   @   sH   e Zd ZdZddddZeeje dddZeje 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.
    Nr   c                 C   s   g | _ d| _d S r"   )buffertrailing_crr   r   r   r   r!   	  s    zLineDecoder.__init__)textr   c                 C   s   d}| j rd| }d| _ |dr4d| _ |d d }|s<g S |d |k}| }t|dkrt|st| j|d  g S | jrd| j|d  g|dd   }g | _|s| g| _|S )	Nu   
  FTrO   r   r    )rl   endswith
splitlinesrU   rk   appendjoinpop)r   rm   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 )Nro   F)rk   rl   rs   )r   ru   r   r   r   r   1  s    zLineDecoder.flush)
r   r   r   r   r!   r&   r<   r^   r   r   r   r   r   r   rj     s   $rj   )identitygzipdeflatebrry   )r   rf   rA   r<   r   _compatr   _exceptionsr   r   r   r   r+   r.   r6   r>   r_   ra   rj   ZSUPPORTED_DECODERSrt   r   r   r   r   <module>   s.   5'':