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 
42 {
43 public:
47  enum class State
48  {
49  ready,
50  active,
51  done
52  };
53 
57  struct Errors
58  {
59  Errors() = delete;
60 
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 
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 
263 protected:
265  Request() = default;
267 };
268 }
269 }
270 }
271 
272 #endif // CORE_NET_HTTP_REQUEST_H_
core::net::http::Request::Errors
The Errors struct collects the Request-specific exceptions and error modes.
Definition: request.h:57
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::Location
Definition: location.h:25
visibility.h
core::net::http::Error
Definition: http/error.h:29
core::net::http::Header
The Header class encapsulates the headers of an HTTP request/response.
Definition: header.h:38
core::net::http::Request::Configuration
The Configuration struct encapsulates all options for creating requests.
Definition: request.h:162
core::net::http::Response
The Response struct models a response to a core::net::http::Request.
Definition: response.h:39
core::net::http::Request::Handler
Encapsulates callbacks that can happen during request execution.
Definition: request.h:119
core::net::http::Request::Errors::AlreadyActive
AlreadyActive is thrown when *execute is called on an active request.
Definition: request.h:64
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:23
core::net::http::Request::State
State
The State enum describes the different states a request can be in.
Definition: request.h:47
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:150
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:82
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::AuthenicationHandler
std::function< Credentials(const std::string &)> AuthenicationHandler
Definition: request.h:157
error.h
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:41
core::net::http::Request::Progress
The Progress struct encapsulates progress information for web-resource requests.
Definition: request.h:77
core::net::http::Request::Configuration::on_progress
ProgressHandler on_progress
Definition: request.h:184
core::net::Error
Definition: error.h:29
header.h
core::net::http::Request::ResponseHandler
std::function< void(const Response &)> ResponseHandler
ResponseHandler is invoked when a request completes.
Definition: request.h:114