σ
±f·\c           @@  s  d  d l  m Z d  d l Z d  d l Z d  d l Z d 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 l m Z d d l m Z d d d g Z e j e  Z d6 Z d7 Z e j d/ e  Z  d0   Z! i e j" e! e   d1 6e j" e! e   d2 6Z# i e d1 6e	 d2 6Z$ d e f d3     YZ% d e% f d4     YZ& d5   Z' d S(8   i    (   t   absolute_importNi   (   t   RecentlyUsedContainer(   t   HTTPConnectionPoolt   HTTPSConnectionPool(   t   port_by_scheme(   t   LocationValueErrort   MaxRetryErrort   ProxySchemeUnknown(   t   six(   t   urljoin(   t   RequestMethods(   t	   parse_url(   t   Retryt   PoolManagert   ProxyManagert   proxy_from_urlt   key_filet	   cert_filet	   cert_reqst   ca_certst   ssl_versiont   ca_cert_dirt   ssl_contextt
   key_schemet   key_hostt   key_portt   key_timeoutt   key_retriest
   key_strictt	   key_blockt   key_source_addresst   key_key_filet   key_cert_filet   key_cert_reqst   key_ca_certst   key_ssl_versiont   key_ca_cert_dirt   key_ssl_contextt   key_maxsizet   key_headerst
   key__proxyt   key__proxy_headerst   key_socket_optionst   key__socks_optionst   key_assert_hostnamet   key_assert_fingerprintt   key_server_hostnamet   PoolKeyc         C@  s  | j    } | d j   | d <| d j   | d <xG d D]? } | | k r; | | d	 k	 r; t | | j    | | <q; q; W| j d  } | d	 k	 r¬ t |  | d <n  x1 t | j    D] } | j	 |  | d | <qΏ Wx* |  j
 D] } | | k rκ d	 | | <qκ qκ W|  |   S(
   sχ  
    Create a pool key out of a request context dictionary.

    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.
    :type  key_class: namedtuple
    :param request_context:
        A dictionary-like object that contain the context for a request.
    :type  request_context: dict

    :return: A namedtuple that can be used as a connection pool key.
    :rtype:  PoolKey
    t   schemet   hostt   headerst   _proxy_headerst   _socks_optionst   socket_optionst   key_(   R2   R3   R4   N(   t   copyt   lowert   Nonet	   frozensett   itemst   gett   tuplet   listt   keyst   popt   _fields(   t	   key_classt   request_contextt   contextt   keyt   socket_optst   field(    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyt   _default_key_normalizer;   s    !t   httpt   httpsc           B@  s   e  Z d  Z d Z d d d  Z d   Z d   Z d d  Z d   Z	 d d d d  Z
 d	   Z d d
  Z 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@  sM   t  j |  |  | |  _ t | d d   |  _ t |  _ t j   |  _ d  S(   Nt   dispose_funcc         S@  s
   |  j    S(   N(   t   close(   t   p(    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyt   <lambda>   t    (   R
   t   __init__t   connection_pool_kwR   t   poolst   pool_classes_by_schemet   key_fn_by_schemeR7   (   t   selft	   num_poolsR2   RQ   (    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyRP      s    			c         C@  s   |  S(   N(    (   RU   (    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyt	   __enter__€   s    c         C@  s   |  j    t S(   N(   t   cleart   False(   RU   t   exc_typet   exc_valt   exc_tb(    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyt   __exit__§   s    
c         C@  s   |  j  | } | d k r+ |  j j   } n  x d D] } | j | d  q2 W| d k r| x! t D] } | j | d  q_ Wn  | | | |  S(   s·  
        Create a new :class:`ConnectionPool` based on host, port, scheme, and
        any additional pool keyword arguments.

        If ``request_context`` is provided, it is provided as keyword arguments
        to the pool class used. 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.
        R0   R1   t   portRI   N(   R0   R1   R^   (   RS   R9   RQ   R7   R@   t   SSL_KEYWORDS(   RU   R0   R1   R^   RC   t   pool_clsRE   t   kw(    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyt	   _new_pool¬   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(   RR   RX   (   RU   (    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyRX   Η   s    RI   c         C@  sz   | s t  d   n  |  j |  } | p- d | d <| sY t j | d j   d  } n  | | d <| | d <|  j |  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``. If ``pool_kwargs`` is
        provided, it is merged with the instance's ``connection_pool_kw``
        variable and used to create the new connection pool, if one is
        needed.
        s   No host specified.RI   R0   iP   R^   R1   (   R   t   _merge_pool_kwargsR   R<   R8   t   connection_from_context(   RU   R1   R^   R0   t   pool_kwargsRC   (    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyt   connection_from_hostΠ   s    

c         C@  s<   | d j    } |  j | } | |  } |  j | d | S(   sβ   
        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.
        R0   RC   (   R8   RT   t   connection_from_pool_key(   RU   RC   R0   t   pool_key_constructort   pool_key(    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyRd   η   s    c      
   C@  sy   |  j  j g |  j  j |  } | r) | S| d } | d } | d } |  j | | | d | } | |  j  | <Wd QX| S(   sϋ   
        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.
        R0   R1   R^   RC   N(   RR   t   lockR<   Rb   (   RU   Ri   RC   t   poolR0   R1   R^   (    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyRg   τ   s    


c         C@  s4   t  |  } |  j | j d | j d | j d | S(   sΦ  
        Similar to :func:`urllib3.connectionpool.connection_from_url`.

        If ``pool_kwargs`` is not provided and a new pool needs to be
        constructed, ``self.connection_pool_kw`` is used to initialize
        the :class:`urllib3.connectionpool.ConnectionPool`. If ``pool_kwargs``
        is provided, it is used instead. Note that if a new pool does not
        need to be created for the request, the provided ``pool_kwargs`` are
        not used.
        R^   R0   Re   (   R   Rf   R1   R^   R0   (   RU   t   urlRe   t   u(    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyt   connection_from_url  s    !c         C@  sq   |  j  j   } | rm xU | j   D]D \ } } | d k r\ y | | =Wqf t k
 rX qf Xq" | | | <q" Wn  | S(   s  
        Merge a dictionary of override values for self.connection_pool_kw.

        This does not modify self.connection_pool_kw and returns a new dict.
        Any keys in the override dictionary with a value of ``None`` are
        removed from the merged dictionary.
        N(   RQ   R7   R;   R9   t   KeyError(   RU   t   overridet   base_pool_kwargsRE   t   value(    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyRc     s    c         K@  s  t  |  } |  j | j d | j d | j } t | d <t | d <d | k rf |  j 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  |	 j r| j |  rt t j | d   }
 x= |
 D]2 } | j   |	 j k rf| d j | d  qfqfWn  y" |	 j | | d
 | d | }	 Wn! t k
 rδ|	 j rΰ  n  | SX|	 | d	 <| | d <t j d | |  |  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^   R0   t   assert_same_hostt   redirectR2   RI   i/  t   GETt   retriest   responset   _pools   Redirecting %s -> %sN(   R   Rf   R1   R^   R0   RY   R2   R7   t   proxyR9   t   urlopent   request_urit   get_redirect_locationR	   t   statusR<   t
   isinstanceR   t   from_intt   remove_headers_on_redirectt   is_same_hostR>   R   t   iterkeysR8   R@   t	   incrementR   t   raise_on_redirectt   logt   info(   RU   t   methodRl   Rt   Ra   Rm   t   connRw   t   redirect_locationRv   R2   t   header(    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyRz   /  sD    	$

		"	

N(   t   __name__t
   __module__t   __doc__R9   Ry   RP   RW   R]   Rb   RX   Rf   Rd   Rg   Rn   Rc   t   TrueRz   (    (    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyR   {   s   						c           B@  sJ   e  Z d  Z d d d d  Z d d d d  Z d d  Z e d  Z RS(   sx  
    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 containing 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 r t	 | j   n  | |  _
 | p  i  |  _ |  j
 | d <|  j | d <t t |   j | | |  d  S(	   Ns
   %s://%s:%iiP   R^   RI   RJ   t   _proxyR3   (   RI   RJ   (   R~   R   R0   R1   R^   R   R   R<   t   _replaceR   Ry   t   proxy_headerst   superR   RP   (   RU   t	   proxy_urlRV   R2   R   RQ   Ry   R^   (    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyRP     s    		RI   c         C@  sb   | d k r. t  t |   j | | | d | St  t |   j |  j j |  j j |  j j d | S(   NRJ   Re   (   R   R   Rf   Ry   R1   R^   R0   (   RU   R1   R^   R0   Re   (    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyRf     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(   RU   Rl   R2   t   headers_R   (    (    s2   lib/python2.7/site-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.RI   R2   Rt   (   R   R0   R<   R2   R   R   R   Rz   (   RU   R   Rl   Rt   Ra   Rm   R2   (    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyRz   Ά  s
    N(	   R   R   R   R9   RP   Rf   R   R   Rz   (    (    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyR   m  s   c         K@  s   t  d |  |  S(   NR   (   R   (   Rl   Ra   (    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyR   Δ  s    (   R   R   R   R   R   R   R   (   R   R   R   R   R   R   R   R   R   R    R!   R"   R#   R$   R%   R&   R'   R(   R)   R*   R+   R,   R-   R.   ((   t
   __future__R    t   collectionst	   functoolst   loggingt   _collectionsR   t   connectionpoolR   R   R   t
   exceptionsR   R   R   t   packagesR   t   packages.six.moves.urllib.parseR	   t   requestR
   t   util.urlR   t
   util.retryR   t   __all__t	   getLoggerR   R   R_   t   _key_fieldst
   namedtupleR/   RH   t   partialRT   RS   R   R   R   (    (    (    s2   lib/python2.7/site-packages/urllib3/poolmanager.pyt   <module>   sf                           	5
ςW