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