o
    diY)                     @   s   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 d dlmZmZ zd dlZW n ey9   dZY nw G dd deZG dd deZG d	d
 d
eZdd Zdd Zdd Zdd Zdd ZdS )    N)	_CHECKSUM_CLSAwsChunkedWrapperFlexibleChecksumError_apply_request_header_checksum'_register_checksum_algorithm_feature_idbase64conditionally_calculate_md5determine_content_lengthlogger)resolve_awaitable)HttpxStreamingBodyStreamingBodyc                   @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )AioAwsChunkedWrapperNc                    s   |d ur|dkrd }| j r| jsdS |d u p|t| jk}| j s?|r?|  j|  I d H 7  _|d u p9|t| jk}| j s?|s#|d u rHt| j}| jd | }| j|d  | _|S )Nr       )	_complete
_remaininglen_make_chunk)selfsizewant_more_bytes	to_return r   L/home/ubuntu/.local/lib/python3.10/site-packages/aiobotocore/httpchecksum.pyread   s   


zAioAwsChunkedWrapper.readc                    s   t | j| jI d H }tt|dd  d}| | _| jr'| j	| | jrA| jrA| j
d}| j d}d||f S d||f S )N   asciis   0
%s:%s

s   %s
%s
)r   _rawr   _chunk_sizehexr   encoder   	_checksumupdate_checksum_name	b64digest)r   	raw_chunkhex_lennamechecksumr   r   r   r   2   s   z AioAwsChunkedWrapper._make_chunkc                 C   s   | S Nr   r   r   r   r   	__aiter__E   s   zAioAwsChunkedWrapper.__aiter__c                    s   | j s|  I d H S t r)   )r   r   StopAsyncIterationr*   r   r   r   	__anext__H   s   zAioAwsChunkedWrapper.__anext__r)   )__name__
__module____qualname__r   r   r+   r-   r   r   r   r   r      s
    
r   c                       @   e Zd Z fddZd fdd	ZdefddZd	d
 Z  ZS )StreamingChecksumBodyc                    s   t  || || _|| _d S r)   super__init__r!   	_expectedr   
raw_streamcontent_lengthr(   expected	__class__r   r   r5   Q   s   
zStreamingChecksumBody.__init__Nc                    B   t  j|dI d H }| j| |d u s|s|dkr|   |S N)amtr   r4   r   r!   r"   _validate_checksumr   r?   chunkr;   r   r   r   V      zStreamingChecksumBody.readbc                    |   | j jt|I d H }t|}||d |< |t|kr |}nt|d | }| j| |dkr<t|dkr<|   |S Nr   __wrapped__contentr   r   
memoryviewr!   r"   rA   r   rE   rC   amount_readviewr   r   r   readinto]      zStreamingChecksumBody.readintoc                 C   <   | j  t| jkrd| j d| j   }t|dd S NExpected checksum $ did not match calculated checksum: 	error_msgr!   digestr   	b64decoder6   r$   r   r   rV   r   r   r   rA   l      

z(StreamingChecksumBody._validate_checksumr)   	r.   r/   r0   r5   r   	bytearrayrO   rA   __classcell__r   r   r;   r   r2   P   s
    r2   c                       r1   )HttpxStreamingChecksumBodyc                    s   t  | || _|| _d S r)   r3   r7   r;   r   r   r5   x   s   
z#HttpxStreamingChecksumBody.__init__Nc                    r=   r>   r@   rB   r;   r   r   r      rD   zHttpxStreamingChecksumBody.readrE   c                    rF   rG   rH   rL   r   r   r   rO      rP   z#HttpxStreamingChecksumBody.readintoc                 C   rQ   rR   rW   rZ   r   r   r   rA      r[   z-HttpxStreamingChecksumBody._validate_checksumr)   r\   r   r   r;   r   r_   w   s
    
r_   c           	         s   |d }| di }| d}|sd S |D ]>}d| }||vr"qd|| v r)q|jr5t| |||d< nt| ||I d H |d< |d  di }||d< ||d d<  d S td	| d S )
Nheadersr(   response_algorithmsx-amz-checksum--bodycontextresponse_algorithmz[Skipping checksum validation. Response did not contain one of the following algorithms: %s.)gethas_streaming_output_handle_streaming_response_handle_bytes_responser
   debug)	http_responseresponsere   operation_modelr`   checksum_context
algorithms	algorithmheader_namer   r   r   handle_checksum_body   s6   


rs   c                 C   sX   t |}d| }td urt| jtjrt}nt}|| j|d d| |d | S )Nrb   r`   zcontent-length)r   rg   httpx
isinstancerawResponser_   r2   )rl   rm   rq   checksum_clsrr   streaming_clsr   r   r   ri      s   


ri   c           	         sr   | j I d H }d| }t|}| }|| |d | }| t|kr7d| d|  }t|d|S )Nrb   r`   rS   rT   rU   )	rJ   r   rg   r"   rX   r   rY   r$   r   )	rl   rm   rq   rd   rr   rx   r(   r:   rV   r   r   r   rj      s   



rj   c                 C   s   |  di  di }| d}|sd S |dkrt|  n |d dkr't|  n|d dkr2t|  n
td|d d	d
|v rP|d
 }|d | d |d < d S d S )Nre   r(   request_algorithmzconditional-md5inheadertrailerzUnknown checksum variant: {}rU   request_algorithm_headervaluer`   r'   )rg   r   r   _apply_request_trailer_checksumr   format)requestro   rq   r~   r   r   r   apply_request_checksum   s$   



r   c                 C   s   |  di  di }| d}|d }t |d }| d }| d }||v r(d S d|d	< d
|v r9|d
  d7  < nd|d
< ||d< t| t|}|d ur_t||d< d|v r_|d= td t|tt	frkt
|}t|||d| d< d S )Nre   r(   rz   r'   rq   r`   rd   chunkedzTransfer-EncodingzContent-Encodingz,aws-chunkedzaws-chunkedzX-Amz-TrailerzX-Amz-Decoded-Content-LengthzContent-LengthzVRemoving the Content-Length header since 'chunked' is specified for Transfer-Encoding.)rx   checksum_name)rg   r   r   r	   strr
   rk   ru   bytesr]   ioBytesIOr   )r   ro   rq   location_namerx   r`   rd   r9   r   r   r   r     s:   

r   )r   botocore.httpchecksumr   r   r   r   r   r   r   r	   r
   aiobotocore._helpersr   aiobotocore.responser   r   rt   ImportErrorr   r2   r_   rs   ri   rj   r   r   r   r   r   r   <module>   s"    ,8'*+