libzypp  17.35.16
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>
20 #include <zypp-media/MediaException>
21 #include <zypp-core/zyppng/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 
32 namespace zyppng {
33 
35  {
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
42 
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 ) {
103  }
104  }
105 
106  Url _url; //file URL
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 {
116  };
117  std::optional<FileVerifyInfo> _fileVerification;
118 
121 
122  std::string _lastRedirect;
123  const std::string _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
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;
145 
146  struct pending_t {
148  std::unique_ptr<CurlMultiPartHandler> _partialHelper = {};
149  };
150 
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 
164 
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
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
Signal< void(NetworkRequest &req)> _sigStarted
Definition: request_p.h:129
std::string errorMessage() const
Definition: request.cc:565
NetworkRequest::Priority _priority
Definition: request_p.h:120
void setCurlOption(CURLoption opt, T data)
Definition: request_p.h:98
std::array< char, CURL_ERROR_SIZE+1 > _errorBuf
Definition: request_p.h:95
std::optional< FileVerifyInfo > _fileVerification
The digest for the full file.
Definition: request_p.h:117
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:424
Compute Message Digests (MD5, SHA1 etc)
Definition: Digest.h:37
NetworkRequest::FileMode _fMode
Definition: request_p.h:119
Store and operate with byte count.
Definition: ByteCount.h:31
const std::string _currentCookieFile
Definition: request_p.h:123
Holds transfer setting.
NetworkRequest::Options _options
Definition: request_p.h:109
running_t(pending_t &&prevState)
Definition: request.cc:63
Signal< void(NetworkRequest &req, zypp::ByteCount count)> _sigBytesDownloaded
Definition: request_p.h:130
~NetworkRequestPrivate() override
Definition: request.cc:81
enum zyppng::NetworkRequestPrivate::ProtocolMode _protocolMode
void onActivityTimeout(Timer &)
Definition: request.cc:556
zypp::Pathname _targetFile
Definition: request_p.h:107
Signal< void(NetworkRequest &req, off_t dltotal, off_t dlnow, off_t ultotal, off_t ulnow)> _sigProgress
Definition: request_p.h:131
void notifyErrorCodeChanged() override
Definition: request.cc:723
bool initialize(std::string &errBuf)
Definition: request.cc:91
The NetworkRequestError class Represents a error that occured in.
The Timer class provides repetitive and single-shot timers.
Definition: timer.h:44
zypp::ByteCount _expectedFileSize
Definition: request_p.h:110
bool setupHandle(std::string &errBuf)
Definition: request.cc:103
size_t writefunction(char *ptr, std::optional< off_t > offset, size_t bytes) override
Definition: request.cc:662
std::unique_ptr< curl_slist, decltype(&curl_slist_free_all) > _headers
Definition: request_p.h:140
std::shared_ptr< Timer > Ptr
Definition: timer.h:51
static std::shared_ptr< Timer > create()
Creates a new Timer object, the timer is not started at this point.
Definition: timer.cc:52
static int curlProgressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
Definition: request.cc:579
std::unique_ptr< CurlMultiPartHandler > _partialHelper
Definition: request_p.h:156
std::string _lastRedirect
to log/report redirections
Definition: request_p.h:122
std::optional< NetworkRequestError > _cachedResult
Definition: request_p.h:173
std::unique_ptr< CurlMultiPartHandler > _partialHelper
Definition: request_p.h:148
TransferSettings _settings
Definition: request_p.h:108
NetworkRequestDispatcher * _dispatcher
Definition: request_p.h:126
std::vector< NetworkRequest::Range > _requestedRanges
the requested ranges that need to be downloaded
Definition: request_p.h:111
AutoDispose<FILE*> calling ::fclose
Definition: AutoDispose.h:320
Signal< void(NetworkRequest &req, const NetworkRequestError &err)> _sigFinished
Definition: request_p.h:132
size_t headerfunction(char *ptr, size_t bytes) override
Definition: request.cc:605
#define ZYPP_DECLARE_PUBLIC(Class)
Definition: zyppglobal.h:98
std::unique_ptr< CurlMultiPartHandler > _partialHelper
Definition: request_p.h:166
NetworkRequestPrivate(Url &&url, zypp::Pathname &&targetFile, NetworkRequest::FileMode fMode, NetworkRequest &p)
Definition: request.cc:73
void setResult(NetworkRequestError &&err)
Definition: request.cc:487
bool prepareToContinue(std::string &errBuf)
Definition: request.cc:411
Url manipulation class.
Definition: Url.h:92
std::variant< pending_t, running_t, prepareNextRangeBatch_t, finished_t > _runningMode
Definition: request_p.h:188