�
��fg�*��z�dZddlmZddlZddlZddlZddlmZmZm Z m
Z
mZmZddl
mZddlmZddlmZdd lmZdd
lmZddlmZ dd
lmZn
#e$rdZYnwxYweje��ZGd�de��ZGd�de��Z Gd�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��eZdZdS)�AppEnginePlatformWarningN��__name__�
__module__�__qualname__���;/usr/lib/python3/dist-packages/urllib3/contrib/appengine.pyrrG��������Drrc��eZdZdS)�AppEnginePlatformErrorNrrrrrrKrrrc�\�eZdZdZ dd�Zd�Zd�Zddddejfd�Z d�Z
d �Zd
�ZdS)�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.
NTc���tstd���tjdt��tj||��||_||_|ptj
|_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(r%r&s rr$zAppEngineManager.__init__csy��� �(�@���
� �
�
W�
%�
�
�
� ���g�.�.�.�$8��!� 0����/�%�-����rc��|S)Nr)r)s r� __enter__zAppEngineManager.__enter__|s���rc��dS)NFr)r)�exc_type�exc_val�exc_tbs r�__exit__zAppEngineManager.__exit__s���urc��|�||��} |o|jdko|j} tj||||pid|jo| |�|��|j���}
�n#tj$r}t||���d}~wtj
$r5}dt|��vrtd|���t|���d}~wtj$r7}dt|��vrt|||����t|���d}~wtj$r}td|���d}~wtj$r}t%|���d}~wtj$r}td |z|���d}~wwxYw|j|
fd
|i|��}|o|���}
|
r�|jr|jrt||d���|jdkrd
} |�||||���}n*#t$r|jrt||d���|cYSwxYw|�|��t4�d||
��t9||
��}|j||||f|||d�|��St=|�d����}|� ||j|��r^|�||||���}t4�d|��|�!|��|j||f|||||d�|��S|S)NrF)�payload�methodr*�allow_truncated�follow_redirects�deadliner%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(�redirect�timeoutzRetry-Afterz Retry: %s)�bodyr*r(r<r=)"�_get_retriesr<�totalr�fetchr&�_get_absolute_timeoutr%�DeadlineExceededErrorr
�InvalidURLError�strrr�
DownloadErrorr�ResponseTooLargeError�SSLCertificateErrorr �InvalidMethodError�#_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)r4�urlr>r*r(r<r=�response_kwr6r:�e�
http_response�redirect_location�redirect_url�has_retry_afters rrRzAppEngineManager.urlopen�s,���#�#�G�X�6�6��* �'�S�G�,<��,A�S�g�m���~�����
�2� %�!%�!6�!K�;K��3�3�G�<�<�%)�%>� � � �H�H���-� (� (� (��t�Q�'�'�'������'� #� #� #��c�!�f�f�$�$�,�<�����
��"�"�"������%� #� #� #�#�s�1�v�v�-�-�#�D�#�a�8�8�8�8���"�"�"������-� � � �(�0����
������+� � � ��1�+�+�������*� � � �(�6��?����
����� ����
A��@��
�
�%�
�)4�
�
�
�
%�N��)L�)L�)N�)N��� ��$�
��)B�
�#�D�#�/C�D�D�D� �'�3�.�.�"�F�)�%�/�/���m�4�0���G�G��%�)�)�)��0�M�+�D�#�7K�L�L�L�(�(�(�(�)����
�'�'�
�6�6�6�� � �0�#�7H�I�I�I�&�s�,=�>�>��#�t�|�� ��� �
$�%�#� � �"� � � ��}�6�6�}�E�E�F�F�����F�M�$8�/�J�J�
��'�'���m�SW�'�X�X�G��I�I�k�3�'�'�'��M�M�-�(�(�(��4�<��� ����!�� � �� � �
��sf�AA+�+E=�:B
�
E=�0C�E=�2D�E=�"D2�2E=�E�E=�%E8�8E=�G/�/$H�Hc��t��r(|j�d��}|dkr|jd=|j�d��}|dkrG|�d��}|�d��d�|��|jd<t
dtj|j ��|j
|j|jd�|��}t
dtj|j ��|j|j|d�|��S) Nzcontent-encoding�deflateztransfer-encoding�chunked�,)r>�msgr*rM)r>r*rM�original_responser)�is_prod_appenginer*�get�split�remove�joinr
�io�BytesIO�content�
header_msg�status_code)r)�
urlfetch_resprX�content_encoding�transfer_encoding� encodingsrcs rrJz4AppEngineManager._urlfetch_response_to_http_response�s6����� >� -�4�8�8�9K�L�L���9�,�,�!�)�*<�=�)�1�5�5�6I�J�J��� �)�)�)�/�/��4�4�I����Y�'�'�'�9<���)�9L�9L�M�!�"5�6�(�
���M�1�2�2��(�!�)� �,�
�
��
�
���
���M�1�2�2�!�)� �,�/�
�
�
�
�
�
rc��|tjurdSt|t��r/|j�|j�tjdt��|jS|S)NzdURLFetch does not support granular timeout settings, reverting to total or default URLFetch timeout.) r�DEFAULT_TIMEOUT�
isinstance�_read�_connectr"r#rr@)r)r=s rrBz&AppEngineManager._get_absolute_timeoutsc���g�-�-�-��4��g�w�'�'� !��}�(�G�,<�,H��
�F�,����
�=� ��rc���t|t��stj|||j���}|js|js|jrtjdt��|S)N)r<�defaultzhURLFetch only supports total retries and does not recognize connect, read, or redirect retry parameters.)
rtr�from_intr(�connect�readr<r"r#r)r)r(r<s rr?zAppEngineManager._get_retries&sn���'�5�)�)� W��n�W�x���V�V�V�G��?� �g�l� �g�.>� ��M�I�(�
�
�
��r)NNTT)
rrr�__doc__r$r,r1rrsrRrJrBr?rrrr r Os���������*��!��0�0�0�0�2�����������'�p�p�p�p�d"
�"
�"
�H�������rr )'r|�
__future__rri�loggingr"�
exceptionsrrrrr r
�six.moves.urllib.parser�requestrr:r
�
util.retryr�util.timeoutr�r�google.appengine.apir�ImportError� getLoggerrrPrrr �is_appengine�is_appengine_sandbox�is_local_appenginerd�is_prod_appengine_mvmsrrr�<module>r�s
��&�&�P'�&�&�&�&�&� � � � �������������������������+�*�*�*�*�*�$�$�$�$�$�$�#�#�#�#�#�#�������"�"�"�"�"�"� � � � � � ��-�-�-�-�-�-�-�������H�H�H������g���!�!�� � � � � �{� � � � � � � � �Y� � � �b�b�b�b�b�~�b�b�b�N"�.��)�>��'�:��&�8��+�B���s�
A�A�A |