o d+@sdZddlmZddlZddlZddlZddlmZmZm Z m Z m Z m Z ddl mZddlmZddlmZdd lmZdd lmZd d lmZzdd lmZWn ey[dZYnweeZGdddeZGdddeZ GdddeZ!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 `_. 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 `_ 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 `_ 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 `_, you can use the standard :class:`PoolManager` without any configuration or special environment variables. )absolute_importN) HTTPError HTTPWarning MaxRetryError ProtocolErrorSSLError TimeoutError)urljoin)RequestMethods) HTTPResponse)Retry)Timeout)_appengine_environ)urlfetchc@ eZdZdS)AppEnginePlatformWarningN__name__ __module__ __qualname__rrt/private/var/folders/v1/_jykv66s6qd26_69j1njbrl80000gr/T/pip-target-p1gutpg6/lib/python/urllib3/contrib/appengine.pyrGrc@r)AppEnginePlatformErrorNrrrrrrKrrc@s`eZdZdZ    dddZddZdd Zddddejfd d Z d d Z ddZ ddZ 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 `_. 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@tstdtdtt||||_||_|pt j |_ dS)Nz.URLFetch is not available 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/1.26.x/reference/urllib3.contrib.html.) rrwarningswarnrr __init__validate_certificateurlfetch_retriesr DEFAULTretries)selfheadersr#r r!rrrrcs zAppEngineManager.__init__cCs|S)Nr)r$rrr __enter__|szAppEngineManager.__enter__cCsdS)NFr)r$exc_typeexc_valexc_tbrrr__exit__szAppEngineManager.__exit__c Ks|||}z"|o|jdko|j} tj||||pid|jo| |||jd} Wn~tjy:} zt || d} ~ wtj yU} zdt | vrMt d| t | d} ~ wtjyr} zdt | vrjt||| dt | d} ~ wtjy} zt d| d} ~ wtjy} zt| d} ~ wtjy} zt d || d} ~ ww|j| fd |i|} |o| } | r|jr|jrt||d | jd krd }z |j||| |d}Wnty|jrt||d | YSw|| td|| t|| }|j||||f|||d|St| j d}|!|| j|rD|j||| |d}td||"| |j||f|||||d|S| S)NrF)payloadmethodr%Zallow_truncatedfollow_redirectsdeadliner z 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#redirecttimeoutz Retry-Afterz Retry: %s)bodyr%r#r3r4)# _get_retriesr3totalrfetchr!_get_absolute_timeoutr ZDeadlineExceededErrorr ZInvalidURLErrorstrrrZ DownloadErrorrZResponseTooLargeErrorZSSLCertificateErrorrZInvalidMethodError#_urlfetch_response_to_http_responseget_redirect_locationraise_on_redirectstatus incrementsleep_for_retrylogdebugr urlopenboolr%getis_retrysleep)r$r,urlr5r%r#r3r4 response_kwr-r1e http_responseredirect_locationZ redirect_urlhas_retry_afterrrrrCs                 zAppEngineManager.urlopencKstr|jd}|dkr|jd=|jd}|dkr-|d}|dd||jd<tdt|j |j |j|j d|}tdt|j |j|j |d|S) Nzcontent-encodingdeflateztransfer-encodingchunked,)r5msgr%r>)r5r%r>original_responser) is_prod_appenginer%rEsplitremovejoinr ioBytesIOcontentZ header_msg status_code)r$Z urlfetch_resprIcontent_encodingZtransfer_encoding encodingsrRrrrr;s2      z4AppEngineManager._urlfetch_response_to_http_responsecCsB|tjurdSt|tr|jdus|jdurtdt|jS|S)NzdURLFetch does not support granular timeout settings, reverting to total or default URLFetch timeout.) rDEFAULT_TIMEOUT isinstance_read_connectrrrr7)r$r4rrrr9s  z&AppEngineManager._get_absolute_timeoutcCs>t|tstj|||jd}|js|js|jrtdt |S)N)r3defaultzhURLFetch only supports total retries and does not recognize connect, read, or redirect retry parameters.) r^r from_intr#connectreadr3rrr)r$r#r3rrrr6&s zAppEngineManager._get_retries)NNTT) rrr__doc__rr&r*rr]rCr;r9r6rrrrrOs$  r$ r)'re __future__rrWloggingr exceptionsrrrrrr Zpackages.six.moves.urllib.parser requestr r1r Z util.retryr Z util.timeoutrrZgoogle.appengine.apir ImportError getLoggerrrArrr is_appengineis_appengine_sandboxis_local_appenginerSis_prod_appengine_mvmsrrrrs6 (         h