libzypp: zypp-curl/ng/network/request.cc Source File

libzypp 17.38.15

request.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8----------------------------------------------------------------------*/
9#include <zypp-curl/ng/network/private/request_p.h>
10#include <zypp-curl/ng/network/private/networkrequesterror_p.h>
11#include <zypp-curl/ng/network/networkrequestdispatcher.h>
12#include <zypp-curl/ng/network/private/mediadebug_p.h>
13#include <zypp-core/ng/base/EventDispatcher>
14#include <zypp-core/ng/base/private/linuxhelpers_p.h>
15#include <zypp-core/ng/core/String>
16#include <zypp-core/fs/PathInfo.h>
17#include <zypp-curl/private/curlhelper_p.h>
18#include <zypp-curl/CurlConfig>
19#include <zypp-curl/auth/CurlAuthData>
20#include <zypp-media/MediaConfig>
21#include <zypp-core/base/String.h>
22#include <zypp-core/base/StringV.h>
23#include <zypp-core/base/IOTools.h>
24#include <zypp-core/Pathname.h>
25#include <curl/curl.h>
26#include <stdio.h>
27#include <fcntl.h>
28#include <utility>
29
30#include <iostream>
31#include <boost/variant.hpp>
32#include <boost/variant/polymorphic_get.hpp>
33
34
35namespace zyppng {
36
37 namespace {
38 static size_t nwr_headerCallback ( char *ptr, size_t size, size_t nmemb, void *userdata ) {
39 if ( !userdata )
40 return 0;
41
42 NetworkRequestPrivate *that = reinterpret_cast<NetworkRequestPrivate *>( userdata );
43 return that->headerfunction( ptr, size * nmemb );
44 }
45 static size_t nwr_writeCallback ( char *ptr, size_t size, size_t nmemb, void *userdata ) {
46 if ( !userdata )
47 return 0;
48
49 NetworkRequestPrivate *that = reinterpret_cast<NetworkRequestPrivate *>( userdata );
50 return that->writefunction( ptr, {}, size * nmemb );
51 }
52
53 //helper for std::visit
54 template<class T> struct always_false : std::false_type {};
55 }
56
57 NetworkRequestPrivate::prepareNextRangeBatch_t::prepareNextRangeBatch_t(running_t &&prevState)
58 : _outFile( std::move(prevState._outFile) )
59 , _downloaded( prevState._downloaded )
60 , _partialHelper( std::move(prevState._partialHelper) )
61 { }
62
63 NetworkRequestPrivate::running_t::running_t( pending_t &&prevState )
64 : _partialHelper( std::move(prevState._partialHelper) )
65 { }
66
67 NetworkRequestPrivate::running_t::running_t( prepareNextRangeBatch_t &&prevState )
68 : _outFile( std::move(prevState._outFile) )
69 , _partialHelper( std::move(prevState._partialHelper) )
70 , _downloaded( prevState._downloaded )
71 { }
72
73 NetworkRequestPrivate::NetworkRequestPrivate(Url &&url, zypp::Pathname &&targetFile, NetworkRequest::FileMode fMode , NetworkRequest &p)
74 : BasePrivate(p)
75 , _url ( std::move(url) )
76 , _targetFile ( std::move( targetFile) )
77 , _fMode ( std::move(fMode) )
78 , _headers( std::unique_ptr< curl_slist, decltype (&curl_slist_free_all) >( nullptr, &curl_slist_free_all ) )
79 { }
80
81 NetworkRequestPrivate::~NetworkRequestPrivate()
82 {
83 if ( _easyHandle ) {
84 //clean up for now, later we might reuse handles
85 curl_easy_cleanup( _easyHandle );
86 MIL << _easyHandle << " curl_easy_cleanup" << std::endl;
87 //reset in request but make sure the request was not enqueued again and got a new handle
88 _easyHandle = nullptr;
89 }
90 }
91
92 bool NetworkRequestPrivate::initialize( std::string &errBuf )
93 {
94 reset();
95
96 if ( _easyHandle ) {
97 //will reset to defaults but keep live connections, session ID and DNS caches
98 curl_easy_reset( _easyHandle );
99 MIL << _easyHandle << " curl_easy_reset" << std::endl;
100 } else
101 _easyHandle = curl_easy_init();
102 return setupHandle ( errBuf );
103 }
104
105 bool NetworkRequestPrivate::setupHandle( std::string &errBuf )
106 {
107 ::internal::setupZYPP_MEDIA_CURL_DEBUG( _easyHandle );
108 curl_easy_setopt( _easyHandle, CURLOPT_ERRORBUFFER, this->_errorBuf.data() );
109 MIL << _easyHandle << " " << "URL: " << _url << std::endl;
110
111 const std::string urlScheme = _url.getScheme();
112 if ( urlScheme == "http" || urlScheme == "https" )
113 _protocolMode = ProtocolMode::HTTP;
114
115 try {
116
117 setCurlOption( CURLOPT_PRIVATE, this );
118 setCurlOption( CURLOPT_XFERINFOFUNCTION, NetworkRequestPrivate::curlProgressCallback );
119 setCurlOption( CURLOPT_XFERINFODATA, this );
120 setCurlOption( CURLOPT_NOPROGRESS, 0L);
121 setCurlOption( CURLOPT_FAILONERROR, 1L);
122 setCurlOption( CURLOPT_NOSIGNAL, 1L);
123
124 std::string urlBuffer( _url.asString() );
125 setCurlOption( CURLOPT_URL, urlBuffer.c_str() );
126
127 setCurlOption( CURLOPT_WRITEFUNCTION, nwr_writeCallback );
128 setCurlOption( CURLOPT_WRITEDATA, this );
129
130 if ( _options & NetworkRequest::ConnectionTest ) {
131 setCurlOption( CURLOPT_CONNECT_ONLY, 1L );
132 setCurlOption( CURLOPT_FRESH_CONNECT, 1L );
133 }
134 if ( _options & NetworkRequest::HeadRequest ) {
135 // instead of returning no data with NOBODY, we return
136 // little data, that works with broken servers, and
137 // works for ftp as well, because retrieving only headers
138 // ftp will return always OK code ?
139 // See http://curl.haxx.se/docs/knownbugs.html #58
140 if ( _protocolMode == ProtocolMode::HTTP && _settings.headRequestsAllowed() )
141 setCurlOption( CURLOPT_NOBODY, 1L );
142 else
143 setCurlOption( CURLOPT_RANGE, "0-1" );
144 }
145
146 //make a local copy of the settings, so headers are not added multiple times
147 TransferSettings locSet = _settings;
148
149 if ( _dispatcher ) {
150 locSet.setUserAgentString( _dispatcher->agentString().c_str() );
151
152 // add custom headers as configured (bsc#955801)
153 const auto &cHeaders = _dispatcher->hostSpecificHeaders();
154 if ( auto i = cHeaders.find(_url.getHost()); i != cHeaders.end() ) {
155 for ( const auto &[key, value] : i->second ) {
156 locSet.addHeader( HttpHeader( key, value ).asString() );
157 }
158 }
159 }
160
161 locSet.addHeader("Pragma:");
162
164 switch ( zypp::env::ZYPP_MEDIA_CURL_IPRESOLVE() )
165 {
166 case 4: setCurlOption( CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); break;
167 case 6: setCurlOption( CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6 ); break;
168 default: break;
169 }
170
171 setCurlOption( CURLOPT_HEADERFUNCTION, &nwr_headerCallback );
172 setCurlOption( CURLOPT_HEADERDATA, this );
173
177 setCurlOption( CURLOPT_CONNECTTIMEOUT, locSet.connectTimeout() );
178 // If a transfer timeout is set, also set CURLOPT_TIMEOUT to an upper limit
179 // just in case curl does not trigger its progress callback frequently
180 // enough.
181 if ( locSet.timeout() )
182 {
183 setCurlOption( CURLOPT_TIMEOUT, 3600L );
184 }
185
186 if ( urlScheme == "https" )
187 {
188 if ( :: internal::setCurlRedirProtocols ( _easyHandle ) != CURLE_OK ) {
189 ZYPP_THROW( zypp::media::MediaCurlSetOptException( _url, _errorBuf.data() ) );
190 }
191
192 if( locSet.verifyPeerEnabled() ||
193 locSet.verifyHostEnabled() )
194 {
195 setCurlOption(CURLOPT_CAPATH, locSet.certificateAuthoritiesPath().c_str());
196 }
197
198 if( ! locSet.clientCertificatePath().empty() )
199 {
200 setCurlOption(CURLOPT_SSLCERT, locSet.clientCertificatePath().c_str());
201 }
202 if( ! locSet.clientKeyPath().empty() )
203 {
204 setCurlOption(CURLOPT_SSLKEY, locSet.clientKeyPath().c_str());
205 }
206
207#ifdef CURLSSLOPT_ALLOW_BEAST
208 // see bnc#779177
209 setCurlOption( CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
210#endif
211 setCurlOption(CURLOPT_SSL_VERIFYPEER, locSet.verifyPeerEnabled() ? 1L : 0L);
212 setCurlOption(CURLOPT_SSL_VERIFYHOST, locSet.verifyHostEnabled() ? 2L : 0L);
213 // bnc#903405 - POODLE: libzypp should only talk TLS
214 setCurlOption(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
215 }
216
217 // follow any Location: header that the server sends as part of
218 // an HTTP header (#113275)
219 setCurlOption( CURLOPT_FOLLOWLOCATION, 1L);
220 // 3 redirects seem to be too few in some cases (bnc #465532)
221 setCurlOption( CURLOPT_MAXREDIRS, 6L );
222
223 //set the user agent
224 setCurlOption(CURLOPT_USERAGENT, locSet.userAgentString().c_str() );
225
226
227 /*---------------------------------------------------------------*
228 CURLOPT_USERPWD: [user name]:[password]
229 Url::username/password -> CURLOPT_USERPWD
230 If not provided, anonymous FTP identification
231 *---------------------------------------------------------------*/
232 if ( locSet.hasCredentials() )
233 {
234 locSet.logCredentials( DBG << "Credentials: " ) << std::endl;
235 setCurlOption(CURLOPT_USERNAME, locSet.username().c_str());
236 setCurlOption(CURLOPT_PASSWORD, locSet.password().c_str());
237 std::string use_auth = _settings.authType();
238 if (use_auth.empty())
239 use_auth = "digest,basic"; // our default
240 long auth = zypp::media::CurlAuthData::auth_type_str2long(use_auth);
241 if( auth != CURLAUTH_NONE)
242 {
243 DBG << _easyHandle << " " << "Enabling HTTP authentication methods: " << use_auth
244 << " (CURLOPT_HTTPAUTH=" << auth << ")" << std::endl;
245 setCurlOption(CURLOPT_HTTPAUTH, auth);
246 }
247 }
248
249 if ( locSet.proxyEnabled() && ! locSet.proxy().empty() )
250 {
251 DBG << _easyHandle << " " << "Proxy: '" << locSet.proxy() << "'" << std::endl;
252 setCurlOption(CURLOPT_PROXY, locSet.proxy().c_str());
253 setCurlOption(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
254
255 /*---------------------------------------------------------------*
256 * CURLOPT_PROXYUSERPWD: [user name]:[password]
257 *
258 * Url::option(proxyuser and proxypassword) -> CURLOPT_PROXYUSERPWD
259 * If not provided, $HOME/.curlrc is evaluated
260 *---------------------------------------------------------------*/
261
262 std::string proxyuserpwd = locSet.proxyUserPassword();
263
264 if ( proxyuserpwd.empty() )
265 {
266 zypp::media::CurlConfig curlconf;
267 zypp::media::CurlConfig::parseConfig(curlconf); // parse ~/.curlrc
268 if ( curlconf.proxyuserpwd.empty() )
269 DBG << _easyHandle << " " << "Proxy: ~/.curlrc does not contain the proxy-user option" << std::endl;
270 else
271 {
272 proxyuserpwd = curlconf.proxyuserpwd;
273 DBG << _easyHandle << " " << "Proxy: using proxy-user from ~/.curlrc" << std::endl;
274 }
275 }
276 else
277 {
278 DBG << _easyHandle << " " << "Proxy: using provided proxy-user '" << _settings.proxyUsername() << "'" << std::endl;
279 }
280
281 if ( ! proxyuserpwd.empty() )
282 {
283 setCurlOption(CURLOPT_PROXYUSERPWD, ::internal::curlUnEscape( proxyuserpwd ).c_str());
284 }
285 }
286#if CURLVERSION_AT_LEAST(7,19,4)
287 else if ( locSet.proxy() == EXPLICITLY_NO_PROXY )
288 {
289 // Explicitly disabled in URL (see fillSettingsFromUrl()).
290 // This should also prevent libcurl from looking into the environment.
291 DBG << _easyHandle << " " << "Proxy: explicitly NOPROXY" << std::endl;
292 setCurlOption(CURLOPT_NOPROXY, "*");
293 }
294
295#endif
296 // else: Proxy: not explicitly set; libcurl may look into the environment
297
299 if ( locSet.minDownloadSpeed() != 0 )
300 {
301 setCurlOption(CURLOPT_LOW_SPEED_LIMIT, locSet.minDownloadSpeed());
302 // default to 10 seconds at low speed
303 setCurlOption(CURLOPT_LOW_SPEED_TIME, 60L);
304 }
305
306#if CURLVERSION_AT_LEAST(7,15,5)
307 if ( locSet.maxDownloadSpeed() != 0 )
308 setCurlOption(CURLOPT_MAX_RECV_SPEED_LARGE, locSet.maxDownloadSpeed());
309#endif
310
311 if ( locSet.cookieFileEnabled() ) {
312 DBG << _easyHandle << " " << "Cookie file enabled: " << _currentCookieFile << std::endl;
313 zypp::filesystem::assert_file_mode( _currentCookieFile, 0600 );
314 setCurlOption( CURLOPT_COOKIEFILE, _currentCookieFile.c_str() );
315 setCurlOption(CURLOPT_COOKIEJAR, _currentCookieFile.c_str() );
316 }
317
318#if CURLVERSION_AT_LEAST(7,18,0)
319 // bnc #306272
320 setCurlOption(CURLOPT_PROXY_TRANSFER_MODE, 1L );
321#endif
322
323 // Append settings custom headers to curl.
324 // TransferSettings assert strings are trimmed (HTTP/2 RFC 9113)
325 for ( const auto &header : locSet.headers() ) {
326 if ( !z_func()->addRequestHeader( header.c_str() ) )
327 ZYPP_THROW(zypp::media::MediaCurlInitException(_url));
328 }
329
330 if ( _headers )
331 setCurlOption( CURLOPT_HTTPHEADER, _headers.get() );
332
333 // set up ranges if required
334 if( !( _options & NetworkRequest::ConnectionTest ) && !( _options & NetworkRequest::HeadRequest ) ){
335 if ( _requestedRanges.size() ) {
336
337 std::sort( _requestedRanges.begin(), _requestedRanges.end(), []( const auto &elem1, const auto &elem2 ){
338 return ( elem1._start < elem2._start );
339 });
340
341 CurlMultiPartHandler *helper = nullptr;
342 if ( auto initState = std::get_if<pending_t>(&_runningMode) ) {
343
344 auto multiPartMode = _protocolMode == ProtocolMode::HTTP ? CurlMultiPartHandler::ProtocolMode::HTTP : CurlMultiPartHandler::ProtocolMode::Basic;
345 initState->_partialHelper = std::make_unique<CurlMultiPartHandler>(
346 multiPartMode
347 , _easyHandle
348 , _requestedRanges
349 , *this
350 );
351 helper = initState->_partialHelper.get();
352
353 } else if ( auto pendingState = std::get_if<prepareNextRangeBatch_t>(&_runningMode) ) {
354 helper = pendingState->_partialHelper.get();
355 } else {
356 errBuf = "Request is in invalid state to call setupHandle";
357 return false;
358 }
359
360 if ( !helper->prepare () ) {
361 errBuf = helper->lastErrorMessage ();
362 return false;
363 }
364 }
365 }
366
367 return true;
368
369 } catch ( const zypp::Exception &excp ) {
370 ZYPP_CAUGHT(excp);
371 errBuf = excp.asString();
372 }
373 return false;
374 }
375
376 bool NetworkRequestPrivate::assertOutputFile()
377 {
378 auto rmode = std::get_if<NetworkRequestPrivate::running_t>( &_runningMode );
379 if ( !rmode ) {
380 DBG << _easyHandle << "Can only create output file in running mode" << std::endl;
381 return false;
382 }
383 // if we have no open file create or open it
384 if ( !rmode->_outFile ) {
385 std::string openMode = "w+b";
386 if ( _fMode == NetworkRequest::WriteShared )
387 openMode = "r+b";
388
389 rmode->_outFile = fopen( _targetFile.asString().c_str() , openMode.c_str() );
390
391 //if the file does not exist create a new one
392 if ( !rmode->_outFile && _fMode == NetworkRequest::WriteShared ) {
393 rmode->_outFile = fopen( _targetFile.asString().c_str() , "w+b" );
394 }
395
396 if ( !rmode->_outFile ) {
397 rmode->_cachedResult = NetworkRequestErrorPrivate::customError( NetworkRequestError::InternalError
398 ,zypp::str::Format("Unable to open target file (%1%). Errno: (%2%:%3%)") % _targetFile.asString() % errno % strerr_cxx() );
399 return false;
400 }
401 }
402
403 return true;
404 }
405
406 bool NetworkRequestPrivate::canRecover() const
407 {
408 // We can recover from RangeFail errors if the helper indicates it
409 auto rmode = std::get_if<NetworkRequestPrivate::running_t>( &_runningMode );
410 if ( rmode->_partialHelper ) return rmode->_partialHelper->canRecover();
411 return false;
412 }
413
414 bool NetworkRequestPrivate::prepareToContinue( std::string &errBuf )
415 {
416 auto rmode = std::get_if<NetworkRequestPrivate::running_t>( &_runningMode );
417 if ( !rmode ) {
418 errBuf = "NetworkRequestPrivate::prepareToContinue called in invalid state";
419 return false;
420 }
421
422 if ( rmode->_partialHelper && rmode->_partialHelper->hasMoreWork() ) {
423
424 bool hadRangeFail = rmode->_partialHelper->lastError () == NetworkRequestError::RangeFail;
425
426 _runningMode = prepareNextRangeBatch_t( std::move(std::get<running_t>( _runningMode )) );
427
428 auto &prepMode = std::get<prepareNextRangeBatch_t>(_runningMode);
429 if ( !prepMode._partialHelper->prepareToContinue() ) {
430 errBuf = prepMode._partialHelper->lastErrorMessage();
431 return false;
432 }
433
434 if ( hadRangeFail ) {
435 // we reset the handle to default values. We do this to not run into
436 // "transfer closed with outstanding read data remaining" error CURL sometimes returns when
437 // we cancel a connection because of a range error to request a smaller batch.
438 // The error will still happen but much less frequently than without resetting the handle.
439 //
440 // Note: Even creating a new handle will NOT fix the issue
441 curl_easy_reset( _easyHandle );
442 MIL << _easyHandle << " curl_easy_reset after hadRangeFail" << std::endl;
443 }
444 if ( !setupHandle(errBuf))
445 return false;
446
447 return true;
448 }
449 errBuf = "Request has no more work";
450 return false;
451
452 }
453
454 bool NetworkRequestPrivate::hasMoreWork() const
455 {
456 // check if we have ranges that have never been requested
457 return std::any_of( _requestedRanges.begin(), _requestedRanges.end(), []( const auto &range ){ return range._rangeState == CurlMultiPartHandler::Pending; });
458 }
459
460 void NetworkRequestPrivate::aboutToStart( )
461 {
462 bool isRangeContinuation = std::holds_alternative<prepareNextRangeBatch_t>( _runningMode );
463 if ( isRangeContinuation ) {
464 MIL << _easyHandle << " " << "Continuing a previously started range batch." << std::endl;
465 _runningMode = running_t( std::move(std::get<prepareNextRangeBatch_t>( _runningMode )) );
466 } else {
467 _runningMode = running_t( std::move(std::get<pending_t>( _runningMode )) );
468 }
469
470 auto &m = std::get<running_t>( _runningMode );
471
472 if ( m._activityTimer ) {
473 DBG_MEDIA << _easyHandle << " Setting activity timeout to: " << _settings.timeout() << std::endl;
474 m._activityTimer->connect( &Timer::sigExpired, *this, &NetworkRequestPrivate::onActivityTimeout );
475 m._activityTimer->start( static_cast<uint64_t>( _settings.timeout() * 1000 ) );
476 }
477
478 if ( !isRangeContinuation )
479 _sigStarted.emit( *z_func() );
480 }
481
482 void NetworkRequestPrivate::dequeueNotify()
483 {
484 if ( std::holds_alternative<running_t>(_runningMode) ) {
485 auto &rmode = std::get<running_t>( _runningMode );
486 if ( rmode._partialHelper )
487 rmode._partialHelper->finalize();
488 }
489 }
490
491 void NetworkRequestPrivate::setResult( NetworkRequestError &&err )
492 {
493 finished_t resState;
494 resState._result = std::move(err);
495
496 if ( std::holds_alternative<running_t>(_runningMode) ) {
497
498 auto &rmode = std::get<running_t>( _runningMode );
499 resState._downloaded = rmode._downloaded;
500 resState._contentLenght = rmode._contentLenght;
501
502 if ( resState._result.type() == NetworkRequestError::NoError && !(_options & NetworkRequest::HeadRequest) && !(_options & NetworkRequest::ConnectionTest) ) {
503 if ( _requestedRanges.size( ) ) {
504 //we have a successful download lets see if we got everything we needed
505 if ( !rmode._partialHelper->verifyData() ){
506 NetworkRequestError::Type err = rmode._partialHelper->lastError();
507 resState._result = NetworkRequestErrorPrivate::customError( err, std::string(rmode._partialHelper->lastErrorMessage()) );
508 }
509
510 // if we have ranges we need to fill our digest from the full file
511 if ( _fileVerification && resState._result.type() == NetworkRequestError::NoError ) {
512 if ( fseek( rmode._outFile, 0, SEEK_SET ) != 0 ) {
513 resState._result = NetworkRequestErrorPrivate::customError( NetworkRequestError::InternalError, "Unable to set output file pointer." );
514 } else {
515 constexpr size_t bufSize = 4096;
516 char buf[bufSize];
517 while( auto cnt = fread(buf, 1, bufSize, rmode._outFile ) > 0 ) {
518 _fileVerification->_fileDigest.update(buf, cnt);
519 }
520 }
521 }
522 } // if ( _requestedRanges.size( ) )
523 }
524
525 // finally check the file digest if we have one
526 if ( _fileVerification && resState._result.type() == NetworkRequestError::NoError ) {
527 const UByteArray &calcSum = _fileVerification->_fileDigest.digestVector ();
528 const UByteArray &expSum = zypp::Digest::hexStringToUByteArray( _fileVerification->_fileChecksum.checksum () );
529 if ( calcSum != expSum ) {
530 resState._result = NetworkRequestErrorPrivate::customError(
531 NetworkRequestError::InvalidChecksum
532 , (zypp::str::Format("Invalid file checksum %1%, expected checksum %2%")
533 % _fileVerification->_fileDigest.digest()
534 % _fileVerification->_fileChecksum.checksum () ) );
535 }
536 }
537
538 rmode._outFile.reset();
539 }
540
541 _runningMode = std::move( resState );
542 _sigFinished.emit( *z_func(), std::get<finished_t>(_runningMode)._result );
543 }
544
545 void NetworkRequestPrivate::reset()
546 {
547 _protocolMode = ProtocolMode::Default;
548 _headers.reset( nullptr );
549 _errorBuf.fill( 0 );
550 _runningMode = pending_t();
551
552 if ( _fileVerification )
553 _fileVerification->_fileDigest.reset ();
554
555 std::for_each( _requestedRanges.begin (), _requestedRanges.end(), []( CurlMultiPartHandler::Range &range ) {
556 range.restart();
557 });
558 }
559
560 void NetworkRequestPrivate::onActivityTimeout( Timer &t )
561 {
562 MIL_MEDIA << _easyHandle << " Request timeout interval: " << t.interval()<< " remaining: " << t.remaining() << std::endl;
563 std::map<std::string, boost::any> extraInfo;
564 extraInfo.insert( {"requestUrl", _url } );
565 extraInfo.insert( {"filepath", _targetFile } );
566 _dispatcher->cancel( *z_func(), NetworkRequestErrorPrivate::customError( NetworkRequestError::Timeout, "Download timed out", std::move(extraInfo) ) );
567 }
568
569 std::string NetworkRequestPrivate::errorMessage() const
570 {
571 return std::string( _errorBuf.data() );
572 }
573
574 void NetworkRequestPrivate::resetActivityTimer()
575 {
576 if ( std::holds_alternative<running_t>( _runningMode ) ){
577 auto &rmode = std::get<running_t>( _runningMode );
578 if ( rmode._activityTimer && rmode._activityTimer->isRunning() )
579 rmode._activityTimer->start();
580 }
581 }
582
583 int NetworkRequestPrivate::curlProgressCallback( void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow )
584 {
585 if ( !clientp )
586 return CURLE_OK;
587 NetworkRequestPrivate *that = reinterpret_cast<NetworkRequestPrivate *>( clientp );
588
589 if ( !std::holds_alternative<running_t>(that->_runningMode) ){
590 DBG << that->_easyHandle << " " << "Curl progress callback was called in invalid state "<< that->z_func()->state() << std::endl;
591 return -1;
592 }
593 auto &rmode = std::get<running_t>( that->_runningMode );
594
595 //reset the timer
596 that->resetActivityTimer();
597
598 rmode._isInCallback = true;
599 if ( rmode._lastProgressNow != dlnow ) {
600 rmode._lastProgressNow = dlnow;
601 that->_sigProgress.emit( *that->z_func(), dltotal, dlnow, ultotal, ulnow );
602 }
603 rmode._isInCallback = false;
604
605 return rmode._cachedResult ? CURLE_ABORTED_BY_CALLBACK : CURLE_OK;
606 }
607
608 size_t NetworkRequestPrivate::headerfunction( char *ptr, size_t bytes )
609 {
610 //it is valid to call this function with no data to write, just return OK
611 if ( bytes == 0)
612 return 0;
613
614 resetActivityTimer();
615
616 if ( _protocolMode == ProtocolMode::HTTP ) {
617
618 std::string_view hdr( ptr, bytes );
619
620 hdr.remove_prefix( std::min( hdr.find_first_not_of(" \t\r\n"), hdr.size() ) );
621 const auto lastNonWhitespace = hdr.find_last_not_of(" \t\r\n");
622 if ( lastNonWhitespace != hdr.npos )
623 hdr.remove_suffix( hdr.size() - (lastNonWhitespace + 1) );
624 else
625 hdr = std::string_view();
626
627 if ( !std::holds_alternative<running_t>(_runningMode) ){
628 DBG << _easyHandle << " " << "Curl headerfunction callback was called in invalid state "<< z_func()->state() << std::endl;
629 return -1;
630 }
631 auto &rmode = std::get<running_t>( _runningMode );
632 if ( !hdr.size() ) {
633 return ( bytes );
634 }
635 if ( _expectedFileSize && rmode._partialHelper ) {
636 const auto &repSize = rmode._partialHelper->reportedFileSize ();
637 if ( repSize && repSize != _expectedFileSize ) {
638 rmode._cachedResult = NetworkRequestErrorPrivate::customError( NetworkRequestError::InternalError, "Reported downloaded data length is not what was expected." );
639 return 0;
640 }
641 }
642 if ( zypp::strv::hasPrefixCI( hdr, "HTTP/" ) ) {
643
644 long statuscode = 0;
645 (void)curl_easy_getinfo( _easyHandle, CURLINFO_RESPONSE_CODE, &statuscode);
646
647 // if we have a status 204 we need to create a empty file
648 if( statuscode == 204 && !( _options & NetworkRequest::ConnectionTest ) && !( _options & NetworkRequest::HeadRequest ) )
649 assertOutputFile();
650
651 } else if ( zypp::strv::hasPrefixCI( hdr, "Location:" ) ) {
652 _lastRedirect = hdr.substr( 9 );
653 DBG << _easyHandle << " " << "redirecting to " << _lastRedirect << std::endl;
654
655 } else if ( zypp::strv::hasPrefixCI( hdr, "Content-Length:") ) {
656 auto lenStr = str::trim( hdr.substr( 15 ), zypp::str::TRIM );
657 auto str = std::string ( lenStr.data(), lenStr.length() );
658 auto len = zypp::str::strtonum<typename zypp::ByteCount::SizeType>( str.data() );
659 if ( len > 0 ) {
660 DBG << _easyHandle << " " << "Got Content-Length Header: " << len << std::endl;
661 rmode._contentLenght = zypp::ByteCount(len, zypp::ByteCount::B);
662 }
663 }
664 }
665
666 return bytes;
667 }
668
669 size_t NetworkRequestPrivate::writefunction( char *data, std::optional<off_t> offset, size_t max )
670 {
671 //it is valid to call this function with no data to write, just return OK
672 if ( max == 0)
673 return 0;
674
675 resetActivityTimer();
676
677 //in case of a HEAD request, we do not write anything
678 if ( _options & NetworkRequest::HeadRequest ) {
679 return ( max );
680 }
681
682 if ( !std::holds_alternative<running_t>(_runningMode) ){
683 DBG << _easyHandle << " " << "Curl writefunction callback was called in invalid state "<< z_func()->state() << std::endl;
684 return -1;
685 }
686 auto &rmode = std::get<running_t>( _runningMode );
687
688 // if we have no open file create or open it
689 if ( !assertOutputFile() )
690 return 0;
691
692 if ( offset ) {
693 // seek to the given offset
694 if ( fseek( rmode._outFile, *offset, SEEK_SET ) != 0 ) {
695 rmode._cachedResult = NetworkRequestErrorPrivate::customError( NetworkRequestError::InternalError,
696 "Unable to set output file pointer." );
697 return 0;
698 }
699 rmode._currentFileOffset = *offset;
700 }
701
702 if ( _expectedFileSize && rmode._partialHelper ) {
703 const auto &repSize = rmode._partialHelper->reportedFileSize ();
704 if ( repSize && repSize != _expectedFileSize ) {
705 rmode._cachedResult = NetworkRequestErrorPrivate::customError( NetworkRequestError::InternalError, "Reported downloaded data length is not what was expected." );
706 return 0;
707 }
708 }
709
710 //make sure we do not write after the expected file size
711 if ( _expectedFileSize && static_cast<zypp::ByteCount::SizeType>( rmode._currentFileOffset + max) > _expectedFileSize ) {
712 rmode._cachedResult = NetworkRequestErrorPrivate::customError( NetworkRequestError::ExceededMaxLen, "Downloaded data exceeds expected length." );
713 return 0;
714 }
715
716 auto written = fwrite( data, 1, max, rmode._outFile );
717 if ( written == 0 )
718 return 0;
719
720 // if we are not downloading in ranges, we can update the file digest on the fly if we have one
721 if ( !rmode._partialHelper && _fileVerification ) {
722 _fileVerification->_fileDigest.update( data, written );
723 }
724
725 rmode._currentFileOffset += written;
726
727 // count the number of real bytes we have downloaded so far
728 rmode._downloaded += written;
729 _sigBytesDownloaded.emit( *z_func(), rmode._downloaded );
730
731 return written;
732 }
733
734 void NetworkRequestPrivate::notifyErrorCodeChanged()
735 {
736 auto rmode = std::get_if<NetworkRequestPrivate::running_t>( &_runningMode );
737 if ( !rmode || !rmode->_partialHelper || !rmode->_partialHelper->hasError() )
738 return;
739
740 // oldest cached result wins
741 if ( rmode->_cachedResult )
742 return;
743
744 auto lastErr = NetworkRequestErrorPrivate::customError( rmode->_partialHelper->lastError() , std::string(rmode->_partialHelper->lastErrorMessage()) );
745 MIL_MEDIA << _easyHandle << " Multipart handler announced error code " << lastErr.toString () << std::endl;
746 rmode->_cachedResult = lastErr;
747 }
748
749 ZYPP_IMPL_PRIVATE(NetworkRequest)
750
751 NetworkRequest::NetworkRequest(zyppng::Url url, zypp::filesystem::Pathname targetFile, zyppng::NetworkRequest::FileMode fMode)
752 : Base ( *new NetworkRequestPrivate( std::move(url), std::move(targetFile), std::move(fMode), *this ) )
753 {
754 }
755
756 NetworkRequest::~NetworkRequest()
757 {
758 Z_D();
759
760 if ( d->_dispatcher )
761 d->_dispatcher->cancel( *this, "Request destroyed while still running" );
762 }
763
764 void NetworkRequest::setExpectedFileSize( zypp::ByteCount expectedFileSize )
765 {
766 d_func()->_expectedFileSize = std::move( expectedFileSize );
767 }
768
769 zypp::ByteCount NetworkRequest::expectedFileSize() const
770 {
771 return d_func()->_expectedFileSize;
772 }
773
774 void NetworkRequest::setPriority( NetworkRequest::Priority prio, bool triggerReschedule )
775 {
776 Z_D();
777 d->_priority = prio;
778 if ( state() == Pending && triggerReschedule && d->_dispatcher )
779 d->_dispatcher->reschedule();
780 }
781
782 NetworkRequest::Priority NetworkRequest::priority() const
783 {
784 return d_func()->_priority;
785 }
786
787 void NetworkRequest::setOptions( Options opt )
788 {
789 d_func()->_options = opt;
790 }
791
792 NetworkRequest::Options NetworkRequest::options() const
793 {
794 return d_func()->_options;
795 }
796
797 void NetworkRequest::addRequestRange(size_t start, size_t len, std::optional<zypp::Digest> &&digest, CheckSumBytes expectedChkSum , std::any userData, std::optional<size_t> digestCompareLen, std::optional<size_t> chksumpad )
798 {
799 Z_D();
800 if ( state() == Running )
801 return;
802
803 d->_requestedRanges.push_back( Range::make( start, len, std::move(digest), std::move( expectedChkSum ), std::move( userData ), digestCompareLen, chksumpad ) );
804 }
805
806 void NetworkRequest::addRequestRange(zyppng::NetworkRequest::Range &&range )
807 {
808 Z_D();
809 if ( state() == Running )
810 return;
811
812 d->_requestedRanges.push_back( std::move(range) );
813 auto &rng = d->_requestedRanges.back();
814 rng._rangeState = CurlMultiPartHandler::Pending;
815 rng.bytesWritten = 0;
816 if ( rng._digest )
817 rng._digest->reset();
818 }
819
820 bool NetworkRequest::setExpectedFileChecksum( const zypp::CheckSum &expected )
821 {
822 Z_D();
823 if ( state() == Running )
824 return false;
825
826 zypp::Digest fDig;
827 if ( !fDig.create( expected.type () ) )
828 return false;
829
830 d->_fileVerification = NetworkRequestPrivate::FileVerifyInfo{
831 ._fileDigest = std::move(fDig),
832 ._fileChecksum = expected
833 };
834 return true;
835 }
836
837 void NetworkRequest::resetRequestRanges()
838 {
839 Z_D();
840 if ( state() == Running )
841 return;
842 d->_requestedRanges.clear();
843 }
844
845 std::vector<NetworkRequest::Range> NetworkRequest::failedRanges() const
846 {
847 const auto mystate = state();
848 if ( mystate != Finished && mystate != Error )
849 return {};
850
851 Z_D();
852
853 std::vector<Range> failed;
854 for ( auto &r : d->_requestedRanges ) {
855 if ( r._rangeState != CurlMultiPartHandler::Finished ) {
856 failed.push_back( r.clone() );
857 }
858 }
859 return failed;
860 }
861
862 const std::vector<NetworkRequest::Range> &NetworkRequest::requestedRanges() const
863 {
864 return d_func()->_requestedRanges;
865 }
866
867 const std::string &NetworkRequest::lastRedirectInfo() const
868 {
869 return d_func()->_lastRedirect;
870 }
871
872 void *NetworkRequest::nativeHandle() const
873 {
874 return d_func()->_easyHandle;
875 }
876
877 std::optional<zyppng::NetworkRequest::Timings> NetworkRequest::timings() const
878 {
879 const auto myerr = error();
880 const auto mystate = state();
881 if ( mystate != Finished )
882 return {};
883
884 Timings t;
885
886 auto getMeasurement = [ this ]( const CURLINFO info, std::chrono::microseconds &target ){
887 using FPSeconds = std::chrono::duration<double, std::chrono::seconds::period>;
888 double val = 0;
889 const auto res = curl_easy_getinfo( d_func()->_easyHandle, info, &val );
890 if ( CURLE_OK == res ) {
891 target = std::chrono::duration_cast<std::chrono::microseconds>( FPSeconds(val) );
892 }
893 };
894
895 getMeasurement( CURLINFO_NAMELOOKUP_TIME, t.namelookup );
896 getMeasurement( CURLINFO_CONNECT_TIME, t.connect);
897 getMeasurement( CURLINFO_APPCONNECT_TIME, t.appconnect);
898 getMeasurement( CURLINFO_PRETRANSFER_TIME , t.pretransfer);
899 getMeasurement( CURLINFO_TOTAL_TIME, t.total);
900 getMeasurement( CURLINFO_REDIRECT_TIME, t.redirect);
901
902 return t;
903 }
904
905 std::vector<char> NetworkRequest::peekData( off_t offset, size_t count ) const
906 {
907 Z_D();
908
909 if ( !std::holds_alternative<NetworkRequestPrivate::running_t>( d->_runningMode) )
910 return {};
911
912 const auto &rmode = std::get<NetworkRequestPrivate::running_t>( d->_runningMode );
913 return zypp::io::peek_data_fd( rmode._outFile, offset, count );
914 }
915
916 Url NetworkRequest::url() const
917 {
918 return d_func()->_url;
919 }
920
921 void NetworkRequest::setUrl(const Url &url)
922 {
923 Z_D();
924 if ( state() == NetworkRequest::Running )
925 return;
926
927 d->_url = url;
928 }
929
930 const zypp::filesystem::Pathname &NetworkRequest::targetFilePath() const
931 {
932 return d_func()->_targetFile;
933 }
934
935 void NetworkRequest::setTargetFilePath( const zypp::filesystem::Pathname &path )
936 {
937 Z_D();
938 if ( state() == NetworkRequest::Running )
939 return;
940 d->_targetFile = path;
941 }
942
943 NetworkRequest::FileMode NetworkRequest::fileOpenMode() const
944 {
945 return d_func()->_fMode;
946 }
947
948 void NetworkRequest::setFileOpenMode( FileMode mode )
949 {
950 Z_D();
951 if ( state() == NetworkRequest::Running )
952 return;
953 d->_fMode = std::move( mode );
954 }
955
956 std::string NetworkRequest::contentType() const
957 {
958 char *ptr = NULL;
959 if ( curl_easy_getinfo( d_func()->_easyHandle, CURLINFO_CONTENT_TYPE, &ptr ) == CURLE_OK && ptr )
960 return std::string(ptr);
961 return std::string();
962 }
963
964 zypp::ByteCount NetworkRequest::reportedByteCount() const
965 {
966 return std::visit([](auto& arg) -> zypp::ByteCount {
967 using T = std::decay_t<decltype(arg)>;
968 if constexpr (std::is_same_v<T, NetworkRequestPrivate::pending_t> || std::is_same_v<T, NetworkRequestPrivate::prepareNextRangeBatch_t> )
969 return zypp::ByteCount(0);
970 else if constexpr (std::is_same_v<T, NetworkRequestPrivate::running_t>
971 || std::is_same_v<T, NetworkRequestPrivate::finished_t>)
972 return arg._contentLenght;
973 else
974 static_assert(always_false<T>::value, "Unhandled state type");
975 }, d_func()->_runningMode);
976 }
977
978 zypp::ByteCount NetworkRequest::downloadedByteCount() const
979 {
980 return std::visit([](auto& arg) -> zypp::ByteCount {
981 using T = std::decay_t<decltype(arg)>;
982 if constexpr (std::is_same_v<T, NetworkRequestPrivate::pending_t>)
983 return zypp::ByteCount();
984 else if constexpr (std::is_same_v<T, NetworkRequestPrivate::running_t>
985 || std::is_same_v<T, NetworkRequestPrivate::prepareNextRangeBatch_t>
986 || std::is_same_v<T, NetworkRequestPrivate::finished_t>)
987 return arg._downloaded;
988 else
989 static_assert(always_false<T>::value, "Unhandled state type");
990 }, d_func()->_runningMode);
991 }
992
993 TransferSettings &NetworkRequest::transferSettings()
994 {
995 return d_func()->_settings;
996 }
997
998 NetworkRequest::State NetworkRequest::state() const
999 {
1000 return std::visit([this](auto& arg) {
1001 using T = std::decay_t<decltype(arg)>;
1002 if constexpr (std::is_same_v<T, NetworkRequestPrivate::pending_t>)
1003 return Pending;
1004 else if constexpr (std::is_same_v<T, NetworkRequestPrivate::running_t> || std::is_same_v<T, NetworkRequestPrivate::prepareNextRangeBatch_t> )
1005 return Running;
1006 else if constexpr (std::is_same_v<T, NetworkRequestPrivate::finished_t>) {
1007 if ( std::get<NetworkRequestPrivate::finished_t>( d_func()->_runningMode )._result.isError() )
1008 return Error;
1009 else
1010 return Finished;
1011 }
1012 else
1013 static_assert(always_false<T>::value, "Unhandled state type");
1014 }, d_func()->_runningMode);
1015 }
1016
1017 NetworkRequestError NetworkRequest::error() const
1018 {
1019 const auto s = state();
1020 if ( s != Error && s != Finished )
1021 return NetworkRequestError();
1022 return std::get<NetworkRequestPrivate::finished_t>( d_func()->_runningMode)._result;
1023 }
1024
1025 std::string NetworkRequest::extendedErrorString() const
1026 {
1027 if ( !hasError() )
1028 return std::string();
1029
1030 return error().nativeErrorString();
1031 }
1032
1033 bool NetworkRequest::hasError() const
1034 {
1035 return error().isError();
1036 }
1037
1038 bool NetworkRequest::addRequestHeader( const std::string &header )
1039 {
1040 Z_D();
1041
1042 curl_slist *res = curl_slist_append( d->_headers ? d->_headers.get() : nullptr, header.c_str() );
1043 if ( !res )
1044 return false;
1045
1046 if ( !d->_headers )
1047 d->_headers = std::unique_ptr< curl_slist, decltype (&curl_slist_free_all) >( res, &curl_slist_free_all );
1048
1049 return true;
1050 }
1051
1052 const zypp::Pathname &NetworkRequest::cookieFile() const
1053 {
1054 return d_func()->_currentCookieFile;
1055 }
1056
1057 void NetworkRequest::setCookieFile(zypp::Pathname cookieFile )
1058 {
1059 d_func()->_currentCookieFile = std::move(cookieFile);
1060 }
1061
1062 SignalProxy<void (NetworkRequest &req)> NetworkRequest::sigStarted()
1063 {
1064 return d_func()->_sigStarted;
1065 }
1066
1067 SignalProxy<void (NetworkRequest &req, zypp::ByteCount count)> NetworkRequest::sigBytesDownloaded()
1068 {
1069 return d_func()->_sigBytesDownloaded;
1070 }
1071
1072 SignalProxy<void (NetworkRequest &req, off_t dltotal, off_t dlnow, off_t ultotal, off_t ulnow)> NetworkRequest::sigProgress()
1073 {
1074 return d_func()->_sigProgress;
1075 }
1076
1077 SignalProxy<void (zyppng::NetworkRequest &req, const zyppng::NetworkRequestError &err)> NetworkRequest::sigFinished()
1078 {
1079 return d_func()->_sigFinished;
1080 }
1081
1082}
ZYPP_CAUGHT
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition Exception.h:475
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
IOTools.h
DBG
#define DBG
Definition Logger.h:129
MIL
#define MIL
Definition Logger.h:130
Pathname.h
StringV.h
String.h
zypp::ByteCount
Store and operate with byte count.
Definition ByteCount.h:32
zypp::ByteCount::SizeType
Unit::ValueType SizeType
Definition ByteCount.h:38
zypp::ByteCount::B
static const Unit B
1 Byte
Definition ByteCount.h:43
zypp::CheckSum
Definition CheckSum.h:34
zypp::Digest
Compute Message Digests (MD5, SHA1 etc).
Definition Digest.h:38
zypp::Digest::create
bool create(const std::string &name)
initialize creation of a new message digest
Definition Digest.cc:198
zypp::Exception
Base class for Exception.
Definition Exception.h:153
zypp::Exception::asString
std::string asString() const
Error message provided by dumpOn as string.
Definition Exception.cc:124
zypp::UByteArray
Definition ByteArray.h:52
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::Url::getScheme
std::string getScheme() const
Returns the scheme name of the URL.
Definition Url.cc:560
zypp::Url::asString
std::string asString() const
Returns a default string representation of the Url object.
Definition Url.cc:524
zypp::Url::getHost
std::string getHost(EEncoding eflag=zypp::url::E_DECODED) const
Returns the hostname or IP from the URL authority.
Definition Url.cc:615
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zypp::filesystem::Pathname::c_str
const char * c_str() const
String representation.
Definition Pathname.h:113
zypp::filesystem::Pathname::asString
const std::string & asString() const
String representation.
Definition Pathname.h:94
zypp::filesystem::Pathname::empty
bool empty() const
Test for an empty path.
Definition Pathname.h:117
zypp::media::CurlAuthData::auth_type_str2long
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_* ...
Definition curlauthdata.cc:50
zypp::media::MediaCurlInitException
Definition mediaexception.h:227
zypp::media::MediaCurlSetOptException
Definition mediaexception.h:385
zypp::media::TransferSettings::password
const std::string & password() const
auth password
Definition transfersettings.cc:175
zypp::media::TransferSettings::maxDownloadSpeed
long maxDownloadSpeed() const
Maximum download speed (bytes per second).
Definition transfersettings.cc:261
zypp::media::TransferSettings::connectTimeout
long connectTimeout() const
connection timeout
Definition transfersettings.cc:240
zypp::media::TransferSettings::authType
const std::string & authType() const
get the allowed authentication types
Definition transfersettings.cc:326
zypp::media::TransferSettings::timeout
long timeout() const
transfer timeout
Definition transfersettings.cc:233
zypp::media::TransferSettings::clientCertificatePath
const Pathname & clientCertificatePath() const
SSL client certificate file.
Definition transfersettings.cc:291
zypp::media::TransferSettings::minDownloadSpeed
long minDownloadSpeed() const
Minimum download speed (bytes per second) until the connection is dropped.
Definition transfersettings.cc:254
zypp::media::TransferSettings::headers
const Headers & headers() const
returns a list of all added headers (trimmed)
Definition transfersettings.cc:141
zypp::media::TransferSettings::proxy
const std::string & proxy() const
proxy host
Definition transfersettings.cc:198
zypp::media::TransferSettings::clientKeyPath
const Pathname & clientKeyPath() const
SSL client key file.
Definition transfersettings.cc:301
zypp::media::TransferSettings::setUserAgentString
void setUserAgentString(std::string &&val_r)
sets the user agent ie: "Mozilla v3" (trims)
Definition transfersettings.cc:150
zypp::media::TransferSettings::addHeader
void addHeader(std::string &&val_r)
add a header, on the form "Foo: Bar" (trims)
Definition transfersettings.cc:138
zypp::media::TransferSettings::proxyUserPassword
std::string proxyUserPassword() const
returns the proxy user and password as a user:pass string
Definition transfersettings.cc:220
zypp::media::TransferSettings::verifyHostEnabled
bool verifyHostEnabled() const
Whether to verify host for ssl.
Definition transfersettings.cc:275
zypp::media::TransferSettings::userAgentString
const std::string & userAgentString() const
user agent string (trimmed)
Definition transfersettings.cc:153
zypp::media::TransferSettings::hasCredentials
bool hasCredentials() const
has a non-empty username and a non-empty password
Definition transfersettings.cc:157
zypp::media::TransferSettings::headRequestsAllowed
bool headRequestsAllowed() const
whether HEAD requests are allowed
Definition transfersettings.cc:333
zypp::media::TransferSettings::cookieFileEnabled
bool cookieFileEnabled() const
Definition transfersettings.cc:307
zypp::media::TransferSettings::proxyEnabled
bool proxyEnabled() const
proxy is enabled
Definition transfersettings.cc:188
zypp::media::TransferSettings::username
const std::string & username() const
auth username
Definition transfersettings.cc:166
zypp::media::TransferSettings::proxyUsername
const std::string & proxyUsername() const
proxy auth username
Definition transfersettings.cc:208
zypp::media::TransferSettings::certificateAuthoritiesPath
const Pathname & certificateAuthoritiesPath() const
SSL certificate authorities path ( default: /etc/ssl/certs ).
Definition transfersettings.cc:316
zypp::media::TransferSettings::logCredentials
std::ostream & logCredentials(std::ostream &str) const
log credentials to stream hiding the password.
Definition transfersettings.h:67
zypp::media::TransferSettings::verifyPeerEnabled
bool verifyPeerEnabled() const
Whether to verify peer for ssl.
Definition transfersettings.cc:282
zyppng::BasePrivate::BasePrivate
BasePrivate(Base &b)
Definition base_p.h:17
zyppng::Base::Base
Base()
Definition base.cc:20
zyppng::CurlMultiPartHandler
The CurlMultiPartHandler class.
Definition curlmultiparthandler.h:70
zyppng::CurlMultiPartHandler::prepare
bool prepare()
Definition curlmultiparthandler.cc:522
zyppng::CurlMultiPartHandler::lastErrorMessage
const std::string & lastErrorMessage() const
Definition curlmultiparthandler.cc:466
zyppng::CurlMultiPartHandler::Pending
@ Pending
Definition curlmultiparthandler.h:95
zyppng::CurlMultiPartHandler::Finished
@ Finished
Definition curlmultiparthandler.h:97
zyppng::CurlMultiPartHandler::ProtocolMode::HTTP
@ HTTP
Definition curlmultiparthandler.h:75
zyppng::CurlMultiPartHandler::ProtocolMode::Basic
@ Basic
Definition curlmultiparthandler.h:74
zyppng::HttpHeader::HttpHeader
HttpHeader(std::string key_r, std::string value_r)
Definition httpheader.h:24
zyppng::NetworkRequestErrorPrivate::customError
static zyppng::NetworkRequestError customError(NetworkRequestError::Type t, std::string &&errorMsg="", std::map< std::string, boost::any > &&extraInfo={})
Definition networkrequesterror.cc:34
zyppng::NetworkRequestError
The NetworkRequestError class Represents a error that occured in.
Definition networkrequesterror.h:24
zyppng::NetworkRequestError::type
Type type() const
type Returns the type of the error
Definition networkrequesterror.cc:230
zyppng::NetworkRequestError::NetworkRequestError
NetworkRequestError()
Definition networkrequesterror.cc:226
zyppng::NetworkRequestError::nativeErrorString
std::string nativeErrorString() const
Definition networkrequesterror.cc:297
zyppng::NetworkRequestError::Type
Type
Definition networkrequesterror.h:26
zyppng::NetworkRequestError::InternalError
@ InternalError
Definition networkrequesterror.h:28
zyppng::NetworkRequestError::ExceededMaxLen
@ ExceededMaxLen
Definition networkrequesterror.h:32
zyppng::NetworkRequestError::RangeFail
@ RangeFail
Definition networkrequesterror.h:44
zyppng::NetworkRequestError::Timeout
@ Timeout
Definition networkrequesterror.h:37
zyppng::NetworkRequestError::NoError
@ NoError
Definition networkrequesterror.h:27
zyppng::NetworkRequestError::InvalidChecksum
@ InvalidChecksum
Definition networkrequesterror.h:33
zyppng::NetworkRequestError::isError
bool isError() const
isError Will return true if this is a actual error
Definition networkrequesterror.cc:240
zyppng::NetworkRequestPrivate
Definition request_p.h:35
zyppng::NetworkRequestPrivate::headerfunction
size_t headerfunction(char *ptr, size_t bytes) override
Definition request.cc:608
zyppng::NetworkRequestPrivate::_easyHandle
void * _easyHandle
Definition request_p.h:125
zyppng::NetworkRequestPrivate::_fileVerification
std::optional< FileVerifyInfo > _fileVerification
The digest for the full file.
Definition request_p.h:117
zyppng::NetworkRequestPrivate::_protocolMode
enum zyppng::NetworkRequestPrivate::ProtocolMode _protocolMode
zyppng::NetworkRequestPrivate::notifyErrorCodeChanged
void notifyErrorCodeChanged() override
Definition request.cc:734
zyppng::NetworkRequestPrivate::_url
Url _url
Definition request_p.h:106
zyppng::NetworkRequestPrivate::_targetFile
zypp::Pathname _targetFile
Definition request_p.h:107
zyppng::NetworkRequestPrivate::resetActivityTimer
void resetActivityTimer()
Definition request.cc:574
zyppng::NetworkRequestPrivate::_currentCookieFile
zypp::Pathname _currentCookieFile
Definition request_p.h:123
zyppng::NetworkRequestPrivate::_sigBytesDownloaded
Signal< void(NetworkRequest &req, zypp::ByteCount count)> _sigBytesDownloaded
Definition request_p.h:130
zyppng::NetworkRequestPrivate::_dispatcher
NetworkRequestDispatcher * _dispatcher
Definition request_p.h:126
zyppng::NetworkRequestPrivate::_requestedRanges
std::vector< NetworkRequest::Range > _requestedRanges
the requested ranges that need to be downloaded
Definition request_p.h:111
zyppng::NetworkRequestPrivate::writefunction
size_t writefunction(char *ptr, std::optional< off_t > offset, size_t bytes) override
Definition request.cc:669
zyppng::NetworkRequestPrivate::curlProgressCallback
static int curlProgressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
Definition request.cc:583
zyppng::NetworkRequestPrivate::errorMessage
std::string errorMessage() const
Definition request.cc:569
zyppng::NetworkRequestPrivate::_sigStarted
Signal< void(NetworkRequest &req)> _sigStarted
Definition request_p.h:129
zyppng::NetworkRequestPrivate::_fMode
NetworkRequest::FileMode _fMode
Definition request_p.h:119
zyppng::NetworkRequestPrivate::_runningMode
std::variant< pending_t, running_t, prepareNextRangeBatch_t, finished_t > _runningMode
Definition request_p.h:188
zyppng::NetworkRequestPrivate::initialize
bool initialize(std::string &errBuf)
Definition request.cc:92
zyppng::NetworkRequestPrivate::onActivityTimeout
void onActivityTimeout(Timer &)
Definition request.cc:560
zyppng::NetworkRequestPrivate::_sigProgress
Signal< void(NetworkRequest &req, off_t dltotal, off_t dlnow, off_t ultotal, off_t ulnow)> _sigProgress
Definition request_p.h:131
zyppng::NetworkRequestPrivate::dequeueNotify
void dequeueNotify()
Definition request.cc:482
zyppng::NetworkRequestPrivate::_lastRedirect
std::string _lastRedirect
to log/report redirections
Definition request_p.h:122
zyppng::NetworkRequestPrivate::_options
NetworkRequest::Options _options
Definition request_p.h:109
zyppng::NetworkRequestPrivate::prepareToContinue
bool prepareToContinue(std::string &errBuf)
Definition request.cc:414
zyppng::NetworkRequestPrivate::ProtocolMode::HTTP
@ HTTP
Definition request_p.h:40
zyppng::NetworkRequestPrivate::ProtocolMode::Default
@ Default
Definition request_p.h:39
zyppng::NetworkRequestPrivate::setResult
void setResult(NetworkRequestError &&err)
Definition request.cc:491
zyppng::NetworkRequestPrivate::assertOutputFile
bool assertOutputFile()
Definition request.cc:376
zyppng::NetworkRequestPrivate::~NetworkRequestPrivate
~NetworkRequestPrivate() override
Definition request.cc:81
zyppng::NetworkRequestPrivate::_errorBuf
std::array< char, CURL_ERROR_SIZE+1 > _errorBuf
Definition request_p.h:95
zyppng::NetworkRequestPrivate::reset
void reset()
Definition request.cc:545
zyppng::NetworkRequestPrivate::canRecover
bool canRecover() const
Definition request.cc:406
zyppng::NetworkRequestPrivate::setupHandle
bool setupHandle(std::string &errBuf)
Definition request.cc:105
zyppng::NetworkRequestPrivate::NetworkRequestPrivate
NetworkRequestPrivate(Url &&url, zypp::Pathname &&targetFile, NetworkRequest::FileMode fMode, NetworkRequest &p)
Definition request.cc:73
zyppng::NetworkRequestPrivate::hasMoreWork
bool hasMoreWork() const
Definition request.cc:454
zyppng::NetworkRequestPrivate::_settings
TransferSettings _settings
Definition request_p.h:108
zyppng::NetworkRequestPrivate::setCurlOption
void setCurlOption(CURLoption opt, T data)
Definition request_p.h:98
zyppng::NetworkRequestPrivate::_expectedFileSize
zypp::ByteCount _expectedFileSize
Definition request_p.h:110
zyppng::NetworkRequestPrivate::_sigFinished
Signal< void(NetworkRequest &req, const NetworkRequestError &err)> _sigFinished
Definition request_p.h:132
zyppng::NetworkRequestPrivate::aboutToStart
void aboutToStart()
Definition request.cc:460
zyppng::NetworkRequestPrivate::_headers
std::unique_ptr< curl_slist, decltype(&curl_slist_free_all) > _headers
Definition request_p.h:140
zyppng::NetworkRequest
Definition request.h:43
zyppng::NetworkRequest::setExpectedFileChecksum
bool setExpectedFileChecksum(const zypp::CheckSum &expected)
Definition request.cc:820
zyppng::NetworkRequest::reportedByteCount
zypp::ByteCount reportedByteCount() const
Returns the number of bytes that are reported from the backend as the full download size,...
Definition request.cc:964
zyppng::NetworkRequest::targetFilePath
const zypp::Pathname & targetFilePath() const
Returns the target filename path.
Definition request.cc:930
zyppng::NetworkRequest::downloadedByteCount
zypp::ByteCount downloadedByteCount() const
Returns the number of already downloaded bytes as reported by the backend.
Definition request.cc:978
zyppng::NetworkRequest::resetRequestRanges
void resetRequestRanges()
Definition request.cc:837
zyppng::NetworkRequest::setUrl
void setUrl(const Url &url)
This will change the URL of the request.
Definition request.cc:921
zyppng::NetworkRequest::setExpectedFileSize
void setExpectedFileSize(zypp::ByteCount expectedFileSize)
Definition request.cc:764
zyppng::NetworkRequest::setPriority
void setPriority(Priority prio, bool triggerReschedule=true)
Definition request.cc:774
zyppng::NetworkRequest::peekData
std::vector< char > peekData(off_t offset, size_t count) const
Definition request.cc:905
zyppng::NetworkRequest::HeadRequest
@ HeadRequest
Definition request.h:71
zyppng::NetworkRequest::ConnectionTest
@ ConnectionTest
Definition request.h:72
zyppng::NetworkRequest::State
State
Definition request.h:51
zyppng::NetworkRequest::Running
@ Running
Definition request.h:53
zyppng::NetworkRequest::Error
@ Error
Definition request.h:55
zyppng::NetworkRequest::Finished
@ Finished
Definition request.h:54
zyppng::NetworkRequest::Pending
@ Pending
Definition request.h:52
zyppng::NetworkRequest::contentType
std::string contentType() const
Returns the content type as reported from the server.
Definition request.cc:956
zyppng::NetworkRequest::setFileOpenMode
void setFileOpenMode(FileMode mode)
Sets the file open mode to mode.
Definition request.cc:948
zyppng::NetworkRequest::addRequestHeader
bool addRequestHeader(const std::string &header)
Definition request.cc:1038
zyppng::NetworkRequest::~NetworkRequest
~NetworkRequest() override
Definition request.cc:756
zyppng::NetworkRequest::setOptions
void setOptions(Options opt)
Definition request.cc:787
zyppng::NetworkRequest::url
Url url() const
Definition request.cc:916
zyppng::NetworkRequest::fileOpenMode
FileMode fileOpenMode() const
Returns the currently configured file open mode.
Definition request.cc:943
zyppng::NetworkRequest::expectedFileSize
zypp::ByteCount expectedFileSize() const
Definition request.cc:769
zyppng::NetworkRequest::hasError
bool hasError() const
Checks if there was a error with the request.
Definition request.cc:1033
zyppng::NetworkRequest::state
State state() const
Returns the current state the HttpDownloadRequest is in.
Definition request.cc:998
zyppng::NetworkRequest::sigFinished
SignalProxy< void(NetworkRequest &req, const NetworkRequestError &err)> sigFinished()
Signals that the download finished.
Definition request.cc:1077
zyppng::NetworkRequest::CheckSumBytes
UByteArray CheckSumBytes
Definition request.h:49
zyppng::NetworkRequest::options
Options options() const
Definition request.cc:792
zyppng::NetworkRequest::sigBytesDownloaded
SignalProxy< void(NetworkRequest &req, zypp::ByteCount count)> sigBytesDownloaded()
Signals that new data has been downloaded, this is only the payload and does not include control data...
Definition request.cc:1067
zyppng::NetworkRequest::timings
std::optional< Timings > timings() const
After the request is finished query the timings that were collected during download.
Definition request.cc:877
zyppng::NetworkRequest::FileMode
FileMode
Definition request.h:64
zyppng::NetworkRequest::WriteShared
@ WriteShared
Definition request.h:66
zyppng::NetworkRequest::extendedErrorString
std::string extendedErrorString() const
In some cases, curl can provide extended error information collected at runtime.
Definition request.cc:1025
zyppng::NetworkRequest::NetworkRequest
NetworkRequest(Url url, zypp::Pathname targetFile, FileMode fMode=WriteExclusive)
Definition request.cc:751
zyppng::NetworkRequest::priority
Priority priority() const
Definition request.cc:782
zyppng::NetworkRequest::error
NetworkRequestError error() const
Returns the last set Error.
Definition request.cc:1017
zyppng::NetworkRequest::setTargetFilePath
void setTargetFilePath(const zypp::Pathname &path)
Changes the target file path of the download.
Definition request.cc:935
zyppng::NetworkRequest::cookieFile
const zypp::Pathname & cookieFile() const
Definition request.cc:1052
zyppng::NetworkRequest::nativeHandle
void * nativeHandle() const
Definition request.cc:872
zyppng::NetworkRequest::setCookieFile
void setCookieFile(zypp::Pathname cookieFile)
Definition request.cc:1057
zyppng::NetworkRequest::addRequestRange
void addRequestRange(size_t start, size_t len=0, std::optional< zypp::Digest > &&digest={}, CheckSumBytes expectedChkSum=CheckSumBytes(), std::any userData=std::any(), std::optional< size_t > digestCompareLen={}, std::optional< size_t > chksumpad={})
Definition request.cc:797
zyppng::NetworkRequest::failedRanges
std::vector< Range > failedRanges() const
Definition request.cc:845
zyppng::NetworkRequest::requestedRanges
const std::vector< Range > & requestedRanges() const
Definition request.cc:862
zyppng::NetworkRequest::sigStarted
SignalProxy< void(NetworkRequest &req)> sigStarted()
Signals that the dispatcher dequeued the request and actually starts downloading data.
Definition request.cc:1062
zyppng::NetworkRequest::sigProgress
SignalProxy< void(NetworkRequest &req, off_t dltotal, off_t dlnow, off_t ultotal, off_t ulnow)> sigProgress()
Signals if there was data read from the download.
Definition request.cc:1072
zyppng::NetworkRequest::transferSettings
TransferSettings & transferSettings()
Definition request.cc:993
zyppng::NetworkRequest::Range
CurlMultiPartHandler::Range Range
Definition request.h:76
zyppng::NetworkRequest::lastRedirectInfo
const std::string & lastRedirectInfo() const
Definition request.cc:867
zyppng::NetworkRequest::Priority
Priority
Definition request.h:58
zyppng::SignalProxy
Definition signals.h:325
zyppng::Timer
The Timer class provides repetitive and single-shot timers.
Definition timer.h:45
zyppng::Timer::sigExpired
SignalProxy< void(Timer &t)> sigExpired()
This signal is always emitted when the timer expires.
Definition timer.cc:120
zyppng::Timer::remaining
uint64_t remaining() const
Definition timer.cc:99
zyppng::Timer::interval
uint64_t interval() const
Definition timer.cc:94
zyppng::expected
Definition expected.h:56
curlhelper_p.h
EXPLICITLY_NO_PROXY
#define EXPLICITLY_NO_PROXY
Definition curlhelper_p.h:23
linuxhelpers_p.h
mediadebug_p.h
MIL_MEDIA
#define MIL_MEDIA
Definition mediadebug_p.h:29
DBG_MEDIA
#define DBG_MEDIA
Definition mediadebug_p.h:28
internal::curlUnEscape
std::string curlUnEscape(const std::string &text_r)
Definition curlhelper.cc:394
internal::setupZYPP_MEDIA_CURL_DEBUG
void setupZYPP_MEDIA_CURL_DEBUG(CURL *curl)
Setup CURLOPT_VERBOSE and CURLOPT_DEBUGFUNCTION according to env::ZYPP_MEDIA_CURL_DEBUG.
Definition curlhelper.cc:130
internal::setCurlRedirProtocols
CURLcode setCurlRedirProtocols(CURL *curl)
Definition curlhelper.cc:544
std
Definition ansi.h:855
std::decay_t
typename decay< T >::type decay_t
Definition TypeTraits.h:42
str
String related utilities and Regular expression matching.
zypp::env::ZYPP_MEDIA_CURL_IPRESOLVE
int ZYPP_MEDIA_CURL_IPRESOLVE()
4/6 to force IPv4/v6
Definition curlhelper.cc:45
zypp::filesystem
Types and functions for filesystem operations.
Definition Glob.cc:24
zypp::filesystem::assert_file_mode
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
zypp::io::peek_data_fd
std::vector< char > peek_data_fd(FILE *fd, off_t offset, size_t count)
Definition IOTools.cc:171
zypp::str::TRIM
@ TRIM
Definition String.h:571
zypp::str::strtonum
TInt strtonum(const C_Str &str)
Parsing numbers from string.
zypp::target
Definition KeyRing.h:325
zypp::url
Url details namespace.
Definition UrlBase.cc:58
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::asString
std::string asString(const Patch::Category &obj)
relates: Patch::Category string representation.
Definition Patch.cc:122
zyppng::str::trim
T trim(StrType &&s, const Trim trim_r)
Definition string.h:35
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::TransferSettings
zypp::media::TransferSettings TransferSettings
Definition transfersettings.h:20
zyppng::strerr_cxx
std::string strerr_cxx(const int err=-1)
Definition linuxhelpers_p.h:14
zyppng::ZYPP_IMPL_PRIVATE
ZYPP_IMPL_PRIVATE(UnixSignalSource)
networkrequestdispatcher.h
networkrequesterror_p.h
request_p.h
zypp::media::CurlConfig
Structure holding values of curlrc options.
Definition curlconfig.h:27
zypp::media::CurlConfig::proxyuserpwd
std::string proxyuserpwd
Definition curlconfig.h:49
zypp::media::CurlConfig::parseConfig
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
zypp::str::Format
Convenient building of std::string with boost::format.
Definition String.h:254
zyppng::CurlMultiPartHandler::Range
Definition curlmultiparthandler.h:104
zyppng::CurlMultiPartHandler::Range::make
static Range make(size_t start, size_t len=0, std::optional< zypp::Digest > &&digest={}, CheckSumBytes &&expectedChkSum=CheckSumBytes(), std::any &&userData=std::any(), std::optional< size_t > digestCompareLen={}, std::optional< size_t > _dataBlockPadding={})
Definition curlmultiparthandler.cc:84
zyppng::NetworkRequestPrivate::FileVerifyInfo
Definition request_p.h:113
zyppng::NetworkRequestPrivate::finished_t
Definition request_p.h:182
zyppng::NetworkRequestPrivate::finished_t::_downloaded
off_t _downloaded
Definition request_p.h:183
zyppng::NetworkRequestPrivate::finished_t::_result
NetworkRequestError _result
Definition request_p.h:185
zyppng::NetworkRequestPrivate::finished_t::_contentLenght
zypp::ByteCount _contentLenght
Definition request_p.h:184
zyppng::NetworkRequestPrivate::pending_t
Definition request_p.h:146
zyppng::NetworkRequestPrivate::pending_t::pending_t
pending_t()
Definition request_p.h:147
zyppng::NetworkRequestPrivate::prepareNextRangeBatch_t
Definition request_p.h:152
zyppng::NetworkRequestPrivate::prepareNextRangeBatch_t::_downloaded
off_t _downloaded
Definition request_p.h:155
zyppng::NetworkRequestPrivate::prepareNextRangeBatch_t::prepareNextRangeBatch_t
prepareNextRangeBatch_t(running_t &&prevState)
Definition request.cc:57
zyppng::NetworkRequestPrivate::prepareNextRangeBatch_t::_partialHelper
std::unique_ptr< CurlMultiPartHandler > _partialHelper
Definition request_p.h:156
zyppng::NetworkRequestPrivate::prepareNextRangeBatch_t::_outFile
zypp::AutoFILE _outFile
Definition request_p.h:154
zyppng::NetworkRequestPrivate::running_t
Definition request_p.h:159
zyppng::NetworkRequestPrivate::running_t::_partialHelper
std::unique_ptr< CurlMultiPartHandler > _partialHelper
Definition request_p.h:166
zyppng::NetworkRequestPrivate::running_t::_downloaded
off_t _downloaded
Definition request_p.h:176
zyppng::NetworkRequestPrivate::running_t::running_t
running_t(pending_t &&prevState)
Definition request.cc:63
zyppng::NetworkRequestPrivate::running_t::_outFile
zypp::AutoFILE _outFile
Definition request_p.h:165
zyppng::NetworkRequest::Timings
Definition request.h:78
zyppng::NetworkRequest::Timings::appconnect
std::chrono::microseconds appconnect
Definition request.h:81
zyppng::NetworkRequest::Timings::redirect
std::chrono::microseconds redirect
Definition request.h:84
zyppng::NetworkRequest::Timings::pretransfer
std::chrono::microseconds pretransfer
Definition request.h:82
zyppng::NetworkRequest::Timings::total
std::chrono::microseconds total
Definition request.h:83
zyppng::NetworkRequest::Timings::namelookup
std::chrono::microseconds namelookup
Definition request.h:79
zyppng::NetworkRequest::Timings::connect
std::chrono::microseconds connect
Definition request.h:80
PathInfo.h
Z_D
#define Z_D()
Definition zyppglobal.h:98