MySQL 8.0.37
Source Code Documentation
http_server_plugin.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_SERVER_PLUGIN_INCLUDED
27#define MYSQLROUTER_HTTP_SERVER_PLUGIN_INCLUDED
28
29#include <mutex>
30#include <regex>
31#include <string>
32#include <thread>
33#include <vector>
34
41
43 public:
44 void append(const std::string &url_regex_str,
45 std::unique_ptr<BaseRequestHandler> cb);
46 void remove(const std::string &url_regex_str);
47
48 // if no routes are specified, return 404
49 void route_default(HttpRequest &req);
50
51 void set_default_route(std::unique_ptr<BaseRequestHandler> cb);
53 void route(HttpRequest req);
54
55 void require_realm(const std::string &realm) { require_realm_ = realm; }
56
57 private:
58 struct RouterData {
59 std::string url_regex_str;
60 std::regex url_regex;
61 std::unique_ptr<BaseRequestHandler> handler;
62 };
63 std::vector<RouterData> request_handlers_;
64
65 std::unique_ptr<BaseRequestHandler> default_route_;
66 std::string require_realm_;
67
68 std::mutex route_mtx_;
69};
70
71/**
72 * base class of all http request handler threads
73 *
74 * - HttpRequestMainThread opens the socket, and accepts and handles connections
75 * - HttpRequestWorkerThread accepts and handles connections, using the socket
76 * listened by the main-thread
77 *
78 * As all threads can accept in parallel this may lead to a thundering herd
79 * problem and quite likely it is better to let only one thread accept() and
80 * push the socket handling into async-deque and let all workers steal from the
81 * queue
82 */
84 public:
86 // enable all methods to allow the higher layers to handle them
87 //
88 // CONNECT, TRACE and OPTIONS are disabled by default if not explicitly
89 // enabled.
91 HttpMethod::Bitset().set(/*all types*/));
92 }
93
95 : event_base_(std::move(object.event_base_)),
96 event_http_(std::move(object.event_http_)),
97 accept_fd_(object.accept_fd_) /* just copy */,
98 initialized_(object.is_initalized()) /* just copy */ {}
99
101
103
104 void accept_socket();
106 void wait_and_dispatch();
107
108 public: // Thread safe methods
109 void break_dispatching();
110 void wait_until_ready();
111
112 protected:
113 static void on_event_loop_ready(native_handle_type, short, void *);
114
115 bool is_initalized() const;
117
122};
123
125 public:
126 HttpServer(const char *address, uint16_t port)
127 : address_(address), port_(port) {}
128
129 HttpServer(const HttpServer &) = delete;
130 HttpServer &operator=(const HttpServer &) = delete;
131
132 HttpServer(HttpServer &&) = delete;
134
135 void join_all();
136
137 virtual ~HttpServer() { join_all(); }
138
139 virtual void start(size_t max_threads);
140 void stop();
141
142 void add_route(const std::string &url_regex,
143 std::unique_ptr<BaseRequestHandler> cb);
144 void remove_route(const std::string &url_regex);
145
147
148 protected:
149 std::vector<HttpRequestThread> thread_contexts_;
150 std::string address_;
151 uint16_t port_;
153
156
157 std::vector<std::thread> sys_threads_;
158};
159
160#endif
Main event registration and dispatch engine
Definition: http_common.h:115
Http server build on top of EventBase.
Definition: http_request.h:525
void set_allowed_http_methods(const HttpMethod::Bitset methods)
Set allowed methods for client request.
Definition: http_common.cc:252
Definition: http_server_plugin.h:42
void route_default(HttpRequest &req)
Definition: http_server_plugin.cc:98
void require_realm(const std::string &realm)
Definition: http_server_plugin.h:55
void clear_default_route()
Definition: http_server_plugin.cc:123
std::string require_realm_
Definition: http_server_plugin.h:66
std::unique_ptr< BaseRequestHandler > default_route_
Definition: http_server_plugin.h:65
void route(HttpRequest req)
Definition: http_server_plugin.cc:128
std::mutex route_mtx_
Definition: http_server_plugin.h:68
void remove(const std::string &url_regex_str)
Definition: http_server_plugin.cc:86
std::vector< RouterData > request_handlers_
Definition: http_server_plugin.h:63
void append(const std::string &url_regex_str, std::unique_ptr< BaseRequestHandler > cb)
request router
Definition: http_server_plugin.cc:78
void set_default_route(std::unique_ptr< BaseRequestHandler > cb)
Definition: http_server_plugin.cc:117
base class of all http request handler threads
Definition: http_server_plugin.h:83
EventHttp event_http_
Definition: http_server_plugin.h:119
HttpRequestThread(HttpRequestThread &&object)
Definition: http_server_plugin.h:94
void wait_and_dispatch()
Definition: http_server_plugin.cc:185
void initialization_finished()
Definition: http_server_plugin.cc:228
void break_dispatching()
Definition: http_server_plugin.cc:197
void accept_socket()
Definition: http_server_plugin.cc:170
bool is_initalized() const
Definition: http_server_plugin.cc:219
void set_request_router(HttpRequestRouter &router)
Definition: http_server_plugin.cc:176
void wait_until_ready()
Definition: http_server_plugin.cc:208
static void on_event_loop_ready(native_handle_type, short, void *)
Definition: http_server_plugin.cc:212
WaitableMonitor< bool > initialized_
Definition: http_server_plugin.h:121
native_handle_type get_socket_fd()
Definition: http_server_plugin.h:102
EventBase event_base_
Definition: http_server_plugin.h:118
native_handle_type accept_fd_
Definition: http_server_plugin.h:120
HttpRequestThread()
Definition: http_server_plugin.h:85
EventBaseSocket native_handle_type
Definition: http_server_plugin.h:100
a HTTP request and response.
Definition: http_request.h:453
Definition: http_server_plugin.h:124
std::string address_
Definition: http_server_plugin.h:150
HttpServer(const HttpServer &)=delete
net::io_context io_ctx_
Definition: http_server_plugin.h:154
virtual ~HttpServer()
Definition: http_server_plugin.h:137
HttpServer & operator=(HttpServer &&)=delete
void remove_route(const std::string &url_regex)
Definition: http_server_plugin.cc:417
HttpRequestRouter request_router_
Definition: http_server_plugin.h:152
HttpServer(const char *address, uint16_t port)
Definition: http_server_plugin.h:126
HttpServer & operator=(const HttpServer &)=delete
std::vector< HttpRequestThread > thread_contexts_
Definition: http_server_plugin.h:149
void join_all()
Definition: http_server_plugin.cc:322
void stop()
Definition: http_server_plugin.cc:363
HttpServer(HttpServer &&)=delete
uint16_t port_
Definition: http_server_plugin.h:151
net::ip::tcp::acceptor listen_sock_
Definition: http_server_plugin.h:155
std::vector< std::thread > sys_threads_
Definition: http_server_plugin.h:157
void add_route(const std::string &url_regex, std::unique_ptr< BaseRequestHandler > cb)
Definition: http_server_plugin.cc:407
virtual void start(size_t max_threads)
Definition: http_server_plugin.cc:332
HttpRequestRouter & request_router()
Definition: http_server_plugin.h:146
Monitor can be waited for.
Definition: monitor.h:62
Definition: socket.h:1412
Definition: io_context.h:61
evutil_socket_t EventBaseSocket
Definition: http_common.h:99
const int kEventBaseInvalidSocket
Definition: http_common.h:107
std::bitset< Pos::_LAST+1 > Bitset
Definition: http_request.h:268
Definition: gcs_xcom_synode.h:64
std::set< Key, Compare, ut::allocator< Key > > set
Specialization of set which uses ut_allocator.
Definition: ut0new.h:2882
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
Definition: http_server_plugin.h:58
std::regex url_regex
Definition: http_server_plugin.h:60
std::string url_regex_str
Definition: http_server_plugin.h:59
std::unique_ptr< BaseRequestHandler > handler
Definition: http_server_plugin.h:61