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