ó
¨9Vc           @   s1  d  d l  Z  y d  d l m Z Wn! e k
 rC d  d l m Z n Xd d l m Z d d l m Z m	 Z	 d d l m
 Z
 d d l m Z m Z d d l m Z d d	 l m Z d d
 l m Z d d d g Z i e d 6e	 d 6Z e  j e ƒ Z d Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d S(   iÿÿÿÿN(   t   urljoini   (   t   RecentlyUsedContainer(   t   HTTPConnectionPoolt   HTTPSConnectionPool(   t   port_by_scheme(   t   LocationValueErrort   MaxRetryError(   t   RequestMethods(   t	   parse_url(   t   Retryt   PoolManagert   ProxyManagert   proxy_from_urlt   httpt   httpst   key_filet	   cert_filet	   cert_reqst   ca_certst   ssl_versionc           B   sk   e  Z d  Z d Z d d d „ Z d „  Z d „  Z d „  Z d „  Z	 d d d „ Z
 d	 „  Z e d
 „ Z RS(   s$  
    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

    i
   c         K   s5   t  j |  | ƒ | |  _ t | d d „  ƒ|  _ d  S(   Nt   dispose_funcc         S   s
   |  j  ƒ  S(   N(   t   close(   t   p(    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyt   <lambda>A   s    (   R   t   __init__t   connection_pool_kwR   t   pools(   t   selft	   num_poolst   headersR   (    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyR   =   s    		c         C   s   |  S(   N(    (   R   (    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyt	   __enter__C   s    c         C   s   |  j  ƒ  t S(   N(   t   cleart   False(   R   t   exc_typet   exc_valt   exc_tb(    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyt   __exit__F   s    
c         C   sb   t  | } |  j } | d k rR |  j j ƒ  } x! t D] } | j | d ƒ q5 Wn  | | | |  S(   s  
        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(   t   pool_classes_by_schemeR   t   copyt   SSL_KEYWORDSt   popt   None(   R   t   schemet   hostt   portt   pool_clst   kwargst   kw(    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyt	   _new_poolK   s    
	c         C   s   |  j  j ƒ  d S(   s´   
        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(   R   R   (   R   (    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyR   \   s    R   c      
   C   s   | s t  d ƒ ‚ n  | p d } | p6 t j | d ƒ } | | | f } |  j j C |  j j | ƒ } | rq | S|  j | | | ƒ } | |  j | <Wd QX| S(   sÖ   
        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``.
        s   No host specified.R   iP   N(   R   R   t   getR   t   lockR0   (   R   R+   R,   R*   t   pool_keyt   pool(    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyt   connection_from_hoste   s    c         C   s.   t  | ƒ } |  j | j d | j d | j ƒS(   s*  
        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   R5   R+   R,   R*   (   R   t   urlt   u(    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyt   connection_from_url   s    	c   
   	   K   s«  t  | ƒ } |  j | j d | j d | j ƒ} t | d <t | 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
 rl|	 j rh‚  n  | SX|	 | d	 <| | d <t j d | | f ƒ |  j	 | | |  S(   s]  
        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*   t   assert_same_hostt   redirectR   R   i/  t   GETt   retriest   responset   _pools   Redirecting %s -> %sN(   R   R5   R+   R,   R*   R    R   t   proxyR)   t   urlopent   request_urit   get_redirect_locationR    t   statusR1   t
   isinstanceR	   t   from_intt	   incrementR   t   raise_on_redirectt   logt   info(
   R   t   methodR6   R:   R/   R7   t   connR=   t   redirect_locationR<   (    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyR@      s8    	$

	"	

N(   t   __name__t
   __module__t   __doc__R)   R?   R   R   R$   R0   R   R5   R8   t   TrueR@   (    (    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyR
      s   						c           B   sG   e  Z d  Z d d d d „ Z d d d „ Z d d „ Z e d „ Z RS(   sw  
    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

    i
   c         K   sá   t  | t ƒ r. d | j | j | j f } n  t | ƒ } | j sm t j | j d ƒ } | j d | ƒ } n  | j d	 k s t	 d | j ƒ ‚ | |  _
 | p¡ i  |  _ |  j
 | d <|  j | d <t t |  ƒ j | | |  d  S(
   Ns
   %s://%s:%iiP   R,   R   R   s   Not supported proxy scheme %st   _proxyt   _proxy_headers(   s   https   https(   RD   R   R*   R+   R,   R   R   R1   t   _replacet   AssertionErrorR?   t   proxy_headerst   superR   R   (   R   t	   proxy_urlR   R   RU   R   R?   R,   (    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyR   Û   s    		R   c         C   sV   | d k r( t  t |  ƒ j | | | ƒ St  t |  ƒ j |  j j |  j j |  j j ƒ S(   NR   (   RV   R   R5   R?   R+   R,   R*   (   R   R+   R,   R*   (    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyR5   ò   s
    c         C   sI   i d d 6} t  | ƒ j } | r/ | | d <n  | rE | j | ƒ n  | S(   s   
        Sets headers needed by proxies: specifically, the Accept and Host
        headers. Only sets headers not provided by the user.
        s   */*t   Acceptt   Host(   R   t   netloct   update(   R   R6   R   t   headers_RZ   (    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyt   _set_proxy_headersú   s    c         K   sk   t  | ƒ } | j d k rI | j d |  j ƒ } |  j | | ƒ | d <n  t t |  ƒ j | | d | | S(   s@   Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute.R   R   R:   (   R   R*   R1   R   R]   RV   R   R@   (   R   RJ   R6   R:   R/   R7   R   (    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyR@   	  s
    N(	   RM   RN   RO   R)   R   R5   R]   RP   R@   (    (    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyR   À   s   c         K   s   t  d |  |  S(   NRW   (   R   (   R6   R/   (    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyR     s    (   s   key_files	   cert_files	   cert_reqss   ca_certss   ssl_version(   t   loggingt   urllib.parseR    t   ImportErrort   urlparset   _collectionsR   t   connectionpoolR   R   R   t
   exceptionsR   R   t   requestR   t   util.urlR   t
   util.retryR	   t   __all__R%   t	   getLoggerRM   RH   R'   R
   R   R   (    (    (    sN   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/packages/urllib3/poolmanager.pyt   <module>   s*   
 ¡W