MySQL 9.7.2
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(TlsServerContext *tls_context, IoThreads *threads, Bind *bind_raw,
85 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 void clear_overrides();
106
107 void start();
108 void stop();
109
110 private: // Ssl connections handling
111 void on_new_ssl_connection(socket socket);
112 uint64_t max_request_body_size() const override {
113 return effective_max_request_body_size();
114 }
115 uint64_t max_response_body_size() const override {
116 return effective_max_response_body_size();
117 }
118 void log_max_request_body_size_rejection(
119 uint64_t max_request_body_size,
120 std::optional<uint64_t> content_length) override;
121 void log_invalid_request_body_headers_rejection(
122 std::string_view reason) override;
123 void log_max_response_body_size_rejection(
124 uint64_t max_response_body_size, uint64_t response_body_size) override;
125 void on_connection_close(ConnectionTls *connection) override;
126 void on_connection_io_error(ConnectionTls *connection,
127 const std::error_code &ec) override;
128
129 private: // Raw connections handling
130 void on_new_connection(socket socket);
131 void on_connection_close(ConnectionRaw *connection) override;
133 const std::error_code &ec) override;
134
135 private:
136 enum class State { kInitializing, kRunning, kStopping, kStopped };
138 std::chrono::steady_clock::time_point next_log_time{};
139 uint64_t suppressed_logs{0};
140 std::optional<net::steady_timer> summary_timer;
141 bool summary_scheduled{false};
142 };
143
144 size_t disconnect_all();
145 std::optional<uint64_t> reject_max_http_connections_limit() const;
146 void log_max_http_connections_rejection(const uint64_t max_http_connections);
147 void schedule_suppressed_log_summary(RateLimitedLogState &state,
148 const char *summary_message);
149 void cancel_suppressed_log_summary(RateLimitedLogState &state);
150 void flush_suppressed_log_summary(RateLimitedLogState &state,
151 const char *summary_message);
152 void flush_all_suppressed_log_summaries();
153 void cancel_all_suppressed_log_summaries();
154
155 void start_accepting();
156 socket socket_move_to_io_thread(socket socket);
157 IoThread *return_next_thread();
159 std::list<IoThread> *threads_;
165 RequestHandlerInterface *handler_ = nullptr;
166
168 std::vector<std::shared_ptr<ServerConnectionRaw>> connections_;
169 std::vector<std::shared_ptr<ServerConnectionTls>> connections_ssl_;
171 State::kInitializing};
172 uint64_t configured_max_http_connections_{kDefaultMaxHttpConnections};
173 uint64_t configured_max_request_body_size_{kDefaultMaxRequestBodySize};
174 uint64_t configured_max_response_body_size_{kDefaultMaxResponseBodySize};
178 std::atomic<uint64_t> max_http_connections_override_;
179 std::atomic<uint64_t> max_request_body_size_override_;
180 std::atomic<uint64_t> max_response_body_size_override_;
186};
187
188} // namespace server
189} // namespace http
190
191#endif // ROUTER_SRC_HTTP_SRC_HTTP_SERVER_SERVER_H_
Definition: io_thread.h:39
TLS Context for the server side.
Definition: tls_server_context.h:51
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:181
net::impl::socket::native_handle_type native_handle_type
Definition: server.h:76
IoIterator current_thread_
Definition: server.h:160
std::atomic< bool > has_max_request_body_size_override_
Definition: server.h:176
RateLimitedLogState max_request_body_size_reject_log_
Definition: server.h:183
base::method::Bitset allowed_methods_
Definition: server.h:164
Bind * bind_ssl_
Definition: server.h:162
RateLimitedLogState invalid_request_body_headers_reject_log_
Definition: server.h:184
void on_connection_close(ConnectionRaw *connection) override
uint64_t max_response_body_size() const override
Definition: server.h:115
std::atomic< bool > has_max_http_connections_override_
Definition: server.h:175
TlsServerContext * tls_context_
Definition: server.h:158
std::list< IoThread > IoThreads
Definition: server.h:80
State
Definition: server.h:136
std::atomic< uint64_t > max_http_connections_override_
Definition: server.h:178
std::mutex mutex_connection_
Definition: server.h:167
std::vector< std::shared_ptr< ServerConnectionTls > > connections_ssl_
Definition: server.h:169
void on_connection_io_error(ConnectionRaw *connection, const std::error_code &ec) override
Bind * bind_raw_
Definition: server.h:161
RateLimitedLogState max_response_body_size_reject_log_
Definition: server.h:185
std::list< IoThread > * threads_
Definition: server.h:159
std::atomic< uint64_t > max_request_body_size_override_
Definition: server.h:179
std::vector< std::shared_ptr< ServerConnectionRaw > > connections_
Definition: server.h:168
std::atomic< bool > has_max_response_body_size_override_
Definition: server.h:177
uint64_t max_request_body_size() const override
Definition: server.h:112
std::atomic< uint64_t > max_response_body_size_override_
Definition: server.h:180
net::io_context * log_flush_context_
Definition: server.h:163
RateLimitedLogState max_http_connections_reject_log_
Definition: server.h:182
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:397
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
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:140
Definition: server_struct.h:39