/ lib / urllib3 / poolmanager.pyc
poolmanager.pyc
  1  o

  2  ��YcJM�@s6ddlmZddlZddlZddlZddlmZddlmZm	Z	m
  3  Z
  4  ddlmZm
Z
mZmZmZddlmZddlmZdd	lmZdd
  5  lmZddlmZddlmZgd
�Ze�e�Z dZ!dZ"e�#de"�Z$dZ%e�#de%�Z&dd�Z'e�(e'e$�e�(e'e$�d�Z)ee	d�Z*Gdd�de�Z+Gdd�de+�Z,dd�Z-dS)�)�absolute_importN�)�RecentlyUsedContainer)�HTTPConnectionPool�HTTPSConnectionPool�port_by_scheme)�LocationValueError�
MaxRetryError�ProxySchemeUnknown�ProxySchemeUnsupported�URLSchemeUnknown)�six)�urljoin)�RequestMethods)�connection_requires_http_tunnel)�Retry)�	parse_url)�PoolManager�ProxyManager�proxy_from_url)	�key_file�	cert_file�	cert_reqs�ca_certs�ssl_version�ca_cert_dir�ssl_context�key_password�server_hostname)Z
  6  key_schemeZkey_hostZkey_portZkey_timeoutZkey_retriesZ
  7  key_strictZ	key_blockZkey_source_addressZkey_key_fileZkey_key_passwordZ
key_cert_fileZ
key_cert_reqsZkey_ca_certsZkey_ssl_versionZkey_ca_cert_dirZkey_ssl_contextZkey_maxsizeZkey_headersZ
  8  key__proxyZkey__proxy_headersZkey__proxy_configZkey_socket_optionsZkey__socks_optionsZkey_assert_hostnameZkey_assert_fingerprintZkey_server_hostname�PoolKey)r�use_forwarding_for_https�ProxyConfigcCs�|��}|d��|d<|d��|d<dD]}||vr,||dur,t||���||<q|�d�}|dur<t|�|d<t|���D]}|�|�|d|<qB|j	D]
  9  }||vr[d||<qQ|di|��S)a�
 10      Create a pool key out of a request context dictionary.
 11  
 12      According to RFC 3986, both the scheme and host are case-insensitive.
 13      Therefore, this function normalizes both before constructing the pool
 14      key for an HTTPS request. If you wish to change this behaviour, provide
 15      alternate callables to ``key_fn_by_scheme``.
 16  
 17      :param key_class:
 18          The class to use when constructing the key. This should be a namedtuple
 19          with the ``scheme`` and ``host`` keys at a minimum.
 20      :type  key_class: namedtuple
 21      :param request_context:
 22          A dictionary-like object that contain the context for a request.
 23      :type  request_context: dict
 24  
 25      :return: A namedtuple that can be used as a connection pool key.
 26      :rtype:  PoolKey
 27      �scheme�host)�headers�_proxy_headers�_socks_optionsN�socket_options�key_�)
 28  �copy�lower�	frozenset�items�get�tuple�list�keys�pop�_fields)Z	key_class�request_context�context�keyZsocket_opts�fieldr)r)��C:\Users\Jacks.GUTTSPC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\poolmanager.py�_default_key_normalizerOs"�
 29  
 30  �r9��http�httpsc@s�eZdZdZdZdZd dd�Zdd�Zdd	�Zd!d
 31  d�Z	dd
�Z
 32  d"dd�Zdd�Zd!dd�Z
