net-cpp  ..
C++11 library for networking purposes
client.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_CLIENT_H_
20 #define CORE_NET_HTTP_CLIENT_H_
21 
22 #include <core/net/visibility.h>
23 
24 #include <core/net/http/method.h>
25 #include <core/net/http/request.h>
26 
27 #include <chrono>
28 #include <iosfwd>
29 #include <memory>
30 
31 namespace core
32 {
33 namespace net
34 {
35 
36 struct Uri;
37 
38 namespace http
39 {
40 class ContentType;
41 class Request;
42 
44 {
45 public:
47  struct Errors
48  {
49  Errors() = delete;
50 
55  {
58  };
59  };
60 
62  struct Timings
63  {
64  typedef std::chrono::duration<double> Seconds;
65 
66  struct Statistics
67  {
69  Seconds max{Seconds::max()};
71  Seconds min{Seconds::max()};
73  Seconds mean{Seconds::max()};
75  Seconds variance{Seconds::max()};
76  };
77 
79  Statistics name_look_up{};
83  Statistics connect{};
87  Statistics app_connect{};
89  Statistics pre_transfer{};
91  Statistics start_transfer{};
93  Statistics total{};
94  };
95 
96  Client(const Client&) = delete;
97  virtual ~Client() = default;
98 
99  Client& operator=(const Client&) = delete;
100  bool operator==(const Client&) const = delete;
101 
102  virtual std::string uri_to_string (const core::net::Uri& uri) const;
103 
105  virtual std::string url_escape(const std::string& s) const = 0;
106 
108  virtual std::string base64_encode(const std::string& s) const = 0;
109 
111  virtual std::string base64_decode(const std::string& s) const = 0;
112 
114  virtual Timings timings() = 0;
115 
117  virtual void run() = 0;
118 
120  virtual void stop() = 0;
121 
128  virtual std::shared_ptr<Request> get(const Request::Configuration& configuration) = 0;
129 
136  virtual std::shared_ptr<Request> head(const Request::Configuration& configuration) = 0;
137 
146  virtual std::shared_ptr<Request> put(const Request::Configuration& configuration, std::istream& payload, std::size_t size) = 0;
147 
156  virtual std::shared_ptr<Request> post(const Request::Configuration& configuration, const std::string& payload, const std::string& type) = 0;
157 
165  virtual std::shared_ptr<Request> post_form(const Request::Configuration& configuration, const std::map<std::string, std::string>& values);
166 
175  std::shared_ptr<Request> post(const Request::Configuration& configuration, std::istream& payload, std::size_t size);
176 
183  std::shared_ptr<Request> del(const Request::Configuration& configuration);
184 
185 protected:
186  Client() = default;
187 };
188 
190 CORE_NET_DLL_PUBLIC std::shared_ptr<Client> make_client();
191 }
192 }
193 }
194 
195 #endif // CORE_NET_HTTP_CLIENT_H_
method.h
core::net::http::Method::head
@ head
core::net::http::Method::post
@ post
core::Location
Definition: location.h:25
core::net::http::Method::put
@ put
visibility.h
core::net::http::Error
Definition: http/error.h:29
core::net::http::Client::Timings::Statistics
Definition: client.h:66
core::net::http::Request::Configuration
The Configuration struct encapsulates all options for creating requests.
Definition: request.h:162
core::net::http::Client
Definition: client.h:43
core::net::http::Client::Errors::HttpMethodNotSupported
HttpMethodNotSupported is thrown if the underlying impl. does not support the requested HTTP method.
Definition: client.h:54
core::net::http::ContentType
Collection of known content types.
Definition: content_type.h:32
core::net::http::Client::Timings
Summarizes timing information about completed requests.
Definition: client.h:62
core::net::http::make_client
CORE_NET_DLL_PUBLIC std::shared_ptr< Client > make_client()
Dispatches to the default implementation and returns a client instance.
core::net::http::Method
Method
Definition: method.h:32
core
Definition: location.h:23
request.h
core::net::http::Client::Timings::Seconds
std::chrono::duration< double > Seconds
Definition: client.h:64
CORE_NET_DLL_PUBLIC
#define CORE_NET_DLL_PUBLIC
Definition: visibility.h:25
core::net::Uri
The Uri class encapsulates the components of a URI.
Definition: uri.h:36
core::net::http::Client::Errors::HttpMethodNotSupported::method
Method method
Definition: client.h:57
core::net::http::Method::del
@ del
core::net::http::Request
The Request class encapsulates a request for a web resource.
Definition: request.h:41
core::net::http::Client::Errors
Summarizes error conditions.
Definition: client.h:47
core::net::http::Method::get
@ get