MySQL 8.4.2
Source Code Documentation
uri.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2015, 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 URI_ROUTING_INCLUDED
27#define URI_ROUTING_INCLUDED
28
30
31#include <cstdint>
32#include <exception>
33#include <map>
34#include <stdexcept>
35#include <string>
36#include <tuple>
37#include <vector>
38#ifndef _WIN32
39#include <unistd.h>
40#endif
41
42namespace mysqlrouter {
43
44using URIAuthority = std::tuple<std::string, uint16_t, std::string,
45 std::string>; // host, port, username, password
46using URIPath = std::vector<std::string>;
47using URIQuery = std::map<std::string, std::string>;
48
49/** @class URIError
50 * @brief Exception when URI was not valid
51 *
52 */
53class URIError : public std::runtime_error {
54 public:
55 URIError(const char *msg, const std::string &uri, size_t position);
56 explicit URIError(const std::string &what_arg)
57 : std::runtime_error(what_arg) {}
58};
59
60/** @class URI
61 * @brief Parse and create URIs according to RFC3986
62 *
63 * This class will parse and make the elements of the URI
64 * available as members.
65 *
66 * Links:
67 * * (RFC 3986)[https://tools.ietf.org/html/rfc3986)
68 *
69 */
71 public:
72 /** @brief Delimiter used in the Query part */
73 static const char query_delimiter = '&';
74
75 /** @brief Default constructor
76 *
77 * Rootless URIs like "mailto:user@example.com" may be forbidden to make sure
78 * that simple "host:addr" doesn't get parsed as (scheme='host', path='addr')
79 *
80 * @param uri URI string to decode
81 * @param allow_path_rootless if parsing rootless URIs is allowed
82 * @param allow_schemeless define if schema is mandatory
83 * @param path_keep_last_slash parsing the URL keeps last slash
84 * @param query_single_parameter_when_cant_parse This parameter allows to
85 * handles query parameter that follows the RFC but is not accepted by default
86 * implementation of URL
87 */
88 URI(const std::string &uri, bool allow_path_rootless = true,
89 bool allow_schemeless = false, bool path_keep_last_slash = false,
90 bool query_single_parameter_when_cant_parse = false)
91 : scheme(),
92 host(),
93 port(0),
94 username(),
95 password(),
96 path(),
97 query(),
98 fragment(),
99 uri_(uri),
100 allow_path_rootless_(allow_path_rootless),
101 allow_schemeless_{allow_schemeless},
102 path_keep_last_slash_{path_keep_last_slash},
103 query_single_parameter_when_cant_parse_{
104 query_single_parameter_when_cant_parse} {
105 if (!uri.empty()) {
106 init_from_uri(uri);
107 }
108 }
109
110 bool operator==(const URI &u2) const;
111 bool operator!=(const URI &u2) const;
112
113 /** return string representation of the URI */
114 std::string str() const;
115
116 /** @brief overload */
117 URI() : URI("") {}
118
119 /** @brief Sets URI using the given URI string
120 *
121 * @param uri URI as string
122 */
123 void set_uri(const std::string &uri) { init_from_uri(uri); }
124
125 /** @brief Path part of the URI as string */
126 std::string get_path_as_string(bool needs_first_slash = true) const;
127
128 /** @brief Set the path part of the URI as string
129 *
130 * @param p path string to decode and store in URI object
131 */
132 void set_path_from_string(const std::string &p);
133
134 /** @brief Get the URIs path part */
135 std::string get_query_as_string() const;
136
137 /** @brief Set the URI query part by reparsing query string
138 *
139 * @param q query string to decode and store in URI object
140 */
141 void set_query_from_string(const std::string &q);
142
143 /** @brief Scheme of the URI */
144 std::string scheme;
145 /** @brief Host part found in the Authority */
146 std::string host;
147 /** @brief Port found in the Authority */
148 uint16_t port; // 0 means use default (no dynamically allocation needed here)
149 /** @brief Username part found in the Authority */
150 std::string username;
151 /** @brief Password part found in the Authority */
152 std::string password;
153 /** @brief Path part of the URI */
155 /** @brief Query part of the URI */
157 /** @brief Fragment part of the URI */
158 std::string fragment;
159
160 private:
161 friend ROUTER_UTILS_EXPORT std::ostream &operator<<(std::ostream &strm,
162 const URI &uri);
163 /** @brief Sets information using the given URI
164 *
165 * Takes a and parsers out all URI elements.
166 *
167 * Throws URIError on errors.
168 *
169 * @param uri URI to use
170 */
171 void init_from_uri(const std::string &uri);
172
173 /** @brief Copy of the original given URI */
174 std::string uri_;
175
176 /** @brief all URIs like mail:foo@example.org which don't have a authority */
178
179 /** @brief all URIs like foo@example.org which don't have a scheme */
183 bool query_is_signle_parameter_{false};
184};
185
186ROUTER_UTILS_EXPORT std::ostream &operator<<(std::ostream &strm,
187 const URI &uri);
188
190 public:
191 static std::string decode(const std::string &uri, bool decode_plus);
192 static URI parse(const std::string &uri, bool allow_path_rootless = true,
193 bool allow_schemeless = false,
194 bool path_keep_last_slash = false,
195 bool query_single_parameter_when_cant_parse = false);
196 static URI parse_shorthand_uri(const std::string &uri,
197 bool allow_path_rootless = true,
198 const std::string &default_scheme = "mysql");
199};
200
201} // namespace mysqlrouter
202
203#endif // URI_ROUTING_INCLUDED
Exception when URI was not valid.
Definition: uri.h:53
URIError(const char *msg, const std::string &uri, size_t position)
Definition: uri.cc:62
URIError(const std::string &what_arg)
Definition: uri.h:56
Definition: uri.h:189
Parse and create URIs according to RFC3986.
Definition: uri.h:70
bool query_single_parameter_when_cant_parse_
Definition: uri.h:182
std::string username
Username part found in the Authority.
Definition: uri.h:150
std::string scheme
Scheme of the URI.
Definition: uri.h:144
std::string password
Password part found in the Authority.
Definition: uri.h:152
bool allow_schemeless_
all URIs like foo@example.org which don't have a scheme
Definition: uri.h:180
void set_uri(const std::string &uri)
Sets URI using the given URI string.
Definition: uri.h:123
bool allow_path_rootless_
all URIs like mail:foo@example.org which don't have a authority
Definition: uri.h:177
std::string uri_
Copy of the original given URI.
Definition: uri.h:174
uint16_t port
Port found in the Authority.
Definition: uri.h:148
URIQuery query
Query part of the URI.
Definition: uri.h:156
URI()
overload
Definition: uri.h:117
std::string fragment
Fragment part of the URI.
Definition: uri.h:158
URI(const std::string &uri, bool allow_path_rootless=true, bool allow_schemeless=false, bool path_keep_last_slash=false, bool query_single_parameter_when_cant_parse=false)
Default constructor.
Definition: uri.h:88
bool path_keep_last_slash_
Definition: uri.h:181
URIPath path
Path part of the URI.
Definition: uri.h:154
std::string host
Host part found in the Authority.
Definition: uri.h:146
const char * p
Definition: ctype-mb.cc:1235
bool operator!=(const my_thread_handle &a, const my_thread_handle &b)
Definition: my_thread.h:158
bool operator==(const my_thread_handle &a, const my_thread_handle &b)
Definition: my_thread.h:151
static char * query
Definition: myisam_ftdump.cc:47
static char * password
Definition: mysql_secure_installation.cc:58
const char * host
Definition: mysqladmin.cc:65
static char * path
Definition: mysqldump.cc:149
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1081
stdx::expected< std::pair< size_t, T >, std::error_code > decode(const net::const_buffer &buffer, capabilities::value_type caps)
decode a message from a buffer.
Definition: classic_protocol_codec_base.h:119
Definition: dim.h:358
std::vector< std::string > URIPath
Definition: uri.h:46
ROUTER_UTILS_EXPORT std::ostream & operator<<(std::ostream &strm, const URI &uri)
Definition: uri.cc:1205
std::tuple< std::string, uint16_t, std::string, std::string > URIAuthority
Definition: uri.h:45
std::map< std::string, std::string > URIQuery
Definition: uri.h:47
bool parse(MYSQL_THD thd, const string &query, bool is_prepared, Condition_handler *handler)
Definition: services.cc:81
Definition: gcs_xcom_synode.h:64
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
#define ROUTER_UTILS_EXPORT
Definition: router_utils_export.h:15
synode_no q[FIFO_SIZE]
Definition: xcom_base.cc:4086