net-cpp  2.0.0
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  */
18 #ifndef CORE_NET_HTTP_CLIENT_H_
19 #define CORE_NET_HTTP_CLIENT_H_
20 
21 #include <core/net/visibility.h>
22 
23 #include <core/net/http/method.h>
24 #include <core/net/http/request.h>
25 
26 #include <chrono>
27 #include <iosfwd>
28 #include <memory>
29 
30 namespace core
31 {
32 namespace net
33 {
34 
35 struct Uri;
36 
37 namespace http
38 {
39 class ContentType;
40 class Request;
41 
43 {
44 public:
46  struct Errors
47  {
48  Errors() = delete;
49 
54  {
57  };
58  };
59 
61  struct Timings
62  {
63  typedef std::chrono::duration<double> Seconds;
64 
65  struct Statistics
66  {
68  Seconds max{Seconds::max()};
70  Seconds min{Seconds::max()};
72  Seconds mean{Seconds::max()};
74  Seconds variance{Seconds::max()};
75  };
76 
78  Statistics name_look_up{};
82  Statistics connect{};
86  Statistics app_connect{};
88  Statistics pre_transfer{};
90  Statistics start_transfer{};
92  Statistics total{};
93  };
94 
95  Client(const Client&) = delete;
96  virtual ~Client() = default;
97 
98  Client& operator=(const Client&) = delete;
99  bool operator==(const Client&) const = delete;
100 
101  virtual std::string uri_to_string (const core::net::Uri& uri) const;
102 
104  virtual std::string url_escape(const std::string& s) const = 0;
105 
107  virtual std::string base64_encode(const std::string& s) const = 0;
108 
110  virtual std::string base64_decode(const std::string& s) const = 0;
111 
113  virtual Timings timings() = 0;
114 
116  virtual void run() = 0;
117 
119  virtual void stop() = 0;
120 
127  virtual std::shared_ptr<Request> get(const Request::Configuration& configuration) = 0;
128 
135  virtual std::shared_ptr<Request> head(const Request::Configuration& configuration) = 0;
136 
145  virtual std::shared_ptr<Request> put(const Request::Configuration& configuration, std::istream& payload, std::size_t size) = 0;
146 
155  virtual std::shared_ptr<Request> post(const Request::Configuration& configuration, const std::string& payload, const std::string& type) = 0;
156 
164  virtual std::shared_ptr<Request> post_form(const Request::Configuration& configuration, const std::map<std::string, std::string>& values);
165 
166 protected:
167  Client() = default;
168 };
169 
171 CORE_NET_DLL_PUBLIC std::shared_ptr<Client> make_client();
172 }
173 }
174 }
175 
176 #endif // CORE_NET_HTTP_CLIENT_H_
The Request class encapsulates a request for a web resource.
Definition: request.h:40
std::chrono::duration< double > Seconds
Definition: client.h:63
The Uri class encapsulates the components of a URI.
Definition: uri.h:36
Definition: location.h:23
Collection of known content types.
Definition: content_type.h:32
Summarizes timing information about completed requests.
Definition: client.h:61
CORE_NET_DLL_PUBLIC std::shared_ptr< Client > make_client()
Dispatches to the default implementation and returns a client instance.
HttpMethodNotSupported is thrown if the underlying impl. does not support the requested HTTP method...
Definition: client.h:53
The Configuration struct encapsulates all options for creating requests.
Definition: request.h:161
#define CORE_NET_DLL_PUBLIC
Definition: visibility.h:25
Summarizes error conditions.
Definition: client.h:46