34 #include <sys/types.h>
36 #include <sys/mount.h>
53 ProgressData( CURL *_curl, time_t _timeout = 0,
const Url & _url =
Url(),
54 ByteCount expectedFileSize_r = 0,
55 callback::SendReport<DownloadProgressReport> *_report =
nullptr )
70 callback::SendReport<DownloadProgressReport> *
report;
87 void updateStats(
double dltotal = 0.0,
double dlnow = 0.0 )
95 if ( dlnow && dlnow !=
_dnlNow )
136 int reportProgress()
const
165 Pathname MediaCurl::_cookieFile =
"/var/lib/YaST2/cookies";
170 #define SET_OPTION(opt,val) do { \
171 ret = curl_easy_setopt ( _curl, opt, val ); \
173 ZYPP_THROW(MediaCurlSetOptException(_url, _curlError)); \
177 #define SET_OPTION_OFFT(opt,val) SET_OPTION(opt,(curl_off_t)val)
178 #define SET_OPTION_LONG(opt,val) SET_OPTION(opt,(long)val)
179 #define SET_OPTION_VOID(opt,val) SET_OPTION(opt,(void*)val)
181 MediaCurl::MediaCurl(
const Url & url_r,
182 const Pathname & attach_point_hint_r )
192 MIL <<
"MediaCurl::MediaCurl(" << url_r <<
", " << attach_point_hint_r <<
")" << endl;
200 char *atemp = ::strdup( apath.
asString().c_str());
203 atemp == NULL || (atest=::mkdtemp(atemp)) == NULL)
205 WAR <<
"attach point " << ainfo.
path()
206 <<
" is not useable for " << url_r.
getScheme() << endl;
209 else if( atest != NULL)
237 curl_version_info_data *curl_info = NULL;
238 curl_info = curl_version_info(CURLVERSION_NOW);
240 if (curl_info->protocols)
242 const char *
const *proto;
245 for(proto=curl_info->protocols; !found && *proto; ++proto)
247 if( scheme == std::string((
const char *)*proto))
252 std::string msg(
"Unsupported protocol '");
266 const std::string & err_r,
267 const std::string & msg_r )
276 char *ptr = getenv(
"ZYPP_MEDIA_CURL_DEBUG");
277 _curlDebug = (ptr && *ptr) ? str::strtonum<long>( ptr) : 0L;
280 curl_easy_setopt(
_curl, CURLOPT_VERBOSE, 1L);
288 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_ERRORBUFFER,
_curlError );
335 case 4:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
break;
336 case 6:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
break;
360 #if CURLVERSION_AT_LEAST(7,19,4)
362 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS );
380 #ifdef CURLSSLOPT_ALLOW_BEAST
382 ret = curl_easy_setopt(
_curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
391 SET_OPTION(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
409 if ( cred && cred->valid() ) {
427 if (use_auth.empty())
428 use_auth =
"digest,basic";
430 if( auth != CURLAUTH_NONE)
432 DBG <<
"Enabling HTTP authentication methods: " << use_auth
433 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
442 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
452 if ( proxyuserpwd.empty() )
457 DBG <<
"Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
461 DBG <<
"Proxy: using proxy-user from ~/.curlrc" << endl;
469 if ( ! proxyuserpwd.empty() )
474 #if CURLVERSION_AT_LEAST(7,19,4)
479 DBG <<
"Proxy: explicitly NOPROXY" << endl;
485 DBG <<
"Proxy: not explicitly set" << endl;
486 DBG <<
"Proxy: libcurl may look into the environment" << endl;
497 #if CURLVERSION_AT_LEAST(7,15,5)
510 MIL <<
"No cookies requested" << endl;
515 #if CURLVERSION_AT_LEAST(7,18,0)
520 for ( TransferSettings::Headers::const_iterator it = vol_settings.
headersBegin();
552 _curl = curl_easy_init();
589 curl_easy_cleanup(
_curl );
633 unsigned internalTry = 0;
634 static constexpr
unsigned maxInternalTry = 3;
659 if ( internalTry < maxInternalTry ) {
718 bool timeout_reached)
const
723 if (filename.
empty())
732 case CURLE_UNSUPPORTED_PROTOCOL:
733 err =
" Unsupported protocol";
736 err +=
" or redirect (";
741 case CURLE_URL_MALFORMAT:
742 case CURLE_URL_MALFORMAT_USER:
745 case CURLE_LOGIN_DENIED:
749 case CURLE_HTTP_RETURNED_ERROR:
751 long httpReturnCode = 0;
752 CURLcode infoRet = curl_easy_getinfo(
_curl,
753 CURLINFO_RESPONSE_CODE,
755 if ( infoRet == CURLE_OK )
757 std::string msg =
"HTTP response: " +
str::numstring( httpReturnCode );
758 switch ( httpReturnCode )
764 DBG << msg <<
" Login failed (URL: " <<
url.
asString() <<
")" << std::endl;
765 DBG <<
"MediaUnauthorizedException auth hint: '" << auth_hint <<
"'" << std::endl;
780 if (
url.
getHost().find(
".suse.com") != std::string::npos )
781 msg403 =
_(
"Visit the SUSE Customer Center to check whether your registration is valid and has not expired.");
782 else if (
url.
asString().find(
"novell.com") != std::string::npos)
783 msg403 =
_(
"Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
796 std::string msg =
"Unable to retrieve HTTP response:";
802 case CURLE_FTP_COULDNT_RETR_FILE:
803 #if CURLVERSION_AT_LEAST(7,16,0)
804 case CURLE_REMOTE_FILE_NOT_FOUND:
806 case CURLE_FTP_ACCESS_DENIED:
807 case CURLE_TFTP_NOTFOUND:
808 err =
"File not found";
811 case CURLE_BAD_PASSWORD_ENTERED:
812 case CURLE_FTP_USER_PASSWORD_INCORRECT:
813 err =
"Login failed";
815 case CURLE_COULDNT_RESOLVE_PROXY:
816 case CURLE_COULDNT_RESOLVE_HOST:
817 case CURLE_COULDNT_CONNECT:
818 case CURLE_FTP_CANT_GET_HOST:
819 err =
"Connection failed";
821 case CURLE_WRITE_ERROR:
824 case CURLE_PARTIAL_FILE:
825 case CURLE_OPERATION_TIMEDOUT:
826 timeout_reached =
true;
828 case CURLE_ABORTED_BY_CALLBACK:
829 if( timeout_reached )
831 err =
"Timeout reached";
842 case CURLE_HTTP2_STREAM:
892 std::string urlBuffer( curlUrl.
asString());
893 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
906 ret = curl_easy_setopt(
_curl, CURLOPT_NOBODY, 1L );
908 ret = curl_easy_setopt(
_curl, CURLOPT_RANGE,
"0-1" );
911 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
912 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
918 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
922 AutoFILE file { ::fopen(
"/dev/null",
"w" ) };
924 ERR <<
"fopen failed for /dev/null" << endl;
925 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
926 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
932 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
939 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, (*file) );
942 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
943 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
949 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
956 CURLcode ok = curl_easy_perform(
_curl );
957 MIL <<
"perform code: " << ok <<
" [ " << curl_easy_strerror(ok) <<
" ]" << endl;
962 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
972 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L);
981 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL);
1002 return ( ok == CURLE_OK );
1008 #if DETECT_DIR_INDEX
1021 bool not_a_file =
false;
1023 CURLcode ret = curl_easy_getinfo(
_curl,
1024 CURLINFO_EFFECTIVE_URL,
1026 if ( ret == CURLE_OK && ptr != NULL)
1031 std::string path( eurl.getPathName());
1032 if( !path.empty() && path !=
"/" && *path.rbegin() ==
'/')
1034 DBG <<
"Effective url ("
1036 <<
") seems to provide the index of a directory"
1055 DBG <<
"assert_dir " << dest.
dirname() <<
" failed" << endl;
1065 ERR <<
"out of memory for temp file name" << endl;
1069 AutoFD tmp_fd { ::mkostemp( buf, O_CLOEXEC ) };
1072 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
1077 file = ::fdopen( tmp_fd,
"we" );
1080 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
1083 tmp_fd.resetDispose();
1086 DBG <<
"dest: " << dest << endl;
1087 DBG <<
"temp: " << destNew << endl;
1092 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1093 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)
PathInfo(target).mtime());
1097 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1098 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1106 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1107 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1111 long httpReturnCode = 0;
1112 CURLcode infoRet = curl_easy_getinfo(
_curl,
1113 CURLINFO_RESPONSE_CODE,
1115 bool modified =
true;
1116 if (infoRet == CURLE_OK)
1119 if ( httpReturnCode == 304
1122 DBG <<
" Not modified.";
1129 WAR <<
"Could not get the response code." << endl;
1132 if (modified || infoRet != CURLE_OK)
1137 ERR <<
"Failed to chmod file " << destNew << endl;
1141 if ( ::fclose( file ) )
1143 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
1148 if (
rename( destNew, dest ) != 0 ) {
1149 ERR <<
"Rename failed" << endl;
1152 destNew.resetDispose();
1188 std::string urlBuffer( curlUrl.
asString());
1189 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
1190 urlBuffer.c_str() );
1195 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, file );
1204 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
1205 WAR <<
"Can't set CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1208 ret = curl_easy_perform(
_curl );
1209 #if CURLVERSION_AT_LEAST(7,19,4)
1214 if ( ftell(file) == 0 && ret == 0 )
1216 long httpReturnCode = 33;
1217 if ( curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
1219 long conditionUnmet = 33;
1220 if ( curl_easy_getinfo(
_curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
1222 WAR <<
"TIMECONDITION unmet - retry without." << endl;
1223 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1224 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1225 ret = curl_easy_perform(
_curl );
1231 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
1232 WAR <<
"Can't unset CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1238 <<
", temp file size " << ftell(file)
1239 <<
" bytes." << endl;
1246 if ( progressData.fileSizeExceeded )
1257 #if DETECT_DIR_INDEX
1272 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1273 Pathname filename = dirname + it->name;
1276 switch ( it->type ) {
1283 getDir( filename, recurse_r );
1287 WAR <<
"Ignore error (" << res <<
") on creating local directory '" <<
localPath( filename ) <<
"'" << endl;
1301 const Pathname & dirname,
bool dots )
const
1309 const Pathname & dirname,
bool dots )
const
1318 ProgressData *pdata =
reinterpret_cast<ProgressData *
>( clientp );
1324 pdata->updateStats( 0.0, dlnow );
1325 return pdata->reportProgress();
1332 ProgressData *pdata =
reinterpret_cast<ProgressData *
>( clientp );
1336 long httpReturnCode = 0;
1337 if ( curl_easy_getinfo( pdata->curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0 )
1338 return aliveCallback( clientp, dltotal, dlnow, ultotal, ulnow );
1340 pdata->updateStats( dltotal, dlnow );
1341 return pdata->reportProgress();
1348 ProgressData *pdata =
reinterpret_cast<ProgressData *
>(clientp);
1349 return pdata ? pdata->curl : 0;
1356 long auth_info = CURLAUTH_NONE;
1359 curl_easy_getinfo(
_curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1361 if(infoRet == CURLE_OK)
1375 ProgressData *data =
reinterpret_cast<ProgressData *
>(clientp);
1377 data->_expectedFileSize = expectedFileSize;
1392 if (cmcred && firstTry)
1395 DBG <<
"got stored credentials:" << endl << *credentials << endl;
1410 curlcred->setUsername(cmcred->username());
1419 curlcred->setAuthType(availAuthTypes);
1422 if (auth_report->prompt(
_url, prompt_msg, *curlcred))
1424 DBG <<
"callback answer: retry" << endl
1425 <<
"CurlAuthData: " << *curlcred << endl;
1427 if (curlcred->valid())
1429 credentials = curlcred;
1443 DBG <<
"callback answer: cancel" << endl;
1459 if (credentials->authType() == CURLAUTH_NONE)
1460 credentials->setAuthType(availAuthTypes);
1463 if (credentials->authType() != CURLAUTH_NONE)
1467 ret = curl_easy_setopt(
_curl, CURLOPT_HTTPAUTH, credentials->authType());
1473 credentials->setUrl(
_url);
#define EXPLICITLY_NO_PROXY
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
void resetDispose()
Set no dispose function.
Store and operate with byte count.
Base class for Exception.
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.
std::string getScheme() const
Returns the scheme name of the URL.
std::string asString() const
Returns a default string representation of the Url object.
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.
std::string getPathName(EEncoding eflag=zypp::url::E_DECODED) const
Returns the path name from the URL.
std::string getQueryParam(const std::string ¶m, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
void setPathName(const std::string &path, EEncoding eflag=zypp::url::E_DECODED)
Set the path name.
std::string getHost(EEncoding eflag=zypp::url::E_DECODED) const
Returns the hostname or IP from the URL authority.
bool isValid() const
Verifies the Url.
static ZConfig & instance()
Singleton ctor.
Wrapper class for ::stat/::lstat.
const Pathname & path() const
Return current Pathname.
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Pathname dirname() const
Return all but the last component od this path.
const std::string & asString() const
String representation.
bool empty() const
Test for an empty path.
Pathname absolutename() const
Return this path, adding a leading '/' if relative.
const char * c_str() const
String representation.
int ZYPP_MEDIA_CURL_IPRESOLVE()
void fillSettingsFromUrl(const Url &url, media::TransferSettings &s)
Fills the settings structure using options passed on the url for example ?timeout=x&proxy=foo.
size_t log_redirects_curl(char *ptr, size_t size, size_t nmemb, void *userdata)
const char * anonymousIdHeader()
initialized only once, this gets the anonymous id from the target, which we pass in the http header
void globalInitCurlOnce()
const char * distributionFlavorHeader()
initialized only once, this gets the distribution flavor from the target, which we pass in the http h...
std::string curlUnEscape(std::string text_r)
const char * agentString()
initialized only once, this gets the agent string which also includes the curl version
void fillSettingsSystemProxy(const Url &url, media::TransferSettings &s)
Reads the system proxy configuration and fills the settings structure proxy information.
Url clearQueryString(const Url &url)
int log_curl(CURL *curl, curl_infotype info, char *ptr, size_t len, void *max_lvl)
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
int unlink(const Pathname &path)
Like 'unlink'.
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
int rmdir(const Pathname &path)
Like 'rmdir'.
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
std::list< DirEntry > DirContent
Returned by readdir.
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.
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<int> calling ::close
AutoDispose<FILE*> calling ::fclose