Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom user agent string via zypp.conf download.user_agent (zypper#382) #299

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions zypp.conf
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@
##
# repo.refresh.locales = en, de

##
## Custom user agent string to send to a HTTP server.
##
## Valid values: String suitable as user agent HTTP header
##
## If not defined or empty the builtin default telling the zypp
## version, distribution and architecture is used.
##
# download.user_agent =

##
## Maximum number of concurrent connections to use per transfer
##
Expand Down
8 changes: 8 additions & 0 deletions zypp/ZConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ namespace zypp
download_mediaMountdir.restoreToDefault( Pathname(value) );
}

else if ( entry == "download.user_agent" )
{
download_user_agent = value;
}
else if ( entry == "download.max_concurrent_connections" )
{
str::strtonum(value, download_max_concurrent_connections);
Expand Down Expand Up @@ -668,6 +672,7 @@ namespace zypp
DefaultOption<bool> download_media_prefer_download;
DefaultOption<Pathname> download_mediaMountdir;

std::string download_user_agent;
int download_max_concurrent_connections;
int download_min_download_speed;
int download_max_download_speed;
Expand Down Expand Up @@ -1055,6 +1060,9 @@ namespace zypp
void ZConfig::set_default_download_media_prefer_download()
{ _pimpl->download_media_prefer_download.restoreToDefault(); }

std::string ZConfig::download_user_agent() const
{ return _pimpl->download_user_agent; }

long ZConfig::download_max_concurrent_connections() const
{ return _pimpl->download_max_concurrent_connections; }

Expand Down
6 changes: 6 additions & 0 deletions zypp/ZConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ namespace zypp
*/
void repoLabelIsAlias( bool yesno_r );


/**
* A custom user agent string to send to a HTTP server.
*/
std::string download_user_agent() const;

/**
* Maximum number of concurrent connections for a single transfer
*/
Expand Down
18 changes: 2 additions & 16 deletions zypp/media/CurlHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <zypp/media/MediaException.h>
#include <list>

#define TRANSFER_TIMEOUT_MAX 60 * 60

using std::endl;
using namespace zypp;

Expand Down Expand Up @@ -319,22 +321,6 @@ const char * distributionFlavorHeader()
return _value.c_str();
}

const char * 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 " LIBZYPP_VERSION_STRING " (curl %s) %s"
, curl_version_info(CURLVERSION_NOW)->version
, Target::targetDistribution( Pathname()/*guess root*/ ).c_str()
)
);
return _value.c_str();
}

