MySQL 9.4.0
Source Code Documentation
http_request_router.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2025, 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_REQUEST_ROUTER_H_
27#define ROUTER_SRC_HTTP_SRC_HTTP_REQUEST_ROUTER_H_
28
29#include <map>
30#include <memory>
31#include <optional>
32#include <shared_mutex>
33#include <string>
34#include <vector>
35
36#include "http/base/request.h"
43
46 public:
48 using BaseRequestHandlerPtr = std::shared_ptr<http::base::RequestHandler>;
49
50 void register_regex_handler(const std::string &url_host,
51 const std::string &url_regex_str,
52 std::unique_ptr<RequestHandler> cb);
53 void register_direct_match_handler(
54 const std::string &url_host,
55 const ::http::base::UriPathMatcher &uri_path_matcher,
56 std::unique_ptr<RequestHandler> cb);
57 void unregister_handler(const void *handler_id);
58
59 void set_default_route(std::unique_ptr<RequestHandler> cb);
60 void clear_default_route();
61 void route(http::base::Request &req) override;
62
63 void require_realm(const std::string &realm) { require_realm_ = realm; }
64
65 private:
67 public:
69 : matcher_(std::make_unique<mysql_harness::RegexMatcher>(url_pattern)),
70 url_pattern_(std::move(url_pattern)),
71 handler_(std::move(handler)) {}
72
73 bool matches(std::string_view input) const;
74
75 const std::string &url_pattern() const { return url_pattern_; }
76
77 BaseRequestHandlerPtr handler() const { return handler_; }
78
79 private:
80 std::unique_ptr<mysql_harness::RegexMatcher> matcher_;
81 std::string url_pattern_;
83 };
84
86 public:
88 // nullopt means the element is optional and matches any string, it only
89 // makes sense as a last element
90 using UrlPathElem = std::optional<std::string>;
91 std::vector<UrlPathElem> path_elements;
93
94 bool operator<(const UrlPathKey &other) const;
95
96 auto str() const {
97 std::string result;
98 for (const auto &el : path_elements) {
99 if (el)
100 result += "/" + *el;
101 else
102 result += "/*";
103 }
104
105 if (allow_trailing_slash) result += "[/]";
106
107 return result;
108 }
109 };
110
111 static UrlPathKey path_key_from_matcher(
112 const ::http::base::UriPathMatcher &url_path_matcher);
113
114 struct PathHandler {
117 };
118
119 RouteDirectMatcher(const PathHandler &path_handler) {
120 add_handler(path_handler);
121 }
122
123 BaseRequestHandlerPtr handler(const std::string_view path) const;
124 std::vector<PathHandler> &handlers() { return handlers_; }
125 void add_handler(const PathHandler &path_handler);
126 bool has_handler(const void *handler_id) const;
127 std::string get_handler_path(const void *handler_id) const;
128 size_t remove_handler(const void *handler_id);
129
130 private:
131 std::vector<PathHandler> handlers_;
132 };
133
134 // if no routes are specified, return 404
135 void handler_not_found(http::base::Request &req);
136
137 BaseRequestHandlerPtr find_route_handler(std::string_view url_host,
138 std::string_view path);
139
140 BaseRequestHandlerPtr find_direct_match_route_handler(
141 std::string_view url_host, std::string_view path);
142
143 BaseRequestHandlerPtr find_regex_route_handler(std::string_view url_host,
144 std::string_view path);
145
146 void unregister_regex_handler(const void *handler_id);
147 void unregister_direct_match_handler(const void *handler_id);
148
150 std::map<std::string, std::map<UrlPathKey, RouteDirectMatcher>, std::less<>>
152
153 std::map<std::string, std::vector<RouteRegexMatcher>, std::less<>>
155
157 std::string require_realm_;
158
159 std::shared_mutex route_mtx_;
160
161 friend class HttpRequestRouterDirectMatchTest;
162};
163
164#endif // ROUTER_SRC_HTTP_SRC_HTTP_REQUEST_ROUTER_H_
Definition: http_request_router.h:85
std::vector< PathHandler > & handlers()
Definition: http_request_router.h:124
std::vector< PathHandler > handlers_
Definition: http_request_router.h:131
RouteDirectMatcher(const PathHandler &path_handler)
Definition: http_request_router.h:119
Definition: http_request_router.h:66
std::string url_pattern_
Definition: http_request_router.h:81
RouteRegexMatcher(std::string url_pattern, BaseRequestHandlerPtr handler)
Definition: http_request_router.h:68
BaseRequestHandlerPtr handler() const
Definition: http_request_router.h:77
std::unique_ptr< mysql_harness::RegexMatcher > matcher_
Definition: http_request_router.h:80
BaseRequestHandlerPtr handler_
Definition: http_request_router.h:82
const std::string & url_pattern() const
Definition: http_request_router.h:75
Definition: http_request_router.h:45
std::shared_mutex route_mtx_
Definition: http_request_router.h:159
void require_realm(const std::string &realm)
Definition: http_request_router.h:63
std::shared_ptr< http::base::RequestHandler > BaseRequestHandlerPtr
Definition: http_request_router.h:48
std::string require_realm_
Definition: http_request_router.h:157
std::map< std::string, std::vector< RouteRegexMatcher >, std::less<> > request_regex_handlers_
Definition: http_request_router.h:154
BaseRequestHandlerPtr default_route_
Definition: http_request_router.h:156
std::map< std::string, std::map< UrlPathKey, RouteDirectMatcher >, std::less<> > request_direct_handlers_
Definition: http_request_router.h:151
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4666
Definition: request_handler.h:36
Definition: request.h:44
Definition: request_handler_interface.h:36
virtual void route(http::base::Request &request)=0
#define HTTP_SERVER_LIB_EXPORT
Definition: http_server_lib_export.h:15
static char * path
Definition: mysqldump.cc:150
static bool cb(unsigned long long new_value)
Definition: option_usage.cc:45
Unique_ptr< T, std::nullptr_t > make_unique(size_t size)
In-place constructs a new unique pointer with no specific allocator and with array type T.
Oauth2Handler::RequestHandler RequestHandler
Definition: oauth2_facebook_handler.cc:45
HARNESS_EXPORT void unregister_handler(std::string name)
Unregister a handler.
Definition: registry.cc:505
Definition: common.h:44
constexpr bool operator<(const address_v4 &a, const address_v4 &b) noexcept
Definition: internet.h:166
Definition: gcs_xcom_synode.h:64
struct result result
Definition: result.h:34
Definition: http_request_router.h:114
BaseRequestHandlerPtr handler
Definition: http_request_router.h:116
::http::base::UriPathMatcher path_matcher
Definition: http_request_router.h:115
Definition: http_request_router.h:87
bool allow_trailing_slash
Definition: http_request_router.h:92
std::optional< std::string > UrlPathElem
Definition: http_request_router.h:90
std::vector< UrlPathElem > path_elements
Definition: http_request_router.h:91
auto str() const
Definition: http_request_router.h:96
Definition: uri_path_matcher.h:40
Definition: result.h:30