
) X                 @   sr  d  d l  m Z d  d l Z d  d l Z d  d l Z d d l m Z m Z 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 d d	 l m Z y d  d
 l m Z Wn e k
 r d Z Yn Xe j e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z d d   Z d d   Z  d d   Z! d d   Z" d d   Z# d S)    )absolute_importN   )	HTTPErrorHTTPWarningMaxRetryErrorProtocolErrorTimeoutErrorSSLError)BytesIO)RequestMethods)HTTPResponse)Timeout)Retry)urlfetchc               @   s   e  Z d  Z d S)AppEnginePlatformWarningN)__name__
__module____qualname__ r   r   [/home/ubuntu/projects/ifolica/build/requests/requests/packages/urllib3/contrib/appengine.pyr      s   r   c               @   s   e  Z d  Z d S)AppEnginePlatformErrorN)r   r   r   r   r   r   r   r   "   s   r   c               @   s   e  Z d  Z d Z d d d d d  Z d d   Z d d	   Z d d d d e j d
 d  Z	 d d   Z
 d d   Z d d   Z d S)AppEngineManagera  
    Connection manager for Google App Engine sandbox applications.

    This manager uses the URLFetch service directly instead of using the
    emulated httplib, and is subject to URLFetch limitations as described in
    the App Engine documentation here:

        https://cloud.google.com/appengine/docs/python/urlfetch

    Notably it will raise an AppEnginePlatformError if:
        * URLFetch is not available.
        * If you attempt to use this on GAEv2 (Managed VMs), as full socket
          support is available.
        * If a request size is more than 10 megabytes.
        * If a response size is more than 32 megabtyes.
        * If you use an unsupported request method such as OPTIONS.

    Beyond those cases, it will raise normal urllib3 errors.
    NTc             C   sl   t  s t d   n  t   r- t d   n  t j d t  t j |  |  | |  _ | pb t	 j
 |  _ d  S)Nz.URLFetch is not available in this environment.zUse normal urllib3.PoolManager instead of AppEngineManageron Managed VMs, as using URLFetch is not necessary in this environment.zurllib3 is using URLFetch on Google App Engine sandbox instead of sockets. To use sockets directly instead of URLFetch see https://urllib3.readthedocs.io/en/latest/contrib.html.)r   r   is_prod_appengine_mvmswarningswarnr   r   __init__validate_certificater   DEFAULTretries)selfheadersr   r   r   r   r   r   ;   s    		zAppEngineManager.__init__c             C   s   |  S)Nr   )r   r   r   r   	__enter__Q   s    zAppEngineManager.__enter__c             C   s   d S)NFr   )r   exc_typeZexc_valexc_tbr   r   r   __exit__T   s    zAppEngineManager.__exit__c             K   s  |  j  | |  } yg t j | d | d | d | p6 i  d d d | oZ | j d k oZ | j d |  j |  d	 |  j }	 Wnt j k
 r }
 z t |  |
   WYd  d  }
 ~
 XnXt j	 k
 r	}
 z5 d
 t
 |
  k r t d |
   n  t |
   WYd  d  }
 ~
 Xnt j k
 rf}
 z; d t
 |
  k rHt |  | d |
  n  t |
   WYd  d  }
 ~
 Xn t j k
 r}
 z t d |
   WYd  d  }
 ~
 Xnn t j k
 r}
 z t |
   WYd  d  }
 ~
 Xn; t j k
 r	}
 z t d | |
   WYd  d  }
 ~
 Xn X|  j |	 |  } | j   rL| j rL| rLt |  | d   n  | j | d | j r| j | | d | d |  } t j d |  | j   |  j | | d | d | d | d | d | | S| S)Npayloadmethodr    Zallow_truncatedFZfollow_redirectsr   Zdeadliner   z	too largezOURLFetch request too large, URLFetch only supports requests up to 10mb in size.zToo many redirectsreasonzPURLFetch response too large, URLFetch only supportsresponses up to 32mb in size.z$URLFetch does not support method: %sztoo many redirectsstatus_coderesponse_poolzForced retry: %sbodyr   redirecttimeout)_get_retriesr   fetchr,   total_get_absolute_timeoutr   ZDeadlineExceededErrorr   ZInvalidURLErrorstrr   r   ZDownloadErrorr   ZResponseTooLargeErrorZSSLCertificateErrorr	   ZInvalidMethodError#_urlfetch_response_to_http_responseget_redirect_locationZraise_on_redirectZis_forced_retrystatus	incrementloginfosleepurlopen)r   r&   urlr+   r    r   r,   r-   response_kwr)   ehttp_responser   r   r   r:   X   sd    	!#
	
zAppEngineManager.urlopenc             K   s   t    r7 | j j d  } | d k r7 | j d =q7 n  | j j d  } | d k r | j d  } | j d  d j |  | j d <n  t d t | j  d | j d | j	 |  S)	Nzcontent-encodingdeflateztransfer-encodingchunked,r+   r    r5   )
is_prod_appenginer    getsplitremovejoinr   r
   contentr(   )r   Zurlfetch_respr<   content_encodingZtransfer_encoding	encodingsr   r   r   r3      s    		z4AppEngineManager._urlfetch_response_to_http_responsec             C   sR   | t  j k r d St | t   rN | j | j k	 rG t j d t  n  | j S| S)N   zPURLFetch does not support granular timeout settings, reverting to total timeout.)	r   DEFAULT_TIMEOUT
isinstance_read_connectr   r   r   r0   )r   r-   r   r   r   r1      s    
z&AppEngineManager._get_absolute_timeoutc             C   sb   t  | t  s0 t j | d | d |  j } n  | j sK | j sK | j r^ t j d t	  n  | S)Nr,   defaultzhURLFetch only supports total retries and does not recognize connect, read, or redirect retry parameters.)
rL   r   Zfrom_intr   connectreadr,   r   r   r   )r   r   r,   r   r   r   r.      s    
zAppEngineManager._get_retries)r   r   r   __doc__r   r!   r$   r   rK   r:   r3   r1   r.   r   r   r   r   r   &   s   Dr   c               C   s   t    p t   p t   S)N)is_local_appenginerB   r   r   r   r   r   is_appengine   s    		rT   c               C   s   t    o t   S)N)rT   r   r   r   r   r   is_appengine_sandbox   s    rU   c               C   s    d t  j k o d t  j d k S)NAPPENGINE_RUNTIMEzDevelopment/SERVER_SOFTWARE)osenvironr   r   r   r   rS      s    rS   c               C   s*   d t  j k o) d t  j d k o) t   S)NrV   zGoogle App Engine/rW   )rX   rY   r   r   r   r   r   rB      s    rB   c               C   s   t  j j d d  d k S)NZGAE_VMFtrue)rX   rY   rC   r   r   r   r   r      s    r   )$
__future__r   loggingrX   r   
exceptionsr   r   r   r   r   r	   Zpackages.sixr
   requestr   r)   r   Zutil.timeoutr   Z
util.retryr   Zgoogle.appengine.apir   ImportError	getLoggerr   r7   r   r   r   rT   rU   rS   rB   r   r   r   r   r   <module>   s,   .	