U
    d$                     @   s   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZmZ d	Zeed
Zedddd ZG dd deZejG dd dZG dd dZeefddZG dd dZdS )z&Base async HTTP client implementation.    N)	responses)Thenablemaybe_promisepromise)	HttpError)coro)bytes_to_str)
maybe_listmemoize)HeadersResponseRequestpypy_version_infoi  )maxsizec                 C   s   d dd | dD S )N-c                 s   s   | ]}|  V  qd S N)
capitalize).0p r   @/tmp/pip-unpacked-wheel-hqfrjlvz/kombu/asynchronous/http/base.py	<genexpr>   s     z#normalize_header.<locals>.<genexpr>)joinsplit)keyr   r   r   normalize_header   s    r   c                   @   s   e Zd ZdZdZdZdS )r   z%Represents a mapping of HTTP headers.FN)__name__
__module____qualname____doc__complete	_prev_keyr   r   r   r   r      s   r   c                   @   s   e Zd ZdZd Z Z Z Z Z Z	 Z
 Z Z Z Z ZZdZdZdZdZdZdZes`dZddd	Zdd
dZdd ZdS )r   a  A HTTP Request.

    Arguments:
        url (str): The URL to request.
        method (str): The HTTP method to use (defaults to ``GET``).

    Keyword Arguments:
        headers (Dict, ~kombu.asynchronous.http.Headers): Optional headers for
            this request
        body (str): Optional body for this request.
        connect_timeout (float): Connection timeout in float seconds
            Default is 30.0.
        timeout (float): Time in float seconds before the request times out
            Default is 30.0.
        follow_redirects (bool): Specify if the client should follow redirects
            Enabled by default.
        max_redirects (int): Maximum number of redirects (default 6).
        use_gzip (bool): Allow the server to use gzip compression.
            Enabled by default.
        validate_cert (bool): Set to true if the server certificate should be
            verified when performing ``https://`` requests.
            Enabled by default.
        auth_username (str): Username for HTTP authentication.
        auth_password (str): Password for HTTP authentication.
        auth_mode (str): Type of HTTP authentication (``basic`` or ``digest``).
        user_agent (str): Custom user agent for this request.
        network_interface (str): Network interface to use for this request.
        on_ready (Callable): Callback to be called when the response has been
            received. Must accept single ``response`` argument.
        on_stream (Callable): Optional callback to be called every time body
            content has been read from the socket.  If specified then the
            response body and buffer attributes will not be available.
        on_timeout (callable): Optional callback to be called if the request
            times out.
        on_header (Callable): Optional callback to be called for every header
            line received from the server.  The signature
            is ``(headers, line)`` and note that if you want
            ``response.headers`` to be populated then your callback needs to
            also call ``client.on_header(headers, line)``.
        on_prepare (Callable): Optional callback that is implementation
            specific (e.g. curl client will pass the ``curl`` instance to
            this callback).
        proxy_host (str): Optional proxy host.  Note that a ``proxy_port`` must
            also be provided or a :exc:`ValueError` will be raised.
        proxy_username (str): Optional username to use when logging in
            to the proxy.
        proxy_password (str): Optional password to use when authenticating
            with the proxy server.
        ca_certs (str): Custom CA certificates file to use.
        client_key (str): Optional filename for client SSL key.
        client_cert (str): Optional filename for client SSL certificate.
    Ng      >@T   )
urlmethodon_ready
on_timeout	on_stream
on_prepare	on_headerheaders__weakref____dict__GETc	                 K   s   || _ |p| j| _t|pt | _t|| _t|| _t|| _t|| _|	rl|		 D ]\}
}t
| |
| qVt|tst|p~i }|| _d S r   )r#   r$   r   r   r%   r&   r'   r(   r)   itemssetattr
isinstancer   r*   )selfr#   r$   r%   r&   r'   r(   r)   r*   kwargskvr   r   r   __init__l   s    




zRequest.__init__c                 C   s   | j || d S r   )r%   then)r1   callbackZerrbackr   r   r   r6   }   s    zRequest.thenc                 C   s
   d | S )Nz&<Request: {0.method} {0.url} {0.body}>)formatr1   r   r   r   __repr__   s    zRequest.__repr__)r-   NNNNNN)N)r   r   r   r   body
user_agentZnetwork_interfaceZauth_usernameZauth_passwordZ	auth_modeZ
proxy_hostZ
proxy_portZproxy_usernameZproxy_passwordca_certsZ
client_keyclient_certconnect_timeoutrequest_timeoutZfollow_redirectsmax_redirectsZuse_gzipZvalidate_certPYPY	__slots__r5   r6   r:   r   r   r   r   r   $   sJ   8        

r   c                   @   sN   e Zd ZdZesdZdddZdd Zedd	 Z	ed
d Z
edd ZdS )r   a  HTTP Response.

    Arguments:
        request (~kombu.asynchronous.http.Request): See :attr:`request`.
        code (int): See :attr:`code`.
        headers (~kombu.asynchronous.http.Headers): See :attr:`headers`.
        buffer (bytes): See :attr:`buffer`
        effective_url (str): See :attr:`effective_url`.
        status (str): See :attr:`status`.

    Attributes:
        request (~kombu.asynchronous.http.Request): object used to
            get this response.
        code (int): HTTP response code (e.g. 200, 404, or 500).
        headers (~kombu.asynchronous.http.Headers): HTTP headers
            for this response.
        buffer (bytes): Socket read buffer.
        effective_url (str): The destination url for this request after
            following redirects.
        error (Exception): Error instance if the request resulted in
            a HTTP error code.
        status (str): Human equivalent of :attr:`code`,
            e.g. ``OK``, `Not found`, or 'Internal Server Error'.
    )	requestcoder*   buffereffective_urlerrorstatus_bodyr+   Nc                 C   s   || _ || _|d k	r|nt | _|| _|p.|j| _d | _|pHt	| jd| _
|| _| jd kr| jdk sp| jdkrt| j| j
| | _d S )NUnknown   i+  )rD   rE   r   r*   rF   r#   rG   rJ   r   getrI   rH   r   )r1   rD   rE   r*   rF   rG   rH   rI   r   r   r   r5      s    zResponse.__init__c                 C   s   | j r| j dS )zRaise if the request resulted in an HTTP error code.

        Raises:
            :class:`~kombu.exceptions.HttpError`
        N)rH   r9   r   r   r   raise_for_error   s    zResponse.raise_for_errorc                 C   s&   | j dkr | jdk	r | j | _ | j S )zThe full contents of the response body.

        Note:
            Accessing this property will evaluate the buffer
            and subsequent accesses will be cached.
        N)rJ   rF   getvaluer9   r   r   r   r;      s    

zResponse.bodyc                 C   s   | j S r   )rE   r9   r   r   r   status_code   s    zResponse.status_codec                 C   s   | j S r   )r;   r9   r   r   r   content   s    zResponse.content)NNNNN)r   r   r   r   rB   rC   r5   rN   propertyr;   rP   rQ   r   r   r   r   r      s         
	

r   c                 c   s   d V \}}| drq q |s&d|_q q |d  rZ|j}d||pHd| g||< q |dd\}}| | }|_| ||< q d S )NzHTTP/Tr     :   )	
startswithr    isspacer!   r   rM   lstripr   strip)Zkeytliner*   Zpkeyr   valuer   r   r   header_parser   s    

"r]   c                   @   sP   e Zd ZeZeZeZdd Zdd Zdd Zdd Z	d	d
 Z
dd Zdd ZdS )
BaseClientc                 K   s   || _ t | _d S r   )hubr]   _header_parser)r1   r_   r2   r   r   r   r5      s    zBaseClient.__init__c                 K   s:   t |p
g D ](}t|| js*| j|f|}| | qd S r   )r	   r0   r   add_request)r1   rD   r2   reqr   r   r   perform   s    zBaseClient.performc                 C   s   t dd S )Nzmust implement add_request)NotImplementedError)r1   rD   r   r   r   ra      s    zBaseClient.add_requestc                 C   s   d S r   r   r9   r   r   r   close   s    zBaseClient.closec                 C   s:   z| j t||f W n tk
r4   t | _ Y nX d S r   )r`   sendr   StopIterationr]   )r1   r*   r[   r   r   r   r)      s    zBaseClient.on_headerc                 C   s   | S r   r   r9   r   r   r   	__enter__   s    zBaseClient.__enter__c                 G   s   |    d S r   )re   )r1   exc_infor   r   r   __exit__   s    zBaseClient.__exit__N)r   r   r   r   r   r   r5   rc   ra   re   r)   rh   rj   r   r   r   r   r^      s   r^   )r   syshttp.clientr   Zviner   r   r   Zkombu.exceptionsr   Zkombu.utils.compatr   Zkombu.utils.encodingr   Zkombu.utils.functionalr	   r
   __all__hasattrrB   r   dictr   registerr   r   r]   r^   r   r   r   r   <module>   s$   

_L