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