forked from microsoft/cpprestsdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuri_parser.cpp
More file actions
363 lines (326 loc) · 10.7 KB
/
uri_parser.cpp
File metadata and controls
363 lines (326 loc) · 10.7 KB
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
/***
* ==++==
*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ==--==
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*
* URI parsing implementation
*
* For the latest on this and related APIs, please see: https://github.com/Microsoft/cpprestsdk
*
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
****/
#include "stdafx.h"
#include <locale>
namespace web { namespace details { namespace uri_parser
{
bool validate(const utility::string_t &encoded_string)
{
const utility::char_t *scheme_begin = nullptr;
const utility::char_t *scheme_end = nullptr;
const utility::char_t *uinfo_begin = nullptr;
const utility::char_t *uinfo_end = nullptr;
const utility::char_t *host_begin = nullptr;
const utility::char_t *host_end = nullptr;
int port_ptr = 0;
const utility::char_t *path_begin = nullptr;
const utility::char_t *path_end = nullptr;
const utility::char_t *query_begin = nullptr;
const utility::char_t *query_end = nullptr;
const utility::char_t *fragment_begin = nullptr;
const utility::char_t *fragment_end = nullptr;
// perform a parse, but don't copy out the data
return inner_parse(
encoded_string.c_str(),
&scheme_begin,
&scheme_end,
&uinfo_begin,
&uinfo_end,
&host_begin,
&host_end,
&port_ptr,
&path_begin,
&path_end,
&query_begin,
&query_end,
&fragment_begin,
&fragment_end);
}
bool parse(const utility::string_t &encoded_string, uri_components &components)
{
const utility::char_t *scheme_begin = nullptr;
const utility::char_t *scheme_end = nullptr;
const utility::char_t *host_begin = nullptr;
const utility::char_t *host_end = nullptr;
const utility::char_t *uinfo_begin = nullptr;
const utility::char_t *uinfo_end = nullptr;
int port_ptr = 0;
const utility::char_t *path_begin = nullptr;
const utility::char_t *path_end = nullptr;
const utility::char_t *query_begin = nullptr;
const utility::char_t *query_end = nullptr;
const utility::char_t *fragment_begin = nullptr;
const utility::char_t *fragment_end = nullptr;
if (inner_parse(
encoded_string.c_str(),
&scheme_begin,
&scheme_end,
&uinfo_begin,
&uinfo_end,
&host_begin,
&host_end,
&port_ptr,
&path_begin,
&path_end,
&query_begin,
&query_end,
&fragment_begin,
&fragment_end))
{
if (scheme_begin)
{
components.m_scheme.assign(scheme_begin, scheme_end);
// convert scheme to lowercase
std::transform(components.m_scheme.begin(), components.m_scheme.end(), components.m_scheme.begin(), [](utility::char_t c) {
return std::tolower(c, std::locale::classic());
});
}
else
{
components.m_scheme.clear();
}
if (uinfo_begin)
{
components.m_user_info.assign(uinfo_begin, uinfo_end);
}
if (host_begin)
{
components.m_host.assign(host_begin, host_end);
// convert host to lowercase
std::transform(components.m_host.begin(), components.m_host.end(), components.m_host.begin(), [](utility::char_t c) {
return std::tolower(c, std::locale::classic());
});
}
else
{
components.m_host.clear();
}
if (port_ptr)
{
components.m_port = port_ptr;
}
else
{
components.m_port = 0;
}
if (path_begin)
{
components.m_path.assign(path_begin, path_end);
}
else
{
// default path to begin with a slash for easy comparison
components.m_path = _XPLATSTR("/");
}
if (query_begin)
{
components.m_query.assign(query_begin, query_end);
}
else
{
components.m_query.clear();
}
if (fragment_begin)
{
components.m_fragment.assign(fragment_begin, fragment_end);
}
else
{
components.m_fragment.clear();
}
return true;
}
else
{
return false;
}
}
bool inner_parse(
const utility::char_t *encoded,
const utility::char_t **scheme_begin, const utility::char_t **scheme_end,
const utility::char_t **uinfo_begin, const utility::char_t **uinfo_end,
const utility::char_t **host_begin, const utility::char_t **host_end,
_Out_ int *port,
const utility::char_t **path_begin, const utility::char_t **path_end,
const utility::char_t **query_begin, const utility::char_t **query_end,
const utility::char_t **fragment_begin, const utility::char_t **fragment_end)
{
*scheme_begin = nullptr;
*scheme_end = nullptr;
*uinfo_begin = nullptr;
*uinfo_end = nullptr;
*host_begin = nullptr;
*host_end = nullptr;
*port = 0;
*path_begin = nullptr;
*path_end = nullptr;
*query_begin = nullptr;
*query_end = nullptr;
*fragment_begin = nullptr;
*fragment_end = nullptr;
const utility::char_t *p = encoded;
// IMPORTANT -- A uri may either be an absolute uri, or an relative-reference
// Absolute: 'http://host.com'
// Relative-Reference: '//:host.com', '/path1/path2?query', './path1:path2'
// A Relative-Reference can be disambiguated by parsing for a ':' before the first slash
bool is_relative_reference = true;
const utility::char_t *p2 = p;
for (;*p2 != _XPLATSTR('/') && *p2 != _XPLATSTR('\0'); p2++)
{
if (*p2 == _XPLATSTR(':'))
{
// found a colon, the first portion is a scheme
is_relative_reference = false;
break;
}
}
if (!is_relative_reference)
{
// the first character of a scheme must be a letter
if (!isalpha(*p))
{
return false;
}
// start parsing the scheme, it's always delimited by a colon (must be present)
*scheme_begin = p++;
for (;*p != ':'; p++)
{
if (!is_scheme_character(*p))
{
return false;
}
}
*scheme_end = p;
// skip over the colon
p++;
}
// if we see two slashes next, then we're going to parse the authority portion
// later on we'll break up the authority into the port and host
const utility::char_t *authority_begin = nullptr;
const utility::char_t *authority_end = nullptr;
if (*p == _XPLATSTR('/') && p[1] == _XPLATSTR('/'))
{
// skip over the slashes
p += 2;
authority_begin = p;
// the authority is delimited by a slash (resource), question-mark (query) or octothorpe (fragment)
// or by EOS. The authority could be empty ('file:///C:\file_name.txt')
for (;*p != _XPLATSTR('/') && *p != _XPLATSTR('?') && *p != _XPLATSTR('#') && *p != _XPLATSTR('\0'); p++)
{
// We're NOT currently supporting IPv6, IPvFuture or username/password in authority
if (!is_authority_character(*p))
{
return false;
}
}
authority_end = p;
// now lets see if we have a port specified -- by working back from the end
if (authority_begin != authority_end)
{
// the port is made up of all digits
const utility::char_t *port_begin = authority_end - 1;
for (;isdigit(*port_begin) && port_begin != authority_begin; port_begin--)
{ }
if (*port_begin == _XPLATSTR(':'))
{
// has a port
*host_begin = authority_begin;
*host_end = port_begin;
//skip the colon
port_begin++;
*port = utility::conversions::scan_string<int>(utility::string_t(port_begin, authority_end), std::locale::classic());
}
else
{
// no port
*host_begin = authority_begin;
*host_end = authority_end;
}
// look for a user_info component
const utility::char_t *u_end = *host_begin;
for (;is_user_info_character(*u_end) && u_end != *host_end; u_end++)
{ }
if (*u_end == _XPLATSTR('@'))
{
*host_begin = u_end+1;
*uinfo_begin = authority_begin;
*uinfo_end = u_end;
}
else
{
uinfo_end = uinfo_begin = nullptr;
}
}
}
// if we see a path character or a slash, then the
// if we see a slash, or any other legal path character, parse the path next
if (*p == _XPLATSTR('/') || is_path_character(*p))
{
*path_begin = p;
// the path is delimited by a question-mark (query) or octothorpe (fragment) or by EOS
for (;*p != _XPLATSTR('?') && *p != _XPLATSTR('#') && *p != _XPLATSTR('\0'); p++)
{
if (!is_path_character(*p))
{
return false;
}
}
*path_end = p;
}
// if we see a ?, then the query is next
if (*p == _XPLATSTR('?'))
{
// skip over the question mark
p++;
*query_begin = p;
// the query is delimited by a '#' (fragment) or EOS
for (;*p != _XPLATSTR('#') && *p != _XPLATSTR('\0'); p++)
{
if (!is_query_character(*p))
{
return false;
}
}
*query_end = p;
}
// if we see a #, then the fragment is next
if (*p == _XPLATSTR('#'))
{
// skip over the hash mark
p++;
*fragment_begin = p;
// the fragment is delimited by EOS
for (;*p != _XPLATSTR('\0'); p++)
{
if (!is_fragment_character(*p))
{
return false;
}
}
*fragment_end = p;
}
return true;
}
}}}