void curlEscape( std::string & str_r,
const char char_r, const std::string & escaped_r ) {
for ( std::string::size_type pos = str_r.find( char_r );
Expand Down
10 changes: 0 additions & 10 deletions zypp/media/CurlHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
#include <zypp/Url.h>
#include <zypp/media/TransferSettings.h>

#define CONNECT_TIMEOUT 60
#define TRANSFER_TIMEOUT_MAX 60 * 60
#define DETECT_DIR_INDEX 0

#define EXPLICITLY_NO_PROXY "_none_"

#undef CURLVERSION_AT_LEAST
Expand Down Expand Up @@ -60,12 +56,6 @@ const char * anonymousIdHeader();
*/
const char * distributionFlavorHeader();

/**
* initialized only once, this gets the agent string
* which also includes the curl version
*/
const char * agentString();

void curlEscape( std::string & str_r, const char char_r, const std::string & escaped_r );
std::string curlEscapedPath( std::string path_r );
std::string curlUnEscape( std::string text_r );
Expand Down
55 changes: 0 additions & 55 deletions zypp/media/MediaCurl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,6 @@ void MediaCurl::setupEasy()
}
vol_settings.addHeader("Pragma:");

_settings.setTimeout(ZConfig::instance().download_transfer_timeout());
_settings.setConnectTimeout(CONNECT_TIMEOUT);

_settings.setUserAgentString(agentString());

// fill some settings from url query parameters
try
{
Expand Down Expand Up @@ -971,49 +966,6 @@ bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const

///////////////////////////////////////////////////////////////////


#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, const ByteCount &expectedFileSize_r, RequestOptions options ) const
{
Pathname dest = target.absolutename();
Expand Down Expand Up @@ -1220,13 +1172,6 @@ void MediaCurl::doGetFileCopyFile(const Pathname & filename , const Pathname & d
ZYPP_RETHROW(e);
}
}

#if DETECT_DIR_INDEX
if (!ret && detectDirIndex())
{
ZYPP_THROW(MediaNotAFileException(_url, filename));
}
#endif // DETECT_DIR_INDEX
}

///////////////////////////////////////////////////////////////////
Expand Down
2 changes: 0 additions & 2 deletions zypp/media/MediaCurl.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ class MediaCurl : public MediaHandler

bool authenticate(const std::string & availAuthTypes, bool firstTry) const;

bool detectDirIndex() const;

private:
long _curlDebug;

Expand Down
35 changes: 26 additions & 9 deletions zypp/media/TransferSettings.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#include <curl/curl.h> // for useragent version info

#include <iostream>
#include <sstream>

#include <zypp/base/String.h>
#include <zypp/base/Logger.h>
#include <zypp/base/WatchFile.h>
#include <zypp/base/ReferenceCounted.h>
#include <zypp/base/NonCopyable.h>
#include <zypp/ExternalProgram.h>
#include <zypp/media/TransferSettings.h>
#include <zypp/ZConfig.h>
#include <zypp/Target.h>

using std::endl;

Expand All @@ -18,6 +16,25 @@ namespace zypp
{
namespace media
{
namespace {
const std::string & defaultUserAgent()
{
// 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 {[](){
std::string ret { ZConfig::instance().download_user_agent() };
if ( ret.empty() )
ret = str::form( "ZYpp " LIBZYPP_VERSION_STRING " (curl %s) %s",
curl_version_info(CURLVERSION_NOW)->version,
Target::targetDistribution( Pathname()/*guess root*/ ).c_str() );
return ret;
}()};
return _value;
}
}

class TransferSettings::Impl
{
public:
Expand Down Expand Up @@ -53,7 +70,7 @@ namespace zypp

public:
std::vector<std::string> _headers;
std::string _useragent;
std::optional<std::string> _useragent;
std::string _username;
std::string _password;
bool _useproxy;
Expand Down Expand Up @@ -100,10 +117,10 @@ namespace zypp


void TransferSettings::setUserAgentString( std::string && val_r )
{ _impl->_useragent = std::move(val_r); }
{ if ( val_r.empty() ) _impl->_useragent.reset(); else _impl->_useragent = std::move(val_r); }

std::string TransferSettings::userAgentString() const
{ return _impl->_useragent; }
const std::string & TransferSettings::userAgentString() const
{ return _impl->_useragent.has_value() ? *(_impl->_useragent) : defaultUserAgent(); }


void TransferSettings::setUsername( std::string && val_r )
Expand Down
2 changes: 1 addition & 1 deletion zypp/media/TransferSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace zypp
void setUserAgentString( std::string && val_r );

/** user agent string */
std::string userAgentString() const;
const std::string & userAgentString() const;


/** sets the auth username */
Expand Down
5 changes: 0 additions & 5 deletions zypp/zyppng/media/network/request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,6 @@ namespace zyppng {

locSet.addHeader("Pragma:");

locSet.setTimeout( zypp::ZConfig::instance().download_transfer_timeout() );
locSet.setConnectTimeout( CONNECT_TIMEOUT );

locSet.setUserAgentString( internal::agentString() );

{
char *ptr = getenv("ZYPP_MEDIA_CURL_DEBUG");
_curlDebug = (ptr && *ptr) ? zypp::str::strtonum<long>( ptr) : 0L;
Expand Down