libzypp  17.31.31
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>
7 #include <zypp-core/zyppng/base/Base>
8 #include <zypp-core/zyppng/core/Url>
9 #include <zypp-core/zyppng/core/ByteArray>
10 #include <zypp-core/zyppng/base/zyppglobal.h>
11 #include <zypp-core/zyppng/base/signals.h>
12 #include <zypp-core/base/Flags.h>
13 #include <zypp-core/ByteCount.h>
14 #include <optional>
15 #include <vector>
16 #include <chrono>
17 #include <any>
18 
19 namespace zypp {
20  class Digest;
21  class CheckSum;
22 
23  namespace media {
24  class AuthData;
25  }
26 }
27 
28 namespace zyppng {
29 
31 
32  class NetworkRequestDispatcher;
33  class NetworkRequestPrivate;
34 
42  class LIBZYPP_NG_EXPORT NetworkRequest : public Base
43  {
44  public:
45 
46  using Ptr = std::shared_ptr<NetworkRequest>;
47  using WeakPtr = std::weak_ptr<NetworkRequest>;
48  using DigestPtr = std::shared_ptr<zypp::Digest>;
49  using CheckSumBytes = UByteArray;
50 
51  enum State {
52  Pending, //< waiting to be dispatched
53  Running, //< currently running
54  Finished, //< finished successfully
55  Error, //< Error, use error function to figure out the issue
56  };
57 
58  enum Priority {
59  Normal, //< Requests with normal priority will be enqueued as they come in
60  High, //< Request with high priority will be moved to the front of the queue
61  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
62  };
63 
64  enum FileMode {
65  WriteExclusive, //< the request will create its own file, overwriting anything that already exists
66  WriteShared //< the request will create or open the file in shared mode and only write between \a start and \a len
67  };
68 
69  enum OptionBits {
70  Default = 0x00, //< no special options, just do a normal download
71  HeadRequest = 0x01, //< only request the header part of the file
72  ConnectionTest = 0x02 //< only connect to collect connection speed information
73  };
74  ZYPP_DECLARE_FLAGS(Options, OptionBits);
75 
77 
78  struct Timings {
79  std::chrono::microseconds namelookup;
80  std::chrono::microseconds connect;
81  std::chrono::microseconds appconnect;
82  std::chrono::microseconds pretransfer;
83  std::chrono::microseconds total;
84  std::chrono::microseconds redirect;
85  };
86 
92  NetworkRequest(Url url, zypp::Pathname targetFile, FileMode fMode = WriteExclusive );
93  virtual ~NetworkRequest();
94 
100  void setExpectedFileSize ( zypp::ByteCount expectedFileSize );
101 
107  void setPriority ( Priority prio, bool triggerReschedule = true );
108 
112  Priority priority ( ) const;
113 
119  void setOptions ( Options opt );
120 
124  Options options () const;
125 
130  void addRequestRange ( 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> chksumpad = {} );
131 
132  void addRequestRange ( Range &&range );
133 
138  bool setExpectedFileChecksum( const zypp::CheckSum &expected );
139 
144  void resetRequestRanges ( );
145 
146  std::vector<Range> failedRanges () const;
147  const std::vector<Range> &requestedRanges () const;
148 
152  const std::string &lastRedirectInfo() const;
153 
160  void *nativeHandle () const;
161 
166  std::optional<Timings> timings () const;
167 
172  std::vector<char> peekData ( off_t offset, size_t count ) const;
173 
177  Url url () const;
178 
183  void setUrl ( const Url & url );
184 
188  const zypp::Pathname & targetFilePath () const;
189 
194  void setTargetFilePath ( const zypp::Pathname &path );
195 
199  FileMode fileOpenMode () const;
200 
205  void setFileOpenMode ( FileMode mode );
206 
211  std::string contentType () const;
212 
219  zypp::ByteCount reportedByteCount() const;
220 
224  zypp::ByteCount downloadedByteCount() const;
225 
230  TransferSettings &transferSettings ();
231 
235  State state () const;
236 
240  NetworkRequestError error () const;
241 
246  std::string extendedErrorString() const;
247 
251  bool hasError () const;
252 
257  bool addRequestHeader(const std::string &header );
258 
262  SignalProxy<void ( NetworkRequest &req )> sigStarted ();
263 
267  SignalProxy<void ( NetworkRequest &req, zypp::ByteCount count )> sigBytesDownloaded ();
268 
274  SignalProxy<void ( NetworkRequest &req, off_t dltotal, off_t dlnow, off_t ultotal, off_t ulnow )> sigProgress ();
275 
282  SignalProxy<void ( NetworkRequest &req, const NetworkRequestError &err)> sigFinished ( );
283 
284  private:
285  friend class NetworkRequestDispatcher;
287  ZYPP_DECLARE_PRIVATE( NetworkRequest )
288  };
289 
290 }
291 ZYPP_DECLARE_OPERATORS_FOR_FLAGS(zyppng::NetworkRequest::Options);
292 
293 #endif
constexpr std::string_view Url("url")
std::chrono::microseconds connect
Definition: request.h:80
Store and operate with byte count.
Definition: ByteCount.h:30
std::chrono::microseconds pretransfer
Definition: request.h:82
std::chrono::microseconds appconnect
Definition: request.h:81
std::chrono::microseconds namelookup
Definition: request.h:79
std::shared_ptr< NetworkRequest > Ptr
Definition: request.h:46
zypp::media::AuthData AuthData
Definition: authdata.h:21
UByteArray CheckSumBytes
Definition: request.h:49
std::weak_ptr< NetworkRequest > WeakPtr
Definition: request.h:47
ZYPP_DECLARE_OPERATORS_FOR_FLAGS(zyppng::NetworkRequest::Options)
#define ZYPP_DECLARE_FLAGS(Name, Enum)
Definition: Flags.h:174
std::chrono::microseconds total
Definition: request.h:83
std::chrono::microseconds redirect
Definition: request.h:84
std::shared_ptr< zypp::Digest > DigestPtr
Definition: request.h:48
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
zypp::media::TransferSettings TransferSettings
Definition: downloader.h:25