libzypp: zypp-curl/ng/network/private/request_p.h Source File

libzypp 17.38.14

request_p.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8----------------------------------------------------------------------/
9*
10* This file contains private API, this might break at any time between releases.
11* You have been warned!
12*
13*/
14#ifndef ZYPP_NG_MEDIA_CURL_PRIVATE_REQUEST_P_H_INCLUDED
15#define ZYPP_NG_MEDIA_CURL_PRIVATE_REQUEST_P_H_INCLUDED
16
17#include <zypp-core/CheckSum.h>
18#include <zypp-core/ng/base/private/base_p.h>
19#include <zypp-curl/ng/network/request.h>
20#include <zypp-media/MediaException>
21#include <zypp-core/ng/base/Timer>
22#include <zypp-core/base/Regex.h>
23#include <curl/curl.h>
24#include <array>
25#include <memory>
26#include <zypp-core/Digest.h>
27#include <zypp-core/AutoDispose.h>
28
29#include <boost/optional.hpp>
30#include <variant>
31
32namespace zyppng {
33
34 class NetworkRequestPrivate : public BasePrivate, public CurlMultiPartDataReceiver
35 {
36 ZYPP_DECLARE_PUBLIC(NetworkRequest)
37 public:
38 enum class ProtocolMode{
39 Default, //< use this mode if no special checks are required in header or write callbacks
40 HTTP //< this mode is used for HTTP and HTTPS downloads
41 } _protocolMode = ProtocolMode::Default;
42
43 NetworkRequestPrivate(Url &&url, zypp::Pathname &&targetFile, NetworkRequest::FileMode fMode, NetworkRequest &p );
44 ~NetworkRequestPrivate() override;
45
46 bool initialize( std::string &errBuf );
47
48 bool setupHandle ( std::string &errBuf );
49
50 bool assertOutputFile ();
51
57 bool canRecover () const;
58
64 bool prepareToContinue ( std::string &errBuf );
65
71 bool hasMoreWork() const;
72
77 void aboutToStart ( );
78
84 void dequeueNotify();
85
86 void setResult ( NetworkRequestError &&err );
87 void reset ();
88 void resetActivityTimer ();
89
90 void onActivityTimeout (Timer &);
91
92 std::string errorMessage () const;
93
94
95 std::array<char, CURL_ERROR_SIZE+1> _errorBuf; //provide a buffer for a nicely formatted error for CURL
96
97 template<typename T>
98 void setCurlOption ( CURLoption opt, T data )
99 {
100 auto ret = curl_easy_setopt( _easyHandle, opt, data );
101 if ( ret != 0 ) {
102 ZYPP_THROW( zypp::media::MediaCurlSetOptException( _url, _errorBuf.data() ) );
103 }
104 }
105
106 Url _url; //file URL
107 zypp::Pathname _targetFile; //target file
108 TransferSettings _settings;
109 NetworkRequest::Options _options;
110 zypp::ByteCount _expectedFileSize; // the file size as expected by the user code
111 std::vector<NetworkRequest::Range> _requestedRanges;
112
113 struct FileVerifyInfo {
114 zypp::Digest _fileDigest;
115 zypp::CheckSum _fileChecksum;
116 };
117 std::optional<FileVerifyInfo> _fileVerification;
118
119 NetworkRequest::FileMode _fMode = NetworkRequest::WriteExclusive;
120 NetworkRequest::Priority _priority = NetworkRequest::Normal;
121
122 std::string _lastRedirect;
123 zypp::Pathname _currentCookieFile = "/var/lib/YaST2/cookies";
124
125 void *_easyHandle = nullptr; // the easy handle that controlling this request
126 NetworkRequestDispatcher *_dispatcher = nullptr; // the parent downloader owning this request
127
128 //signals
129 Signal< void ( NetworkRequest &req )> _sigStarted;
130 Signal< void ( NetworkRequest &req, zypp::ByteCount count )> _sigBytesDownloaded;
131 Signal< void ( NetworkRequest &req, off_t dltotal, off_t dlnow, off_t ultotal, off_t ulnow )> _sigProgress;
132 Signal< void ( NetworkRequest &req, const NetworkRequestError &err )> _sigFinished;
133
134 static int curlProgressCallback ( void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow );
135
136 size_t headerfunction ( char *ptr, size_t bytes ) override;
137 size_t writefunction (char *ptr, std::optional<off_t> offset, size_t bytes ) override;
138 void notifyErrorCodeChanged () override;
139
140 std::unique_ptr< curl_slist, decltype (&curl_slist_free_all) > _headers;
141
142 struct pending_t;
143 struct running_t;
144 struct prepareNextRangeBatch_t;
145
146 struct pending_t {
147 pending_t(){}
148 std::unique_ptr<CurlMultiPartHandler> _partialHelper = {};
149 };
150
151 struct prepareNextRangeBatch_t
152 {
153 prepareNextRangeBatch_t( running_t &&prevState );
154 zypp::AutoFILE _outFile; //the file we are writing to
155 off_t _downloaded = 0; //downloaded bytes
156 std::unique_ptr<CurlMultiPartHandler> _partialHelper = {};
157 };
158
159 struct running_t {
160 running_t( pending_t &&prevState );
161 running_t( prepareNextRangeBatch_t &&prevState );
162
163 Timer::Ptr _activityTimer = Timer::create();
164
165 zypp::AutoFILE _outFile;
166 std::unique_ptr<CurlMultiPartHandler> _partialHelper = {};
167
168 // handle the case when cancel() is called from a slot to the progress signal
169 bool _isInCallback = false;
170
171 // used to handle cancel() when emitting the progress signal, or when we explicitely trigger
172 // a error during callbacks.
173 std::optional<NetworkRequestError> _cachedResult;
174
175 off_t _lastProgressNow = -1; // last value returned from CURL, lets only send signals if we get actual updates
176 off_t _downloaded = 0; //downloaded bytes
177 off_t _currentFileOffset = 0;
178
179 zypp::ByteCount _contentLenght; // the content length as reported by the server
180 };
181
182 struct finished_t {
183 off_t _downloaded = 0; //downloaded bytes
184 zypp::ByteCount _contentLenght = 0; // the content length as reported by the server
185 NetworkRequestError _result; // the overall result of the download
186 };
187
188 std::variant< pending_t, running_t, prepareNextRangeBatch_t, finished_t > _runningMode = pending_t();
189 };
190}
191
192#endif
AutoDispose.h
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
Regex.h
base_p.h
zypp::ByteCount
Store and operate with byte count.
Definition ByteCount.h:32
zypp::CheckSum
Definition CheckSum.h:34
zypp::Digest
Compute Message Digests (MD5, SHA1 etc).
Definition Digest.h:38
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zypp::media::MediaCurlSetOptException
Definition mediaexception.h:385
zyppng::BasePrivate::BasePrivate
BasePrivate(Base &b)
Definition base_p.h:17
zyppng::CurlMultiPartDataReceiver
Definition curlmultiparthandler.h:19
zyppng::NetworkRequestError
The NetworkRequestError class Represents a error that occured in.
Definition networkrequesterror.h:24
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::_priority
NetworkRequest::Priority _priority
Definition request_p.h:120
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
ProtocolMode
Definition request_p.h:38
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::_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::FileMode
FileMode
Definition request.h:64
zyppng::NetworkRequest::WriteExclusive
@ WriteExclusive
Definition request.h:65
zyppng::NetworkRequest::Priority
Priority
Definition request.h:58
zyppng::NetworkRequest::Normal
@ Normal
Definition request.h:59
zyppng::Signal
Definition signals.h:151
zyppng::Timer
The Timer class provides repetitive and single-shot timers.
Definition timer.h:45
zyppng::Timer::create
static std::shared_ptr< Timer > create()
Creates a new Timer object, the timer is not started at this point.
Definition timer.cc:52
zyppng::Timer::Ptr
std::shared_ptr< Timer > Ptr
Definition timer.h:51
zypp::url
Url details namespace.
Definition UrlBase.cc:58
zypp::ResolverFocus::Default
@ Default
Request the standard behavior (as defined in zypp.conf or 'Job').
Definition ResolverFocus.h:25
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::TransferSettings
zypp::media::TransferSettings TransferSettings
Definition transfersettings.h:20
request.h
zypp::AutoFILE
AutoDispose<FILE*> calling fclose
Definition AutoDispose.h:321
zyppng::NetworkRequestPrivate::FileVerifyInfo
Definition request_p.h:113
zyppng::NetworkRequestPrivate::FileVerifyInfo::_fileDigest
zypp::Digest _fileDigest
Definition request_p.h:114
zyppng::NetworkRequestPrivate::FileVerifyInfo::_fileChecksum
zypp::CheckSum _fileChecksum
Definition request_p.h:115
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::_partialHelper
std::unique_ptr< CurlMultiPartHandler > _partialHelper
Definition request_p.h:148
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::_contentLenght
zypp::ByteCount _contentLenght
Definition request_p.h:179
zyppng::NetworkRequestPrivate::running_t::_cachedResult
std::optional< NetworkRequestError > _cachedResult
Definition request_p.h:173
zyppng::NetworkRequestPrivate::running_t::_isInCallback
bool _isInCallback
Definition request_p.h:169
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::_lastProgressNow
off_t _lastProgressNow
Definition request_p.h:175
zyppng::NetworkRequestPrivate::running_t::running_t
running_t(pending_t &&prevState)
Definition request.cc:63
zyppng::NetworkRequestPrivate::running_t::_currentFileOffset
off_t _currentFileOffset
Definition request_p.h:177
zyppng::NetworkRequestPrivate::running_t::_activityTimer
Timer::Ptr _activityTimer
Definition request_p.h:163
zyppng::NetworkRequestPrivate::running_t::_outFile
zypp::AutoFILE _outFile
Definition request_p.h:165
CheckSum.h
Digest.h
ZYPP_DECLARE_PUBLIC
#define ZYPP_DECLARE_PUBLIC(Class)
Definition zyppglobal.h:91