
) X$3                 @   s  d  d l  m Z d  d l Z d  d l Z d  d l Z y d  d l m Z Wn" e k
 rl d  d l m Z Yn Xd d l	 m
 Z
 d d l m Z m 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 d g Z e j e  Z d' Z e j d d(  Z e j d e j  d)  Z! e j d e! j  e  Z" d  d!   Z# i e j$ e# e!  d" 6e j$ e# e"  d# 6Z% i e d" 6e d# 6Z& Gd$ d   d e  Z' Gd% d   d e'  Z( d& d   Z) d S)*    )absolute_importN)urljoin   )RecentlyUsedContainer)HTTPConnectionPoolHTTPSConnectionPool)port_by_scheme)LocationValueErrorMaxRetryErrorProxySchemeUnknown)RequestMethods)	parse_url)RetryPoolManagerProxyManagerproxy_from_urlkey_file	cert_file	cert_reqsca_certsssl_versionca_cert_dirBasePoolKeyschemehostportHTTPPoolKeytimeoutretriesstrictblocksource_addressHTTPSPoolKeyc             C   s_   i  } x$ |  j  D] } | j |  | | <q W| d j   | d <| d j   | d <|  |   S)a  
    Create a pool key of type ``key_class`` for a request.

    According to RFC 3986, both the scheme and host are case-insensitive.
    Therefore, this function normalizes both before constructing the pool
    key for an HTTPS request. If you wish to change this behaviour, provide
    alternate callables to ``key_fn_by_scheme``.

    :param key_class:
        The class to use when constructing the key. This should be a namedtuple
        with the ``scheme`` and ``host`` keys at a minimum.

    :param request_context:
        A dictionary-like object that contain the context for a request.
        It should contain a key for each field in the :class:`HTTPPoolKey`
    r   r   )_fieldsgetlower)Z	key_classrequest_contextcontextkey r)   U/home/ubuntu/projects/ifolica/build/requests/requests/packages/urllib3/poolmanager.py_default_key_normalizer/   s    r+   httphttpsc               @   s   e  Z d  Z d Z d Z d d d d  Z d d   Z d d	   Z d
 d   Z d d   Z	 d d d d  Z
 d d   Z d d   Z d d   Z d d d  Z d S)r   a$  
    Allows for arbitrary requests while transparently keeping track of
    necessary connection pools for you.

    :param num_pools:
        Number of connection pools to cache before discarding the least
        recently used pool.

    :param headers:
        Headers to include with all requests, unless other headers are given
        explicitly.

    :param \**connection_pool_kw:
        Additional parameters are used to create fresh
        :class:`urllib3.connectionpool.ConnectionPool` instances.

    Example::

        >>> manager = PoolManager(num_pools=2)
        >>> r = manager.request('GET', 'http://google.com/')
        >>> r = manager.request('GET', 'http://google.com/mail')
        >>> r = manager.request('GET', 'http://yahoo.com/')
        >>> len(manager.pools)
        2

    N
   c             K   sP   t  j |  |  | |  _ t | d d d   |  _ t |  _ t j   |  _ d  S)Ndispose_funcc             S   s
   |  j    S)N)close)pr)   r)   r*   <lambda>y   s    z&PoolManager.__init__.<locals>.<lambda>)r   __init__connection_pool_kwr   poolspool_classes_by_schemekey_fn_by_schemecopy)self	num_poolsheadersr4   r)   r)   r*   r3   u   s    			zPoolManager.__init__c             C   s   |  S)Nr)   )r9   r)   r)   r*   	__enter__   s    zPoolManager.__enter__c             C   s   |  j    d S)NF)clear)r9   exc_typeZexc_valexc_tbr)   r)   r*   __exit__   s    
zPoolManager.__exit__c             C   se   |  j  | } |  j } | d k rU |  j j   } x! t D] } | j | d  q8 Wn  | | | |  S)a  
        Create a new :class:`ConnectionPool` based on host, port and scheme.

        This method is used to actually create the connection pools handed out
        by :meth:`connection_from_url` and companion methods. It is intended
        to be overridden for customization.
        r,   N)r6   r4   r8   SSL_KEYWORDSpop)r9   r   r   r   pool_clskwargskwr)   r)   r*   	_new_pool   s    	zPoolManager._new_poolc             C   s   |  j  j   d S)z
        Empty our store of pools and direct them all to close.

        This will not affect in-flight connections, but they will not be
        re-used after completion.
        N)r5   r=   )r9   r)   r)   r*   r=      s    zPoolManager.clearr,   c             C   sz   | s t  d   n  |  j j   } | p- d | d <| sY t j | d j   d  } n  | | d <| | d <|  j |  S)z
        Get a :class:`ConnectionPool` based on the host, port, and scheme.

        If ``port`` isn't given, it will be derived from the ``scheme`` using
        ``urllib3.connectionpool.port_by_scheme``.
        zNo host specified.r,   r   P   r   r   )r	   r4   r8   r   r$   r%   connection_from_context)r9   r   r   r   r&   r)   r)   r*   connection_from_host   s    

