34 #include <sys/types.h> 36 #include <sys/mount.h> 52 ProgressData( CURL *_curl, time_t _timeout = 0,
const Url & _url =
Url(),
53 ByteCount expectedFileSize_r = 0,
54 callback::SendReport<DownloadProgressReport> *_report =
nullptr )
69 callback::SendReport<DownloadProgressReport> *
report;
86 void updateStats(
double dltotal = 0.0,
double dlnow = 0.0 )
94 if ( dlnow && dlnow !=
_dnlNow )
135 int reportProgress()
const 164 Pathname MediaCurl::_cookieFile =
"/var/lib/YaST2/cookies";
169 #define SET_OPTION(opt,val) do { \ 170 ret = curl_easy_setopt ( _curl, opt, val ); \ 172 ZYPP_THROW(MediaCurlSetOptException(_url, _curlError)); \ 176 #define SET_OPTION_OFFT(opt,val) SET_OPTION(opt,(curl_off_t)val) 177 #define SET_OPTION_LONG(opt,val) SET_OPTION(opt,(long)val) 178 #define SET_OPTION_VOID(opt,val) SET_OPTION(opt,(void*)val) 180 MediaCurl::MediaCurl(
const Url & url_r,
181 const Pathname & attach_point_hint_r )
191 MIL <<
"MediaCurl::MediaCurl(" << url_r <<
", " << attach_point_hint_r <<
")" << endl;
199 char *atemp = ::strdup( apath.
asString().c_str());
202 atemp == NULL || (atest=::mkdtemp(atemp)) == NULL)
204 WAR <<
"attach point " << ainfo.
path()
205 <<
" is not useable for " << url_r.
getScheme() << endl;
208 else if( atest != NULL)
236 curl_version_info_data *curl_info = NULL;
237 curl_info = curl_version_info(CURLVERSION_NOW);
239 if (curl_info->protocols)
241 const char *
const *proto;
244 for(proto=curl_info->protocols; !found && *proto; ++proto)
246 if( scheme == std::string((
const char *)*proto))
251 std::string msg(
"Unsupported protocol '");
262 char *ptr = getenv(
"ZYPP_MEDIA_CURL_DEBUG");
263 _curlDebug = (ptr && *ptr) ? str::strtonum<long>( ptr) : 0L;
266 curl_easy_setopt(
_curl, CURLOPT_VERBOSE, 1L);
274 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_ERRORBUFFER,
_curlError );
321 case 4:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
break;
322 case 6:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
break;
346 #if CURLVERSION_AT_LEAST(7,19,4) 348 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS );
366 #ifdef CURLSSLOPT_ALLOW_BEAST 368 ret = curl_easy_setopt(
_curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
377 SET_OPTION(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
393 if (use_auth.empty())
394 use_auth =
"digest,basic";
396 if( auth != CURLAUTH_NONE)
398 DBG <<
"Enabling HTTP authentication methods: " << use_auth
399 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
408 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
418 if ( proxyuserpwd.empty() )
423 DBG <<
"Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
427 DBG <<
"Proxy: using proxy-user from ~/.curlrc" << endl;
435 if ( ! proxyuserpwd.empty() )
440 #if CURLVERSION_AT_LEAST(7,19,4) 445 DBG <<
"Proxy: explicitly NOPROXY" << endl;
451 DBG <<
"Proxy: not explicitly set" << endl;
452 DBG <<
"Proxy: libcurl may look into the environment" << endl;
463 #if CURLVERSION_AT_LEAST(7,15,5) 476 MIL <<
"No cookies requested" << endl;
481 #if CURLVERSION_AT_LEAST(7,18,0) 486 for ( TransferSettings::Headers::const_iterator it = vol_settings.
headersBegin();
518 _curl = curl_easy_init();
555 curl_easy_cleanup(
_curl );
671 bool timeout_reached)
const 676 if (filename.
empty())
685 case CURLE_UNSUPPORTED_PROTOCOL:
686 err =
" Unsupported protocol";
689 err +=
" or redirect (";
694 case CURLE_URL_MALFORMAT:
695 case CURLE_URL_MALFORMAT_USER:
698 case CURLE_LOGIN_DENIED:
702 case CURLE_HTTP_RETURNED_ERROR:
704 long httpReturnCode = 0;
705 CURLcode infoRet = curl_easy_getinfo(
_curl,
706 CURLINFO_RESPONSE_CODE,
708 if ( infoRet == CURLE_OK )
710 string msg =
"HTTP response: " +
str::numstring( httpReturnCode );
711 switch ( httpReturnCode )
717 DBG << msg <<
" Login failed (URL: " <<
url.
asString() <<
")" << std::endl;
718 DBG <<
"MediaUnauthorizedException auth hint: '" << auth_hint <<
"'" << std::endl;
733 if (
url.
getHost().find(
".suse.com") != string::npos )
734 msg403 =
_(
"Visit the SUSE Customer Center to check whether your registration is valid and has not expired.");
735 else if (
url.
asString().find(
"novell.com") != string::npos)
736 msg403 =
_(
"Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
749 string msg =
"Unable to retrieve HTTP response:";
755 case CURLE_FTP_COULDNT_RETR_FILE:
756 #if CURLVERSION_AT_LEAST(7,16,0) 757 case CURLE_REMOTE_FILE_NOT_FOUND:
759 case CURLE_FTP_ACCESS_DENIED:
760 case CURLE_TFTP_NOTFOUND:
761 err =
"File not found";
764 case CURLE_BAD_PASSWORD_ENTERED:
765 case CURLE_FTP_USER_PASSWORD_INCORRECT:
766 err =
"Login failed";
768 case CURLE_COULDNT_RESOLVE_PROXY:
769 case CURLE_COULDNT_RESOLVE_HOST:
770 case CURLE_COULDNT_CONNECT:
771 case CURLE_FTP_CANT_GET_HOST:
772 err =
"Connection failed";
774 case CURLE_WRITE_ERROR:
777 case CURLE_PARTIAL_FILE:
778 case CURLE_OPERATION_TIMEDOUT:
779 timeout_reached =
true;
781 case CURLE_ABORTED_BY_CALLBACK:
782 if( timeout_reached )
784 err =
"Timeout reached";
792 case CURLE_SSL_PEER_CERTIFICATE:
838 string urlBuffer( curlUrl.
asString());
839 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
852 ret = curl_easy_setopt(
_curl, CURLOPT_NOBODY, 1L );
854 ret = curl_easy_setopt(
_curl, CURLOPT_RANGE,
"0-1" );
857 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
858 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
864 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
868 AutoFILE file { ::fopen(
"/dev/null",
"w" ) };
870 ERR <<
"fopen failed for /dev/null" << endl;
871 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
872 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
878 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
885 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, (*file) );
888 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
889 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
895 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
902 CURLcode ok = curl_easy_perform(
_curl );
903 MIL <<
"perform code: " << ok <<
" [ " << curl_easy_strerror(ok) <<
" ]" << endl;
908 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
918 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L);
927 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL);
948 return ( ok == CURLE_OK );
967 bool not_a_file =
false;
969 CURLcode ret = curl_easy_getinfo(
_curl,
970 CURLINFO_EFFECTIVE_URL,
972 if ( ret == CURLE_OK && ptr != NULL)
977 std::string path( eurl.getPathName());
978 if( !path.empty() && path !=
"/" && *path.rbegin() ==
'/')
980 DBG <<
"Effective url (" 982 <<
") seems to provide the index of a directory" 1001 DBG <<
"assert_dir " << dest.
dirname() <<
" failed" << endl;
1011 ERR <<
"out of memory for temp file name" << endl;
1015 AutoFD tmp_fd { ::mkostemp( buf, O_CLOEXEC ) };
1018 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
1023 file = ::fdopen( tmp_fd,
"we" );
1026 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
1029 tmp_fd.resetDispose();
1032 DBG <<
"dest: " << dest << endl;
1033 DBG <<
"temp: " << destNew << endl;
1038 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1039 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)
PathInfo(target).mtime());
1043 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1044 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1052 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1053 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1057 long httpReturnCode = 0;
1058 CURLcode infoRet = curl_easy_getinfo(
_curl,
1059 CURLINFO_RESPONSE_CODE,
1061 bool modified =
true;
1062 if (infoRet == CURLE_OK)
1065 if ( httpReturnCode == 304
1068 DBG <<
" Not modified.";
1075 WAR <<
"Could not get the reponse code." << endl;
1078 if (modified || infoRet != CURLE_OK)
1083 ERR <<
"Failed to chmod file " << destNew << endl;
1087 if ( ::fclose( file ) )
1089 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
1094 if (
rename( destNew, dest ) != 0 ) {
1095 ERR <<
"Rename failed" << endl;
1098 destNew.resetDispose();
1134 string urlBuffer( curlUrl.
asString());
1135 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
1136 urlBuffer.c_str() );
1141 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, file );
1150 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
1151 WAR <<
"Can't set CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1154 ret = curl_easy_perform(
_curl );
1155 #if CURLVERSION_AT_LEAST(7,19,4) 1160 if ( ftell(file) == 0 && ret == 0 )
1162 long httpReturnCode = 33;
1163 if ( curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
1165 long conditionUnmet = 33;
1166 if ( curl_easy_getinfo(
_curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
1168 WAR <<
"TIMECONDITION unmet - retry without." << endl;
1169 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1170 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1171 ret = curl_easy_perform(
_curl );
1177 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
1178 WAR <<
"Can't unset CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1184 <<
", temp file size " << ftell(file)
1185 <<
" bytes." << endl;
1192 if ( progressData.fileSizeExceeded )
1203 #if DETECT_DIR_INDEX 1208 #endif // DETECT_DIR_INDEX 1218 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1219 Pathname filename = dirname + it->name;
1222 switch ( it->type ) {
1229 getDir( filename, recurse_r );
1233 WAR <<
"Ignore error (" << res <<
") on creating local directory '" <<
localPath( filename ) <<
"'" << endl;
1247 const Pathname & dirname,
bool dots )
const 1255 const Pathname & dirname,
bool dots )
const 1264 ProgressData *pdata =
reinterpret_cast<ProgressData *
>( clientp );
1270 pdata->updateStats( 0.0, dlnow );
1271 return pdata->reportProgress();
1278 ProgressData *pdata =
reinterpret_cast<ProgressData *
>( clientp );
1282 long httpReturnCode = 0;
1283 if ( curl_easy_getinfo( pdata->curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0 )
1284 return aliveCallback( clientp, dltotal, dlnow, ultotal, ulnow );
1286 pdata->updateStats( dltotal, dlnow );
1287 return pdata->reportProgress();
1294 ProgressData *pdata =
reinterpret_cast<ProgressData *
>(clientp);
1295 return pdata ? pdata->curl : 0;
1302 long auth_info = CURLAUTH_NONE;
1305 curl_easy_getinfo(
_curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1307 if(infoRet == CURLE_OK)
1321 ProgressData *data =
reinterpret_cast<ProgressData *
>(clientp);
1323 data->_expectedFileSize = expectedFileSize;
1338 if (cmcred && firstTry)
1341 DBG <<
"got stored credentials:" << endl << *credentials << endl;
1356 curlcred->setUsername(cmcred->username());
1365 curlcred->setAuthType(availAuthTypes);
1368 if (auth_report->prompt(
_url, prompt_msg, *curlcred))
1370 DBG <<
"callback answer: retry" << endl
1371 <<
"CurlAuthData: " << *curlcred << endl;
1373 if (curlcred->valid())
1375 credentials = curlcred;
1389 DBG <<
"callback answer: cancel" << endl;
1405 if (credentials->authType() == CURLAUTH_NONE)
1406 credentials->setAuthType(availAuthTypes);
1409 if (credentials->authType() != CURLAUTH_NONE)
1413 ret = curl_easy_setopt(
_curl, CURLOPT_HTTPAUTH, credentials->authType());
1419 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.