MySQL 8.1.0
Source Code Documentation
http_common.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_COMMON_INCLUDED
26#define MYSQLROUTER_HTTP_COMMON_INCLUDED
27
29
30// http_common is a wrapper on `libevent`, thus
31// all header from `libevent` should be included in "c" file.
32// We need this header to have `evutil_socket_t` because it
33// defines socket in different way than most libraries do.
34// No function nor structure from "util.h" should be used by
35// "wrapper-importers".
36#include <event2/util.h>
37
38#include <bitset>
39#include <ctime>
40#include <functional> // std::function
41#include <memory>
42#include <stdexcept>
43#include <string>
44#include <system_error>
45#include <vector>
46
47#include "my_compiler.h"
48#include "my_io.h"
49#include "my_macros.h"
50
53
54// http_common.cc
55
56/**
57 * `libevent` global state management
58 */
60 public:
61 enum class Log { Debug, Error, Warning, Message };
62 enum class DebugLogLevel : uint32_t { None = 0, All = ~(uint32_t)0 };
63 using CallbackLog = void (*)(const Log log, const char *message);
64 Event() = delete;
65
66 public:
67 static bool initialize_threads();
68 static void shutdown();
69 static void set_log_callback(const CallbackLog);
70 static void enable_debug_logging(const DebugLogLevel which);
71
72 static bool has_ssl();
73
75};
76
77/**
78 * Flags that represents which I/O events should be monitored
79 */
80namespace EventFlags {
81using type = int;
82using pos_type = unsigned;
83namespace Pos {
84constexpr pos_type Timeout = 0;
85constexpr pos_type Read = 1;
86constexpr pos_type Write = 2;
87constexpr pos_type Signal = 3;
88
89constexpr pos_type _LAST = Signal;
90} // namespace Pos
91using Bitset = std::bitset<Pos::_LAST + 1>;
92
93constexpr type Timeout{1 << Pos::Timeout};
94constexpr type Read{1 << Pos::Read};
95constexpr type Write{1 << Pos::Write};
96constexpr type Signal{1 << Pos::Signal};
97} // namespace EventFlags
98using EventBaseSocket = evutil_socket_t;
99
101// Suppress warning for now.
102// TODO(lkotula) Use proper 64bit/32bit signed/unsigned type based on platform.
103// 'initializing': truncation of constant value.
105MY_COMPILER_CLANG_DIAGNOSTIC_IGNORE("-Wconstant-conversion")
108
109/**
110 * Main event registration and dispatch `engine`
111 *
112 * Wrapper for `event_base` structure from `libevent`.
113 */
115 public:
117 using CallbackEvent = void (*)(SocketHandle, short, void *);
118 EventBase();
119 // Needed because the class defined dtor
121 // Needed by pimpl
123
124 public:
125 /**
126 * Register new event notification
127 *
128 * Depending on arguments, the function may register notification for:
129 * socket read/write, timeout, signal handler.
130 *
131 * @retval false if `registration` fails
132 * @retval true if event was scheduled
133 */
134 bool once(const SocketHandle fd, const EventFlags::Bitset events,
135 CallbackEvent cb, void *arg, const struct timeval *tv);
136
137 /**
138 * Stop dispatching
139 *
140 * While some thread is blocked inside `dispatch` method, some other
141 * thread might call this function to notify and break the dispatching
142 * loop inside `dispatch`.
143 *
144 * @retval false if breaking the loop fails
145 * @retval true if event breaking the loop was scheduled
146 */
147 bool loop_exit(const struct timeval *tv);
148
149 /**
150 * Wait for registered notifications, and when they become `active`
151 * dispatch them. If there is no event registered return.
152 *
153 * @return 0 if successful, -1 if an error occurred, or 1 if we exited because
154 * no events were pending or active.
155 */
156 int dispatch();
157
158 private:
159 struct impl;
160 friend class EventHttp;
161 friend class EventBuffer;
162
163 EventBase(std::unique_ptr<impl> &&pImpl);
164
165 std::unique_ptr<impl> pImpl_;
166};
167
168/**
169 * Flags that represents different `bufferevent` options.
170 */
172using type = int;
173using pos_type = unsigned;
174namespace Pos {
175constexpr pos_type CloseOnFree = 0;
176constexpr pos_type ThreadSafe = 1;
179
181} // namespace Pos
182using Bitset = std::bitset<Pos::_LAST + 1>;
183
188} // namespace EventBufferOptionsFlags
189
190/**
191 * Enables bufforing of I/O for a socket
192 *
193 * Wrapper for `bufferevent` structure from `libevent`.
194 * Additionally this class allows custom processing, like
195 * SSL.
196 *
197 * Notice: For now the functionality is limited to minimum.
198 */
200 public:
201 // State of SSL-connection that is passed to this structure.
202 enum class SslState { Open = 0, Connecting = 1, Accepting = 2 };
204
205 /**
206 * Initialize the buffer, with OpenSSL processing.
207 *
208 * This c-tor, creates new SSL-connection from `TlsContext`, which means
209 * that `state` can only be set to `SslState::Connecting` or
210 * `SslState::Accepting`.
211 */
212 EventBuffer(EventBase *, const SocketHandle socket, TlsContext *tls_context,
213 const SslState state,
216 ~EventBuffer();
217
218 private:
219 friend class EventHttp;
220 struct impl;
221
222 std::unique_ptr<impl> pImpl_;
223};
224
225#endif
A simple bitset wrapper class, whose size can be specified after the object has been defined.
Definition: ut0bitset.h:38
Main event registration and dispatch engine
Definition: http_common.h:114
EventBaseSocket SocketHandle
Definition: http_common.h:116
void(*)(SocketHandle, short, void *) CallbackEvent
Definition: http_common.h:117
std::unique_ptr< impl > pImpl_
Definition: http_common.h:165
Enables bufforing of I/O for a socket.
Definition: http_common.h:199
SslState
Definition: http_common.h:202
EventBaseSocket SocketHandle
Definition: http_common.h:203
std::unique_ptr< impl > pImpl_
Definition: http_common.h:220
Http server build on top of EventBase.
Definition: http_request.h:524
libevent global state management
Definition: http_common.h:59
static CallbackLog cbLog_
Definition: http_common.h:74
Event()=delete
DebugLogLevel
Definition: http_common.h:62
void(*)(const Log log, const char *message) CallbackLog
Definition: http_common.h:63
Definition: logger.h:48
wraps SSL_CTX.
Definition: tls_context.h:84
Class representing a warning.
Definition: warning.h:40
evutil_socket_t EventBaseSocket
Definition: http_common.h:98
const int kEventBaseInvalidSocket
Definition: http_common.h:106
#define HTTP_COMMON_EXPORT
Definition: http_common_export.h:39
Header for compiler-dependent features.
#define MY_COMPILER_MSVC_DIAGNOSTIC_IGNORE(X)
Definition: my_compiler.h:254
#define MY_COMPILER_DIAGNOSTIC_PUSH()
save the compiler's diagnostic (enabled warnings, errors, ...) state
Definition: my_compiler.h:284
#define MY_COMPILER_DIAGNOSTIC_POP()
restore the compiler's diagnostic (enabled warnings, errors, ...) state
Definition: my_compiler.h:285
Common #defines and includes for file and socket I/O.
#define INVALID_SOCKET
Definition: my_io.h:189
Some common macros.
constexpr pos_type CloseOnFree
Definition: http_common.h:175
constexpr pos_type DeferCallbacks
Definition: http_common.h:177
constexpr pos_type _LAST
Definition: http_common.h:180
constexpr pos_type UnlockCallbacks
Definition: http_common.h:178
constexpr pos_type ThreadSafe
Definition: http_common.h:176
Flags that represents different bufferevent options.
Definition: http_common.h:171
std::bitset< Pos::_LAST+1 > Bitset
Definition: http_common.h:182
int type
Definition: http_common.h:172
unsigned pos_type
Definition: http_common.h:173
constexpr type UnlockCallbacks
Definition: http_common.h:187
constexpr type DeferCallbacks
Definition: http_common.h:186
constexpr type ThreadSafe
Definition: http_common.h:185
constexpr type CloseOnFree
Definition: http_common.h:184
constexpr pos_type Write
Definition: http_common.h:86
constexpr pos_type Timeout
Definition: http_common.h:84
constexpr pos_type Read
Definition: http_common.h:85
constexpr pos_type Signal
Definition: http_common.h:87
constexpr pos_type _LAST
Definition: http_common.h:89
Flags that represents which I/O events should be monitored.
Definition: http_common.h:80
constexpr type Signal
Definition: http_common.h:96
std::bitset< Pos::_LAST+1 > Bitset
Definition: http_common.h:91
constexpr type Write
Definition: http_common.h:95
unsigned pos_type
Definition: http_common.h:82
constexpr type Timeout
Definition: http_common.h:93
constexpr type Read
Definition: http_common.h:94
int type
Definition: http_common.h:81
Message
Definition: histogram.h:82
Definition: authentication.cc:35
MYSQL_STRINGS_EXPORT void shutdown()
Shutdown character set/collation library.
Definition: collations.cc:104
stdx::expected< native_handle_type, error_type > socket(int family, int sock_type, int protocol)
Definition: socket.h:62
auto dispatch(CompletionToken &&token)
Definition: executor.h:717
Definition: options.cc:56
MY_COMPILER_CLANG_DIAGNOSTIC_IGNORE("-Winconsistent-missing-destructor-override") extern "C"
Definition: protobuf_plugin.cc:31
required string event
Definition: replication_group_member_actions.proto:31