a ;jE@sddlZddlZddlZddlZddlmZddlmZm Z m Z m Z m Z ddl mZmZmZmZmZmZddZdd d Zd d Zd dZdddZddZddZeZe jeddddZdS)N)SSLError)config constantserrors log_utilsutils) HTTPErrorRequestURLErrorhttplib std_urlopenurlparsec Ost|dr|}n |}t|}|di}|tjtjd|D]\}}| ||qHt d ||zrd|vrt j|d<t jsttddrt}d|_tj|_||d<t|g|Ri|WSt|g|Ri|WSty2}z6|jd krt|j|j|j|j|jWYd}~nd}~0ty}zf|j rt|j d d r|j d j!t!j"krt|d t#|ddd |||_$||_WYd}~n d}~00dS) N get_full_urlheaders) KC-VersionzKC-Patch-Versionz#Requesting url: `{0}`. Headers: {1}timeoutHAS_SNIFcontextirerrnozRequest for `{0}` failed: {1})%hasattrrr popupdaterVERSIONKC_PATCH_VERSIONitems add_headerrlogdebugformatr HTTP_TIMEOUTCHECK_SSL_CERTSgetattrsslcreate_default_contextcheck_hostname CERT_NONE verify_moder r coderNotFoundurlmsghdrsfpr argsrENOENTstrreason) r)r-kwargsZ request_urlrheadervaluectxexr60/usr/libexec/kcare/python/kcarectl/http_utils.py urlopen_basesB     .r8Tcsfdd}|S)Ncsbt|tro|jdkSt|ttjttjfr2dSt |dr^t |j dkr^|j dt j kr^dSdS)NiTr-r) isinstancer r'r r HTTPExceptionrsocketrrlenr-rZ ECONNRESET)estate retry_on_500r6r7check_functionCs  (z3check_urlopen_retry_factory..check_functionr6)rArBr6r@r7check_urlopen_retry_factoryBs rCcCst|dr|}|dS)Nrzfile:)rr startswith)r)r6r6r7 is_local_urlOs rEcOsb|dd}|dtj}t|r8t|g|Ri|Stjt|d|dt|g|Ri|S)NrAT retry_countr@)count)rr RETRY_COUNTrEr8rretryrC)r)r-r1rArFr6r6r7urlopenUs  rJcCsDt||d}tjs(|r(|dd|tjs@|r@|tj||S)N)method Authorization Basic {0})r rUPDATE_FROM_LOCALrrrAUTH_TOKEN_HEADER)r) auth_string auth_tokenrKrequestr6r6r7 http_request]s    rScCs<|dkrtdptdS|dkr8tdp6tdSdS)NhttpZ http_proxyZ HTTP_PROXYhttpsZ https_proxyZ HTTPS_PROXY)osgetenv)schemer6r6r7get_proxy_from_envhsrYcCsttdpttdS)NrTrU)boolrYr6r6r6r7 proxy_is_usedosr[)Z check_retrycCstj|}|std|tt|}|j}|j }|jp@d}|durRtd|durl|j dkrhdnd}|j dkr~t j }n|j dkrt j }ntd |||tjd } i} |rd || d <d | d<t|| d<tj| d<z| d|| D]\} } | | | q| t|d} | | Wdn1s<0Y| }|jdkrz |}Wnty~d}Yn0d|j}|dur|d|7}t|t||j|ddW| n | 0dS)aUpload 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 hostnamerUiPrTzInvalid URL: unsupported scheme)rrMrLzapplication/octet-streamz Content-TypezContent-LengthrPUTrbizFailed to upload file: HTTP {0}z - {0})!rVpathgetsize ValueErrorrrrnstrhostnameportrXr HTTPConnectionHTTPSConnectionrHTTP_UPLOAD_TIMEOUTr/rr putrequestr putheader endheadersopensend getresponsestatusread Exceptionrlogerrorr close)Z file_path upload_urlrP file_sizeparsedhostreZurl_pathZconn_clsconnrr2r3fresponseZ error_body error_msgr6r6r7 upload_filevsT        *      r|)T)NN)N)rrVr<r"rOptionalUnionrrrrrpy23r r r r r rr8rCrErJrSrYr[check_urlopen_retryrIr|r6r6r6r7s   0