Your IP : 18.118.19.89


Current Path : /opt/alt/python37/lib/python3.7/site-packages/urllib3/contrib/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/urllib3/contrib/__pycache__/appengine.cpython-37.pyc

B

��`+�@sdZddlmZddlZddlZddlZddlmZmZm	Z	m
Z
mZmZddl
mZddlmZddlmZdd	lmZdd
lmZddlmZydd
lmZWnek
r�dZYnXe�e�ZGdd�de�ZGdd�de�Z Gdd�de�Z!ej"Z"ej#Z#ej$Z$ej%Z%ej&Z&dS)aC
This module provides a pool manager that uses Google App Engine's
`URLFetch Service <https://cloud.google.com/appengine/docs/python/urlfetch>`_.

Example usage::

    from urllib3 import PoolManager
    from urllib3.contrib.appengine import AppEngineManager, is_appengine_sandbox

    if is_appengine_sandbox():
        # AppEngineManager uses AppEngine's URLFetch API behind the scenes
        http = AppEngineManager()
    else:
        # PoolManager uses a socket-level API behind the scenes
        http = PoolManager()

    r = http.request('GET', 'https://google.com/')

There are `limitations <https://cloud.google.com/appengine/docs/python/urlfetch/#Python_Quotas_and_limits>`_ to the URLFetch service and it may not be
the best choice for your application. There are three options for using
urllib3 on Google App Engine:

1. You can use :class:`AppEngineManager` with URLFetch. URLFetch is
   cost-effective in many circumstances as long as your usage is within the
   limitations.
2. You can use a normal :class:`~urllib3.PoolManager` by enabling sockets.
   Sockets also have `limitations and restrictions
   <https://cloud.google.com/appengine/docs/python/sockets/   #limitations-and-restrictions>`_ and have a lower free quota than URLFetch.
   To use sockets, be sure to specify the following in your ``app.yaml``::

        env_variables:
            GAE_USE_SOCKETS_HTTPLIB : 'true'

3. If you are using `App Engine Flexible
<https://cloud.google.com/appengine/docs/flexible/>`_, you can use the standard
:class:`PoolManager` without any configuration or special environment variables.
�)�absolute_importN�)�	HTTPError�HTTPWarning�
MaxRetryError�
ProtocolError�SSLError�TimeoutError)�urljoin)�RequestMethods)�HTTPResponse)�Retry)�Timeout�)�_appengine_environ)�urlfetchc@seZdZdS)�AppEnginePlatformWarningN)�__name__�
__module__�__qualname__�rr�J/opt/alt/python37/lib/python3.7/site-packages/urllib3/contrib/appengine.pyrGsrc@seZdZdS)�AppEnginePlatformErrorN)rrrrrrrrKsrc@sXeZdZdZddd�Zdd�Zdd	�Zddddejfd
d�Z	dd
�Z
dd�Zdd�ZdS)�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 :class:`AppEnginePlatformError` if:
        * URLFetch is not available.
        * If you attempt to use this on App Engine Flexible, as full socket
          support is available.
        * If a request size is more than 10 megabytes.
        * If a response size is more than 32 megabytes.
        * If you use an unsupported request method such as OPTIONS.

    Beyond those cases, it will raise normal urllib3 errors.
    NTcCs@tstd��t�dt�t�||�||_||_|p8t	j
