U
    M8cY                     @   s\  d dl Z d dlZd dlmZ d dlZd dlmZmZ d dl	m
Z
mZ d dlZd dlmZmZmZmZmZmZmZ d dlmZ eeZG dd deZG d	d
 d
ZG dd deeZG dd deeZG dd de
ZG dd deZ d%ddZ!dd Z"dd Z#G dd dZ$G dd dZ%G dd dZ&G dd  d Z'G d!d" d"Z(G d#d$ d$eZ)dS )&    N)Mapping)HTTPConnectionVerifiedHTTPSConnection)HTTPConnectionPoolHTTPSConnectionPool)HTTPHeadersHTTPResponseMutableMapping	urlencodeurlparseurlsplit
urlunsplit)UnseekableStreamErrorc                   @   s   e Zd Zdd Zdd ZdS )AWSHTTPResponsec                 O   s"   | d| _tj| f|| d S )Nstatus_tuple)pop_status_tupler   __init__selfargskwargs r   7/tmp/pip-unpacked-wheel-ozje0y8b/botocore/awsrequest.pyr   (   s    zAWSHTTPResponse.__init__c                 C   s(   | j d k	r| j }d | _ |S t| S d S N)r   r   _read_status)r   r   r   r   r   r   ,   s
    
zAWSHTTPResponse._read_statusN)__name__
__module____qualname__r   r   r   r   r   r   r   %   s   r   c                       sv   e Zd ZdZ fddZ fddZ fddZdd	 ZdddZdd Z	dd Z
dd Z fddZdd Z  ZS )AWSConnectiona  Mixin for HTTPConnection that supports Expect 100-continue.

    This when mixed with a subclass of httplib.HTTPConnection (though
    technically we subclass from urllib3, which subclasses
    httplib.HTTPConnection) and we only override this class to support Expect
    100-continue, which we need for S3.  As far as I can tell, this is
    general purpose enough to not be specific to S3, but I'm being
    tentative and keeping it in botocore because I've only tested
    this against AWS services.

    c                    s&   t  j|| | j| _d| _d| _d S NF)superr   response_class_original_response_cls_response_received_expect_header_setr   	__class__r   r   r   B   s    	zAWSConnection.__init__c                    s"   t    d| _d| _| j| _d S r    )r!   closer$   r%   r#   r"   r   r&   r   r   r(   P   s    
zAWSConnection.closec                    sP   d| _ |dddkrd| _nd| _| j| _t j||||f||}d| _|S )NFZExpect    s   100-continueT)r$   getr%   r#   r"   r!   _send_request)r   methodurlbodyheadersr   r   rvalr&   r   r   r,   W   s"       zAWSConnection._send_requestc                 C   sB   g }|D ]*}t |tr(||d q|| qd|}|S )Nutf-8   
)
isinstancestrappendencodejoin)r   Zmixed_bufferZbytes_bufferchunkmsgr   r   r   _convert_to_bytesd   s    

zAWSConnection._convert_to_bytesNc                 O   s   | j d | | j }| j d d = t|tr:||7 }d }| | | jr|td t	j
| jdrr| | d S td |d k	r| | d S )N)r*   r*   z"Waiting for 100 Continue response.   zCNo response seen from server, continuing to send the response body.)_bufferextendr;   r4   bytessendr%   loggerdebugurllib3utilwait_for_readsock_handle_expect_response)r   message_bodyr   r   r:   r   r   r   _send_outputq   s"    



