HEX
Server: Apache
System: Linux pdx1-shared-a1-38 6.6.104-grsec-jammy+ #3 SMP Tue Sep 16 00:28:11 UTC 2025 x86_64
User: mmickelson (3396398)
PHP: 8.1.31
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/uaclient/http/__pycache__/__init__.cpython-310.pyc
o

��Jhi;�@s�ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZm
Z
mZmZmZddlmZmZddlmZmZddlmZmZmZmZdZdZdZd	Zd
Z e�!e�"e#��Z$edde%fd
ee&e&ffde'fg�Z(edde%fd
ee&e&ffde&fdee&effde
efg�Z)de&de*fdd�Z+de&dee&de&dee&fdd�Z,ia-dee&dee&ddfdd�Z.dee&e&ffdd �Z/d
ej0j1dee&e&ffd!d"�Z2	d@d#ej3d$ee%de(fd%d&�Z4d'd(�Z5d)d*�Z6		dAd#ej3d$ee%dee&de(fd+d,�Z7deefd-d.�Z8de&fd/d0�Z9	dAde&d$ee%dee'e&ffd1d2�Z:diddd3fde&d4ee'd
ee&e&fd5ee&d$ee%d6e*de)fd7d8�Z;	9dBd:e&d;e&d<e&d=e&de)f
d>d?�Z<dS)C�N)�Any�Dict�List�
NamedTuple�Optional�Tuple)�error�request)�ParseResult�urlparse)�defaults�
exceptions�system�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	�Requestrr�_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�openerr r r!�validate_proxy<sT���
	

���rB�
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: %srErFzSetting global proxy dict�extra)rGN)�join�sorted�UA_NO_PROXY_URLS�os�environ�get�set�split�unionr3�debugr	r6r7�install_opener�_global_proxy_dict)rCr)�
proxy_dictrE�env_var�proxy_valuer@rAr r r!�configure_web_proxyqs0���




rWcCstS�N)rSr r r r!�get_configured_web_proxy�srYcCsdd�|��D�S)NcSsi|]	\}}|��|�qSr )�lower)�.0�k�vr r r!�
<dictcomp>�sz$_headers_to_dict.<locals>.<dictcomp>)�items�rr r r!�_headers_to_dict�srar=r:c
Cs�z	tj||d�}Wn1tjy}z|}WYd}~n!d}~wtjy:}zt�t|j��t	j
||jd��d}~ww|��}t
|jt|j�|d�S)N�r:)�causer�rrr)r	�urlopenr�	HTTPErrorr;r3�	exceptionr4r+r
�ConnectivityError�full_url�readrrrar)r=r:�respr?rr r r!�_readurl_urllib�s ����rlcCsJt|�}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�hostnamer3rQ)r)�
target_url�parsed_target_url�parsed_https_proxy�retr r r!�should_use_pycurl�s
���rtcCstd}d}t|j�dkr|jd}t|j�dkr|jd}||kr*|r*d|vr*t���||kr4tj|d��tj|d��)Nr��407�r)r?)�len�argsr
r0r1�PycurlError)rr�authentication_error_code�ca_certificates_error_coder�msgr r r!�_handle_pycurl_error�s

r~c

szddl}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*��}|�+�}|�,�t-|�|d�S)Nr�GETTr&�POSTz5HTTP method "{}" not supported in HTTPS-in-HTTPS modecSsg|]
\}}d�||��qS)z{}: {}��format)r[�name�valr r r!�
<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�:ru)�decoderO�striprZ)�header_line�name_raw�	value_rawr��valuer`r r!�save_header(s
z3_readurl_pycurl_https_in_https.<locals>.save_header)rr{r|rd).�pycurl�ImportErrorr
r/�Curl�
get_method�upper�setopt�HTTPGET�NOBODYr��data�COPYPOSTFIELDSrr��URL�get_full_url�header_itemsrx�
HTTPHEADER�FOLLOWLOCATION�CAINFOr�SSL_CERTS_PATH�TIMEOUTrm�geturl�PROXY�	PROXYTYPEr3�warning�io�BytesIO�	WRITEDATA�HEADERFUNCTION�performrr~�E_RECV_ERROR�E_SSL_CACERT_BADFILE�int�getinfo�
RESPONSE_CODE�getvalue�closer)
r=r:r)r��cr'�header_str_listrr�body_outputr�r?rrr r`r!r.�s|������
	����r.cCs"|s	t���d�}|rt|�SdS)Nr)r	�
