- Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathuri_parser.h
369 lines (278 loc) · 8.97 KB
/
uri_parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright (c) 2016, 2024, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0, as
* published by the Free Software Foundation.
*
* This program is designed to work with certain software (including
* but not limited to OpenSSL) that is licensed under separate terms, as
* designated in a particular file or component or in included license
* documentation. The authors of MySQL hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have either included with
* the program or referenced in the documentation.
*
* Without limiting anything contained in the foregoing, this file,
* which is part of Connector/C++, is also subject to the
* Universal FOSS Exception, version 1.0, a copy of which can be found at
* https://oss.oracle.com/licenses/universal-foss-exception.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _URI_PARSER_H_
#define_URI_PARSER_H_
#include<mysql/cdk/common.h>
#include"parser.h"
PUSH_SYS_WARNINGS_CDK
#include<list>
#include<stack>
#include<bitset>
#include<iomanip>
#include<functional>
POP_SYS_WARNINGS_CDK
namespaceparser {
/*
Interface used to report contents of mysqlx URI or connection
string.
*/
classURI_processor
{
public:
// LCOV_EXCL_START
/*
Callbacks for the main components of the URI:
<scheme>://<user>:<password>@<host>:<port>/<schema>
If an optional component is not present, the corresponding callback
is not called.
*/
virtualvoidscheme(const std::string&) {}
virtualvoiduser(const std::string&) {}
virtualvoidpassword(const std::string&) {}
virtualvoidschema(const std::string&) {}
/*
Callbacks host(), socket() and pipe() can be called several times to
report multiple targets listed in a connection string. They report
priority 1+x if user specified priority x, or 0 if no priority was specified.
*/
virtualvoidhost(unsignedshort/*priority*/,
const std::string &/*host*/)
{}
virtualvoidhost(unsignedshort/*priority*/,
const std::string &/*host*/,
unsignedshort/*port*/)
{}
// Report Unix socket path.
virtualvoidsocket(unsignedshort/*priority*/,
const std::string &/*socket_path*/)
{}
// Report Win pipe path, including "\\.\" prefix.
virtualvoidpipe(unsignedshort/*priority*/, const std::string &/*pipe*/)
{}
/*
Callbacks for reporting the query component, which is a sequence
of key-value pair. Keys without any value are allowed. Key value
can be a list: "...&key=[v1,..,vN]&...".
*/
virtualvoidkey_val(const std::string&) {}
virtualvoidkey_val(const std::string&, const std::string&) {}
virtualvoidkey_val(const std::string&, const std::list<std::string>&) {}
// LCOV_EXCL_STOP
};
/*
Parse given string as mysqlx URI ("mysqlx:://" scheme required). Pass
extracted information to the given processor.
*/
voidparse_uri(const std::string &uri, URI_processor &prc);
/*
Parse given string as mysqlx connection string ("mysqlx:://" scheme optional).
Pass extracted information to the given processor.
*/
voidparse_conn_str(const std::string &str, URI_processor &prc);
/*
Parser for parsing mysqlx URIs. The same parser can be used to parse
connection string, which is like URI but without the "mysqlx://" scheme
prefix.
*/
classURI_parser
: public cdk::api::Expr_base<URI_processor>
{
public:
classError;
private:
/*
Represents single token in URI string. Tokens are single characters.
If m_pct is true then this character was read in the %XX form (and then
it is never treated as a special URI charcater.
*/
structToken
{
Token() : m_char(0), m_pct(false)
{}
Token(char c, bool pct =false)
: m_char(c), m_pct(pct)
{}
shortget_type() const;
charget_char() const
{
return m_char;
}
boolpct_encoded() const
{
return m_pct;
}
private:
char m_char;
bool m_pct;
friendclassError;
};
/*
Stored constructor parameters.
*/
std::string m_uri;
bool m_force_uri;
/*
Set to true if string contains "mysqlx:://" schema prefix (which
is optional for connection strings).
*/
bool m_has_scheme;
/*
Parser state.
m_tok - the current token (not yet consumed),
m_pos - position of the current token,
m_pos_next - position of the next token following the current one;
if there are no more tokens then m_pos_next = m_pos,
otherwise m_pos_next > m_pos.
*/
structState
{
Token m_tok;
size_t m_pos = 0;
size_t m_pos_next = 0;
State(const Token &tok, size_t pos, size_t next)
: m_tok(tok), m_pos(pos), m_pos_next(next)
{}
};
// We use stack to be able to easily save and restore state.
std::stack<State> m_state;
size_tget_pos() const
{
return m_state.empty() ? 0 : m_state.top().m_pos;
}
voidpush()
{
assert(!m_state.empty());
m_state.push(m_state.top());
}
voidpop()
{
assert(!m_state.empty());
m_state.pop();
}
public:
/*
Create parser for a given string. If 'force_uri' parameter is true,
then the string is expected to be a full URI with the schema part
(errors are reported if schema is missing). Otherwise 'uri' is treated
as a connection string with optional scheme prefix.
*/
URI_parser(const std::string &uri, bool force_uri=false)
: m_uri(uri), m_force_uri(force_uri)
{
// make sure state is not empty (safety)
m_state.emplace(Token(), 0, 0);
}
/*
Method 'process' parses the string passed to constructor and reports
information extracted from it to the given processor. Throws errors
if parsing was not possible. These errors are derived from URI_parser::Error.
*/
voidprocess(Processor &prc) const
{
const_cast<URI_parser*>(this)->parse(prc);
}
voidprocess_if(Processor *prc) const
{
if (!prc)
return;
process(*prc);
}
private:
structTokSet;
// Character classes used in the grammar.
staticconst TokSet unreserved;
staticconst TokSet gen_delims;
staticconst TokSet host_chars;
staticconst TokSet user_chars;
staticconst TokSet db_chars;
voidparse(Processor &prc);
// Methods corresponding to grammar rules.
voidparse_connection(Processor &prc);
boolparse_userinfo(Processor &prc);
voidparse_path(Processor &prc);
voidparse_query(Processor &prc);
voidparse_val_list(const std::string&, Processor &prc);
typedef std::bitset<2> Addr_opts;
staticconstsize_t ADDR_IP = 0;
staticconstsize_t ADDR_OTHER = 1;
voidparse_list_entry(Processor &prc);
Addr_opts parse_host(std::string &address, std::string &port);
boolparse_ip_address(std::string &host, std::string &port);
voidparse_balanced(std::string &chars, bool include_parens = false);
// Helper methods.
unsignedshortconvert_val(const std::string &port) const;
boolreport_address(Processor &prc,
Addr_opts opts,
unsignedshort priority,
const std::string &host,
const std::string &port) const;
voidparse_scheme(bool,Processor&);
// Methods for processing tokens.
Token consume_token();
boolconsume_token(short tt);
boolconsume_word_base(const std::string &word,
std::function<bool(char,char)> compare);
boolconsume_word(const std::string &word);
boolconsume_word_ci(const std::string& word);
voidconsume_until(std::string&, const TokSet&);
voidconsume_while(std::string&, const TokSet&);
voidconsume_all(std::string&);
boolhas_more_tokens() const;
boolat_end() const;
boolnext_token_is(short) const;
boolnext_token_in(const TokSet&) const;
boolget_token();
// Error reporting methods.
voidparse_error(const std::string&) const;
voidinvalid_char(char) const;
voidunexpected(const std::string&, const std::string &msg = std::string()) const;
voidunexpected(char, const std::string &msg = std::string()) const;
structGuard;
friend Error;
};
/*
Base class for URI parser errors.
*/
classURI_parser::Error
: public parser::Error_base
{
protected:
Error(const URI_parser *p, const std::string &descr = std::string())
: Error_base(descr, p->m_uri, p->get_pos())
{}
friend URI_parser;
};
inline
voidURI_parser::parse_error(const std::string &msg) const
{
throwError(this, msg);
}
} // parser
#endif