net-cpp ..
C++11 library for networking purposes
request.h
Go to the documentation of this file.
1/*
2 * Copyright © 2013 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by: Thomas Voß <thomas.voss@canonical.com>
17 * Gary Wang <gary.wang@canonical.com>
18 */
19#ifndef CORE_NET_HTTP_REQUEST_H_
20#define CORE_NET_HTTP_REQUEST_H_
21
22#include <core/net/visibility.h>
23
24#include <core/net/http/error.h>
26
27#include <chrono>
28#include <memory>
29
30namespace core
31{
32namespace net
33{
34namespace http
35{
36class Response;
37
42{
43public:
47 enum class State
48 {
49 ready,
50 active,
51 done
52 };
53
57 struct Errors
58 {
59 Errors() = delete;
60
65 {
71 };
72 };
73
77 struct Progress
78 {
82 enum class Next
83 {
84 continue_operation,
85 abort_operation
86 };
87
88 struct
89 {
90 double total{-1.};
91 double current{-1.};
92 } download{};
93
94 struct
95 {
96 double total{-1.};
97 double current{-1.};
98 } upload{};
99 };
100
104 typedef std::function<void(const core::net::Error&)> ErrorHandler;
105
109 typedef std::function<Progress::Next(const Progress&)> ProgressHandler;
110
114 typedef std::function<void(const Response&)> ResponseHandler;
115
120 {
121 public:
122 Handler() = default;
123
128
133
135 const ErrorHandler& on_error() const;
137 Handler& on_error(const ErrorHandler& handler);
138
139 private:
141 ProgressHandler progress_handler{};
142 ResponseHandler response_handler{};
143 ErrorHandler error_handler{};
145 };
146
151 {
152 std::string username;
153 std::string password;
154 };
155
157 typedef std::function<Credentials(const std::string&)> AuthenicationHandler;
158
163 {
169 inline static Configuration from_uri_as_string(const std::string& uri)
170 {
171 Configuration result;
172 result.uri = uri;
173
174 return result;
175 }
176
178 std::string uri;
179
182
185
188
191
193 struct
194 {
196 bool verify_peer
197 {
198 true
199 };
200
202 bool verify_host
203 {
204 true
205 };
206 } ssl;
207
209 struct
210 {
215 } authentication_handler;
216 };
217
218 Request(const Request&) = delete;
219 virtual ~Request() = default;
220
221 Request& operator=(const Request&) = delete;
222 bool operator==(const Request&) const = delete;
223
228 virtual State state() = 0;
229
234 virtual void set_timeout(const std::chrono::milliseconds& timeout) = 0;
235
242 virtual Response execute(const ProgressHandler& ph) = 0;
243
249 virtual void async_execute(const Handler& handler) = 0;
250
255 virtual std::string url_escape(const std::string& s) = 0;
256
261 virtual std::string url_unescape(const std::string& s) = 0;
262
263protected:
265 Request() = default;
267};
268}
269}
270}
271
272#endif // CORE_NET_HTTP_REQUEST_H_
The Header class encapsulates the headers of an HTTP request/response.
Definition: header.h:39
Encapsulates callbacks that can happen during request execution.
Definition: request.h:120
const ProgressHandler & on_progress() const
Returns the currently set progress handler.
Handler & on_response(const ResponseHandler &handler)
Adjusts the currently set response handler.
const ErrorHandler & on_error() const
Returns the currently set error handler.
Handler & on_error(const ErrorHandler &handler)
Adjusts the currently set error handler.
const ResponseHandler & on_response() const
Returns the currently set response handler.
Handler & on_progress(const ProgressHandler &handler)
Adjusts the currently set progress handler.
The Request class encapsulates a request for a web resource.
Definition: request.h:42
virtual ~Request()=default
virtual Response execute(const ProgressHandler &ph)=0
Synchronously executes the request.
virtual std::string url_escape(const std::string &s)=0
Returns the input string in URL-escaped format.
virtual State state()=0
state queries the current state of the operation.
Request(const Request &)=delete
std::function< Credentials(const std::string &)> AuthenicationHandler
Definition: request.h:157
virtual std::string url_unescape(const std::string &s)=0
Returns the input string in URL-unescaped format.
std::function< void(const Response &)> ResponseHandler
ResponseHandler is invoked when a request completes.
Definition: request.h:114
State
The State enum describes the different states a request can be in.
Definition: request.h:48
virtual void async_execute(const Handler &handler)=0
Asynchronously executes the request, reporting errors, progress and completion to the given handlers.
bool operator==(const Request &) const =delete
Request & operator=(const Request &)=delete
std::function< Progress::Next(const Progress &)> ProgressHandler
ProgressHandler is invoked for progress updates while executing the request.
Definition: request.h:109
virtual void set_timeout(const std::chrono::milliseconds &timeout)=0
Adjusts the timeout of a State::ready request.
std::function< void(const core::net::Error &)> ErrorHandler
ErrorHandler is invoked in case of errors arising while executing the request.
Definition: request.h:104
Definition: location.h:24
The Configuration struct encapsulates all options for creating requests.
Definition: request.h:163
static Configuration from_uri_as_string(const std::string &uri)
from_uri_as_string creates a new instance of Configuration for a url.
Definition: request.h:169
AuthenicationHandler for_proxy
Definition: request.h:214
The Credentials struct encapsulates username and password for basic & digest authentication.
Definition: request.h:151
AlreadyActive is thrown when *execute is called on an active request.
Definition: request.h:65
AlreadyActive(const core::Location &loc)
AlreadyActive creates a new instance with a location hint.
The Errors struct collects the Request-specific exceptions and error modes.
Definition: request.h:58
The Progress struct encapsulates progress information for web-resource requests.
Definition: request.h:78
Next
The Next enum summarizes the available return-types for the progress callback.
Definition: request.h:83
The Response struct models a response to a core::net::http::Request.
Definition: response.h:40
#define CORE_NET_DLL_PUBLIC
Definition: visibility.h:25