3 #include <zypp/PathInfo.h> 4 #include <zypp/Pathname.h> 6 #include <zypp/base/Logger.h> 7 #include <zypp/base/String.h> 22 inline int getZYPP_MEDIA_CURL_IPRESOLVE()
25 if (
const char * envp = getenv(
"ZYPP_MEDIA_CURL_IPRESOLVE" ) )
27 WAR <<
"env set: $ZYPP_MEDIA_CURL_IPRESOLVE='" << envp <<
"'" << std::endl;
28 if ( strcmp( envp,
"4" ) == 0 ) ret = 4;
29 else if ( strcmp( envp,
"6" ) == 0 ) ret = 6;
37 static int _v = getZYPP_MEDIA_CURL_IPRESOLVE();
49 static bool once __attribute__ ((__unused__)) = ( [] {
50 if ( curl_global_init( CURL_GLOBAL_ALL ) != 0 )
51 WAR <<
"curl global init failed" << std::endl;
56 char *ptr,
size_t len,
void *max_lvl)
58 if ( max_lvl ==
nullptr )
61 long maxlvl = *((
long *)max_lvl);
66 case CURLINFO_TEXT:
if ( maxlvl < 1 )
return 0; pfx =
'*';
break;
67 case CURLINFO_HEADER_IN:
if ( maxlvl < 2 )
return 0; pfx =
'<';
break;
68 case CURLINFO_HEADER_OUT:
if ( maxlvl < 2 )
return 0; pfx =
'>';
break;
73 std::vector<std::string> lines;
74 str::split( std::string(ptr,len), std::back_inserter(lines),
"\r\n" );
75 for(
const auto & line : lines )
79 if ( pos == std::string::npos )
81 DBG << pfx <<
" " << line.substr( 0, pos ) <<
" <credentials removed>" << std::endl;
84 DBG << pfx <<
" " << line << std::endl;
93 char * lstart = ptr, * lend = ptr;
95 size_t max = size * nmemb;
99 for (lstart = lend; *lend !=
'\n' && pos < max; ++lend, ++pos);
102 if ( strncasecmp( lstart,
"Location:", 9 ) == 0 )
104 std::string line { lstart, *(lend-1)==
'\r' ? lend-1 : lend };
105 DBG <<
"redirecting to " << line << std::endl;
107 *
reinterpret_cast<std::string *
>( userdata ) = line;
133 if( ! param.empty() )
135 long num = str::strtonum<long>(param);
142 if ( ! param.empty() )
146 if ( ! param.empty() )
161 const std::string & verify { url.
getQueryParam(
"ssl_verify") };
162 if( verify.empty() || verify ==
"yes" )
167 else if ( verify ==
"no" )
174 std::vector<std::string> flags;
175 str::split( verify, std::back_inserter(flags),
"," );
176 for (
const auto & flag : flags )
178 if ( flag ==
"host" )
180 else if ( flag ==
"peer" )
189 if( ! ca_path.empty() )
199 if( ! client_cert.empty() )
201 if( !
PathInfo(client_cert).
isFile() || ! client_cert.absolute() )
209 if( ! client_key.empty() )
219 if ( ! param.empty() )
230 const std::string & proxyport { url.
getQueryParam(
"proxyport" ) };
231 if ( ! proxyport.empty() ) {
242 if ( ! param.empty() )
259 DBG <<
"Rethrowing as MediaUnauthorizedException.";
267 const std::string & param { url.
getQueryParam(
"head_requests") };
268 if( ! param.empty() && param ==
"no" )
306 static const std::string
_value(
308 "X-ZYpp-AnonymousId: %s",
320 static const std::string
_value(
322 "X-ZYpp-DistributionFlavor: %s",
334 static const std::string
_value(
336 "ZYpp " LIBZYPP_VERSION_STRING
" (curl %s) %s" 337 , curl_version_info(CURLVERSION_NOW)->version
345 const char char_r,
const std::string & escaped_r ) {
347 pos != std::string::npos; pos = str_r.find( char_r, pos ) ) {
348 str_r.replace( pos, 1, escaped_r );
358 char * tmp = curl_unescape( text_r.c_str(), 0 );
359 std::string ret( tmp );
392 for ( std::string param : {
"proxy",
"proxyport",
"proxyuser",
"proxypass"} )
394 const std::string & value( template_r.
getQueryParam( param ) );
395 if ( ! value.empty() )
404 , timeout( _timeout )
406 , fileSizeExceeded ( false )
408 , _expectedFileSize( expectedFileSize_r )
419 if ( dlnow && dlnow !=
_dnlNow )
std::string getScheme() const
Returns the scheme name of the URL.
void setPassword(const std::string &pass, EEncoding eflag=zypp::url::E_DECODED)
Set the password in the URL authority.
void globalInitCurlOnce()
std::string targetDistribution() const
This is register.target attribute of the installed base product.
size_t log_redirects_curl(char *ptr, size_t size, size_t nmemb, void *userdata)
void setQueryParam(const std::string ¶m, const std::string &value)
Set or add value for the specified query parameter.
ProgressData()
Ctor no range [0,0](0).
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
const char * anonymousIdHeader()
initialized only once, this gets the anonymous id from the target, which we pass in the http header ...
zypp::Url propagateQueryParams(zypp::Url url_r, const zypp::Url &template_r)
Flag to request encoded string(s).
Store and operate with byte count.
#define TRANSFER_TIMEOUT_MAX
int reportProgress() const
Url clearQueryString(const Url &url)
static const ViewOption WITH_SCHEME
Option to include scheme name in the URL string.
static const ViewOption WITH_HOST
Option to include hostname in the URL string.
void setPathParams(const std::string ¶ms)
Set the path parameters.
std::string curlEscapedPath(std::string path_r)
void setUsername(const std::string &user, EEncoding eflag=zypp::url::E_DECODED)
Set the username in the URL authority.
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
const char * distributionFlavorHeader()
initialized only once, this gets the distribution flavor from the target, which we pass in the http h...
void setFragment(const std::string &fragment, EEncoding eflag=zypp::url::E_DECODED)
Set the fragment string in the URL.
int ZYPP_MEDIA_CURL_IPRESOLVE()
4/6 to force IPv4/v6
void curlEscape(std::string &str_r, const char char_r, const std::string &escaped_r)
double _dnlTotal
Bytes to download or 0 if unknown.
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \, const Trim trim_r=NO_TRIM)
Split line_r into words.
std::string getQueryParam(const std::string ¶m, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
std::string trim(const std::string &s, const Trim trim_r)
bool startsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasPrefix
double _dnlLast
Bytes downloaded at period start.
void fillSettingsFromUrl(const Url &url, media::TransferSettings &s)
Fills the settings structure using options passed on the url for example ?timeout=x&proxy=foo.
zypp::ByteCount _expectedFileSize
int _dnlPercent
Percent completed or 0 if _dnlTotal is unknown.
double _drateLast
Download rate in last period.
std::string curlUnEscape(std::string text_r)
time_t _timeStart
Start total stats.
double _dnlNow
Bytes downloaded now.
Wrapper class for ::stat/::lstat.
const char * agentString()
initialized only once, this gets the agent string which also includes the curl version ...
void updateStats(double dltotal=0.0, double dlnow=0.0)
void fillSettingsSystemProxy(const Url &url, media::TransferSettings &s)
Reads the system proxy configuration and fills the settings structure proxy information.
double _drateTotal
Download rate so far.
std::string anonymousUniqueId() const
anonymous unique id
time_t _timeRcv
Start of no-data timeout.
static const ViewOption WITH_PORT
Option to include port number in the URL string.
Easy-to use interface to the ZYPP dependency resolver.
std::string distributionFlavor() const
This is flavor attribute of the installed base product but does not require the target to be loaded a...
int log_curl(CURL *curl, curl_infotype info, char *ptr, size_t len, void *max_lvl)
time_t _timeLast
Start last period(~1sec)
std::string getPassword(EEncoding eflag=zypp::url::E_DECODED) const
Returns the password from the URL authority.
const char * c_str() const
#define EXPLICITLY_NO_PROXY
void delQueryParam(const std::string ¶m)
remove the specified query parameter.
zypp::callback::SendReport< zypp::media::DownloadProgressReport > * report
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.
const std::string & msg() const
Return the message string provided to the ctor.