
j^c           @   s(  d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z d d l m	 Z	 d d l
 m Z m Z m Z m Z m Z d d l m Z m Z m Z m Z d d	 l m Z m Z 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# m$ Z$ m% Z% m& Z& d d l' m( Z( d d l) m* Z* d d l m+ Z+ m, Z, m- Z- m. Z. m/ Z/ m0 Z0 d d l1 m2 Z2 d d l m3 Z3 e j4 d k ry e j5 Z6 Wn e7 k
 re j8 Z6 n Xn	 e j Z6 e d  Z9 e d  Z: d e; f d     YZ< d e< f d     YZ= d   Z> d S(   s   
requests.session
~~~~~~~~~~~~~~~~

This module provides a Session object to manage and persist settings across
requests (cookies, auth, proxies).
iN(   t	   timedelta(   t   OrderedDicti   (   t   _basic_auth_str(   t	   cookielibt   is_py3t   urljoint   urlparset   Mapping(   t   cookiejar_from_dictt   extract_cookies_to_jart   RequestsCookieJart   merge_cookies(   t   Requestt   PreparedRequestt   DEFAULT_REDIRECT_LIMIT(   t   default_hookst   dispatch_hook(   t   to_native_string(   t   to_key_val_listt   default_headerst   DEFAULT_PORTS(   t   TooManyRedirectst   InvalidSchemat   ChunkedEncodingErrort   ContentDecodingError(   t   CaseInsensitiveDict(   t   HTTPAdapter(   t   requote_urit   get_environ_proxiest   get_netrc_autht   should_bypass_proxiest   get_auth_from_urlt   rewind_body(   t   codes(   t   REDIRECT_STATIt   win32c         C   s   | d k r |  S|  d k r  | St | t  o; t |  t  sB |  S| t |   } | j t |    g  | j   D] \ } } | d k rt | ^ qt } x | D] } | | =q W| S(   s   Determines appropriate setting for a given request, taking into account
    the explicit setting on that request, and the setting in the session. If a
    setting is a dictionary, they will be merged together using `dict_class`
    N(   t   Nonet
   isinstanceR   R   t   updatet   items(   t   request_settingt   session_settingt
   dict_classt   merged_settingt   kt   vt	   none_keyst   key(    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   merge_setting2   s    1c         C   sZ   | d k s! | j d  g  k r% |  S|  d k sF |  j d  g  k rJ | St |  | |  S(   s   Properly merges both requests and session hooks.

    This is necessary because when request_hooks == {'response': []}, the
    merge breaks Session hooks entirely.
    t   responseN(   R$   t   getR0   (   t   request_hookst   session_hooksR*   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   merge_hooksQ   s
    !!t   SessionRedirectMixinc           B   sP   e  Z d    Z d   Z e d e d d e d  Z d   Z d   Z	 d   Z
 RS(   c         C   s?   | j  r; | j d } t r. | j d  } n  t | d  Sd S(   s7   Receives a Response. Returns a redirect URI or ``None``t   locationt   latin1t   utf8N(   t   is_redirectt   headersR   t   encodeR   R$   (   t   selft   respR7   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   get_redirect_targetb   s    	c         C   s   t  |  } t  |  } | j | j k r. t S| j d k rn | j d k rn | j d k rn | j d k rn t S| j | j k } | j | j k } t j | j d  d f } | r | j | k r | j | k r t S| p | S(   sF   Decide whether Authorization header should be removed when redirectingt   httpiP   t   httpsi  N(   iP   N(   i  N(	   R   t   hostnamet   Truet   schemet   portR$   t   FalseR   R2   (   R=   t   old_urlt   new_urlt
   old_parsedt
   new_parsedt   changed_portt   changed_schemet   default_port(    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   should_strip_authw   s    c	         k   s4  g  }
 |  j  |  } t | j  j } x| r/| j   } |
 j |  |
 d | _ y | j Wn- t t	 t
 f k
 r | j j d t  n Xt | j  |  j k r t d j |  j  d |  n  | j   | j d  rt | j  } d j t | j  | g  } n  t |  } | j d k rI| rI| j d |  } n | j r^| j } n  | j   } | j st | j t |   } n t |  } t |  | _ |  j | |  | j t j t j  f k r
d } x! | D] } | j! j" | d  qWd | _$ n  | j! } | j" d d  t% | j& | | j  t' | j& |  j(  | j) | j&  |  j* | |  } |  j+ | |  | j, d k	 od	 | k pd | k } | rt- |  n  | } | r| Vq* |  j. | d | d | d | d | d | d t |	 } t% |  j( | | j  |  j  |  } | Vq* Wd S(   sB   Receives a Response. Returns a generator of Responses or Requests.i   t   decode_contents   Exceeded {} redirects.R1   s   //t   :t    t   fragments   Content-Lengths   Content-Types   Transfer-Encodingt   Cookiet   streamt   timeoutt   verifyt   certt   proxiest   allow_redirectsN(   s   Content-Lengths   Content-Types   Transfer-Encoding(/   R?   R   t   urlRR   t   copyt   appendt   historyt   contentR   R   t   RuntimeErrort   rawt   readRF   t   lent   max_redirectsR   t   formatt   closet
   startswitht   joinR   RD   t   _replacet   geturlt   netlocR   R   t   rebuild_methodt   status_codeR!   t   temporary_redirectt   permanent_redirectR;   t   popR$   t   bodyR	   t   _cookiesR   t   cookiest   prepare_cookiest   rebuild_proxiest   rebuild_autht   _body_positionR    t   send(   R=   R>   t   reqRT   RU   RV   RW   RX   t   yield_requestst   adapter_kwargst   histRZ   t   previous_fragmentt   prepared_requestt   parsed_rurlt   parsedt   purged_headerst   headerR;   t
   rewindable(    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   resolve_redirects   sv    	!
!				c         C   s{   | j  } | j } d | k r@ |  j | j j |  r@ | d =n  |  j rU t |  n d } | d k	 rw | j |  n  d S(   s   When being redirected we may want to strip authentication from the
        request to avoid leaking credentials. This method intelligently removes
        and reapplies authentication where possible to avoid credential loss.
        t   AuthorizationN(   R;   RZ   RN   t   requestt	   trust_envR   R$   t   prepare_auth(   R=   R}   R1   R;   RZ   t   new_auth(    (    s.   site-packages/pip/_vendor/requests/sessions.pyRu      s    		$
c         C   s5  | d k	 r | n i  } | j } | j } t |  j } | j   } | j d  } t | d | } |  j r | r t	 | d | }	 |	 j | |	 j d   }
 |
 r | j
 | |
  q n  d | k r | d =n  y t | |  \ } } Wn t k
 rd \ } } n X| r1| r1t | |  | d <n  | S(   s  This method re-evaluates the proxy configuration by considering the
        environment variables. If we are redirected to a URL covered by
        NO_PROXY, we strip the proxy configuration. Otherwise, we set missing
        proxy keys for this URL (in case they were stripped by a previous
        redirect).

        This method also replaces the Proxy-Authorization header where
        necessary.

        :rtype: dict
        t   no_proxyt   alls   Proxy-AuthorizationN(   NN(   R$   R;   RZ   R   RD   R[   R2   R   R   R   t
   setdefaultR   t   KeyErrorR   (   R=   R}   RX   R;   RZ   RD   t   new_proxiesR   t   bypass_proxyt   environ_proxiest   proxyt   usernamet   password(    (    s.   site-packages/pip/_vendor/requests/sessions.pyRt     s*    		
c         C   s   | j  } | j t j k r0 | d k r0 d } n  | j t j k rW | d k rW d } n  | j t j k r~ | d k r~ d } n  | | _  d S(   s   When being redirected we may want to change the method of the request
        based on certain specs or browser behavior.
        t   HEADt   GETt   POSTN(   t   methodRl   R!   t	   see_othert   foundt   moved(   R=   R}   R1   R   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyRk   :  s    				N(   t   __name__t
   __module__R?   RN   RF   R$   RC   R   Ru   Rt   Rk   (    (    (    s.   site-packages/pip/_vendor/requests/sessions.pyR6   `   s   		m		)t   Sessionc           B   s  e  Z d  Z d d d d d d d d d	 d
 d d g Z d   Z d   Z d   Z d   Z d  d  d  d  d  d  d  e	 d  d  d  d  d  d  d  Z
 d   Z d   Z d   Z d  d  d  Z d  d  Z d  d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(!   s  A Requests session.

    Provides cookie persistence, connection-pooling, and configuration.

    Basic Usage::

      >>> import requests
      >>> s = requests.Session()
      >>> s.get('https://httpbin.org/get')
      <Response [200]>

    Or as a context manager::

      >>> with requests.Session() as s:
      ...     s.get('https://httpbin.org/get')
      <Response [200]>
    R;   Rr   t   authRX   t   hookst   paramsRV   RW   t   adaptersRT   R   Rc   c         C   s   t    |  _ d  |  _ i  |  _ t   |  _ i  |  _ t |  _	 t
 |  _ d  |  _ t |  _ t
 |  _ t i   |  _ t   |  _ |  j d t    |  j d t    d  S(   Ns   https://s   http://(   R   R;   R$   R   RX   R   R   R   RF   RT   RC   RV   RW   R   Rc   R   R   Rr   R   R   t   mountR   (   R=   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   __init__j  s    								c         C   s   |  S(   N(    (   R=   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyt	   __enter__  s    c         G   s   |  j    d  S(   N(   Re   (   R=   t   args(    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   __exit__  s    c         C   s*  | j  p i  } t | t j  s0 t |  } n  t t t   |  j   |  } | j } |  j r | r |  j r t	 | j
  } n  t   } | j d | j j   d | j
 d | j d | j d | j d t | j |  j d t d t | j |  j  d	 t | |  j  d
 | d t | j |  j   
| S(   s  Constructs a :class:`PreparedRequest <PreparedRequest>` for
        transmission and returns it. The :class:`PreparedRequest` has settings
        merged from the :class:`Request <Request>` instance and those of the
        :class:`Session`.

        :param request: :class:`Request` instance to prepare with this
            session's settings.
        :rtype: requests.PreparedRequest
        R   RZ   t   filest   datat   jsonR;   R*   R   R   Rr   R   (   Rr   R%   R   t	   CookieJarR   R   R
   R   R   R   RZ   R   t   prepareR   t   upperR   R   R   R0   R;   R   R   R5   R   (   R=   R   Rr   t   merged_cookiesR   t   p(    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   prepare_request  s*    
							c         C   s   t  d | j   d | d | d | d | p- i  d | d | p? i  d | d	 | d
 |  
} |  j |  } | po i  } |  j | j | | | |  } i |	 d 6|
 d 6} | j |  |  j | |  } | S(   s  Constructs a :class:`Request <Request>`, prepares it and sends it.
        Returns :class:`Response <Response>` object.

        :param method: method for the new :class:`Request` object.
        :param url: URL for the new :class:`Request` object.
        :param params: (optional) Dictionary or bytes to be sent in the query
            string for the :class:`Request`.
        :param data: (optional) Dictionary, list of tuples, bytes, or file-like
            object to send in the body of the :class:`Request`.
        :param json: (optional) json to send in the body of the
            :class:`Request`.
        :param headers: (optional) Dictionary of HTTP Headers to send with the
            :class:`Request`.
        :param cookies: (optional) Dict or CookieJar object to send with the
            :class:`Request`.
        :param files: (optional) Dictionary of ``'filename': file-like-objects``
            for multipart encoding upload.
        :param auth: (optional) Auth tuple or callable to enable
            Basic/Digest/Custom HTTP Auth.
        :param timeout: (optional) How long to wait for the server to send
            data before giving up, as a float, or a :ref:`(connect timeout,
            read timeout) <timeouts>` tuple.
        :type timeout: float or tuple
        :param allow_redirects: (optional) Set to True by default.
        :type allow_redirects: bool
        :param proxies: (optional) Dictionary mapping protocol or protocol and
            hostname to the URL of the proxy.
        :param stream: (optional) whether to immediately download the response
            content. Defaults to ``False``.
        :param verify: (optional) Either a boolean, in which case it controls whether we verify
            the server's TLS certificate, or a string, in which case it must be a path
            to a CA bundle to use. Defaults to ``True``.
        :param cert: (optional) if String, path to ssl client cert file (.pem).
            If Tuple, ('cert', 'key') pair.
        :rtype: requests.Response
        R   RZ   R;   R   R   R   R   R   Rr   R   RU   RY   (   R   R   R   t   merge_environment_settingsRZ   R&   Rw   (   R=   R   RZ   R   R   R;   Rr   R   R   RU   RY   RX   R   RT   RV   RW   R   Rx   t   prept   settingst   send_kwargsR>   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyR     s*    )	
c         K   s#   | j  d t  |  j d | |  S(   s   Sends a GET request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        :rtype: requests.Response
        RY   R   (   R   RC   R   (   R=   RZ   t   kwargs(    (    s.   site-packages/pip/_vendor/requests/sessions.pyR2     s    c         K   s#   | j  d t  |  j d | |  S(   s   Sends a OPTIONS request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        :rtype: requests.Response
        RY   t   OPTIONS(   R   RC   R   (   R=   RZ   R   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   options!  s    c         K   s#   | j  d t  |  j d | |  S(   s   Sends a HEAD request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        :rtype: requests.Response
        RY   R   (   R   RF   R   (   R=   RZ   R   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   head,  s    c         K   s   |  j  d | d | d | | S(   s  Sends a POST request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param data: (optional) Dictionary, list of tuples, bytes, or file-like
            object to send in the body of the :class:`Request`.
        :param json: (optional) json to send in the body of the :class:`Request`.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        :rtype: requests.Response
        R   R   R   (   R   (   R=   RZ   R   R   R   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   post7  s    c         K   s   |  j  d | d | | S(   su  Sends a PUT request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param data: (optional) Dictionary, list of tuples, bytes, or file-like
            object to send in the body of the :class:`Request`.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        :rtype: requests.Response
        t   PUTR   (   R   (   R=   RZ   R   R   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   putD  s    
c         K   s   |  j  d | d | | S(   sw  Sends a PATCH request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param data: (optional) Dictionary, list of tuples, bytes, or file-like
            object to send in the body of the :class:`Request`.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        :rtype: requests.Response
        t   PATCHR   (   R   (   R=   RZ   R   R   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   patchP  s    
c         K   s   |  j  d | |  S(   s   Sends a DELETE request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        :rtype: requests.Response
        t   DELETE(   R   (   R=   RZ   R   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   delete\  s    c         K   s  | j  d |  j  | j  d |  j  | j  d |  j  | j  d |  j  t | t  rj t d   n  | j d t	  } | j
 d  } | j } |  j d | j  } t   } | j | |  } t   | }	 t d |	  | _ t d	 | | |  } | j r1x- | j D] }
 t |  j |
 j |
 j  qWn  t |  j | | j  |  j | | |  } | r{g  | D] }
 |
 ^ qin g  } | r| j d
 |  | j   } | | _ n  | sy( t |  j | | d t	 |  | _ Wqt k
 rqXn  | s| j n  | S(   sI   Send a given PreparedRequest.

        :rtype: requests.Response
        RT   RV   RW   RX   s#   You can only send PreparedRequests.RY   RZ   t   secondsR1   i    Ry   (   R   RT   RV   RW   RX   R%   R   t
   ValueErrorRo   RC   R2   R   t   get_adapterRZ   t   preferred_clockRw   R    t   elapsedR   R]   R	   Rr   R   R`   R   t   insertt   nextt   _nextt   StopIterationR^   (   R=   R   R   RY   RT   R   t   adaptert   startt   rR   R>   t   genR]   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyRw   f  sB    			 %(
c   
      C   s  |  j  r | d k	 r$ | j d  n d } t | d | } x* | j   D] \ } }	 | j | |	  qI W| t k s | d k r t j j d  p t j j d  } q n  t	 | |  j
  } t	 | |  j  } t	 | |  j  } t	 | |  j  } i | d 6| d 6| d 6| d 6S(	   s^   
        Check the environment and merge it with some settings.

        :rtype: dict
        R   t   REQUESTS_CA_BUNDLEt   CURL_CA_BUNDLERV   RX   RT   RW   N(   R   R$   R2   R   R'   R   RC   t   ost   environR0   RX   RT   RV   RW   (
   R=   RZ   RX   RT   RV   RW   R   t   env_proxiesR,   R-   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyR     s    	!c         C   sX   x< |  j  j   D]+ \ } } | j   j | j    r | Sq Wt d j |    d S(   s~   
        Returns the appropriate connection adapter for the given URL.

        :rtype: requests.adapters.BaseAdapter
        s*   No connection adapters were found for {!r}N(   R   R'   t   lowerRf   R   Rd   (   R=   RZ   t   prefixR   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyR     s    c         C   s(   x! |  j  j   D] } | j   q Wd S(   s+   Closes all adapters and as such the sessionN(   R   t   valuesRe   (   R=   R-   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyRe     s    c         C   so   | |  j  | <g  |  j  D]$ } t |  t |  k  r | ^ q } x' | D] } |  j  j |  |  j  | <qH Wd S(   sw   Registers a connection adapter to a prefix.

        Adapters are sorted in descending order by prefix length.
        N(   R   Rb   Ro   (   R=   R   R   R,   t   keys_to_moveR/   (    (    s.   site-packages/pip/_vendor/requests/sessions.pyR     s    4c            s     f d     j  D } | S(   Nc            s%   i  |  ] } t    | d   |  q S(   N(   t   getattrR$   (   t   .0t   attr(   R=   (    s.   site-packages/pip/_vendor/requests/sessions.pys
   <dictcomp>  s   	 (   t	   __attrs__(   R=   t   state(    (   R=   s.   site-packages/pip/_vendor/requests/sessions.pyt   __getstate__  s    c         C   s1   x* | j    D] \ } } t |  | |  q Wd  S(   N(   R'   t   setattr(   R=   R   R   t   value(    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   __setstate__  s    N(   R   R   t   __doc__R   R   R   R   R   R$   RC   R   R2   R   R   R   R   R   R   Rw   R   R   Re   R   R   R   (    (    (    s.   site-packages/pip/_vendor/requests/sessions.pyR   Q  s2   		7			)D				
	I					c           C   s   t    S(   sZ  
    Returns a :class:`Session` for context-management.

    .. deprecated:: 1.0.0

        This method has been deprecated since version 1.0.0 and is only kept for
        backwards compatibility. New code should use :class:`~requests.sessions.Session`
        to create a session. This may be removed at a future date.

    :rtype: Session
    (   R   (    (    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   session  s    (?   R   R   t   syst   timet   datetimeR    t   collectionsR   R   R   t   compatR   R   R   R   R   Rr   R   R	   R
   R   t   modelsR   R   R   R   R   R   t   _internal_utilsR   t   utilsR   R   R   t
   exceptionsR   R   R   R   t
   structuresR   R   R   R   R   R   R   R   R    t   status_codesR!   R"   t   platformt   perf_counterR   t   AttributeErrort   clockR0   R5   t   objectR6   R   R   (    (    (    s.   site-packages/pip/_vendor/requests/sessions.pyt   <module>	   s<   ("".	 