d!dd�Zdd�Zdd�Zdd�Zd#dd�ZdS)$ra$
 33      Allows for arbitrary requests while transparently keeping track of
 34      necessary connection pools for you.
 35  
 36      :param num_pools:
 37          Number of connection pools to cache before discarding the least
 38          recently used pool.
 39  
 40      :param headers:
 41          Headers to include with all requests, unless other headers are given
 42          explicitly.
 43  
 44      :param \**connection_pool_kw:
 45          Additional parameters are used to create fresh
 46          :class:`urllib3.connectionpool.ConnectionPool` instances.
 47  
 48      Example::
 49  
 50          >>> manager = PoolManager(num_pools=2)
 51          >>> r = manager.request('GET', 'http://google.com/')
 52          >>> r = manager.request('GET', 'http://google.com/mail')
 53          >>> r = manager.request('GET', 'http://yahoo.com/')
 54          >>> len(manager.pools)
 55          2
 56  
 57      N�
 58  cKs8t�||�||_t|dd�d�|_t|_t��|_dS)NcSs|��S�N)�close)�pr)r)r8�<lambda>�sz&PoolManager.__init__.<locals>.<lambda>)�dispose_func)r�__init__�connection_pool_kwr�pools�pool_classes_by_scheme�key_fn_by_schemer*)�self�	num_poolsr$rDr)r)r8rC�s
 59  zPoolManager.__init__cCs|Sr>r)�rHr)r)r8�	__enter__�szPoolManager.__enter__cCs|��dS)NF)�clear)rH�exc_type�exc_val�exc_tbr)r)r8�__exit__�szPoolManager.__exit__cCsb|j|}|dur|j��}dD]}|�|d�q|dkr(tD]}|�|d�q|||fi|��S)a�
 60          Create a new :class:`urllib3.connectionpool.ConnectionPool` based on host, port, scheme, and
 61          any additional pool keyword arguments.
 62  
 63          If ``request_context`` is provided, it is provided as keyword arguments
 64          to the pool class used. This method is used to actually create the
 65          connection pools handed out by :meth:`connection_from_url` and
 66          companion methods. It is intended to be overridden for customization.
 67          N)r"r#�portr;)rFrDr*r2�SSL_KEYWORDS)rHr"r#rQr4Zpool_clsr6�kwr)r)r8�	_new_pool�s
 68  
 69  
 70  zPoolManager._new_poolcCs|j��dS)z�
 71          Empty our store of pools and direct them all to close.
 72  
 73          This will not affect in-flight connections, but they will not be
 74          re-used after completion.
 75          N)rErLrJr)r)r8rL�szPoolManager.clearr;cCsT|std��|�|�}|pd|d<|st�|d��d�}||d<||d<|�|�S)a�
 76          Get a :class:`urllib3.connectionpool.ConnectionPool` based on the host, port, and scheme.
 77  
 78          If ``port`` isn't given, it will be derived from the ``scheme`` using
 79          ``urllib3.connectionpool.port_by_scheme``. If ``pool_kwargs`` is
 80          provided, it is merged with the instance's ``connection_pool_kw``
 81          variable and used to create the new connection pool, if one is
 82          needed.
 83          zNo host specified.r;r"�PrQr#)r�_merge_pool_kwargsrr.r+�connection_from_context)rHr#rQr"�pool_kwargsr4r)r)r8�connection_from_host�s
 84  
 85  z PoolManager.connection_from_hostcCs:|d��}|j�|�}|st|��||�}|j||d�S)z�
 86          Get a :class:`urllib3.connectionpool.ConnectionPool` based on the request context.
 87  
 88          ``request_context`` must at least contain the ``scheme`` key and its
 89          value must be a key in ``key_fn_by_scheme`` instance variable.
 90          r"�r4)r+rGr.r�connection_from_pool_key)rHr4r"Zpool_key_constructor�pool_keyr)r)r8rW�sz#PoolManager.connection_from_contextcCs�|jj�4|j�|�}|r|Wd�S|d}|d}|d}|j||||d�}||j|<Wd�|S1s;wY|S)a
 91          Get a :class:`urllib3.connectionpool.ConnectionPool` based on the provided pool key.
 92  
 93          ``pool_key`` should be a namedtuple that only contains immutable
 94          objects. At a minimum it must have the ``scheme``, ``host``, and
 95          ``port`` fields.
 96          Nr"r#rQrZ)rE�lockr.rT)rHr\r4�poolr"r#rQr)r)r8r[s
 97  �
 98  ��z$PoolManager.connection_from_pool_keycCs t|�}|j|j|j|j|d�S)a�
 99          Similar to :func:`urllib3.connectionpool.connection_from_url`.
