libzypp 17.37.17
curlmultiparthandler.h
Go to the documentation of this file.
1#ifndef ZYPPNG_CURLMULTIPARTHANDLER_H
2#define ZYPPNG_CURLMULTIPARTHANDLER_H
3
4#include <zypp-core/zyppng/base/Base>
5#include <zypp-core/zyppng/core/ByteArray>
6#include <zypp-core/Digest.h>
7#include <zypp-core/zyppng/pipelines/Expected>
8#include <zypp-curl/ng/network/NetworkRequestError>
10
11#include <optional>
12#include <any>
13
14namespace zyppng {
15
20 public:
21 virtual ~CurlMultiPartDataReceiver() = default;
22
26 virtual size_t headerfunction ( char *ptr, size_t bytes ) = 0;
27
33 virtual size_t writefunction ( char *ptr, std::optional<off_t> offset, size_t bytes ) = 0;
34
42 virtual bool beginRange ( off_t range, std::string &cancelReason ) { return true; };
43
54 virtual bool finishedRange ( off_t range, bool validated, std::string &cancelReason ) { return true; };
55
59 virtual void notifyErrorCodeChanged () {};
60 };
61
70 {
71 public:
72
73 enum class ProtocolMode{
74 Basic, //< use this mode if no special checks are required in header or write callbacks
75 HTTP //< this mode is used for HTTP and HTTPS downloads
76 };
77
78 // when requesting ranges from the server, we need to make sure not to request
79 // too many at the same time. Instead we batch our requests and reuse the open
80 // connection until we have the full file.
81 // However different server have different maximum nr of ranges, so we start with
82 // a high number and decrease until we find a rangecount that works
83 constexpr static unsigned _rangeAttempt[] = {
84 255,
85 127,
86 63,
87 15,
88 5,
89 1
90 };
91
92 constexpr static unsigned _rangeAttemptSize = ( sizeof( _rangeAttempt ) / sizeof(unsigned) );
93
94 enum State {
95 Pending, //< waiting to be dispatched
96 Running, //< currently running
97 Finished, //< finished successfully
98 Error, //< Error, use error function to figure out the issue
99 };
100
103
104 struct Range : public RangeDesc {
105
106 Range( ) = default;
107 Range( RangeDesc &&rd, std::optional<zypp::Digest> dig, std::any userD, State rangeState = State::Pending );
108
109 size_t bytesWritten = 0;
110
114 std::optional<zypp::Digest> _digest = {}; //< zypp::Digest that is updated when data is written, can be used to validate the file contents with a checksum
115
116 std::any userData; //< Custom data the user can associate with the Range
117
118 State _rangeState = State::Pending; //< Flag to know if this range has been already requested and if the request was successful
119
120 void restart();
121 Range clone() const;
122
123 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 = {} );
124 };
125
126 CurlMultiPartHandler( ProtocolMode mode, void *easyHandle, std::vector<Range> &ranges, CurlMultiPartDataReceiver &receiver );
127 ~CurlMultiPartHandler() override;
128
129 void *easyHandle() const;
130 bool canRecover() const;
131 bool hasMoreWork() const;
132
133 bool hasError() const;
134
135 Code lastError() const;
136 const std::string &lastErrorMessage() const;
137
138 bool validateRange(Range &rng);
139
140 bool prepare( );
141 bool prepareToContinue( );
142 void finalize( );
143
144 bool verifyData( );
145
146 std::optional<size_t> reportedFileSize() const;
147 std::optional<off_t> currentRange() const;
148
149 private:
150
151 void setCode ( Code c, std::string msg, bool force = false );
152
153 static size_t curl_hdrcallback ( char *ptr, size_t size, size_t nmemb, void *userdata );
154 static size_t curl_wrtcallback ( char *ptr, size_t size, size_t nmemb, void *userdata );
155
156 size_t hdrcallback ( char *ptr, size_t size, size_t nmemb );
157 size_t wrtcallback ( char *ptr, size_t size, size_t nmemb );
158 bool parseContentRangeHeader(const std::string_view &line, size_t &start, size_t &len, size_t &fileLen);
159 bool parseContentTypeMultiRangeHeader(const std::string_view &line, std::string &boundary);
160 bool checkIfRangeChkSumIsValid( Range &rng );
161 void setRangeState ( Range &rng, State state );
162
164 void *_easyHandle = nullptr;
166
167 Code _lastCode = Code::NoError;
168 std::string _lastErrorMsg;
169
170 bool _allHeadersReceived = false; //< set to true once writefunc was called once e.g. all headers have been received
171 bool _gotContentRangeInfo = false; //< Set to true if the server indicates ranges
172 bool _isMuliPartResponse = false; //< Set to true if the respone is in multipart form
173
174 std::string _seperatorString;
175 std::vector<char> _rangePrefaceBuffer;
176
177 std::optional<off_t> _currentRange;
178 std::optional<Range> _currentSrvRange;
179 std::optional<size_t> _reportedFileSize;
180
181 unsigned _rangeAttemptIdx = 0;
182 std::vector<Range> &_requestedRanges;
183 };
184
185} // namespace zyppng
186
187#endif // ZYPPNG_CURLMULTIPARTHANDLER_H
virtual size_t headerfunction(char *ptr, size_t bytes)=0
virtual ~CurlMultiPartDataReceiver()=default
virtual size_t writefunction(char *ptr, std::optional< off_t > offset, size_t bytes)=0
virtual bool finishedRange(off_t range, bool validated, std::string &cancelReason)
virtual bool beginRange(off_t range, std::string &cancelReason)
size_t wrtcallback(char *ptr, size_t size, size_t nmemb)
static constexpr unsigned _rangeAttemptSize
CurlMultiPartHandler(ProtocolMode mode, void *easyHandle, std::vector< Range > &ranges, CurlMultiPartDataReceiver &receiver)
std::optional< Range > _currentSrvRange
std::string _seperatorString
The seperator string for multipart responses as defined in RFC 7233 Section 4.1.
NetworkRequestError::Type Code
static size_t curl_wrtcallback(char *ptr, size_t size, size_t nmemb, void *userdata)
static size_t curl_hdrcallback(char *ptr, size_t size, size_t nmemb, void *userdata)
const std::string & lastErrorMessage() const
static constexpr unsigned _rangeAttempt[]
bool parseContentRangeHeader(const std::string_view &line, size_t &start, size_t &len, size_t &fileLen)
void setCode(Code c, std::string msg, bool force=false)
std::vector< char > _rangePrefaceBuffer
Here we buffer.
std::optional< off_t > currentRange() const
std::optional< size_t > _reportedFileSize
Filesize as reported by the content range or byte range headers.
size_t hdrcallback(char *ptr, size_t size, size_t nmemb)
CurlMultiPartDataReceiver & _receiver
void setRangeState(Range &rng, State state)
std::optional< size_t > reportedFileSize() const
bool parseContentTypeMultiRangeHeader(const std::string_view &line, std::string &boundary)
std::vector< Range > & _requestedRanges
the requested ranges that need to be downloaded
std::optional< off_t > _currentRange
std::optional< zypp::Digest > _digest
Enables automated checking of downloaded contents against a checksum.
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={})