MySQL 8.0.39
Source Code Documentation
rest_api_utils.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2024, 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 MYSQLROUTER_REST_API_UTILS_INCLUDED
27#define MYSQLROUTER_REST_API_UTILS_INCLUDED
28
29#include <chrono>
30#include <map>
31#include <string>
32
33#ifdef RAPIDJSON_NO_SIZETYPEDEFINE
34#include "my_rapidjson_size_t.h"
35#endif
36
37#include <rapidjson/document.h>
38
39#include "mysql/harness/utility/string.h" // string_format()
41
42/**
43 * send a JsonProblem HTTP response.
44 *
45 * RFC 7807 defines `application/problem+json`:
46 *
47 * - title
48 * - description
49 * - instance
50 * - detail
51 *
52 * @param req HttpRequest object to send error-msg
53 * @param status_code HTTP Status code of the problem message
54 * @param fields fields on the problem message
55 */
57 const std::map<std::string, std::string> &fields);
58
59/**
60 * ensure HTTP method is allowed.
61 *
62 * sends HTTP-response with status 405 if method is not allowed and sets `Allow`
63 * HTTP header.
64 *
65 * @returns success
66 * @retval true if HTTP method is allowed
67 * @retval false if HTTP is not allowed and HTTP response has been sent
68 */
69bool ensure_http_method(HttpRequest &req, HttpMethod::Bitset allowed_methods);
70
71/**
72 * ensure request is authenticated.
73 *
74 * sends HTTP-response with status 401 if authentication was not successful.
75 *
76 * @returns success
77 * @retval true if request is authenticaticated
78 * @retval false if authentication was not successful and HTTP response has been
79 * sent
80 */
81bool ensure_auth(HttpRequest &req, const std::string require_realm);
82
83/**
84 * ensure request has no parameters.
85 *
86 * sends HTTP-response with status 400 if request contained a query string.
87 *
88 * @returns success
89 * @retval true if request did not contain a query-string
90 * @retval false if request contained a query-string and HTTP response has
91 * been sent
92 */
94
95/**
96 * send a JsonProblem "Not Found" error.
97 *
98 * @param req HttpRequest object to send error-msg
99 */
101
102/**
103 * ensure resource has modified since client received it.
104 *
105 * sends HTTP-response with status 304 if client has a newer version that
106 *
107 * @returns success
108 * @retval true if resource is modified since client received it.
109 * @retval false if client has the same resource and HTTP response has been
110 * sent
111 */
112bool ensure_modified_since(HttpRequest &req, time_t last_modified);
113
114/**
115 * send json document as HTTP response.
116 *
117 * Content-Type must be sent before the function is called.
118 *
119 * @param req HttpRequest object to send error-msg
120 * @param status_code HTTP Status code of the problem message
121 * @param json_doc json document to send as response
122 */
124 const rapidjson::Document &json_doc);
125
126/**
127 * format a timepoint as json-value (date-time format).
128 */
129template <class Encoding, class AllocatorType>
130rapidjson::GenericValue<Encoding, AllocatorType> json_value_from_timepoint(
131 std::chrono::time_point<std::chrono::system_clock> tp,
132 AllocatorType &allocator) {
133 time_t cur = std::chrono::system_clock::to_time_t(tp);
134 struct tm cur_gmtime;
135#ifdef _WIN32
136 gmtime_s(&cur_gmtime, &cur);
137#else
138 gmtime_r(&cur, &cur_gmtime);
139#endif
140 auto usec = std::chrono::duration_cast<std::chrono::microseconds>(
141 tp - std::chrono::system_clock::from_time_t(cur));
142
143 std::string iso8601_datetime{mysql_harness::utility::string_format(
144 "%04d-%02d-%02dT%02d:%02d:%02d.%06ldZ", cur_gmtime.tm_year + 1900,
145 cur_gmtime.tm_mon + 1, cur_gmtime.tm_mday, cur_gmtime.tm_hour,
146 cur_gmtime.tm_min, cur_gmtime.tm_sec,
147 // cast to long int as it is "longlong" on 32bit, and "long" on
148 // 64bit platforms, but we only have a range of 0-999
149 static_cast<long int>(usec.count()))};
150
151 return {iso8601_datetime.c_str(), iso8601_datetime.size(), allocator};
152}
153
154#endif
a HTTP request and response.
Definition: http_request.h:453
Define rapidjson::SizeType to be std::size_t.
std::bitset< Pos::_LAST+1 > Bitset
Definition: http_request.h:268
int key_type
Definition: http_request.h:50
HARNESS_EXPORT std::string string_format(const char *format,...)
Definition: utilities.cc:64
void send_json_document(HttpRequest &req, HttpStatusCode::key_type status_code, const rapidjson::Document &json_doc)
send json document as HTTP response.
Definition: rest_api_utils.cc:39
bool ensure_modified_since(HttpRequest &req, time_t last_modified)
ensure resource has modified since client received it.
Definition: rest_api_utils.cc:159
bool ensure_auth(HttpRequest &req, const std::string require_realm)
ensure request is authenticated.
Definition: rest_api_utils.cc:130
rapidjson::GenericValue< Encoding, AllocatorType > json_value_from_timepoint(std::chrono::time_point< std::chrono::system_clock > tp, AllocatorType &allocator)
format a timepoint as json-value (date-time format).
Definition: rest_api_utils.h:130
void send_rfc7807_not_found_error(HttpRequest &req)
send a JsonProblem "Not Found" error.
Definition: rest_api_utils.cc:83
bool ensure_no_params(HttpRequest &req)
ensure request has no parameters.
Definition: rest_api_utils.cc:146
bool ensure_http_method(HttpRequest &req, HttpMethod::Bitset allowed_methods)
ensure HTTP method is allowed.
Definition: rest_api_utils.cc:91
void send_rfc7807_error(HttpRequest &req, HttpStatusCode::key_type status_code, const std::map< std::string, std::string > &fields)
send a JsonProblem HTTP response.
Definition: rest_api_utils.cc:61