1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaCurl.cc
16 #include "zypp/base/Logger.h"
17 #include "zypp/ExternalProgram.h"
18 #include "zypp/base/String.h"
19 #include "zypp/base/Gettext.h"
20 #include "zypp/base/Sysconfig.h"
21 #include "zypp/base/Gettext.h"
23 #include "zypp/media/MediaCurl.h"
24 #include "zypp/media/ProxyInfo.h"
25 #include "zypp/media/MediaUserAuth.h"
26 #include "zypp/media/CredentialManager.h"
27 #include "zypp/media/CurlConfig.h"
28 #include "zypp/thread/Once.h"
29 #include "zypp/Target.h"
30 #include "zypp/ZYppFactory.h"
31 #include "zypp/ZConfig.h"
34 #include <sys/types.h>
36 #include <sys/mount.h>
41 #define DETECT_DIR_INDEX 0
42 #define CONNECT_TIMEOUT 60
43 #define TRANSFER_TIMEOUT_MAX 60 * 60
45 #define EXPLICITLY_NO_PROXY "_none_"
47 #undef CURLVERSION_AT_LEAST
48 #define CURLVERSION_AT_LEAST(M,N,O) LIBCURL_VERSION_NUM >= ((((M)<<8)+(N))<<8)+(O)
51 using namespace zypp::base;
55 zypp::thread::OnceFlag g_InitOnceFlag = PTHREAD_ONCE_INIT;
56 zypp::thread::OnceFlag g_FreeOnceFlag = PTHREAD_ONCE_INIT;
58 extern "C" void _do_free_once()
60 curl_global_cleanup();
63 extern "C" void globalFreeOnce()
65 zypp::thread::callOnce(g_FreeOnceFlag, _do_free_once);
68 extern "C" void _do_init_once()
70 CURLcode ret = curl_global_init( CURL_GLOBAL_ALL );
73 WAR << "curl global init failed" << endl;
77 // register at exit handler ?
78 // this may cause trouble, because we can protect it
79 // against ourself only.
80 // if the app sets an atexit handler as well, it will
81 // cause a double free while the second of them runs.
83 //std::atexit( globalFreeOnce);
86 inline void globalInitOnce()
88 zypp::thread::callOnce(g_InitOnceFlag, _do_init_once);
91 int log_curl(CURL *curl, curl_infotype info,
92 char *ptr, size_t len, void *max_lvl)
98 case CURLINFO_TEXT: lvl = 1; pfx = "*"; break;
99 case CURLINFO_HEADER_IN: lvl = 2; pfx = "<"; break;
100 case CURLINFO_HEADER_OUT: lvl = 2; pfx = ">"; break;
103 if( lvl > 0 && max_lvl != NULL && lvl <= *((long *)max_lvl))
105 std::string msg(ptr, len);
106 std::list<std::string> lines;
107 std::list<std::string>::const_iterator line;
108 zypp::str::split(msg, std::back_inserter(lines), "\r\n");
109 for(line = lines.begin(); line != lines.end(); ++line)
111 DBG << pfx << " " << *line << endl;
117 static size_t log_redirects_curl( char *ptr, size_t size, size_t nmemb, void *userdata)
119 // INT << "got header: " << string(ptr, ptr + size*nmemb) << endl;
121 char * lstart = ptr, * lend = ptr;
123 size_t max = size * nmemb;
124 while (pos + 1 < max)
127 for (lstart = lend; *lend != '\n' && pos < max; ++lend, ++pos);
129 // look for "Location"
130 if ( lstart[0] == 'L'
138 && lstart[8] == ':' )
140 std::string line { lstart, *(lend-1)=='\r' ? lend-1 : lend };
141 DBG << "redirecting to " << line << endl;
143 *reinterpret_cast<std::string *>( userdata ) = line;
148 // continue with the next line
164 ///////////////////////////////////////////////////////////////////
169 inline int getZYPP_MEDIA_CURL_IPRESOLVE()
172 if ( const char * envp = getenv( "ZYPP_MEDIA_CURL_IPRESOLVE" ) )
174 WAR << "env set: $ZYPP_MEDIA_CURL_IPRESOLVE='" << envp << "'" << endl;
175 if ( strcmp( envp, "4" ) == 0 ) ret = 4;
176 else if ( strcmp( envp, "6" ) == 0 ) ret = 6;
182 inline int ZYPP_MEDIA_CURL_IPRESOLVE()
184 static int _v = getZYPP_MEDIA_CURL_IPRESOLVE();
188 ///////////////////////////////////////////////////////////////////
195 ProgressData( CURL *_curl, time_t _timeout = 0, const Url & _url = Url(),
196 ByteCount expectedFileSize_r = 0,
197 callback::SendReport<DownloadProgressReport> *_report = nullptr )
200 , timeout( _timeout )
202 , fileSizeExceeded ( false )
204 , _expectedFileSize( expectedFileSize_r )
211 bool fileSizeExceeded;
212 callback::SendReport<DownloadProgressReport> *report;
213 ByteCount _expectedFileSize;
215 time_t _timeStart = 0; ///< Start total stats
216 time_t _timeLast = 0; ///< Start last period(~1sec)
217 time_t _timeRcv = 0; ///< Start of no-data timeout
218 time_t _timeNow = 0; ///< Now
220 double _dnlTotal = 0.0; ///< Bytes to download or 0 if unknown
221 double _dnlLast = 0.0; ///< Bytes downloaded at period start
222 double _dnlNow = 0.0; ///< Bytes downloaded now
224 int _dnlPercent= 0; ///< Percent completed or 0 if _dnlTotal is unknown
226 double _drateTotal= 0.0; ///< Download rate so far
227 double _drateLast = 0.0; ///< Download rate in last period
229 void updateStats( double dltotal = 0.0, double dlnow = 0.0 )
231 time_t now = _timeNow = time(0);
233 // If called without args (0.0), recompute based on the last values seen
234 if ( dltotal && dltotal != _dnlTotal )
237 if ( dlnow && dlnow != _dnlNow )
242 else if ( !_dnlNow && !_dnlTotal )
244 // Start time counting as soon as first data arrives.
245 // Skip the connection / redirection time at begin.
249 // init or reset if time jumps back
250 if ( !_timeStart || _timeStart > now )
251 _timeStart = _timeLast = _timeRcv = now;
255 reached = ( (now - _timeRcv) > timeout );
257 // check if the downloaded data is already bigger than what we expected
258 fileSizeExceeded = _expectedFileSize > 0 && _expectedFileSize < static_cast<ByteCount::SizeType>(_dnlNow);
262 _dnlPercent = int(_dnlNow * 100 / _dnlTotal);
265 _drateTotal = _dnlNow / std::max( int(now - _timeStart), 1 );
267 if ( _timeLast < now )
269 _drateLast = (_dnlNow - _dnlLast) / int(now - _timeLast);
274 else if ( _timeStart == _timeLast )
275 _drateLast = _drateTotal;
278 int reportProgress() const
280 if ( fileSizeExceeded )
283 return 1; // no-data timeout
284 if ( report && !(*report)->progress( _dnlPercent, url, _drateTotal, _drateLast ) )
285 return 1; // user requested abort
290 // download rate of the last period (cca 1 sec)
292 // bytes downloaded at the start of the last period
294 // seconds from the start of the download
296 // average download rate
298 // last time the progress was reported
300 // bytes downloaded at the moment the progress was last reported
302 // bytes uploaded at the moment the progress was last reported
306 ///////////////////////////////////////////////////////////////////
308 inline void escape( string & str_r,
309 const char char_r, const string & escaped_r ) {
310 for ( string::size_type pos = str_r.find( char_r );
311 pos != string::npos; pos = str_r.find( char_r, pos ) ) {
312 str_r.replace( pos, 1, escaped_r );
316 inline string escapedPath( string path_r ) {
317 escape( path_r, ' ', "%20" );
321 inline string unEscape( string text_r ) {
322 char * tmp = curl_unescape( text_r.c_str(), 0 );
331 * Fills the settings structure using options passed on the url
332 * for example ?timeout=x&proxy=foo
334 void fillSettingsFromUrl( const Url &url, TransferSettings &s )
336 std::string param(url.getQueryParam("timeout"));
339 long num = str::strtonum<long>(param);
340 if( num >= 0 && num <= TRANSFER_TIMEOUT_MAX)
344 if ( ! url.getUsername().empty() )
346 s.setUsername(url.getUsername());
347 if ( url.getPassword().size() )
348 s.setPassword(url.getPassword());
352 // if there is no username, set anonymous auth
353 if ( ( url.getScheme() == "ftp" || url.getScheme() == "tftp" ) && s.username().empty() )
354 s.setAnonymousAuth();
357 if ( url.getScheme() == "https" )
359 s.setVerifyPeerEnabled(false);
360 s.setVerifyHostEnabled(false);
362 std::string verify( url.getQueryParam("ssl_verify"));
363 if( verify.empty() ||
366 s.setVerifyPeerEnabled(true);
367 s.setVerifyHostEnabled(true);
369 else if( verify == "no")
371 s.setVerifyPeerEnabled(false);
372 s.setVerifyHostEnabled(false);
376 std::vector<std::string> flags;
377 std::vector<std::string>::const_iterator flag;
378 str::split( verify, std::back_inserter(flags), ",");
379 for(flag = flags.begin(); flag != flags.end(); ++flag)
382 s.setVerifyHostEnabled(true);
383 else if( *flag == "peer")
384 s.setVerifyPeerEnabled(true);
386 ZYPP_THROW(MediaBadUrlException(url, "Unknown ssl_verify flag"));
391 Pathname ca_path( url.getQueryParam("ssl_capath") );
392 if( ! ca_path.empty())
394 if( !PathInfo(ca_path).isDir() || ! ca_path.absolute())
395 ZYPP_THROW(MediaBadUrlException(url, "Invalid ssl_capath path"));
397 s.setCertificateAuthoritiesPath(ca_path);
400 Pathname client_cert( url.getQueryParam("ssl_clientcert") );
401 if( ! client_cert.empty())
403 if( !PathInfo(client_cert).isFile() || !client_cert.absolute())
404 ZYPP_THROW(MediaBadUrlException(url, "Invalid ssl_clientcert file"));
406 s.setClientCertificatePath(client_cert);
408 Pathname client_key( url.getQueryParam("ssl_clientkey") );
409 if( ! client_key.empty())
411 if( !PathInfo(client_key).isFile() || !client_key.absolute())
412 ZYPP_THROW(MediaBadUrlException(url, "Invalid ssl_clientkey file"));
414 s.setClientKeyPath(client_key);
417 param = url.getQueryParam( "proxy" );
418 if ( ! param.empty() )
420 if ( param == EXPLICITLY_NO_PROXY ) {
421 // Workaround TransferSettings shortcoming: With an
422 // empty proxy string, code will continue to look for
423 // valid proxy settings. So set proxy to some non-empty
424 // string, to indicate it has been explicitly disabled.
425 s.setProxy(EXPLICITLY_NO_PROXY);
426 s.setProxyEnabled(false);
429 string proxyport( url.getQueryParam( "proxyport" ) );
430 if ( ! proxyport.empty() ) {
431 param += ":" + proxyport;
434 s.setProxyEnabled(true);
438 param = url.getQueryParam( "proxyuser" );
439 if ( ! param.empty() )
441 s.setProxyUsername(param);
442 s.setProxyPassword(url.getQueryParam( "proxypass" ));
445 // HTTP authentication type
446 param = url.getQueryParam("auth");
447 if (!param.empty() && (url.getScheme() == "http" || url.getScheme() == "https"))
451 CurlAuthData::auth_type_str2long(param); // check if we know it
453 catch (MediaException & ex_r)
455 DBG << "Rethrowing as MediaUnauthorizedException.";
456 ZYPP_THROW(MediaUnauthorizedException(url, ex_r.msg(), "", ""));
458 s.setAuthType(param);
462 param = url.getQueryParam("head_requests");
463 if( !param.empty() && param == "no" )
464 s.setHeadRequestsAllowed(false);
468 * Reads the system proxy configuration and fills the settings
469 * structure proxy information
471 void fillSettingsSystemProxy( const Url&url, TransferSettings &s )
473 ProxyInfo proxy_info;
474 if ( proxy_info.useProxyFor( url ) )
476 // We must extract any 'user:pass' from the proxy url
477 // otherwise they won't make it into curl (.curlrc wins).
479 Url u( proxy_info.proxy( url ) );
480 s.setProxy( u.asString( url::ViewOption::WITH_SCHEME + url::ViewOption::WITH_HOST + url::ViewOption::WITH_PORT ) );
481 // don't overwrite explicit auth settings
482 if ( s.proxyUsername().empty() )
484 s.setProxyUsername( u.getUsername( url::E_ENCODED ) );
485 s.setProxyPassword( u.getPassword( url::E_ENCODED ) );
487 s.setProxyEnabled( true );
489 catch (...) {} // no proxy if URL is malformed
493 Pathname MediaCurl::_cookieFile = "/var/lib/YaST2/cookies";
496 * initialized only once, this gets the anonymous id
497 * from the target, which we pass in the http header
499 static const char *const anonymousIdHeader()
501 // we need to add the release and identifier to the
503 // The target could be not initialized, and then this information
505 static const std::string _value(
506 str::trim( str::form(
507 "X-ZYpp-AnonymousId: %s",
508 Target::anonymousUniqueId( Pathname()/*guess root*/ ).c_str() ) )
510 return _value.c_str();
514 * initialized only once, this gets the distribution flavor
515 * from the target, which we pass in the http header
517 static const char *const distributionFlavorHeader()
519 // we need to add the release and identifier to the
521 // The target could be not initialized, and then this information
523 static const std::string _value(
524 str::trim( str::form(
525 "X-ZYpp-DistributionFlavor: %s",
526 Target::distributionFlavor( Pathname()/*guess root*/ ).c_str() ) )
528 return _value.c_str();
532 * initialized only once, this gets the agent string
533 * which also includes the curl version
535 static const char *const agentString()
537 // we need to add the release and identifier to the
539 // The target could be not initialized, and then this information
541 static const std::string _value(
543 "ZYpp %s (curl %s) %s"
545 , curl_version_info(CURLVERSION_NOW)->version
546 , Target::targetDistribution( Pathname()/*guess root*/ ).c_str()
549 return _value.c_str();
552 // we use this define to unbloat code as this C setting option
553 // and catching exception is done frequently.
554 /** \todo deprecate SET_OPTION and use the typed versions below. */
555 #define SET_OPTION(opt,val) do { \
556 ret = curl_easy_setopt ( _curl, opt, val ); \
558 ZYPP_THROW(MediaCurlSetOptException(_url, _curlError)); \
562 #define SET_OPTION_OFFT(opt,val) SET_OPTION(opt,(curl_off_t)val)
563 #define SET_OPTION_LONG(opt,val) SET_OPTION(opt,(long)val)
564 #define SET_OPTION_VOID(opt,val) SET_OPTION(opt,(void*)val)
566 MediaCurl::MediaCurl( const Url & url_r,
567 const Pathname & attach_point_hint_r )
568 : MediaHandler( url_r, attach_point_hint_r,
569 "/", // urlpath at attachpoint
570 true ), // does_download
574 _curlError[0] = '\0';
577 MIL << "MediaCurl::MediaCurl(" << url_r << ", " << attach_point_hint_r << ")" << endl;
581 if( !attachPoint().empty())
583 PathInfo ainfo(attachPoint());
584 Pathname apath(attachPoint() + "XXXXXX");
585 char *atemp = ::strdup( apath.asString().c_str());
587 if( !ainfo.isDir() || !ainfo.userMayRWX() ||
588 atemp == NULL || (atest=::mkdtemp(atemp)) == NULL)
590 WAR << "attach point " << ainfo.path()
591 << " is not useable for " << url_r.getScheme() << endl;
592 setAttachPoint("", true);
594 else if( atest != NULL)
602 Url MediaCurl::clearQueryString(const Url &url) const
605 curlUrl.setUsername( "" );
606 curlUrl.setPassword( "" );
607 curlUrl.setPathParams( "" );
608 curlUrl.setFragment( "" );
609 curlUrl.delQueryParam("cookies");
610 curlUrl.delQueryParam("proxy");
611 curlUrl.delQueryParam("proxyport");
612 curlUrl.delQueryParam("proxyuser");
613 curlUrl.delQueryParam("proxypass");
614 curlUrl.delQueryParam("ssl_capath");
615 curlUrl.delQueryParam("ssl_verify");
616 curlUrl.delQueryParam("ssl_clientcert");
617 curlUrl.delQueryParam("timeout");
618 curlUrl.delQueryParam("auth");
619 curlUrl.delQueryParam("username");
620 curlUrl.delQueryParam("password");
621 curlUrl.delQueryParam("mediahandler");
622 curlUrl.delQueryParam("credentials");
623 curlUrl.delQueryParam("head_requests");
627 TransferSettings & MediaCurl::settings()
633 void MediaCurl::setCookieFile( const Pathname &fileName )
635 _cookieFile = fileName;
638 ///////////////////////////////////////////////////////////////////
640 void MediaCurl::checkProtocol(const Url &url) const
642 curl_version_info_data *curl_info = NULL;
643 curl_info = curl_version_info(CURLVERSION_NOW);
644 // curl_info does not need any free (is static)
645 if (curl_info->protocols)
647 const char * const *proto;
648 std::string scheme( url.getScheme());
650 for(proto=curl_info->protocols; !found && *proto; ++proto)
652 if( scheme == std::string((const char *)*proto))
657 std::string msg("Unsupported protocol '");
660 ZYPP_THROW(MediaBadUrlException(_url, msg));
665 void MediaCurl::setupEasy()
668 char *ptr = getenv("ZYPP_MEDIA_CURL_DEBUG");
669 _curlDebug = (ptr && *ptr) ? str::strtonum<long>( ptr) : 0L;
672 curl_easy_setopt( _curl, CURLOPT_VERBOSE, 1L);
673 curl_easy_setopt( _curl, CURLOPT_DEBUGFUNCTION, log_curl);
674 curl_easy_setopt( _curl, CURLOPT_DEBUGDATA, &_curlDebug);
678 curl_easy_setopt(_curl, CURLOPT_HEADERFUNCTION, log_redirects_curl);
679 curl_easy_setopt(_curl, CURLOPT_HEADERDATA, &_lastRedirect);
680 CURLcode ret = curl_easy_setopt( _curl, CURLOPT_ERRORBUFFER, _curlError );
682 ZYPP_THROW(MediaCurlSetOptException(_url, "Error setting error buffer"));
685 SET_OPTION(CURLOPT_FAILONERROR, 1L);
686 SET_OPTION(CURLOPT_NOSIGNAL, 1L);
688 // create non persistant settings
689 // so that we don't add headers twice
690 TransferSettings vol_settings(_settings);
692 // add custom headers for download.opensuse.org (bsc#955801)
693 if ( _url.getHost() == "download.opensuse.org" )
695 vol_settings.addHeader(anonymousIdHeader());
696 vol_settings.addHeader(distributionFlavorHeader());
698 vol_settings.addHeader("Pragma:");
700 _settings.setTimeout(ZConfig::instance().download_transfer_timeout());
701 _settings.setConnectTimeout(CONNECT_TIMEOUT);
703 _settings.setUserAgentString(agentString());
705 // fill some settings from url query parameters
708 fillSettingsFromUrl(_url, _settings);
710 catch ( const MediaException &e )
715 // if the proxy was not set (or explicitly unset) by url, then look...
716 if ( _settings.proxy().empty() )
718 // ...at the system proxy settings
719 fillSettingsSystemProxy(_url, _settings);
723 if ( env::ZYPP_MEDIA_CURL_IPRESOLVE() )
725 switch ( env::ZYPP_MEDIA_CURL_IPRESOLVE() )
727 case 4: SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); break;
728 case 6: SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6); break;
735 SET_OPTION(CURLOPT_CONNECTTIMEOUT, _settings.connectTimeout());
736 // If a transfer timeout is set, also set CURLOPT_TIMEOUT to an upper limit
737 // just in case curl does not trigger its progress callback frequently
739 if ( _settings.timeout() )
741 SET_OPTION(CURLOPT_TIMEOUT, 3600L);
744 // follow any Location: header that the server sends as part of
745 // an HTTP header (#113275)
746 SET_OPTION(CURLOPT_FOLLOWLOCATION, 1L);
747 // 3 redirects seem to be too few in some cases (bnc #465532)
748 SET_OPTION(CURLOPT_MAXREDIRS, 6L);
750 if ( _url.getScheme() == "https" )
752 #if CURLVERSION_AT_LEAST(7,19,4)
753 // restrict following of redirections from https to https only
754 SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS );
757 if( _settings.verifyPeerEnabled() ||
758 _settings.verifyHostEnabled() )
760 SET_OPTION(CURLOPT_CAPATH, _settings.certificateAuthoritiesPath().c_str());
763 if( ! _settings.clientCertificatePath().empty() )
765 SET_OPTION(CURLOPT_SSLCERT, _settings.clientCertificatePath().c_str());
767 if( ! _settings.clientKeyPath().empty() )
769 SET_OPTION(CURLOPT_SSLKEY, _settings.clientKeyPath().c_str());
772 #ifdef CURLSSLOPT_ALLOW_BEAST
774 ret = curl_easy_setopt( _curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
777 ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
780 SET_OPTION(CURLOPT_SSL_VERIFYPEER, _settings.verifyPeerEnabled() ? 1L : 0L);
781 SET_OPTION(CURLOPT_SSL_VERIFYHOST, _settings.verifyHostEnabled() ? 2L : 0L);
782 // bnc#903405 - POODLE: libzypp should only talk TLS
783 SET_OPTION(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
786 SET_OPTION(CURLOPT_USERAGENT, _settings.userAgentString().c_str() );
788 /*---------------------------------------------------------------*
789 CURLOPT_USERPWD: [user name]:[password]
791 Url::username/password -> CURLOPT_USERPWD
792 If not provided, anonymous FTP identification
793 *---------------------------------------------------------------*/
795 if ( _settings.userPassword().size() )
797 SET_OPTION(CURLOPT_USERPWD, _settings.userPassword().c_str());
798 string use_auth = _settings.authType();
799 if (use_auth.empty())
800 use_auth = "digest,basic"; // our default
801 long auth = CurlAuthData::auth_type_str2long(use_auth);
802 if( auth != CURLAUTH_NONE)
804 DBG << "Enabling HTTP authentication methods: " << use_auth
805 << " (CURLOPT_HTTPAUTH=" << auth << ")" << std::endl;
806 SET_OPTION(CURLOPT_HTTPAUTH, auth);
810 if ( _settings.proxyEnabled() && ! _settings.proxy().empty() )
812 DBG << "Proxy: '" << _settings.proxy() << "'" << endl;
813 SET_OPTION(CURLOPT_PROXY, _settings.proxy().c_str());
814 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
815 /*---------------------------------------------------------------*
816 * CURLOPT_PROXYUSERPWD: [user name]:[password]
818 * Url::option(proxyuser and proxypassword) -> CURLOPT_PROXYUSERPWD
819 * If not provided, $HOME/.curlrc is evaluated
820 *---------------------------------------------------------------*/
822 string proxyuserpwd = _settings.proxyUserPassword();
824 if ( proxyuserpwd.empty() )
827 CurlConfig::parseConfig(curlconf); // parse ~/.curlrc
828 if ( curlconf.proxyuserpwd.empty() )
829 DBG << "Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
832 proxyuserpwd = curlconf.proxyuserpwd;
833 DBG << "Proxy: using proxy-user from ~/.curlrc" << endl;
838 DBG << "Proxy: using provided proxy-user '" << _settings.proxyUsername() << "'" << endl;
841 if ( ! proxyuserpwd.empty() )
843 SET_OPTION(CURLOPT_PROXYUSERPWD, unEscape( proxyuserpwd ).c_str());
846 #if CURLVERSION_AT_LEAST(7,19,4)
847 else if ( _settings.proxy() == EXPLICITLY_NO_PROXY )
849 // Explicitly disabled in URL (see fillSettingsFromUrl()).
850 // This should also prevent libcurl from looking into the environment.
851 DBG << "Proxy: explicitly NOPROXY" << endl;
852 SET_OPTION(CURLOPT_NOPROXY, "*");
857 DBG << "Proxy: not explicitly set" << endl;
858 DBG << "Proxy: libcurl may look into the environment" << endl;
862 if ( _settings.minDownloadSpeed() != 0 )
864 SET_OPTION(CURLOPT_LOW_SPEED_LIMIT, _settings.minDownloadSpeed());
865 // default to 10 seconds at low speed
866 SET_OPTION(CURLOPT_LOW_SPEED_TIME, 60L);
869 #if CURLVERSION_AT_LEAST(7,15,5)
870 if ( _settings.maxDownloadSpeed() != 0 )
871 SET_OPTION_OFFT(CURLOPT_MAX_RECV_SPEED_LARGE, _settings.maxDownloadSpeed());
874 /*---------------------------------------------------------------*
875 *---------------------------------------------------------------*/
877 _currentCookieFile = _cookieFile.asString();
878 if ( str::strToBool( _url.getQueryParam( "cookies" ), true ) )
879 SET_OPTION(CURLOPT_COOKIEFILE, _currentCookieFile.c_str() );
881 MIL << "No cookies requested" << endl;
882 SET_OPTION(CURLOPT_COOKIEJAR, _currentCookieFile.c_str() );
883 SET_OPTION(CURLOPT_PROGRESSFUNCTION, &progressCallback );
884 SET_OPTION(CURLOPT_NOPROGRESS, 0L);
886 #if CURLVERSION_AT_LEAST(7,18,0)
888 SET_OPTION(CURLOPT_PROXY_TRANSFER_MODE, 1L );
890 // append settings custom headers to curl
891 for ( TransferSettings::Headers::const_iterator it = vol_settings.headersBegin();
892 it != vol_settings.headersEnd();
895 // MIL << "HEADER " << *it << std::endl;
897 _customHeaders = curl_slist_append(_customHeaders, it->c_str());
898 if ( !_customHeaders )
899 ZYPP_THROW(MediaCurlInitException(_url));
902 SET_OPTION(CURLOPT_HTTPHEADER, _customHeaders);
905 ///////////////////////////////////////////////////////////////////
908 void MediaCurl::attachTo (bool next)
911 ZYPP_THROW(MediaNotSupportedException(_url));
913 if ( !_url.isValid() )
914 ZYPP_THROW(MediaBadUrlException(_url));
917 if( !isUseableAttachPoint( attachPoint() ) )
919 setAttachPoint( createAttachPoint(), true );
922 disconnectFrom(); // clean _curl if needed
923 _curl = curl_easy_init();
925 ZYPP_THROW(MediaCurlInitException(_url));
931 catch (Exception & ex)
937 // FIXME: need a derived class to propelly compare url's
938 MediaSourceRef media( new MediaSource(_url.getScheme(), _url.asString()));
939 setMediaSource(media);
943 MediaCurl::checkAttachPoint(const Pathname &apoint) const
945 return MediaHandler::checkAttachPoint( apoint, true, true);
948 ///////////////////////////////////////////////////////////////////
950 void MediaCurl::disconnectFrom()
952 if ( _customHeaders )
954 curl_slist_free_all(_customHeaders);
960 curl_easy_cleanup( _curl );
965 ///////////////////////////////////////////////////////////////////
967 void MediaCurl::releaseFrom( const std::string & ejectDev )
972 Url MediaCurl::getFileUrl( const Pathname & filename_r ) const
974 // Simply extend the URLs pathname. An 'absolute' URL path
975 // is achieved by encoding the leading '/' in an URL path:
976 // URL: ftp://user@server -> ~user
977 // URL: ftp://user@server/ -> ~user
978 // URL: ftp://user@server// -> ~user
979 // URL: ftp://user@server/%2F -> /
980 // ^- this '/' is just a separator
982 newurl.setPathName( ( Pathname("./"+_url.getPathName()) / filename_r ).asString().substr(1) );
986 ///////////////////////////////////////////////////////////////////
988 void MediaCurl::getFile(const Pathname & filename , const ByteCount &expectedFileSize_r) const
990 // Use absolute file name to prevent access of files outside of the
991 // hierarchy below the attach point.
992 getFileCopy(filename, localPath(filename).absolutename(), expectedFileSize_r);
995 ///////////////////////////////////////////////////////////////////
997 void MediaCurl::getFileCopy( const Pathname & filename , const Pathname & target, const ByteCount &expectedFileSize_r ) const
999 callback::SendReport<DownloadProgressReport> report;
1001 Url fileurl(getFileUrl(filename));
1009 doGetFileCopy(filename, target, report, expectedFileSize_r);
1012 // retry with proper authentication data
1013 catch (MediaUnauthorizedException & ex_r)
1015 if(authenticate(ex_r.hint(), !retry))
1019 report->finish(fileurl, zypp::media::DownloadProgressReport::ACCESS_DENIED, ex_r.asUserHistory());
1023 // unexpected exception
1024 catch (MediaException & excpt_r)
1026 media::DownloadProgressReport::Error reason = media::DownloadProgressReport::ERROR;
1027 if( typeid(excpt_r) == typeid( media::MediaFileNotFoundException ) ||
1028 typeid(excpt_r) == typeid( media::MediaNotAFileException ) )
1030 reason = media::DownloadProgressReport::NOT_FOUND;
1032 report->finish(fileurl, reason, excpt_r.asUserHistory());
1033 ZYPP_RETHROW(excpt_r);
1038 report->finish(fileurl, zypp::media::DownloadProgressReport::NO_ERROR, "");
1041 ///////////////////////////////////////////////////////////////////
1043 bool MediaCurl::getDoesFileExist( const Pathname & filename ) const
1051 return doGetDoesFileExist( filename );
1053 // authentication problem, retry with proper authentication data
1054 catch (MediaUnauthorizedException & ex_r)
1056 if(authenticate(ex_r.hint(), !retry))
1061 // unexpected exception
1062 catch (MediaException & excpt_r)
1064 ZYPP_RETHROW(excpt_r);
1072 ///////////////////////////////////////////////////////////////////
1074 void MediaCurl::evaluateCurlCode(const Pathname &filename,
1076 bool timeout_reached) const
1081 if (filename.empty())
1084 url = getFileUrl(filename);
1090 case CURLE_UNSUPPORTED_PROTOCOL:
1091 err = " Unsupported protocol";
1092 if ( !_lastRedirect.empty() )
1094 err += " or redirect (";
1095 err += _lastRedirect;
1099 case CURLE_URL_MALFORMAT:
1100 case CURLE_URL_MALFORMAT_USER:
1103 case CURLE_LOGIN_DENIED:
1105 MediaUnauthorizedException(url, "Login failed.", _curlError, ""));
1107 case CURLE_HTTP_RETURNED_ERROR:
1109 long httpReturnCode = 0;
1110 CURLcode infoRet = curl_easy_getinfo( _curl,
1111 CURLINFO_RESPONSE_CODE,
1113 if ( infoRet == CURLE_OK )
1115 string msg = "HTTP response: " + str::numstring( httpReturnCode );
1116 switch ( httpReturnCode )
1120 string auth_hint = getAuthHint();
1122 DBG << msg << " Login failed (URL: " << url.asString() << ")" << std::endl;
1123 DBG << "MediaUnauthorizedException auth hint: '" << auth_hint << "'" << std::endl;
1125 ZYPP_THROW(MediaUnauthorizedException(
1126 url, "Login failed.", _curlError, auth_hint
1130 case 502: // bad gateway (bnc #1070851)
1131 case 503: // service temporarily unavailable (bnc #462545)
1132 ZYPP_THROW(MediaTemporaryProblemException(url));
1133 case 504: // gateway timeout
1134 ZYPP_THROW(MediaTimeoutException(url));
1138 if ( url.getHost().find(".suse.com") != string::npos )
1139 msg403 = _("Visit the SUSE Customer Center to check whether your registration is valid and has not expired.");
1140 else if (url.asString().find("novell.com") != string::npos)
1141 msg403 = _("Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
1142 ZYPP_THROW(MediaForbiddenException(url, msg403));
1146 ZYPP_THROW(MediaFileNotFoundException(_url, filename));
1149 DBG << msg << " (URL: " << url.asString() << ")" << std::endl;
1150 ZYPP_THROW(MediaCurlException(url, msg, _curlError));
1154 string msg = "Unable to retrieve HTTP response:";
1155 DBG << msg << " (URL: " << url.asString() << ")" << std::endl;
1156 ZYPP_THROW(MediaCurlException(url, msg, _curlError));
1160 case CURLE_FTP_COULDNT_RETR_FILE:
1161 #if CURLVERSION_AT_LEAST(7,16,0)
1162 case CURLE_REMOTE_FILE_NOT_FOUND:
1164 case CURLE_FTP_ACCESS_DENIED:
1165 case CURLE_TFTP_NOTFOUND:
1166 err = "File not found";
1167 ZYPP_THROW(MediaFileNotFoundException(_url, filename));
1169 case CURLE_BAD_PASSWORD_ENTERED:
1170 case CURLE_FTP_USER_PASSWORD_INCORRECT:
1171 err = "Login failed";
1173 case CURLE_COULDNT_RESOLVE_PROXY:
1174 case CURLE_COULDNT_RESOLVE_HOST:
1175 case CURLE_COULDNT_CONNECT:
1176 case CURLE_FTP_CANT_GET_HOST:
1177 err = "Connection failed";
1179 case CURLE_WRITE_ERROR:
1180 err = "Write error";
1182 case CURLE_PARTIAL_FILE:
1183 case CURLE_OPERATION_TIMEDOUT:
1184 timeout_reached = true; // fall though to TimeoutException
1186 case CURLE_ABORTED_BY_CALLBACK:
1187 if( timeout_reached )
1189 err = "Timeout reached";
1190 ZYPP_THROW(MediaTimeoutException(url));
1197 case CURLE_SSL_PEER_CERTIFICATE:
1199 err = "Curl error " + str::numstring( code );
1203 // uhm, no 0 code but unknown curl exception
1204 ZYPP_THROW(MediaCurlException(url, err, _curlError));
1209 // actually the code is 0, nothing happened
1213 ///////////////////////////////////////////////////////////////////
1215 bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const
1217 DBG << filename.asString() << endl;
1220 ZYPP_THROW(MediaBadUrlException(_url));
1222 if(_url.getHost().empty())
1223 ZYPP_THROW(MediaBadUrlEmptyHostException(_url));
1225 Url url(getFileUrl(filename));
1227 DBG << "URL: " << url.asString() << endl;
1228 // Use URL without options and without username and passwd
1229 // (some proxies dislike them in the URL).
1230 // Curl seems to need the just scheme, hostname and a path;
1231 // the rest was already passed as curl options (in attachTo).
1232 Url curlUrl( clearQueryString(url) );
1235 // See also Bug #154197 and ftp url definition in RFC 1738:
1236 // The url "ftp://user@host/foo/bar/file" contains a path,
1237 // that is relative to the user's home.
1238 // The url "ftp://user@host//foo/bar/file" (or also with
1239 // encoded slash as %2f) "ftp://user@host/%2ffoo/bar/file"
1240 // contains an absolute path.
1242 _lastRedirect.clear();
1243 string urlBuffer( curlUrl.asString());
1244 CURLcode ret = curl_easy_setopt( _curl, CURLOPT_URL,
1245 urlBuffer.c_str() );
1247 ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
1250 // instead of returning no data with NOBODY, we return
1251 // little data, that works with broken servers, and
1252 // works for ftp as well, because retrieving only headers
1253 // ftp will return always OK code ?
1254 // See http://curl.haxx.se/docs/knownbugs.html #58
1255 if ( (_url.getScheme() == "http" || _url.getScheme() == "https") &&
1256 _settings.headRequestsAllowed() )
1257 ret = curl_easy_setopt( _curl, CURLOPT_NOBODY, 1L );
1259 ret = curl_easy_setopt( _curl, CURLOPT_RANGE, "0-1" );
1262 curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L);
1263 curl_easy_setopt( _curl, CURLOPT_RANGE, NULL );
1264 /* yes, this is why we never got to get NOBODY working before,
1265 because setting it changes this option too, and we also
1267 See: http://curl.haxx.se/mail/archive-2005-07/0073.html
1269 curl_easy_setopt( _curl, CURLOPT_HTTPGET, 1L );
1270 ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
1273 FILE *file = ::fopen( "/dev/null", "w" );
1275 ERR << "fopen failed for /dev/null" << endl;
1276 curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L);
1277 curl_easy_setopt( _curl, CURLOPT_RANGE, NULL );
1278 /* yes, this is why we never got to get NOBODY working before,
1279 because setting it changes this option too, and we also
1281 See: http://curl.haxx.se/mail/archive-2005-07/0073.html
1283 curl_easy_setopt( _curl, CURLOPT_HTTPGET, 1L );
1285 ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
1287 ZYPP_THROW(MediaWriteException("/dev/null"));
1290 ret = curl_easy_setopt( _curl, CURLOPT_WRITEDATA, file );
1293 std::string err( _curlError);
1294 curl_easy_setopt( _curl, CURLOPT_RANGE, NULL );
1295 curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L);
1296 /* yes, this is why we never got to get NOBODY working before,
1297 because setting it changes this option too, and we also
1299 See: http://curl.haxx.se/mail/archive-2005-07/0073.html
1301 curl_easy_setopt( _curl, CURLOPT_HTTPGET, 1L );
1303 ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
1305 ZYPP_THROW(MediaCurlSetOptException(url, err));
1308 CURLcode ok = curl_easy_perform( _curl );
1309 MIL << "perform code: " << ok << " [ " << curl_easy_strerror(ok) << " ]" << endl;
1311 // reset curl settings
1312 if ( _url.getScheme() == "http" || _url.getScheme() == "https" )
1314 curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L);
1316 ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
1319 /* yes, this is why we never got to get NOBODY working before,
1320 because setting it changes this option too, and we also
1322 See: http://curl.haxx.se/mail/archive-2005-07/0073.html
1324 curl_easy_setopt( _curl, CURLOPT_HTTPGET, 1L);
1326 ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
1332 // for FTP we set different options
1333 curl_easy_setopt( _curl, CURLOPT_RANGE, NULL);
1335 ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
1339 // if the code is not zero, close the file
1343 // as we are not having user interaction, the user can't cancel
1344 // the file existence checking, a callback or timeout return code
1345 // will be always a timeout.
1347 evaluateCurlCode( filename, ok, true /* timeout */);
1349 catch ( const MediaFileNotFoundException &e ) {
1350 // if the file did not exist then we can return false
1353 catch ( const MediaException &e ) {
1354 // some error, we are not sure about file existence, rethrw
1358 return ( ok == CURLE_OK );
1361 ///////////////////////////////////////////////////////////////////
1364 #if DETECT_DIR_INDEX
1365 bool MediaCurl::detectDirIndex() const
1367 if(_url.getScheme() != "http" && _url.getScheme() != "https")
1370 // try to check the effective url and set the not_a_file flag
1371 // if the url path ends with a "/", what usually means, that
1372 // we've received a directory index (index.html content).
1374 // Note: This may be dangerous and break file retrieving in
1375 // case of some server redirections ... ?
1377 bool not_a_file = false;
1379 CURLcode ret = curl_easy_getinfo( _curl,
1380 CURLINFO_EFFECTIVE_URL,
1382 if ( ret == CURLE_OK && ptr != NULL)
1387 std::string path( eurl.getPathName());
1388 if( !path.empty() && path != "/" && *path.rbegin() == '/')
1390 DBG << "Effective url ("
1392 << ") seems to provide the index of a directory"
1404 ///////////////////////////////////////////////////////////////////
1406 void MediaCurl::doGetFileCopy(const Pathname & filename , const Pathname & target, callback::SendReport<DownloadProgressReport> & report, const ByteCount &expectedFileSize_r, RequestOptions options ) const
1408 Pathname dest = target.absolutename();
1409 if( assert_dir( dest.dirname() ) )
1411 DBG << "assert_dir " << dest.dirname() << " failed" << endl;
1412 Url url(getFileUrl(filename));
1413 ZYPP_THROW( MediaSystemException(url, "System error on " + dest.dirname().asString()) );
1415 string destNew = target.asString() + ".new.zypp.XXXXXX";
1416 char *buf = ::strdup( destNew.c_str());
1419 ERR << "out of memory for temp file name" << endl;
1420 Url url(getFileUrl(filename));
1421 ZYPP_THROW(MediaSystemException(url, "out of memory for temp file name"));
1424 int tmp_fd = ::mkostemp( buf, O_CLOEXEC );
1428 ERR << "mkstemp failed for file '" << destNew << "'" << endl;
1429 ZYPP_THROW(MediaWriteException(destNew));
1434 FILE *file = ::fdopen( tmp_fd, "we" );
1437 filesystem::unlink( destNew );
1438 ERR << "fopen failed for file '" << destNew << "'" << endl;
1439 ZYPP_THROW(MediaWriteException(destNew));
1442 DBG << "dest: " << dest << endl;
1443 DBG << "temp: " << destNew << endl;
1445 // set IFMODSINCE time condition (no download if not modified)
1446 if( PathInfo(target).isExist() && !(options & OPTION_NO_IFMODSINCE) )
1448 curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1449 curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, (long)PathInfo(target).mtime());
1453 curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1454 curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, 0L);
1458 doGetFileCopyFile(filename, dest, file, report, expectedFileSize_r, options);
1460 catch (Exception &e)
1463 filesystem::unlink( destNew );
1464 curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1465 curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, 0L);
1469 long httpReturnCode = 0;
1470 CURLcode infoRet = curl_easy_getinfo(_curl,
1471 CURLINFO_RESPONSE_CODE,
1473 bool modified = true;
1474 if (infoRet == CURLE_OK)
1476 DBG << "HTTP response: " + str::numstring(httpReturnCode);
1477 if ( httpReturnCode == 304
1478 || ( httpReturnCode == 213 && (_url.getScheme() == "ftp" || _url.getScheme() == "tftp") ) ) // not modified
1480 DBG << " Not modified.";
1487 WAR << "Could not get the reponse code." << endl;
1490 if (modified || infoRet != CURLE_OK)
1493 if ( ::fchmod( ::fileno(file), filesystem::applyUmaskTo( 0644 ) ) )
1495 ERR << "Failed to chmod file " << destNew << endl;
1497 if (::fclose( file ))
1499 ERR << "Fclose failed for file '" << destNew << "'" << endl;
1500 ZYPP_THROW(MediaWriteException(destNew));
1502 // move the temp file into dest
1503 if ( rename( destNew, dest ) != 0 ) {
1504 ERR << "Rename failed" << endl;
1505 ZYPP_THROW(MediaWriteException(dest));
1510 // close and remove the temp file
1512 filesystem::unlink( destNew );
1515 DBG << "done: " << PathInfo(dest) << endl;
1518 ///////////////////////////////////////////////////////////////////
1520 void MediaCurl::doGetFileCopyFile(const Pathname & filename , const Pathname & dest, FILE *file, callback::SendReport<DownloadProgressReport> & report, const ByteCount &expectedFileSize_r, RequestOptions options ) const
1522 DBG << filename.asString() << endl;
1525 ZYPP_THROW(MediaBadUrlException(_url));
1527 if(_url.getHost().empty())
1528 ZYPP_THROW(MediaBadUrlEmptyHostException(_url));
1530 Url url(getFileUrl(filename));
1532 DBG << "URL: " << url.asString() << endl;
1533 // Use URL without options and without username and passwd
1534 // (some proxies dislike them in the URL).
1535 // Curl seems to need the just scheme, hostname and a path;
1536 // the rest was already passed as curl options (in attachTo).
1537 Url curlUrl( clearQueryString(url) );
1540 // See also Bug #154197 and ftp url definition in RFC 1738:
1541 // The url "ftp://user@host/foo/bar/file" contains a path,
1542 // that is relative to the user's home.
1543 // The url "ftp://user@host//foo/bar/file" (or also with
1544 // encoded slash as %2f) "ftp://user@host/%2ffoo/bar/file"
1545 // contains an absolute path.
1547 _lastRedirect.clear();
1548 string urlBuffer( curlUrl.asString());
1549 CURLcode ret = curl_easy_setopt( _curl, CURLOPT_URL,
1550 urlBuffer.c_str() );
1552 ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
1555 ret = curl_easy_setopt( _curl, CURLOPT_WRITEDATA, file );
1557 ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
1560 // Set callback and perform.
1561 ProgressData progressData(_curl, _settings.timeout(), url, expectedFileSize_r, &report);
1562 if (!(options & OPTION_NO_REPORT_START))
1563 report->start(url, dest);
1564 if ( curl_easy_setopt( _curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
1565 WAR << "Can't set CURLOPT_PROGRESSDATA: " << _curlError << endl;;
1568 ret = curl_easy_perform( _curl );
1569 #if CURLVERSION_AT_LEAST(7,19,4)
1570 // bnc#692260: If the client sends a request with an If-Modified-Since header
1571 // with a future date for the server, the server may respond 200 sending a
1573 // curl-7.19.4 introduces CURLINFO_CONDITION_UNMET to check this condition.
1574 if ( ftell(file) == 0 && ret == 0 )
1576 long httpReturnCode = 33;
1577 if ( curl_easy_getinfo( _curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
1579 long conditionUnmet = 33;
1580 if ( curl_easy_getinfo( _curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
1582 WAR << "TIMECONDITION unmet - retry without." << endl;
1583 curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1584 curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, 0L);
1585 ret = curl_easy_perform( _curl );
1591 if ( curl_easy_setopt( _curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
1592 WAR << "Can't unset CURLOPT_PROGRESSDATA: " << _curlError << endl;;
1597 ERR << "curl error: " << ret << ": " << _curlError
1598 << ", temp file size " << ftell(file)
1599 << " bytes." << endl;
1601 // the timeout is determined by the progress data object
1602 // which holds whether the timeout was reached or not,
1603 // otherwise it would be a user cancel
1606 if ( progressData.fileSizeExceeded )
1607 ZYPP_THROW(MediaFileSizeExceededException(url, progressData._expectedFileSize));
1609 evaluateCurlCode( filename, ret, progressData.reached );
1611 catch ( const MediaException &e ) {
1612 // some error, we are not sure about file existence, rethrw
1617 #if DETECT_DIR_INDEX
1618 if (!ret && detectDirIndex())
1620 ZYPP_THROW(MediaNotAFileException(_url, filename));
1622 #endif // DETECT_DIR_INDEX
1625 ///////////////////////////////////////////////////////////////////
1627 void MediaCurl::getDir( const Pathname & dirname, bool recurse_r ) const
1629 filesystem::DirContent content;
1630 getDirInfo( content, dirname, /*dots*/false );
1632 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1633 Pathname filename = dirname + it->name;
1636 switch ( it->type ) {
1637 case filesystem::FT_NOT_AVAIL: // old directory.yast contains no typeinfo at all
1638 case filesystem::FT_FILE:
1639 getFile( filename, 0 );
1641 case filesystem::FT_DIR: // newer directory.yast contain at least directory info
1643 getDir( filename, recurse_r );
1645 res = assert_dir( localPath( filename ) );
1647 WAR << "Ignore error (" << res << ") on creating local directory '" << localPath( filename ) << "'" << endl;
1652 // don't provide devices, sockets, etc.
1658 ///////////////////////////////////////////////////////////////////
1660 void MediaCurl::getDirInfo( std::list<std::string> & retlist,
1661 const Pathname & dirname, bool dots ) const
1663 getDirectoryYast( retlist, dirname, dots );
1666 ///////////////////////////////////////////////////////////////////
1668 void MediaCurl::getDirInfo( filesystem::DirContent & retlist,
1669 const Pathname & dirname, bool dots ) const
1671 getDirectoryYast( retlist, dirname, dots );
1674 ///////////////////////////////////////////////////////////////////
1676 int MediaCurl::aliveCallback( void *clientp, double /*dltotal*/, double dlnow, double /*ultotal*/, double /*ulnow*/ )
1678 ProgressData *pdata = reinterpret_cast<ProgressData *>( clientp );
1681 // Do not propagate dltotal in alive callbacks. MultiCurl uses this to
1682 // prevent a percentage raise while downloading a metalink file. Download
1683 // activity however is indicated by propagating the download rate (via dlnow).
1684 pdata->updateStats( 0.0, dlnow );
1685 return pdata->reportProgress();
1690 int MediaCurl::progressCallback( void *clientp, double dltotal, double dlnow, double ultotal, double ulnow )
1692 ProgressData *pdata = reinterpret_cast<ProgressData *>( clientp );
1695 // work around curl bug that gives us old data
1696 long httpReturnCode = 0;
1697 if ( curl_easy_getinfo( pdata->curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0 )
1698 return aliveCallback( clientp, dltotal, dlnow, ultotal, ulnow );
1700 pdata->updateStats( dltotal, dlnow );
1701 return pdata->reportProgress();
1706 CURL *MediaCurl::progressCallback_getcurl( void *clientp )
1708 ProgressData *pdata = reinterpret_cast<ProgressData *>(clientp);
1709 return pdata ? pdata->curl : 0;
1712 ///////////////////////////////////////////////////////////////////
1714 string MediaCurl::getAuthHint() const
1716 long auth_info = CURLAUTH_NONE;
1719 curl_easy_getinfo(_curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1721 if(infoRet == CURLE_OK)
1723 return CurlAuthData::auth_type_long2str(auth_info);
1730 * MediaMultiCurl needs to reset the expected filesize in case a metalink file is downloaded
1731 * otherwise this function should not be called
1733 void MediaCurl::resetExpectedFileSize(void *clientp, const ByteCount &expectedFileSize)
1735 ProgressData *data = reinterpret_cast<ProgressData *>(clientp);
1737 data->_expectedFileSize = expectedFileSize;
1741 ///////////////////////////////////////////////////////////////////
1743 bool MediaCurl::authenticate(const string & availAuthTypes, bool firstTry) const
1745 //! \todo need a way to pass different CredManagerOptions here
1746 CredentialManager cm(CredManagerOptions(ZConfig::instance().repoManagerRoot()));
1747 CurlAuthData_Ptr credentials;
1749 // get stored credentials
1750 AuthData_Ptr cmcred = cm.getCred(_url);
1752 if (cmcred && firstTry)
1754 credentials.reset(new CurlAuthData(*cmcred));
1755 DBG << "got stored credentials:" << endl << *credentials << endl;
1757 // if not found, ask user
1761 CurlAuthData_Ptr curlcred;
1762 curlcred.reset(new CurlAuthData());
1763 callback::SendReport<AuthenticationReport> auth_report;
1765 // preset the username if present in current url
1766 if (!_url.getUsername().empty() && firstTry)
1767 curlcred->setUsername(_url.getUsername());
1768 // if CM has found some credentials, preset the username from there
1770 curlcred->setUsername(cmcred->username());
1772 // indicate we have no good credentials from CM
1775 string prompt_msg = str::Format(_("Authentication required for '%s'")) % _url.asString();
1777 // set available authentication types from the exception
1778 // might be needed in prompt
1779 curlcred->setAuthType(availAuthTypes);
1782 if (auth_report->prompt(_url, prompt_msg, *curlcred))
1784 DBG << "callback answer: retry" << endl
1785 << "CurlAuthData: " << *curlcred << endl;
1787 if (curlcred->valid())
1789 credentials = curlcred;
1790 // if (credentials->username() != _url.getUsername())
1791 // _url.setUsername(credentials->username());
1793 * \todo find a way to save the url with changed username
1794 * back to repoinfo or dont store urls with username
1795 * (and either forbid more repos with the same url and different
1796 * user, or return a set of credentials from CM and try them one
1803 DBG << "callback answer: cancel" << endl;
1807 // set username and password
1810 // HACK, why is this const?
1811 const_cast<MediaCurl*>(this)->_settings.setUsername(credentials->username());
1812 const_cast<MediaCurl*>(this)->_settings.setPassword(credentials->password());
1814 // set username and password
1815 CURLcode ret = curl_easy_setopt(_curl, CURLOPT_USERPWD, _settings.userPassword().c_str());
1816 if ( ret != 0 ) ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
1818 // set available authentication types from the exception
1819 if (credentials->authType() == CURLAUTH_NONE)
1820 credentials->setAuthType(availAuthTypes);
1822 // set auth type (seems this must be set _after_ setting the userpwd)
1823 if (credentials->authType() != CURLAUTH_NONE)
1825 // FIXME: only overwrite if not empty?
1826 const_cast<MediaCurl*>(this)->_settings.setAuthType(credentials->authTypeAsString());
1827 ret = curl_easy_setopt(_curl, CURLOPT_HTTPAUTH, credentials->authType());
1828 if ( ret != 0 ) ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
1833 credentials->setUrl(_url);
1834 cm.addCred(*credentials);
1845 } // namespace media