*/
#include <iostream>
+#include <list>
#include "zypp/base/Logger.h"
#include "zypp/ExternalProgram.h"
#include "zypp/base/String.h"
+#include "zypp/base/Gettext.h"
+#include "zypp/base/Sysconfig.h"
+#include "zypp/base/Gettext.h"
#include "zypp/media/MediaCurl.h"
#include "zypp/media/proxyinfo/ProxyInfos.h"
#include "zypp/media/ProxyInfo.h"
+#include "zypp/media/MediaUserAuth.h"
+#include "zypp/media/CredentialManager.h"
+#include "zypp/media/CurlConfig.h"
#include "zypp/thread/Once.h"
+#include "zypp/Target.h"
+#include "zypp/ZYppFactory.h"
+
#include <cstdlib>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <errno.h>
#include <dirent.h>
+#include <unistd.h>
+#include <boost/format.hpp>
+
+#define DETECT_DIR_INDEX 0
+#define CONNECT_TIMEOUT 60
+#define TRANSFER_TIMEOUT 60 * 3
+#define TRANSFER_TIMEOUT_MAX 60 * 60
-#include "config.h"
using namespace std;
using namespace zypp::base;
{
zypp::thread::callOnce(g_InitOnceFlag, _do_init_once);
}
+
+ int log_curl(CURL *curl, curl_infotype info,
+ char *ptr, size_t len, void *max_lvl)
+ {
+ std::string pfx(" ");
+ long lvl = 0;
+ switch( info)
+ {
+ case CURLINFO_TEXT: lvl = 1; pfx = "*"; break;
+ case CURLINFO_HEADER_IN: lvl = 2; pfx = "<"; break;
+ case CURLINFO_HEADER_OUT: lvl = 2; pfx = ">"; break;
+ default: break;
+ }
+ if( lvl > 0 && max_lvl != NULL && lvl <= *((long *)max_lvl))
+ {
+ std::string msg(ptr, len);
+ std::list<std::string> lines;
+ std::list<std::string>::const_iterator line;
+ zypp::str::split(msg, std::back_inserter(lines), "\r\n");
+ for(line = lines.begin(); line != lines.end(); ++line)
+ {
+ DBG << pfx << " " << *line << endl;
+ }
+ }
+ return 0;
+ }
+
+ static size_t
+ log_redirects_curl(
+ void *ptr, size_t size, size_t nmemb, void *stream)
+ {
+ // INT << "got header: " << string((char *)ptr, ((char*)ptr) + size*nmemb) << endl;
+
+ char * lstart = (char *)ptr, * lend = (char *)ptr;
+ size_t pos = 0;
+ size_t max = size * nmemb;
+ while (pos + 1 < max)
+ {
+ // get line
+ for (lstart = lend; *lend != '\n' && pos < max; ++lend, ++pos);
+
+ // look for "Location"
+ string line(lstart, lend);
+ if (line.find("Location") != string::npos)
+ {
+ DBG << "redirecting to " << line << endl;
+ return max;
+ }
+
+ // continue with the next line
+ if (pos + 1 < max)
+ {
+ ++lend;
+ ++pos;
+ }
+ else
+ break;
+ }
+
+ return max;
+ }
}
namespace zypp {
namespace media {
-Pathname MediaCurl::_cookieFile = "/var/lib/YaST2/cookies";
-std::string MediaCurl::_agent = "Novell ZYPP Installer";
+ namespace {
+ struct ProgressData
+ {
+ ProgressData(const long _timeout, const zypp::Url &_url = zypp::Url(),
+ callback::SendReport<DownloadProgressReport> *_report=NULL)
+ : timeout(_timeout)
+ , reached(false)
+ , report(_report)
+ , drate_period(-1)
+ , dload_period(0)
+ , secs(0)
+ , drate_avg(-1)
+ , ltime( time(NULL))
+ , dload( 0)
+ , uload( 0)
+ , url(_url)
+ {}
+ long timeout;
+ bool reached;
+ callback::SendReport<DownloadProgressReport> *report;
+ // download rate of the last period (cca 1 sec)
+ double drate_period;
+ // bytes downloaded at the start of the last period
+ double dload_period;
+ // seconds from the start of the download
+ long secs;
+ // average download rate
+ double drate_avg;
+ // last time the progress was reported
+ time_t ltime;
+ // bytes downloaded at the moment the progress was last reported
+ double dload;
+ // bytes uploaded at the moment the progress was last reported
+ double uload;
+ zypp::Url url;
+ };
+
+ ///////////////////////////////////////////////////////////////////
+
+ inline void escape( string & str_r,
+ const char char_r, const string & escaped_r ) {
+ for ( string::size_type pos = str_r.find( char_r );
+ pos != string::npos; pos = str_r.find( char_r, pos ) ) {
+ str_r.replace( pos, 1, escaped_r );
+ }
+ }
-///////////////////////////////////////////////////////////////////
+ inline string escapedPath( string path_r ) {
+ escape( path_r, ' ', "%20" );
+ return path_r;
+ }
+
+ inline string unEscape( string text_r ) {
+ char * tmp = curl_unescape( text_r.c_str(), 0 );
+ string ret( tmp );
+ curl_free( tmp );
+ return ret;
+ }
-static inline void escape( string & str_r,
- const char char_r, const string & escaped_r ) {
- for ( string::size_type pos = str_r.find( char_r );
- pos != string::npos; pos = str_r.find( char_r, pos ) ) {
- str_r.replace( pos, 1, escaped_r );
}
+
+/**
+ * Fills the settings structure using options passed on the url
+ * for example ?timeout=x&proxy=foo
+ */
+void fillSettingsFromUrl( const Url &url, TransferSettings &s )
+{
+ std::string param(url.getQueryParam("timeout"));
+ if( !param.empty())
+ {
+ long num = str::strtonum<long>(param);
+ if( num >= 0 && num <= TRANSFER_TIMEOUT_MAX)
+ s.setTimeout(num);
+ }
+
+ if ( ! url.getUsername().empty() )
+ {
+ s.setUsername(url.getUsername());
+ if ( url.getPassword().size() )
+ s.setPassword(url.getPassword());
+ }
+ else
+ {
+ // if there is no username, set anonymous auth
+ if ( url.getScheme() == "ftp" && s.username().empty() )
+ s.setAnonymousAuth();
+ }
+
+ if ( url.getScheme() == "https" )
+ {
+ s.setVerifyPeerEnabled(false);
+ s.setVerifyHostEnabled(false);
+
+ std::string verify( url.getQueryParam("ssl_verify"));
+ if( verify.empty() ||
+ verify == "yes")
+ {
+ s.setVerifyPeerEnabled(true);
+ s.setVerifyHostEnabled(true);
+ }
+ else if( verify == "no")
+ {
+ s.setVerifyPeerEnabled(false);
+ s.setVerifyHostEnabled(false);
+ }
+ else
+ {
+ std::vector<std::string> flags;
+ std::vector<std::string>::const_iterator flag;
+ str::split( verify, std::back_inserter(flags), ",");
+ for(flag = flags.begin(); flag != flags.end(); ++flag)
+ {
+ if( *flag == "host")
+ s.setVerifyHostEnabled(true);
+ else if( *flag == "peer")
+ s.setVerifyPeerEnabled(true);
+ else
+ ZYPP_THROW(MediaBadUrlException(url, "Unknown ssl_verify flag"));
+ }
+ }
+ }
+
+ Pathname ca_path = Pathname(url.getQueryParam("ssl_capath")).asString();
+ if( ! ca_path.empty())
+ {
+ if( !PathInfo(ca_path).isDir() || !Pathname(ca_path).absolute())
+ ZYPP_THROW(MediaBadUrlException(url, "Invalid ssl_capath path"));
+ else
+ s.setCertificateAuthoritiesPath(ca_path);
+ }
+
+ string proxy = url.getQueryParam( "proxy" );
+ if ( ! proxy.empty() )
+ {
+ if ( proxy == "_none_" ) {
+ s.setProxyEnabled(false);
+ }
+ else {
+ string proxyport( url.getQueryParam( "proxyport" ) );
+ if ( ! proxyport.empty() ) {
+ proxy += ":" + proxyport;
+ }
+ s.setProxy(proxy);
+ s.setProxyEnabled(true);
+ }
+ }
+
+ // HTTP authentication type
+ string use_auth = url.getQueryParam("auth");
+ if (!use_auth.empty() && (url.getScheme() == "http" || url.getScheme() == "https"))
+ {
+ try
+ {
+ CurlAuthData::auth_type_str2long(use_auth); // check if we know it
+ }
+ catch (MediaException & ex_r)
+ {
+ DBG << "Rethrowing as MediaUnauthorizedException.";
+ ZYPP_THROW(MediaUnauthorizedException(url, ex_r.msg(), "", ""));
+ }
+ s.setAuthType(use_auth);
+ }
+
+ // workarounds
+ std::string head_requests( url.getQueryParam("head_requests"));
+ if( !head_requests.empty() && head_requests == "no")
+ s.setHeadRequestsAllowed(false);
}
-static inline string escapedPath( string path_r ) {
- escape( path_r, ' ', "%20" );
- return path_r;
+/**
+ * Reads the system proxy configuration and fills the settings
+ * structure proxy information
+ */
+void fillSettingsSystemProxy( const Url&url, TransferSettings &s )
+{
+#ifdef _WITH_LIBPROXY_SUPPORT_
+ ProxyInfo proxy_info (ProxyInfo::ImplPtr(new ProxyInfoLibproxy()));
+#else
+ ProxyInfo proxy_info (ProxyInfo::ImplPtr(new ProxyInfoSysconfig("proxy")));
+#endif
+ s.setProxyEnabled( proxy_info.useProxyFor( url ) );
+ if ( s.proxyEnabled() )
+ s.setProxy(proxy_info.proxy(url));
}
-static inline string unEscape( string text_r ) {
- char * tmp = curl_unescape( text_r.c_str(), 0 );
- string ret( tmp );
- curl_free( tmp );
- return ret;
+Pathname MediaCurl::_cookieFile = "/var/lib/YaST2/cookies";
+
+/**
+ * initialized only once, this gets the anonymous id
+ * from the target, which we pass in the http header
+ */
+static const char *const anonymousIdHeader()
+{
+ // we need to add the release and identifier to the
+ // agent string.
+ // The target could be not initialized, and then this information
+ // is guessed.
+ static const std::string _value(
+ str::trim( str::form(
+ "X-ZYpp-AnonymousId: %s",
+ Target::anonymousUniqueId( Pathname()/*guess root*/ ).c_str() ) )
+ );
+ return _value.c_str();
}
-///////////////////////////////////////////////////////////////////
-//
-// CLASS NAME : MediaCurl
-//
-///////////////////////////////////////////////////////////////////
+/**
+ * initialized only once, this gets the distribution flavor
+ * from the target, which we pass in the http header
+ */
+static const char *const distributionFlavorHeader()
+{
+ // we need to add the release and identifier to the
+ // agent string.
+ // The target could be not initialized, and then this information
+ // is guessed.
+ static const std::string _value(
+ str::trim( str::form(
+ "X-ZYpp-DistributionFlavor: %s",
+ Target::distributionFlavor( Pathname()/*guess root*/ ).c_str() ) )
+ );
+ return _value.c_str();
+}
+
+/**
+ * initialized only once, this gets the agent string
+ * which also includes the curl version
+ */
+static const char *const agentString()
+{
+ // we need to add the release and identifier to the
+ // agent string.
+ // The target could be not initialized, and then this information
+ // is guessed.
+ static const std::string _value(
+ str::form(
+ "ZYpp %s (curl %s) %s"
+ , VERSION
+ , curl_version_info(CURLVERSION_NOW)->version
+ , Target::targetDistribution( Pathname()/*guess root*/ ).c_str()
+ )
+ );
+ return _value.c_str();
+}
+
+// we use this define to unbloat code as this C setting option
+// and catching exception is done frequently.
+/** \todo deprecate SET_OPTION and use the typed versions below. */
+#define SET_OPTION(opt,val) do { \
+ ret = curl_easy_setopt ( _curl, opt, val ); \
+ if ( ret != 0) { \
+ ZYPP_THROW(MediaCurlSetOptException(_url, _curlError)); \
+ } \
+ } while ( false )
+
+#define SET_OPTION_OFFT(opt,val) SET_OPTION(opt,(curl_off_t)val)
+#define SET_OPTION_LONG(opt,val) SET_OPTION(opt,(long)val)
+#define SET_OPTION_VOID(opt,val) SET_OPTION(opt,(void*)val)
MediaCurl::MediaCurl( const Url & url_r,
- const Pathname & attach_point_hint_r )
+ const Pathname & attach_point_hint_r )
: MediaHandler( url_r, attach_point_hint_r,
- "/", // urlpath at attachpoint
- true ), // does_download
- _curl( NULL )
+ "/", // urlpath at attachpoint
+ true ), // does_download
+ _curl( NULL ),
+ _customHeaders(0L)
{
+ _curlError[0] = '\0';
+ _curlDebug = 0L;
+
MIL << "MediaCurl::MediaCurl(" << url_r << ", " << attach_point_hint_r << ")" << endl;
globalInitOnce();
}
}
+Url MediaCurl::clearQueryString(const Url &url) const
+{
+ Url curlUrl (url);
+ curlUrl.setUsername( "" );
+ curlUrl.setPassword( "" );
+ curlUrl.setPathParams( "" );
+ curlUrl.setFragment( "" );
+ curlUrl.delQueryParam("cookies");
+ curlUrl.delQueryParam("proxy");
+ curlUrl.delQueryParam("proxyport");
+ curlUrl.delQueryParam("proxyuser");
+ curlUrl.delQueryParam("proxypass");
+ curlUrl.delQueryParam("ssl_capath");
+ curlUrl.delQueryParam("ssl_verify");
+ curlUrl.delQueryParam("timeout");
+ curlUrl.delQueryParam("auth");
+ curlUrl.delQueryParam("username");
+ curlUrl.delQueryParam("password");
+ curlUrl.delQueryParam("mediahandler");
+ return curlUrl;
+}
+
+TransferSettings & MediaCurl::settings()
+{
+ return _settings;
+}
+
+
void MediaCurl::setCookieFile( const Pathname &fileName )
{
_cookieFile = fileName;
}
///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : MediaCurl::attachTo
-// METHOD TYPE : PMError
-//
-// DESCRIPTION : Asserted that not already attached, and attachPoint is a directory.
-//
-void MediaCurl::attachTo (bool next)
-{
- if ( next )
- ZYPP_THROW(MediaNotSupportedException(_url));
-
- if ( !_url.isValid() )
- ZYPP_THROW(MediaBadUrlException(_url));
+void MediaCurl::checkProtocol(const Url &url) const
+{
curl_version_info_data *curl_info = NULL;
curl_info = curl_version_info(CURLVERSION_NOW);
// curl_info does not need any free (is static)
if (curl_info->protocols)
{
const char * const *proto;
- std::string scheme( _url.getScheme());
+ std::string scheme( url.getScheme());
bool found = false;
for(proto=curl_info->protocols; !found && *proto; ++proto)
{
if( scheme == std::string((const char *)*proto))
- found = true;
+ found = true;
}
if( !found)
{
ZYPP_THROW(MediaBadUrlException(_url, msg));
}
}
+}
- if( !isUseableAttachPoint(attachPoint()))
+void MediaCurl::setupEasy()
+{
{
- std::string mountpoint = createAttachPoint().asString();
-
- if( mountpoint.empty())
- ZYPP_THROW( MediaBadAttachPointException(url()));
-
- setAttachPoint( mountpoint, true);
- }
-
- disconnectFrom(); // clean _curl if needed
- _curl = curl_easy_init();
- if ( !_curl ) {
- ZYPP_THROW(MediaCurlInitException(_url));
+ char *ptr = getenv("ZYPP_MEDIA_CURL_DEBUG");
+ _curlDebug = (ptr && *ptr) ? str::strtonum<long>( ptr) : 0L;
+ if( _curlDebug > 0)
+ {
+ curl_easy_setopt( _curl, CURLOPT_VERBOSE, 1L);
+ curl_easy_setopt( _curl, CURLOPT_DEBUGFUNCTION, log_curl);
+ curl_easy_setopt( _curl, CURLOPT_DEBUGDATA, &_curlDebug);
+ }
}
+ curl_easy_setopt(_curl, CURLOPT_HEADERFUNCTION, log_redirects_curl);
CURLcode ret = curl_easy_setopt( _curl, CURLOPT_ERRORBUFFER, _curlError );
if ( ret != 0 ) {
- disconnectFrom();
ZYPP_THROW(MediaCurlSetOptException(_url, "Error setting error buffer"));
}
- ret = curl_easy_setopt( _curl, CURLOPT_FAILONERROR, true );
- if ( ret != 0 ) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
+ SET_OPTION(CURLOPT_FAILONERROR, 1L);
+ SET_OPTION(CURLOPT_NOSIGNAL, 1L);
+
+ // create non persistant settings
+ // so that we don't add headers twice
+ TransferSettings vol_settings(_settings);
+
+ // add custom headers
+ vol_settings.addHeader(anonymousIdHeader());
+ vol_settings.addHeader(distributionFlavorHeader());
+ vol_settings.addHeader("Pragma:");
+
+ _settings.setTimeout(TRANSFER_TIMEOUT);
+ _settings.setConnectTimeout(CONNECT_TIMEOUT);
+
+ _settings.setUserAgentString(agentString());
+
+ // fill some settings from url query parameters
+ try
+ {
+ fillSettingsFromUrl(_url, _settings);
+ }
+ catch ( const MediaException &e )
+ {
+ disconnectFrom();
+ ZYPP_RETHROW(e);
}
- ret = curl_easy_setopt( _curl, CURLOPT_NOSIGNAL, 1 );
- if ( ret != 0 ) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
- }
-
- /*
- ** Don't block "forever" on system calls. Curl seems to
- ** recover nicely, if the ftp server has e.g. a 30sec
- ** timeout. If required, it closes the connection, trys
- ** to reopen and fetch it - this works in many cases
- ** without to report any error to us.
- **
- ** Disabled, because it breaks normal operations over a
- ** slow link :(
- **
- ret = curl_easy_setopt( _curl, CURLOPT_TIMEOUT, 600 );
- if ( ret != 0 ) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
+ // if the proxy was not set by url, then look
+ if ( _settings.proxy().empty() )
+ {
+ // at the system proxy settings
+ fillSettingsSystemProxy(_url, _settings);
}
+
+ DBG << "Proxy: " << (_settings.proxy().empty() ? "-none-" : _settings.proxy()) << endl;
+
+ /**
+ * Connect timeout
*/
+ SET_OPTION(CURLOPT_CONNECTTIMEOUT, _settings.connectTimeout());
- if ( _url.getScheme() == "http" ) {
- // follow any Location: header that the server sends as part of
- // an HTTP header (#113275)
- ret = curl_easy_setopt ( _curl, CURLOPT_FOLLOWLOCATION, true );
- if ( ret != 0) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
- }
- ret = curl_easy_setopt ( _curl, CURLOPT_MAXREDIRS, 3L );
- if ( ret != 0) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
- }
- ret = curl_easy_setopt ( _curl, CURLOPT_USERAGENT, _agent.c_str() );
- if ( ret != 0) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
- }
- }
+ // follow any Location: header that the server sends as part of
+ // an HTTP header (#113275)
+ SET_OPTION(CURLOPT_FOLLOWLOCATION, 1L);
+ // 3 redirects seem to be too few in some cases (bnc #465532)
+ SET_OPTION(CURLOPT_MAXREDIRS, 6L);
- if ( _url.getScheme() == "https" ) {
- ret = curl_easy_setopt( _curl, CURLOPT_SSL_VERIFYPEER, 1 );
- if ( ret != 0 ) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
- }
- ret = curl_easy_setopt( _curl, CURLOPT_CAPATH, "/etc/ssl/certs/" );
- if ( ret != 0 ) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
- }
- ret = curl_easy_setopt( _curl, CURLOPT_SSL_VERIFYHOST, 2 );
- if ( ret != 0 ) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
- }
- ret = curl_easy_setopt ( _curl, CURLOPT_USERAGENT, _agent.c_str() );
- if ( ret != 0) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
+ if ( _url.getScheme() == "https" )
+ {
+ // restrict following of redirections from https to https only
+ SET_OPTION( CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS );
+
+ if( _settings.verifyPeerEnabled() ||
+ _settings.verifyHostEnabled() )
+ {
+ SET_OPTION(CURLOPT_CAPATH, _settings.certificateAuthoritiesPath().c_str());
}
+
+ SET_OPTION(CURLOPT_SSL_VERIFYPEER, _settings.verifyPeerEnabled() ? 1L : 0L);
+ SET_OPTION(CURLOPT_SSL_VERIFYHOST, _settings.verifyHostEnabled() ? 2L : 0L);
}
+ SET_OPTION(CURLOPT_USERAGENT, _settings.userAgentString().c_str() );
/*---------------------------------------------------------------*
CURLOPT_USERPWD: [user name]:[password]
If not provided, anonymous FTP identification
*---------------------------------------------------------------*/
- if ( _url.getUsername().empty() ) {
- if ( _url.getScheme() == "ftp" ) {
- string id = "yast2@";
- id += VERSION;
- DBG << "Anonymous FTP identification: '" << id << "'" << endl;
- _userpwd = "anonymous:" + id;
- }
- } else {
- _userpwd = _url.getUsername();
- if ( _url.getPassword().size() ) {
- _userpwd += ":" + _url.getPassword();
- }
- }
-
- if ( _userpwd.size() ) {
- _userpwd = unEscape( _userpwd );
- ret = curl_easy_setopt( _curl, CURLOPT_USERPWD, _userpwd.c_str() );
- if ( ret != 0 ) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
- }
-
- if( !_url.getQueryParam("auth").empty() &&
- (_url.getScheme() == "http" || _url.getScheme() == "https"))
+ if ( _settings.userPassword().size() )
+ {
+ SET_OPTION(CURLOPT_USERPWD, _settings.userPassword().c_str());
+ string use_auth = _settings.authType();
+ if (use_auth.empty())
+ use_auth = "digest,basic"; // our default
+ long auth = CurlAuthData::auth_type_str2long(use_auth);
+ if( auth != CURLAUTH_NONE)
{
- std::vector<std::string> list;
- std::vector<std::string>::const_iterator it;
- str::split(_url.getQueryParam("auth"), std::back_inserter(list), ",");
-
- long auth = CURLAUTH_NONE;
- for(it = list.begin(); it != list.end(); ++it)
- {
- if(*it == "basic")
- {
- auth |= CURLAUTH_BASIC;
- }
- else
- if(*it == "digest")
- {
- auth |= CURLAUTH_DIGEST;
- }
- else
- if((curl_info && (curl_info->features & CURL_VERSION_NTLM)) &&
- (*it == "ntlm"))
- {
- auth |= CURLAUTH_NTLM;
- }
- else
- if((curl_info && (curl_info->features & CURL_VERSION_SPNEGO)) &&
- (*it == "spnego" || *it == "negotiate"))
- {
- // there is no separate spnego flag for auth
- auth |= CURLAUTH_GSSNEGOTIATE;
- }
- else
- if((curl_info && (curl_info->features & CURL_VERSION_GSSNEGOTIATE)) &&
- (*it == "gssnego" || *it == "negotiate"))
- {
- auth |= CURLAUTH_GSSNEGOTIATE;
- }
- else
- {
- std::string msg("Unsupported HTTP authentication method '");
- msg += *it;
- msg += "'";
- disconnectFrom();
- ZYPP_THROW(MediaBadUrlException(_url, msg));
- }
- }
-
- if( auth != CURLAUTH_NONE)
- {
- DBG << "Enabling HTTP authentication methods: "
- << _url.getQueryParam("auth") << std::endl;
-
- ret = curl_easy_setopt( _curl, CURLOPT_HTTPAUTH, auth);
- if ( ret != 0 ) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
- }
- }
+ DBG << "Enabling HTTP authentication methods: " << use_auth
+ << " (CURLOPT_HTTPAUTH=" << auth << ")" << std::endl;
+ SET_OPTION(CURLOPT_HTTPAUTH, auth);
}
}
- /*---------------------------------------------------------------*
- CURLOPT_PROXY: host[:port]
-
- Url::option(proxy and proxyport) -> CURLOPT_PROXY
- If not provided, /etc/sysconfig/proxy is evaluated
- *---------------------------------------------------------------*/
-
- _proxy = _url.getQueryParam( "proxy" );
-
- if ( ! _proxy.empty() ) {
- string proxyport( _url.getQueryParam( "proxyport" ) );
- if ( ! proxyport.empty() ) {
- _proxy += ":" + proxyport;
- }
- } else {
+ if ( _settings.proxyEnabled() )
+ {
+ if ( ! _settings.proxy().empty() )
+ {
+ SET_OPTION(CURLOPT_PROXY, _settings.proxy().c_str());
+ /*---------------------------------------------------------------*
+ CURLOPT_PROXYUSERPWD: [user name]:[password]
- ProxyInfo proxy_info (ProxyInfo::ImplPtr(new ProxyInfoSysconfig("proxy")));
+ Url::option(proxyuser and proxypassword) -> CURLOPT_PROXYUSERPWD
+ If not provided, $HOME/.curlrc is evaluated
+ *---------------------------------------------------------------*/
- if ( proxy_info.enabled())
- {
- bool useproxy = true;
+ string proxyuserpwd = _settings.proxyUserPassword();
- std::list<std::string> nope = proxy_info.noProxy();
- for (ProxyInfo::NoProxyIterator it = proxy_info.noProxyBegin();
- it != proxy_info.noProxyEnd();
- it++)
+ if ( proxyuserpwd.empty() )
{
- std::string host( str::toLower(_url.getHost()));
- std::string temp( str::toLower(*it));
-
- // no proxy if it points to a suffix
- // preceeded by a '.', that maches
- // the trailing portion of the host.
- if( temp.size() > 1 && temp.at(0) == '.')
- {
- if(host.size() > temp.size() &&
- host.compare(host.size() - temp.size(), temp.size(), temp) == 0)
- {
- DBG << "NO_PROXY: '" << *it << "' matches host '"
- << host << "'" << endl;
- useproxy = false;
- break;
- }
- }
- else
- // no proxy if we have an exact match
- if( host == temp)
- {
- DBG << "NO_PROXY: '" << *it << "' matches host '"
- << host << "'" << endl;
- useproxy = false;
- break;
- }
+ CurlConfig curlconf;
+ CurlConfig::parseConfig(curlconf); // parse ~/.curlrc
+ if (curlconf.proxyuserpwd.empty())
+ DBG << "~/.curlrc does not contain the proxy-user option" << endl;
+ else
+ {
+ proxyuserpwd = curlconf.proxyuserpwd;
+ DBG << "using proxy-user from ~/.curlrc" << endl;
+ }
}
- if ( useproxy ) {
- _proxy = proxy_info.proxy(_url.getScheme());
- }
+ proxyuserpwd = unEscape( proxyuserpwd );
+ if ( ! proxyuserpwd.empty() )
+ SET_OPTION(CURLOPT_PROXYUSERPWD, proxyuserpwd.c_str());
}
}
+ else
+ {
+ SET_OPTION(CURLOPT_NOPROXY, "*");
+ }
+ /** Speed limits */
+ if ( _settings.minDownloadSpeed() != 0 )
+ {
+ SET_OPTION(CURLOPT_LOW_SPEED_LIMIT, _settings.minDownloadSpeed());
+ // default to 10 seconds at low speed
+ SET_OPTION(CURLOPT_LOW_SPEED_TIME, 10L);
+ }
- DBG << "Proxy: " << (_proxy.empty() ? "-none-" : _proxy) << endl;
-
- if ( ! _proxy.empty() ) {
-
- ret = curl_easy_setopt( _curl, CURLOPT_PROXY, _proxy.c_str() );
- if ( ret != 0 ) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
- }
-
- /*---------------------------------------------------------------*
- CURLOPT_PROXYUSERPWD: [user name]:[password]
-
- Url::option(proxyuser and proxypassword) -> CURLOPT_PROXYUSERPWD
- If not provided, $HOME/.curlrc is evaluated
- *---------------------------------------------------------------*/
+ if ( _settings.maxDownloadSpeed() != 0 )
+ SET_OPTION_OFFT(CURLOPT_MAX_RECV_SPEED_LARGE, _settings.maxDownloadSpeed());
- _proxyuserpwd = _url.getQueryParam( "proxyuser" );
+ /*---------------------------------------------------------------*
+ *---------------------------------------------------------------*/
- if ( ! _proxyuserpwd.empty() ) {
+ _currentCookieFile = _cookieFile.asString();
+ if ( str::strToBool( _url.getQueryParam( "cookies" ), true ) )
+ SET_OPTION(CURLOPT_COOKIEFILE, _currentCookieFile.c_str() );
+ else
+ MIL << "No cookies requested" << endl;
+ SET_OPTION(CURLOPT_COOKIEJAR, _currentCookieFile.c_str() );
+ SET_OPTION(CURLOPT_PROGRESSFUNCTION, &progressCallback );
+ SET_OPTION(CURLOPT_NOPROGRESS, 0L);
+
+ // bnc #306272
+ SET_OPTION(CURLOPT_PROXY_TRANSFER_MODE, 1L );
+
+ // append settings custom headers to curl
+ for ( TransferSettings::Headers::const_iterator it = vol_settings.headersBegin();
+ it != vol_settings.headersEnd();
+ ++it )
+ {
+ MIL << "HEADER " << *it << std::endl;
- string proxypassword( _url.getQueryParam( "proxypassword" ) );
- if ( ! proxypassword.empty() ) {
- _proxyuserpwd += ":" + proxypassword;
- }
+ _customHeaders = curl_slist_append(_customHeaders, it->c_str());
+ if ( !_customHeaders )
+ ZYPP_THROW(MediaCurlInitException(_url));
+ }
- } else {
+ SET_OPTION(CURLOPT_HTTPHEADER, _customHeaders);
+}
- string curlrcFile = string( getenv("HOME") ) + string( "/.curlrc" );
- map<string,string> rc_data
- = proxyinfo::sysconfigRead(Pathname(curlrcFile));
- map<string,string>::const_iterator it = rc_data.find("proxy-user");
- if (it != rc_data.end())
- _proxyuserpwd = it->second;
- }
+///////////////////////////////////////////////////////////////////
- _proxyuserpwd = unEscape( _proxyuserpwd );
- ret = curl_easy_setopt( _curl, CURLOPT_PROXYUSERPWD, _proxyuserpwd.c_str() );
- if ( ret != 0 ) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
- }
- }
- /*---------------------------------------------------------------*
- *---------------------------------------------------------------*/
+void MediaCurl::attachTo (bool next)
+{
+ if ( next )
+ ZYPP_THROW(MediaNotSupportedException(_url));
- _currentCookieFile = _cookieFile.asString();
+ if ( !_url.isValid() )
+ ZYPP_THROW(MediaBadUrlException(_url));
- ret = curl_easy_setopt( _curl, CURLOPT_COOKIEFILE,
- _currentCookieFile.c_str() );
- if ( ret != 0 ) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
- }
+ checkProtocol(_url);
+ if( !isUseableAttachPoint(attachPoint()))
+ {
+ std::string mountpoint = createAttachPoint().asString();
- ret = curl_easy_setopt( _curl, CURLOPT_COOKIEJAR,
- _currentCookieFile.c_str() );
- if ( ret != 0 ) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
- }
+ if( mountpoint.empty())
+ ZYPP_THROW( MediaBadAttachPointException(url()));
- ret = curl_easy_setopt( _curl, CURLOPT_PROGRESSFUNCTION,
- &progressCallback );
- if ( ret != 0 ) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
+ setAttachPoint( mountpoint, true);
}
- ret = curl_easy_setopt( _curl, CURLOPT_NOPROGRESS, false );
- if ( ret != 0 ) {
- disconnectFrom();
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
+ disconnectFrom(); // clean _curl if needed
+ _curl = curl_easy_init();
+ if ( !_curl ) {
+ ZYPP_THROW(MediaCurlInitException(_url));
}
+ try
+ {
+ setupEasy();
+ }
+ catch (Exception & ex)
+ {
+ disconnectFrom();
+ ZYPP_RETHROW(ex);
+ }
// FIXME: need a derived class to propelly compare url's
MediaSourceRef media( new MediaSource(_url.getScheme(), _url.asString()));
}
///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : MediaCurl::disconnectFrom
-// METHOD TYPE : PMError
-//
+
void MediaCurl::disconnectFrom()
{
+ if ( _customHeaders )
+ {
+ curl_slist_free_all(_customHeaders);
+ _customHeaders = 0L;
+ }
+
if ( _curl )
{
curl_easy_cleanup( _curl );
}
///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : MediaCurl::releaseFrom
-// METHOD TYPE : PMError
-//
-// DESCRIPTION : Asserted that media is attached.
-//
-void MediaCurl::releaseFrom( bool eject )
+
+void MediaCurl::releaseFrom( const std::string & ejectDev )
{
disconnect();
}
+Url MediaCurl::getFileUrl(const Pathname & filename) const
+{
+ Url newurl(_url);
+ string path = _url.getPathName();
+ if ( !path.empty() && path != "/" && *path.rbegin() == '/' &&
+ filename.absolute() )
+ {
+ // If url has a path with trailing slash, remove the leading slash from
+ // the absolute file name
+ path += filename.asString().substr( 1, filename.asString().size() - 1 );
+ }
+ else if ( filename.relative() )
+ {
+ // Add trailing slash to path, if not already there
+ if (path.empty()) path = "/";
+ else if (*path.rbegin() != '/' ) path += "/";
+ // Remove "./" from begin of relative file name
+ path += filename.asString().substr( 2, filename.asString().size() - 2 );
+ }
+ else
+ {
+ path += filename.asString();
+ }
+
+ newurl.setPathName(path);
+ return newurl;
+}
///////////////////////////////////////////////////////////////////
-//
-// METHOD NAME : MediaCurl::getFile
-// METHOD TYPE : PMError
-//
void MediaCurl::getFile( const Pathname & filename ) const
{
getFileCopy(filename, localPath(filename).absolutename());
}
+///////////////////////////////////////////////////////////////////
void MediaCurl::getFileCopy( const Pathname & filename , const Pathname & target) const
{
callback::SendReport<DownloadProgressReport> report;
-
- Url url( _url );
-
- try {
- doGetFileCopy(filename, target, report);
+
+ Url fileurl(getFileUrl(filename));
+
+ bool retry = false;
+
+ do
+ {
+ try
+ {
+ doGetFileCopy(filename, target, report);
+ retry = false;
+ }
+ // retry with proper authentication data
+ catch (MediaUnauthorizedException & ex_r)
+ {
+ if(authenticate(ex_r.hint(), !retry))
+ retry = true;
+ else
+ {
+ report->finish(fileurl, zypp::media::DownloadProgressReport::ACCESS_DENIED, ex_r.asUserHistory());
+ ZYPP_RETHROW(ex_r);
+ }
+ }
+ // unexpected exception
+ catch (MediaException & excpt_r)
+ {
+ // FIXME: error number fix
+ report->finish(fileurl, zypp::media::DownloadProgressReport::ERROR, excpt_r.asUserHistory());
+ ZYPP_RETHROW(excpt_r);
+ }
}
- catch (MediaException & excpt_r)
+ while (retry);
+
+ report->finish(fileurl, zypp::media::DownloadProgressReport::NO_ERROR, "");
+}
+
+///////////////////////////////////////////////////////////////////
+
+bool MediaCurl::getDoesFileExist( const Pathname & filename ) const
+{
+ bool retry = false;
+
+ do
{
- // FIXME: this will not match the first URL
- // FIXME: error number fix
- report->finish(url, zypp::media::DownloadProgressReport::NOT_FOUND, excpt_r.msg());
- ZYPP_RETHROW(excpt_r);
+ try
+ {
+ return doGetDoesFileExist( filename );
+ }
+ // authentication problem, retry with proper authentication data
+ catch (MediaUnauthorizedException & ex_r)
+ {
+ if(authenticate(ex_r.hint(), !retry))
+ retry = true;
+ else
+ ZYPP_RETHROW(ex_r);
+ }
+ // unexpected exception
+ catch (MediaException & excpt_r)
+ {
+ ZYPP_RETHROW(excpt_r);
+ }
}
- report->finish(url, zypp::media::DownloadProgressReport::NO_ERROR, "");
+ while (retry);
+
+ return false;
}
-void MediaCurl::doGetFileCopy( const Pathname & filename , const Pathname & target, callback::SendReport<DownloadProgressReport> & report) const
+///////////////////////////////////////////////////////////////////
+
+void MediaCurl::evaluateCurlCode( const Pathname &filename,
+ CURLcode code,
+ bool timeout_reached ) const
{
- DBG << filename.asString() << endl;
+ if ( code != 0 )
+ {
+ Url url;
+ if (filename.empty())
+ url = _url;
+ else
+ url = getFileUrl(filename);
+ std::string err;
+ try
+ {
+ switch ( code )
+ {
+ case CURLE_UNSUPPORTED_PROTOCOL:
+ case CURLE_URL_MALFORMAT:
+ case CURLE_URL_MALFORMAT_USER:
+ err = " Bad URL";
+ case CURLE_LOGIN_DENIED:
+ ZYPP_THROW(
+ MediaUnauthorizedException(url, "Login failed.", _curlError, ""));
+ break;
+ case CURLE_HTTP_RETURNED_ERROR:
+ {
+ long httpReturnCode = 0;
+ CURLcode infoRet = curl_easy_getinfo( _curl,
+ CURLINFO_RESPONSE_CODE,
+ &httpReturnCode );
+ if ( infoRet == CURLE_OK )
+ {
+ string msg = "HTTP response: " + str::numstring( httpReturnCode );
+ switch ( httpReturnCode )
+ {
+ case 401:
+ {
+ string auth_hint = getAuthHint();
- if(!_url.isValid())
- ZYPP_THROW(MediaBadUrlException(_url));
+ DBG << msg << " Login failed (URL: " << url.asString() << ")" << std::endl;
+ DBG << "MediaUnauthorizedException auth hint: '" << auth_hint << "'" << std::endl;
- if(_url.getHost().empty())
- ZYPP_THROW(MediaBadUrlEmptyHostException(_url));
+ ZYPP_THROW(MediaUnauthorizedException(
+ url, "Login failed.", _curlError, auth_hint
+ ));
+ }
- string path = _url.getPathName();
- if ( !path.empty() && path != "/" && *path.rbegin() == '/' &&
- filename.absolute() ) {
- // If url has a path with trailing slash, remove the leading slash from
- // the absolute file name
- path += filename.asString().substr( 1, filename.asString().size() - 1 );
- } else if ( filename.relative() ) {
- // Add trailing slash to path, if not already there
- if ( !path.empty() && *path.rbegin() != '/' ) path += "/";
- // Remove "./" from begin of relative file name
- path += filename.asString().substr( 2, filename.asString().size() - 2 );
- } else {
- path += filename.asString();
- }
+ case 503: // service temporarily unavailable (bnc #462545)
+ ZYPP_THROW(MediaTemporaryProblemException(url));
+ case 504: // gateway timeout
+ ZYPP_THROW(MediaTimeoutException(url));
+ case 403:
+ {
+ string msg403;
+ if (url.asString().find("novell.com") != string::npos)
+ msg403 = _("Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
+ ZYPP_THROW(MediaForbiddenException(url, msg403));
+ }
+ case 404:
+ ZYPP_THROW(MediaFileNotFoundException(_url, filename));
+ }
- Url url( _url );
- url.setPathName( path );
+ DBG << msg << " (URL: " << url.asString() << ")" << std::endl;
+ ZYPP_THROW(MediaCurlException(url, msg, _curlError));
+ }
+ else
+ {
+ string msg = "Unable to retrieve HTTP response:";
+ DBG << msg << " (URL: " << url.asString() << ")" << std::endl;
+ ZYPP_THROW(MediaCurlException(url, msg, _curlError));
+ }
+ }
+ break;
+ case CURLE_FTP_COULDNT_RETR_FILE:
+ case CURLE_FTP_ACCESS_DENIED:
+ err = "File not found";
+ ZYPP_THROW(MediaFileNotFoundException(_url, filename));
+ break;
+ case CURLE_BAD_PASSWORD_ENTERED:
+ case CURLE_FTP_USER_PASSWORD_INCORRECT:
+ err = "Login failed";
+ break;
+ case CURLE_COULDNT_RESOLVE_PROXY:
+ case CURLE_COULDNT_RESOLVE_HOST:
+ case CURLE_COULDNT_CONNECT:
+ case CURLE_FTP_CANT_GET_HOST:
+ err = "Connection failed";
+ break;
+ case CURLE_WRITE_ERROR:
+ err = "Write error";
+ break;
+ case CURLE_PARTIAL_FILE:
+ case CURLE_ABORTED_BY_CALLBACK:
+ case CURLE_OPERATION_TIMEDOUT:
+ if( timeout_reached)
+ {
+ err = "Timeout reached";
+ ZYPP_THROW(MediaTimeoutException(url));
+ }
+ else
+ {
+ err = "User abort";
+ }
+ break;
+ case CURLE_SSL_PEER_CERTIFICATE:
+ default:
+ err = "Unrecognized error";
+ break;
+ }
- Pathname dest = target.absolutename();
- if( assert_dir( dest.dirname() ) )
+ // uhm, no 0 code but unknown curl exception
+ ZYPP_THROW(MediaCurlException(url, err, _curlError));
+ }
+ catch (const MediaException & excpt_r)
{
- DBG << "assert_dir " << dest.dirname() << " failed" << endl;
- ZYPP_THROW( MediaSystemException(_url, "System error on " + dest.dirname().asString()) );
+ ZYPP_RETHROW(excpt_r);
}
+ }
+ else
+ {
+ // actually the code is 0, nothing happened
+ }
+}
- DBG << "URL: " << url.asString().c_str() << endl;
- // Use URL without options (not RFC conform) and without
- // username and passwd (some proxies dislike them in the URL.
- // Curloptions for these were set in attachTo().
- Url curlUrl( url );
- curlUrl.setUsername( "" );
- curlUrl.setPassword( "" );
-#warning Check whether the call is correct
-// string urlBuffer = curlUrl.asString(true,false,true); // without options
- string urlBuffer = curlUrl.asString(); // without options
+///////////////////////////////////////////////////////////////////
- CURLcode ret = curl_easy_setopt( _curl, CURLOPT_URL,
- urlBuffer.c_str() );
+bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const
+{
+ DBG << filename.asString() << endl;
+
+ if(!_url.isValid())
+ ZYPP_THROW(MediaBadUrlException(_url));
+
+ if(_url.getHost().empty())
+ ZYPP_THROW(MediaBadUrlEmptyHostException(_url));
+
+ Url url(getFileUrl(filename));
+
+ DBG << "URL: " << url.asString() << endl;
+ // Use URL without options and without username and passwd
+ // (some proxies dislike them in the URL).
+ // Curl seems to need the just scheme, hostname and a path;
+ // the rest was already passed as curl options (in attachTo).
+ Url curlUrl( clearQueryString(url) );
+
+ //
+ // See also Bug #154197 and ftp url definition in RFC 1738:
+ // The url "ftp://user@host/foo/bar/file" contains a path,
+ // that is relative to the user's home.
+ // The url "ftp://user@host//foo/bar/file" (or also with
+ // encoded slash as %2f) "ftp://user@host/%2ffoo/bar/file"
+ // contains an absolute path.
+ //
+ string urlBuffer( curlUrl.asString());
+ CURLcode ret = curl_easy_setopt( _curl, CURLOPT_URL,
+ urlBuffer.c_str() );
+ if ( ret != 0 ) {
+ ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
+ }
+
+ // instead of returning no data with NOBODY, we return
+ // little data, that works with broken servers, and
+ // works for ftp as well, because retrieving only headers
+ // ftp will return always OK code ?
+ // See http://curl.haxx.se/docs/knownbugs.html #58
+ if ( (_url.getScheme() == "http" || _url.getScheme() == "https") &&
+ _settings.headRequestsAllowed() )
+ ret = curl_easy_setopt( _curl, CURLOPT_NOBODY, 1L );
+ else
+ ret = curl_easy_setopt( _curl, CURLOPT_RANGE, "0-1" );
+
+ if ( ret != 0 ) {
+ curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L);
+ curl_easy_setopt( _curl, CURLOPT_RANGE, NULL );
+ /* yes, this is why we never got to get NOBODY working before,
+ because setting it changes this option too, and we also
+ need to reset it
+ See: http://curl.haxx.se/mail/archive-2005-07/0073.html
+ */
+ curl_easy_setopt( _curl, CURLOPT_HTTPGET, 1L );
+ ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
+ }
+
+ FILE *file = ::fopen( "/dev/null", "w" );
+ if ( !file ) {
+ ::fclose(file);
+ ERR << "fopen failed for /dev/null" << endl;
+ curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L);
+ curl_easy_setopt( _curl, CURLOPT_RANGE, NULL );
+ /* yes, this is why we never got to get NOBODY working before,
+ because setting it changes this option too, and we also
+ need to reset it
+ See: http://curl.haxx.se/mail/archive-2005-07/0073.html
+ */
+ curl_easy_setopt( _curl, CURLOPT_HTTPGET, 1L );
+ if ( ret != 0 ) {
+ ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
+ }
+ ZYPP_THROW(MediaWriteException("/dev/null"));
+ }
+
+ ret = curl_easy_setopt( _curl, CURLOPT_WRITEDATA, file );
+ if ( ret != 0 ) {
+ ::fclose(file);
+ std::string err( _curlError);
+ curl_easy_setopt( _curl, CURLOPT_RANGE, NULL );
+ curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L);
+ /* yes, this is why we never got to get NOBODY working before,
+ because setting it changes this option too, and we also
+ need to reset it
+ See: http://curl.haxx.se/mail/archive-2005-07/0073.html
+ */
+ curl_easy_setopt( _curl, CURLOPT_HTTPGET, 1L );
+ if ( ret != 0 ) {
+ ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
+ }
+ ZYPP_THROW(MediaCurlSetOptException(url, err));
+ }
+
+ CURLcode ok = curl_easy_perform( _curl );
+ MIL << "perform code: " << ok << " [ " << curl_easy_strerror(ok) << " ]" << endl;
+
+ // reset curl settings
+ if ( _url.getScheme() == "http" || _url.getScheme() == "https" )
+ {
+ curl_easy_setopt( _curl, CURLOPT_NOBODY, 0L);
+ if ( ret != 0 ) {
+ ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
+ }
+
+ /* yes, this is why we never got to get NOBODY working before,
+ because setting it changes this option too, and we also
+ need to reset it
+ See: http://curl.haxx.se/mail/archive-2005-07/0073.html
+ */
+ curl_easy_setopt( _curl, CURLOPT_HTTPGET, 1L);
+ if ( ret != 0 ) {
+ ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
+ }
+
+ }
+ else
+ {
+ // for FTP we set different options
+ curl_easy_setopt( _curl, CURLOPT_RANGE, NULL);
if ( ret != 0 ) {
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
+ ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
+ }
+ }
+
+ // if the code is not zero, close the file
+ if ( ok != 0 )
+ ::fclose(file);
+
+ // as we are not having user interaction, the user can't cancel
+ // the file existence checking, a callback or timeout return code
+ // will be always a timeout.
+ try {
+ evaluateCurlCode( filename, ok, true /* timeout */);
+ }
+ catch ( const MediaFileNotFoundException &e ) {
+ // if the file did not exist then we can return false
+ return false;
+ }
+ catch ( const MediaException &e ) {
+ // some error, we are not sure about file existence, rethrw
+ ZYPP_RETHROW(e);
+ }
+ // exists
+ return ( ok == CURLE_OK );
+}
+
+///////////////////////////////////////////////////////////////////
+
+
+#if DETECT_DIR_INDEX
+bool MediaCurl::detectDirIndex() const
+{
+ if(_url.getScheme() != "http" && _url.getScheme() != "https")
+ return false;
+ //
+ // try to check the effective url and set the not_a_file flag
+ // if the url path ends with a "/", what usually means, that
+ // we've received a directory index (index.html content).
+ //
+ // Note: This may be dangerous and break file retrieving in
+ // case of some server redirections ... ?
+ //
+ bool not_a_file = false;
+ char *ptr = NULL;
+ CURLcode ret = curl_easy_getinfo( _curl,
+ CURLINFO_EFFECTIVE_URL,
+ &ptr);
+ if ( ret == CURLE_OK && ptr != NULL)
+ {
+ try
+ {
+ Url eurl( ptr);
+ std::string path( eurl.getPathName());
+ if( !path.empty() && path != "/" && *path.rbegin() == '/')
+ {
+ DBG << "Effective url ("
+ << eurl
+ << ") seems to provide the index of a directory"
+ << endl;
+ not_a_file = true;
+ }
}
+ catch( ... )
+ {}
+ }
+ return not_a_file;
+}
+#endif
+///////////////////////////////////////////////////////////////////
+
+void MediaCurl::doGetFileCopy( const Pathname & filename , const Pathname & target, callback::SendReport<DownloadProgressReport> & report, RequestOptions options ) const
+{
+ Pathname dest = target.absolutename();
+ if( assert_dir( dest.dirname() ) )
+ {
+ DBG << "assert_dir " << dest.dirname() << " failed" << endl;
+ Url url(getFileUrl(filename));
+ ZYPP_THROW( MediaSystemException(url, "System error on " + dest.dirname().asString()) );
+ }
string destNew = target.asString() + ".new.zypp.XXXXXX";
char *buf = ::strdup( destNew.c_str());
if( !buf)
{
ERR << "out of memory for temp file name" << endl;
- ZYPP_THROW(MediaSystemException(
- _url, "out of memory for temp file name"
- ));
+ Url url(getFileUrl(filename));
+ ZYPP_THROW(MediaSystemException(url, "out of memory for temp file name"));
}
int tmp_fd = ::mkstemp( buf );
DBG << "dest: " << dest << endl;
DBG << "temp: " << destNew << endl;
- ret = curl_easy_setopt( _curl, CURLOPT_WRITEDATA, file );
- if ( ret != 0 ) {
+ // set IFMODSINCE time condition (no download if not modified)
+ if( PathInfo(target).isExist() && !(options & OPTION_NO_IFMODSINCE) )
+ {
+ curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
+ curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, (long)PathInfo(target).mtime());
+ }
+ else
+ {
+ curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
+ curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, 0L);
+ }
+ try
+ {
+ doGetFileCopyFile(filename, dest, file, report, options);
+ }
+ catch (Exception &e)
+ {
+ ::fclose( file );
+ filesystem::unlink( destNew );
+ curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
+ curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, 0L);
+ ZYPP_RETHROW(e);
+ }
+
+ long httpReturnCode = 0;
+ CURLcode infoRet = curl_easy_getinfo(_curl,
+ CURLINFO_RESPONSE_CODE,
+ &httpReturnCode);
+ bool modified = true;
+ if (infoRet == CURLE_OK)
+ {
+ DBG << "HTTP response: " + str::numstring(httpReturnCode);
+ if ( httpReturnCode == 304
+ || ( httpReturnCode == 213 && _url.getScheme() == "ftp" ) ) // not modified
+ {
+ DBG << " Not modified.";
+ modified = false;
+ }
+ DBG << endl;
+ }
+ else
+ {
+ WAR << "Could not get the reponse code." << endl;
+ }
+
+ if (modified || infoRet != CURLE_OK)
+ {
+ // apply umask
+ if ( ::fchmod( ::fileno(file), filesystem::applyUmaskTo( 0644 ) ) )
+ {
+ ERR << "Failed to chmod file " << destNew << endl;
+ }
+ if (::fclose( file ))
+ {
+ ERR << "Fclose failed for file '" << destNew << "'" << endl;
+ ZYPP_THROW(MediaWriteException(destNew));
+ }
+ // move the temp file into dest
+ if ( rename( destNew, dest ) != 0 ) {
+ ERR << "Rename failed" << endl;
+ ZYPP_THROW(MediaWriteException(dest));
+ }
+ }
+ else
+ {
+ // close and remove the temp file
::fclose( file );
filesystem::unlink( destNew );
- ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
+ }
+
+ DBG << "done: " << PathInfo(dest) << endl;
+}
+
+///////////////////////////////////////////////////////////////////
+
+void MediaCurl::doGetFileCopyFile( const Pathname & filename , const Pathname & dest, FILE *file, callback::SendReport<DownloadProgressReport> & report, RequestOptions options ) const
+{
+ DBG << filename.asString() << endl;
+
+ if(!_url.isValid())
+ ZYPP_THROW(MediaBadUrlException(_url));
+
+ if(_url.getHost().empty())
+ ZYPP_THROW(MediaBadUrlEmptyHostException(_url));
+
+ Url url(getFileUrl(filename));
+
+ DBG << "URL: " << url.asString() << endl;
+ // Use URL without options and without username and passwd
+ // (some proxies dislike them in the URL).
+ // Curl seems to need the just scheme, hostname and a path;
+ // the rest was already passed as curl options (in attachTo).
+ Url curlUrl( clearQueryString(url) );
+
+ //
+ // See also Bug #154197 and ftp url definition in RFC 1738:
+ // The url "ftp://user@host/foo/bar/file" contains a path,
+ // that is relative to the user's home.
+ // The url "ftp://user@host//foo/bar/file" (or also with
+ // encoded slash as %2f) "ftp://user@host/%2ffoo/bar/file"
+ // contains an absolute path.
+ //
+ string urlBuffer( curlUrl.asString());
+ CURLcode ret = curl_easy_setopt( _curl, CURLOPT_URL,
+ urlBuffer.c_str() );
+ if ( ret != 0 ) {
+ ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
+ }
+
+ ret = curl_easy_setopt( _curl, CURLOPT_WRITEDATA, file );
+ if ( ret != 0 ) {
+ ZYPP_THROW(MediaCurlSetOptException(url, _curlError));
}
// Set callback and perform.
- report->start(url, dest);
- if ( curl_easy_setopt( _curl, CURLOPT_PROGRESSDATA, &report ) != 0 ) {
+ ProgressData progressData(_settings.timeout(), url, &report);
+ if (!(options & OPTION_NO_REPORT_START))
+ report->start(url, dest);
+ if ( curl_easy_setopt( _curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
WAR << "Can't set CURLOPT_PROGRESSDATA: " << _curlError << endl;;
}
WAR << "Can't unset CURLOPT_PROGRESSDATA: " << _curlError << endl;;
}
- if ( ret != 0 ) {
- ::fclose( file );
- filesystem::unlink( destNew );
+ if ( ret != 0 )
+ {
+ ERR << "curl error: " << ret << ": " << _curlError
+ << ", temp file size " << ftell(file)
+ << " bytes." << endl;
- ERR << "curl error: " << ret << ": " << _curlError << endl;
- std::string err;
+ // the timeout is determined by the progress data object
+ // which holds wheter the timeout was reached or not,
+ // otherwise it would be a user cancel
try {
- switch ( ret ) {
- case CURLE_UNSUPPORTED_PROTOCOL:
- case CURLE_URL_MALFORMAT:
- case CURLE_URL_MALFORMAT_USER:
- err = "Bad URL";
- case CURLE_HTTP_NOT_FOUND:
- {
- long httpReturnCode;
- CURLcode infoRet = curl_easy_getinfo( _curl, CURLINFO_HTTP_CODE,
- &httpReturnCode );
- if ( infoRet == CURLE_OK ) {
- string msg = "HTTP return code: " +
- str::numstring( httpReturnCode ) +
- " (URL: " + url.asString() + ")";
- DBG << msg << endl;
- if ( httpReturnCode == 401 )
- {
- msg = "URL: " + url.asString();
- err = "Login failed";
- }
- else
- {
- err = "File not found";
- }
- ZYPP_THROW( MediaCurlException(_url, err, _curlError));
- }
- }
- break;
- case CURLE_FTP_COULDNT_RETR_FILE:
- case CURLE_FTP_ACCESS_DENIED:
- err = "File not found";
- break;
- case CURLE_BAD_PASSWORD_ENTERED:
- case CURLE_FTP_USER_PASSWORD_INCORRECT:
- err = "Login failed";
- break;
- case CURLE_COULDNT_RESOLVE_PROXY:
- case CURLE_COULDNT_RESOLVE_HOST:
- case CURLE_COULDNT_CONNECT:
- case CURLE_FTP_CANT_GET_HOST:
- err = "Connection failed";
- break;
- case CURLE_WRITE_ERROR:
- err = "Write error";
- break;
- case CURLE_ABORTED_BY_CALLBACK:
- err = "User abort";
- break;
- case CURLE_SSL_PEER_CERTIFICATE:
- default:
- err = "Unrecognized error";
- break;
- }
- ZYPP_THROW(MediaCurlException(_url, err, _curlError));
+ evaluateCurlCode( filename, ret, progressData.reached);
}
- catch (const MediaException & excpt_r)
- {
- ZYPP_RETHROW(excpt_r);
+ catch ( const MediaException &e ) {
+ // some error, we are not sure about file existence, rethrw
+ ZYPP_RETHROW(e);
}
}
- mode_t mask;
- // getumask() would be fine, but does not exist
- // [ the linker can't find it in glibc :-( ].
- mask = ::umask(0022); ::umask(mask);
- if ( ::fchmod( ::fileno(file), 0644 & ~mask))
- {
- ERR << "Failed to chmod file " << destNew << endl;
- }
- ::fclose( file );
-
- if ( rename( destNew, dest ) != 0 ) {
- ERR << "Rename failed" << endl;
- ZYPP_THROW(MediaWriteException(dest));
- }
+#if DETECT_DIR_INDEX
+ if (!ret && detectDirIndex())
+ {
+ ZYPP_THROW(MediaNotAFileException(_url, filename));
+ }
+#endif // DETECT_DIR_INDEX
}
-
///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : MediaCurl::getDir
-// METHOD TYPE : PMError
-//
-// DESCRIPTION : Asserted that media is attached
-//
+
void MediaCurl::getDir( const Pathname & dirname, bool recurse_r ) const
{
filesystem::DirContent content;
switch ( it->type ) {
case filesystem::FT_NOT_AVAIL: // old directory.yast contains no typeinfo at all
case filesystem::FT_FILE:
- getFile( filename );
- break;
+ getFile( filename );
+ break;
case filesystem::FT_DIR: // newer directory.yast contain at least directory info
- if ( recurse_r ) {
- getDir( filename, recurse_r );
- } else {
- res = assert_dir( localPath( filename ) );
- if ( res ) {
- WAR << "Ignore error (" << res << ") on creating local directory '" << localPath( filename ) << "'" << endl;
- }
- }
- break;
+ if ( recurse_r ) {
+ getDir( filename, recurse_r );
+ } else {
+ res = assert_dir( localPath( filename ) );
+ if ( res ) {
+ WAR << "Ignore error (" << res << ") on creating local directory '" << localPath( filename ) << "'" << endl;
+ }
+ }
+ break;
default:
- // don't provide devices, sockets, etc.
- break;
+ // don't provide devices, sockets, etc.
+ break;
}
}
}
///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : MediaCurl::getDirInfo
-// METHOD TYPE : PMError
-//
-// DESCRIPTION : Asserted that media is attached and retlist is empty.
-//
+
void MediaCurl::getDirInfo( std::list<std::string> & retlist,
- const Pathname & dirname, bool dots ) const
+ const Pathname & dirname, bool dots ) const
{
getDirectoryYast( retlist, dirname, dots );
}
///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : MediaCurl::getDirInfo
-// METHOD TYPE : PMError
-//
-// DESCRIPTION : Asserted that media is attached and retlist is empty.
-//
+
void MediaCurl::getDirInfo( filesystem::DirContent & retlist,
const Pathname & dirname, bool dots ) const
{
}
///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : MediaCurl::progressCallback
-// METHOD TYPE : int
-//
-// DESCRIPTION : Progress callback triggered from MediaCurl::getFile
-//
-int MediaCurl::progressCallback( void *clientp, double dltotal, double dlnow,
- double ultotal, double ulnow )
+
+int MediaCurl::progressCallback( void *clientp,
+ double dltotal, double dlnow,
+ double ultotal, double ulnow)
{
- callback::SendReport<DownloadProgressReport> *report
- = reinterpret_cast<callback::SendReport<DownloadProgressReport>*>( clientp );
- if (report)
+ ProgressData *pdata = reinterpret_cast<ProgressData *>(clientp);
+ if( pdata)
{
- // FIXME: empty URL
- if (! (*report)->progress(int( dlnow * 100 / dltotal ), Url() ))
- return 1;
+ time_t now = time(NULL);
+ if( now > 0)
+ {
+ // reset time of last change in case initial time()
+ // failed or the time was adjusted (goes backward)
+ if( pdata->ltime <= 0 || pdata->ltime > now)
+ {
+ pdata->ltime = now;
+ }
+
+ // start time counting as soon as first data arrives
+ // (skip the connection / redirection time at begin)
+ time_t dif = 0;
+ if (dlnow > 0 || ulnow > 0)
+ {
+ dif = (now - pdata->ltime);
+ dif = dif > 0 ? dif : 0;
+
+ pdata->secs += dif;
+ }
+
+ // update the drate_avg and drate_period only after a second has passed
+ // (this callback is called much more often than a second)
+ // otherwise the values would be far from accurate when measuring
+ // the time in seconds
+ //! \todo more accurate download rate computationn, e.g. compute average value from last 5 seconds, or work with milliseconds instead of seconds
+
+ if ( pdata->secs > 1 && (dif > 0 || dlnow == dltotal ))
+ pdata->drate_avg = (dlnow / pdata->secs);
+
+ if ( dif > 0 )
+ {
+ pdata->drate_period = ((dlnow - pdata->dload_period) / dif);
+ pdata->dload_period = dlnow;
+ }
+ }
+
+ // send progress report first, abort transfer if requested
+ if( pdata->report)
+ {
+ if (!(*(pdata->report))->progress(int( dltotal ? dlnow * 100 / dltotal : 0 ),
+ pdata->url,
+ pdata->drate_avg,
+ pdata->drate_period))
+ {
+ return 1; // abort transfer
+ }
+ }
+
+ // check if we there is a timeout set
+ if( pdata->timeout > 0)
+ {
+ if( now > 0)
+ {
+ bool progress = false;
+
+ // update download data if changed, mark progress
+ if( dlnow != pdata->dload)
+ {
+ progress = true;
+ pdata->dload = dlnow;
+ pdata->ltime = now;
+ }
+ // update upload data if changed, mark progress
+ if( ulnow != pdata->uload)
+ {
+ progress = true;
+ pdata->uload = ulnow;
+ pdata->ltime = now;
+ }
+
+ if( !progress && (now >= (pdata->ltime + pdata->timeout)))
+ {
+ pdata->reached = true;
+ return 1; // aborts transfer
+ }
+ }
+ }
}
return 0;
}
+///////////////////////////////////////////////////////////////////
+
+string MediaCurl::getAuthHint() const
+{
+ long auth_info = CURLAUTH_NONE;
+
+ CURLcode infoRet =
+ curl_easy_getinfo(_curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
+
+ if(infoRet == CURLE_OK)
+ {
+ return CurlAuthData::auth_type_long2str(auth_info);
+ }
+
+ return "";
+}
+
+///////////////////////////////////////////////////////////////////
+
+bool MediaCurl::authenticate(const string & availAuthTypes, bool firstTry) const
+{
+ //! \todo need a way to pass different CredManagerOptions here
+ Target_Ptr target = zypp::getZYpp()->getTarget();
+ CredentialManager cm(CredManagerOptions(target ? target->root() : ""));
+ CurlAuthData_Ptr credentials;
+
+ // get stored credentials
+ AuthData_Ptr cmcred = cm.getCred(_url);
+
+ if (cmcred && firstTry)
+ {
+ credentials.reset(new CurlAuthData(*cmcred));
+ DBG << "got stored credentials:" << endl << *credentials << endl;
+ }
+ // if not found, ask user
+ else
+ {
+
+ CurlAuthData_Ptr curlcred;
+ curlcred.reset(new CurlAuthData());
+ callback::SendReport<AuthenticationReport> auth_report;
+
+ // preset the username if present in current url
+ if (!_url.getUsername().empty() && firstTry)
+ curlcred->setUsername(_url.getUsername());
+ // if CM has found some credentials, preset the username from there
+ else if (cmcred)
+ curlcred->setUsername(cmcred->username());
+
+ // indicate we have no good credentials from CM
+ cmcred.reset();
+
+ string prompt_msg = boost::str(boost::format(
+ //!\todo add comma to the message for the next release
+ _("Authentication required for '%s'")) % _url.asString());
+
+ // set available authentication types from the exception
+ // might be needed in prompt
+ curlcred->setAuthType(availAuthTypes);
+
+ // ask user
+ if (auth_report->prompt(_url, prompt_msg, *curlcred))
+ {
+ DBG << "callback answer: retry" << endl
+ << "CurlAuthData: " << *curlcred << endl;
+
+ if (curlcred->valid())
+ {
+ credentials = curlcred;
+ // if (credentials->username() != _url.getUsername())
+ // _url.setUsername(credentials->username());
+ /**
+ * \todo find a way to save the url with changed username
+ * back to repoinfo or dont store urls with username
+ * (and either forbid more repos with the same url and different
+ * user, or return a set of credentials from CM and try them one
+ * by one)
+ */
+ }
+ }
+ else
+ {
+ DBG << "callback answer: cancel" << endl;
+ }
+ }
+
+ // set username and password
+ if (credentials)
+ {
+ // HACK, why is this const?
+ const_cast<MediaCurl*>(this)->_settings.setUsername(credentials->username());
+ const_cast<MediaCurl*>(this)->_settings.setPassword(credentials->password());
+
+ // set username and password
+ CURLcode ret = curl_easy_setopt(_curl, CURLOPT_USERPWD, _settings.userPassword().c_str());
+ if ( ret != 0 ) ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
+
+ // set available authentication types from the exception
+ if (credentials->authType() == CURLAUTH_NONE)
+ credentials->setAuthType(availAuthTypes);
+
+ // set auth type (seems this must be set _after_ setting the userpwd)
+ if (credentials->authType() != CURLAUTH_NONE)
+ {
+ // FIXME: only overwrite if not empty?
+ const_cast<MediaCurl*>(this)->_settings.setAuthType(credentials->authTypeAsString());
+ ret = curl_easy_setopt(_curl, CURLOPT_HTTPAUTH, credentials->authType());
+ if ( ret != 0 ) ZYPP_THROW(MediaCurlSetOptException(_url, _curlError));
+ }
+
+ if (!cmcred)
+ {
+ credentials->setUrl(_url);
+ cm.addCred(*credentials);
+ cm.save();
+ }
+
+ return true;
+ }
+
+ return false;
+}
+
} // namespace media
} // namespace zypp
+//