net-cpp: /home/odroid/lomiri/net-cpp-git/src/net-cpp/include/core/net/http/request.h Source File

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>
25 #include <core/net/http/header.h>
26
27 #include <chrono>
28 #include <memory>
29
30 namespace core
31 {
32 namespace net
33 {
34 namespace http
35 {
36 class Response;
37
41 class CORE_NET_DLL_PUBLIC Request
42 {
43 public:
47 enum class State
48 {
49 ready,
50 active,
51 done
52 };
53
57 struct Errors
58 {
59 Errors() = delete;
60
64 struct AlreadyActive : public core::net::http::Error
65 {
70 AlreadyActive(const core::Location& loc);
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
119 class Handler
120 {
121 public:
122 Handler() = default;
123
125 const ProgressHandler& on_progress() const;
127 Handler& on_progress(const ProgressHandler& handler);
128
130 const ResponseHandler& on_response() const;
132 Handler& on_response(const ResponseHandler& handler);
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
150 struct Credentials
151 {
152 std::string username;
153 std::string password;
154 };
155
157 typedef std::function<Credentials(const std::string&)> AuthenicationHandler;
158
162 struct Configuration
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
181 Header header;
182
184 ProgressHandler on_progress;
185
187 ResponseHandler on_response;
188
190 ErrorHandler on_error;
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 {
212 AuthenicationHandler for_http;
214 AuthenicationHandler for_proxy;
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
263 protected:
265 Request() = default;
267 };
268 }
269 }
270 }
271
272 #endif // CORE_NET_HTTP_REQUEST_H_
core::net::http::Request::~Request
virtual ~Request()=default
core::net::http::Request::Errors
The Errors struct collects the Request-specific exceptions and error modes.
Definition: request.h:58
core::net::http::Request::Handler::on_response
const ResponseHandler & on_response() const
Returns the currently set response handler.
core::net::http::Request::Configuration::from_uri_as_string
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
core::net::http::Request::ErrorHandler
std::function< void(const core::net::Error &)> ErrorHandler
ErrorHandler is invoked in case of errors arising while executing the request.
Definition: request.h:104
core::net::http::Request::url_escape
virtual std::string url_escape(const std::string &s)=0
Returns the input string in URL-escaped format.
core::Location
Definition: location.h:26
visibility.h
core::net::http::Request::url_unescape
virtual std::string url_unescape(const std::string &s)=0
Returns the input string in URL-unescaped format.
core::net::http::Request::operator==
bool operator==(const Request &) const =delete
core::net::http::Error
Definition: error.h:30
core::net::http::Request::Handler::on_progress
const ProgressHandler & on_progress() const
Returns the currently set progress handler.
core::net::http::Header
The Header class encapsulates the headers of an HTTP request/response.
Definition: header.h:39
core::net::http::Request::async_execute
virtual void async_execute(const Handler &handler)=0
Asynchronously executes the request, reporting errors, progress and completion to the given handlers.
core::net::http::Request::Errors::Errors
Errors()=delete
core::net::http::Request::Configuration
The Configuration struct encapsulates all options for creating requests.
Definition: request.h:163
core::net::http::Request::Request
Request(const Request &)=delete
core::net::http::Response
The Response struct models a response to a core::net::http::Request.
Definition: response.h:40
core::net::http::Request::Handler
Encapsulates callbacks that can happen during request execution.
Definition: request.h:120
core::net::http::Request::state
virtual State state()=0
state queries the current state of the operation.
core::net::http::Request::Errors::AlreadyActive
AlreadyActive is thrown when *execute is called on an active request.
Definition: request.h:65
core::net::http::Request::Configuration::header
Header header
Definition: request.h:181
core::net::http::Request::Configuration::on_response
ResponseHandler on_response
Definition: request.h:187
core::net::http::Request::Configuration::for_proxy
AuthenicationHandler for_proxy
Definition: request.h:214
core
Definition: location.h:24
core::net::http::Request::State
State
The State enum describes the different states a request can be in.
Definition: request.h:48
core::net::http::Request::Credentials::password
std::string password
Definition: request.h:153
core::net::http::Request::Configuration::for_http
AuthenicationHandler for_http
Definition: request.h:212
core::net::http::Request::Configuration::uri
std::string uri
Definition: request.h:178
core::net::http::Request::Configuration::on_error
ErrorHandler on_error
Definition: request.h:190
core::net::http::Request::Credentials
The Credentials struct encapsulates username and password for basic & digest authentication.
Definition: request.h:151
core::net::http::Request::Handler::on_error
const ErrorHandler & on_error() const
Returns the currently set error handler.
core::net::http::Request::execute
virtual Response execute(const ProgressHandler &ph)=0
Synchronously executes the request.
CORE_NET_DLL_PUBLIC
#define CORE_NET_DLL_PUBLIC
Definition: visibility.h:25
core::net::http::Request::Progress::Next
Next
The Next enum summarizes the available return-types for the progress callback.
Definition: request.h:83
core::net::http::Request::ProgressHandler
std::function< Progress::Next(const Progress &)> ProgressHandler
ProgressHandler is invoked for progress updates while executing the request.
Definition: request.h:109
core::net::http::Request::Handler::on_response
Handler & on_response(const ResponseHandler &handler)
Adjusts the currently set response handler.
core::net::http::Request::operator=
Request & operator=(const Request &)=delete
core::net::http::Request::Handler::on_error
Handler & on_error(const ErrorHandler &handler)
Adjusts the currently set error handler.
core::net::http::Request::AuthenicationHandler
std::function< Credentials(const std::string &)> AuthenicationHandler
Definition: request.h:157
error.h
core::net::http::Request::Handler::on_progress
Handler & on_progress(const ProgressHandler &handler)
Adjusts the currently set progress handler.
core::net::http::Request::Handler::Handler
Handler()=default
core::net::http::Request::Credentials::username
std::string username
Definition: request.h:152
core::net::http::Request
The Request class encapsulates a request for a web resource.
Definition: request.h:42
core::net::http::Request::Errors::AlreadyActive::AlreadyActive
AlreadyActive(const core::Location &loc)
AlreadyActive creates a new instance with a location hint.
core::net::http::Request::Progress
The Progress struct encapsulates progress information for web-resource requests.
Definition: request.h:78
core::net::http::Request::Configuration::on_progress
ProgressHandler on_progress
Definition: request.h:184
core::net::http::Request::set_timeout
virtual void set_timeout(const std::chrono::milliseconds &timeout)=0
Adjusts the timeout of a State::ready request.
core::net::Error
Definition: error.h:30
header.h
core::net::http::Request::ResponseHandler
std::function< void(const Response &)> ResponseHandler
ResponseHandler is invoked when a request completes.
Definition: request.h:114