13#include <zypp-core/zyppng/base/EventDispatcher>
15#include <zypp-core/zyppng/core/String>
18#include <zypp-curl/CurlConfig>
19#include <zypp-curl/auth/CurlAuthData>
20#include <zypp-media/MediaConfig>
31#include <boost/variant.hpp>
32#include <boost/variant/polymorphic_get.hpp>
38 static size_t nwr_headerCallback (
char *ptr,
size_t size,
size_t nmemb,
void *userdata ) {
45 static size_t nwr_writeCallback (
char *ptr,
size_t size,
size_t nmemb,
void *userdata ) {
54 template<
class T>
struct always_false : std::false_type {};
78 ,
_headers(
std::unique_ptr< curl_slist, decltype (&curl_slist_free_all) >( nullptr, &curl_slist_free_all ) )
111 const std::string urlScheme =
_url.getScheme();
112 if ( urlScheme ==
"http" || urlScheme ==
"https" )
124 std::string urlBuffer(
_url.asString() );
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 ) {
157 "%s: %s", key.c_str(), value.c_str() )
168 case 4:
setCurlOption( CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
break;
169 case 6:
setCurlOption( CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6 );
break;
173 setCurlOption( CURLOPT_HEADERFUNCTION, &nwr_headerCallback );
188 if ( urlScheme ==
"https" )
209#ifdef CURLSSLOPT_ALLOW_BEAST
211 setCurlOption( CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST );
237 std::string use_auth =
_settings.authType();
238 if (use_auth.empty())
239 use_auth =
"digest,basic";
241 if( auth != CURLAUTH_NONE)
243 DBG <<
_easyHandle <<
" " <<
"Enabling HTTP authentication methods: " << use_auth
244 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
253 setCurlOption(CURLOPT_PROXYAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_NTLM );
264 if ( proxyuserpwd.empty() )
269 DBG <<
_easyHandle <<
" " <<
"Proxy: ~/.curlrc does not contain the proxy-user option" << std::endl;
273 DBG <<
_easyHandle <<
" " <<
"Proxy: using proxy-user from ~/.curlrc" << std::endl;
278 DBG <<
_easyHandle <<
" " <<
"Proxy: using provided proxy-user '" <<
_settings.proxyUsername() <<
"'" << std::endl;
281 if ( ! proxyuserpwd.empty() )
286#if CURLVERSION_AT_LEAST(7,19,4)
291 DBG <<
_easyHandle <<
" " <<
"Proxy: explicitly NOPROXY" << std::endl;
306#if CURLVERSION_AT_LEAST(7,15,5)
318#if CURLVERSION_AT_LEAST(7,18,0)
325 for (
const auto &header : locSet.
headers() ) {
326 if ( !z_func()->addRequestHeader( header.c_str() ) )
338 return ( elem1._start < elem2._start );
342 if (
auto initState = std::get_if<pending_t>(&
_runningMode) ) {
345 initState->_partialHelper = std::make_unique<CurlMultiPartHandler>(
351 helper = initState->_partialHelper.get();
353 }
else if (
auto pendingState = std::get_if<prepareNextRangeBatch_t>(&
_runningMode) ) {
354 helper = pendingState->_partialHelper.get();
356 errBuf =
"Request is in invalid state to call setupHandle";
378 auto rmode = std::get_if<NetworkRequestPrivate::running_t>( &
_runningMode );
380 DBG <<
_easyHandle <<
"Can only create output file in running mode" << std::endl;
384 if ( !rmode->_outFile ) {
385 std::string openMode =
"w+b";
389 rmode->_outFile = fopen(
_targetFile.asString().c_str() , openMode.c_str() );
393 rmode->_outFile = fopen(
_targetFile.asString().c_str() ,
"w+b" );
396 if ( !rmode->_outFile ) {
409 auto rmode = std::get_if<NetworkRequestPrivate::running_t>( &
_runningMode );
410 if ( rmode->_partialHelper )
return rmode->_partialHelper->canRecover();
416 auto rmode = std::get_if<NetworkRequestPrivate::running_t>( &
_runningMode );
418 errBuf =
"NetworkRequestPrivate::prepareToContinue called in invalid state";
422 if ( rmode->_partialHelper && rmode->_partialHelper->hasMoreWork() ) {
428 auto &prepMode = std::get<prepareNextRangeBatch_t>(
_runningMode);
429 if ( !prepMode._partialHelper->prepareToContinue() ) {
430 errBuf = prepMode._partialHelper->lastErrorMessage();
434 if ( hadRangeFail ) {
442 MIL <<
_easyHandle <<
" curl_easy_reset after hadRangeFail" << std::endl;
449 errBuf =
"Request has no more work";
457 return std::any_of(
_requestedRanges.begin(),
_requestedRanges.end(), [](
const auto &range ){ return range._rangeState == CurlMultiPartHandler::Pending; });
462 bool isRangeContinuation = std::holds_alternative<prepareNextRangeBatch_t>(
_runningMode );
463 if ( isRangeContinuation ) {
464 MIL <<
_easyHandle <<
" " <<
"Continuing a previously started range batch." << std::endl;
472 if ( m._activityTimer ) {
475 m._activityTimer->start(
static_cast<uint64_t
>(
_settings.timeout() * 1000 ) );
478 if ( !isRangeContinuation )
484 if ( std::holds_alternative<running_t>(
_runningMode) ) {
486 if ( rmode._partialHelper )
487 rmode._partialHelper->finalize();
494 resState.
_result = std::move(err);
496 if ( std::holds_alternative<running_t>(
_runningMode) ) {
505 if ( !rmode._partialHelper->verifyData() ){
512 if ( fseek( rmode._outFile, 0, SEEK_SET ) != 0 ) {
515 constexpr size_t bufSize = 4096;
517 while(
auto cnt = fread(buf, 1, bufSize, rmode._outFile ) > 0 ) {
529 if ( calcSum != expSum ) {
538 rmode._outFile.reset();
563 std::map<std::string, boost::any> extraInfo;
564 extraInfo.insert( {
"requestUrl",
_url } );
576 if ( std::holds_alternative<running_t>(
_runningMode ) ){
578 if ( rmode._activityTimer && rmode._activityTimer->isRunning() )
579 rmode._activityTimer->start();
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;
593 auto &rmode = std::get<running_t>( that->
_runningMode );
598 rmode._isInCallback =
true;
599 if ( rmode._lastProgressNow != dlnow ) {
600 rmode._lastProgressNow = dlnow;
601 that->
_sigProgress.emit( *that->z_func(), dltotal, dlnow, ultotal, ulnow );
603 rmode._isInCallback =
false;
605 return rmode._cachedResult ? CURLE_ABORTED_BY_CALLBACK : CURLE_OK;
618 std::string_view hdr( ptr, bytes );
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) );
625 hdr = std::string_view();
627 if ( !std::holds_alternative<running_t>(
_runningMode) ){
628 DBG <<
_easyHandle <<
" " <<
"Curl headerfunction callback was called in invalid state "<< z_func()->state() << std::endl;
636 const auto &repSize = rmode._partialHelper->reportedFileSize ();
642 if ( zypp::strv::hasPrefixCI( hdr,
"HTTP/" ) ) {
645 (void)curl_easy_getinfo(
_easyHandle, CURLINFO_RESPONSE_CODE, &statuscode);
651 }
else if ( zypp::strv::hasPrefixCI( hdr,
"Location:" ) ) {
655 }
else if ( zypp::strv::hasPrefixCI( hdr,
"Content-Length:") ) {
657 auto str = std::string ( lenStr.data(), lenStr.length() );
660 DBG <<
_easyHandle <<
" " <<
"Got Content-Length Header: " << len << std::endl;
682 if ( !std::holds_alternative<running_t>(
_runningMode) ){
683 DBG <<
_easyHandle <<
" " <<
"Curl writefunction callback was called in invalid state "<< z_func()->state() << std::endl;
694 if ( fseek( rmode._outFile, *offset, SEEK_SET ) != 0 ) {
696 "Unable to set output file pointer." );
699 rmode._currentFileOffset = *offset;
703 const auto &repSize = rmode._partialHelper->reportedFileSize ();
716 auto written = fwrite( data, 1, max, rmode._outFile );
725 rmode._currentFileOffset += written;
728 rmode._downloaded += written;
736 auto rmode = std::get_if<NetworkRequestPrivate::running_t>( &
_runningMode );
737 if ( !rmode || !rmode->_partialHelper || !rmode->_partialHelper->hasError() )
741 if ( rmode->_cachedResult )
745 MIL_MEDIA <<
_easyHandle <<
" Multipart handler announced error code " << lastErr.toString () << std::endl;
746 rmode->_cachedResult = lastErr;
760 if ( d->_dispatcher )
761 d->_dispatcher->cancel( *
this,
"Request destroyed while still running" );
771 return d_func()->_expectedFileSize;
778 if (
state() ==
Pending && triggerReschedule && d->_dispatcher )
779 d->_dispatcher->reschedule();
784 return d_func()->_priority;
789 d_func()->_options = opt;
794 return d_func()->_options;
803 d->_requestedRanges.push_back(
Range::make( start, len, std::move(digest), std::move( expectedChkSum ), std::move( userData ), digestCompareLen, chksumpad ) );
812 d->_requestedRanges.push_back( std::move(range) );
813 auto &rng = d->_requestedRanges.back();
815 rng.bytesWritten = 0;
817 rng._digest->reset();
831 ._fileDigest = std::move(fDig),
842 d->_requestedRanges.clear();
847 const auto mystate =
state();
853 std::vector<Range> failed;
854 for (
auto &r : d->_requestedRanges ) {
856 failed.push_back( r.clone() );
864 return d_func()->_requestedRanges;
869 return d_func()->_lastRedirect;
874 return d_func()->_easyHandle;
879 const auto myerr =
error();
880 const auto mystate =
state();
886 auto getMeasurement = [ this ](
const CURLINFO info, std::chrono::microseconds &
target ){
887 using FPSeconds = std::chrono::duration<double, std::chrono::seconds::period>;
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) );
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);
909 if ( !std::holds_alternative<NetworkRequestPrivate::running_t>( d->_runningMode) )
912 const auto &rmode = std::get<NetworkRequestPrivate::running_t>( d->_runningMode );
918 return d_func()->_url;
932 return d_func()->_targetFile;
940 d->_targetFile = path;
945 return d_func()->_fMode;
953 d->_fMode = std::move( mode );
959 if ( curl_easy_getinfo( d_func()->_easyHandle, CURLINFO_CONTENT_TYPE, &ptr ) == CURLE_OK && ptr )
960 return std::string(ptr);
961 return std::string();
968 if constexpr (std::is_same_v<T, NetworkRequestPrivate::pending_t> || std::is_same_v<T, NetworkRequestPrivate::prepareNextRangeBatch_t> )
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;
974 static_assert(always_false<T>::value,
"Unhandled state type");
975 }, d_func()->_runningMode);
982 if constexpr (std::is_same_v<T, NetworkRequestPrivate::pending_t>)
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;
989 static_assert(always_false<T>::value,
"Unhandled state type");
990 }, d_func()->_runningMode);
995 return d_func()->_settings;
1000 return std::visit([
this](
auto& arg) {
1002 if constexpr (std::is_same_v<T, NetworkRequestPrivate::pending_t>)
1004 else if constexpr (std::is_same_v<T, NetworkRequestPrivate::running_t> || std::is_same_v<T, NetworkRequestPrivate::prepareNextRangeBatch_t> )
1006 else if constexpr (std::is_same_v<T, NetworkRequestPrivate::finished_t>) {
1007 if ( std::get<NetworkRequestPrivate::finished_t>( d_func()->_runningMode )._result.isError() )
1013 static_assert(always_false<T>::value,
"Unhandled state type");
1014 }, d_func()->_runningMode);
1019 const auto s =
state();
1022 return std::get<NetworkRequestPrivate::finished_t>( d_func()->_runningMode)._result;
1028 return std::string();
1042 curl_slist *res = curl_slist_append( d->_headers ? d->_headers.get() :
nullptr, header.c_str() );
1047 d->_headers = std::unique_ptr< curl_slist, decltype (&curl_slist_free_all) >( res, &curl_slist_free_all );
1054 return d_func()->_currentCookieFile;
1059 d_func()->_currentCookieFile = std::move(
cookieFile);
1064 return d_func()->_sigStarted;
1069 return d_func()->_sigBytesDownloaded;
1074 return d_func()->_sigProgress;
1079 return d_func()->_sigFinished;
Store and operate with byte count.
static const Unit B
1 Byte
Compute Message Digests (MD5, SHA1 etc)
bool create(const std::string &name)
initialize creation of a new message digest
Base class for Exception.
std::string asString() const
Error message provided by dumpOn as string.
const char * c_str() const
String representation.
bool empty() const
Test for an empty path.
The CurlMultiPartHandler class.
const std::string & lastErrorMessage() const
static zyppng::NetworkRequestError customError(NetworkRequestError::Type t, std::string &&errorMsg="", std::map< std::string, boost::any > &&extraInfo={})
The NetworkRequestError class Represents a error that occured in.
Type type() const
type Returns the type of the error
std::string nativeErrorString() const
bool isError() const
isError Will return true if this is a actual error
size_t headerfunction(char *ptr, size_t bytes) override
std::optional< FileVerifyInfo > _fileVerification
The digest for the full file.
enum zyppng::NetworkRequestPrivate::ProtocolMode _protocolMode
void notifyErrorCodeChanged() override
zypp::Pathname _targetFile
void resetActivityTimer()
zypp::Pathname _currentCookieFile
Signal< void(NetworkRequest &req, zypp::ByteCount count)> _sigBytesDownloaded
NetworkRequestDispatcher * _dispatcher
std::vector< NetworkRequest::Range > _requestedRanges
the requested ranges that need to be downloaded
size_t writefunction(char *ptr, std::optional< off_t > offset, size_t bytes) override
static int curlProgressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
std::string errorMessage() const
Signal< void(NetworkRequest &req)> _sigStarted
NetworkRequest::FileMode _fMode
std::variant< pending_t, running_t, prepareNextRangeBatch_t, finished_t > _runningMode
bool initialize(std::string &errBuf)
void onActivityTimeout(Timer &)
Signal< void(NetworkRequest &req, off_t dltotal, off_t dlnow, off_t ultotal, off_t ulnow)> _sigProgress
std::string _lastRedirect
to log/report redirections
NetworkRequest::Options _options
bool prepareToContinue(std::string &errBuf)
void setResult(NetworkRequestError &&err)
~NetworkRequestPrivate() override
std::array< char, CURL_ERROR_SIZE+1 > _errorBuf
bool setupHandle(std::string &errBuf)
NetworkRequestPrivate(Url &&url, zypp::Pathname &&targetFile, NetworkRequest::FileMode fMode, NetworkRequest &p)
TransferSettings _settings
void setCurlOption(CURLoption opt, T data)
zypp::ByteCount _expectedFileSize
Signal< void(NetworkRequest &req, const NetworkRequestError &err)> _sigFinished
std::unique_ptr< curl_slist, decltype(&curl_slist_free_all) > _headers
bool setExpectedFileChecksum(const zypp::CheckSum &expected)
zypp::ByteCount reportedByteCount() const
Returns the number of bytes that are reported from the backend as the full download size,...
const zypp::Pathname & targetFilePath() const
Returns the target filename path.
zypp::ByteCount downloadedByteCount() const
Returns the number of already downloaded bytes as reported by the backend.
void resetRequestRanges()
void setUrl(const Url &url)
This will change the URL of the request.
void setExpectedFileSize(zypp::ByteCount expectedFileSize)
void setPriority(Priority prio, bool triggerReschedule=true)
std::vector< char > peekData(off_t offset, size_t count) const
std::string contentType() const
Returns the content type as reported from the server.
void setFileOpenMode(FileMode mode)
Sets the file open mode to mode.
bool addRequestHeader(const std::string &header)
~NetworkRequest() override
void setOptions(Options opt)
FileMode fileOpenMode() const
Returns the currently configured file open mode.
zypp::ByteCount expectedFileSize() const
bool hasError() const
Checks if there was a error with the request.
State state() const
Returns the current state the HttpDownloadRequest is in.
SignalProxy< void(NetworkRequest &req, const NetworkRequestError &err)> sigFinished()
Signals that the download finished.
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...
std::optional< Timings > timings() const
After the request is finished query the timings that were collected during download.
std::string extendedErrorString() const
In some cases, curl can provide extended error information collected at runtime.
NetworkRequest(Url url, zypp::Pathname targetFile, FileMode fMode=WriteExclusive)
Priority priority() const
NetworkRequestError error() const
Returns the last set Error.
void setTargetFilePath(const zypp::Pathname &path)
Changes the target file path of the download.
const zypp::Pathname & cookieFile() const
void * nativeHandle() const
void setCookieFile(zypp::Pathname cookieFile)
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={})
std::vector< Range > failedRanges() const
const std::vector< Range > & requestedRanges() const
SignalProxy< void(NetworkRequest &req)> sigStarted()
Signals that the dispatcher dequeued the request and actually starts downloading data.
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.
TransferSettings & transferSettings()
CurlMultiPartHandler::Range Range
const std::string & lastRedirectInfo() const
The Timer class provides repetitive and single-shot timers.
SignalProxy< void(Timer &t)> sigExpired()
This signal is always emitted when the timer expires.
uint64_t remaining() const
uint64_t interval() const
#define EXPLICITLY_NO_PROXY
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)
typename decay< T >::type decay_t
String related utilities and Regular expression matching.
int ZYPP_MEDIA_CURL_IPRESOLVE()
4/6 to force IPv4/v6
Types and functions for filesystem operations.
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
std::vector< char > peek_data_fd(FILE *fd, off_t offset, size_t count)
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
TInt strtonum(const C_Str &str)
Parsing numbers from string.
std::string trim(const std::string &s, const Trim trim_r)
Easy-to use interface to the ZYPP dependency resolver.
T trim(StrType &&s, const Trim trim_r)
zypp::media::TransferSettings TransferSettings
std::string strerr_cxx(const int err=-1)
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={})
NetworkRequestError _result
zypp::ByteCount _contentLenght
std::unique_ptr< CurlMultiPartHandler > _partialHelper
running_t(pending_t &&prevState)
std::chrono::microseconds appconnect
std::chrono::microseconds redirect
std::chrono::microseconds pretransfer
std::chrono::microseconds total
std::chrono::microseconds namelookup
std::chrono::microseconds connect
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
#define ZYPP_IMPL_PRIVATE(Class)