34 #include <sys/types.h> 36 #include <sys/mount.h> 50 Pathname MediaCurl::_cookieFile =
"/var/lib/YaST2/cookies";
55 #define SET_OPTION(opt,val) do { \ 56 ret = curl_easy_setopt ( _curl, opt, val ); \ 58 ZYPP_THROW(MediaCurlSetOptException(_url, _curlError)); \ 62 #define SET_OPTION_OFFT(opt,val) SET_OPTION(opt,(curl_off_t)val) 63 #define SET_OPTION_LONG(opt,val) SET_OPTION(opt,(long)val) 64 #define SET_OPTION_VOID(opt,val) SET_OPTION(opt,(void*)val) 66 MediaCurl::MediaCurl(
const Url & url_r,
67 const Pathname & attach_point_hint_r )
77 MIL <<
"MediaCurl::MediaCurl(" << url_r <<
", " << attach_point_hint_r <<
")" << endl;
85 char *atemp = ::strdup( apath.
asString().c_str());
88 atemp == NULL || (atest=::mkdtemp(atemp)) == NULL)
90 WAR <<
"attach point " << ainfo.
path()
91 <<
" is not useable for " << url_r.
getScheme() << endl;
94 else if( atest != NULL)
122 curl_version_info_data *curl_info = NULL;
123 curl_info = curl_version_info(CURLVERSION_NOW);
125 if (curl_info->protocols)
127 const char *
const *proto;
130 for(proto=curl_info->protocols; !found && *proto; ++proto)
132 if( scheme == std::string((
const char *)*proto))
137 std::string msg(
"Unsupported protocol '");
148 char *ptr = getenv(
"ZYPP_MEDIA_CURL_DEBUG");
149 _curlDebug = (ptr && *ptr) ? str::strtonum<long>( ptr) : 0L;
152 curl_easy_setopt(
_curl, CURLOPT_VERBOSE, 1L);
160 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_ERRORBUFFER,
_curlError );
207 case 4:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
break;
208 case 6:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
break;
232 #if CURLVERSION_AT_LEAST(7,19,4) 234 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS );
252 #ifdef CURLSSLOPT_ALLOW_BEAST 254 ret = curl_easy_setopt(
_curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
263 SET_OPTION(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
281 if ( cred && cred->valid() ) {
299 if (use_auth.empty())
300 use_auth =
"digest,basic";
302 if( auth != CURLAUTH_NONE)
304 DBG <<
"Enabling HTTP authentication methods: " << use_auth
305 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
314 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
324 if ( proxyuserpwd.empty() )
329 DBG <<
"Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
333 DBG <<
"Proxy: using proxy-user from ~/.curlrc" << endl;
341 if ( ! proxyuserpwd.empty() )
346 #if CURLVERSION_AT_LEAST(7,19,4) 351 DBG <<
"Proxy: explicitly NOPROXY" << endl;
357 DBG <<
"Proxy: not explicitly set" << endl;
358 DBG <<
"Proxy: libcurl may look into the environment" << endl;
369 #if CURLVERSION_AT_LEAST(7,15,5) 382 MIL <<
"No cookies requested" << endl;
387 #if CURLVERSION_AT_LEAST(7,18,0) 392 for (
const auto &header : vol_settings.
headers() )
422 _curl = curl_easy_init();
459 curl_easy_cleanup(
_curl );
575 bool timeout_reached)
const 580 if (filename.
empty())
589 case CURLE_UNSUPPORTED_PROTOCOL:
590 err =
" Unsupported protocol";
593 err +=
" or redirect (";
598 case CURLE_URL_MALFORMAT:
599 case CURLE_URL_MALFORMAT_USER:
602 case CURLE_LOGIN_DENIED:
606 case CURLE_HTTP_RETURNED_ERROR:
608 long httpReturnCode = 0;
609 CURLcode infoRet = curl_easy_getinfo(
_curl,
610 CURLINFO_RESPONSE_CODE,
612 if ( infoRet == CURLE_OK )
614 std::string msg =
"HTTP response: " +
str::numstring( httpReturnCode );
615 switch ( httpReturnCode )
621 DBG << msg <<
" Login failed (URL: " <<
url.
asString() <<
")" << std::endl;
622 DBG <<
"MediaUnauthorizedException auth hint: '" << auth_hint <<
"'" << std::endl;
637 if (
url.
getHost().find(
".suse.com") != std::string::npos )
638 msg403 =
_(
"Visit the SUSE Customer Center to check whether your registration is valid and has not expired.");
639 else if (
url.
asString().find(
"novell.com") != std::string::npos)
640 msg403 =
_(
"Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
653 std::string msg =
"Unable to retrieve HTTP response:";
659 case CURLE_FTP_COULDNT_RETR_FILE:
660 #if CURLVERSION_AT_LEAST(7,16,0) 661 case CURLE_REMOTE_FILE_NOT_FOUND:
663 case CURLE_FTP_ACCESS_DENIED:
664 case CURLE_TFTP_NOTFOUND:
665 err =
"File not found";
668 case CURLE_BAD_PASSWORD_ENTERED:
669 case CURLE_FTP_USER_PASSWORD_INCORRECT:
670 err =
"Login failed";
672 case CURLE_COULDNT_RESOLVE_PROXY:
673 case CURLE_COULDNT_RESOLVE_HOST:
674 case CURLE_COULDNT_CONNECT:
675 case CURLE_FTP_CANT_GET_HOST:
676 err =
"Connection failed";
678 case CURLE_WRITE_ERROR:
681 case CURLE_PARTIAL_FILE:
682 case CURLE_OPERATION_TIMEDOUT:
683 timeout_reached =
true;
685 case CURLE_ABORTED_BY_CALLBACK:
686 if( timeout_reached )
688 err =
"Timeout reached";
696 case CURLE_SSL_PEER_CERTIFICATE:
742 std::string urlBuffer( curlUrl.
asString());
743 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
756 ret = curl_easy_setopt(
_curl, CURLOPT_NOBODY, 1L );
758 ret = curl_easy_setopt(
_curl, CURLOPT_RANGE,
"0-1" );
761 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
762 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
768 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
772 AutoFILE file { ::fopen(
"/dev/null",
"w" ) };
774 ERR <<
"fopen failed for /dev/null" << endl;
775 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
776 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
782 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
789 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, (*file) );
792 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
793 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
799 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
806 CURLcode ok = curl_easy_perform(
_curl );
807 MIL <<
"perform code: " << ok <<
" [ " << curl_easy_strerror(ok) <<
" ]" << endl;
812 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
822 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L);
831 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL);
852 return ( ok == CURLE_OK );
871 bool not_a_file =
false;
873 CURLcode ret = curl_easy_getinfo(
_curl,
874 CURLINFO_EFFECTIVE_URL,
876 if ( ret == CURLE_OK && ptr != NULL)
881 std::string path( eurl.getPathName());
882 if( !path.empty() && path !=
"/" && *path.rbegin() ==
'/')
884 DBG <<
"Effective url (" 886 <<
") seems to provide the index of a directory" 905 DBG <<
"assert_dir " << dest.
dirname() <<
" failed" << endl;
915 ERR <<
"out of memory for temp file name" << endl;
919 AutoFD tmp_fd { ::mkostemp( buf, O_CLOEXEC ) };
922 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
927 file = ::fdopen( tmp_fd,
"we" );
930 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
933 tmp_fd.resetDispose();
936 DBG <<
"dest: " << dest << endl;
937 DBG <<
"temp: " << destNew << endl;
942 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
943 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)
PathInfo(target).mtime());
947 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
948 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
956 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
957 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
961 long httpReturnCode = 0;
962 CURLcode infoRet = curl_easy_getinfo(
_curl,
963 CURLINFO_RESPONSE_CODE,
965 bool modified =
true;
966 if (infoRet == CURLE_OK)
969 if ( httpReturnCode == 304
972 DBG <<
" Not modified.";
979 WAR <<
"Could not get the reponse code." << endl;
982 if (modified || infoRet != CURLE_OK)
987 ERR <<
"Failed to chmod file " << destNew << endl;
991 if ( ::fclose( file ) )
993 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
998 if (
rename( destNew, dest ) != 0 ) {
999 ERR <<
"Rename failed" << endl;
1002 destNew.resetDispose();
1038 std::string urlBuffer( curlUrl.
asString());
1039 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
1040 urlBuffer.c_str() );
1045 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, file );
1053 report->start(
url, dest);
1054 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
1055 WAR <<
"Can't set CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1058 ret = curl_easy_perform(
_curl );
1059 #if CURLVERSION_AT_LEAST(7,19,4) 1064 if ( ftell(file) == 0 && ret == 0 )
1066 long httpReturnCode = 33;
1067 if ( curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
1069 long conditionUnmet = 33;
1070 if ( curl_easy_getinfo(
_curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
1072 WAR <<
"TIMECONDITION unmet - retry without." << endl;
1073 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1074 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1075 ret = curl_easy_perform(
_curl );
1081 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
1082 WAR <<
"Can't unset CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1088 <<
", temp file size " << ftell(file)
1089 <<
" bytes." << endl;
1096 if ( progressData.fileSizeExceeded )
1107 #if DETECT_DIR_INDEX 1112 #endif // DETECT_DIR_INDEX 1122 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1123 Pathname filename = dirname + it->name;
1126 switch ( it->type ) {
1133 getDir( filename, recurse_r );
1137 WAR <<
"Ignore error (" << res <<
") on creating local directory '" <<
localPath( filename ) <<
"'" << endl;
1151 const Pathname & dirname,
bool dots )
const 1159 const Pathname & dirname,
bool dots )
const 1186 long httpReturnCode = 0;
1187 if ( curl_easy_getinfo( pdata->
curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0 )
1188 return aliveCallback( clientp, dltotal, dlnow, ultotal, ulnow );
1199 return pdata ? pdata->
curl : 0;
1206 long auth_info = CURLAUTH_NONE;
1209 curl_easy_getinfo(
_curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1211 if(infoRet == CURLE_OK)
1242 if (cmcred && firstTry)
1245 DBG <<
"got stored credentials:" << endl << *credentials << endl;
1260 curlcred->setUsername(cmcred->username());
1269 curlcred->setAuthType(availAuthTypes);
1272 if (auth_report->prompt(
_url, prompt_msg, *curlcred))
1274 DBG <<
"callback answer: retry" << endl
1275 <<
"CurlAuthData: " << *curlcred << endl;
1277 if (curlcred->valid())
1279 credentials = curlcred;
1293 DBG <<
"callback answer: cancel" << endl;
1309 if (credentials->authType() == CURLAUTH_NONE)
1310 credentials->setAuthType(availAuthTypes);
1313 if (credentials->authType() != CURLAUTH_NONE)
1317 ret = curl_easy_setopt(
_curl, CURLOPT_HTTPAUTH, credentials->authType());
1323 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.
int reportProgress() const
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.
zypp::ByteCount _expectedFileSize
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 updateStats(double dltotal=0.0, double dlnow=0.0)
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.