MySQL 9.5.0
Source Code Documentation
rest_request_handler.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 ROUTER_SRC_REST_MRS_SRC_MRS_REST_REQUEST_HANDLER_H_
27#define ROUTER_SRC_REST_MRS_SRC_MRS_REST_REQUEST_HANDLER_H_
28
29#include <map>
30#include <string>
31
34#include "mrs/http/error.h"
38#include "mrs/rest/handler.h"
40
41namespace mrs {
42namespace rest {
43
45 const std::vector<database::entry::AuthPrivilege> &privileges,
46 const UniversalId &service_id, const std::string &service_path,
47 const UniversalId &schema_id, const std::string &schema_path,
48 const UniversalId &db_object_id, const std::string &db_object_path);
49
51 public:
55 using HandlerPtr = std::weak_ptr<RestHandler>;
61 using ObjectKeyValue = std::map<std::string, std::string>;
63
64 public:
67 const bool may_log_requests)
68 : rest_handler_{rest_handler},
69 auth_manager_{auth_manager},
70 may_log_requests_{may_log_requests} {}
71
73 void trace_error(const http::Error &e);
75 void trace_error(const RestError &e);
76 void trace_error(const std::exception &e);
78
80 HttpMethod::key_type method, const std::string &path,
81 const HttpHeaders &headers, HttpBuffer &buffer) const;
82
84 RequestContext &ctxt);
85
86 void handle_request(::http::base::Request &req) override;
87
88 static std::string mask_password(const std::string &input);
89
90 private:
92
95
97 return err;
98 }
99
102 return {HttpStatusCode::BadRequest, err.what()};
103 }
104
106 return {HttpStatusCode::BadRequest, err.what()};
107 }
108
111 }
112
113 static http::Error err_to_http_error(const std::invalid_argument &err) {
114 return {HttpStatusCode::BadRequest, err.what()};
115 }
116
117 static http::Error err_to_http_error(const std::exception &) {
119 }
120
123 ObjectKeyValue result{{"message", e.message()},
124 {"what", e.what()},
125 {"sqlcode", std::to_string(e.code())}};
126 return result;
127 }
128
130 const std::exception &e) {
131 ObjectKeyValue result{{"message", converted.message}, {"what", e.what()}};
132 return result;
133 }
134
136 const http::Error &) {
137 ObjectKeyValue result{{"message", converted.message}};
138 return result;
139 }
140
141 template <typename Err>
143
145
146 void send_reply(HttpRequest &req, int status_code);
147
148 void send_reply(HttpRequest &req, int status_code,
149 const std::string &status_text);
150
151 void send_reply(HttpRequest &req, int status_code,
152 const std::string &status_text, HttpBuffer &buffer);
153
155 HttpStatusCode::key_type status_code,
156 const std::map<std::string, std::string> &fields);
157
159 HttpStatusCode::key_type status_code,
160 const rapidjson::Document &json_doc);
161
165};
166
167} // namespace rest
168} // namespace mrs
169
170#endif // ROUTER_SRC_REST_MRS_SRC_MRS_REST_HANDLER_H_
Definition: cache_manager.h:41
MySqlCacheManager::CachedObject CachedObject
Definition: mysql_cache_manager.h:92
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4741
headers of a HTTP response/request.
Definition: headers.h:43
Definition: io_buffer.h:41
Definition: request_handler.h:36
Definition: request.h:44
Definition: errors.h:35
Definition: error.h:53
Definition: error.h:37
std::string message
Definition: error.h:50
Definition: authorize_manager.h:58
Definition: rest_error.h:51
Definition: rest_handler.h:77
Definition: rest_handler.h:62
User visible error message.
Definition: rest_error.h:42
Definition: rest_handler.h:141
Definition: rest_request_handler.h:50
static http::Error err_to_http_error(const mysqlrouter::MySQLSession::Error &err)
void handle_error(RestHandler *handler, RequestContext *ctxt, const Err &err)
void trace_error(const http::ErrorChangeResponse &e)
ObjectKeyValue responose_encode_error(const http::Error &, const mysqlrouter::MySQLSession::Error &e)
Definition: rest_request_handler.h:121
void send_reply(HttpRequest &req, int status_code)
HandlerPtr rest_handler_
Definition: rest_request_handler.h:162
ObjectKeyValue responose_encode_error(const http::Error &converted, const http::Error &)
Definition: rest_request_handler.h:135
static http::Error err_to_http_error(const mrs::database::JSONInputError &err)
Definition: rest_request_handler.h:100
void trace_error(const RestError &e)
void trace_http(const char *type, interface::ReqRes &options, HttpMethod::key_type method, const std::string &path, const HttpHeaders &headers, HttpBuffer &buffer) const
const bool may_log_requests_
Definition: rest_request_handler.h:164
std::map< std::string, std::string > ObjectKeyValue
Definition: rest_request_handler.h:61
mysql_harness::logging::DomainLogger logger_
Definition: rest_request_handler.h:91
static std::string mask_password(const std::string &input)
RestRequestHandler(HandlerPtr rest_handler, mrs::interface::AuthorizeManager *auth_manager, const bool may_log_requests)
Definition: rest_request_handler.h:65
void send_reply(HttpRequest &req, int status_code, const std::string &status_text)
void trace_error(const http::Error &e)
static const http::Error & err_to_http_error(const http::Error &err)
Definition: rest_request_handler.h:96
void send_reply(HttpRequest &req, int status_code, const std::string &status_text, HttpBuffer &buffer)
database::entry::AuthUser AuthUser
Definition: rest_request_handler.h:53
mrs::interface::AuthorizeManager * auth_manager_
Definition: rest_request_handler.h:163
Handler::HttpResult handle_request_impl(RestHandler *handler, RequestContext &ctxt)
void send_rfc7807_error(HttpRequest &req, HttpStatusCode::key_type status_code, const std::map< std::string, std::string > &fields)
static http::Error err_to_http_error(const RestError &err)
Definition: rest_request_handler.h:105
static http::Error err_to_http_error(const std::invalid_argument &err)
Definition: rest_request_handler.h:113
const Options & get_options()
void handle_request(::http::base::Request &req) override
static http::Error err_to_http_error(const std::exception &)
Definition: rest_request_handler.h:117
ObjectKeyValue responose_encode_error(const http::Error &converted, const std::exception &e)
Definition: rest_request_handler.h:129
static http::Error err_to_http_error(const ETagMismatch &err)
Definition: rest_request_handler.h:109
void trace_error(const mysqlrouter::MySQLSession::Error &e)
void trace_error(const std::exception &e)
std::weak_ptr< RestHandler > HandlerPtr
Definition: rest_request_handler.h:55
void send_json_document(HttpRequest &req, HttpStatusCode::key_type status_code, const rapidjson::Document &json_doc)
void trace_error(const Handler::HttpResult &e)
Definition: mysql_session.h:289
std::string message() const
Definition: mysql_session.h:304
unsigned int code() const
Definition: mysql_session.h:303
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
static char * path
Definition: mysqldump.cc:150
constexpr pos_type Options
Definition: method.h:48
int key_type
Definition: method.h:38
int key_type
Definition: status_code.h:36
constexpr key_type InternalError
Definition: status_code.h:93
constexpr key_type PreconditionFailed
Definition: status_code.h:76
constexpr key_type BadRequest
Definition: status_code.h:64
Request::IOBuffer IOBuffer
Definition: request.cc:32
Request::Headers Headers
Definition: request.cc:34
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:924
mrs::database::entry::AuthUser AuthUser
Definition: scram_handler.cc:54
mrs::interface::RestError RestError
Definition: filter_object_generator.cc:52
uint32_t do_privilege_check(const std::vector< database::entry::AuthPrivilege > &privileges, const UniversalId &service_id, const std::string &service_path, const UniversalId &schema_id, const std::string &schema_path, const UniversalId &db_object_id, const std::string &db_object_path)
Definition: authorize_manager.h:48
mrs::database::entry::UniversalId UniversalId
Definition: universal_id.h:33
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
Definition: options.cc:57
struct result result
Definition: result.h:34
required string type
Definition: replication_group_member_actions.proto:34
RestClient::Request Request
Definition: rest_client.cc:29
Definition: http_result.h:37
Definition: request_context.h:47
Definition: result.h:30