HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/uaclient/http/__pycache__/__init__.cpython-310.pyc
o

nHJe-�@sddlZddlZddlZddlZddlZddlmZmZmZm	Z	m
Z
ddlmZm
Z
ddlmZmZddlmZmZmZdZdZdZd	Zd
Ze�e�e��Ze	ddefd
eeeffdefg�Z e	ddefd
eeeffdefdeeeffdeefg�Z!dede"fdd�Z#dede
edede
efdd�Z$ia%de
ede
eddfdd�Z&deeeffdd �Z'	d3d!e
j(d"e
ede fd#d$�Z)d%d&�Z*d'd(�Z+		d4d!e
j(d"e
ede
ede fd)d*�Z,de
efd+d,�Z-diddd-fded.e
e.d
eeefd/e
ed"e
ed0e"de!fd1d2�Z/dS)5�N)�Any�Dict�List�
NamedTuple�Optional)�error�request)�ParseResult�urlparse)�defaults�
exceptions�util)z169.254.169.254�metadataz[fd00:ec2::254]zhttp://archive.ubuntu.comzhttps://esm.ubuntu.comzhttp://api.snapcraft.iozhttps://api.snapcraft.io�UnparsedHTTPResponse�code�headers�body�HTTPResponse�	json_dict�	json_list�url�returncCsRzt|�}Wn
tyYdSw|jdvrdSz|jWdSty(YdSw)NF)�https�httpT)r
�
ValueError�scheme�port)r�
parsed_url�r�8/usr/lib/python3/dist-packages/uaclient/http/__init__.py�is_service_url(s�
��r �protocol�proxy�test_urlc
CsF|sdSt|�stj|d��tj|dd�}|dkrjt|�jdkrjzt||d�}Wn4tjy1�tj	y9�tj
yA�ty\}zt�
d||t|��tj|d��d}~ww|jdkrd|Stj|d��t�||i�}t�|�}z|�|�|WStjt
jfy�}zt�
d||t|d	t|���tj|d��d}~ww)
N)r"�HEAD)�methodr��https_proxyz:Error trying to use "%s" as pycurl proxy to reach "%s": %s��z:Error trying to use "%s" as urllib proxy to reach "%s": %s�reason)r r�ProxyInvalidUrlr�Requestr
r�_readurl_pycurl_https_in_https�PycurlRequiredError�ProxyAuthenticationFailed�PycurlCACertificatesError�	Exception�LOGr�str�ProxyNotWorkingErrorr�ProxyHandler�build_opener�open�socket�timeout�URLError�getattr)r!r"r#�req�response�e�
proxy_handler�openerrrr�validate_proxy9sT���
	

���r@�
http_proxyr'cCs�i}|r||d<|r||d<d�tt��}dD]}tj�|�}|r2d�tt|�d���tt����}qt	�
d|�|tjd<|tjd<|rTt�|�}t�
|�}t�|�t	j
dd	|id
�|adS)aW
    Globally configure pro-client to use http and https proxies.

    - sets global proxy configuration for urllib
    - sets the no_proxy environment variable for the current process
      which gets inherited for all subprocesses
    - sets module variable for use in https-in-https pycurl requests
      this is retrieved later using get_configured_web_proxy

    :param http_proxy: http proxy to be used by urllib. If None, it will
                       not be configured
    :param https_proxy: https proxy to be used by urllib. If None, it will
                        not be configured
    rr�,)�no_proxy�NO_PROXYzSetting no_proxy: %srCrDzSetting global proxy dict�extra)rEN)�join�sorted�UA_NO_PROXY_URLS�os�environ�get�set�split�unionr1�debugrr4r5�install_opener�_global_proxy_dict)rAr'�
proxy_dictrC�env_var�proxy_valuer>r?rrr�configure_web_proxyns0���




rUcCstS�N)rQrrrr�get_configured_web_proxy�srWr;r8c
Cs�z	tj||d�}Wn)tjy}z|}WYd}~nd}~wtjy2}ztj||jd��d}~ww|���	d�}dd�|j
��D�}t|j
||d�S)N�r8�r�utf-8cSsi|]	\}}|��|�qSr)�lower)�.0�k�vrrr�
<dictcomp>�sz#_readurl_urllib.<locals>.<dictcomp>�rrr)r�urlopenr�	HTTPErrorr9r�UrlError�full_url�read�decoder�itemsrr)r;r8�respr=rrrrr�_readurl_urllib�s ����ricCsJt|�}t|�}|jdkot�|j�o|duo|jdk}t�d|�|S)a�
    We only want to use pycurl if all of the following are true

    - The target url scheme is https
    - The target host is not in no_proxy
    - An https_proxy is configured either via pro's config or via environment
    - The https_proxy url scheme is https

    urllib.request provides some helpful functions that we re-use here.

    This function also returns the https_proxy to use, since it is calculated
    here anyway.
    rNzShould use pycurl: %r)r
