21#include <zypp-curl/ProxyInfo>
22#include <zypp-curl/auth/CurlAuthData>
23#include <zypp-media/MediaException>
27#define TRANSFER_TIMEOUT_MAX 60 * 60
38 static const long ret = [](){
39 const char *
env =
getenv(
"ZYPP_MEDIA_CURL_DEBUG");
40 return env && *
env ? str::strtonum<ulong>(
env ) : 0;
49 if (
const char *
envp =
getenv(
"ZYPP_MEDIA_CURL_IPRESOLVE" ) ) {
50 WAR <<
"env set: $ZYPP_MEDIA_CURL_IPRESOLVE='" <<
envp <<
"'" << std::endl;
70 WAR <<
"curl global init failed" << std::endl;
77 return curlV->version_num;
86 const char *
pfx =
"";
105 std::vector<std::string_view> lines;
106 strv::split( std::string_view(
ptr, len ),
"\n", [&lines]( std::string_view
line,
unsigned,
bool last ) {
109 lines.push_back(
line );
111 for (
const auto &
line : lines ) {
113 std::string_view::size_type pos {
line.find(
" ", 15 ) };
114 if ( pos == std::string::npos )
116 DBG << curl <<
" " <<
pfx <<
" " <<
line.substr( 0, pos ) <<
" <credentials removed>" << endl;
119 DBG << curl <<
" " <<
pfx <<
" " <<
line << endl;
123 DBG << curl <<
" " <<
pfx <<
" " << len <<
" byte" << endl;
133 INT <<
"Got a NULL curl handle" << endl;
149 size_t max = size *
nmemb;
150 while (pos + 1 < max)
159 DBG <<
"redirecting to " <<
line << std::endl;
161 *
reinterpret_cast<std::string *
>( userdata ) =
line;
187 if( !
param.empty() )
189 long num = str::strtonum<long>(
param);
196 if ( !
param.empty() )
198 s.setUsername( std::move(
param) );
200 if ( !
param.empty() )
201 s.setPassword( std::move(
param) );
207 s.setAnonymousAuth();
212 s.setVerifyPeerEnabled(
false );
213 s.setVerifyHostEnabled(
false );
215 const std::string & verify { url.
getQueryParam(
"ssl_verify") };
216 if( verify.empty() || verify ==
"yes" )
218 s.setVerifyPeerEnabled(
true );
219 s.setVerifyHostEnabled(
true );
221 else if ( verify ==
"no" )
223 s.setVerifyPeerEnabled(
false );
224 s.setVerifyHostEnabled(
false );
228 std::vector<std::string> flags;
229 str::split( verify, std::back_inserter(flags),
"," );
230 for (
const auto &
flag : flags )
232 if (
flag ==
"host" )
233 s.setVerifyHostEnabled(
true );
234 else if (
flag ==
"peer" )
235 s.setVerifyPeerEnabled(
true );
248 s.setCertificateAuthoritiesPath( std::move(
ca_path) );
273 if ( !
param.empty() )
281 s.setProxyEnabled(
false);
289 s.setProxy( std::move(
param) );
290 s.setProxyEnabled(
true );
296 if ( !
param.empty() )
298 s.setProxyUsername( std::move(
param) );
313 DBG <<
"Rethrowing as MediaUnauthorizedException.";
316 s.setAuthType( std::move(
param) );
323 s.setHeadRequestsAllowed(
false );
340 s.setProxy(
u.asString( url::ViewOption::WITH_SCHEME + url::ViewOption::WITH_HOST + url::ViewOption::WITH_PORT ) );
342 if (
s.proxyUsername().empty() )
347 s.setProxyEnabled(
true );
355 for ( std::string::size_type pos =
str_r.find(
char_r );
356 pos != std::string::npos; pos =
str_r.find(
char_r, pos ) ) {
405 using namespace std::literals::string_literals;
406 for (
const std::string &
param : {
"proxy"s,
"proxyport"s,
"proxyuser"s,
"proxypass"s,
"ssl_capath"s,
"ssl_verify"s } )
409 if ( ! value.empty() )
430 auto it = std::find_if(
userp->socks.begin(),
userp->socks.end(), [&](
const GPollFD &
fd){ return fd.fd == s; });
433 if (
it ==
userp->socks.end() ) {
434 WAR <<
"Ignoring unknown socket in static_socketcb" << std::endl;
447 if (
it !=
userp->socks.end() ) {
451 userp->socks.push_back(
476 if (
waitFd.revents == 0 )
517#if CURLVERSION_AT_LEAST(7,19,4)
518#if CURLVERSION_AT_LEAST(7,85,0)
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
std::string getScheme() const
Returns the scheme name of the URL.
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.
std::string getQueryParam(const std::string ¶m, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
std::string getPassword(EEncoding eflag=zypp::url::E_DECODED) const
Returns the password from the URL authority.
Wrapper class for stat/lstat.
#define TRANSFER_TIMEOUT_MAX
#define EXPLICITLY_NO_PROXY
void fillSettingsFromUrl(const Url &url, media::TransferSettings &s)
Fills the settings structure using options passed on the url for example ?timeout=x&proxy=foo.
size_t log_redirects_curl(char *ptr, size_t size, size_t nmemb, void *userdata)
void globalInitCurlOnce()
zypp::Url propagateQueryParams(zypp::Url url_r, const zypp::Url &template_r)
std::string curlUnEscape(const std::string &text_r)
void setupZYPP_MEDIA_CURL_DEBUG(CURL *curl)
Setup CURLOPT_VERBOSE and CURLOPT_DEBUGFUNCTION according to env::ZYPP_MEDIA_CURL_DEBUG.
std::string curlEscapedPath(std::string path_r)
CURLcode setCurlRedirProtocols(CURL *curl)
void fillSettingsSystemProxy(const Url &url, media::TransferSettings &s)
Reads the system proxy configuration and fills the settings structure proxy information.
Url clearQueryString(const Url &url)
void curlEscape(std::string &str_r, const char char_r, const std::string &escaped_r)
int log_curl(CURL *curl, curl_infotype info, char *ptr, size_t len, void *max_lvl)
Namespace intended to collect all environment variables we use.
const long & ZYPP_MEDIA_CURL_DEBUG()
const long& for setting CURLOPT_DEBUGDATA Returns a reference to a static variable,...
int ZYPP_MEDIA_CURL_IPRESOLVE()
4/6 to force IPv4/v6
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", const Trim trim_r=NO_TRIM)
Split line_r into words.
@ E_ENCODED
Flag to request encoded string(s).
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & hexdumpOn(std::ostream &outs, const unsigned char *ptr, size_t size)
hexdump data on stream
static int socketcb(CURL *easy, curl_socket_t s, int what, CurlPollHelper *userp, void *sockp)
CurlPollHelper(CurlPoll &p)
CURLMcode handleSocketActions(const std::vector< GPollFD > &actionsFds, int first=0)
std::optional< long > timeout_ms
static int timercb(CURLM *, long timeout_ms, CurlPollHelper *thatPtr)
Provides API related macros.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.