zAWSConnection._send_outputc                 C   s   d }|dkr|  }qd S )Nr3   )readline)r   fpcurrentr   r   r   _consume_headers   s    zAWSConnection._consume_headersc                 C   s   | j dd}z| }|d d}| |rN| | td | 	| ndt
|dkr|d drtd |d dt|d	 |d df}tjt|d
}|| _d| _W 5 |  X d S )Nrbr      z5100 Continue response seen, now sending request body.      HTTP/zOReceived a non 100 Continue response from the server, NOT sending request body.asciir<   )r   T)rF   makefiler(   rJ   split_is_100_continue_statusrM   rA   rB   _send_message_bodylen
startswithdecodeint	functoolspartialr   r"   r$   )r   rH   rK   maybe_status_linepartsr   r"   r   r   r   rG      s2    


 
z%AWSConnection._handle_expect_responsec                 C   s   |d k	r|  | d S r   )r@   )r   rH   r   r   r   rV      s    z AWSConnection._send_message_bodyc                    s    | j rtd d S t |S )Nz@send() called, but reseponse already received. Not sending data.)r$   rA   rB   r!   r@   )r   r5   r&   r   r   r@      s    zAWSConnection.sendc                 C   s2   | d d}t|dko0|d do0|d dkS )NrO   rP   r   rQ   r<   s   100)rT   rW   rX   )r   r]   r^   r   r   r   rU      s    
z%AWSConnection._is_100_continue_status)N)r   r   r   __doc__r   r(   r,   r;   rI   rM   rG   rV   r@   rU   __classcell__r   r   r&   r   r   5   s   
&,	r   c                   @   s   e Zd ZdZdS )AWSHTTPConnectionz6An HTTPConnection that supports 100 Continue behavior.Nr   r   r   r_   r   r   r   r   ra      s   ra   c                   @   s   e Zd ZdZdS )AWSHTTPSConnectionz7An HTTPSConnection that supports 100 Continue behavior.Nrb   r   r   r   r   rc      s   rc   c                   @   s   e Zd ZeZdS )AWSHTTPConnectionPoolN)r   r   r   ra   ConnectionClsr   r   r   r   rd      s   rd   c                   @   s   e Zd ZeZdS )AWSHTTPSConnectionPoolN)r   r   r   rc   re   r   r   r   r   rf      s   rf   c           
      C   s   | }|dk	r|d }||d< | d}t||d |}|d rttjj}||d }	d|krh|d|	 7 }n|d	|	 7 }||d
< ||d< |dkri |d< dS )a;  
    This method prepares a request dict to be created into an
    AWSRequestObject. This prepares the request dict by adding the
    url and the user agent to the request dict.

    :type request_dict: dict
    :param request_dict:  The request dict (created from the
        ``serialize`` module).

    :type user_agent: string
    :param user_agent: The user agent to use for this request.

    :type endpoint_url: string
    :param endpoint_url: The full endpoint url, which contains at least
        the scheme, the hostname, and optionally any path components.
    Nr0   z
User-Agenthost_prefixurl_pathZquery_string?z?%sz&%sr.   context)r+   _urljoinbotocoreutilspercent_encode_sequence)
request_dictendpoint_urlrj   
user_agentrr0   rg   r.   rn   Zencoded_query_stringr   r   r   prepare_request_dict   s     
rs   c                 C   s:   | }t |d |d |d |d |dd}|d |_|S )aK  
    This method takes a request dict and creates an AWSRequest object
    from it.

    :type request_dict: dict
    :param request_dict:  The request dict (created from the
        ``prepare_request_dict`` method).

    :rtype: ``botocore.awsrequest.AWSRequest``
    :return: An AWSRequest object based on the request_dict.

    r-   r.   r/   r0   	auth_path)r-   r.   datar0   rt   rj   )
AWSRequestr+   rj   )ro   rr   Zrequest_objectr   r   r   create_request_object  s    
rw   c                 C   s   t | }|r|dkr,|d s"d}qf|d }n:|d drZ|drZ|d d d | }n|d | }|d }|d k	r~|| }t|d |||d |d f}|S )N/rO   r<   r   rP      )r   endswithrX   r   )rp   rh   rg   pnew_pathZ
new_netlocZreconstructedr   r   r   rk   8  s    
rk   c                   @   sB   e Zd ZdZdd Zdd ZdddZd	d
 Zdd Zdd Z	dS )AWSRequestPreparera
  
    This class performs preparation on AWSRequest objects similar to that of
    the PreparedRequest class does in the requests library. However, the logic
    has been boiled down to meet the specific use cases in botocore. Of note
    there are the following differences:
        This class does not heavily prepare the URL. Requests performed many
        validations and corrections to ensure the URL is properly formatted.
        Botocore either performs these validations elsewhere or otherwise
        consistently provides well formatted URLs.

        This class does not heavily prepare the body. Body preperation is
        simple and supports only the cases that we document: bytes and
        file-like objects to determine the content-length. This will also
        additionally prepare a body that is a dict to be url encoded params
        string as some signers rely on this. Finally, this class does not
        support multipart file uploads.

        This class does not prepare the method, auth or cookies.
    c                 C   s<   |j }| |}| |}| ||}|j}t|||||S r   )r-   _prepare_url_prepare_body_prepare_headersstream_outputAWSPreparedRequest)r   originalr-   r.   r/   r0   r   r   r   r   preparei  s    

