MySQL 9.4.0
Source Code Documentation
routine_utilities.h
Go to the documentation of this file.
1/*
2 Copyright (c) 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 _MYSQL_REST_SERVICE_SRC_MRS_ENDPOINT_HANDLER_ROUTINE_UTILITIES_H_
27#define _MYSQL_REST_SERVICE_SRC_MRS_ENDPOINT_HANDLER_ROUTINE_UTILITIES_H_
28
29#include <memory>
30#include <string>
31#include <vector>
32#include "helper/http/url.h"
34#include "mrs/http/error.h"
38
39namespace mrs {
40namespace endpoint {
41namespace handler {
42
44
45inline std::string get_endpoint_url(
46 std::weak_ptr<mrs::endpoint::DbObjectEndpoint> &wp) {
47 auto locked = lock_or_throw_unavail(wp);
48 return locked->get_url().join();
49}
50
51inline std::string get_path_after_object_name(
52 const ::http::base::Uri &base_uri, const ::http::base::Uri &requests_uri) {
53 const auto &elements_path = requests_uri.get_path_elements();
54 const auto &elements_base = base_uri.get_path_elements();
55
56 if (elements_path.size() > elements_base.size())
57 return elements_path[elements_base.size()];
58
59 return {};
60}
61
62inline std::string get_path_after_object_name(
63 std::weak_ptr<mrs::endpoint::DbObjectEndpoint> &wp,
64 const ::http::base::Uri &requests_uri) {
65 auto endpoint = lock_or_throw_unavail(wp);
66 return get_path_after_object_name(endpoint->get_url(), requests_uri);
67}
68
70 const char *sql_state) {
71 static const std::string k_state_with_user_defined_error = "45000";
72 if (!sql_state) throw e;
73
74 log_debug("While handling a routine, received a mysql-error with state: %s",
75 sql_state);
76 if (k_state_with_user_defined_error != sql_state) {
77 throw e;
78 }
79 // 5000 is the offset for HTTPStatus errors,
80 // Still first HTTP status begins with 100 code,
81 // because of that we are validating the value
82 // not against 5000, but 5100.
83 if (e.code() < 5100 || e.code() >= 5600) {
84 throw e;
85 }
86 helper::json::MapObject map{{"message", e.message()}};
88 try {
90 } catch (...) {
91 throw e;
92 }
93 auto json = helper::json::to_string(map);
94 log_debug("routine - generated custom HTTPstats + message:%s", json.c_str());
95 return HttpResult(status, std::move(json), HttpResult::Type::typeJson);
96}
97
99 bool required) {
100 // this will not be set if the endpoint does not require auth, even if the
101 // request IS authenticated
102 if (!ctxt->user.has_user_id) {
103 if (required) throw http::Error(HttpStatusCode::Forbidden);
104 return {};
105 }
106
107 mysqlrouter::sqlstring sql("?");
108 sql << to_string(ctxt->user.user_id);
109
110 return sql;
111}
112
113inline std::string get_user_name(rest::RequestContext *ctxt) {
114 // this will not be set if the endpoint does not require auth, even if the
115 // request IS authenticated
116 if (!ctxt->user.has_user_id) {
118 }
119
120 return ctxt->user.name;
121}
122
124 const std::vector<database::entry::Field> &param_fields,
125 const rapidjson::Document &doc) {
126 for (auto el : helper::json::member_iterator(doc)) {
127 auto key = el.first;
128 if (!helper::container::has_if(param_fields, [key](const auto &v) {
129 return v.mode != database::entry::Field::modeOut && v.name == key;
130 })) {
132 "Not allowed parameter:" + std::string{key});
133 }
134 }
135}
136
137} // namespace handler
138} // namespace endpoint
139} // namespace mrs
140
141#endif /* _MYSQL_REST_SERVICE_SRC_MRS_ENDPOINT_HANDLER_ROUTINE_UTILITIES_H_ */
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4666
Definition: error.h:37
Definition: mysql_session.h:289
std::string message() const
Definition: mysql_session.h:304
unsigned int code() const
Definition: mysql_session.h:303
Definition: utils_sqlstring.h:67
Log log_debug(std::cout, "DEBUG")
mrs::rest::Handler::HttpResult HttpResult
Definition: handler_content_file.cc:43
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
auto get_default_status_text(key_type status)
Definition: status_code.h:114
bool has_if(const Container &c, Find_if &&find_if)
Definition: generic.h:106
std::string to_string(const rapidjson::Value &value)
Definition: rapid_json_to_text.h:67
std::map< std::string, std::string > MapObject
std::map that represents simple JSON object.
Definition: to_string.h:44
auto member_iterator(Obj &o)
Definition: rapid_json_iterator.h:90
@ typeJson
Definition: media_type.h:47
constexpr key_type Forbidden
Definition: status_code.h:67
constexpr key_type BadRequest
Definition: status_code.h:64
Request::Uri Uri
Definition: request.cc:36
std::shared_ptr< Type > lock_or_throw_unavail(std::weak_ptr< Type > &endpoint)
Definition: utilities.h:52
void check_input_parameters(const std::vector< database::entry::Field > &param_fields, const rapidjson::Document &doc)
Definition: routine_utilities.h:123
std::string get_endpoint_url(std::weak_ptr< mrs::endpoint::DbObjectEndpoint > &wp)
Definition: routine_utilities.h:45
mysqlrouter::sqlstring get_user_id(rest::RequestContext *ctxt, bool required)
Definition: routine_utilities.h:98
std::string get_user_name(rest::RequestContext *ctxt)
Definition: routine_utilities.h:113
HandlerAuthorizeAuthApps::HttpResult HttpResult
Definition: handler_authorize_auth_apps.cc:45
HttpResult handler_mysqlerror(const mysqlrouter::MySQLSession::Error &e, const char *sql_state)
Definition: routine_utilities.h:69
std::string get_path_after_object_name(const ::http::base::Uri &base_uri, const ::http::base::Uri &requests_uri)
Definition: routine_utilities.h:51
Definition: authorize_manager.h:48
std::map< Key, Value, Compare, ut::allocator< std::pair< const Key, Value > > > map
Specialization of map which uses ut_allocator.
Definition: ut0new.h:2894
required string key
Definition: replication_asynchronous_connection_failover.proto:60
required uint32 status
Definition: replication_asynchronous_connection_failover.proto:61
@ modeOut
Definition: field.h:45
Definition: http_result.h:37
int HttpStatus
Definition: http_result.h:39
Definition: request_context.h:47
AuthUser user
Definition: request_context.h:73