Debian Codegraph @

LspCpp/third_party/uri/src/detail/uri_parse.cpp

raw
1 // Copyright 2016-2017 Glyn Matthews.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5
6 #include "uri_parse.hpp"
7 #include <iterator>
8 #include <limits>
9 #include "grammar.hpp"
10 #include <network/uri/detail/uri_parts.hpp>
11
12 namespace network {
13 namespace detail {
14 namespace {
15 enum class uri_state { scheme, hier_part, query, fragment };
16
17 enum class hier_part_state {
18 first_slash,
19 second_slash,
20 authority,
21 host,
22 host_ipv6,
23 port,
24 path
25 };
26
27 bool validate_scheme(string_view::const_iterator &it,
28 string_view::const_iterator last) {
29 if (it == last) {
30 return false;
31 }
32
33 // The first character must be a letter
34 if (!std::isalpha(*it, std::locale("C"))) {
35 return false;
36 }
37 ++it;
38
39 while (it != last) {
40 if (*it == ':') {
41 break;
42 } else if (!isalnum(it, last) && !is_in(it, last, "+-.")) {
43 return false;
44 }
45 }
46
47 return true;
48 }
49
50 bool validate_user_info(string_view::const_iterator it,
51 string_view::const_iterator last) {
52 while (it != last) {
53 if (!is_unreserved(it, last) && !is_pct_encoded(it, last) &&
54 !is_sub_delim(it, last) && !is_in(it, last, ":")) {
55 return false;
56 }
57 }
58 return true;
59 }
60
61 bool set_host_and_port(string_view::const_iterator first,
62 string_view::const_iterator last,
63 string_view::const_iterator last_colon,
64 uri_parts &parts) {
65 if (first >= last_colon) {
66 parts.hier_part.host = uri_part(first, last);
67 } else {
68 auto port_start = last_colon;
69 ++port_start;
70 parts.hier_part.host = uri_part(first, last_colon);
71 if (!is_valid_port(port_start)) {
72 return false;
73 }
74 parts.hier_part.port = uri_part(port_start, last);
75 }
76 return true;
77 }
78
79 bool validate_fragment(string_view::const_iterator &it,
80 string_view::const_iterator last) {
81 while (it != last) {
82 if (!is_pchar(it, last) && !is_in(it, last, "?/")) {
83 return false;
84 }
85 }
86 return true;
87 }
88 } // namespace
89
90 bool parse(string_view::const_iterator &it, string_view::const_iterator last,
91 uri_parts &parts) {
92 auto state = uri_state::scheme;
93
94 auto first = it;
95
96 if (it == last) {
97 return false;
98 }
99
100 if (validate_scheme(it, last)) {
101 parts.scheme = uri_part(first, it);
102 // move past the scheme delimiter
103 ++it;
104 state = uri_state::hier_part;
105 } else {
106 return false;
107 }
108
109 // Hierarchical part
110 auto hp_state = hier_part_state::first_slash;
111 // this is used by the user_info/port
112 auto last_colon = first;
113 while (it != last) {
114 if (hp_state == hier_part_state::first_slash) {
115 if (*it == '/') {
116 hp_state = hier_part_state::second_slash;
117 // set the first iterator in case the second slash is not forthcoming
118 first = it;
119 ++it;
120 continue;
121 } else {
122 hp_state = hier_part_state::path;
123 first = it;
124 }
125 } else if (hp_state == hier_part_state::second_slash) {
126 if (*it == '/') {
127 hp_state = hier_part_state::authority;
128 ++it;
129 first = it;
130 continue;
131 } else {
132 // it's a valid URI, and this is the beginning of the path
133 hp_state = hier_part_state::path;
134 }
135 } else if (hp_state == hier_part_state::authority) {
136 if (is_in(first, last, "@:")) {
137 return false;
138 }
139
140 // reset the last colon
141 if (first == it) {
142 last_colon = first;
143 }
144
145 if (*it == '@') {
146 if (!validate_user_info(first, it)) {
147 return false;
148 }
149 parts.hier_part.user_info = uri_part(first, it);
150 hp_state = hier_part_state::host;
151 ++it;
152 first = it;
153
154 if (*first == '[') {
155 // this is an IPv6 address
156 hp_state = hier_part_state::host_ipv6;
157 }
158
159 continue;
160 } else if (*it == '[') {
161 // this is an IPv6 address
162 hp_state = hier_part_state::host_ipv6;
163 first = it;
164 continue;
165 } else if (*it == ':') {
166 last_colon = it;
167 } else if (*it == '/') {
168 // we skipped right past the host and port, and are at the path.
169 if (!set_host_and_port(first, it, last_colon, parts)) {
170 return false;
171 }
172 hp_state = hier_part_state::path;
173 first = it;
174 continue;
175 } else if (*it == '?') {
176 // the path is empty, but valid, and the next part is the query
177 if (!set_host_and_port(first, it, last_colon, parts)) {
178 return false;
179 }
180 parts.hier_part.path = uri_part(it, it);
181 state = uri_state::query;
182 ++it;
183 first = it;
184 break;
185 } else if (*it == '#') {
186 // the path is empty, but valid, and the next part is the fragment
187 if (!set_host_and_port(first, it, last_colon, parts)) {
188 return false;
189 }
190 parts.hier_part.path = uri_part(it, it);
191 state = uri_state::fragment;
192 ++it;
193 first = it;
194 break;
195 }
196 } else if (hp_state == hier_part_state::host) {
197 if (*first == ':') {
198 return false;
199 }
200
201 if (*it == ':') {
202 parts.hier_part.host = uri_part(first, it);
203 hp_state = hier_part_state::port;
204 ++it;
205 first = it;
206 continue;
207 } else if (*it == '/') {
208 parts.hier_part.host = uri_part(first, it);
209 hp_state = hier_part_state::path;
210 first = it;
211 continue;
212 } else if (*it == '?') {
213 // the path is empty, but valid, and the next part is the query
214 parts.hier_part.host = uri_part(first, it);
215 parts.hier_part.path = uri_part(it, it);
216 state = uri_state::query;
217 ++it;
218 first = it;
219 break;
220 } else if (*it == '#') {
221 // the path is empty, but valid, and the next part is the fragment
222 parts.hier_part.host = uri_part(first, it);
223 parts.hier_part.path = uri_part(it, it);
224 state = uri_state::fragment;
225 ++it;
226 first = it;
227 break;
228 }
229 } else if (hp_state == hier_part_state::host_ipv6) {
230 if (*first != '[') {
231 return false;
232 }
233
234 if (*it == ']') {
235 ++it;
236 // Then test if the next part is a host, part, or the end of the file
237 if (it == last) {
238 break;
239 } else if (*it == ':') {
240 parts.hier_part.host = uri_part(first, it);
241 hp_state = hier_part_state::port;
242 ++it;
243 first = it;
244 } else if (*it == '/') {
245 parts.hier_part.host = uri_part(first, it);
246 hp_state = hier_part_state::path;
247 first = it;
248 } else if (*it == '?') {
249 parts.hier_part.host = uri_part(first, it);
250 parts.hier_part.path = uri_part(it, it);
251 state = uri_state::query;
252 ++it;
253 first = it;
254 break;
255 } else if (*it == '#') {
256 parts.hier_part.host = uri_part(first, it);
257 parts.hier_part.path = uri_part(it, it);
258 state = uri_state::fragment;
259 ++it;
260 first = it;
261 break;
262 }
263 continue;
264 }
265 } else if (hp_state == hier_part_state::port) {
266 if (*first == '/') {
267 // the port is empty, but valid
268 if (!is_valid_port(first)) {
269 return false;
270 }
271 parts.hier_part.port = uri_part(first, it);
272
273 // the port isn't set, but the path is
274 hp_state = hier_part_state::path;
275 continue;
276 }
277
278 if (*it == '/') {
279 if (!is_valid_port(first)) {
280 return false;
281 }
282 parts.hier_part.port = uri_part(first, it);
283 hp_state = hier_part_state::path;
284 first = it;
285 continue;
286 } else if (!isdigit(it, last)) {
287 return false;
288 }
289 continue;
290 } else if (hp_state == hier_part_state::path) {
291 if (*it == '?') {
292 parts.hier_part.path = uri_part(first, it);
293 // move past the query delimiter
294 ++it;
295 first = it;
296 state = uri_state::query;
297 break;
298 } else if (*it == '#') {
299 parts.hier_part.path = uri_part(first, it);
300 // move past the fragment delimiter
301 ++it;
302 first = it;
303 state = uri_state::fragment;
304 break;
305 }
306
307 if (!is_pchar(it, last) && !is_in(it, last, "/")) {
308 return false;
309 } else {
310 continue;
311 }
312 }
313
314 ++it;
315 }
316
317 if (state == uri_state::query) {
318 while (it != last) {
319 if (!is_pchar(it, last) && !is_in(it, last, "?/")) {
320 // If this is a fragment, keep going
321 if (*it == '#') {
322 parts.query = uri_part(first, it);
323 // move past the fragment delimiter
324 ++it;
325 first = it;
326 state = uri_state::fragment;
327 break;
328 } else {
329 return false;
330 }
331 }
332 }
333 }
334
335 if (state == uri_state::fragment) {
336 if (!validate_fragment(it, last)) {
337 return false;
338 }
339 }
340
341 // we're done!
342 if (state == uri_state::hier_part) {
343 if (hp_state == hier_part_state::authority) {
344 if (first == last) {
345 return false;
346 }
347
348 if (!set_host_and_port(first, last, last_colon, parts)) {
349 return false;
350 }
351 parts.hier_part.path = uri_part(last, last);
352 } else if (hp_state == hier_part_state::host) {
353 if (first == last) {
354 return false;
355 }
356
357 if (!set_host_and_port(first, last, last_colon, parts)) {
358 return false;
359 }
360 parts.hier_part.path = uri_part(last, last);
361 } else if (hp_state == hier_part_state::host_ipv6) {
362 if (!set_host_and_port(first, last, last_colon, parts)) {
363 return false;
364 }
365 parts.hier_part.path = uri_part(last, last);
366 } else if (hp_state == hier_part_state::port) {
367 if (!is_valid_port(first)) {
368 return false;
369 }
370 parts.hier_part.port = uri_part(first, last);
371 parts.hier_part.path = uri_part(last, last);
372 } else if (hp_state == hier_part_state::path) {
373 parts.hier_part.path = uri_part(first, last);
374 }
375 } else if (state == uri_state::query) {
376 parts.query = uri_part(first, last);
377 } else if (state == uri_state::fragment) {
378 parts.fragment = uri_part(first, last);
379 }
380
381 return true;
382 }
383 } // namespace detail
384 } // namespace network