net-cpp  2.0.0
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  */
18 #ifndef CORE_NET_HTTP_REQUEST_H_
19 #define CORE_NET_HTTP_REQUEST_H_
20 
21 #include <core/net/visibility.h>
22 
23 #include <core/net/http/error.h>
24 #include <core/net/http/header.h>
25 
26 #include <chrono>
27 #include <memory>
28 
29 namespace core
30 {
31 namespace net
32 {
33 namespace http
34 {
35 class Response;
36 
41 {
42 public:
46  enum class State
47  {
48  ready,
49  active,
50  done
51  };
52 
56  struct Errors
57  {
58  Errors() = delete;
59 
64  {
69  AlreadyActive(const core::Location& loc);
70  };
71  };
72 
76  struct Progress
77  {
81  enum class Next
82  {
83  continue_operation,
84  abort_operation
85  };
86 
87  struct
88  {
89  double total{-1.};
90  double current{-1.};
91  } download{};
92 
93  struct
94  {
95  double total{-1.};
96  double current{-1.};
97  } upload{};
98  };
99 
103  typedef std::function<void(const core::net::Error&)> ErrorHandler;
104 
108  typedef std::function<Progress::Next(const Progress&)> ProgressHandler;
109 
113  typedef std::function<void(const Response&)> ResponseHandler;
114 
118  class Handler
119  {
120  public:
121  Handler() = default;
122 
124  const ProgressHandler& on_progress() const;
126  Handler& on_progress(const ProgressHandler& handler);
127 
129  const ResponseHandler& on_response() const;
131  Handler& on_response(const ResponseHandler& handler);
132 
134  const ErrorHandler& on_error() const;
136  Handler& on_error(const ErrorHandler& handler);
137 
138  private:
140  ProgressHandler progress_handler{};
141  ResponseHandler response_handler{};
142  ErrorHandler error_handler{};
144  };
145 
149  struct Credentials
150  {
151  std::string username;
152  std::string password;
153  };
154 
156  typedef std::function<Credentials(const std::string&)> AuthenicationHandler;
157 
162  {
168  inline static Configuration from_uri_as_string(const std::string& uri)
169  {
170  Configuration result;
171  result.uri = uri;
172 
173  return result;
174  }
175 
177  std::string uri;
178 
181 
183  ProgressHandler on_progress;
184 
186  ResponseHandler on_response;
187 
189  ErrorHandler on_error;
190 
192  struct
193  {
195  bool verify_peer
196  {
197  true
198  };
199 
201  bool verify_host
202  {
203  true
204  };
205  } ssl;
206 
208  struct
209  {
211  AuthenicationHandler for_http;
213  AuthenicationHandler for_proxy;
214  } authentication_handler;
215  };
216 
217  Request(const Request&) = delete;
218  virtual ~Request() = default;
219 
220  Request& operator=(const Request&) = delete;
221  bool operator==(const Request&) const = delete;
222 
227  virtual State state() = 0;
228 
233  virtual void set_timeout(const std::chrono::milliseconds& timeout) = 0;
234 
241  virtual Response execute(const ProgressHandler& ph) = 0;
242 
248  virtual void async_execute(const Handler& handler) = 0;
249 
254  virtual std::string url_escape(const std::string& s) = 0;
255 
260  virtual std::string url_unescape(const std::string& s) = 0;
261 
262 protected:
264  Request() = default;
266 };
267 }
268 }
269 }
270 
271 #endif // CORE_NET_HTTP_REQUEST_H_
The Request class encapsulates a request for a web resource.
Definition: request.h:40
Next
The Next enum summarizes the available return-types for the progress callback.
Definition: request.h:81
std::function< void(const core::net::Error &)> ErrorHandler
ErrorHandler is invoked in case of errors arising while executing the request.
Definition: request.h:103
The Progress struct encapsulates progress information for web-resource requests.
Definition: request.h:76
Definition: location.h:23
The Errors struct collects the Request-specific exceptions and error modes.
Definition: request.h:56
std::function< void(const Response &)> ResponseHandler
ResponseHandler is invoked when a request completes.
Definition: request.h:113
The Response struct models a response to a core::net::http::Request.
Definition: response.h:39
The Header class encapsulates the headers of an HTTP request/response.
Definition: header.h:37
AuthenicationHandler for_proxy
Definition: request.h:213
The Configuration struct encapsulates all options for creating requests.
Definition: request.h:161
AlreadyActive is thrown when *execute is called on an active request.
Definition: request.h:63
#define CORE_NET_DLL_PUBLIC
Definition: visibility.h:25
std::function< Credentials(const std::string &)> AuthenicationHandler
Definition: request.h:156
Encapsulates callbacks that can happen during request execution.
Definition: request.h:118
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:168
The Credentials struct encapsulates username and password for basic & digest authentication.
Definition: request.h:149
std::function< Progress::Next(const Progress &)> ProgressHandler
ProgressHandler is invoked for progress updates while executing the request.
Definition: request.h:108
State
The State enum describes the different states a request can be in.
Definition: request.h:46