HEX
Server: Apache
System: Linux box5936.bluehost.com 5.14.0-162.23.1.9991722448259.nf.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jul 31 18:11:45 UTC 2024 x86_64
User: aviatjd3 (1318)
PHP: 8.3.32
Disabled: NONE
Upload Files
File: //usr/libexec/kcare/python/kcarectl/__pycache__/http_utils.cpython-39.pyc
a

��;jE�@s�ddlZddlZddlZddlZddlmZddlmZm	Z	m
Z
mZmZddl
mZmZmZmZmZmZdd�Zdd	d
�Zdd�Zd
d�Zddd�Zdd�Zdd�Ze�Zejed�ddd��ZdS)�N)�SSLError�)�config�	constants�errors�	log_utils�utils)�	HTTPError�Request�URLError�httplib�std_urlopen�urlparsec	
Os�t|d�r|��}n|}t|�}|�di�}|�tjtjd��|��D]\}}|�	||�qHt
�d�||��zrd|vr�t
j|d<t
js�ttdd�r�t��}d|_tj|_||d<t|g|�Ri|��WSt|g|�Ri|��WSt�y2}z6|jd	k�rt�|j|j|j|j|j���WYd}~n�d}~0t�y�}zf|j �r�t|j d
d��r�|j d
j!t!j"k�r�t�|d	t#|�dd��d�||�|_$||_�WYd}~n
d}~00dS)
N�get_full_url�headers)�
KC-VersionzKC-Patch-Versionz#Requesting url: `{0}`. Headers: {1}�timeout�HAS_SNIF�contexti�r�errnozRequest for `{0}` failed: {1})%�hasattrrr
�pop�updater�VERSION�KC_PATCH_VERSION�items�
add_headerr�logdebug�formatr�HTTP_TIMEOUT�CHECK_SSL_CERTS�getattr�ssl�create_default_context�check_hostname�	CERT_NONE�verify_moder
r	�coder�NotFound�url�msg�hdrs�fpr�argsr�ENOENT�str�reason)	r)r-�kwargsZrequest_urlr�header�value�ctx�ex�r6�0/usr/libexec/kcare/python/kcarectl/http_utils.py�urlopen_basesB

��
.r8Tcs�fdd�}|S)Ncsbt|t�r�o|jdkSt|ttjttjf�r2dSt	|d�r^t
|j�dkr^|jdtj
kr^dSdS)Ni�Tr-�r)�
isinstancer	r'rr�
HTTPExceptionr�socketrr�lenr-rZ
ECONNRESET)�e�state��retry_on_500r6r7�check_functionCs
(z3check_urlopen_retry_factory.<locals>.check_functionr6)rArBr6r@r7�check_urlopen_retry_factoryBs	rCcCst|d�r|��}|�d�S)Nrzfile:)rr�
startswith)r)r6r6r7�is_local_urlOs
rEcOsb|�dd�}|�dtj�}t|�r8t|g|�Ri|��Stjt|d�|d�t�|g|�Ri|��S)NrAT�retry_countr@)�count)rr�RETRY_COUNTrEr8r�retryrC)r)r-r1rArFr6r6r7�urlopenUs
rJcCsDt||d�}tjs(|r(|�dd�|��tjs@|r@|�tj|�|S)N)�method�
Authorization�	Basic {0})r
r�UPDATE_FROM_LOCALrrr�AUTH_TOKEN_HEADER)r)�auth_string�
auth_tokenrK�requestr6r6r7�http_request]s

rScCs<|dkrt�d�pt�d�S|dkr8t�d�p6t�d�SdS)N�httpZ
http_proxyZ
HTTP_PROXY�httpsZhttps_proxyZHTTPS_PROXY)�os�getenv)�schemer6r6r7�get_proxy_from_envhsrYcCsttd��pttd��S)NrTrU)�boolrYr6r6r6r7�
proxy_is_usedosr[)Zcheck_retrycCs�tj�|�}|std�|���tt�|��}|j}|j	}|jp@d}|durRtd��|durl|j
dkrhdnd}|j
dkr~tj}n|j
dkr�tj
}ntd	��|||tjd
�}	i}
|r�d�|�|
d<d
|
d<t|�|
d<tj|
d<z�|	�d|�|
��D]\}}|	�||�q�|	��t|d��}
|	�|
�Wd�n1�s<0Y|	��}|jdk�r�z|��}Wnt�y~d}Yn0d�|j�}|du�r�|d�|�7}t�|�t||j|dd��W|	� �n
|	� �0dS)a�Upload a file to the given URL using HTTP PUT with chunked streaming.
    Note: The standard library urllib doesn't support PUT with data
    We need to use httplib directly for this

    This function uses streaming upload to support large files up to 1GB
    without loading the entire file into memory.

    :param file_path: Path to the file to upload
    :param upload_url: Full URL to upload the file to. Query params are ignored.
    :param auth_string: Optional authentication string for Basic Auth
    :return: None if upload succeeded
    :raises HTTPError: If upload fails with HTTP status >= 400
    :raises ValueError: If URL is invalid
    z"Refusing to upload empty file: {0}�/NzInvalid URL: missing hostnamerUi��PrTzInvalid URL: unsupported scheme)rrMrLzapplication/octet-streamzContent-TypezContent-Lengthr�PUT�rbi�zFailed to upload file: HTTP {0}z - {0})!rV�path�getsize�
ValueErrorrrr�nstr�hostname�portrXr�HTTPConnection�HTTPSConnectionr�HTTP_UPLOAD_TIMEOUTr/rr�
putrequestr�	putheader�
endheaders�open�send�getresponse�status�read�	Exceptionr�logerrorr	�close)Z	file_path�
upload_urlrP�	file_size�parsed�hostreZurl_pathZconn_cls�connrr2r3�f�responseZ
error_body�	error_msgr6r6r7�upload_filevsT



*


r|)T)NN)N)rrVr<r"r�Optional�Union�rrrrr�py23r	r
rrr
rr8rCrErJrSrYr[�check_urlopen_retryrIr|r6r6r6r7�<module>s  0