3

Z'íc+ã@sdZddlmZddlZddlZddlZddlmZmZm	Z	m
Z
mZmZddl
mZddlmZddlmZdd	lmZdd
lmZddlmZydd
lmZWnek
r¸dZYnXejeƒ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ú</tmp/pip-build-gk9425m9/urllib3/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ƒ‚tjdtƒtj||ƒ||_||_|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__cKsà|j||ƒ}yF|o |jdko |j}	tj||||p2id|jo<|	|j|ƒ|jd}
WnBtjk
r‚}zt	||ƒ‚WYdd}~Xntj
k
rÂ}z$dt|ƒkrªtd|ƒ‚t
|ƒ‚WYdd}~XnÔtjk
r}z(dt|ƒkrðt|||d‚t
|ƒ‚WYdd}~XnŽtjk
r6}ztd|ƒ‚WYdd}~Xn`tjk
rb}zt|ƒ‚WYdd}~Xn4tjk
r”}ztd	||ƒ‚WYdd}~XnX|j|
fd
|i|—Ž}|o¸|jƒ}
|
rr|jrÞ|jrÞt||dƒ‚n”|jdkrîd
}y|j||||d}Wn*tk
r.|jr*t||dƒ‚|SX|j|ƒtjd||
ƒt||
ƒ}|j||||f|||dœ|—ŽSt|jj dƒƒ}|j!||j|ƒrÜ|j||||d}tjd|ƒ|j"|ƒ|j||f|||||dœ|—ŽS|S)NrF)ÚpayloadÚmethodr"Zallow_truncatedÚfollow_redirectsÚdeadlinerz	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/ÚGET)ÚresponseÚ_poolzRedirecting %s -> %s)r ÚredirectÚtimeoutzRetry-Afterz	Retry: %s)Úbodyr"r r0r1)#Ú_get_retriesr0ÚtotalrÚfetchrÚ_get_absolute_timeoutrZDeadlineExceededErrorr	ZInvalidURLErrorÚstrrrZ
DownloadErrorrZResponseTooLargeErrorZSSLCertificateErrorrZInvalidMethodErrorÚ#_urlfetch_response_to_http_responseÚget_redirect_locationÚraise_on_redirectÚstatusÚ	incrementÚsleep_for_retryÚlogÚdebugr
ÚurlopenÚboolr"ÚgetÚis_retryÚsleep)r!r)Úurlr2r"r r0r1Úresponse_kwr*r.ÚeÚ
http_responseÚredirect_locationZredirect_urlÚhas_retry_afterrrrr@ƒs–


zAppEngineManager.urlopencKs¤tƒr"|jjdƒ}|dkr"|jd=|jjdƒ}|dkrZ|jdƒ}|jdƒdj|ƒ|jd<tftj|j	ƒ|j
|j|jdœ|—Ž}tftj|j	ƒ|j|j|dœ|—ŽS)Nzcontent-encodingÚdeflateztransfer-encodingÚchunkedú,)r2Úmsgr"r;)r2r"r;Úoriginal_response)Úis_prod_appenginer"rBÚsplitÚremoveÚjoinrÚioÚBytesIOÚcontentZ
header_msgÚstatus_code)r!Z
urlfetch_resprFÚcontent_encodingZtransfer_encodingÚ	encodingsrOrrrr8õs*



z4AppEngineManager._urlfetch_response_to_http_responsecCsB|tjkrdSt|tƒr>|jdk	s,|jdk	r8tjdtƒ|jS|S)NzdURLFetch does not support granular timeout settings, reverting to total or default URLFetch timeout.)	rÚDEFAULT_TIMEOUTÚ
isinstanceÚ_readÚ_connectrrrr4)r!r1rrrr6s

z&AppEngineManager._get_absolute_timeoutcCs>t|tƒstj|||jd}|js.|js.|jr:tjdt	ƒ|S)N)r0ÚdefaultzhURLFetch only supports total retries and does not recognize connect, read, or redirect retry parameters.)
r[r
Úfrom_intr ÚconnectÚreadr0rrr)r!r r0rrrr3&s
zAppEngineManager._get_retries)NNTT)
rrrÚ__doc__rr#r'rrZr@r8r6r3rrrrrOs
j$
r)'rbÚ
__future__rrTÚloggingrÚ
exceptionsrrrrrr	Zpackages.six.moves.urllib.parser
Úrequestrr.rZ
util.retryr
Zutil.timeoutrÚrZgoogle.appengine.apirÚImportErrorÚ	getLoggerrr>rrrZis_appengineZis_appengine_sandboxZis_local_appenginerPZis_prod_appengine_mvmsrrrrÚ<module>'s2 

h