MySQL 8.1.0
Source Code Documentation
http_client.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2023, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef MYSQLROUTER_HTTP_CLIENT_INCLUDED
26#define MYSQLROUTER_HTTP_CLIENT_INCLUDED
27
28#include <chrono>
29
33
34struct evhttp_connection;
35struct event_base;
36
37/**
38 * IO Context for network operations.
39 *
40 * wraps libevent's base
41 */
43 public:
44 IOContext();
46
47 /**
48 * wait for events to fire and calls handlers.
49 *
50 * exits if no more pending events
51 *
52 * @returns false if no events were pending nor active, true otherwise
53 * @throws std::runtime_error on internal, unexpected error
54 */
55 bool dispatch();
56
57 private:
58 class impl;
59
60 std::unique_ptr<impl> pImpl_;
62};
63
65 public:
67
68 void make_request(HttpRequest *req, HttpMethod::type method,
69 const std::string &uri,
71 void make_request_sync(HttpRequest *req, HttpMethod::type method,
72 const std::string &uri,
74 60});
75
76 /**
77 * connection has an error.
78 *
79 * @see error_msg()
80 */
81 operator bool() const;
82
83 /**
84 * error-msg of the connection.
85 *
86 * @note may not be human friendly as it may come directly from openssl
87 */
88 std::string error_msg() const;
89
90 /**
91 * last socket errno.
92 */
93 std::error_code socket_errno() const { return socket_errno_; }
94
95 protected:
97
98 class impl;
99
100 std::unique_ptr<impl> pImpl_;
101
103
104 std::error_code socket_errno_;
105
106 /**
107 * event-base associated with this connection.
108 */
109 event_base *ev_base() const;
110};
111
113 : public HttpClientConnectionBase {
114 public:
115 HttpClientConnection(IOContext &io_ctx, const std::string &address,
116 uint16_t port);
117};
118
120 : public HttpClientConnectionBase {
121 public:
123 const std::string &address, uint16_t port);
124};
125
127 public:
128 HttpClient(IOContext &io_ctx, const std::string &hostname, uint16_t port)
129 : io_ctx_{io_ctx}, hostname_{hostname}, port_{port} {}
130
131 virtual ~HttpClient();
132
133 /**
134 * initiate a request on the bound IOContext.
135 *
136 * allows to send out multiple requests on different clients
137 * and wait for them in parallel.
138 */
139 void make_request(HttpRequest *req, HttpMethod::type method,
140 const std::string &uri);
141
142 /**
143 * make a request and wait for the response.
144 */
145 void make_request_sync(HttpRequest *req, HttpMethod::type method,
146 const std::string &uri);
147
148 /**
149 * check if connection had an error.
150 *
151 * see: error_msg()
152 */
153 operator bool() const;
154
155 /**
156 * current error message.
157 *
158 * @see: HttpClientConnectionBase::error_msg()
159 */
160 std::string error_msg() const;
161
162 /**
163 * hostname to connect to.
164 */
165 std::string hostname() const { return hostname_; }
166
167 /**
168 * TCP port to connect to.
169 */
170 uint16_t port() const { return port_; }
171
172 protected:
173 virtual std::unique_ptr<HttpClientConnectionBase> make_connection();
174
176 const std::string hostname_;
177 uint16_t port_;
178
179 std::unique_ptr<HttpClientConnectionBase> conn_;
180};
181
183 public:
185 const std::string &address, uint16_t port)
186 : HttpClient(io_ctx, address, port), tls_ctx_{std::move(tls_ctx)} {}
187
188 private:
189 std::unique_ptr<HttpClientConnectionBase> make_connection() override;
190
192};
193
194#endif
Definition: http_client.h:64
std::unique_ptr< impl > pImpl_
Definition: http_client.h:98
std::error_code socket_errno() const
last socket errno.
Definition: http_client.h:93
std::error_code socket_errno_
Definition: http_client.h:104
IOContext & io_ctx_
Definition: http_client.h:102
Definition: http_client.h:113
Definition: http_client.h:126
uint16_t port_
Definition: http_client.h:177
virtual ~HttpClient()
IOContext & io_ctx_
Definition: http_client.h:175
uint16_t port() const
TCP port to connect to.
Definition: http_client.h:170
std::string hostname() const
hostname to connect to.
Definition: http_client.h:165
std::unique_ptr< HttpClientConnectionBase > conn_
Definition: http_client.h:179
const std::string hostname_
Definition: http_client.h:176
HttpClient(IOContext &io_ctx, const std::string &hostname, uint16_t port)
Definition: http_client.h:128
a HTTP request and response.
Definition: http_request.h:452
Definition: http_client.h:120
Definition: http_client.h:182
TlsClientContext tls_ctx_
Definition: http_client.h:191
HttpsClient(IOContext &io_ctx, TlsClientContext &&tls_ctx, const std::string &address, uint16_t port)
Definition: http_client.h:184
IO Context for network operations.
Definition: http_client.h:42
std::unique_ptr< impl > pImpl_
Definition: http_client.h:58
Client TLS Context.
Definition: tls_client_context.h:42
#define HTTP_CLIENT_EXPORT
Definition: http_client_export.h:39
int type
Definition: http_request.h:252
Definition: authentication.cc:35
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:497
auto dispatch(CompletionToken &&token)
Definition: executor.h:717
Definition: varlen_sort.h:183
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:32
double seconds()
Definition: task.cc:309