MySQL 9.3.0
Source Code Documentation
url.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_REST_MRS_SRC_MRS_HTTP_URL_PATH_QUERY_H_
27#define ROUTER_SRC_REST_MRS_SRC_MRS_HTTP_URL_PATH_QUERY_H_
28
29#include <cstdlib>
30#include <cstring>
31#include <map>
32#include <optional>
33#include <string>
34#include <string_view>
35#include <vector>
36
38#include "http/base/uri.h"
39#include "mrs/http/error.h"
40
41namespace helper {
42namespace http {
43
44class Url {
45 public:
46 struct Key {
47 uint32_t index;
48 std::string name;
49 };
52 using Keys = std::vector<std::string>;
53 using Values = std::vector<std::string>;
54
55 Url(const HttpUri &uri) : uri_{uri} {}
56
57 static void append_query_parameter(HttpUri &uri, const std::string &key,
58 const std::string &value) {
60 }
61
62 std::string get_path() { return uri_.get_path(); }
63
64 std::string get_query() { return uri_.get_query(); }
65
67
68 bool remove_query_parameter(const std::string &key) {
69 return 0 != uri_.get_query_elements().erase(key);
70 }
71
72 std::string get_query_parameter(const std::string &key) const {
73 std::string result;
75
76 return result;
77 }
78
79 bool get_if_query_parameter(const std::string &key,
80 std::optional<std::string> *value) const {
81 const auto &query = uri_.get_query_elements();
82 auto it = query.find(key);
83 if (it != query.end()) {
84 *value = it->second;
85 return true;
86 }
87
88 return false;
89 }
90
91 bool get_if_query_parameter(const std::string &key,
92 std::string *value) const {
94 }
95
96 bool is_query_parameter(const std::string &key) const {
97 return 0 != uri_.get_query_elements().count(key);
98 }
99
100 static std::string get_query_parameter(const HttpUri &uri,
101 const std::string &key) {
102 std::string result;
104
105 return result;
106 }
107
108 static uint64_t to_uint64(const char *value,
109 const char *conversion_failure_message) {
110 if (*value == '-') // stoull doesn't throw if value<0
111 throw ::mrs::http::Error(HttpStatusCode::BadRequest,
112 conversion_failure_message);
113 try {
114 return std::stoull(value);
115 } catch (...) {
116 // The exception generated by `stdoull` doesn't have any useful
117 // informations.
118 throw ::mrs::http::Error(HttpStatusCode::BadRequest,
119 conversion_failure_message);
120 }
121 }
122
123 static void parse_offset_limit(const Parameters &query, uint64_t *out_offset,
124 uint64_t *out_limit) {
125 if (query.empty()) return;
126
127 auto of = query.find("offset");
128 auto li = query.find("limit");
129
130 if (of != query.end()) {
131 *out_offset =
132 to_uint64(of->second.c_str(), "Invalid value for 'offset' parameter");
133 }
134
135 if (li != query.end()) {
136 *out_limit =
137 to_uint64(li->second.c_str(), "Invalid value for 'limit' parameter");
138 }
139 }
140
141 void parse_offset_limit(uint64_t *out_offset, uint64_t *out_limit) const {
142 parse_offset_limit(uri_.get_query_elements(), out_offset, out_limit);
143 }
144
145 public:
147};
148
149} // namespace http
150} // namespace helper
151
152#endif // ROUTER_SRC_REST_MRS_SRC_MRS_HTTP_URL_PATH_QUERY_H_
Definition: url.h:44
Url(const HttpUri &uri)
Definition: url.h:55
bool is_query_parameter(const std::string &key) const
Definition: url.h:96
std::vector< std::string > Keys
Definition: url.h:52
std::string get_query_parameter(const std::string &key) const
Definition: url.h:72
HttpUri uri_
Definition: url.h:146
std::string get_path()
Definition: url.h:62
bool remove_query_parameter(const std::string &key)
Definition: url.h:68
bool get_if_query_parameter(const std::string &key, std::optional< std::string > *value) const
Definition: url.h:79
std::string get_query()
Definition: url.h:64
static std::string get_query_parameter(const HttpUri &uri, const std::string &key)
Definition: url.h:100
bool get_if_query_parameter(const std::string &key, std::string *value) const
Definition: url.h:91
static void parse_offset_limit(const Parameters &query, uint64_t *out_offset, uint64_t *out_limit)
Definition: url.h:123
static uint64_t to_uint64(const char *value, const char *conversion_failure_message)
Definition: url.h:108
void parse_offset_limit(uint64_t *out_offset, uint64_t *out_limit) const
Definition: url.h:141
static void append_query_parameter(HttpUri &uri, const std::string &key, const std::string &value)
Definition: url.h:57
std::vector< std::string > Values
Definition: url.h:53
HttpUri::QueryElements Parameters
Definition: url.h:51
Parameters get_query_elements()
Definition: url.h:66
Definition: uri.h:40
virtual std::string get_query() const
Definition: uri.cc:172
std::map< std::string, std::string > QueryElements
Definition: uri.h:42
virtual QueryElements & get_query_elements()
Definition: uri.cc:178
virtual std::string get_path() const
get path part of the URI.
Definition: uri.cc:160
static char * query
Definition: myisam_ftdump.cc:47
bool get_value(Container &container, const typename Container::key_type &key, typename Container::mapped_type *out)
Definition: map.h:46
Definition: cache.h:33
constexpr key_type BadRequest
Definition: status_code.h:64
Request::Uri Uri
Definition: request.cc:36
Definition: connection.h:55
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
struct result result
Definition: result.h:34
required string key
Definition: replication_asynchronous_connection_failover.proto:60
Definition: url.h:46
uint32_t index
Definition: url.h:47
std::string name
Definition: url.h:48
Definition: result.h:30