z PoolManager.connection_from_hostc             C   s6   | d j    } |  j | } | |  } |  j |  S)z
        Get a :class:`ConnectionPool` based on the request context.

        ``request_context`` must at least contain the ``scheme`` key and its
        value must be a key in ``key_fn_by_scheme`` instance variable.
        r   )r%   r7   connection_from_pool_key)r9   r&   r   Zpool_key_constructorpool_keyr)   r)   r*   rH      s    z#PoolManager.connection_from_contextc             C   s^   |  j  j L |  j  j |  } | r) | S|  j | j | j | j  } | |  j  | <Wd QX| S)z
        Get a :class:`ConnectionPool` based on the provided pool key.

        ``pool_key`` should be a namedtuple that only contains immutable
        objects. At a minimum it must have the ``scheme``, ``host``, and
        ``port`` fields.
        N)r5   lockr$   rF   r   r   r   )r9   rK   poolr)   r)   r*   rJ      s    z$PoolManager.connection_from_pool_keyc             C   s.   t  |  } |  j | j d | j d | j S)a*  
        Similar to :func:`urllib3.connectionpool.connection_from_url` but
        doesn't pass any additional parameters to the
        :class:`urllib3.connectionpool.ConnectionPool` constructor.

        Additional parameters are taken from the :class:`.PoolManager`
        constructor.
        r   r   )r   rI   r   r   r   )r9   urlur)   r)   r*   connection_from_url   s    	zPoolManager.connection_from_urlTc       
      K   s  t  |  } |  j | j d | j d | j } d | d <d | d <d | k r` |  j | d <n  |  j d k	 r | j d k r | j | | |  } n | j | | j |  } | o | j	   } | s | St
 | |  } | j d	 k r d
 } n  | j d  }	 t |	 t  s't j |	 d | }	 n  y" |	 j | | d | d | }	 Wn% t k
 rp|	 j rh  n  | SYn X|	 | d <| | d <t j d | |  |  j | | |  S)a]  
        Same as :meth:`urllib3.connectionpool.HTTPConnectionPool.urlopen`
        with custom cross-host redirect logic and only sends the request-uri
        portion of the ``url``.

        The given ``url`` parameter must be absolute, such that an appropriate
        :class:`urllib3.connectionpool.ConnectionPool` can be chosen for it.
        r   r   Fassert_same_hostredirectr;   Nr,   i/  GETr   response_poolzRedirecting %s -> %s)r   rI   r   r   r   r;   proxyurlopenrequest_uriget_redirect_locationr   statusr$   
isinstancer   Zfrom_int	incrementr
   Zraise_on_redirectloginfo)
r9   methodrN   rR   rE   rO   connrT   redirect_locationr   r)   r)   r*   rW      s8    	$

	"		

zPoolManager.urlopen)__name__
__module____qualname____doc__rV   r3   r<   r@   rF   r=   rI   rH   rJ   rP   rW   r)   r)   r)   r*   r   W   s   	c                   sm   e  Z d  Z d Z d d d   f d d  Z d d   f d d  Z d d	 d
  Z d   f d d  Z   S)r   aw  
    Behaves just like :class:`PoolManager`, but sends all requests through
    the defined proxy, using the CONNECT method for HTTPS URLs.

    :param proxy_url:
        The URL of the proxy to be used.

    :param proxy_headers:
        A dictionary contaning headers that will be sent to the proxy. In case
        of HTTP they are being sent with each request, while in the
        HTTPS/CONNECT case they are sent only once. Could be used for proxy
        authentication.

    Example:
        >>> proxy = urllib3.ProxyManager('http://localhost:3128/')
        >>> r1 = proxy.request('GET', 'http://google.com/')
        >>> r2 = proxy.request('GET', 'http://httpbin.org/')
        >>> len(proxy.pools)
        1
        >>> r3 = proxy.request('GET', 'https://httpbin.org/')
        >>> r4 = proxy.request('GET', 'https://twitter.com/')
        >>> len(proxy.pools)
        3

    r.   Nc                s   t  | t  r. d | j | j | j f } n  t |  } | j sm t j | j d  } | j d |  } n  | j d k r t	 | j   n  | |  _
 | p i  |  _ |  j
 | d <|  j | d <t t |   j | | |  d  S)	Nz
%s://%s:%irG   r   r,   r-   _proxy_proxy_headers)zhttpzhttps)r[   r   r   r   r   r   r   r$   _replacer   rV   proxy_headerssuperr   r3   )r9   	proxy_urlr:   r;   ri   r4   rV   r   )	__class__r)   r*   r3   2  s    		zProxyManager.__init__r,   c                sV   | d k r( t  t |   j | | |  St  t |   j |  j j |  j j |  j j  S)Nr-   )rj   r   rI   rV   r   r   r   )r9   r   r   r   )rl   r)   r*   rI   I  s
    z!ProxyManager.connection_from_hostc             C   sI   i d d 6} t  |  j } | r/ | | d <n  | rE | j |  n  | S)z
        Sets headers needed by proxies: specifically, the Accept and Host
        headers. Only sets headers not provided by the user.
        z*/*AcceptHost)r   netlocupdate)r9   rN   r;   headers_ro   r)   r)   r*   _set_proxy_headersQ  s    zProxyManager._set_proxy_headersTc                sk   t  |  } | j d k rI | j d |  j  } |  j | |  | d <n  t t |   j | | d | | S)z@Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute.r,   r;   rR   )r   r   r$   r;   rr   rj   r   rW   )r9   r_   rN   rR   rE   rO   r;   )rl   r)   r*   rW   `  s
    zProxyManager.urlopen)rb   rc   rd   re   r3   rI   rr   rW   r)   r)   )rl   r*   r     s   c             K   s   t  d |  |  S)Nrk   )r   )rN   rE   r)   r)   r*   r   n  s    )zkey_filez	cert_filez	cert_reqszca_certszssl_versionr   )zschemezhostzport)ztimeoutzretrieszstrictzblockzsource_address)*
__future__r   collections	functoolsloggingurllib.parser   ImportErrorurlparse_collectionsr   connectionpoolr   r   r   
exceptionsr	   r
   r   requestr   Zutil.urlr   Z
util.retryr   __all__	getLoggerrb   r]   rA   
namedtupler   r#   r   r"   r+   partialr7   r6   r   r   r   r)   r)   r)   r*   <module>   sD    	

W