MySQL 8.3.0
Source Code Documentation
uri.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2015, 2023, 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 also distributed 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 included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef URI_ROUTING_INCLUDED
26#define URI_ROUTING_INCLUDED
27
29
30#include <cstdint>
31#include <exception>
32#include <map>
33#include <stdexcept>
34#include <string>
35#include <tuple>
36#include <vector>
37#ifndef _WIN32
38#include <unistd.h>
39#endif
40
41namespace mysqlrouter {
42
43using URIAuthority = std::tuple<std::string, uint16_t, std::string,
44 std::string>; // host, port, username, password
45using URIPath = std::vector<std::string>;
46using URIQuery = std::map<std::string, std::string>;
47
48/** @class URIError
49 * @brief Exception when URI was not valid
50 *
51 */
52class URIError : public std::runtime_error {
53 public:
54 URIError(const char *msg, const std::string &uri, size_t position);
55 explicit URIError(const std::string &what_arg)
56 : std::runtime_error(what_arg) {}
57};
58
59/** @class URI
60 * @brief Parse and create URIs according to RFC3986
61 *
62 * This class will parse and make the elements of the URI
63 * available as members.
64 *
65 * Links:
66 * * (RFC 3986)[https://tools.ietf.org/html/rfc3986)
67 *
68 */
70 public:
71 /** @brief Delimiter used in the Query part */
72 static const char query_delimiter = '&';
73
74 /** @brief Default constructor
75 *
76 * Rootless URIs like "mailto:user@example.com" may be forbidden to make sure
77 * that simple "host:addr" doesn't get parsed as (scheme='host', path='addr')
78 *
79 * @param uri URI string to decode
80 * @param allow_path_rootless if parsing rootless URIs is allowed.
81 */
82 URI(const std::string &uri, bool allow_path_rootless = true)
83 : scheme(),
84 host(),
85 port(0),
86 username(),
87 password(),
88 path(),
89 query(),
90 fragment(),
91 uri_(uri),
92 allow_path_rootless_(allow_path_rootless) {
93 if (!uri.empty()) {
94 init_from_uri(uri);
95 }
96 }
97
98 bool operator==(const URI &u2) const;
99 bool operator!=(const URI &u2) const;
100
101 /** return string representation of the URI */
102 std::string str() const;
103
104 /** @brief overload */
105 URI() : URI("") {}
106
107 /** @brief Sets URI using the given URI string
108 *
109 * @param uri URI as string
110 */
111 void set_uri(const std::string &uri) { init_from_uri(uri); }
112
113 /** @brief Scheme of the URI */
114 std::string scheme;
115 /** @brief Host part found in the Authority */
116 std::string host;
117 /** @brief Port found in the Authority */
118 uint16_t port; // 0 means use default (no dynamically allocation needed here)
119 /** @brief Username part found in the Authority */
120 std::string username;
121 /** @brief Password part found in the Authority */
122 std::string password;
123 /** @brief Path part of the URI */
125 /** @brief Query part of the URI */
127 /** @brief Fragment part of the URI */
128 std::string fragment;
129
130 private:
131 /** @brief Sets information using the given URI
132 *
133 * Takes a and parsers out all URI elements.
134 *
135 * Throws URIError on errors.
136 *
137 * @param uri URI to use
138 */
139 void init_from_uri(const std::string &uri);
140
141 /** @brief Copy of the original given URI */
142 std::string uri_;
143
144 /** @brief all URIs like mail:foo@example.org which don't have a authority */
146};
147
148std::ostream &operator<<(std::ostream &strm, const URI &uri);
149
151 public:
152 static URI parse(const std::string &uri, bool allow_path_rootless = true);
153 static URI parse_shorthand_uri(const std::string &uri,
154 bool allow_path_rootless = true,
155 const std::string &default_scheme = "mysql");
156};
157
158} // namespace mysqlrouter
159
160#endif // URI_ROUTING_INCLUDED
Exception when URI was not valid.
Definition: uri.h:52
URIError(const char *msg, const std::string &uri, size_t position)
Definition: uri.cc:61
URIError(const std::string &what_arg)
Definition: uri.h:55
Definition: uri.h:150
Parse and create URIs according to RFC3986.
Definition: uri.h:69
std::string username
Username part found in the Authority.
Definition: uri.h:120
std::string scheme
Scheme of the URI.
Definition: uri.h:114
std::string password
Password part found in the Authority.
Definition: uri.h:122
void set_uri(const std::string &uri)
Sets URI using the given URI string.
Definition: uri.h:111
bool allow_path_rootless_
all URIs like mail:foo@example.org which don't have a authority
Definition: uri.h:145
std::string uri_
Copy of the original given URI.
Definition: uri.h:142
uint16_t port
Port found in the Authority.
Definition: uri.h:118
URIQuery query
Query part of the URI.
Definition: uri.h:126
URI()
overload
Definition: uri.h:105
URI(const std::string &uri, bool allow_path_rootless=true)
Default constructor.
Definition: uri.h:82
std::string fragment
Fragment part of the URI.
Definition: uri.h:128
URIPath path
Path part of the URI.
Definition: uri.h:124
std::string host
Host part found in the Authority.
Definition: uri.h:116
bool operator!=(const my_thread_handle &a, const my_thread_handle &b)
Definition: my_thread.h:157
bool operator==(const my_thread_handle &a, const my_thread_handle &b)
Definition: my_thread.h:150
static char * query
Definition: myisam_ftdump.cc:46
static char * password
Definition: mysql_secure_installation.cc:57
const char * host
Definition: mysqladmin.cc:63
static char * path
Definition: mysqldump.cc:148
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1065
Definition: dim.h:357
std::vector< std::string > URIPath
Definition: uri.h:45
std::ostream & operator<<(std::ostream &strm, const URI &uri)
Definition: uri.cc:1165
std::tuple< std::string, uint16_t, std::string, std::string > URIAuthority
Definition: uri.h:44
std::map< std::string, std::string > URIQuery
Definition: uri.h:46
bool parse(MYSQL_THD thd, const string &query, bool is_prepared, Condition_handler *handler)
Definition: services.cc:80
Definition: varlen_sort.h:174
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:32
#define ROUTER_LIB_EXPORT
Definition: router_export.h:15