zAWSRequestPreparer.preparec                 C   sb   |j }|jr^t|}|jrdnd}t|jtr>t|j }n|j}t|dd}|	||f}|S )N&ri   Tdoseq)
r.   paramsr   queryr4   r   listitemsr
   r8   )r   r   r.   Z	url_partsdelimZparams_to_encoder   r   r   r   r   r  s    zAWSRequestPreparer._prepare_urlNc                 C   sl   t |j }d|ksd|kr"|S |jdkrh| |}|d k	rLt||d< nt|}td| d|d< |S )NzTransfer-EncodingzContent-Length)GETHEADOPTIONSz Failed to determine length of %schunked)	HeadersDictr0   r   r-   _determine_content_lengthr5   typerA   rB   )r   r   Zprepared_bodyr0   lengthZ	body_typer   r   r   r     s    

z#AWSRequestPreparer._prepare_headersc                 C   s8   |\}}t |tr|d}t |tr0|d}||fS Nr2   )r4   r5   r7   )r   itemkeyvaluer   r   r   _to_utf8  s    



zAWSRequestPreparer._to_utf8c                    sB   |j }|dkrd}t|tr> fdd| D }t|dd}|S )z"Prepares the given HTTP body data.r*   Nc                    s   g | ]}  |qS r   )r   ).0r   r)   r   r   
<listcomp>  s     z4AWSRequestPreparer._prepare_body.<locals>.<listcomp>Tr   )ru   r4   dictr   r
   )r   r   r/   r   r   r)   r   r     s    
z AWSRequestPreparer._prepare_bodyc                 C   s   t j|S r   )rl   rm   Zdetermine_content_lengthr   r/   r   r   r   r     s    z,AWSRequestPreparer._determine_content_length)N)
r   r   r   r_   r   r   r   r   r   r   r   r   r   r   r~   T  s   	
r~   c                   @   s2   e Zd ZdZeZd
ddZdd Zedd	 Z	dS )rv   a  Represents the elements of an HTTP request.

    This class was originally inspired by requests.models.Request, but has been
    boiled down to meet the specific use cases in botocore. That being said this
    class (even in requests) is effectively a named-tuple.
    NFc           
      C   st   |   | _|d kri n|}|| _|| _t | _|| _|| _|| _|| _	|d k	rj|
 D ]\}}	|	| j|< qVi | _d S r   )_REQUEST_PREPARER_CLS_request_preparerr-   r.   r   r0   ru   r   rt   r   r   rj   )
r   r-   r.   r0   ru   r   rt   r   r   r   r   r   r   r     s    
	zAWSRequest.__init__c                 C   s   | j | S )z>Constructs a :class:`AWSPreparedRequest <AWSPreparedRequest>`.)r   r   r)   r   r   r   r     s    zAWSRequest.preparec                 C   s"   |   j}t|tr|d}|S r   )r   r/   r4   r5   r7   r   r   r   r   r/     s    


zAWSRequest.body)NNNNNNF)
r   r   r   r_   r~   r   r   r   propertyr/   r   r   r   r   rv     s          
%rv   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   a  A data class representing a finalized request to be sent over the wire.

    Requests at this stage should be treated as final, and the properties of
    the request should not be modified.

    :ivar method: The HTTP Method
    :ivar url: The full url
    :ivar headers: The HTTP headers to send.
    :ivar body: The HTTP body.
    :ivar stream_output: If the response for this request should be streamed.
    c                 C   s"   || _ || _|| _|| _|| _d S r   )r-   r.   r0   r/   r   )r   r-   r.   r0   r/   r   r   r   r   r     s
    zAWSPreparedRequest.__init__c                 C   s   d}|| j | j| j| jf S )NzD<AWSPreparedRequest stream_output=%s, method=%s, url=%s, headers=%s>)r   r-   r.   r0   )r   fmtr   r   r   __repr__  s    zAWSPreparedRequest.__repr__c              
   C   s   t ttf}| jdks t| j|r$dS ztd| j | jd W n: tk
r| } ztd| t	| jdW 5 d}~X Y nX dS )a+  Resets the streaming body to it's initial position.

        If the request contains a streaming body (a streamable file-like object)
        seek to the object's initial position to ensure the entire contents of
        the object is sent. This is a no-op for static bytes-like body types.
        NzRewinding stream: %sr   zUnable to rewind stream: %s)Zstream_object)
