ó
qJ¿[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 m Z d d l m Z m Z m Z m Z d d	 l m Z m Z d d
 l m Z m Z d d l m Z m Z d d l 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& d d l' m( Z( e( j) e( j* e( j+ e( j, f Z- d Z. e	 d „ Z/ e	 d „ Z0 d e1 f d „  ƒ  YZ2 d e2 f d „  ƒ  YZ3 d „  Z4 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   urlparset   builtin_str(   t   cookiejar_from_dictt   extract_cookies_to_jart   RequestsCookieJart   merge_cookies(   t   Requestt   PreparedRequest(   t   default_hookst   dispatch_hook(   t   to_key_val_listt   default_headers(   t   TooManyRedirectst   InvalidSchema(   t   CaseInsensitiveDict(   t   HTTPAdapter(   t   requote_urit   get_environ_proxiest   get_netrc_autht   should_bypass_proxiest   get_auth_from_url(   t   codesi   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 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   v(    (    s5   /usr/lib/python2.7/dist-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/dist-packages/requests/sessions.pyt   merge_hooksI   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 p | j | j k 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   False(   t   selft   old_urlt   new_urlt
   old_parsedt
   new_parsed(    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyt   should_strip_authZ   s    c         c   s´  d } x§d | j  k r¯| j t k r¯| j ƒ  }	 | j | |  j k r_ t d |  j ƒ ‚ n  | j ƒ  | j  d }
 | j } |
 j	 d ƒ r³ t
 | j ƒ } d | j |
 f }
 n  t
 |
 ƒ } | j ƒ  }
 t
 |
 ƒ j sõ t | j t |
 ƒ ƒ }
 n t |
 ƒ }
 |
 |	 _ | j t j k r1| d k r1d } n  | j t j k rX| d k rXd } n  | j t j k r| d k rd } n  | |	 _ | j t j t j f k rËd	 |	 j  k r¿|	 j  d	 =n  d |	 _ n  |	 j  } y | d
 =Wn t k
 rò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   locations   Exceeded %s redirects.s   //s   %s:%st   HEADt   GETt   POSTs   Content-Lengtht   Cookiet   streamt   timeoutt   verifyt   certt   proxiest   allow_redirectsi   N($   t   headerst   status_codet   REDIRECT_STATIt   copyt   contentt   max_redirectsR   t   closet   methodt
   startswithR   t   urlR1   t   geturlt   netlocR   R   R   t	   see_othert   foundt   movedt	   temporaryt   resumeR   t   bodyt   KeyErrorR	   t   _cookiest   rawR   t   cookiest   prepare_cookiest   rebuild_proxiest   rebuild_autht   sendR3   (   R4   t   respt   reqR?   R@   RA   RB   RC   t   it   prepared_requestRN   RL   t   parsed_rurlt   parsedRE   (    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyt   resolve_redirectsj   sh    !
								
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(   RE   RN   R9   t   requestt	   trust_envR   R   t   prepare_auth(   R4   Rb   R'   RE   RN   t   new_auth(    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyR]   Ê   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(   RE   RN   R   R1   R   RH   Rh   R   R   R(   t
   setdefaultR   RW   R   (   R4   Rb   RC   RE   RN   R1   t   new_proxiest   environ_proxiest   proxyt   usernamet   password(    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyR\   ß   s$    		
N(	   t   __name__t
   __module__R9   R3   R   R0   Re   R]   R\   (    (    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyR,   Y   s
   	_	t   Sessionc           B   s  e  Z d  Z d 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 „ Z
 d „  Z d „  Z d „  Z d! d „ Z d! d „ Z d! 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
    RE   RZ   t   authR@   RC   t   hookst   paramsRA   RB   t   prefetcht   adaptersR?   Rh   RJ   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   RE   R   Rt   RC   R   Ru   Rv   R3   R?   R0   RA   RB   t   DEFAULT_REDIRECT_LIMITRJ   Rh   R   RZ   R   Rx   t   mountR   (   R4   (    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyt   __init__  s    								c         C   s   |  S(   N(    (   R4   (    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyt	   __enter__J  s    c         G   s   |  j  ƒ  d  S(   N(   RK   (   R4   t   args(    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyt   __exit__M  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 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.
        RL   RN   t   filest   dataRE   R"   Rv   Rt   RZ   Ru   (   RZ   R   R   t	   CookieJarR   R   R
   Rt   Rh   R   RN   R   t   prepareRL   t   upperR   R€   R&   RE   R   Rv   R+   Ru   (   R4   Rg   RZ   t   merged_cookiesRt   t   p(    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyt   prepare_requestP  s(    							c         C   s¡  t  | ƒ } t d | j ƒ  d | d | d | d | p9 i  d | pE i  d | d | d	 | ƒ 	} |  j | ƒ } | pu i  } |  j rt | ƒ p i  } x* | j ƒ  D] \ } } | j | | ƒ q  W| rè | t k	 rè t	 j
 j d
 ƒ } n  | r| t k	 rt	 j
 j d ƒ } qn  t | |  j ƒ } t | |  j ƒ } t | |  j ƒ } t | |  j ƒ } i | d 6|	 d 6| d 6| d 6| d 6|
 d 6} |  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 or bytes 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) Float describing the timeout of the
            request.
        :param allow_redirects: (optional) Boolean. Set to True by default.
        :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.
        RL   RN   RE   R   R€   Rv   Rt   RZ   Ru   t   REQUESTS_CA_BUNDLEt   CURL_CA_BUNDLER?   R@   RA   RB   RC   RD   (   R   R   Rƒ   R†   Rh   R   R   Rk   R3   t   ost   environR(   R&   RC   R?   RA   RB   R^   (   R4   RL   RN   Rv   R€   RE   RZ   R   Rt   R@   RD   RC   Ru   R?   RA   RB   R`   t   prept   env_proxiesR$   R%   t   send_kwargsR_   (    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyRg   w  sD    ,		
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.
        RD   R<   (   Rk   R0   Rg   (   R4   RN   t   kwargs(    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyR(   ×  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.
        RD   t   OPTIONS(   Rk   R0   Rg   (   R4   RN   RŽ   (    (    s5   /usr/lib/python2.7/dist-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.
        RD   R;   (   Rk   R3   Rg   (   R4   RN   RŽ   (    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyt   headë  s    c         K   s   |  j  d | d | | S(   s8  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 \*\*kwargs: Optional arguments that ``request`` takes.
        R=   R€   (   Rg   (   R4   RN   R€   RŽ   (    (    s5   /usr/lib/python2.7/dist-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€   (   Rg   (   R4   RN   R€   RŽ   (    (    s5   /usr/lib/python2.7/dist-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€   (   Rg   (   R4   RN   R€   RŽ   (    (    s5   /usr/lib/python2.7/dist-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(   Rg   (   R4   RN   RŽ   (    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyt   delete  s    c         K   s  | j  d |  j ƒ | j  d |  j ƒ | j  d |  j ƒ | j  d |  j ƒ t | t ƒ sj t d ƒ ‚ n  | 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 rd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 ƒ  } t | ƒ | _ n  | S(   s   Send a given PreparedRequest.R?   RA   RB   RC   s#   You can only send PreparedRequests.RD   R@   RN   R'   i    (   Rk   R?   RA   RB   RC   R   R   t
   ValueErrort   popR0   R(   Ru   t   get_adapterRN   R   t   utcnowR^   t   elapsedR   t   historyR	   RZ   Rg   RY   Re   t   insertt   tuple(   R4   Rg   RŽ   RD   R?   R@   RA   RB   RC   Ru   t   adaptert   startt   rR_   t   genRž   (    (    s5   /usr/lib/python2.7/dist-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(   Rx   R   t   lowerRM   R   (   R4   RN   t   prefixR¡   (    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyR›   Z  s    c         C   s(   x! |  j  j ƒ  D] } | j ƒ  q Wd S(   s+   Closes all adapters and as such the sessionN(   Rx   t   valuesRK   (   R4   R%   (    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyRK   d  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(   Rx   t   lenRš   (   R4   R¦   R¡   R$   t   keys_to_movet   key(    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyRz   i  s    4c            s   t  ‡  f d †  ˆ  j Dƒ ƒ S(   Nc         3   s'   |  ] } | t  ˆ  | d  ƒ f Vq d  S(   N(   t   getattrR   (   t   .0t   attr(   R4   (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pys	   <genexpr>s  s    (   t   dictt	   __attrs__(   R4   (    (   R4   s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyt   __getstate__r  s    c         C   s1   x* | j  ƒ  D] \ } } t |  | | ƒ q Wd  S(   N(   R   t   setattr(   R4   t   stateR­   t   value(    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyt   __setstate__u  s    N(   Rq   Rr   t   __doc__R¯   R{   R|   R~   R†   R   R0   Rg   R(   R   R‘   R’   R”   R–   R˜   R^   R›   RK   Rz   R°   R´   (    (    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyRs     sD   	3			(S	
	
	



			>	
				c           C   s   t  ƒ  S(   s2   Returns a :class:`Session` for context-management.(   Rs   (    (    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyt   sessionz  s    (5   Rµ   R‰   t   collectionsR    R   Rt   R   t   compatR   R   R   R   R   RZ   R   R	   R
   R   t   modelsR   R   Ru   R   R   t   utilsR   R   t
   exceptionsR   R   t
   structuresR   Rx   R   R   R   R   R   R   t   status_codesR   RS   RR   t   othert   temporary_movedRG   Ry   R&   R+   t   objectR,   Rs   R¶   (    (    (    s5   /usr/lib/python2.7/dist-packages/requests/sessions.pyt   <module>
   s2   ("(¬ÿ v