|_dS)Nz.URLFetch is not available in this environment.z�urllib3 is using URLFetch on Google App Engine sandbox instead of sockets. To use sockets directly instead of URLFetch see https://urllib3.readthedocs.io/en/1.26.x/reference/urllib3.contrib.html.)rr�warnings�warnrr�__init__�validate_certificate�urlfetch_retriesr
�DEFAULT�retries)�self�headersr rrrrrrcszAppEngineManager.__init__cCs|S)Nr)r!rrr�	__enter__|szAppEngineManager.__enter__cCsdS)NFr)r!�exc_type�exc_val�exc_tbrrr�__exit__szAppEngineManager.__exit__c
Ks�|�||�}yF|o |jdko |j}	tj||||p2id|jo<|	|�|�|jd�}
W�nBtjk
r�}zt	||��Wdd}~XY�ntj
k
r�}z"dt|�kr�td|��t
|��Wdd}~XYn�tjk
�r}z&dt|�kr�t|||d��t
|��Wdd}~XYn�tjk
�r6}ztd|��Wdd}~XYn`tjk
�rb}zt|��Wdd}~XYn4tjk
�r�}ztd	||��Wdd}~XYnX|j|
fd
|i|��}|�o�|��}
|
�rr|j�r�|j�r�t||d��n�|jdk�r�d
}y|j||||d�}Wn*tk
�r.|j�r*t||d��|SX|�|�t�d||
�t||
�}|j||||f|||d�|��St|�d��}|� ||j|��r�|j||||d�}t�d|�|�!|�|j||f|||||d�|��S|S)NrF)�payload�methodr"Zallow_truncated�follow_redirectsZdeadlinerz	too largezOURLFetch request too large, URLFetch only supports requests up to 10mb in size.zToo many redirects)�reasonzPURLFetch response too large, URLFetch only supportsresponses up to 32mb in size.z$URLFetch does not support method: %sr ztoo many redirectsi/ZGET)�responseZ_poolzRedirecting %s -> %s)r �redirect�timeoutzRetry-Afterz	Retry: %s)�bodyr"r r-r.)"�_get_retriesr-�totalrZfetchr�_get_absolute_timeoutrZDeadlineExceededErrorr	ZInvalidURLError�strrrZ
DownloadErrorrZResponseTooLargeErrorZSSLCertificateErrorrZInvalidMethodError�#_urlfetch_response_to_http_responseZget_redirect_locationZraise_on_redirect�statusZ	incrementZsleep_for_retry�log�debugr
�urlopen�bool�	getheaderZis_retry�sleep)r!r)�urlr/r"r r-r.�response_kwr*r,�eZ
http_responseZredirect_locationZredirect_urlZhas_retry_afterrrrr8�s�


zAppEngineManager.urlopencKs�t�r"|j�d�}|dkr"|jd=|j�d�}|dkrZ|�d�}|�d�d�|�|jd<tft�|j	�|j
|j|jd�|��}tft�|j	�|j|j|d�|��S)Nzcontent-encoding�deflateztransfer-encoding�chunked�,)r/�msgr"r5)r/r"r5�original_response)�is_prod_appenginer"�get�split�remove�joinr�io�BytesIO�contentZ
header_msgZstatus_code)r!Z
urlfetch_respr=Zcontent_encodingZtransfer_encoding�	encodingsrCrrrr4�s*



z4AppEngineManager._urlfetch_response_to_http_responsecCsB|tjkrdSt|t�r>|jdk	s,|jdk	r8t�dt�|jS|S)NzdURLFetch does not support granular timeout settings, reverting to total or default URLFetch timeout.)	r�DEFAULT_TIMEOUT�
isinstance�_readZ_connectrrrr1)r!r.rrrr2s

z&AppEngineManager._get_absolute_timeoutcCs>t|t�stj|||jd�}|js.|js.|jr:t�dt	�|S)N)r-�defaultzhURLFetch only supports total retries and does not recognize connect, read, or redirect retry parameters.)
rNr
Zfrom_intr �connect�readr-rrr)r!r r-rrrr0&s
zAppEngineManager._get_retries)NNTT)
rrr�__doc__rr#r'rrMr8r4r2r0rrrrrOs
j$
r)'rS�
__future__rrI�loggingr�
exceptionsrrrrrr	Zpackages.six.moves.urllib.parser
Zrequestrr,rZ
util.retryr
Zutil.timeoutr�rZgoogle.appengine.apir�ImportError�	getLoggerrr6rrrZis_appengineZis_appengine_sandboxZis_local_appenginerDZis_prod_appengine_mvmsrrrr�<module>'s2 

h

?>