100  
101          If ``pool_kwargs`` is not provided and a new pool needs to be
102          constructed, ``self.connection_pool_kw`` is used to initialize
103          the :class:`urllib3.connectionpool.ConnectionPool`. If ``pool_kwargs``
104          is provided, it is used instead. Note that if a new pool does not
105          need to be created for the request, the provided ``pool_kwargs`` are
106          not used.
107          )rQr"rX)rrYr#rQr")rH�urlrX�ur)r)r8�connection_from_urls�zPoolManager.connection_from_urlc	CsR|j��}|r'|��D]\}}|dur"z||=Wqty!Yqw|||<q|S)a
108          Merge a dictionary of override values for self.connection_pool_kw.
109  
110          This does not modify self.connection_pool_kw and returns a new dict.
111          Any keys in the override dictionary with a value of ``None`` are
112          removed from the merged dictionary.
113          N)rDr*r-�KeyError)rH�overrideZbase_pool_kwargsr6�valuer)r)r8rV/s
114  
115  �
116  zPoolManager._merge_pool_kwargscCs"|jdurdSt|j|j|j�S)z�
117          Indicates if the proxy requires the complete destination URL in the
118          request.  Normally this is only needed when not using an HTTP CONNECT
119          tunnel.
120          NF)�proxyr�proxy_configr")rH�
121  parsed_urlr)r)r8�!_proxy_requires_url_absolute_formCs
122  
123  �z-PoolManager._proxy_requires_url_absolute_formcCsD|jdus	|dkrdS|jjdkrdStjr|jjs td��dSdS)z�
124          Validates that were not attempting to do TLS in TLS connections on
125          Python2 or with unsupported SSL implementations.
126          Nr<zfContacting HTTPS destinations through HTTPS proxies 'via CONNECT tunnels' is not supported in Python 2)rer"r
�PY2rfr r)rHZ
127  url_schemer)r)r8�$_validate_proxy_scheme_url_selectionPs��z0PoolManager._validate_proxy_scheme_url_selectionTcKs�t|�}|�|j�|j|j|j|jd�}d|d<d|d<d|vr(|j��|d<|�|�r8|j	||fi|��}n|j	||j
128  fi|��}|oH|��}|sM|St||�}|j
dkrYd}|�d�}	t|	t�sjtj|	|d	�}	|	jr�|�|�s�tt�|d��}
129  |
130  D]}|��|	jvr�|d�|d
131  �q}z|	j||||d�}	Wnty�|	jr�|���|YSw|	|d<||d<t�d||�|��|j	||fi|��S)
aN
132          Same as :meth:`urllib3.HTTPConnectionPool.urlopen`
133          with custom cross-host redirect logic and only sends the request-uri
134          portion of the ``url``.
135  
136          The given ``url`` parameter must be absolute, such that an appropriate
137          :class:`urllib3.connectionpool.ConnectionPool` can be chosen for it.
138          )rQr"F�assert_same_host�redirectr$i/�GET�retries)rlN)�response�_poolzRedirecting %s -> %s)rrjr"rYr#rQr$r*rh�urlopen�request_uri�get_redirect_locationr�statusr.�
139  isinstancer�from_int�remove_headers_on_redirect�is_same_hostr0r
�iterkeysr+r2�	incrementr	�raise_on_redirect�
140  drain_conn�log�info)rH�methodr_rlrSr`�connro�redirect_locationrnr$�headerr)r)r8rqasP	
141  
142  
143  
144  
145  
146  ���zPoolManager.urlopen)r=Nr>�Nr;N�T)�__name__�
147  __module__�__qualname__�__doc__rerfrCrKrPrTrLrYrWr[rarVrhrjrqr)r)r)r8r�s"
148  
149  
150  
151  	
152  
153  
rcsReZdZdZ					d�fdd�	Zd�fdd	�	Zdd
154  d�Zd�fd
d�	Z�ZS)ra>
155      Behaves just like :class:`PoolManager`, but sends all requests through
156      the defined proxy, using the CONNECT method for HTTPS URLs.
157  
158      :param proxy_url:
159          The URL of the proxy to be used.
160  
161      :param proxy_headers:
162          A dictionary containing headers that will be sent to the proxy. In case
163          of HTTP they are being sent with each request, while in the
164          HTTPS/CONNECT case they are sent only once. Could be used for proxy
165          authentication.
166  
167      :param proxy_ssl_context:
168          The proxy SSL context is used to establish the TLS connection to the
169          proxy when using HTTPS proxies.
170  
171      :param use_forwarding_for_https:
172          (Defaults to False) If set to True will forward requests to the HTTPS
173          proxy to be made on behalf of the client instead of creating a TLS
174          tunnel via the CONNECT method. **Enabling this flag means that request
175          and response headers and content will be visible from the HTTPS proxy**
176          whereas tunneling keeps request and response headers and content
177          private.  IP address, target hostname, SNI, and port are always visible
178          to an HTTPS proxy even when this flag is disabled.
179  
180      Example:
181          >>> proxy = urllib3.ProxyManager('http://localhost:3128/')
182          >>> r1 = proxy.request('GET', 'http://google.com/')
183          >>> r2 = proxy.request('GET', 'http://httpbin.org/')
184          >>> len(proxy.pools)
185          1
186          >>> r3 = proxy.request('GET', 'https://httpbin.org/')
187          >>> r4 = proxy.request('GET', 'https://twitter.com/')
188          >>> len(proxy.pools)
189          3
190  
191      r=NFc
192  s�t|t�rd|j|j|jf}t|�}|jdvrt|j��|js-t�|jd�}	|j	|	d�}||_
193  |p3i|_||_t
||�|_|j
194  |d<|j|d<|j|d<tt|�j||fi|��dS)Nz
195  %s://%s:%ir:rU)rQ�_proxyr%�
_proxy_config)rurr"r#rQrr
196  rr.�_replacere�
proxy_headers�proxy_ssl_contextr!rf�superrrC)
197  rH�	proxy_urlrIr$r�r�r rDrerQ��	__class__r)r8rC�s(
198  �
199  
200  
201  
202  
203  
204  zProxyManager.__init__r;csD|dkrtt|�j||||d�Stt|�j|jj|jj|jj|d�S)Nr<)rX)r�rrYrer#rQr")rHr#rQr"rXr�r)r8rY�s
205  �
206  �z!ProxyManager.connection_from_hostcCs0ddi}t|�j}|r||d<|r|�|�|S)z�
207          Sets headers needed by proxies: specifically, the Accept and Host
208          headers. Only sets headers not provided by the user.
209          �Acceptz*/*�Host)r�netloc�update)rHr_r$Zheaders_r�r)r)r8�_set_proxy_headers�s
210  
211  zProxyManager._set_proxy_headersTcsVt|�}t|j|j|j�s|�d|j�}|�||�|d<tt	|�j
212  ||fd|i|��S)z@Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute.r$rl)rrrerfr"r.r$r�r�rrq)rHrr_rlrSr`r$r�r)r8rqs
213  zProxyManager.urlopen)r=NNNFr�r>r�)	r�r�r�r�rCrYr�rq�
__classcell__r)r)r�r8r�s*�%
214  
215  rcKstdd|i|��S)Nr�r))r)r_rSr)r)r8rsr).�
216  __future__r�collections�	functools�logging�_collectionsr�connectionpoolrrr�
217  exceptionsrr	r
218  rr�packagesr
�packages.six.moves.urllib.parser�requestr�
219  util.proxyr�
220  util.retryr�util.urlr�__all__�	getLoggerr�r}rR�_key_fields�
221  namedtupler�_proxy_config_fieldsr!r9�partialrGrFrrrr)r)r)r8�<module>s<
222  
223  6
224  �
225  s