getproxiesrMrr(r r r!rmJsrmcCsH|j�d�}i}|s
i}ntj�|�si}nt�t�|��}|�|g�S)N�serviceclient_url_responses)	�featuresrMrK�path�exists�json�loadsr�	load_file)�cfgr�response_overlay_path�response_overlayr r r!�_get_overlay_dataPsr�c
Cs�t||�}|r+|�d�}t�|dd��}|��dfWd�S1s&wYt|�s5tj|d��t�	d�
|��t��d�}i}|rK||d<t
||�rsttj||d	�||d
�}|jdkrgtj|d��t�|j�|j�d�fStj||d	�}	zAt�|	��1}t�|��}|��|j�d
�fWd�Wd�WS1s�wYWd�WdS1s�wYWdStjy�}
z|
jdkr�tj|d��|
jdkr�t����d}
~
ww)Nrr>�	file_path�rwz
URL [GET]: {}rz
If-None-Matchr`�r:r)i0�etag�ETagi�)r��pop�lzmar8rjr"r
�
InvalidUrlr3rQr�rYrMrtr.r	r-r�
ETagUnchanged�
decompressrrrerrf�VulnerabilityDataNotFound)r�rr:r��overlay_responser>�fr)rr=r?r r r!�download_xz_file_from_url^sV


 �
�


�
���&�

��r�Tr�r'�log_response_bodyc
s�t|�s
tj|d��|r|sd}tj||�|d�}d��fdd�t��D��}t�d�	|p.d|||r7|�
d	�nd��t��d
�}t
||�rNt|||d��nt||d���jj
d	d
d�}	i}
g}d�j�dd�vr�tj|	tjd�}t|t�ry|}
nt|t�r�|}nt�dt|��d��fdd�t�j�D��}d�	|p�d||�}
|r��j}|
r�|
}n|r�|}|
d�	|�7}
t�|
�t�j�j|	|
|d�S)Nrwr�)r�rr'z, csg|]
}d�|�|��qS�z
'{}': '{}'r��r[r\r`r r!r��szreadurl.<locals>.<listcomp>z'URL [{}]: {}, headers: {{{}}}, data: {}r�utf-8rr�rb�ignore��errors�application/json�content-typer���cls�unexpected JSON response: %scsg|]}d�|�j|��qSr�)r�rr�)rkr r!r��sz&URL [{}] response: {}, headers: {{{}}}z
, data: {}�rrrrr)r"r
r�r	r-rHrIr3rQr�r�rYrMrtr.rlrrr�r�r�DatetimeAwareJSONDecoder�
isinstance�dict�listr�r4rr)rr�rr'r:r�r=�sorted_header_strr)�decoded_bodyrr�	json_body�	debug_msg�body_to_logr )rrkr!�readurl�sj���	
�

�
�
�r��	localhost�socket_path�http_method�	http_path�
http_hostnamecCs�t�tjtj�}|�|�tj�|�}||_z|�||�|�	�}|�
�jddd�}W|��|��n	|��|��wi}g}	d|j
�dd�vrjtj|tjd�}
t|
t�rZ|
}nt|
t�rb|
}	nt�dt|
��t|jt|j
�|||	d	�S)
Nr�r�r�r�r�r�r�r�r�)r9�AF_UNIX�SOCK_STREAM�connectr�client�HTTPConnection�sockr	�getresponserjr�r�rrMr�r�rr�r�r�r�r3r�r4r�statusra)r�r�r�r�r��connrk�outrrr�r r r!�unix_socket_request�s8

�


�r�rX)NN)r�)=�
email.message�email�http.clientrr�r��loggingr�rKr9�typingrrrrrr�urllibrr	�urllib.parser
r�uaclientrr
rrrJ�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__r3r�r4�bytesrr�boolr"rBrSrWrY�message�Messagerar-rlrtr~r.rmr�r�r�r�r r r r!�<module>s� ��
�����
�2��
�1���
�����
�_���

�6���
����
�M������