22#include <zypp-core/parser/Sysconfig>
28#include <zypp-curl/ProxyInfo>
29#include <zypp-curl/auth/CurlAuthData>
30#include <zypp-media/auth/CredentialManager>
31#include <zypp-curl/CurlConfig>
57 void updateStats( curl_off_t dltotal = 0.0, curl_off_t dlnow = 0.0 );
120 if ( dlnow && dlnow !=
_dnlNow )
173 const std::string & err_r,
174 const std::string & msg_r )
195#define SET_OPTION(opt,val) do { \
196 ret = curl_easy_setopt ( curl, opt, val ); \
198 ZYPP_THROW(MediaCurlSetOptException(_urls.at(rData.mirror).url(), _curlError)); \
202#define SET_OPTION_OFFT(opt,val) SET_OPTION(opt,(curl_off_t)val)
203#define SET_OPTION_LONG(opt,val) SET_OPTION(opt,(long)val)
204#define SET_OPTION_VOID(opt,val) SET_OPTION(opt,(void*)val)
207 const Pathname & attach_point_hint_r )
213 _multi = curl_multi_init();
217 MIL <<
"MediaCurl::MediaCurl(" << url_r .
url() <<
", " << attach_point_hint_r <<
")" << endl;
225 char *atemp = ::strdup( apath.
asString().c_str());
228 atemp == NULL || (atest=::mkdtemp(atemp)) == NULL)
230 WAR <<
"attach point " << ainfo.
path()
231 <<
" is not useable for " << url_r.
url().
getScheme() << endl;
234 else if( atest != NULL)
244 try {
release(); }
catch(...) {}
246 curl_multi_cleanup(
_multi);
265 curl_version_info_data *curl_info = NULL;
266 curl_info = curl_version_info(CURLVERSION_NOW);
268 if (curl_info->protocols)
270 const char *
const *
proto =
nullptr;
271 std::string scheme(
url.getScheme());
275 if( scheme == std::string((
const char *)*
proto))
280 std::string msg(
"Unsupported protocol '");
290 CURL *curl = rData.
curl;
293 curl_easy_reset ( curl );
298 CURLcode ret = curl_easy_setopt( curl, CURLOPT_ERRORBUFFER,
_curlError );
309 case 4:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
break;
310 case 6:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
break;
331 if (
_urls.at(rData.
mirror).url().getScheme() ==
"https" )
352#ifdef CURLSSLOPT_ALLOW_BEAST
354 ret = curl_easy_setopt( curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
363 SET_OPTION(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
381 if ( cred && cred->valid() ) {
398 std::string use_auth = settings.
authType();
399 if (use_auth.empty())
400 use_auth =
"digest,basic";
402 if( auth != CURLAUTH_NONE)
404 DBG <<
"Enabling HTTP authentication methods: " << use_auth
405 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
412 DBG <<
"Proxy: '" << settings.
proxy() <<
"'" << endl;
414 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
424 if ( proxyuserpwd.empty() )
429 DBG <<
"Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
433 DBG <<
"Proxy: using proxy-user from ~/.curlrc" << endl;
438 DBG <<
"Proxy: using provided proxy-user '" << settings.
proxyUsername() <<
"'" << endl;
441 if ( ! proxyuserpwd.empty() )
446#if CURLVERSION_AT_LEAST(7,19,4)
451 DBG <<
"Proxy: explicitly NOPROXY" << endl;
457 DBG <<
"Proxy: not explicitly set" << endl;
458 DBG <<
"Proxy: libcurl may look into the environment" << endl;
469#if CURLVERSION_AT_LEAST(7,15,5)
481 const auto &cookieFileParam =
_urls.at(rData.
mirror).url().getQueryParam(
"cookies" );
482 if ( !cookieFileParam.empty() &&
str::strToBool( cookieFileParam,
true ) )
485 MIL <<
"No cookies requested" << endl;
490#if CURLVERSION_AT_LEAST(7,18,0)
500 for (
const auto &header : settings.
headers() ) {
533 auto that =
const_cast<MediaCurl *
>(
this);
534 std::exception_ptr lastErr;
535 for (
int i = 0; i < (int)
_urls.size (); i++ ) {
537 return that->getFileCopyFromMirror ( i, srcFile,
target );
556 const auto &filename = srcFile.
filename();
563 AutoDispose<CURL*> curl( curl_easy_init(), []( CURL *hdl ) {
if ( hdl ) { curl_easy_cleanup(hdl); } } );
566 rData.mirror = mirror;
567 rData.curl = curl.
value ();
569 const auto &myUrl =
_urls[mirror];
571 if( !myUrl.url().isValid() )
574 if( myUrl.url().getHost().empty() )
579 bool firstAuth =
true;
580 unsigned internalTry = 0;
581 static constexpr unsigned maxInternalTry = 3;
588 if( assert_dir( dest.
dirname() ) )
590 DBG <<
"assert_dir " << dest.
dirname() <<
" failed" << endl;
600 ERR <<
"out of memory for temp file name" << endl;
604 AutoFD tmp_fd { ::mkostemp( buf, O_CLOEXEC ) };
607 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
612 file = ::fdopen( tmp_fd,
"we" );
615 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
618 tmp_fd.resetDispose();
621 DBG <<
"dest: " << dest << endl;
622 DBG <<
"temp: " << destNew << endl;
629 curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
630 curl_easy_setopt(curl, CURLOPT_TIMEVALUE, (
long)
PathInfo(
target).mtime());
634 curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
635 curl_easy_setopt(curl, CURLOPT_TIMEVALUE, 0L);
639 curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
640 curl_easy_setopt(curl, CURLOPT_TIMEVALUE, 0L);
661 std::string urlBuffer( curlUrl.
asString());
662 CURLcode ret = curl_easy_setopt( curl, CURLOPT_URL,
668 ret = curl_easy_setopt( curl, CURLOPT_WRITEDATA, file.
value() );
675 report->start(fileurl, dest);
677 if ( curl_easy_setopt( curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
678 WAR <<
"Can't set CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
682 #if CURLVERSION_AT_LEAST(7,19,4)
687 if ( ftell(file) == 0 && ret == 0 )
689 long httpReturnCode = 33;
690 if ( curl_easy_getinfo( curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
692 long conditionUnmet = 33;
693 if ( curl_easy_getinfo( curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
695 WAR <<
"TIMECONDITION unmet - retry without." << endl;
696 curl_easy_setopt( curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
697 curl_easy_setopt( curl, CURLOPT_TIMEVALUE, 0L);
704 if ( curl_easy_setopt( curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
705 WAR <<
"Can't unset CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
710 <<
", temp file size " << ftell(file)
711 <<
" bytes." << endl;
723 long httpReturnCode = 0;
724 CURLcode infoRet = curl_easy_getinfo(curl,
725 CURLINFO_RESPONSE_CODE,
727 bool modified =
true;
728 if (infoRet == CURLE_OK)
731 if ( httpReturnCode == 304
732 || ( httpReturnCode == 213 && (myUrl.url().getScheme() ==
"ftp" || myUrl.url().getScheme() ==
"tftp") ) )
734 DBG <<
" Not modified.";
741 WAR <<
"Could not get the response code." << endl;
744 if (modified || infoRet != CURLE_OK)
749 ERR <<
"Failed to chmod file " << destNew << endl;
753 if ( ::fclose( file ) )
755 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
760 if ( rename( destNew, dest ) != 0 ) {
761 ERR <<
"Rename failed" << endl;
764 destNew.resetDispose();
786 if ( internalTry < maxInternalTry ) {
813 auto that =
const_cast<MediaCurl *
>(
this);
815 std::exception_ptr lastErr;
816 for (
int i = 0; i < (int)
_urls.size (); i++ ) {
818 return that->doGetDoesFileExist( i, filename );
843 bool timeout_reached)
const
849 if (filename.
empty())
850 url = baseMirr.url();
858 case CURLE_UNSUPPORTED_PROTOCOL:
859 err =
" Unsupported protocol";
862 err +=
" or redirect (";
867 case CURLE_URL_MALFORMAT:
868 case CURLE_URL_MALFORMAT_USER:
871 case CURLE_LOGIN_DENIED:
875 case CURLE_HTTP_RETURNED_ERROR:
877 long httpReturnCode = 0;
878 CURLcode infoRet = curl_easy_getinfo( rData.
curl,
879 CURLINFO_RESPONSE_CODE,
881 if ( infoRet == CURLE_OK )
883 std::string msg =
"HTTP response: " +
str::numstring( httpReturnCode );
884 switch ( httpReturnCode )
890 DBG << msg <<
" Login failed (URL: " <<
url.asString() <<
")" << std::endl;
891 DBG <<
"MediaUnauthorizedException auth hint: '" << auth_hint <<
"'" << std::endl;
906 if (
url.getHost().find(
".suse.com") != std::string::npos )
907 msg403 =
_(
"Visit the SUSE Customer Center to check whether your registration is valid and has not expired.");
908 else if (
url.asString().find(
"novell.com") != std::string::npos)
909 msg403 =
_(
"Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
917 DBG << msg <<
" (URL: " <<
url.asString() <<
")" << std::endl;
922 std::string msg =
"Unable to retrieve HTTP response:";
923 DBG << msg <<
" (URL: " <<
url.asString() <<
")" << std::endl;
928 case CURLE_FTP_COULDNT_RETR_FILE:
929#if CURLVERSION_AT_LEAST(7,16,0)
930 case CURLE_REMOTE_FILE_NOT_FOUND:
932 case CURLE_FTP_ACCESS_DENIED:
933 case CURLE_TFTP_NOTFOUND:
934 err =
"File not found";
937 case CURLE_BAD_PASSWORD_ENTERED:
938 case CURLE_FTP_USER_PASSWORD_INCORRECT:
939 err =
"Login failed";
941 case CURLE_COULDNT_RESOLVE_PROXY:
942 case CURLE_COULDNT_RESOLVE_HOST:
943 case CURLE_COULDNT_CONNECT:
944 case CURLE_FTP_CANT_GET_HOST:
945 err =
"Connection failed";
947 case CURLE_WRITE_ERROR:
950 case CURLE_PARTIAL_FILE:
951 case CURLE_OPERATION_TIMEDOUT:
952 timeout_reached =
true;
954 case CURLE_ABORTED_BY_CALLBACK:
955 if( timeout_reached )
957 err =
"Timeout reached";
987 AutoDispose<CURL*> curl( curl_easy_init(), []( CURL *hdl ) {
if ( hdl ) { curl_easy_cleanup(hdl); } } );
989 rData.mirror = mirror;
990 rData.curl = curl.
value ();
992 const auto &myUrl =
_urls[mirror];
994 if( !myUrl.url().isValid() )
997 if( myUrl.url().getHost().empty() )
1002 DBG <<
"URL: " <<
url.asString() << endl;
1017 std::string urlBuffer( curlUrl.
asString());
1020 bool canRetry =
true;
1021 bool firstAuth =
false;
1024 while ( canRetry ) {
1028 CURLcode ret = curl_easy_setopt( curl, CURLOPT_URL,
1029 urlBuffer.c_str() );
1034 AutoFILE file { ::fopen(
"/dev/null",
"w" ) };
1036 ERR <<
"fopen failed for /dev/null" << endl;
1040 ret = curl_easy_setopt( curl, CURLOPT_WRITEDATA, (*file) );
1051 const bool doHeadRequest = (myUrl.url().getScheme() ==
"http" || myUrl.url().getScheme() ==
"https") && settings.headRequestsAllowed();
1052 if ( doHeadRequest ) {
1053 curl_easy_setopt( curl, CURLOPT_NOBODY, 1L );
1055 curl_easy_setopt( curl, CURLOPT_RANGE,
"0-1" );
1060 MIL <<
"perform code: " << ok <<
" [ " << curl_easy_strerror(ok) <<
" ]" << endl;
1080 return ( ok == CURLE_OK );
1108 long httpReturnCode = 0;
1109 if ( curl_easy_getinfo( pdata->
curl(), CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0 )
1110 return aliveCallback( clientp, dltotal, dlnow, ultotal, ulnow );
1121 return pdata ? pdata->
curl() : 0;
1128 long auth_info = CURLAUTH_NONE;
1131 curl_easy_getinfo(curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1133 if(infoRet == CURLE_OK)
1160 CURL *curl = rData.
curl;
1169 if ( curl_multi_add_handle(
_multi, curl ) != CURLM_OK )
1177 if (mcode != CURLM_OK)
1180 bool canContinue =
true;
1181 while ( canContinue ) {
1183 CURLMsg *msg =
nullptr;
1185 while ((msg = curl_multi_info_read(
_multi, &nqueue)) != 0) {
1186 if ( msg->msg != CURLMSG_DONE )
continue;
1187 if ( msg->easy_handle != curl )
continue;
1189 return msg->data.result;
1193 std::vector<GPollFD> requestedFds = _curlHelper.
socks;
1202 if (mcode != CURLM_OK)
1206 if (mcode != CURLM_OK)
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
reference value() const
Reference to the Tp object.
void resetDispose()
Set no dispose function.
Store and operate with byte count.
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
void addHistory(const std::string &msg_r)
Add some message text to the history.
ProgressData()
Ctor no range [0,0](0).
std::string getScheme() const
Returns the scheme name of the URL.
std::string asString() const
Returns a default string representation of the Url object.
static ZConfig & instance()
Singleton ctor.
Wrapper class for stat/lstat.
const Pathname & path() const
Return current Pathname.
Pathname dirname() const
Return all but the last component od this path.
const char * c_str() const
String representation.
const std::string & asString() const
String representation.
bool empty() const
Test for an empty path.
#define EXPLICITLY_NO_PROXY
size_t log_redirects_curl(char *ptr, size_t size, size_t nmemb, void *userdata)
void globalInitCurlOnce()
std::string curlUnEscape(const std::string &text_r)
void setupZYPP_MEDIA_CURL_DEBUG(CURL *curl)
Setup CURLOPT_VERBOSE and CURLOPT_DEBUGFUNCTION according to env::ZYPP_MEDIA_CURL_DEBUG.
CURLcode setCurlRedirProtocols(CURL *curl)
int ZYPP_MEDIA_CURL_IPRESOLVE()
4/6 to force IPv4/v6
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
int unlink(const Pathname &path)
Like 'unlink'.
std::string numstring(char n, int w=0)
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
int zypp_poll(std::vector< GPollFD > &fds, int timeout)
Small wrapper around g_poll that additionally listens to the shutdown FD returned by ZYpp::shutdownSi...
Easy-to use interface to the ZYPP dependency resolver.
AutoDispose< const Pathname > ManagedFile
A Pathname plus associated cleanup code to be executed when path is no longer needed.
AutoDispose< void > OnScopeExit
CURLMcode handleSocketActions(const std::vector< GPollFD > &actionsFds, int first=0)
std::vector< GPollFD > socks
std::optional< long > timeout_ms
Bottleneck filtering all DownloadProgressReport issued from Media[Muli]Curl.
ByteCount _expectedFileSize
curl_off_t _dnlNow
Bytes downloaded now.
int _dnlPercent
Percent completed or 0 if _dnlTotal is unknown.
time_t _timeRcv
Start of no-data timeout.
ByteCount expectedFileSize() const
time_t _timeLast
Start last period(~1sec)
int reportProgress() const
double _drateLast
Download rate in last period.
bool timeoutReached() const
void expectedFileSize(ByteCount newval_r)
curl_off_t _dnlLast
Bytes downloaded at period start.
bool fileSizeExceeded() const
void updateStats(curl_off_t dltotal=0.0, curl_off_t dlnow=0.0)
double _drateTotal
Download rate so far.
zypp::callback::SendReport< zypp::media::DownloadProgressReport > * report
curl_off_t _dnlTotal
Bytes to download or 0 if unknown.
time_t _timeStart
Start total stats.
ProgressData(CURL *curl, time_t timeout=0, zypp::Url url=zypp::Url(), zypp::ByteCount expectedFileSize_r=0, zypp::callback::SendReport< zypp::media::DownloadProgressReport > *_report=nullptr)
AutoDispose<int> calling ::close
AutoDispose<FILE*> calling ::fclose
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
#define ZYPP_FWD_CURRENT_EXCPT()
Drops a logline and returns the current Exception as a std::exception_ptr.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.