libzypp 17.38.4
MediaCurl.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
13#include <iostream>
14#include <chrono>
15#include <list>
16
21#include <utility>
22#include <zypp-core/parser/Sysconfig>
24
28#include <zypp-curl/ProxyInfo>
29#include <zypp-curl/auth/CurlAuthData>
30#include <zypp-media/auth/CredentialManager>
31#include <zypp-curl/CurlConfig>
32#include <zypp/Target.h>
33#include <zypp/ZYppFactory.h>
34#include <zypp/ZConfig.h>
35#include <zypp/zypp_detail/ZYppImpl.h> // for zypp_poll
36
37#include <cstdlib>
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <sys/mount.h>
41#include <dirent.h>
42#include <unistd.h>
43#include <glib.h>
44
46
47using std::endl;
48
49namespace internal {
50 using namespace zypp;
52 {
53 ProgressData( AutoFILE file, CURL *curl, time_t timeout = 0, zypp::Url url = zypp::Url(),
54 zypp::ByteCount expectedFileSize_r = 0,
56
57 void updateStats( curl_off_t dltotal = 0.0, curl_off_t dlnow = 0.0 );
58
59 int reportProgress() const;
60
61 CURL * curl()
62 { return _curl; }
63
64 bool timeoutReached() const
65 { return _timeoutReached; }
66
67 bool fileSizeExceeded() const
68 { return _fileSizeExceeded; }
69
72
73 void expectedFileSize( ByteCount newval_r )
74 { _expectedFileSize = newval_r; }
75
76 zypp::Url url() const
77 { return _url; }
78
79 FILE* file()
80 { return _file.value(); }
81
82 size_t writeBytes( char *ptr, ByteCount bytes );
83
85 return _bytesWritten;
86 }
87
88 private:
89 CURL * _curl;
92 time_t _timeout;
97
98 time_t _timeStart = 0;
99 time_t _timeLast = 0;
100 time_t _timeRcv = 0;
101 time_t _timeNow = 0;
102
103 curl_off_t _dnlTotal = 0.0;
104 curl_off_t _dnlLast = 0.0;
105 curl_off_t _dnlNow = 0.0;
106
108
109 int _dnlPercent= 0;
110
111 double _drateTotal= 0.0;
112 double _drateLast = 0.0;
113 };
114
115
116
118 : _curl( curl )
119 , _file( std::move(file) )
120 , _url(std::move( url ))
121 , _timeout( timeout )
122 , _timeoutReached( false )
123 , _fileSizeExceeded ( false )
124 , _expectedFileSize( expectedFileSize_r )
125 , report( _report )
126 {}
127
128 void ProgressData::updateStats( curl_off_t dltotal, curl_off_t dlnow )
129 {
130 time_t now = _timeNow = time(0);
131
132 // If called without args (0.0), recompute based on the last values seen
133 if ( dltotal && dltotal != _dnlTotal )
134 _dnlTotal = dltotal;
135
136 if ( dlnow && dlnow != _dnlNow )
137 {
138 _timeRcv = now;
139 _dnlNow = dlnow;
140 }
141
142 // init or reset if time jumps back
143 if ( !_timeStart || _timeStart > now )
144 _timeStart = _timeLast = _timeRcv = now;
145
146 // timeout condition
147 if ( _timeout )
148 _timeoutReached = ( (now - _timeRcv) > _timeout );
149
150 // percentage:
151 if ( _dnlTotal )
152 _dnlPercent = int( _dnlNow * 100 / _dnlTotal );
153
154 // download rates:
155 _drateTotal = double(_dnlNow) / std::max( int(now - _timeStart), 1 );
156
157 if ( _timeLast < now )
158 {
159 _drateLast = double(_dnlNow - _dnlLast) / int(now - _timeLast);
160 // start new period
161 _timeLast = now;
163 }
164 else if ( _timeStart == _timeLast )
166 }
167
169 {
170 if ( _fileSizeExceeded )
171 return 1;
172 if ( _timeoutReached )
173 return 1; // no-data timeout
174 if ( report && !(*report)->progress( _dnlPercent, _url, _drateTotal, _drateLast ) )
175 return 1; // user requested abort
176 return 0;
177 }
178
179 size_t ProgressData::writeBytes(char *ptr, ByteCount bytes)
180 {
181 // check if the downloaded data is already bigger than what we expected
183 if ( _fileSizeExceeded )
184 return 0;
185
186 auto written = fwrite( ptr, 1, bytes, _file );
187 _bytesWritten += written;
188 return written;
189 }
190
195 {
196 public:
198 const std::string & err_r,
199 const std::string & msg_r )
200 : media::MediaCurlException( url_r, err_r, msg_r )
201 {}
202 //~MediaCurlExceptionMayRetryInternaly() noexcept {}
203 };
204
205}
206
207
208using namespace internal;
209using namespace zypp::base;
210
211namespace zypp {
212
213 namespace media {
214
215Pathname MediaCurl::_cookieFile = "/var/lib/YaST2/cookies";
216
217// we use this define to unbloat code as this C setting option
218// and catching exception is done frequently.
220#define SET_OPTION(opt,val) do { \
221 ret = curl_easy_setopt ( curl, opt, val ); \
222 if ( ret != 0) { \
223 ZYPP_THROW(MediaCurlSetOptException(_origin.at(rData.mirror).url(), _curlError)); \
224 } \
225 } while ( false )
226
227#define SET_OPTION_OFFT(opt,val) SET_OPTION(opt,(curl_off_t)val)
228#define SET_OPTION_LONG(opt,val) SET_OPTION(opt,(long)val)
229#define SET_OPTION_VOID(opt,val) SET_OPTION(opt,(void*)val)
230
232 const Pathname & attach_point_hint_r )
233 : MediaNetworkCommonHandler( origin_r, attach_point_hint_r,
234 "/", // urlpath at attachpoint
235 true ), // does_download
237{
238 _multi = curl_multi_init();
239
240 _curlError[0] = '\0';
241
242 MIL << "MediaCurl::MediaCurl(" << origin_r.authority().url() << ", " << attach_point_hint_r << ")" << endl;
243
245
246 if( !attachPoint().empty())
247 {
248 PathInfo ainfo(attachPoint());
249 Pathname apath(attachPoint() + "XXXXXX");
250 char *atemp = ::strdup( apath.asString().c_str());
251 char *atest = NULL;
252 if( !ainfo.isDir() || !ainfo.userMayRWX() ||
253 atemp == NULL || (atest=::mkdtemp(atemp)) == NULL)
254 {
255 WAR << "attach point " << ainfo.path()
256 << " is not useable for " << origin_r.authority().url().getScheme() << endl;
257 setAttachPoint("", true);
258 }
259 else if( atest != NULL)
260 ::rmdir(atest);
261
262 if( atemp != NULL)
263 ::free(atemp);
264 }
265}
266
268{
269 try { release(); } catch(...) {}
270 if (_multi)
271 curl_multi_cleanup(_multi);
272}
273
274void MediaCurl::setCookieFile( const Pathname &fileName )
275{
276 _cookieFile = fileName;
277}
278
279void MediaCurl::setCurlError(const char* error)
280{
281 // FIXME(dmllr): Use strlcpy if available for better performance
282 strncpy(_curlError, error, sizeof(_curlError)-1);
283 _curlError[sizeof(_curlError)-1] = '\0';
284}
285
287
289{
290 curl_version_info_data *curl_info = NULL;
291 curl_info = curl_version_info(CURLVERSION_NOW);
292 // curl_info does not need any free (is static)
293 if (curl_info->protocols)
294 {
295 const char * const *proto = nullptr;
296 std::string scheme( url.getScheme());
297 bool found = false;
298 for(proto=curl_info->protocols; !found && *proto; ++proto)
299 {
300 if( scheme == std::string((const char *)*proto))
301 found = true;
302 }
303 if( !found)
304 {
305 std::string msg("Unsupported protocol '");
306 msg += scheme;
307 msg += "'";
309 }
310 }
311}
312
314{
315 CURL *curl = rData.curl;
316
317 // kill old settings
318 curl_easy_reset ( curl );
319
321 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, log_redirects_curl);
322 curl_easy_setopt(curl, CURLOPT_HEADERDATA, &_lastRedirect);
323 CURLcode ret = curl_easy_setopt( curl, CURLOPT_ERRORBUFFER, _curlError );
324 if ( ret != 0 ) {
325 ZYPP_THROW(MediaCurlSetOptException( _origin.at(rData.mirror).url(), "Error setting error buffer"));
326 }
327
328 SET_OPTION(CURLOPT_FAILONERROR, 1L);
329 SET_OPTION(CURLOPT_NOSIGNAL, 1L);
330
333 {
334 case 4: SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); break;
335 case 6: SET_OPTION(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6); break;
336 }
337
341 SET_OPTION(CURLOPT_CONNECTTIMEOUT, settings.connectTimeout());
342 // If a transfer timeout is set, also set CURLOPT_TIMEOUT to an upper limit
343 // just in case curl does not trigger its progress callback frequently
344 // enough.
345 if ( settings.timeout() )
346 {
347 SET_OPTION(CURLOPT_TIMEOUT, 3600L);
348 }
349
350 // follow any Location: header that the server sends as part of
351 // an HTTP header (#113275)
352 SET_OPTION(CURLOPT_FOLLOWLOCATION, 1L);
353 // 3 redirects seem to be too few in some cases (bnc #465532)
354 SET_OPTION(CURLOPT_MAXREDIRS, 6L);
355
356 if ( _origin.at(rData.mirror).url().getScheme() == "https" )
357 {
358 if ( :: internal::setCurlRedirProtocols ( curl ) != CURLE_OK ) {
360 }
361
362 if( settings.verifyPeerEnabled() ||
363 settings.verifyHostEnabled() )
364 {
365 SET_OPTION(CURLOPT_CAPATH, settings.certificateAuthoritiesPath().c_str());
366 }
367
368 if( ! settings.clientCertificatePath().empty() )
369 {
370 SET_OPTION(CURLOPT_SSLCERT, settings.clientCertificatePath().c_str());
371 }
372 if( ! settings.clientKeyPath().empty() )
373 {
374 SET_OPTION(CURLOPT_SSLKEY, settings.clientKeyPath().c_str());
375 }
376
377#ifdef CURLSSLOPT_ALLOW_BEAST
378 // see bnc#779177
379 ret = curl_easy_setopt( curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
380 if ( ret != 0 ) {
383 }
384#endif
385 SET_OPTION(CURLOPT_SSL_VERIFYPEER, settings.verifyPeerEnabled() ? 1L : 0L);
386 SET_OPTION(CURLOPT_SSL_VERIFYHOST, settings.verifyHostEnabled() ? 2L : 0L);
387 // bnc#903405 - POODLE: libzypp should only talk TLS
388 SET_OPTION(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
389 }
390
391 SET_OPTION(CURLOPT_USERAGENT, settings.userAgentString().c_str() );
392
393 /* Fixes bsc#1174011 "auth=basic ignored in some cases"
394 * We should proactively add the password to the request if basic auth is configured
395 * and a password is available in the credentials but not in the URL.
396 *
397 * We will be a bit paranoid here and require that the URL has a user embedded, otherwise we go the default route
398 * and ask the server first about the auth method
399 */
400 if ( settings.authType() == "basic" && not settings.username().empty() && settings.password().empty() ) {
402 const auto cred = cm.getCred( _origin.at(rData.mirror).url() );
403 if ( cred && cred->valid() ) {
404 settings.setPassword(cred->password());
405 }
406 }
407
408 /*---------------------------------------------------------------*
409 CURLOPT_USERPWD: [user name]:[password]
410
411 Url::username/password -> CURLOPT_USERPWD
412 If not provided, anonymous FTP identification
413 *---------------------------------------------------------------*/
414
415 if ( settings.hasCredentials() )
416 {
417 settings.logCredentials( DBG << "Credentials: " ) << endl;
418 SET_OPTION(CURLOPT_USERNAME, settings.username().c_str());
419 SET_OPTION(CURLOPT_PASSWORD, settings.password().c_str());
420 std::string use_auth = settings.authType();
421 if (use_auth.empty())
422 use_auth = "digest,basic"; // our default
423 long auth = CurlAuthData::auth_type_str2long(use_auth);
424 if( auth != CURLAUTH_NONE)
425 {
426 DBG << "Enabling HTTP authentication methods: " << use_auth
427 << " (CURLOPT_HTTPAUTH=" << auth << ")" << std::endl;
428 SET_OPTION(CURLOPT_HTTPAUTH, auth);
429 }
430 }
431
432 if ( settings.proxyEnabled() && ! settings.proxy().empty() )
433 {
434 DBG << "Proxy: '" << settings.proxy() << "'" << endl;
435 SET_OPTION(CURLOPT_PROXY, settings.proxy().c_str());
436 SET_OPTION(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
437 /*---------------------------------------------------------------*
438 * CURLOPT_PROXYUSERPWD: [user name]:[password]
439 *
440 * Url::option(proxyuser and proxypassword) -> CURLOPT_PROXYUSERPWD
441 * If not provided, $HOME/.curlrc is evaluated
442 *---------------------------------------------------------------*/
443
444 std::string proxyuserpwd = settings.proxyUserPassword();
445
446 if ( proxyuserpwd.empty() )
447 {
448 CurlConfig curlconf;
449 CurlConfig::parseConfig(curlconf); // parse ~/.curlrc
450 if ( curlconf.proxyuserpwd.empty() )
451 DBG << "Proxy: ~/.curlrc does not contain the proxy-user option" << endl;
452 else
453 {
454 proxyuserpwd = curlconf.proxyuserpwd;
455 DBG << "Proxy: using proxy-user from ~/.curlrc" << endl;
456 }
457 }
458 else
459 {
460 DBG << "Proxy: using provided proxy-user '" << settings.proxyUsername() << "'" << endl;
461 }
462
463 if ( ! proxyuserpwd.empty() )
464 {
465 SET_OPTION(CURLOPT_PROXYUSERPWD, curlUnEscape( proxyuserpwd ).c_str());
466 }
467 }
468#if CURLVERSION_AT_LEAST(7,19,4)
469 else if ( settings.proxy() == EXPLICITLY_NO_PROXY )
470 {
471 // Explicitly disabled in URL (see fillSettingsFromUrl()).
472 // This should also prevent libcurl from looking into the environment.
473 DBG << "Proxy: explicitly NOPROXY" << endl;
474 SET_OPTION(CURLOPT_NOPROXY, "*");
475 }
476#endif
477 else
478 {
479 DBG << "Proxy: not explicitly set, libcurl may look into the environment" << endl;
480 }
481
483 if ( settings.minDownloadSpeed() != 0 )
484 {
485 SET_OPTION(CURLOPT_LOW_SPEED_LIMIT, settings.minDownloadSpeed());
486 // default to 10 seconds at low speed
487 SET_OPTION(CURLOPT_LOW_SPEED_TIME, 60L);
488 }
489
490#if CURLVERSION_AT_LEAST(7,15,5)
491 if ( settings.maxDownloadSpeed() != 0 )
492 SET_OPTION_OFFT(CURLOPT_MAX_RECV_SPEED_LARGE, settings.maxDownloadSpeed());
493#endif
494
495 /*---------------------------------------------------------------*
496 *---------------------------------------------------------------*/
497
498 _currentCookieFile = _cookieFile.asString();
499 if ( ::geteuid() == 0 || PathInfo(_currentCookieFile).owner() == ::geteuid() )
501
502 const auto &cookieFileParam = _origin.at(rData.mirror).url().getQueryParam( "cookies" );
503 if ( !cookieFileParam.empty() && str::strToBool( cookieFileParam, true ) )
504 SET_OPTION(CURLOPT_COOKIEFILE, _currentCookieFile.c_str() );
505 else
506 MIL << "No cookies requested" << endl;
507 SET_OPTION(CURLOPT_COOKIEJAR, _currentCookieFile.c_str() );
508 SET_OPTION(CURLOPT_XFERINFOFUNCTION, &progressCallback );
509 SET_OPTION(CURLOPT_NOPROGRESS, 0L);
510
511#if CURLVERSION_AT_LEAST(7,18,0)
512 // bnc #306272
513 SET_OPTION(CURLOPT_PROXY_TRANSFER_MODE, 1L );
514#endif
515 // Append settings custom headers to curl.
516 // TransferSettings assert strings are trimmed (HTTP/2 RFC 9113)
517 if ( _customHeaders ) {
518 curl_slist_free_all(_customHeaders);
519 _customHeaders = 0L;
520 }
521 for ( const auto &header : settings.headers() ) {
522 _customHeaders = curl_slist_append(_customHeaders, header.c_str());
523 if ( !_customHeaders )
525 }
526 SET_OPTION(CURLOPT_HTTPHEADER, _customHeaders);
527}
528
530
532{
533 if ( _customHeaders ) {
534 curl_slist_free_all(_customHeaders);
535 _customHeaders = 0L;
536 }
537
538 // clear effective settings
540}
541
543
544void MediaCurl::releaseFrom( const std::string & ejectDev )
545{
546 disconnect();
547}
548
550
551void MediaCurl::getFileCopy( const OnMediaLocation & srcFile , const Pathname & target ) const
552{
553 // we need a non const pointer to work around the current API
554 auto that = const_cast<MediaCurl *>(this);
555 std::exception_ptr lastErr;
556 const auto &mirrOrder = mirrorOrder (srcFile);
557 for ( unsigned mirr : mirrOrder ) {
558 try {
559 return that->getFileCopyFromMirror ( mirr, srcFile, target );
560
561 } catch (MediaException & excpt_r) {
562 if ( !canTryNextMirror ( excpt_r ) )
563 ZYPP_RETHROW(excpt_r);
564 lastErr = ZYPP_FWD_CURRENT_EXCPT();
565 }
566 }
567 if ( lastErr ) {
568 ZYPP_RETHROW( lastErr );
569 }
570
571 // should not happen
572 ZYPP_THROW( MediaException("No usable mirror available.") );
573
574}
575
576void MediaCurl::getFileCopyFromMirror(const int mirror, const OnMediaLocation &srcFile, const Pathname &target)
577{
578 const auto &filename = srcFile.filename();
579
580 // Optional files will send no report until data are actually received (we know it exists).
581 OptionalDownloadProgressReport reportfilter( srcFile.optional() );
583
584 auto &endpoint = _origin[mirror];
585 auto &settings = endpoint.getConfig<TransferSettings>(MIRR_SETTINGS_KEY.data());
586
587 AutoDispose<CURL*> curl( curl_easy_init(), []( CURL *hdl ) { if ( hdl ) { curl_easy_cleanup(hdl); } } );
588
589 RequestData rData;
590 rData.mirror = mirror;
591 rData.curl = curl.value ();
592
593 if( !endpoint.url().isValid() )
594 ZYPP_THROW(MediaBadUrlException(endpoint.url()));
595
596 if( endpoint.url().getHost().empty() )
598
599 Url fileurl( getFileUrl(mirror, filename) );
600
601 bool firstAuth = true; // bsc#1210870: authenticate must not return stored credentials more than once.
602 unsigned internalTry = 0;
603 static constexpr unsigned maxInternalTry = 3;
604
605 do
606 {
607 try
608 {
609 Pathname dest = target.absolutename();
610 if( assert_dir( dest.dirname() ) )
611 {
612 DBG << "assert_dir " << dest.dirname() << " failed" << endl;
613 ZYPP_THROW( MediaSystemException(fileurl, "System error on " + dest.dirname().asString()) );
614 }
615
616 ManagedFile destNew { target.extend( ".new.zypp.XXXXXX" ) };
617 AutoFILE file;
618 {
619 AutoFREE<char> buf { ::strdup( (*destNew).c_str() ) };
620 if( ! buf )
621 {
622 ERR << "out of memory for temp file name" << endl;
623 ZYPP_THROW(MediaSystemException(fileurl, "out of memory for temp file name"));
624 }
625
626 AutoFD tmp_fd { ::mkostemp( buf, O_CLOEXEC ) };
627 if( tmp_fd == -1 )
628 {
629 ERR << "mkstemp failed for file '" << destNew << "'" << endl;
631 }
632 destNew = ManagedFile( (*buf), filesystem::unlink );
633
634 file = ::fdopen( tmp_fd, "we" );
635 if ( ! file )
636 {
637 ERR << "fopen failed for file '" << destNew << "'" << endl;
639 }
640 tmp_fd.resetDispose(); // don't close it here! ::fdopen moved ownership to file
641 }
642
643 DBG << "dest: " << dest << endl;
644 DBG << "temp: " << destNew << endl;
645
646 setupEasy( rData, settings );
647
648 // set IFMODSINCE time condition (no download if not modified)
649 if( PathInfo(target).isExist() )
650 {
651 curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
652 curl_easy_setopt(curl, CURLOPT_TIMEVALUE, (long)PathInfo(target).mtime());
653 }
654 else
655 {
656 curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
657 curl_easy_setopt(curl, CURLOPT_TIMEVALUE, 0L);
658 }
659
661 curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
662 curl_easy_setopt(curl, CURLOPT_TIMEVALUE, 0L);
663 };
664
665 DBG << srcFile.filename().asString() << endl;
666
667 DBG << "URL: " << fileurl.asString() << endl;
668 // Use URL without options and without username and passwd
669 // (some proxies dislike them in the URL).
670 // Curl seems to need the just scheme, hostname and a path;
671 // the rest was already passed as curl options (in attachTo).
672 Url curlUrl( clearQueryString(fileurl) );
673
674 //
675 // See also Bug #154197 and ftp url definition in RFC 1738:
676 // The url "ftp://user@host/foo/bar/file" contains a path,
677 // that is relative to the user's home.
678 // The url "ftp://user@host//foo/bar/file" (or also with
679 // encoded slash as %2f) "ftp://user@host/%2ffoo/bar/file"
680 // contains an absolute path.
681 //
682 _lastRedirect.clear();
683 std::string urlBuffer( curlUrl.asString());
684 CURLcode ret = curl_easy_setopt( curl, CURLOPT_URL,
685 urlBuffer.c_str() );
686 if ( ret != 0 ) {
688 }
689
690 // Set callback and perform.
691 internal::ProgressData progressData( file, curl, settings.timeout(), fileurl, srcFile.downloadSize(), &report );
692
693 ret = curl_easy_setopt( curl, CURLOPT_WRITEDATA, &progressData );
694 if ( ret != 0 ) {
696 }
697
698 ret = curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, &MediaCurl::writeCallback );
699 if ( ret != 0 ) {
701 }
702
703 report->start(fileurl, dest);
704
705 if ( curl_easy_setopt( curl, CURLOPT_PROGRESSDATA, &progressData ) != 0 ) {
706 WAR << "Can't set CURLOPT_PROGRESSDATA: " << _curlError << endl;;
707 }
708
709 ret = executeCurl( rData );
710
711 // flush buffers
712 fflush ( file );
713
714 #if CURLVERSION_AT_LEAST(7,19,4)
715 // bnc#692260: If the client sends a request with an If-Modified-Since header
716 // with a future date for the server, the server may respond 200 sending a
717 // zero size file.
718 // curl-7.19.4 introduces CURLINFO_CONDITION_UNMET to check this condition.
719 if ( ftell(file) == 0 && ret == 0 )
720 {
721 long httpReturnCode = 33;
722 if ( curl_easy_getinfo( curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) == CURLE_OK && httpReturnCode == 200 )
723 {
724 long conditionUnmet = 33;
725 if ( curl_easy_getinfo( curl, CURLINFO_CONDITION_UNMET, &conditionUnmet ) == CURLE_OK && conditionUnmet )
726 {
727 WAR << "TIMECONDITION unmet - retry without." << endl;
728 curl_easy_setopt( curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
729 curl_easy_setopt( curl, CURLOPT_TIMEVALUE, 0L);
730 ret = executeCurl( rData );
731 }
732 }
733 }
734 #endif
735
736 if ( curl_easy_setopt( curl, CURLOPT_PROGRESSDATA, NULL ) != 0 ) {
737 WAR << "Can't unset CURLOPT_PROGRESSDATA: " << _curlError << endl;;
738 }
739
740 if ( ret != 0 ) {
741 ERR << "curl error: " << ret << ": " << _curlError
742 << ", temp file size " << ftell(file)
743 << " bytes." << endl;
744
745 // the timeout is determined by the progress data object
746 // which holds whether the timeout was reached or not,
747 // otherwise it would be a user cancel
748
749 if ( progressData.fileSizeExceeded() )
751
752 evaluateCurlCode( rData, srcFile.filename(), ret, progressData.timeoutReached() );
753 }
754
755 long httpReturnCode = 0;
756 CURLcode infoRet = curl_easy_getinfo(curl,
757 CURLINFO_RESPONSE_CODE,
758 &httpReturnCode);
759 bool modified = true;
760 if (infoRet == CURLE_OK)
761 {
762 DBG << "HTTP response: " + str::numstring(httpReturnCode);
763 if ( httpReturnCode == 304
764 || ( httpReturnCode == 213 && (endpoint.url().getScheme() == "ftp" || endpoint.url().getScheme() == "tftp") ) ) // not modified
765 {
766 DBG << " Not modified.";
767 modified = false;
768 }
769 DBG << endl;
770 }
771 else
772 {
773 WAR << "Could not get the response code." << endl;
774 }
775
776 if (modified || infoRet != CURLE_OK)
777 {
778 // apply umask
779 if ( ::fchmod( ::fileno(file), filesystem::applyUmaskTo( 0644 ) ) )
780 {
781 ERR << "Failed to chmod file " << destNew << endl;
782 }
783
784 file.resetDispose(); // we're going to close it manually here
785 if ( ::fclose( file ) )
786 {
787 ERR << "Fclose failed for file '" << destNew << "'" << endl;
789 }
790
791 // move the temp file into dest
792 if ( rename( destNew, dest ) != 0 ) {
793 ERR << "Rename failed" << endl;
795 }
796 destNew.resetDispose(); // no more need to unlink it
797 }
798
799 DBG << "done: " << PathInfo(dest) << endl;
800 break; // success!
801 }
802 catch (MediaUnauthorizedException & ex_r)
803 {
804 if ( authenticate( endpoint.url(), settings, ex_r.hint(), firstAuth) ) {
805 firstAuth = false; // must not return stored credentials again
806 continue; // retry
807 }
808
810 ZYPP_RETHROW(ex_r);
811 }
812 // unexpected exception
813 catch (MediaException & excpt_r)
814 {
815 if ( typeid(excpt_r) == typeid( MediaCurlExceptionMayRetryInternaly ) ) {
816 ++internalTry;
817 if ( internalTry < maxInternalTry ) {
818 // just report (NO_ERROR); no interactive request to the user
819 report->problem(fileurl, media::DownloadProgressReport::NO_ERROR, excpt_r.asUserHistory()+_("Will try again..."));
820 continue; // retry
821 }
822 excpt_r.addHistory( str::Format(_("Giving up after %1% attempts.")) % maxInternalTry );
823 }
824
826 if( typeid(excpt_r) == typeid( media::MediaFileNotFoundException ) ||
827 typeid(excpt_r) == typeid( media::MediaNotAFileException ) )
828 {
830 }
831 report->finish(fileurl, reason, excpt_r.asUserHistory());
832 ZYPP_RETHROW(excpt_r);
833 }
834 } while ( true );
835
836 report->finish(fileurl, zypp::media::DownloadProgressReport::NO_ERROR, "");
837}
838
840
841bool MediaCurl::getDoesFileExist( const Pathname & filename ) const
842{
843 // we need a non const pointer to work around the current API
844 auto that = const_cast<MediaCurl *>(this);
845
846 std::exception_ptr lastErr;
847 for ( int i : mirrorOrder( OnMediaLocation(filename).setMirrorsAllowed(false) )) {
848 try {
849 return that->doGetDoesFileExist( i, filename );
850
851 } catch (MediaException & excpt_r) {
852 if ( !canTryNextMirror ( excpt_r ) )
853 ZYPP_RETHROW(excpt_r);
854 lastErr = ZYPP_FWD_CURRENT_EXCPT();
855 }
856 }
857 if ( lastErr ) {
858 try {
859 ZYPP_RETHROW( lastErr );
860 } catch ( const MediaFileNotFoundException &e ) {
861 // on file not found we return false
862 ZYPP_CAUGHT(e);
863 return false;
864 }
865 }
866 return false;
867}
868
870
872 const Pathname &filename,
873 CURLcode code,
874 bool timeout_reached) const
875{
876 if ( code != 0 )
877 {
878 const auto &baseMirr = _origin[rData.mirror];
879 Url url;
880 if (filename.empty())
881 url = baseMirr.url();
882 else
883 url = getFileUrl(rData.mirror, filename);
884
885 std::string err;
886 {
887 switch ( code )
888 {
889 case CURLE_UNSUPPORTED_PROTOCOL:
890 err = " Unsupported protocol";
891 if ( !_lastRedirect.empty() )
892 {
893 err += " or redirect (";
894 err += _lastRedirect;
895 err += ")";
896 }
897 break;
898 case CURLE_URL_MALFORMAT:
899 case CURLE_URL_MALFORMAT_USER:
900 err = " Bad URL";
901 break;
902 case CURLE_LOGIN_DENIED:
904 MediaUnauthorizedException(url, "Login failed.", _curlError, ""));
905 break;
906 case CURLE_HTTP_RETURNED_ERROR:
907 {
908 long httpReturnCode = 0;
909 CURLcode infoRet = curl_easy_getinfo( rData.curl,
910 CURLINFO_RESPONSE_CODE,
911 &httpReturnCode );
912 if ( infoRet == CURLE_OK )
913 {
914 std::string msg = "HTTP response: " + str::numstring( httpReturnCode );
915 switch ( httpReturnCode )
916 {
917 case 401:
918 {
919 std::string auth_hint = getAuthHint( rData.curl );
920
921 DBG << msg << " Login failed (URL: " << url.asString() << ")" << std::endl;
922 DBG << "MediaUnauthorizedException auth hint: '" << auth_hint << "'" << std::endl;
923
925 url, "Login failed.", _curlError, auth_hint
926 ));
927 }
928
929 case 502: // bad gateway (bnc #1070851)
930 case 503: // service temporarily unavailable (bnc #462545)
932 case 504: // gateway timeout
934 case 403:
935 {
936 std::string msg403;
937 if ( url.getHost().find(".suse.com") != std::string::npos )
938 msg403 = _("Visit the SUSE Customer Center to check whether your registration is valid and has not expired.");
939 else if (url.asString().find("novell.com") != std::string::npos)
940 msg403 = _("Visit the Novell Customer Center to check whether your registration is valid and has not expired.");
942 }
943 case 404:
944 case 410:
945 ZYPP_THROW(MediaFileNotFoundException(baseMirr.url(), filename));
946 }
947
948 DBG << msg << " (URL: " << url.asString() << ")" << std::endl;
950 }
951 else
952 {
953 std::string msg = "Unable to retrieve HTTP response:";
954 DBG << msg << " (URL: " << url.asString() << ")" << std::endl;
956 }
957 }
958 break;
959 case CURLE_FTP_COULDNT_RETR_FILE:
960#if CURLVERSION_AT_LEAST(7,16,0)
961 case CURLE_REMOTE_FILE_NOT_FOUND:
962#endif
963 case CURLE_FTP_ACCESS_DENIED:
964 case CURLE_TFTP_NOTFOUND:
965 err = "File not found";
966 ZYPP_THROW(MediaFileNotFoundException(baseMirr.url(), filename));
967 break;
968 case CURLE_BAD_PASSWORD_ENTERED:
969 case CURLE_FTP_USER_PASSWORD_INCORRECT:
970 err = "Login failed";
971 break;
972 case CURLE_COULDNT_RESOLVE_PROXY:
973 case CURLE_COULDNT_RESOLVE_HOST:
974 case CURLE_COULDNT_CONNECT:
975 case CURLE_FTP_CANT_GET_HOST:
976 err = "Connection failed";
977 break;
978 case CURLE_WRITE_ERROR:
979 err = "Write error";
980 break;
981 case CURLE_PARTIAL_FILE:
982 case CURLE_OPERATION_TIMEDOUT:
983 timeout_reached = true; // fall though to TimeoutException
984 // fall though...
985 case CURLE_ABORTED_BY_CALLBACK:
986 if( timeout_reached )
987 {
988 err = "Timeout reached";
990 }
991 else
992 {
993 err = "User abort";
994 }
995 break;
996
997 default:
998 err = "Curl error " + str::numstring( code );
999 break;
1000 }
1001
1002 // uhm, no 0 code but unknown curl exception
1004 }
1005 }
1006 else
1007 {
1008 // actually the code is 0, nothing happened
1009 }
1010}
1011
1013
1014bool MediaCurl::doGetDoesFileExist( const int mirror, const Pathname & filename )
1015{
1016 DBG << filename.asString() << endl;
1017
1018 AutoDispose<CURL*> curl( curl_easy_init(), []( CURL *hdl ) { if ( hdl ) { curl_easy_cleanup(hdl); } } );
1019 RequestData rData;
1020 rData.mirror = mirror;
1021 rData.curl = curl.value ();
1022
1023 auto &endpoint = _origin[mirror];
1024
1025 if( !endpoint.url().isValid() )
1026 ZYPP_THROW(MediaBadUrlException(endpoint.url()));
1027
1028 if( endpoint.url().getHost().empty() )
1030
1031 Url url(getFileUrl(mirror, filename));
1032
1033 DBG << "URL: " << url.asString() << endl;
1034 // Use URL without options and without username and passwd
1035 // (some proxies dislike them in the URL).
1036 // Curl seems to need the just scheme, hostname and a path;
1037 // the rest was already passed as curl options (in attachTo).
1038 Url curlUrl( clearQueryString(url) );
1039
1040 // See also Bug #154197 and ftp url definition in RFC 1738:
1041 // The url "ftp://user@host/foo/bar/file" contains a path,
1042 // that is relative to the user's home.
1043 // The url "ftp://user@host//foo/bar/file" (or also with
1044 // encoded slash as %2f) "ftp://user@host/%2ffoo/bar/file"
1045 // contains an absolute path.
1046 //
1047 _lastRedirect.clear();
1048 std::string urlBuffer( curlUrl.asString());
1049
1050 CURLcode ok;
1051 bool canRetry = true;
1052 bool firstAuth = true;
1053 auto &settings = endpoint.getConfig<TransferSettings>( MIRR_SETTINGS_KEY.data() );
1054
1055 while ( canRetry ) {
1056 canRetry = false;
1057 setupEasy( rData, settings );
1058
1059 CURLcode ret = curl_easy_setopt( curl, CURLOPT_URL,
1060 urlBuffer.c_str() );
1061 if ( ret != 0 ) {
1063 }
1064
1065 AutoFILE file { ::fopen( "/dev/null", "w" ) };
1066 if ( !file ) {
1067 ERR << "fopen failed for /dev/null" << endl;
1068 ZYPP_THROW(MediaWriteException("/dev/null"));
1069 }
1070
1071 ret = curl_easy_setopt( curl, CURLOPT_WRITEDATA, (*file) );
1072 if ( ret != 0 ) {
1074 }
1075
1076 // If no head requests allowed (?head_requests=no):
1077 // Instead of returning no data with NOBODY, we return
1078 // little data, that works with broken servers, and
1079 // works for ftp as well, because retrieving only headers
1080 // ftp will return always OK code ?
1081 // See http://curl.haxx.se/docs/knownbugs.html #58
1082 const bool doHeadRequest = (endpoint.url().getScheme() == "http" || endpoint.url().getScheme() == "https") && settings.headRequestsAllowed();
1083 if ( doHeadRequest ) {
1084 curl_easy_setopt( curl, CURLOPT_NOBODY, 1L );
1085 } else {
1086 curl_easy_setopt( curl, CURLOPT_RANGE, "0-1" );
1087 }
1088
1089 try {
1090 ok = const_cast<MediaCurl *>(this)->executeCurl( rData );
1091 MIL << "perform code: " << ok << " [ " << curl_easy_strerror(ok) << " ]" << endl;
1092
1093 // as we are not having user interaction, the user can't cancel
1094 // the file existence checking, a callback or timeout return code
1095 // will be always a timeout.
1096 evaluateCurlCode( rData, filename, ok, true /* timeout */);
1097 }
1098 catch ( const MediaFileNotFoundException &e ) {
1099 // if the file did not exist then we can return false
1100 return false;
1101 }
1102 catch ( const MediaUnauthorizedException &e ) {
1103 if ( authenticate( endpoint.url(), settings, e.hint(), firstAuth ) ) {
1104 firstAuth = false;
1105 canRetry = true;
1106 continue;
1107 }
1108 }
1109
1110 // exists
1111 return ( ok == CURLE_OK );
1112 }
1113
1114 return false;
1115}
1116
1118//
1119int MediaCurl::aliveCallback( void *clientp, curl_off_t /*dltotal*/, curl_off_t dlnow, curl_off_t /*ultotal*/, curl_off_t /*ulnow*/ )
1120{
1121 internal::ProgressData *pdata = reinterpret_cast<internal::ProgressData *>( clientp );
1122 if( pdata )
1123 {
1124 // Do not propagate dltotal in alive callbacks. MultiCurl uses this to
1125 // prevent a percentage raise while downloading a metalink file. Download
1126 // activity however is indicated by propagating the download rate (via dlnow).
1127 pdata->updateStats( 0.0, dlnow );
1128 return pdata->reportProgress();
1129 }
1130 return 0;
1131}
1132
1133int MediaCurl::progressCallback( void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow )
1134{
1135 internal::ProgressData *pdata = reinterpret_cast<internal::ProgressData *>( clientp );
1136 if( pdata )
1137 {
1138 // work around curl bug that gives us old data
1139 long httpReturnCode = 0;
1140 if ( curl_easy_getinfo( pdata->curl(), CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0 ) {
1141 return aliveCallback( clientp, dltotal, dlnow, ultotal, ulnow );
1142 }
1143 pdata->updateStats( dltotal, dlnow );
1144 return pdata->reportProgress();
1145 }
1146 return 0;
1147}
1148
1149size_t MediaCurl::writeCallback( char *ptr, size_t size, size_t nmemb, void *userdata )
1150{
1151 internal::ProgressData *pdata = reinterpret_cast<internal::ProgressData *>( userdata );
1152 if( pdata ) {
1153 return pdata->writeBytes ( ptr, size * nmemb );
1154 }
1155 return 0;
1156}
1157
1159
1160std::string MediaCurl::getAuthHint( CURL *curl ) const
1161{
1162 long auth_info = CURLAUTH_NONE;
1163
1164 CURLcode infoRet =
1165 curl_easy_getinfo(curl, CURLINFO_HTTPAUTH_AVAIL, &auth_info);
1166
1167 if(infoRet == CURLE_OK)
1168 {
1169 return CurlAuthData::auth_type_long2str(auth_info);
1170 }
1171
1172 return "";
1173}
1174
1179void MediaCurl::resetExpectedFileSize(void *clientp, const ByteCount &expectedFileSize)
1180{
1181 internal::ProgressData *data = reinterpret_cast<internal::ProgressData *>(clientp);
1182 if ( data ) {
1183 data->expectedFileSize( expectedFileSize );
1184 }
1185}
1186
1193{
1194 CURL *curl = rData.curl;
1195 const auto &baseUrl = _origin.at(rData.mirror);
1196
1197 if (!_multi)
1198 ZYPP_THROW(MediaCurlInitException(baseUrl.url()));
1199
1200 internal::CurlPollHelper _curlHelper(*this);
1201
1202 // add the easy handle to the multi instance
1203 if ( curl_multi_add_handle( _multi, curl ) != CURLM_OK )
1204 ZYPP_THROW(MediaCurlException( baseUrl.url(), "curl_multi_add_handle", "unknown error"));
1205
1206 // make sure the handle is cleanly removed from the multi handle
1207 OnScopeExit autoRemove([&](){ curl_multi_remove_handle( _multi, curl ); });
1208
1209 // kickstart curl, this will cause libcurl to go over the added handles and register sockets and timeouts
1210 CURLMcode mcode = _curlHelper.handleTimout();
1211 if (mcode != CURLM_OK)
1212 ZYPP_THROW(MediaCurlException( baseUrl.url(), "curl_multi_socket_action", "unknown error"));
1213
1214 bool canContinue = true;
1215 while ( canContinue ) {
1216
1217 CURLMsg *msg = nullptr;
1218 int nqueue = 0;
1219 while ((msg = curl_multi_info_read( _multi, &nqueue)) != 0) {
1220 if ( msg->msg != CURLMSG_DONE ) continue;
1221 if ( msg->easy_handle != curl ) continue;
1222
1223 return msg->data.result;
1224 }
1225
1226 // copy watched sockets in case curl changes the vector as we go over the events later
1227 std::vector<GPollFD> requestedFds = _curlHelper.socks;
1228
1229 int r = zypp_detail::zypp_poll( requestedFds, _curlHelper.timeout_ms.value_or( -1 ) );
1230 if ( r == -1 )
1231 ZYPP_THROW( MediaCurlException(baseUrl.url(), "zypp_poll() failed", "unknown error") );
1232
1233 // run curl
1234 if ( r == 0 ) {
1235 CURLMcode mcode = _curlHelper.handleTimout();
1236 if (mcode != CURLM_OK)
1237 ZYPP_THROW(MediaCurlException(baseUrl.url(), "curl_multi_socket_action", "unknown error"));
1238 } else {
1239 CURLMcode mcode = _curlHelper.handleSocketActions( requestedFds );
1240 if (mcode != CURLM_OK)
1241 ZYPP_THROW(MediaCurlException(baseUrl.url(), "curl_multi_socket_action", "unknown error"));
1242 }
1243 }
1244 return CURLE_OK;
1245}
1246
1247
1248 } // namespace media
1249} // namespace zypp
1250//
#define zypp_defer
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
Definition Exception.h:479
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition Exception.h:475
#define ZYPP_FWD_CURRENT_EXCPT()
Drops a logline and returns the current Exception as a std::exception_ptr.
Definition Exception.h:471
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
#define _(MSG)
Definition Gettext.h:39
#define DBG
Definition Logger.h:99
#define MIL
Definition Logger.h:100
#define ERR
Definition Logger.h:102
#define WAR
Definition Logger.h:101
#define SET_OPTION_OFFT(opt, val)
Definition MediaCurl.cc:227
#define SET_OPTION(opt, val)
Definition MediaCurl.cc:220
Attempt to work around certain issues by autoretry in MediaCurl::getFileCopy E.g.
Definition MediaCurl.cc:195
MediaCurlExceptionMayRetryInternaly(const Url &url_r, const std::string &err_r, const std::string &msg_r)
Definition MediaCurl.cc:197
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition AutoDispose.h:95
reference value() const
Reference to the Tp object.
void resetDispose()
Set no dispose function.
Store and operate with byte count.
Definition ByteCount.h:32
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition Exception.cc:140
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition Exception.cc:189
Manages a data source characterized by an authoritative URL and a list of mirror URLs.
const OriginEndpoint & authority() const
Describes a resource file located on a medium.
bool optional() const
Whether this is an optional resource.
const ByteCount & downloadSize() const
The size of the resource on the server.
const Pathname & filename() const
The path to the resource on the medium.
const zypp::Url & url() const
ProgressData()
Ctor no range [0,0](0).
Url manipulation class.
Definition Url.h:93
std::string getScheme() const
Returns the scheme name of the URL.
Definition Url.cc:560
std::string asString() const
Returns a default string representation of the Url object.
Definition Url.cc:524
static ZConfig & instance()
Singleton ctor.
Definition ZConfig.cc:971
Wrapper class for stat/lstat.
Definition PathInfo.h:226
const Pathname & path() const
Return current Pathname.
Definition PathInfo.h:251
Pathname dirname() const
Return all but the last component od this path.
Definition Pathname.h:133
const char * c_str() const
String representation.
Definition Pathname.h:113
const std::string & asString() const
String representation.
Definition Pathname.h:94
bool empty() const
Test for an empty path.
Definition Pathname.h:117
AuthData_Ptr getCred(const Url &url)
Get credentials for the specified url.
static std::string auth_type_long2str(long auth_type)
Converts a long of ORed CURLAUTH_* identifiers into a string of comma separated list of authenticatio...
static long auth_type_str2long(std::string &auth_type_str)
Converts a string of comma separated list of authetication type names into a long of ORed CURLAUTH_* ...
MediaCurlException(const Url &url_r, std::string err_r, std::string msg_r)
bool doGetDoesFileExist(const int mirror, const Pathname &filename)
static size_t writeCallback(char *ptr, size_t size, size_t nmemb, void *userdata)
Callback writing the data into our file.
void checkProtocol(const Url &url) const override
check the url is supported by the curl library
Definition MediaCurl.cc:288
static void setCookieFile(const Pathname &)
Definition MediaCurl.cc:274
bool getDoesFileExist(const Pathname &filename) const override
Repeatedly calls doGetDoesFileExist() until it successfully returns, fails unexpectedly,...
Definition MediaCurl.cc:841
static void resetExpectedFileSize(void *clientp, const ByteCount &expectedFileSize)
MediaMultiCurl needs to reset the expected filesize in case a metalink file is downloaded otherwise t...
std::string _currentCookieFile
Definition MediaCurl.h:126
static Pathname _cookieFile
Definition MediaCurl.h:127
std::string getAuthHint(CURL *curl) const
Return a comma separated list of available authentication methods supported by server.
void getFileCopyFromMirror(const int mirror, const OnMediaLocation &srcFile, const Pathname &target)
Definition MediaCurl.cc:576
static int progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
Callback reporting download progress.
std::string _lastRedirect
to log/report redirections
Definition MediaCurl.h:130
char _curlError[CURL_ERROR_SIZE]
Definition MediaCurl.h:128
void evaluateCurlCode(RequestData &rData, const zypp::Pathname &fileName, CURLcode code, bool timeout) const
Evaluates a curl return code and throws the right MediaException filename Filename being downloaded c...
Definition MediaCurl.cc:871
CURLcode executeCurl(RequestData &rData)
MediaCurl(const MirroredOrigin &origin_r, const Pathname &attach_point_hint_r)
Definition MediaCurl.cc:231
void setCurlError(const char *error)
Definition MediaCurl.cc:279
void releaseFrom(const std::string &ejectDev) override
Call concrete handler to release the media.
Definition MediaCurl.cc:544
static int aliveCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
Callback sending just an alive trigger to the UI, without stats (e.g.
void disconnectFrom() override
Definition MediaCurl.cc:531
void setupEasy(RequestData &rData, TransferSettings &settings)
initializes the curl easy handle with the data from the url
Definition MediaCurl.cc:313
curl_slist * _customHeaders
Definition MediaCurl.h:131
void getFileCopy(const OnMediaLocation &srcFile, const Pathname &target) const override
Definition MediaCurl.cc:551
Just inherits Exception to separate media exceptions.
MirroredOrigin _origin
Contains the authority URL and mirrors.
Url url() const
Primary Url used.
void disconnect()
Use concrete handler to isconnect media.
void release(const std::string &ejectDev="")
Use concrete handler to release the media.
void setAttachPoint(const Pathname &path, bool temp)
Set a new attach point.
Pathname attachPoint() const
Return the currently used attach point.
bool authenticate(const Url &url, TransferSettings &settings, const std::string &availAuthTypes, bool firstTry)
static bool canTryNextMirror(const Excpt &excpt_r)
std::vector< unsigned > mirrorOrder(const OnMediaLocation &loc) const
Url getFileUrl(int mirrorIdx, const Pathname &filename) const
concatenate the attach url and the filename to a complete download url
MediaNetworkCommonHandler(const MirroredOrigin &origin_r, const Pathname &attach_point_r, const Pathname &urlpath_below_attachpoint_r, const bool does_download_r)
static constexpr std::string_view MIRR_SETTINGS_KEY
const std::string & hint() const
comma separated list of available authentication types
Holds transfer setting.
const std::string & password() const
auth password
long maxDownloadSpeed() const
Maximum download speed (bytes per second).
long connectTimeout() const
connection timeout
const std::string & authType() const
get the allowed authentication types
long timeout() const
transfer timeout
const Pathname & clientCertificatePath() const
SSL client certificate file.
long minDownloadSpeed() const
Minimum download speed (bytes per second) until the connection is dropped.
const Headers & headers() const
returns a list of all added headers (trimmed)
const std::string & proxy() const
proxy host
const Pathname & clientKeyPath() const
SSL client key file.
std::string proxyUserPassword() const
returns the proxy user and password as a user:pass string
bool verifyHostEnabled() const
Whether to verify host for ssl.
const std::string & userAgentString() const
user agent string (trimmed)
bool hasCredentials() const
has a username, maybe even the password
void setPassword(const std::string &val_r)
sets the auth password
bool proxyEnabled() const
proxy is enabled
const std::string & username() const
auth username
const std::string & proxyUsername() const
proxy auth username
const Pathname & certificateAuthoritiesPath() const
SSL certificate authorities path ( default: /etc/ssl/certs ).
std::ostream & logCredentials(std::ostream &str) const
log credentials to stream hiding the password.
bool verifyPeerEnabled() const
Whether to verify peer for ssl.
#define EXPLICITLY_NO_PROXY
size_t log_redirects_curl(char *ptr, size_t size, size_t nmemb, void *userdata)
void globalInitCurlOnce()
Definition curlhelper.cc:64
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.
CURLcode setCurlRedirProtocols(CURL *curl)
Definition ansi.h:855
int ZYPP_MEDIA_CURL_IPRESOLVE()
4/6 to force IPv4/v6
Definition curlhelper.cc:45
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
Definition PathInfo.h:813
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
Definition PathInfo.cc:1224
int unlink(const Pathname &path)
Like 'unlink'.
Definition PathInfo.cc:719
std::string numstring(char n, int w=0)
Definition String.h:290
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Definition String.h:500
int zypp_poll(std::vector< GPollFD > &fds, int timeout)
Small wrapper around g_poll that additionally listens to the shutdown FD returned by ZYpp::shutdownSi...
Definition ZYppImpl.cc:323
Easy-to use interface to the ZYPP dependency resolver.
AutoDispose< const Pathname > ManagedFile
A Pathname plus associated cleanup code to be executed when path is no longer needed.
Definition ManagedFile.h:27
AutoDispose< void > OnScopeExit
CURLMcode handleSocketActions(const std::vector< GPollFD > &actionsFds, int first=0)
std::vector< GPollFD > socks
std::optional< long > timeout_ms
Bottleneck filtering all DownloadProgressReport issued from Media[Muli]Curl.
ByteCount bytesWritten() const
Definition MediaCurl.cc:84
ByteCount _expectedFileSize
Definition MediaCurl.cc:95
curl_off_t _dnlNow
Bytes downloaded now.
Definition MediaCurl.cc:105
int _dnlPercent
Percent completed or 0 if _dnlTotal is unknown.
Definition MediaCurl.cc:109
time_t _timeRcv
Start of no-data timeout.
Definition MediaCurl.cc:100
ByteCount expectedFileSize() const
Definition MediaCurl.cc:70
time_t _timeLast
Start last period(~1sec).
Definition MediaCurl.cc:99
int reportProgress() const
Definition MediaCurl.cc:168
double _drateLast
Download rate in last period.
Definition MediaCurl.cc:112
bool timeoutReached() const
Definition MediaCurl.cc:64
void expectedFileSize(ByteCount newval_r)
Definition MediaCurl.cc:73
ByteCount _bytesWritten
Bytes actually written into the file.
Definition MediaCurl.cc:107
zypp::Url url() const
Definition MediaCurl.cc:76
curl_off_t _dnlLast
Bytes downloaded at period start.
Definition MediaCurl.cc:104
bool fileSizeExceeded() const
Definition MediaCurl.cc:67
void updateStats(curl_off_t dltotal=0.0, curl_off_t dlnow=0.0)
Definition MediaCurl.cc:128
double _drateTotal
Download rate so far.
Definition MediaCurl.cc:111
zypp::callback::SendReport< zypp::media::DownloadProgressReport > * report
Definition MediaCurl.cc:96
size_t writeBytes(char *ptr, ByteCount bytes)
Definition MediaCurl.cc:179
curl_off_t _dnlTotal
Bytes to download or 0 if unknown.
Definition MediaCurl.cc:103
ProgressData(AutoFILE file, CURL *curl, time_t timeout=0, zypp::Url url=zypp::Url(), zypp::ByteCount expectedFileSize_r=0, zypp::callback::SendReport< zypp::media::DownloadProgressReport > *_report=nullptr)
Definition MediaCurl.cc:117
time_t _timeStart
Start total stats.
Definition MediaCurl.cc:98
AutoDispose<int> calling close
AutoDispose<FILE*> calling fclose
Structure holding values of curlrc options.
Definition curlconfig.h:27
std::string proxyuserpwd
Definition curlconfig.h:49
static int parseConfig(CurlConfig &config, const std::string &filename="")
Parse a curlrc file and store the result in the config structure.
Definition curlconfig.cc:24
Convenient building of std::string with boost::format.
Definition String.h:254