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) 350 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS );
352 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS );
370 #ifdef CURLSSLOPT_ALLOW_BEAST 372 ret = curl_easy_setopt(
_curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
381 SET_OPTION(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
399 if ( cred && cred->valid() ) {
417 if (use_auth.empty())
418 use_auth =
"digest,basic";
420 if( auth != CURLAUTH_NONE)
422 DBG <<
"Enabling HTTP authentication methods: " << use_auth
423 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
432 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
442 if ( proxyuserpwd.empty() )
447 DBG <<
"Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
451 DBG <<
"Proxy: using proxy-user from ~/.curlrc" << endl;
459 if ( ! proxyuserpwd.empty() )
464 #if CURLVERSION_AT_LEAST(7,19,4) 469 DBG <<
"Proxy: explicitly NOPROXY" << endl;
475 DBG <<
"Proxy: not explicitly set" << endl;
476 DBG <<
"Proxy: libcurl may look into the environment" << endl;
487 #if CURLVERSION_AT_LEAST(7,15,5) 500 MIL <<
"No cookies requested" << endl;
505 #if CURLVERSION_AT_LEAST(7,18,0) 510 for ( TransferSettings::Headers::const_iterator it = vol_settings.
headersBegin();
542 _curl = curl_easy_init();
579 curl_easy_cleanup(
_curl );
695 bool timeout_reached)
const 700 if (filename.
empty())
709 case CURLE_UNSUPPORTED_PROTOCOL:
710 err =
" Unsupported protocol";
713 err +=
" or redirect (";
718 case CURLE_URL_MALFORMAT:
719 case CURLE_URL_MALFORMAT_USER:
722 case CURLE_LOGIN_DENIED:
726 case CURLE_HTTP_RETURNED_ERROR:
728 long httpReturnCode = 0;
729 CURLcode infoRet = curl_easy_getinfo(
_curl,
730 CURLINFO_RESPONSE_CODE,
732 if ( infoRet == CURLE_OK )
734 std::string msg =
"HTTP response: " +
str::numstring( httpReturnCode );
735 switch ( httpReturnCode )
741 DBG << msg <<
" Login failed (URL: " <<
url.
asString() <<
")" << std::endl;
742 DBG <<
"MediaUnauthorizedException auth hint: '" << auth_hint <<
"'" << std::endl;
757 if (
url.
getHost().find(
".suse.com") != std::string::npos )
758 msg403 =
_(
"Visit the SUSE Customer Center to check whether your registration is valid and has not expired.");
759 else if (
url.
asString().find(
"novell.com") != std::string::npos)
760 msg403 =
_(
"Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
773 std::string msg =
"Unable to retrieve HTTP response:";
779 case CURLE_FTP_COULDNT_RETR_FILE:
780 #if CURLVERSION_AT_LEAST(7,16,0) 781 case CURLE_REMOTE_FILE_NOT_FOUND:
783 case CURLE_FTP_ACCESS_DENIED:
784 case CURLE_TFTP_NOTFOUND:
785 err =
"File not found";
788 case CURLE_BAD_PASSWORD_ENTERED:
789 case CURLE_FTP_USER_PASSWORD_INCORRECT:
790 err =
"Login failed";
792 case CURLE_COULDNT_RESOLVE_PROXY:
793 case CURLE_COULDNT_RESOLVE_HOST:
794 case CURLE_COULDNT_CONNECT:
795 case CURLE_FTP_CANT_GET_HOST:
796 err =
"Connection failed";
798 case CURLE_WRITE_ERROR:
801 case CURLE_PARTIAL_FILE:
802 case CURLE_OPERATION_TIMEDOUT:
803 timeout_reached =
true;
805 case CURLE_ABORTED_BY_CALLBACK:
806 if( timeout_reached )
808 err =
"Timeout reached";
816 case CURLE_SSL_PEER_CERTIFICATE:
862 std::string urlBuffer( curlUrl.
asString());
863 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
876 ret = curl_easy_setopt(
_curl, CURLOPT_NOBODY, 1L );
878 ret = curl_easy_setopt(
_curl, CURLOPT_RANGE,
"0-1" );
881 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
882 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
888 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
892 AutoFILE file { ::fopen(
"/dev/null",
"w" ) };
894 ERR <<
"fopen failed for /dev/null" << endl;
895 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
896 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
902 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
909 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, (*file) );
912 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
913 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
919 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
926 CURLcode ok = curl_easy_perform(
_curl );
927 MIL <<
"perform code: " << ok <<
" [ " << curl_easy_strerror(ok) <<
" ]" << endl;
932 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
942 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L);
951 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL);
972 return ( ok == CURLE_OK );
991 bool not_a_file =
false;
993 CURLcode ret = curl_easy_getinfo(
_curl,
994 CURLINFO_EFFECTIVE_URL,
996 if ( ret == CURLE_OK && ptr != NULL)
1001 std::string path( eurl.getPathName());
1002 if( !path.empty() && path !=
"/" && *path.rbegin() ==
'/')
1004 DBG <<
"Effective url (" 1006 <<
") seems to provide the index of a directory" 1025 DBG <<
"assert_dir " << dest.
dirname() <<
" failed" << endl;
1035 ERR <<
"out of memory for temp file name" << endl;
1039 AutoFD tmp_fd { ::mkostemp( buf, O_CLOEXEC ) };
1042 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
1047 file = ::fdopen( tmp_fd,
"we" );
1050 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
1053 tmp_fd.resetDispose();
1056 DBG <<
"dest: " << dest << endl;
1057 DBG <<
"temp: " << destNew << endl;
1062 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1063 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)
PathInfo(target).mtime());
1067 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1068 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1076 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1077 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1081 long httpReturnCode = 0;
1082 CURLcode infoRet = curl_easy_getinfo(
_curl,
1083 CURLINFO_RESPONSE_CODE,
1085 bool modified =
true;
1086 if (infoRet == CURLE_OK)
1089 if ( httpReturnCode == 304
1092 DBG <<
" Not modified.";
1099 WAR <<
"Could not get the reponse code." << endl;
1102 if (modified || infoRet != CURLE_OK)
1107 ERR <<
"Failed to chmod file " << destNew << endl;
1111 if ( ::fclose( file ) )
1113 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
1118 if (
rename( destNew, dest ) != 0 ) {
1119 ERR <<
"Rename failed" << endl;
1122 destNew.resetDispose();
1158 std::string urlBuffer( curlUrl.
asString());
1159 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
1160 urlBuffer.c_str() );
1165 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, file );
1174 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
1175 WAR <<
"Can't set CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1178 ret = curl_easy_perform(
_curl );
1179 #if CURLVERSION_AT_LEAST(7,19,4) 1184 if ( ftell(file) == 0 && ret == 0 )
1186 long httpReturnCode = 33;
1187 if ( curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
1189 long conditionUnmet = 33;
1190 if ( curl_easy_getinfo(
_curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
1192 WAR <<
"TIMECONDITION unmet - retry without." << endl;
1193 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1194 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1195 ret = curl_easy_perform(
_curl );
1201 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
1202 WAR <<
"Can't unset CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1208 <<
", temp file size " << ftell(file)
1209 <<
" bytes." << endl;
1216 if ( progressData.fileSizeExceeded )
1227 #if DETECT_DIR_INDEX 1232 #endif // DETECT_DIR_INDEX 1242 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1243 Pathname filename = dirname + it->name;
1246 switch ( it->type ) {
1253 getDir( filename, recurse_r );
1257 WAR <<
"Ignore error (" << res <<
") on creating local directory '" <<
localPath( filename ) <<
"'" << endl;
1271 const Pathname & dirname,
bool dots )
const 1279 const Pathname & dirname,
bool dots )
const 1288 ProgressData *pdata =
reinterpret_cast<ProgressData *
>( clientp );
1294 pdata->updateStats( 0.0, dlnow );
1295 return pdata->reportProgress();
1302 ProgressData *pdata =
reinterpret_cast<ProgressData *
>( clientp );
1306 long httpReturnCode = 0;
1307 if ( curl_easy_getinfo( pdata->curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0 )
1308 return aliveCallback( clientp, dltotal, dlnow, ultotal, ulnow );
1310 pdata->updateStats( dltotal, dlnow );
1311 return pdata->reportProgress();
1318 ProgressData *pdata =
reinterpret_cast<ProgressData *
>(clientp);
1319 return pdata ? pdata->curl : 0;
1326 long auth_info = CURLAUTH_NONE;
1329 curl_easy_getinfo(
_curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1331 if(infoRet == CURLE_OK)
1345 ProgressData *data =
reinterpret_cast<ProgressData *
>(clientp);
1347 data->_expectedFileSize = expectedFileSize;
1362 if (cmcred && firstTry)
1365 DBG <<
"got stored credentials:" << endl << *credentials << endl;
1380 curlcred->setUsername(cmcred->username());
1389 curlcred->setAuthType(availAuthTypes);
1392 if (auth_report->prompt(
_url, prompt_msg, *curlcred))
1394 DBG <<
"callback answer: retry" << endl
1395 <<
"CurlAuthData: " << *curlcred << endl;
1397 if (curlcred->valid())
1399 credentials = curlcred;
1413 DBG <<
"callback answer: cancel" << endl;
1429 if (credentials->authType() == CURLAUTH_NONE)
1430 credentials->setAuthType(availAuthTypes);
1433 if (credentials->authType() != CURLAUTH_NONE)
1437 ret = curl_easy_setopt(
_curl, CURLOPT_HTTPAUTH, credentials->authType());
1443 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.