3 Pfq* @s dZddlmZddlZddlZddlZddlmZddlm Z m Z m Z m Z m Z mZddlmZddlmZdd lmZdd lmZdd lmZydd lmZWnek rdZYnXejeZGd dde ZGddde Z GdddeZ!ddZ"ddZ#ddZ$ddZ%ddZ&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)urljoin) HTTPError HTTPWarning MaxRetryError ProtocolError TimeoutErrorSSLError)BytesIO)RequestMethods) HTTPResponse)Timeout)Retry)urlfetchc@s eZdZdS)AppEnginePlatformWarningN)__name__ __module__ __qualname__rr/usr/lib/python3.6/appengine.pyrGsrc@s eZdZdS)AppEnginePlatformErrorN)rrrrrrrrKsrc@sXeZdZdZdddZddZdd 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 megabtyes. * If you use an unsupported request method such as OPTIONS. Beyond those cases, it will raise normal urllib3 errors. NTcCsNts tdtrtdtjdttj||||_||_ |pFt j |_ dS)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/reference/urllib3.contrib.html.) rris_prod_appengine_mvmswarningswarnrr __init__validate_certificateurlfetch_retriesrZDEFAULTretries)selfheadersrrrrrrrcs zAppEngineManager.__init__cCs|S)Nr)r rrr __enter__{szAppEngineManager.__enter__cCsdS)NFr)r exc_typeZexc_valZexc_tbrrr__exit__~szAppEngineManager.__exit__cKs|j||}yF|o |jdko |j} tj||||p2id|jo<| |j||jd} WnBtjk r} zt || WYdd} ~ Xntj k r} z$dt | krt d| t | WYdd} ~ Xntjk r} z(dt | krt||| dt | WYdd} ~ Xntjk r6} zt d| WYdd} ~ Xn`tjk rb} zt| WYdd} ~ Xn4tjk r} zt d || WYdd} ~ XnX|j| fd |i|} |o| j} | rr|jr|jrt||d n| jd krd }y|j||| |d}Wn*tk r.|jr*t||d | SX|j| tjd|| t|| }|j||||f|||d|St| jd}|j || j|r|j||| |d}tjd||j!| |j||f|||||d|S| S)NrF)Zpayloadmethodr!Zallow_truncatedfollow_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: %srztoo many redirectsi/ZGET)responseZ_poolzRedirecting %s -> %s)rredirecttimeoutz Retry-Afterz Retry: %s)bodyr!rr)r*)" _get_retriesr)totalrZfetchr_get_absolute_timeoutrZDeadlineExceededErrorr ZInvalidURLErrorstrrrZ DownloadErrorrZResponseTooLargeErrorZSSLCertificateErrorr ZInvalidMethodError#_urlfetch_response_to_http_responseZget_redirect_locationZraise_on_redirectstatusZ incrementZsleep_for_retrylogdebugrurlopenboolZ getheaderZis_retryZsleep)r r%Zurlr+r!rr)r* response_kwr&r(eZ http_responseZredirect_locationZ redirect_urlZhas_retry_afterrrrr4s           zAppEngineManager.urlopencKsztr"|jjd}|dkr"|jd=|jjd}|dkrZ|jd}|jddj||jd<tft|j|j|j d|S)Nzcontent-encodingZdeflateztransfer-encodingZchunked,)r+r!r1) is_prod_appenginer!getsplitremovejoinr r ZcontentZ status_code)r Z urlfetch_respr6Zcontent_encodingZtransfer_encodingZ encodingsrrrr0s    z4AppEngineManager._urlfetch_response_to_http_responsecCsB|tjkrdSt|tr>|jdk s,|jdk r8tjdt|jS|S)NzdURLFetch does not support granular timeout settings, reverting to total or default URLFetch timeout.) rDEFAULT_TIMEOUT isinstanceZ_readZ_connectrrrr-)r r*rrrr.s  z&AppEngineManager._get_absolute_timeoutcCs>t|tstj|||jd}|js.|js.|jr:tjdt |S)N)r)defaultzhURLFetch only supports total retries and does not recognize connect, read, or redirect retry parameters.) r?rZfrom_intrZconnectreadr)rrr)r rr)rrrr,s zAppEngineManager._get_retries)NNTT) rrr__doc__rr"r$rr>r4r0r.r,rrrrrOs Z rcCstptptS)N)is_local_appenginer9rrrrr is_appenginesrDcCsto t S)N)rDrrrrris_appengine_sandboxsrEcCsdtjkodtjdkS)NAPPENGINE_RUNTIMEz Development/SERVER_SOFTWARE)osenvironrrrrrCs rCcCs dtjkodtjdkot S)NrFzGoogle App Engine/rG)rHrIrrrrrr9!s r9cCstjjdddkS)NZGAE_VMFtrue)rHrIr:rrrrr'sr)'rBZ __future__rZloggingrHrZpackages.six.moves.urllib.parser exceptionsrrrrr r Z packages.sixr Zrequestr r(r Z util.timeoutrZ util.retryrZgoogle.appengine.apir ImportErrorZ getLoggerrr2rrrrDrErCr9rrrrr's2         D