File: //usr/lib/python3/dist-packages/pip/_vendor/requests/__pycache__/sessions.cpython-310.pyc
o
@%Ne�t � @ s� d Z ddlZddlZddlZddlmZ ddlmZ ddlm Z ddl
mZmZm
Z
mZmZ ddlmZmZmZmZ dd lmZmZmZ dd
lmZmZ ddlmZ ddlmZm Z m!Z! dd
l"m#Z#m$Z$m%Z%m&Z& ddl'm(Z( ddl)m*Z* ddlm+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3 ddlm4Z4 ej5dkr�zej6Z7W n e8y� ej9Z7Y nw ejZ7efdd�Z:efdd�Z;G dd� de<�Z=G dd� de=�Z>dd� Z?dS )z�
requests.sessions
~~~~~~~~~~~~~~~~~
This module provides a Session object to manage and persist settings across
requests (cookies, auth, proxies).
� N)� timedelta)�OrderedDict� )�_basic_auth_str)� cookielib�is_py3�urljoin�urlparse�Mapping)�cookiejar_from_dict�extract_cookies_to_jar�RequestsCookieJar�
merge_cookies)�Request�PreparedRequest�DEFAULT_REDIRECT_LIMIT)�
default_hooks�
dispatch_hook)�to_native_string)�to_key_val_list�default_headers�
DEFAULT_PORTS)�TooManyRedirects�
InvalidSchema�ChunkedEncodingError�ContentDecodingError)�CaseInsensitiveDict)�HTTPAdapter)�requote_uri�get_environ_proxies�get_netrc_auth�should_bypass_proxies�get_auth_from_url�rewind_body�resolve_proxies)�codes)�REDIRECT_STATI�win32c C sp |du r| S | du r|S t |t�rt | t�s| S |t|��}|�t| �� dd� |�� D �}|D ]}||= q0|S )z�Determines appropriate setting for a given request, taking into account
the explicit setting on that request, and the setting in the session. If a
setting is a dictionary, they will be merged together using `dict_class`
Nc S s g | ]
\}}|d u r|�qS �N� )�.0�k�vr) r) �?/usr/lib/python3/dist-packages/pip/_vendor/requests/sessions.py�
<listcomp>J s z!merge_setting.<locals>.<listcomp>)�
isinstancer
r �update�items)�request_setting�session_setting�
dict_class�merged_setting� none_keys�keyr) r) r- �
merge_setting2 s ��r8 c C s@ |du s|� d�g kr
| S | du s| � d�g kr|S t| ||�S )z�Properly merges both requests and session hooks.
This is necessary because when request_hooks == {'response': []}, the
merge breaks Session hooks entirely.
N�response)�getr8 )�
request_hooks�
session_hooksr4 r) r) r- �merge_hooksQ s
r= c @ sB e Zd Zdd� Zdd� Z ddd �Zd
d� Zdd
� Zdd� ZdS )�SessionRedirectMixinc C s, |j r|jd }tr|�d�}t|d�S dS )z7Receives a Response. Returns a redirect URI or ``None``�location�latin1�utf8N)�is_redirect�headersr �encoder )�self�respr? r) r) r- �get_redirect_targetb s
z(SessionRedirectMixin.get_redirect_targetc C s� t |�}t |�}|j|jkrdS |jdkr&|jdv r&|jdkr&|jdv r&dS |j|jk}|j|jk}t�|jd�df}|sI|j|v rI|j|v rIdS |pL|S )zFDecide whether Authorization header should be removed when redirectingT�http)�P N�https)i� NFN)r �hostname�scheme�portr r: )rE �old_url�new_url�
old_parsed�
new_parsed�changed_port�changed_scheme�default_portr) r) r- �should_strip_authw s
z&SessionRedirectMixin.should_strip_authFNTc
k s@ � g }
| � |�}t|j�j}|�r|�� }
|
�|� |
dd� |_z|j W n tt t
fy9 |jjdd� Y nw t
|j�| jkrLtd�| j�|d��|�� |�d�rdt|j�}d�t|j�|g�}t|�}|jd krv|rv|j|d
�}n|jr||j}|�� }|js�t|jt|��}nt|�}t|�|
_| �|
|� |jtjtjfvr�d}|D ] }|
j �!|d� q�d|
_"|
j }|�!dd� t#|
j$||j� t%|
j$| j&� |
�'|
j$� | �(|
|�}| �)|
|� |
j*duo�d
|v p�d|v }|r�t+|
� |
}|r�|V n | j,|f|||||dd�| ��}t#| j&|
|j� | � |�}|V |sdS dS )zBReceives a Response. Returns a generator of Responses or Requests.r NF)�decode_contentzExceeded {} redirects.)r9 z//�:� )�fragment)�Content-LengthzContent-Type�Transfer-Encoding�CookierZ r[ )�stream�timeout�verify�cert�proxies�allow_redirects)-rG r �urlrY �copy�append�history�contentr r �RuntimeError�raw�read�len�
max_redirectsr �format�close�
startswith�joinr rL �_replace�geturl�netlocr r �rebuild_method�status_coder% �temporary_redirect�permanent_redirectrC �pop�bodyr �_cookiesr �cookies�prepare_cookies�rebuild_proxies�rebuild_auth�_body_positionr# �send)rE rF �reqr] r^ r_ r` ra �yield_requests�adapter_kwargs�histrc �previous_fragment�prepared_request�parsed_rurl�parsed�purged_headers�headerrC �
rewindabler) r) r- �resolve_redirects� s� �
�
����
�z&SessionRedirectMixin.resolve_redirectsc C sV |j }|j}d|v r| �|jj|�r|d= | jrt|�nd}|dur)|�|� dS dS )z�When being redirected we may want to strip authentication from the
request to avoid leaking credentials. This method intelligently removes
and reapplies authentication where possible to avoid credential loss.
�
AuthorizationN)rC rc rU �request� trust_envr �prepare_auth)rE r� r9 rC rc �new_authr) r) r- r~ � s �z!SessionRedirectMixin.rebuild_authc C sx |j }t|j�j}t||| j�}d|v r|d= z
t|| �\}}W n
ty. d\}}Y nw |r:|r:t||�|d<