ó
ZÐ…_c           @   sË  d  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 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 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' d d l m( Z( m) Z) m* Z* m+ Z+ m, Z, d d l- m. Z. d d l m/ Z/ d Z0 e	 d „ Z1 e	 d „ Z2 d e3 f d „  ƒ  YZ4 d e4 f d „  ƒ  YZ5 d „  Z6 d S(   s”   
requests.session
~~~~~~~~~~~~~~~~

This module provides a Session object to manage and persist settings across
requests (cookies, auth, proxies).

iÿÿÿÿN(   t   Mapping(   t   datetimei   (   t   _basic_auth_str(   t	   cookielibt   OrderedDictt   urljoint   urlparse(   t   cookiejar_from_dictt   extract_cookies_to_jart   RequestsCookieJart   merge_cookies(   t   Requestt   PreparedRequestt   DEFAULT_REDIRECT_LIMIT(   t   default_hookst   dispatch_hook(   t   to_key_val_listt   default_headerst   to_native_stringt   DEFAULT_PORTS(   t   TooManyRedirectst   InvalidSchemat   ChunkedEncodingErrort   ContentDecodingError(   t   RecentlyUsedContainer(   t   CaseInsensitiveDict(   t   HTTPAdapter(   t   requote_urit   get_environ_proxiest   get_netrc_autht   should_bypass_proxiest   get_auth_from_url(   t   codes(   t   REDIRECT_STATIiè  c         C   sº   | d k r |  S|  d k r  | St | t ƒ o; t |  t ƒ sB |  S| t | ƒ ƒ } | j t |  ƒ ƒ x0 |  j ƒ  D]" \ } } | d k rt | | =qt qt Wt d „  | j ƒ  Dƒ ƒ } | 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`
    c         s   s-   |  ]# \ } } | d  k	 r | | f Vq d  S(   N(   t   None(   t   .0t   kt   v(    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pys	   <genexpr>F   s    N(   R"   t
   isinstanceR    R   t   updatet   itemst   dict(   t   request_settingt   session_settingt
   dict_classt   merged_settingR$   R%   (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   merge_setting*   s    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   getR.   (   t   request_hookst   session_hooksR,   (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   merge_hooksK   s
    !!t   SessionRedirectMixinc           B   s;   e  Z d  „  Z e d e d d d „ Z d „  Z d „  Z RS(   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   R0   (   t   selft   old_urlt   new_urlt
   old_parsedt
   new_parsedt   changed_portt   changed_schemet   default_port(    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   should_strip_auth]   s    c         c   s;  d } g  }	 x(| j  r6| j ƒ  }
 | d k rU |	 j | ƒ t |	 ƒ } | | _ n  y | j Wn- t t t f k
 r | j	 j
 d t ƒ n X| |  j k rµ t d |  j ƒ ‚ n  | j ƒ  | j d } | j } | j d ƒ r	t | j ƒ } d | j | f } n  t | ƒ } | j ƒ  } | j sEt | j t | ƒ ƒ } n t | ƒ } t | ƒ |
 _ | j r‘| j |
 j k r‘|
 j |  j | j <n  | j t j k r¸| d k r¸d } n  | j t j k rß| d k rßd } n  | j t j  k r| d	 k rd } n  | |
 _ | j t j! t j" f k rRd
 |
 j k rF|
 j d
 =n  d |
 _$ n  |
 j } y | d =Wn t% k
 ryn Xt& |
 j' | | j	 ƒ |
 j' j( |  j) ƒ |
 j* |
 j' ƒ |  j+ |
 | ƒ } |  j, |
 | ƒ |
 } |  j- | d | d | d | d | d | d t ƒ} t& |  j) |
 | j	 ƒ | d 7} | Vq Wd S(   s6   Receives a Response. Returns a generator of Responses.i    t   decode_contents   Exceeded %s redirects.t   locations   //s   %s:%st   HEADt   GETt   POSTs   Content-Lengtht   Cookiet   streamt   timeoutt   verifyt   certt   proxiest   allow_redirectsi   N(.   t   is_redirectt   copyt   appendt   listt   historyt   contentR   R   t   RuntimeErrort   rawt   readR;   t   max_redirectsR   t   closet   headerst   methodt
   startswithR   t   urlR9   t   geturlt   netlocR   R   R   t   is_permanent_redirectt   redirect_cachet   status_codeR    t	   see_othert   foundt   movedt   temporary_redirectt   permanent_redirectR"   t   bodyt   KeyErrorR   t   _cookiesR'   t   cookiest   prepare_cookiest   rebuild_proxiest   rebuild_autht   send(   R<   t   respt   reqRK   RL   RM   RN   RO   t   it   histt   prepared_requestt   new_histR_   R]   t   parsed_rurlt   parsedR\   (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   resolve_redirectsv   s|    
								
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\   R_   RD   t   requestt	   trust_envR   R"   t   prepare_auth(   R<   Rv   R/   R\   R_   t   new_auth(    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyRp   å   s    		$
c         C   s  | j  } | j } t | ƒ j } | d k	 r9 | j ƒ  n i  } |  j r t | ƒ r t | ƒ } | j	 | ƒ } | r | j
 | | | ƒ q n  d | k r¦ | d =n  y t | | ƒ \ }	 }
 Wn t k
 rß d \ }	 }
 n X|	 r|
 r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.
        s   Proxy-AuthorizationN(   NN(   R\   R_   R   R9   R"   RR   R}   R   R   R0   t
   setdefaultR   Rk   R   (   R<   Rv   RO   R\   R_   R9   t   new_proxiest   environ_proxiest   proxyt   usernamet   password(    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyRo   ú   s$    		
N(	   t   __name__t
   __module__RD   R;   R"   R8   Rz   Rp   Ro   (    (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyR4   [   s
   	n	t   Sessionc           B   s  e  Z d  Z d 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('http://httpbin.org/get')
      200
    R\   Rm   t   authRO   t   hookst   paramsRM   RN   t   prefetcht   adaptersRK   R}   RZ   c         C   s´   t  ƒ  |  _ d  |  _ i  |  _ t ƒ  |  _ i  |  _ t |  _	 t
 |  _ d  |  _ t |  _ t
 |  _ t i  ƒ |  _ t ƒ  |  _ |  j d t ƒ  ƒ |  j d t ƒ  ƒ t t ƒ |  _ d  S(   Ns   https://s   http://(   R   R\   R"   R‰   RO   R   RŠ   R‹   R;   RK   R8   RM   RN   R   RZ   R}   R   Rm   R   R   t   mountR   R   t   REDIRECT_CACHE_SIZERc   (   R<   (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   __init__3  s    								c         C   s   |  S(   N(    (   R<   (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt	   __enter__i  s    c         G   s   |  j  ƒ  d  S(   N(   R[   (   R<   t   args(    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   __exit__l  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(   sb  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.
        R]   R_   t   filest   datat   jsonR\   R,   R‹   R‰   Rm   RŠ   (   Rm   R&   R   t	   CookieJarR   R
   R	   R‰   R}   R   R_   R   t   prepareR]   t   upperR”   R•   R–   R.   R\   R   R‹   R3   RŠ   (   R<   R|   Rm   t   merged_cookiesR‰   t   p(    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   prepare_requesto  s*    								c         C   sÓ   t  | ƒ } t d | j ƒ  d | d | d | d | p9 i  d | d | pK i  d | d	 | d
 | ƒ 
} |  j | ƒ } | p{ i  } |  j | j | | | | ƒ } i |	 d 6|
 d 6} | j | ƒ |  j | |  } | S(   sC  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 or bytes 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 (`connect timeout, read
            timeout <user/advanced.html#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 to the URL of
            the proxy.
        :param stream: (optional) whether to immediately download the response
            content. Defaults to ``False``.
        :param verify: (optional) if ``True``, the SSL cert will be verified.
            A CA_BUNDLE path can also be provided.
        :param cert: (optional) if String, path to ssl client cert file (.pem).
            If Tuple, ('cert', 'key') pair.
        R]   R_   R\   R”   R•   R–   R‹   R‰   Rm   RŠ   RL   RP   (   R   R   R™   Rœ   t   merge_environment_settingsR_   R'   Rq   (   R<   R]   R_   R‹   R•   R\   Rm   R”   R‰   RL   RP   RO   RŠ   RK   RM   RN   R–   Rs   t   prept   settingst   send_kwargsRr   (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyR|   —  s,    2	
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.
        RP   RH   (   R€   R8   R|   (   R<   R_   t   kwargs(    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyR0   ê  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.
        RP   t   OPTIONS(   R€   R8   R|   (   R<   R_   R¡   (    (    s5   /usr/lib/python2.7/site-packages/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.
        RP   RG   (   R€   R;   R|   (   R<   R_   R¡   (    (    s5   /usr/lib/python2.7/site-packages/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, 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.
        RI   R•   R–   (   R|   (   R<   R_   R•   R–   R¡   (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   post  s    	c         K   s   |  j  d | d | | S(   s7  Sends a PUT request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        t   PUTR•   (   R|   (   R<   R_   R•   R¡   (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   put  s    c         K   s   |  j  d | d | | S(   s9  Sends a PATCH request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        t   PATCHR•   (   R|   (   R<   R_   R•   R¡   (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   patch  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.
        t   DELETE(   R|   (   R<   R_   R¡   (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   delete'  s    c         K   sq  | j  d |  j ƒ | j  d |  j ƒ | j  d |  j ƒ | j  d |  j ƒ t | t ƒ sj t d ƒ ‚ n  t ƒ  } xT | j	 |  j
 k rÉ | j | j	 ƒ |  j
 j | j	 ƒ } | | k r½ Pn  | | _	 qv W| j d t ƒ } | j d ƒ } | j d ƒ } | j d ƒ } | j d ƒ }	 | j d ƒ }
 | j } |  j d | j	 ƒ } t j ƒ  } | j | |  } t j ƒ  | | _ t d	 | | |  } | j rÄx- | j D] } t |  j | j | j ƒ qžWn  t |  j | | j ƒ |  j | | d | d | d | d |	 d |
 ƒ} | r)g  | D] } | ^ qn g  } | r]| j d
 | ƒ | j ƒ  } | | _ n  | sm| j n  | S(   s   Send a given PreparedRequest.RK   RM   RN   RO   s#   You can only send PreparedRequests.RP   RL   R_   R/   i    (   R€   RK   RM   RN   RO   R&   R   t
   ValueErrort   setR_   Rc   t   addR0   t   popR8   RŠ   t   get_adapterR   t   utcnowRq   t   elapsedR   RU   R   Rm   R|   RX   Rz   t   insertRV   (   R<   R|   R¡   t   checked_urlsR>   RP   RK   RL   RM   RN   RO   RŠ   t   adaptert   startt   rRr   t   genRU   (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyRq   0  sV    			 	%
c   	      C   sò   |  j  rŠ t | ƒ p i  } x* | j ƒ  D] \ } } | j | | ƒ q( 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(   s6   Check the environment and merge it with some settings.t   REQUESTS_CA_BUNDLEt   CURL_CA_BUNDLERM   RO   RK   RN   N(   R}   R   R(   R€   R8   R"   t   ost   environR0   R.   RO   RK   RM   RN   (	   R<   R_   RO   RK   RM   RN   t   env_proxiesR$   R%   (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyR     s    	c         C   sM   x6 |  j  j ƒ  D]% \ } } | j ƒ  j | ƒ r | Sq Wt d | ƒ ‚ d S(   s>   Returns the appropriate connnection adapter for the given URL.s*   No connection adapters were found for '%s'N(   R   R(   t   lowerR^   R   (   R<   R_   t   prefixRµ   (    (    s5   /usr/lib/python2.7/site-packages/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   valuesR[   (   R<   R%   (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyR[   ¡  s    c         C   so   | |  j  | <g  |  j  D]$ } t | ƒ t | ƒ k  r | ^ q } x' | D] } |  j  j | ƒ |  j  | <qH Wd S(   sk   Registers a connection adapter to a prefix.

        Adapters are sorted in descending order by key length.N(   R   t   lenR¯   (   R<   R¿   Rµ   R$   t   keys_to_movet   key(    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyRŽ   ¦  s    4c            s6   t  ‡  f d †  ˆ  j Dƒ ƒ } t  ˆ  j ƒ | d <| S(   Nc         3   s'   |  ] } | t  ˆ  | d  ƒ f Vq d  S(   N(   t   getattrR"   (   R#   t   attr(   R<   (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pys	   <genexpr>²  s    Rc   (   R)   t	   __attrs__Rc   (   R<   t   state(    (   R<   s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   __getstate__±  s    c         C   s|   | j  d i  ƒ } x* | j ƒ  D] \ } } t |  | | ƒ q Wt t ƒ |  _ x' | j ƒ  D] \ } } | |  j | <q[ Wd  S(   NRc   (   R¯   R(   t   setattrR   R   Rc   (   R<   RÇ   Rc   RÅ   t   valuet   redirectt   to(    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   __setstate__¶  s    N(   R†   R‡   t   __doc__RÆ   R   R‘   R“   Rœ   R"   R8   R|   R0   R£   R¤   R¥   R§   R©   R«   Rq   R   R°   R[   RŽ   RÈ   RÍ   (    (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyRˆ      sH   		6			)E	
	
	


			O		
			c           C   s   t  ƒ  S(   s2   Returns a :class:`Session` for context-management.(   Rˆ   (    (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   sessionÀ  s    (7   RÎ   R»   t   collectionsR    R   R‰   R   t   compatR   R   R   R   Rm   R   R   R	   R
   t   modelsR   R   R   RŠ   R   R   t   utilsR   R   R   R   t
   exceptionsR   R   R   R   t   urllib3._collectionsR   t
   structuresR   R   R   R   R   R   R   R   t   status_codesR    R!   R   R.   R3   t   objectR4   Rˆ   RÏ   (    (    (    s5   /usr/lib/python2.7/site-packages/requests/sessions.pyt   <module>
   s.   """"(!Åÿ ¡