B -<`+ã@sdZddlmZddlZddlZddlZddlmZmZm Z m Z m Z m Z ddl mZddlmZddlmZdd lmZdd lmZd d 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 `_. 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Ú ProtocolErrorÚSSLErrorÚ TimeoutError)Úurljoin)ÚRequestMethods)Ú HTTPResponse)ÚRetry)ÚTimeouté)Ú_appengine_environ)Úurlfetchc@s eZdZdS)ÚAppEnginePlatformWarningN)Ú__name__Ú __module__Ú __qualname__©rrú@/tmp/pip-target-7cdyy134/lib/python/urllib3/contrib/appengine.pyrGsrc@s eZdZdS)ÚAppEnginePlatformErrorN)rrrrrrrrKsrc@sXeZdZdZddd„Zdd„Zdd „Zddddejfd d „Z d d „Z dd„Z dd„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 `_. 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/latest/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_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Útimeoutz Retry-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ÚboolÚ getheaderÚ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"|j d¡}|dkr"|jd=|j d¡}|dkrZ| d¡}| d¡d |¡|jd<tft |j ¡|j |j|j dœ|—Ž}tft |j ¡|j|j |dœ|—ŽS)Nzcontent-encodingÚdeflateztransfer-encodingÚchunkedú,)r2Úmsgr"r;)r2r"r;Úoriginal_response) Úis_prod_appenginer"ÚgetÚ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 r8t dt¡|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:t dt ¡|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'rr[r@r8r6r3rrrrrOs j$ r)'rcÚ __future__rrUÚloggingrÚ exceptionsrrrrrr Zpackages.six.moves.urllib.parser Úrequestr r.r Z util.retryr Z util.timeoutrÚrZgoogle.appengine.apirÚ ImportErrorÚ getLoggerrr>rrrÚ is_appengineÚis_appengine_sandboxÚis_local_appenginerPÚis_prod_appengine_mvmsrrrrÚ's2          h