16 #include <zypp/base/Logger.h> 17 #include <zypp/ExternalProgram.h> 18 #include <zypp/base/String.h> 19 #include <zypp/base/Gettext.h> 21 #include <zypp/base/Gettext.h> 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)
116 curl_version_info_data *curl_info = NULL;
117 curl_info = curl_version_info(CURLVERSION_NOW);
119 if (curl_info->protocols)
121 const char *
const *proto;
124 for(proto=curl_info->protocols; !found && *proto; ++proto)
126 if( scheme == std::string((
const char *)*proto))
131 std::string msg(
"Unsupported protocol '");
145 curl_easy_setopt(
_curl, CURLOPT_VERBOSE, 1L);
153 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_ERRORBUFFER,
_curlError );
170 vol_settings.
addHeader(anonymousIdHeader());
171 vol_settings.
addHeader(distributionFlavorHeader());
201 case 4:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
break;
202 case 6:
SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
break;
225 #if CURLVERSION_AT_LEAST(7,19,4) 228 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS );
230 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS );
248 #ifdef CURLSSLOPT_ALLOW_BEAST 250 ret = curl_easy_setopt(
_curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
259 SET_OPTION(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
277 if ( cred && cred->valid() ) {
295 if (use_auth.empty())
296 use_auth =
"digest,basic";
298 if( auth != CURLAUTH_NONE)
300 DBG <<
"Enabling HTTP authentication methods: " << use_auth
301 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
310 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
320 if ( proxyuserpwd.empty() )
325 DBG <<
"Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
329 DBG <<
"Proxy: using proxy-user from ~/.curlrc" << endl;
337 if ( ! proxyuserpwd.empty() )
342 #if CURLVERSION_AT_LEAST(7,19,4) 347 DBG <<
"Proxy: explicitly NOPROXY" << endl;
353 DBG <<
"Proxy: not explicitly set" << endl;
354 DBG <<
"Proxy: libcurl may look into the environment" << endl;
365 #if CURLVERSION_AT_LEAST(7,15,5) 378 MIL <<
"No cookies requested" << endl;
383 #if CURLVERSION_AT_LEAST(7,18,0) 388 for (
const auto &header : vol_settings.
headers() )
418 _curl = curl_easy_init();
455 curl_easy_cleanup(
_curl );
495 const auto &filename = srcFile.
filename();
574 bool timeout_reached)
const 579 if (filename.
empty())
588 case CURLE_UNSUPPORTED_PROTOCOL:
589 err =
" Unsupported protocol";
592 err +=
" or redirect (";
597 case CURLE_URL_MALFORMAT:
598 case CURLE_URL_MALFORMAT_USER:
601 case CURLE_LOGIN_DENIED:
605 case CURLE_HTTP_RETURNED_ERROR:
607 long httpReturnCode = 0;
608 CURLcode infoRet = curl_easy_getinfo(
_curl,
609 CURLINFO_RESPONSE_CODE,
611 if ( infoRet == CURLE_OK )
613 std::string msg =
"HTTP response: " +
str::numstring( httpReturnCode );
614 switch ( httpReturnCode )
620 DBG << msg <<
" Login failed (URL: " <<
url.
asString() <<
")" << std::endl;
621 DBG <<
"MediaUnauthorizedException auth hint: '" << auth_hint <<
"'" << std::endl;
636 if (
url.
getHost().find(
".suse.com") != std::string::npos )
637 msg403 =
_(
"Visit the SUSE Customer Center to check whether your registration is valid and has not expired.");
638 else if (
url.
asString().find(
"novell.com") != std::string::npos)
639 msg403 =
_(
"Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
652 std::string msg =
"Unable to retrieve HTTP response:";
658 case CURLE_FTP_COULDNT_RETR_FILE:
659 #if CURLVERSION_AT_LEAST(7,16,0) 660 case CURLE_REMOTE_FILE_NOT_FOUND:
662 case CURLE_FTP_ACCESS_DENIED:
663 case CURLE_TFTP_NOTFOUND:
664 err =
"File not found";
667 case CURLE_BAD_PASSWORD_ENTERED:
668 case CURLE_FTP_USER_PASSWORD_INCORRECT:
669 err =
"Login failed";
671 case CURLE_COULDNT_RESOLVE_PROXY:
672 case CURLE_COULDNT_RESOLVE_HOST:
673 case CURLE_COULDNT_CONNECT:
674 case CURLE_FTP_CANT_GET_HOST:
675 err =
"Connection failed";
677 case CURLE_WRITE_ERROR:
680 case CURLE_PARTIAL_FILE:
681 case CURLE_OPERATION_TIMEDOUT:
682 timeout_reached =
true;
684 case CURLE_ABORTED_BY_CALLBACK:
685 if( timeout_reached )
687 err =
"Timeout reached";
695 case CURLE_SSL_PEER_CERTIFICATE:
741 std::string urlBuffer( curlUrl.
asString());
742 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
755 ret = curl_easy_setopt(
_curl, CURLOPT_NOBODY, 1L );
757 ret = curl_easy_setopt(
_curl, CURLOPT_RANGE,
"0-1" );
760 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
761 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
767 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
771 AutoFILE file { ::fopen(
"/dev/null",
"w" ) };
773 ERR <<
"fopen failed for /dev/null" << endl;
774 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
775 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
781 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
788 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, (*file) );
791 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL );
792 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
798 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L );
805 CURLcode ok = curl_easy_perform(
_curl );
806 MIL <<
"perform code: " << ok <<
" [ " << curl_easy_strerror(ok) <<
" ]" << endl;
811 curl_easy_setopt(
_curl, CURLOPT_NOBODY, 0L);
821 curl_easy_setopt(
_curl, CURLOPT_HTTPGET, 1L);
830 curl_easy_setopt(
_curl, CURLOPT_RANGE, NULL);
851 return ( ok == CURLE_OK );
870 bool not_a_file =
false;
872 CURLcode ret = curl_easy_getinfo(
_curl,
873 CURLINFO_EFFECTIVE_URL,
875 if ( ret == CURLE_OK && ptr != NULL)
880 std::string path( eurl.getPathName());
881 if( !path.empty() && path !=
"/" && *path.rbegin() ==
'/')
883 DBG <<
"Effective url (" 885 <<
") seems to provide the index of a directory" 904 DBG <<
"assert_dir " << dest.
dirname() <<
" failed" << endl;
914 ERR <<
"out of memory for temp file name" << endl;
918 AutoFD tmp_fd { ::mkostemp( buf, O_CLOEXEC ) };
921 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
926 file = ::fdopen( tmp_fd,
"we" );
929 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
932 tmp_fd.resetDispose();
935 DBG <<
"dest: " << dest << endl;
936 DBG <<
"temp: " << destNew << endl;
941 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
942 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)
PathInfo(target).mtime());
946 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
947 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
955 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
956 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
960 long httpReturnCode = 0;
961 CURLcode infoRet = curl_easy_getinfo(
_curl,
962 CURLINFO_RESPONSE_CODE,
964 bool modified =
true;
965 if (infoRet == CURLE_OK)
968 if ( httpReturnCode == 304
971 DBG <<
" Not modified.";
978 WAR <<
"Could not get the reponse code." << endl;
981 if (modified || infoRet != CURLE_OK)
986 ERR <<
"Failed to chmod file " << destNew << endl;
990 if ( ::fclose( file ) )
992 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
997 if (
rename( destNew, dest ) != 0 ) {
998 ERR <<
"Rename failed" << endl;
1001 destNew.resetDispose();
1037 std::string urlBuffer( curlUrl.
asString());
1038 CURLcode ret = curl_easy_setopt(
_curl, CURLOPT_URL,
1039 urlBuffer.c_str() );
1044 ret = curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, file );
1052 report->start(
url, dest);
1053 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
1054 WAR <<
"Can't set CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1057 ret = curl_easy_perform(
_curl );
1058 #if CURLVERSION_AT_LEAST(7,19,4) 1063 if ( ftell(file) == 0 && ret == 0 )
1065 long httpReturnCode = 33;
1066 if ( curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
1068 long conditionUnmet = 33;
1069 if ( curl_easy_getinfo(
_curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
1071 WAR <<
"TIMECONDITION unmet - retry without." << endl;
1072 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1073 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1074 ret = curl_easy_perform(
_curl );
1080 if ( curl_easy_setopt(
_curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
1081 WAR <<
"Can't unset CURLOPT_PROGRESSDATA: " <<
_curlError << endl;;
1087 <<
", temp file size " << ftell(file)
1088 <<
" bytes." << endl;
1095 if ( progressData.fileSizeExceeded )
1106 #if DETECT_DIR_INDEX 1111 #endif // DETECT_DIR_INDEX 1121 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1122 Pathname filename = dirname + it->name;
1125 switch ( it->type ) {
1132 getDir( filename, recurse_r );
1136 WAR <<
"Ignore error (" << res <<
") on creating local directory '" <<
localPath( filename ) <<
"'" << endl;
1150 const Pathname & dirname,
bool dots )
const 1158 const Pathname & dirname,
bool dots )
const 1185 long httpReturnCode = 0;
1186 if ( curl_easy_getinfo( pdata->
curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0 )
1187 return aliveCallback( clientp, dltotal, dlnow, ultotal, ulnow );
1198 return pdata ? pdata->
curl : 0;
1205 long auth_info = CURLAUTH_NONE;
1208 curl_easy_getinfo(
_curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1210 if(infoRet == CURLE_OK)
1241 if (cmcred && firstTry)
1244 DBG <<
"got stored credentials:" << endl << *credentials << endl;
1259 curlcred->setUsername(cmcred->username());
1268 curlcred->setAuthType(availAuthTypes);
1271 if (auth_report->prompt(
_url, prompt_msg, *curlcred))
1273 DBG <<
"callback answer: retry" << endl
1274 <<
"CurlAuthData: " << *curlcred << endl;
1276 if (curlcred->valid())
1278 credentials = curlcred;
1292 DBG <<
"callback answer: cancel" << endl;
1308 if (credentials->authType() == CURLAUTH_NONE)
1309 credentials->setAuthType(availAuthTypes);
1312 if (credentials->authType() != CURLAUTH_NONE)
1316 ret = curl_easy_setopt(
_curl, CURLOPT_HTTPAUTH, credentials->authType());
1322 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.
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
int ZYPP_MEDIA_CURL_IPRESOLVE()
4/6 to force IPv4/v6
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
long ZYPP_MEDIA_CURL_DEBUG()
Long number for setting CURLOPT_DEBUGDATA.
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
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.