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);
394 if (use_auth.empty())
395 use_auth =
"digest,basic";
397 if( auth != CURLAUTH_NONE)
399 DBG <<
"Enabling HTTP authentication methods: " << use_auth
400 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
409 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
419 if ( proxyuserpwd.empty() )
424 DBG <<
"Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
428 DBG <<
"Proxy: using proxy-user from ~/.curlrc" << endl;
436 if ( ! proxyuserpwd.empty() )
441 #if CURLVERSION_AT_LEAST(7,19,4) 446 DBG <<
"Proxy: explicitly NOPROXY" << endl;
452 DBG <<
"Proxy: not explicitly set" << endl;
453 DBG <<
"Proxy: libcurl may look into the environment" << endl;
464 #if CURLVERSION_AT_LEAST(7,15,5) 477 MIL <<
"No cookies requested" << endl;
482 #if CURLVERSION_AT_LEAST(7,18,0) 487 for ( TransferSettings::Headers::const_iterator it = vol_settings.
headersBegin();
519 _curl = curl_easy_init();
556 curl_easy_cleanup(
_curl );
672 bool timeout_reached)
const 677 if (filename.
empty())
686 case CURLE_UNSUPPORTED_PROTOCOL:
687 err =
" Unsupported protocol";
690 err +=
" or redirect (";
695 case CURLE_URL_MALFORMAT:
696 case CURLE_URL_MALFORMAT_USER:
699 case CURLE_LOGIN_DENIED:
703 case CURLE_HTTP_RETURNED_ERROR:
705 long httpReturnCode = 0;
706 CURLcode infoRet = curl_easy_getinfo(
_curl,
707 CURLINFO_RESPONSE_CODE,
709 if ( infoRet == CURLE_OK )
711 std::string msg =
"HTTP response: " +
str::numstring( httpReturnCode );
712 switch ( httpReturnCode )
718 DBG << msg <<
" Login failed (URL: " <<
url.
asString() <<
")" << std::endl;
719 DBG <<
"MediaUnauthorizedException auth hint: '" << auth_hint <<
"'" << std::endl;
734 if (
url.
getHost().find(
".suse.com") != std::string::npos )
735 msg403 =
_(
"Visit the SUSE Customer Center to check whether your registration is valid and has not expired.");
736 else if (
url.
asString().find(
"novell.com") != std::string::npos)
737 msg403 =
_(
"Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
750 std::string msg =
"Unable to retrieve HTTP response:";
756 case CURLE_FTP_COULDNT_RETR_FILE:
757 #if CURLVERSION_AT_LEAST(7,16,0) 758 case CURLE_REMOTE_FILE_NOT_FOUND:
760 case CURLE_FTP_ACCESS_DENIED:
761 case CURLE_TFTP_NOTFOUND:
762 err =
"File not found";
765 case CURLE_BAD_PASSWORD_ENTERED:
766 case CURLE_FTP_USER_PASSWORD_INCORRECT:
767 err =
"Login failed";
769 case CURLE_COULDNT_RESOLVE_PROXY:
770 case CURLE_COULDNT_RESOLVE_HOST:
771 case CURLE_COULDNT_CONNECT:
772 case CURLE_FTP_CANT_GET_HOST:
773 err =
"Connection failed";
775 case CURLE_WRITE_ERROR:
778 case CURLE_PARTIAL_FILE:
779 case CURLE_OPERATION_TIMEDOUT:
780 timeout_reached =
true;
782 case CURLE_ABORTED_BY_CALLBACK:
783 if( timeout_reached )
785 err =
"Timeout reached";
793 case CURLE_SSL_PEER_CERTIFICATE:
839 std::string urlBuffer( curlUrl.
asString());
840 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
853 ret = curl_easy_setopt(
_curl, CURLOPT_NOBODY, 1L );
855 ret = curl_easy_setopt(
_curl, CURLOPT_RANGE,
"0-1" );
858 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
859 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
865 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
869 AutoFILE file { ::fopen(
"/dev/null",
"w" ) };
871 ERR <<
"fopen failed for /dev/null" << endl;
872 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
873 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
879 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
886 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, (*file) );
889 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
890 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
896 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
903 CURLcode ok = curl_easy_perform(
_curl );
904 MIL <<
"perform code: " << ok <<
" [ " << curl_easy_strerror(ok) <<
" ]" << endl;
909 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
919 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L);
928 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL);
949 return ( ok == CURLE_OK );
968 bool not_a_file =
false;
970 CURLcode ret = curl_easy_getinfo(
_curl,
971 CURLINFO_EFFECTIVE_URL,
973 if ( ret == CURLE_OK && ptr != NULL)
978 std::string path( eurl.getPathName());
979 if( !path.empty() && path !=
"/" && *path.rbegin() ==
'/')
981 DBG <<
"Effective url (" 983 <<
") seems to provide the index of a directory" 1002 DBG <<
"assert_dir " << dest.
dirname() <<
" failed" << endl;
1012 ERR <<
"out of memory for temp file name" << endl;
1016 AutoFD tmp_fd { ::mkostemp( buf, O_CLOEXEC ) };
1019 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
1024 file = ::fdopen( tmp_fd,
"we" );
1027 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
1030 tmp_fd.resetDispose();
1033 DBG <<
"dest: " << dest << endl;
1034 DBG <<
"temp: " << destNew << endl;
1039 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1040 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)
PathInfo(target).mtime());
1044 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1045 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1053 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1054 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1058 long httpReturnCode = 0;
1059 CURLcode infoRet = curl_easy_getinfo(
_curl,
1060 CURLINFO_RESPONSE_CODE,
1062 bool modified =
true;
1063 if (infoRet == CURLE_OK)
1066 if ( httpReturnCode == 304
1069 DBG <<
" Not modified.";
1076 WAR <<
"Could not get the reponse code." << endl;
1079 if (modified || infoRet != CURLE_OK)
1084 ERR <<
"Failed to chmod file " << destNew << endl;
1088 if ( ::fclose( file ) )
1090 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
1095 if (
rename( destNew, dest ) != 0 ) {
1096 ERR <<
"Rename failed" << endl;
1099 destNew.resetDispose();
1135 std::string urlBuffer( curlUrl.
asString());
1136 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
1137 urlBuffer.c_str() );
1142 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, file );
1151 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
1152 WAR <<
"Can't set CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1155 ret = curl_easy_perform(
_curl );
1156 #if CURLVERSION_AT_LEAST(7,19,4) 1161 if ( ftell(file) == 0 && ret == 0 )
1163 long httpReturnCode = 33;
1164 if ( curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
1166 long conditionUnmet = 33;
1167 if ( curl_easy_getinfo(
_curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
1169 WAR <<
"TIMECONDITION unmet - retry without." << endl;
1170 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1171 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1172 ret = curl_easy_perform(
_curl );
1178 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
1179 WAR <<
"Can't unset CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1185 <<
", temp file size " << ftell(file)
1186 <<
" bytes." << endl;
1193 if ( progressData.fileSizeExceeded )
1204 #if DETECT_DIR_INDEX 1209 #endif // DETECT_DIR_INDEX 1219 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1220 Pathname filename = dirname + it->name;
1223 switch ( it->type ) {
1230 getDir( filename, recurse_r );
1234 WAR <<
"Ignore error (" << res <<
") on creating local directory '" <<
localPath( filename ) <<
"'" << endl;
1248 const Pathname & dirname,
bool dots )
const 1256 const Pathname & dirname,
bool dots )
const 1265 ProgressData *pdata =
reinterpret_cast<ProgressData *
>( clientp );
1271 pdata->updateStats( 0.0, dlnow );
1272 return pdata->reportProgress();
1279 ProgressData *pdata =
reinterpret_cast<ProgressData *
>( clientp );
1283 long httpReturnCode = 0;
1284 if ( curl_easy_getinfo( pdata->curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0 )
1285 return aliveCallback( clientp, dltotal, dlnow, ultotal, ulnow );
1287 pdata->updateStats( dltotal, dlnow );
1288 return pdata->reportProgress();
1295 ProgressData *pdata =
reinterpret_cast<ProgressData *
>(clientp);
1296 return pdata ? pdata->curl : 0;
1303 long auth_info = CURLAUTH_NONE;
1306 curl_easy_getinfo(
_curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1308 if(infoRet == CURLE_OK)
1322 ProgressData *data =
reinterpret_cast<ProgressData *
>(clientp);
1324 data->_expectedFileSize = expectedFileSize;
1339 if (cmcred && firstTry)
1342 DBG <<
"got stored credentials:" << endl << *credentials << endl;
1357 curlcred->setUsername(cmcred->username());
1366 curlcred->setAuthType(availAuthTypes);
1369 if (auth_report->prompt(
_url, prompt_msg, *curlcred))
1371 DBG <<
"callback answer: retry" << endl
1372 <<
"CurlAuthData: " << *curlcred << endl;
1374 if (curlcred->valid())
1376 credentials = curlcred;
1390 DBG <<
"callback answer: cancel" << endl;
1406 if (credentials->authType() == CURLAUTH_NONE)
1407 credentials->setAuthType(availAuthTypes);
1410 if (credentials->authType() != CURLAUTH_NONE)
1414 ret = curl_easy_setopt(
_curl, CURLOPT_HTTPAUTH, credentials->authType());
1420 credentials->setUrl(
_url);
std::string getScheme() const
Returns the scheme name of the URL.
void globalInitCurlOnce()
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
size_t log_redirects_curl(char *ptr, size_t size, size_t nmemb, void *userdata)
const Pathname & path() const
Return current Pathname.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
static ZConfig & instance()
Singleton ctor.
const char * anonymousIdHeader()
initialized only once, this gets the anonymous id from the target, which we pass in the http header ...
Store and operate with byte count.
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Url clearQueryString(const Url &url)
const char * c_str() const
String representation.
AutoDispose< const Pathname > ManagedFile
A Pathname plus associated cleanup code to be executed when path is no longer needed.
AutoDispose<int> calling ::close
const char * distributionFlavorHeader()
initialized only once, this gets the distribution flavor from the target, which we pass in the http h...
bool empty() const
Test for an empty path.
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
void setPathName(const std::string &path, EEncoding eflag=zypp::url::E_DECODED)
Set the path name.
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
std::string asString() const
Returns a default string representation of the Url object.
std::string getQueryParam(const std::string ¶m, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
int unlink(const Pathname &path)
Like 'unlink'.
const std::string & asString() const
String representation.
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Pathname dirname() const
Return all but the last component od this path.
std::list< DirEntry > DirContent
Returned by readdir.
void fillSettingsFromUrl(const Url &url, media::TransferSettings &s)
Fills the settings structure using options passed on the url for example ?timeout=x&proxy=foo.
bool isValid() const
Verifies the Url.
std::string numstring(char n, int w=0)
void resetDispose()
Set no dispose function.
int rmdir(const Pathname &path)
Like 'rmdir'.
Pathname absolutename() const
Return this path, adding a leading '/' if relative.
Base class for Exception.
std::string getPathName(EEncoding eflag=zypp::url::E_DECODED) const
Returns the path name from the URL.
std::string curlUnEscape(std::string text_r)
std::string getHost(EEncoding eflag=zypp::url::E_DECODED) const
Returns the hostname or IP from the URL authority.
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Wrapper class for ::stat/::lstat.
AutoDispose<FILE*> calling ::fclose
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.
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
Easy-to use interface to the ZYPP dependency resolver.
int log_curl(CURL *curl, curl_infotype info, char *ptr, size_t len, void *max_lvl)
#define EXPLICITLY_NO_PROXY
int ZYPP_MEDIA_CURL_IPRESOLVE()
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.