r?   r5   	bytearrayr/   r4   rA   rB   seek	Exceptionr   )r   Znon_seekable_typeser   r   r   reset_stream  s    
zAWSPreparedRequest.reset_streamN)r   r   r   r_   r   r   r   r   r   r   r   r     s   r   c                   @   s0   e Zd ZdZdd Zedd Zedd ZdS )	AWSResponsea  A data class representing an HTTP response.

    This class was originally inspired by requests.models.Response, but has
    been boiled down to meet the specific use cases in botocore. This has
    effectively been reduced to a named tuple.

    :ivar url: The full url.
    :ivar status_code: The status code of the HTTP response.
    :ivar headers: The HTTP headers received.
    :ivar body: The HTTP response body.
    c                 C   s&   || _ || _t|| _|| _d | _d S r   )r.   status_coder   r0   raw_content)r   r.   r   r0   r   r   r   r   r   '  s
    
zAWSResponse.__init__c                 C   s*   | j dkr$t | j p t | _ | j S )z!Content of the response as bytes.N)r   r?   r8   r   streamr)   r   r   r   content/  s    
zAWSResponse.contentc                 C   s.   t j| j}|r| j|S | jdS dS )a
  Content of the response as a proper text type.

        Uses the encoding type provided in the reponse headers to decode the
        response content into a proper text type. If the encoding is not
        present in the headers, UTF-8 is used as a default.
        r2   N)rl   rm   get_encoding_from_headersr0   r   rY   )r   encodingr   r   r   text<  s    zAWSResponse.textN)r   r   r   r_   r   r   r   r   r   r   r   r   r     s   
r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )
_HeaderKeyc                 C   s   || _ | | _d S r   )_keylower_lowerr   r   r   r   r   r   L  s    z_HeaderKey.__init__c                 C   s
   t | jS r   )hashr   r)   r   r   r   __hash__P  s    z_HeaderKey.__hash__c                 C   s   t |to| j|jkS r   )r4   r   r   )r   otherr   r   r   __eq__S  s    z_HeaderKey.__eq__c                 C   s   | j S r   )r   r)   r   r   r   __str__V  s    z_HeaderKey.__str__c                 C   s
   t | jS r   )reprr   r)   r   r   r   r   Y  s    z_HeaderKey.__repr__N)r   r   r   r   r   r   r   r   r   r   r   r   r   K  s
   r   c                   @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )r   z9A case-insenseitive dictionary to represent HTTP headers.c                 O   s   i | _ | j|| d S r   )_dictupdater   r   r   r   r   `  s    zHeadersDict.__init__c                 C   s   || j t|< d S r   r   r   )r   r   r   r   r   r   __setitem__d  s    zHeadersDict.__setitem__c                 C   s   | j t| S r   r   r   r   r   r   __getitem__g  s    zHeadersDict.__getitem__c                 C   s   | j t|= d S r   r   r   r   r   r   __delitem__j  s    zHeadersDict.__delitem__c                 C   s   dd | j D S )Nc                 s   s   | ]}t |V  qd S r   )r5   )r   r   r   r   r   	<genexpr>n  s     z'HeadersDict.__iter__.<locals>.<genexpr>)r   r)   r   r   r   __iter__m  s    zHeadersDict.__iter__c                 C   s
   t | jS r   )rW   r   r)   r   r   r   __len__p  s    zHeadersDict.__len__c                 C   s
   t | jS r   )r   r   r)   r   r   r   r   s  s    zHeadersDict.__repr__c                 C   s   t |  S r   )r   r   r)   r   r   r   copyv  s    zHeadersDict.copyN)r   r   r   r_   r   r   r   r   r   r   r   r   r   r   r   r   r   ]  s   r   )NN)*r[   loggingcollections.abcr   Zurllib3.utilrC   Zurllib3.connectionr   r   Zurllib3.connectionpoolr   r   Zbotocore.utilsrl   Zbotocore.compatr   r   r	   r
   r   r   r   Zbotocore.exceptionsr   	getLoggerr   rA   r   r   ra   rc   rd   rf   rs   rw   rk   r~   rv   r   r   r   r   r   r   r   r   <module>   s6   $	
 2   
)X;31