MySQL 8.1.0
Source Code Documentation
http_server_component.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_COMPONENT_INCLUDED
26#define MYSQLROUTER_HTTP_SERVER_COMPONENT_INCLUDED
27
28#include <mutex>
29
32
33class HttpServer;
34
36 public:
37 void call(HttpRequest &req, void *me) {
38 auto *my = static_cast<BaseRequestHandler *>(me);
39 my->handle_request(req);
40 }
41
42 virtual void handle_request(HttpRequest &req) = 0;
43
44 BaseRequestHandler() = default;
45 explicit BaseRequestHandler(const BaseRequestHandler &) = delete;
48};
49
51 public:
52 static HttpServerComponent &get_instance();
53 void init(std::shared_ptr<HttpServer> srv);
54 void add_route(const std::string &url_regex,
55 std::unique_ptr<BaseRequestHandler> cb);
56 void remove_route(const std::string &url_regex);
57
58 private:
59 // disable copy, as we are a single-instance
61 void operator=(HttpServerComponent const &) = delete;
62
63 struct RouterData {
64 std::string url_regex_str;
65 std::unique_ptr<BaseRequestHandler> handler;
66 };
67
68 std::mutex rh_mu; // request handler mutex
69 std::vector<RouterData> request_handlers_;
70
71 std::weak_ptr<HttpServer> srv_;
72
74};
75
76class HttpAuthRealm;
77
78/**
79 * high-level Authentication frontend.
80 *
81 * bridges HttpRequest with the HttpAuthRealm's
82 */
84 public:
85 /**
86 * require Authorization.
87 *
88 * @returns if request has been handled
89 * @retval true request handled
90 */
91 static bool require_auth(HttpRequest &req,
92 std::shared_ptr<HttpAuthRealm> realm);
93};
94
95#endif
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:570
Definition: http_server_component.h:35
BaseRequestHandler & operator=(const BaseRequestHandler &)=delete
void call(HttpRequest &req, void *me)
Definition: http_server_component.h:37
virtual void handle_request(HttpRequest &req)=0
BaseRequestHandler(const BaseRequestHandler &)=delete
BaseRequestHandler()=default
virtual ~BaseRequestHandler()
Authentication Realm.
Definition: http_auth_realm.h:46
high-level Authentication frontend.
Definition: http_server_component.h:83
a HTTP request and response.
Definition: http_request.h:452
Definition: http_server_component.h:50
std::mutex rh_mu
Definition: http_server_component.h:68
HttpServerComponent(HttpServerComponent const &)=delete
std::weak_ptr< HttpServer > srv_
Definition: http_server_component.h:71
HttpServerComponent()=default
std::vector< RouterData > request_handlers_
Definition: http_server_component.h:69
void operator=(HttpServerComponent const &)=delete
Definition: http_server_plugin.h:123
#define HTTP_SERVER_EXPORT
Definition: http_server_export.h:39
Definition: srv0dynamic_procedures.h:47
Definition: http_server_component.h:63
std::string url_regex_str
Definition: http_server_component.h:64
std::unique_ptr< BaseRequestHandler > handler
Definition: http_server_component.h:65