U
    n/e                     @   st   d dl Z d dlmZmZmZmZmZmZmZm	Z	m
Z
 d dlmZ G dd deZdd Zdd	 Zd
d Zdd ZdS )    N)	_CHECKSUM_CLSAwsChunkedWrapperFlexibleChecksumError_apply_request_header_checksum_handle_streaming_responsebase64conditionally_calculate_md5determine_content_lengthlogger)resolve_awaitablec                   @   s$   e Zd Zdd Zdd Zdd ZdS )AioAwsChunkedWrapperc                    s   t | j| jI d H }tt|dd  d}| | _| jrL| j	| | jr| jr| j
d}| j d}d||f S d||f S )N   asciis   0
%s:%s

s   %s
%s
)r   _rawreadZ_chunk_sizehexlenencode	_completeZ	_checksumupdateZ_checksum_name	b64digest)selfZ	raw_chunkZhex_lennamechecksum r   </tmp/pip-unpacked-wheel-8mnez3y6/aiobotocore/httpchecksum.py_make_chunk   s    z AioAwsChunkedWrapper._make_chunkc                 C   s   | S Nr   r   r   r   r   	__aiter__&   s    zAioAwsChunkedWrapper.__aiter__c                    s   | j s|  I d H S t d S r   )r   r   StopAsyncIterationr   r   r   r   	__anext__)   s    zAioAwsChunkedWrapper.__anext__N)__name__
__module____qualname__r   r   r!   r   r   r   r   r      s   r   c           	         s   |d }| di }| d}|s&d S |D ]z}d| }||kr@q*d|| krNq*|jrft| |||d< nt| ||I d H |d< |d  di }||d< ||d d<  d S td	| d
 d S )Nheadersr   Zresponse_algorithmsx-amz-checksum-%s-bodycontextZresponse_algorithmzXSkipping checksum validation. Response did not contain one of the following algorithms: .)getZhas_streaming_outputr   _handle_bytes_responser
   info)	http_responseresponser)   Zoperation_modelr%   checksum_contextZ
algorithms	algorithmheader_namer   r   r   handle_checksum_body/   s:    
  
  
r3   c           	         sj   | j I d H }d| }t|}| }|| |d | }| t|krfd|| f }t|d|S )Nr&   r%   z:Expected checksum %s did not match calculated checksum: %s	error_msg)	contentr   r+   r   digestr   	b64decoder   r   )	r.   r/   r1   r(   r2   checksum_clsr   expectedr5   r   r   r   r,   Z   s    


r,   c                 C   sz   |  di  di }| d}|s&d S |dkr8t|  n>|d dkrNt|  n(|d dkrdt|  ntd|d  d	d S )
Nr)   r   request_algorithmzconditional-md5inheadertrailerzUnknown checksum variant: %sr4   )r+   r   r   _apply_request_trailer_checksumr   )requestr0   r1   r   r   r   apply_request_checksumm   s    




rA   c                 C   s   |  di  di }| d}|d }t |d }| d }| d }||krPd S d|d	< d
|krr|d
  d7  < nd|d
< ||d< t|}|d k	rt||d< t|ttfrt|}t	|||d| d< d S )Nr)   r   r;   r   r1   r%   r(   chunkedzTransfer-EncodingzContent-Encodingz,aws-chunkedzaws-chunkedzX-Amz-TrailerzX-Amz-Decoded-Content-Length)r9   Zchecksum_name)
r+   r   r	   str
isinstancebytes	bytearrayioBytesIOr   )r@   r0   r1   Zlocation_namer9   r%   r(   content_lengthr   r   r   r?      s.    

r?   )rG   Zbotocore.httpchecksumr   r   r   r   r   r   r   r	   r
   Zaiobotocore._helpersr   r   r3   r,   rA   r?   r   r   r   r   <module>   s   ,+