MySQL 8.0.37
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 */
83 URI(const std::string &uri, bool allow_path_rootless = true)
84 : scheme(),
85 host(),
86 port(0),
87 username(),
88 password(),
89 path(),
90 query(),
91 fragment(),
92 uri_(uri),
93 allow_path_rootless_(allow_path_rootless) {
94 if (!uri.empty()) {
95 init_from_uri(uri);
96 }
97 }
98
99 bool operator==(const URI &u2) const;
100 bool operator!=(const URI &u2) const;
101
102 /** return string representation of the URI */
103 std::string str() const;
104
105 /** @brief overload */
106 URI() : URI("") {}
107
108 /** @brief Sets URI using the given URI string
109 *
110 * @param uri URI as string
111 */
112 void set_uri(const std::string &uri) { init_from_uri(uri); }
113
114 /** @brief Scheme of the URI */
115 std::string scheme;
116 /** @brief Host part found in the Authority */
117 std::string host;
118 /** @brief Port found in the Authority */
119 uint16_t port; // 0 means use default (no dynamically allocation needed here)
120 /** @brief Username part found in the Authority */
121 std::string username;
122 /** @brief Password part found in the Authority */
123 std::string password;
124 /** @brief Path part of the URI */
126 /** @brief Query part of the URI */
128 /** @brief Fragment part of the URI */
129 std::string fragment;
130
131 private:
132 /** @brief Sets information using the given URI
133 *
134 * Takes a and parsers out all URI elements.
135 *
136 * Throws URIError on errors.
137 *
138 * @param uri URI to use
139 */
140 void init_from_uri(const std::string &uri);
141
142 /** @brief Copy of the original given URI */
143 std::string uri_;
144
145 /** @brief all URIs like mail:foo@example.org which don't have a authority */
147};
148
149std::ostream &operator<<(std::ostream &strm, const URI &uri);
150
152 public:
153 static URI parse(const std::string &uri, bool allow_path_rootless = true);
154 static URI parse_shorthand_uri(const std::string &uri,
155 bool allow_path_rootless = true,
156 const std::string &default_scheme = "mysql");
157};
158
159} // namespace mysqlrouter
160
161#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:151
Parse and create URIs according to RFC3986.
Definition: uri.h:70
std::string username
Username part found in the Authority.
Definition: uri.h:121
std::string scheme
Scheme of the URI.
Definition: uri.h:115
std::string password
Password part found in the Authority.
Definition: uri.h:123
void set_uri(const std::string &uri)
Sets URI using the given URI string.
Definition: uri.h:112
bool allow_path_rootless_
all URIs like mail:foo@example.org which don't have a authority
Definition: uri.h:146
std::string uri_
Copy of the original given URI.
Definition: uri.h:143
uint16_t port
Port found in the Authority.
Definition: uri.h:119
URIQuery query
Query part of the URI.
Definition: uri.h:127
URI()
overload
Definition: uri.h:106
URI(const std::string &uri, bool allow_path_rootless=true)
Default constructor.
Definition: uri.h:83
std::string fragment
Fragment part of the URI.
Definition: uri.h:129
URIPath path
Path part of the URI.
Definition: uri.h:125
std::string host
Host part found in the Authority.
Definition: uri.h:117
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:45
static char * password
Definition: mysql_secure_installation.cc:56
const char * host
Definition: mysqladmin.cc:59
static char * path
Definition: mysqldump.cc:137
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1044
Definition: dim.h:358
std::vector< std::string > URIPath
Definition: uri.h:46
std::ostream & operator<<(std::ostream &strm, const URI &uri)
Definition: uri.cc:1166
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_LIB_EXPORT
Definition: router_export.h:15