libzypp 17.31.23
request.h
Go to the documentation of this file.
1#ifndef ZYPP_NG_MEDIA_CURL_REQUEST_H_INCLUDED
2#define ZYPP_NG_MEDIA_CURL_REQUEST_H_INCLUDED
3
5#include <zypp-curl/ng/network/TransferSettings>
6#include <zypp-core/zyppng/base/Base>
7#include <zypp-core/zyppng/core/Url>
8#include <zypp-core/zyppng/core/ByteArray>
9#include <zypp-core/zyppng/base/zyppglobal.h>
10#include <zypp-core/zyppng/base/signals.h>
11#include <zypp-core/base/Flags.h>
12#include <zypp-core/ByteCount.h>
13#include <optional>
14#include <vector>
15#include <chrono>
16#include <any>
17
18namespace zypp {
19 class Digest;
20
21 namespace media {
22 class AuthData;
23 }
24}
25
26namespace zyppng {
27
29
30 class NetworkRequestDispatcher;
31 class NetworkRequestPrivate;
32
40 class LIBZYPP_NG_EXPORT NetworkRequest : public Base
41 {
42 public:
43
44 using Ptr = std::shared_ptr<NetworkRequest>;
45 using WeakPtr = std::weak_ptr<NetworkRequest>;
46 using DigestPtr = std::shared_ptr<zypp::Digest>;
47 using CheckSumBytes = UByteArray;
48
49 enum State {
50 Pending, //< waiting to be dispatched
51 Running, //< currently running
52 Finished, //< finished successfully
53 Error, //< Error, use error function to figure out the issue
54 };
55
56 enum Priority {
57 Normal, //< Requests with normal priority will be enqueued as they come in
58 High, //< Request with high priority will be moved to the front of the queue
59 Critical = 100, //< Those requests will be enqueued as fast as possible, even before High priority requests, this should be used only if requests needs to start immediately
60 };
61
62 enum FileMode {
63 WriteExclusive, //< the request will create its own file, overwriting anything that already exists
64 WriteShared //< the request will create or open the file in shared mode and only write between \a start and \a len
65 };
66
68 Default = 0x00, //< no special options, just do a normal download
69 HeadRequest = 0x01, //< only request the header part of the file
70 ConnectionTest = 0x02 //< only connect to collect connection speed information
71 };
73
74 struct Range {
75 size_t start = 0;
76 size_t len = 0;
77 size_t bytesWritten = 0;
78 DigestPtr _digest; //< zypp::Digest that is updated when data is written, can be used to validate the file contents with a checksum
79
87 std::optional<size_t> _relevantDigestLen; //< If this is initialized , it defines how many bytes of the resulting checkum are compared
88 std::optional<size_t> _chksumPad; //< If initialized we need to pad the digest with zeros before calculating the final checksum
89 std::any userData; //< Custom data the user can associate with the Range
90
91 State _rangeState = State::Pending; //< Flag to know if this range has been already requested and if the request was successful
92
93 static Range make ( size_t start, size_t len = 0, DigestPtr &&digest = nullptr, CheckSumBytes &&expectedChkSum = CheckSumBytes(), std::any &&userData = std::any(), std::optional<size_t> digestCompareLen = {}, std::optional<size_t> _dataBlockPadding = {} );
94 };
95
96 struct Timings {
97 std::chrono::microseconds namelookup;
98 std::chrono::microseconds connect;
99 std::chrono::microseconds appconnect;
100 std::chrono::microseconds pretransfer;
101 std::chrono::microseconds total;
102 std::chrono::microseconds redirect;
103 };
104
110 NetworkRequest(Url url, zypp::Pathname targetFile, FileMode fMode = WriteExclusive );
111 virtual ~NetworkRequest();
112
118 void setExpectedFileSize ( zypp::ByteCount expectedFileSize );
119
125 void setPriority ( Priority prio, bool triggerReschedule = true );
126
130 Priority priority ( ) const;
131
137 void setOptions ( Options opt );
138
142 Options options () const;
143
148 void addRequestRange ( size_t start, size_t len = 0, DigestPtr digest = nullptr, CheckSumBytes expectedChkSum = CheckSumBytes(), std::any userData = std::any(), std::optional<size_t> digestCompareLen = {}, std::optional<size_t> chksumpad = {} );
149
150 void addRequestRange ( const Range &range );
151
156 void resetRequestRanges ( );
157
158 std::vector<Range> failedRanges () const;
159 const std::vector<Range> &requestedRanges () const;
160
164 const std::string &lastRedirectInfo() const;
165
172 void *nativeHandle () const;
173
178 std::optional<Timings> timings () const;
179
184 std::vector<char> peekData ( off_t offset, size_t count ) const;
185
189 Url url () const;
190
195 void setUrl ( const Url & url );
196
200 const zypp::Pathname & targetFilePath () const;
201
206 void setTargetFilePath ( const zypp::Pathname &path );
207
211 FileMode fileOpenMode () const;
212
217 void setFileOpenMode ( FileMode mode );
218
223 std::string contentType () const;
224
231 zypp::ByteCount reportedByteCount() const;
232
236 zypp::ByteCount downloadedByteCount() const;
237
242 TransferSettings &transferSettings ();
243
247 State state () const;
248
252 NetworkRequestError error () const;
253
258 std::string extendedErrorString() const;
259
263 bool hasError () const;
264
269 bool addRequestHeader(const std::string &header );
270
274 SignalProxy<void ( NetworkRequest &req )> sigStarted ();
275
279 SignalProxy<void ( NetworkRequest &req, zypp::ByteCount count )> sigBytesDownloaded ();
280
286 SignalProxy<void ( NetworkRequest &req, off_t dltotal, off_t dlnow, off_t ultotal, off_t ulnow )> sigProgress ();
287
294 SignalProxy<void ( NetworkRequest &req, const NetworkRequestError &err)> sigFinished ( );
295
296 private:
297 friend class NetworkRequestDispatcher;
299 ZYPP_DECLARE_PRIVATE( NetworkRequest )
300 };
301
302}
303ZYPP_DECLARE_OPERATORS_FOR_FLAGS(zyppng::NetworkRequest::Options);
304
305#endif
Store and operate with byte count.
Definition: ByteCount.h:31
Class for handling media authentication data.
Definition: authdata.h:29
std::shared_ptr< zypp::Digest > DigestPtr
Definition: request.h:46
std::shared_ptr< NetworkRequest > Ptr
Definition: request.h:44
UByteArray CheckSumBytes
Definition: request.h:47
ZYPP_DECLARE_FLAGS(Options, OptionBits)
std::weak_ptr< NetworkRequest > WeakPtr
Definition: request.h:45
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
zypp::media::AuthData AuthData
Definition: authdata.h:21
CheckSumBytes _checksum
Enables automated checking of downloaded contents against a checksum.
Definition: request.h:86
std::optional< size_t > _relevantDigestLen
Definition: request.h:87
std::optional< size_t > _chksumPad
Definition: request.h:88
std::chrono::microseconds appconnect
Definition: request.h:99
std::chrono::microseconds redirect
Definition: request.h:102
std::chrono::microseconds pretransfer
Definition: request.h:100
std::chrono::microseconds total
Definition: request.h:101
std::chrono::microseconds namelookup
Definition: request.h:97
std::chrono::microseconds connect
Definition: request.h:98
#define ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Name)
Definition: Flags.h:177