Go to the documentation of this file.
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 '");
263 char *ptr = getenv(
"ZYPP_MEDIA_CURL_DEBUG");
264 _curlDebug = (ptr && *ptr) ? str::strtonum<long>( ptr) : 0L;
267 curl_easy_setopt(
_curl, CURLOPT_VERBOSE, 1L);
275 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_ERRORBUFFER,
_curlError );
322 case 4:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
break;
323 case 6:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
break;
347 #if CURLVERSION_AT_LEAST(7,19,4)
349 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS );
367 #ifdef CURLSSLOPT_ALLOW_BEAST
369 ret = curl_easy_setopt(
_curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
378 SET_OPTION(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
396 if ( cred && cred->valid() ) {
414 if (use_auth.empty())
415 use_auth =
"digest,basic";
417 if( auth != CURLAUTH_NONE)
419 DBG <<
"Enabling HTTP authentication methods: " << use_auth
420 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
429 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
439 if ( proxyuserpwd.empty() )
444 DBG <<
"Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
448 DBG <<
"Proxy: using proxy-user from ~/.curlrc" << endl;
456 if ( ! proxyuserpwd.empty() )
461 #if CURLVERSION_AT_LEAST(7,19,4)
466 DBG <<
"Proxy: explicitly NOPROXY" << endl;
472 DBG <<
"Proxy: not explicitly set" << endl;
473 DBG <<
"Proxy: libcurl may look into the environment" << endl;
484 #if CURLVERSION_AT_LEAST(7,15,5)
497 MIL <<
"No cookies requested" << endl;
502 #if CURLVERSION_AT_LEAST(7,18,0)
507 for ( TransferSettings::Headers::const_iterator it = vol_settings.
headersBegin();
539 _curl = curl_easy_init();
576 curl_easy_cleanup(
_curl );
692 bool timeout_reached)
const
697 if (filename.
empty())
706 case CURLE_UNSUPPORTED_PROTOCOL:
707 err =
" Unsupported protocol";
710 err +=
" or redirect (";
715 case CURLE_URL_MALFORMAT:
716 case CURLE_URL_MALFORMAT_USER:
719 case CURLE_LOGIN_DENIED:
723 case CURLE_HTTP_RETURNED_ERROR:
725 long httpReturnCode = 0;
726 CURLcode infoRet = curl_easy_getinfo(
_curl,
727 CURLINFO_RESPONSE_CODE,
729 if ( infoRet == CURLE_OK )
731 std::string msg =
"HTTP response: " +
str::numstring( httpReturnCode );
732 switch ( httpReturnCode )
738 DBG << msg <<
" Login failed (URL: " <<
url.
asString() <<
")" << std::endl;
739 DBG <<
"MediaUnauthorizedException auth hint: '" << auth_hint <<
"'" << std::endl;
754 if (
url.
getHost().find(
".suse.com") != std::string::npos )
755 msg403 =
_(
"Visit the SUSE Customer Center to check whether your registration is valid and has not expired.");
756 else if (
url.
asString().find(
"novell.com") != std::string::npos)
757 msg403 =
_(
"Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
770 std::string msg =
"Unable to retrieve HTTP response:";
776 case CURLE_FTP_COULDNT_RETR_FILE:
777 #if CURLVERSION_AT_LEAST(7,16,0)
778 case CURLE_REMOTE_FILE_NOT_FOUND:
780 case CURLE_FTP_ACCESS_DENIED:
781 case CURLE_TFTP_NOTFOUND:
782 err =
"File not found";
785 case CURLE_BAD_PASSWORD_ENTERED:
786 case CURLE_FTP_USER_PASSWORD_INCORRECT:
787 err =
"Login failed";
789 case CURLE_COULDNT_RESOLVE_PROXY:
790 case CURLE_COULDNT_RESOLVE_HOST:
791 case CURLE_COULDNT_CONNECT:
792 case CURLE_FTP_CANT_GET_HOST:
793 err =
"Connection failed";
795 case CURLE_WRITE_ERROR:
798 case CURLE_PARTIAL_FILE:
799 case CURLE_OPERATION_TIMEDOUT:
800 timeout_reached =
true;
802 case CURLE_ABORTED_BY_CALLBACK:
803 if( timeout_reached )
805 err =
"Timeout reached";
813 case CURLE_SSL_PEER_CERTIFICATE:
859 std::string urlBuffer( curlUrl.
asString());
860 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
873 ret = curl_easy_setopt(
_curl, CURLOPT_NOBODY, 1L );
875 ret = curl_easy_setopt(
_curl, CURLOPT_RANGE,
"0-1" );
878 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
879 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
885 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
889 AutoFILE file { ::fopen(
"/dev/null",
"w" ) };
891 ERR <<
"fopen failed for /dev/null" << endl;
892 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
893 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
899 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
906 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, (*file) );
909 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
910 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
916 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
923 CURLcode ok = curl_easy_perform(
_curl );
924 MIL <<
"perform code: " << ok <<
" [ " << curl_easy_strerror(ok) <<
" ]" << endl;
929 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
939 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L);
948 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL);
969 return ( ok == CURLE_OK );
988 bool not_a_file =
false;
990 CURLcode ret = curl_easy_getinfo(
_curl,
991 CURLINFO_EFFECTIVE_URL,
993 if ( ret == CURLE_OK && ptr != NULL)
998 std::string path( eurl.getPathName());
999 if( !path.empty() && path !=
"/" && *path.rbegin() ==
'/')
1001 DBG <<
"Effective url ("
1003 <<
") seems to provide the index of a directory"
1022 DBG <<
"assert_dir " << dest.
dirname() <<
" failed" << endl;
1032 ERR <<
"out of memory for temp file name" << endl;
1036 AutoFD tmp_fd { ::mkostemp( buf, O_CLOEXEC ) };
1039 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
1044 file = ::fdopen( tmp_fd,
"we" );
1047 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
1050 tmp_fd.resetDispose();
1053 DBG <<
"dest: " << dest << endl;
1054 DBG <<
"temp: " << destNew << endl;
1059 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1060 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)
PathInfo(target).mtime());
1064 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1065 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1073 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1074 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1078 long httpReturnCode = 0;
1079 CURLcode infoRet = curl_easy_getinfo(
_curl,
1080 CURLINFO_RESPONSE_CODE,
1082 bool modified =
true;
1083 if (infoRet == CURLE_OK)
1086 if ( httpReturnCode == 304
1089 DBG <<
" Not modified.";
1096 WAR <<
"Could not get the reponse code." << endl;
1099 if (modified || infoRet != CURLE_OK)
1104 ERR <<
"Failed to chmod file " << destNew << endl;
1108 if ( ::fclose( file ) )
1110 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
1115 if (
rename( destNew, dest ) != 0 ) {
1116 ERR <<
"Rename failed" << endl;
1119 destNew.resetDispose();
1155 std::string urlBuffer( curlUrl.
asString());
1156 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
1157 urlBuffer.c_str() );
1162 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, file );
1171 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
1172 WAR <<
"Can't set CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1175 ret = curl_easy_perform(
_curl );
1176 #if CURLVERSION_AT_LEAST(7,19,4)
1181 if ( ftell(file) == 0 && ret == 0 )
1183 long httpReturnCode = 33;
1184 if ( curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
1186 long conditionUnmet = 33;
1187 if ( curl_easy_getinfo(
_curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
1189 WAR <<
"TIMECONDITION unmet - retry without." << endl;
1190 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1191 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1192 ret = curl_easy_perform(
_curl );
1198 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
1199 WAR <<
"Can't unset CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1205 <<
", temp file size " << ftell(file)
1206 <<
" bytes." << endl;
1213 if ( progressData.fileSizeExceeded )
1224 #if DETECT_DIR_INDEX
1229 #endif // DETECT_DIR_INDEX
1239 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1240 Pathname filename = dirname + it->name;
1243 switch ( it->type ) {
1250 getDir( filename, recurse_r );
1254 WAR <<
"Ignore error (" << res <<
") on creating local directory '" <<
localPath( filename ) <<
"'" << endl;
1268 const Pathname & dirname,
bool dots )
const
1276 const Pathname & dirname,
bool dots )
const
1285 ProgressData *pdata =
reinterpret_cast<ProgressData *
>( clientp );
1291 pdata->updateStats( 0.0, dlnow );
1292 return pdata->reportProgress();
1299 ProgressData *pdata =
reinterpret_cast<ProgressData *
>( clientp );
1303 long httpReturnCode = 0;
1304 if ( curl_easy_getinfo( pdata->curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0 )
1305 return aliveCallback( clientp, dltotal, dlnow, ultotal, ulnow );
1307 pdata->updateStats( dltotal, dlnow );
1308 return pdata->reportProgress();
1315 ProgressData *pdata =
reinterpret_cast<ProgressData *
>(clientp);
1316 return pdata ? pdata->curl : 0;
1323 long auth_info = CURLAUTH_NONE;
1326 curl_easy_getinfo(
_curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1328 if(infoRet == CURLE_OK)
1342 ProgressData *data =
reinterpret_cast<ProgressData *
>(clientp);
1344 data->_expectedFileSize = expectedFileSize;
1359 if (cmcred && firstTry)
1362 DBG <<
"got stored credentials:" << endl << *credentials << endl;
1377 curlcred->setUsername(cmcred->username());
1386 curlcred->setAuthType(availAuthTypes);
1389 if (auth_report->prompt(
_url, prompt_msg, *curlcred))
1391 DBG <<
"callback answer: retry" << endl
1392 <<
"CurlAuthData: " << *curlcred << endl;
1394 if (curlcred->valid())
1396 credentials = curlcred;
1410 DBG <<
"callback answer: cancel" << endl;
1426 if (credentials->authType() == CURLAUTH_NONE)
1427 credentials->setAuthType(availAuthTypes);
1430 if (credentials->authType() != CURLAUTH_NONE)
1434 ret = curl_easy_setopt(
_curl, CURLOPT_HTTPAUTH, credentials->authType());
1440 credentials->setUrl(
_url);
std::string curlUnEscape(std::string text_r)
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
const Pathname & path() const
Return current Pathname.
std::string getScheme() const
Returns the scheme name of the URL.
const char * agentString()
initialized only once, this gets the agent string which also includes the curl version
Base class for Exception.
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
int log_curl(CURL *curl, curl_infotype info, char *ptr, size_t len, void *max_lvl)
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
AutoDispose< const Pathname > ManagedFile
A Pathname plus associated cleanup code to be executed when path is no longer needed.
void fillSettingsSystemProxy(const Url &url, media::TransferSettings &s)
Reads the system proxy configuration and fills the settings structure proxy information.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
const char * anonymousIdHeader()
initialized only once, this gets the anonymous id from the target, which we pass in the http header
int unlink(const Pathname &path)
Like 'unlink'.
void fillSettingsFromUrl(const Url &url, media::TransferSettings &s)
Fills the settings structure using options passed on the url for example ?timeout=x&proxy=foo.
Store and operate with byte count.
void globalInitCurlOnce()
int ZYPP_MEDIA_CURL_IPRESOLVE()
std::string getQueryParam(const std::string ¶m, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
int rmdir(const Pathname &path)
Like 'rmdir'.
Wrapper class for ::stat/::lstat.
AutoDispose<int> calling ::close
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Url clearQueryString(const Url &url)
std::string asString() const
Returns a default string representation of the Url object.
const char * c_str() const
String representation.
void setPathName(const std::string &path, EEncoding eflag=zypp::url::E_DECODED)
Set the path name.
Pathname absolutename() const
Return this path, adding a leading '/' if relative.
bool isValid() const
Verifies the Url.
static ZConfig & instance()
Singleton ctor.
Easy-to use interface to the ZYPP dependency resolver.
std::list< DirEntry > DirContent
Returned by readdir.
std::string numstring(char n, int w=0)
AutoDispose<FILE*> calling ::fclose
std::string getPathName(EEncoding eflag=zypp::url::E_DECODED) const
Returns the path name from the URL.
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Pathname dirname() const
Return all but the last component od this path.
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
bool empty() const
Test for an empty path.
const char * distributionFlavorHeader()
initialized only once, this gets the distribution flavor from the target, which we pass in the http h...
std::string getHost(EEncoding eflag=zypp::url::E_DECODED) const
Returns the hostname or IP from the URL authority.
void resetDispose()
Set no dispose function.
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
const std::string & asString() const
String representation.
size_t log_redirects_curl(char *ptr, size_t size, size_t nmemb, void *userdata)
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.
#define EXPLICITLY_NO_PROXY