libzypp  17.31.31
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/zyppng/base/private/base_p.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  {
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
42 
44  virtual ~NetworkRequestPrivate();
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  void onActivityTimeout (Timer &);
90 
91  std::string errorMessage () const;
92 
93 
94  std::array<char, CURL_ERROR_SIZE+1> _errorBuf; //provide a buffer for a nicely formatted error for CURL
95 
96  template<typename T>
97  void setCurlOption ( CURLoption opt, T data )
98  {
99  auto ret = curl_easy_setopt( _easyHandle, opt, data );
100  if ( ret != 0 ) {
102  }
103  }
104 
105  Url _url; //file URL
108  NetworkRequest::Options _options;
109  zypp::ByteCount _expectedFileSize; // the file size as expected by the user code
110  std::vector<NetworkRequest::Range> _requestedRanges;
111 
112  struct FileVerifyInfo {
115  };
116  std::optional<FileVerifyInfo> _fileVerification;
117 
120 
121  std::string _lastRedirect;
122  const std::string _currentCookieFile = "/var/lib/YaST2/cookies";
123 
124  void *_easyHandle = nullptr; // the easy handle that controlling this request
125  NetworkRequestDispatcher *_dispatcher = nullptr; // the parent downloader owning this request
126 
127  //signals
128  Signal< void ( NetworkRequest &req )> _sigStarted;
129  Signal< void ( NetworkRequest &req, zypp::ByteCount count )> _sigBytesDownloaded;
130  Signal< void ( NetworkRequest &req, off_t dltotal, off_t dlnow, off_t ultotal, off_t ulnow )> _sigProgress;
131  Signal< void ( NetworkRequest &req, const NetworkRequestError &err )> _sigFinished;
132 
133  static int curlProgressCallback ( void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow );
134 
135  size_t headerfunction ( char *ptr, size_t bytes ) override;
136  size_t writefunction (char *ptr, std::optional<off_t> offset, size_t bytes ) override;
137 
138  std::unique_ptr< curl_slist, decltype (&curl_slist_free_all) > _headers;
139 
140  struct pending_t;
141  struct running_t;
143 
144  struct pending_t {
146  std::unique_ptr<CurlMultiPartHandler> _partialHelper = {};
147  };
148 
150  {
151  prepareNextRangeBatch_t( running_t &&prevState );
152  zypp::AutoFILE _outFile; //the file we are writing to
153  off_t _downloaded = 0; //downloaded bytes
154  std::unique_ptr<CurlMultiPartHandler> _partialHelper = {};
155  };
156 
157  struct running_t {
158  running_t( pending_t &&prevState );
159  running_t( prepareNextRangeBatch_t &&prevState );
160 
161  Timer::Ptr _activityTimer = Timer::create();
162 
164  std::unique_ptr<CurlMultiPartHandler> _partialHelper = {};
165 
166  // handle the case when cancel() is called from a slot to the progress signal
167  bool _isInCallback = false;
168 
169  // used to handle cancel() when emitting the progress signal, or when we explicitely trigger
170  // a error during callbacks.
171  std::optional<NetworkRequestError> _cachedResult;
172 
173  off_t _lastProgressNow = -1; // last value returned from CURL, lets only send signals if we get actual updates
174  off_t _downloaded = 0; //downloaded bytes
176 
177  zypp::ByteCount _contentLenght; // the content length as reported by the server
178  };
179 
180  struct finished_t {
181  off_t _downloaded = 0; //downloaded bytes
182  zypp::ByteCount _contentLenght = 0; // the content length as reported by the server
183  NetworkRequestError _result; // the overall result of the download
184  };
185 
186  std::variant< pending_t, running_t, prepareNextRangeBatch_t, finished_t > _runningMode = pending_t();
187  };
188 }
189 
190 #endif
Signal< void(NetworkRequest &req)> _sigStarted
Definition: request_p.h:128
std::string errorMessage() const
Definition: request.cc:564
NetworkRequest::Priority _priority
Definition: request_p.h:119
void setCurlOption(CURLoption opt, T data)
Definition: request_p.h:97
std::array< char, CURL_ERROR_SIZE+1 > _errorBuf
Definition: request_p.h:94
std::optional< FileVerifyInfo > _fileVerification
The digest for the full file.
Definition: request_p.h:116
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:428
Compute Message Digests (MD5, SHA1 etc)
Definition: Digest.h:37
NetworkRequest::FileMode _fMode
Definition: request_p.h:118
Store and operate with byte count.
Definition: ByteCount.h:30
const std::string _currentCookieFile
Definition: request_p.h:122
Holds transfer setting.
NetworkRequest::Options _options
Definition: request_p.h:108
running_t(pending_t &&prevState)
Definition: request.cc:63
Signal< void(NetworkRequest &req, zypp::ByteCount count)> _sigBytesDownloaded
Definition: request_p.h:129
enum zyppng::NetworkRequestPrivate::ProtocolMode _protocolMode
void onActivityTimeout(Timer &)
Definition: request.cc:555
zypp::Pathname _targetFile
Definition: request_p.h:106
virtual ~NetworkRequestPrivate()
Definition: request.cc:81
Signal< void(NetworkRequest &req, off_t dltotal, off_t dlnow, off_t ultotal, off_t ulnow)> _sigProgress
Definition: request_p.h:130
bool initialize(std::string &errBuf)
Definition: request.cc:91
The NetworkRequestError class Represents a error that occured in.
zypp::ByteCount _expectedFileSize
Definition: request_p.h:109
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:661
std::unique_ptr< curl_slist, decltype(&curl_slist_free_all) > _headers
Definition: request_p.h:138
static int curlProgressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
Definition: request.cc:578
std::unique_ptr< CurlMultiPartHandler > _partialHelper
Definition: request_p.h:154
std::string _lastRedirect
to log/report redirections
Definition: request_p.h:121
std::optional< NetworkRequestError > _cachedResult
Definition: request_p.h:171
std::unique_ptr< CurlMultiPartHandler > _partialHelper
Definition: request_p.h:146
TransferSettings _settings
Definition: request_p.h:107
NetworkRequestDispatcher * _dispatcher
Definition: request_p.h:125
std::vector< NetworkRequest::Range > _requestedRanges
the requested ranges that need to be downloaded
Definition: request_p.h:110
AutoDispose<FILE*> calling ::fclose
Definition: AutoDispose.h:312
Signal< void(NetworkRequest &req, const NetworkRequestError &err)> _sigFinished
Definition: request_p.h:131
size_t headerfunction(char *ptr, size_t bytes) override
Definition: request.cc:604
std::unique_ptr< CurlMultiPartHandler > _partialHelper
Definition: request_p.h:164
NetworkRequestPrivate(Url &&url, zypp::Pathname &&targetFile, NetworkRequest::FileMode fMode, NetworkRequest &p)
Definition: request.cc:73
void setResult(NetworkRequestError &&err)
Definition: request.cc:486
bool prepareToContinue(std::string &errBuf)
Definition: request.cc:411
std::variant< pending_t, running_t, prepareNextRangeBatch_t, finished_t > _runningMode
Definition: request_p.h:186