1 #ifndef ZYPP_NG_MEDIA_CURL_REQUEST_H_INCLUDED
2 #define ZYPP_NG_MEDIA_CURL_REQUEST_H_INCLUDED
4 #include <zypp/zyppng/media/network/networkrequesterror.h>
5 #include <zypp/zyppng/media/network/TransferSettings>
6 #include <zypp/zyppng/base/Base>
7 #include <zypp/zyppng/core/Url>
8 #include <zypp/zyppng/base/zyppglobal.h>
9 #include <zypp/zyppng/base/signals.h>
10 #include <zypp/base/Flags.h>
23 using AuthData = zypp::media::AuthData;
25 class NetworkRequestDispatcher;
26 class NetworkRequestPrivate;
29 * Represents a (http/https/ftp) request. This is the low level API for the
30 * \sa zyppng::Downloader , usually it makes more sense to use the Downloader for the
31 * more features it supports.
32 * After creating a NetworkRequest and changing the required settings is enqueued
33 * to the \sa zyppng::NetworkRequestDispatcher.
35 class LIBZYPP_NG_EXPORT NetworkRequest : public Base
39 using Ptr = std::shared_ptr<NetworkRequest>;
40 using WeakPtr = std::weak_ptr<NetworkRequest>;
43 Pending, //< waiting to be dispatched
44 Running, //< currently running
45 Finished, //< finished successfully
46 Error, //< Error, use error function to figure out the issue
50 Normal, //< Requests with normal priority will be enqueued at the end
51 High //< Request with high priority will be moved to the front of the queue
55 WriteExclusive, //< the request will create its own file, overwriting anything that already exists
56 WriteShared //< the request will create or open the file in shared mode and only write between \a start and \a len
60 HeadRequest = 0x01 //< only request the header part of the file
62 ZYPP_DECLARE_FLAGS(Options, OptionBits);
65 * \param url The source URL of the download
66 * \param targetFile The path where the file should be stored
67 * \param start File offset, if set this will create a range download request
68 * \param len File range length
69 * \param fMode The mode in which the file is opened in.
71 NetworkRequest( Url url, zypp::Pathname targetFile, off_t start = -1, off_t len = 0, FileMode fMode = WriteExclusive );
72 virtual ~NetworkRequest();
75 * Sets the priority of the NetworkRequest, this will affect where
76 * the \sa NetworkRequestDispatcher puts the Request in the Queue.
77 * \note changing this makes only sense before enqueueing the request
79 void setPriority ( Priority prio );
82 * Returns the requested priority of the NetworkRequest
84 Priority priority ( ) const;
87 * Change request options, currently only the \sa OptionBits::HeadRequest option is supported
89 * \note changing this makes only sense before the request was started
91 void setOptions ( Options opt );
94 * Returns the currently set options
96 Options options () const;
99 * Sets the range description
100 * \note This will not change a running download
104 void setRequestRange ( off_t start = -1, off_t len = 0 );
107 * Returns the last redirect information from the headers.
109 const std::string &lastRedirectInfo() const;
112 * Returns a pointer to the native CURL easy handle
114 * \note consider adding the functionality here instead of using the Handle directly.
115 * In case we ever decide to switch out the CURL backend your code will break
117 void *nativeHandle () const;
120 * Will return the data at \a offset with length \a count.
121 * If there is not yet enough data a empty vector will be returned
123 std::vector<char> peekData ( off_t offset, size_t count ) const;
126 * Returns the request URL
131 * This will change the URL of the request.
132 * \note calling this on a currently running request has no impact
134 void setUrl ( const Url & url );
137 * Returns the target filename path
139 const zypp::Pathname & targetFilePath () const;
142 * Returns the content type as reported from the server
143 * \note can only return a valid value if the download has started already
145 std::string contentType () const;
148 * Returns the requested start offset
150 off_t downloadOffset () const;
153 * Returns the number of bytes that are reported from the backend as the full download size, those can
154 * be 0 even when the download is already running.
156 off_t reportedByteCount () const;
159 * Returns the expected byte count that was passed to the constructor, zero if
162 off_t expectedByteCount () const;
165 * Returns the number of already downloaded bytes as reported by the backend
167 off_t downloadedByteCount () const;
170 * Set a \sa zypp::Digest that is updated when data is written, can be used to
171 * validate the file contents with a checksum
173 void setDigest ( std::shared_ptr<zypp::Digest> dig );
176 * Returns the currently used \sa zypp::Digest, null if non was set.
178 std::shared_ptr<zypp::Digest> digest () const;
181 * Enables automated checking of downloaded contents against a checksum.
182 * Only makes a difference if a \sa zypp::Digest was with with \sa setDigest.
184 * \note expects checksum in byte NOT in string format
186 void setExpectedChecksum (std::vector<unsigned char> checksum );
190 * Returns a writeable reference to the internal \sa zyppng::TransferSettings.
191 * \note calling this on a already running request has no impact
193 TransferSettings &transferSettings ();
196 * Returns the current state the \a HttpDownloadRequest is in
198 State state () const;
201 * Returns the last set Error
203 const NetworkRequestError &error () const;
206 * In some cases curl can provide extended error informations collected at
207 * runtime. In those cases its possible to query that information.
209 std::string extendedErrorString() const;
212 * Checks if there was a error with the request
214 bool hasError () const;
217 * Adds a raw header to the request data. Use this to send custom headers
220 bool addRequestHeader(const std::string &header );
223 * Signals that the dispatcher dequeued the request and actually starts downloading data
225 SignalProxy<void ( NetworkRequest &req )> sigStarted ();
228 * Signals if there was data read from the download
230 SignalProxy<void ( NetworkRequest &req, off_t dltotal, off_t dlnow, off_t ultotal, off_t ulnow )> sigProgress ();
233 * Signals that the download finished.
235 * \note After this signal was emitted the Curl handle is reset,
236 * so all queries to it need to be done in a Slot connected to this signal
238 SignalProxy<void ( NetworkRequest &req, const NetworkRequestError &err)> sigFinished ( );
241 friend class NetworkRequestDispatcher;
242 friend class NetworkRequestDispatcherPrivate;
243 ZYPP_DECLARE_PRIVATE( NetworkRequest )
247 ZYPP_DECLARE_OPERATORS_FOR_FLAGS(zyppng::NetworkRequest::Options);