�_parse_https_proxyrr�proxy_bypass�hostnamer1rO)r'�
target_url�parsed_target_url�parsed_https_proxy�retrrr�should_use_pycurl�s
���rqcCstd}d}t|j�dkr|jd}t|j�dkr|jd}||kr*|r*d|vr*t���||kr4tj|d��tj|d��)Nr�zHTTP code 407 from proxyrY)r=)�len�argsrr.r/�PycurlError)rr�authentication_error_code�ca_certificates_error_coder�msgrrr�_handle_pycurl_error�s

�ryc

s
zddl}Wntyt���w|��}|����}|dkr(|�|jd�n*|dkr4|�|j	d�n|dkrK|�|j
d�|jrJ|�|j|j�nt
d�|���|�|j|���dd�|��D�}t|�dkrq|�|j|�|�|jd�|�|jtj�|r�|�|j|�|r�t|�}|r�|��nd}|�|j|�|�|jd	�nt�d
�t� �}|�|j!|�i��fdd�}	|�|j"|	�z|�#�Wn|j$y�}
zt%|
|��|j&|j'd
�WYd}
~
nd}
~
wwt(|�)|j*��}|�+��,d�}|�-�t.|�|d�S)Nr�GETTr$�POSTz5HTTP method "{}" not supported in HTTPS-in-HTTPS modecSsg|]
\}}d�||��qS)z{}: {}��format)r\�name�valrrr�
<listcomp>	s�z2_readurl_pycurl_https_in_https.<locals>.<listcomp>�z1in pycurl request function without an https proxycsF|�d�}d|vrdS|�dd�\}}|����}|��}|�|<dS)Nz
iso-8859-1�:rr)rfrM�stripr[)�header_line�name_raw�	value_rawr~�value�rrr�save_header%s
z3_readurl_pycurl_https_in_https.<locals>.save_header)rrvrwrZr`)/�pycurl�ImportErrorrr-�Curl�
get_method�upper�setopt�HTTPGET�NOBODYr{�data�COPYPOSTFIELDSrr}�URL�get_full_url�header_itemsrs�
HTTPHEADER�FOLLOWLOCATION�CAINFOr�SSL_CERTS_PATH�TIMEOUTrj�geturl�PROXY�	PROXYTYPEr1�warning�io�BytesIO�	WRITEDATA�HEADERFUNCTION�performrry�E_RECV_ERROR�E_SSL_CACERT_BADFILE�int�getinfo�
RESPONSE_CODE�getvaluerf�closer)
r;r8r'r��cr%�header_str_listro�body_outputr�r=rrrr�rr,�s|������
	����r,cCs"|s	t���d�}|rt|�SdS)Nr)r�
getproxiesrKr
r&rrrrjGsrjTr�r%�log_response_bodyc
sZ|r|sd}tj||�|d�}d��fdd�t��D��}t�d�|p$d|||r-|�d�nd��t��	d	�}t
||�rDt|||d
��nt||d��i}	g}
d�j
�	d
d�vrotj�jtjd�}t|t�rh|}	nt|t�ro|}
d��fdd�t�j
�D��}d�|p�d||�}|r��j}
|	r�|	}
n|
r�|
}
|d�|
�7}t�|�t�j�j
�j|	|
d�S)Nr{)r�rr%z, csg|]
}d�|�|��qS�z
'{}': '{}'r|�r\r]r�rrr�Zszreadurl.<locals>.<listcomp>z'URL [{}]: {}, headers: {{{}}}, data: {}rzrZr)r8r'rXzapplication/jsonzcontent-type�)�clscsg|]}d�|�j|��qSr�)r}rr�)rhrrr�wsz&URL [{}] response: {}, headers: {{{}}}z
, data: {})rrrrr)rr+rFrGr1rOr}rfrWrKrqr,rir�json�loadsrr
�DatetimeAwareJSONDecoder�
isinstance�dict�listrr)rr�rr%r8r�r;�sorted_header_strr'rr�	json_body�	debug_msg�body_to_logr)rrhr�readurlMsb���	
�

�
�
�r�rV)NN)0r�r��loggingrIr7�typingrrrrr�urllibrr�urllib.parser	r
�uaclientrrr
rH�PROXY_VALIDATION_APT_HTTP_URL�PROXY_VALIDATION_APT_HTTPS_URL�PROXY_VALIDATION_SNAP_HTTP_URL�PROXY_VALIDATION_SNAP_HTTPS_URL�	getLogger�replace_top_level_logger_name�__name__r1r�r2rr�boolr r@rQrUrWr+rirqryr,rj�bytesr�rrrr�<module>s���
�����
�2��
�1���
�����
�_���
�����