MySQL 26.10.0
Source Code Documentation
server.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2026, 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 ROUTER_SRC_HTTP_SRC_HTTP_SERVER_SERVER_H_
27#define ROUTER_SRC_HTTP_SRC_HTTP_SERVER_SERVER_H_
28
29#include <atomic>
30#include <chrono>
31#include <cstdint>
32#include <list>
33#include <memory>
34#include <mutex> // NOLINT(build/c++11)
35#include <optional>
36#include <string_view>
37#include <vector>
38
41
44#include "http/base/method.h"
45#include "http/server/bind.h"
49#include "tls/tls_stream.h"
50
51class IoThread;
52
53namespace http {
54namespace server {
55
56constexpr uint64_t kDefaultMaxHttpConnections{1024};
57constexpr uint64_t kDefaultMaxRequestBodySize{16 * 1024 * 1024};
58constexpr uint64_t kDefaultMaxResponseBodySize{16 * 1024 * 1024};
59constexpr uint64_t kMaxHttpConnectionsUpperBound{100000};
60constexpr uint64_t kMaxBodySizeUpperBound{2147483648ULL};
61
72
75 public:
80 using IoThreads = std::list<IoThread>;
81 using IoIterator = IoThreads::iterator;
82
83 public:
84 Server(std::shared_ptr<TlsServerContext> tls_context, IoThreads *threads,
85 Bind *bind_raw, Bind *bind_ssl,
86 uint64_t max_http_connections = kDefaultMaxHttpConnections,
87 uint64_t max_request_body_size = kDefaultMaxRequestBodySize,
88 uint64_t max_response_body_size = kDefaultMaxResponseBodySize,
89 net::io_context *log_flush_context = nullptr);
90
91 void set_allowed_methods(const Methods &methods);
92 void set_request_handler(RequestHandlerInterface *handler);
93
94 // Runtime overrides are policy-free. Callers that expose user/config input
95 // must validate their own bounds before setting an override.
96 void set_max_http_connections(std::optional<uint64_t> value);
97 uint64_t effective_max_http_connections() const;
98
99 void set_max_request_body_size(std::optional<uint64_t> value);
100 uint64_t effective_max_request_body_size() const;
101
102 void set_max_response_body_size(std::optional<uint64_t> value);
103 uint64_t effective_max_response_body_size() const;
104
105 // TLS context management
106 void replace_tls_context(std::shared_ptr<TlsServerContext> tls_context) {
107 std::lock_guard<std::mutex> lock{tls_context_mtx_};
108 tls_context_ = std::move(tls_context);
109 }
110
111 // Server lifecycle
112 void start();
113 void stop();
114
115 void clear_overrides();
116
117 private: // Ssl connections handling
118 void on_new_ssl_connection(socket socket);
119 uint64_t max_request_body_size() const override {
120 return effective_max_request_body_size();
121 }
122 uint64_t max_response_body_size() const override {
123 return effective_max_response_body_size();
124 }
125 void log_max_request_body_size_rejection(
126 uint64_t max_request_body_size,
127 std::optional<uint64_t> content_length) override;
128 void log_invalid_request_body_headers_rejection(
129 std::string_view reason) override;
130 void log_max_response_body_size_rejection(
131 uint64_t max_response_body_size, uint64_t response_body_size) override;
132 void on_connection_close(ConnectionTls *connection) override;
133 void on_connection_io_error(ConnectionTls *connection,
134 const std::error_code &ec) override;
135
136 private: // Raw connections handling
137 void on_new_connection(socket socket);
138 void on_connection_close(ConnectionRaw *connection) override;
140 const std::error_code &ec) override;
141
142 private:
143 enum class State { kInitializing, kRunning, kStopping, kStopped };
145 std::chrono::steady_clock::time_point next_log_time{};
146 uint64_t suppressed_logs{0};
147 std::optional<net::steady_timer> summary_timer;
148 bool summary_scheduled{false};
149 };
150
151 size_t disconnect_all();
152 std::optional<uint64_t> reject_max_http_connections_limit() const;
153 void log_max_http_connections_rejection(const uint64_t max_http_connections);
154 void schedule_suppressed_log_summary(RateLimitedLogState &state,
155 const char *summary_message);
156 void cancel_suppressed_log_summary(RateLimitedLogState &state);
157 void flush_suppressed_log_summary(RateLimitedLogState &state,
158 const char *summary_message);
159 void flush_all_suppressed_log_summaries();
160 void cancel_all_suppressed_log_summaries();
161
162 void start_accepting();
163 socket socket_move_to_io_thread(socket socket);
164 IoThread *return_next_thread();
166 std::shared_ptr<TlsServerContext> tls_context_;
167 std::list<IoThread> *threads_;
173 RequestHandlerInterface *handler_ = nullptr;
174
176 std::vector<std::shared_ptr<ServerConnectionRaw>> connections_;
177 std::vector<std::shared_ptr<ServerConnectionTls>> connections_ssl_;
179 State::kInitializing};
180 uint64_t configured_max_http_connections_{kDefaultMaxHttpConnections};
181 uint64_t configured_max_request_body_size_{kDefaultMaxRequestBodySize};
182 uint64_t configured_max_response_body_size_{kDefaultMaxResponseBodySize};
186 std::atomic<uint64_t> max_http_connections_override_;
187 std::atomic<uint64_t> max_request_body_size_override_;
188 std::atomic<uint64_t> max_response_body_size_override_;
194};
195
196} // namespace server
197} // namespace http
198
199#endif // ROUTER_SRC_HTTP_SRC_HTTP_SERVER_SERVER_H_
Definition: io_thread.h:47
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4753
Definition: bind.h:41
Definition: request_handler_interface.h:36
Definition: connection.h:49
Definition: server.h:74
base::method::Bitset Methods
Definition: server.h:79
std::mutex mutex_limit_log_
Definition: server.h:189
net::impl::socket::native_handle_type native_handle_type
Definition: server.h:76
IoIterator current_thread_
Definition: server.h:168
std::atomic< bool > has_max_request_body_size_override_
Definition: server.h:184
RateLimitedLogState max_request_body_size_reject_log_
Definition: server.h:191
std::shared_ptr< TlsServerContext > tls_context_
Definition: server.h:166
base::method::Bitset allowed_methods_
Definition: server.h:172
Bind * bind_ssl_
Definition: server.h:170
RateLimitedLogState invalid_request_body_headers_reject_log_
Definition: server.h:192
std::mutex tls_context_mtx_
Definition: server.h:165
void on_connection_close(ConnectionRaw *connection) override
uint64_t max_response_body_size() const override
Definition: server.h:122
std::atomic< bool > has_max_http_connections_override_
Definition: server.h:183
std::list< IoThread > IoThreads
Definition: server.h:80
State
Definition: server.h:143
std::atomic< uint64_t > max_http_connections_override_
Definition: server.h:186
std::mutex mutex_connection_
Definition: server.h:175
std::vector< std::shared_ptr< ServerConnectionTls > > connections_ssl_
Definition: server.h:177
void on_connection_io_error(ConnectionRaw *connection, const std::error_code &ec) override
Bind * bind_raw_
Definition: server.h:169
RateLimitedLogState max_response_body_size_reject_log_
Definition: server.h:193
std::list< IoThread > * threads_
Definition: server.h:167
std::atomic< uint64_t > max_request_body_size_override_
Definition: server.h:187
std::vector< std::shared_ptr< ServerConnectionRaw > > connections_
Definition: server.h:176
std::atomic< bool > has_max_response_body_size_override_
Definition: server.h:185
uint64_t max_request_body_size() const override
Definition: server.h:119
void replace_tls_context(std::shared_ptr< TlsServerContext > tls_context)
Definition: server.h:106
std::atomic< uint64_t > max_response_body_size_override_
Definition: server.h:188
net::io_context * log_flush_context_
Definition: server.h:171
RateLimitedLogState max_http_connections_reject_log_
Definition: server.h:190
IoThreads::iterator IoIterator
Definition: server.h:81
Definition: wait_variable.h:37
Definition: socket.h:1090
Definition: io_context.h:61
basic_stream_socket< tcp > socket
Definition: internet.h:1159
Definition: tls_stream.h:47
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
#define HTTP_SERVER_LIB_EXPORT
Definition: http_server_lib_export.h:15
static void stop(mysql_harness::PluginFuncEnv *)
Definition: mysql_rest_service_plugin.cc:413
std::bitset< Pos::_LAST+1 > Bitset
Definition: method.h:57
constexpr uint64_t kDefaultMaxHttpConnections
Definition: server.h:56
constexpr uint64_t kDefaultMaxRequestBodySize
Definition: server.h:57
constexpr uint64_t kMaxHttpConnectionsUpperBound
Definition: server.h:59
constexpr uint64_t kMaxBodySizeUpperBound
Definition: server.h:60
constexpr uint64_t kDefaultMaxResponseBodySize
Definition: server.h:58
Definition: connection.h:58
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
stdx::expected< native_handle_type, error_type > socket(int family, int sock_type, int protocol)
Definition: socket.h:63
int native_handle_type
Definition: socket_constants.h:51
std::optional< net::steady_timer > summary_timer
Definition: server.h:147
Definition: server_struct.h:39