Debian Codegraph @

crt/aws-c-common/source/uri.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/common/uri.h>
6
7 #include <aws/common/common.h>
8
9 #include <ctype.h>
10 #include <inttypes.h>
11 #include <stdio.h>
12 #include <string.h>
13
14 #ifdef _MSC_VER
15 # pragma warning(disable : 4221) /* aggregate initializer using local variable addresses */
16 # pragma warning(disable : 4204) /* non-constant aggregate initializer */
17 #endif
18
19 enum parser_state {
20 ON_SCHEME,
21 ON_AUTHORITY,
22 ON_PATH,
23 ON_QUERY_STRING,
24 FINISHED,
25 ERROR,
26 };
27
28 struct uri_parser {
29 struct aws_uri *uri;
30 enum parser_state state;
31 };
32
33 /* strlen of UINT32_MAX "4294967295" is 10, plus 1 for '\0' */
34 #define PORT_BUFFER_SIZE 11
35
36 typedef void(parse_fn)(struct uri_parser *parser, struct aws_byte_cursor *str);
37
38 static void s_parse_scheme(struct uri_parser *parser, struct aws_byte_cursor *str);
39 static void s_parse_authority(struct uri_parser *parser, struct aws_byte_cursor *str);
40 static void s_parse_path(struct uri_parser *parser, struct aws_byte_cursor *str);
41 static void s_parse_query_string(struct uri_parser *parser, struct aws_byte_cursor *str);
42
43 static parse_fn *s_states[] = {
44 [ON_SCHEME] = s_parse_scheme,
45 [ON_AUTHORITY] = s_parse_authority,
46 [ON_PATH] = s_parse_path,
47 [ON_QUERY_STRING] = s_parse_query_string,
48 };
49
50 static int s_init_from_uri_str(struct aws_uri *uri) {
51 struct uri_parser parser = {
52 .state = ON_SCHEME,
53 .uri = uri,
54 };
55
56 struct aws_byte_cursor uri_cur = aws_byte_cursor_from_buf(&uri->uri_str);
57
58 while (parser.state < FINISHED) {
59 s_states[parser.state](&parser, &uri_cur);
60 }
61
62 /* Each state function sets the next state, if something goes wrong it sets it to ERROR which is > FINISHED */
63 if (parser.state == FINISHED) {
64 return AWS_OP_SUCCESS;
65 }
66
67 aws_byte_buf_clean_up(&uri->uri_str);
68 AWS_ZERO_STRUCT(*uri);
69 return AWS_OP_ERR;
70 }
71
72 int aws_uri_init_parse(struct aws_uri *uri, struct aws_allocator *allocator, const struct aws_byte_cursor *uri_str) {
73 AWS_ZERO_STRUCT(*uri);
74 uri->self_size = sizeof(struct aws_uri);
75 uri->allocator = allocator;
76
77 if (aws_byte_buf_init_copy_from_cursor(&uri->uri_str, allocator, *uri_str)) {
78 return AWS_OP_ERR;
79 }
80
81 return s_init_from_uri_str(uri);
82 }
83
84 int aws_uri_init_from_builder_options(
85 struct aws_uri *uri,
86 struct aws_allocator *allocator,
87 struct aws_uri_builder_options *options) {
88
89 AWS_ZERO_STRUCT(*uri);
90
91 if (options->query_string.len && options->query_params) {
92 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
93 }
94
95 uri->self_size = sizeof(struct aws_uri);
96 uri->allocator = allocator;
97
98 size_t buffer_size = 0;
99 if (options->scheme.len) {
100 /* 3 for :// */
101 buffer_size += options->scheme.len + 3;
102 }
103
104 buffer_size += options->host_name.len;
105
106 if (options->port) {
107 buffer_size += PORT_BUFFER_SIZE;
108 }
109
110 buffer_size += options->path.len;
111
112 if (options->query_params) {
113 size_t query_len = aws_array_list_length(options->query_params);
114 if (query_len) {
115 /* for the '?' */
116 buffer_size += 1;
117 for (size_t i = 0; i < query_len; ++i) {
118 struct aws_uri_param *uri_param_ptr = NULL;
119 int result = aws_array_list_get_at_ptr(options->query_params, (void **)&uri_param_ptr, i);
120 AWS_FATAL_ASSERT(result == AWS_OP_SUCCESS);
121 /* 2 == 1 for '&' and 1 for '='. who cares if we over-allocate a little? */
122 buffer_size += uri_param_ptr->key.len + uri_param_ptr->value.len + 2;
123 }
124 }
125 } else if (options->query_string.len) {
126 /* for the '?' */
127 buffer_size += 1;
128 buffer_size += options->query_string.len;
129 }
130
131 if (aws_byte_buf_init(&uri->uri_str, allocator, buffer_size)) {
132 return AWS_OP_ERR;
133 }
134
135 uri->uri_str.len = 0;
136 if (options->scheme.len) {
137 aws_byte_buf_append(&uri->uri_str, &options->scheme);
138 struct aws_byte_cursor scheme_app = aws_byte_cursor_from_c_str("://");
139 aws_byte_buf_append(&uri->uri_str, &scheme_app);
140 }
141
142 aws_byte_buf_append(&uri->uri_str, &options->host_name);
143
144 struct aws_byte_cursor port_app = aws_byte_cursor_from_c_str(":");
145 if (options->port) {
146 aws_byte_buf_append(&uri->uri_str, &port_app);
147 char port_arr[PORT_BUFFER_SIZE] = {0};
148 snprintf(port_arr, sizeof(port_arr), "%" PRIu32, options->port);
149 struct aws_byte_cursor port_csr = aws_byte_cursor_from_c_str(port_arr);
150 aws_byte_buf_append(&uri->uri_str, &port_csr);
151 }
152
153 aws_byte_buf_append(&uri->uri_str, &options->path);
154
155 struct aws_byte_cursor query_app = aws_byte_cursor_from_c_str("?");
156
157 if (options->query_params) {
158 struct aws_byte_cursor query_param_app = aws_byte_cursor_from_c_str("&");
159 struct aws_byte_cursor key_value_delim = aws_byte_cursor_from_c_str("=");
160
161 aws_byte_buf_append(&uri->uri_str, &query_app);
162 size_t query_len = aws_array_list_length(options->query_params);
163 for (size_t i = 0; i < query_len; ++i) {
164 struct aws_uri_param *uri_param_ptr = NULL;
165 aws_array_list_get_at_ptr(options->query_params, (void **)&uri_param_ptr, i);
166 aws_byte_buf_append(&uri->uri_str, &uri_param_ptr->key);
167 aws_byte_buf_append(&uri->uri_str, &key_value_delim);
168 aws_byte_buf_append(&uri->uri_str, &uri_param_ptr->value);
169
170 if (i < query_len - 1) {
171 aws_byte_buf_append(&uri->uri_str, &query_param_app);
172 }
173 }
174 } else if (options->query_string.len) {
175 aws_byte_buf_append(&uri->uri_str, &query_app);
176 aws_byte_buf_append(&uri->uri_str, &options->query_string);
177 }
178
179 return s_init_from_uri_str(uri);
180 }
181
182 void aws_uri_clean_up(struct aws_uri *uri) {
183 if (uri->uri_str.allocator) {
184 aws_byte_buf_clean_up(&uri->uri_str);
185 }
186 AWS_ZERO_STRUCT(*uri);
187 }
188
189 const struct aws_byte_cursor *aws_uri_scheme(const struct aws_uri *uri) {
190 return &uri->scheme;
191 }
192
193 const struct aws_byte_cursor *aws_uri_authority(const struct aws_uri *uri) {
194 return &uri->authority;
195 }
196
197 const struct aws_byte_cursor *aws_uri_path(const struct aws_uri *uri) {
198 return &uri->path;
199 }
200
201 const struct aws_byte_cursor *aws_uri_query_string(const struct aws_uri *uri) {
202 return &uri->query_string;
203 }
204
205 const struct aws_byte_cursor *aws_uri_path_and_query(const struct aws_uri *uri) {
206 return &uri->path_and_query;
207 }
208
209 const struct aws_byte_cursor *aws_uri_host_name(const struct aws_uri *uri) {
210 return &uri->host_name;
211 }
212
213 uint32_t aws_uri_port(const struct aws_uri *uri) {
214 return uri->port;
215 }
216
217 bool aws_query_string_next_param(struct aws_byte_cursor query_string, struct aws_uri_param *param) {
218 /* If param is zeroed, then this is the first run. */
219 bool first_run = param->value.ptr == NULL;
220
221 /* aws_byte_cursor_next_split() is used to iterate over params in the query string.
222 * It takes an in/out substring arg similar to how this function works */
223 struct aws_byte_cursor substr;
224 if (first_run) {
225 /* substring must be zeroed to start */
226 AWS_ZERO_STRUCT(substr);
227 } else {
228 /* re-assemble substring which contained key and value */
229 substr.ptr = param->key.ptr;
230 substr.len = (param->value.ptr - param->key.ptr) + param->value.len;
231 }
232
233 /* The do-while is to skip over any empty substrings */
234 do {
235 if (!aws_byte_cursor_next_split(&query_string, '&', &substr)) {
236 /* no more splits, done iterating */
237 return false;
238 }
239 } while (substr.len == 0);
240
241 uint8_t *delim = memchr(substr.ptr, '=', substr.len);
242 if (delim) {
243 param->key.ptr = substr.ptr;
244 param->key.len = delim - substr.ptr;
245 param->value.ptr = delim + 1;
246 param->value.len = substr.len - param->key.len - 1;
247 } else {
248 /* no '=', key gets substring, value is blank */
249 param->key = substr;
250 param->value.ptr = substr.ptr + substr.len;
251 param->value.len = 0;
252 }
253
254 return true;
255 }
256
257 int aws_query_string_params(struct aws_byte_cursor query_string_cursor, struct aws_array_list *out_params) {
258 struct aws_uri_param param;
259 AWS_ZERO_STRUCT(param);
260 while (aws_query_string_next_param(query_string_cursor, &param)) {
261 if (aws_array_list_push_back(out_params, &param)) {
262 return AWS_OP_ERR;
263 }
264 }
265
266 return AWS_OP_SUCCESS;
267 }
268
269 bool aws_uri_query_string_next_param(const struct aws_uri *uri, struct aws_uri_param *param) {
270 return aws_query_string_next_param(uri->query_string, param);
271 }
272
273 int aws_uri_query_string_params(const struct aws_uri *uri, struct aws_array_list *out_params) {
274 return aws_query_string_params(uri->query_string, out_params);
275 }
276
277 static void s_parse_scheme(struct uri_parser *parser, struct aws_byte_cursor *str) {
278 const uint8_t *location_of_colon = memchr(str->ptr, ':', str->len);
279
280 if (!location_of_colon) {
281 parser->state = ON_AUTHORITY;
282 return;
283 }
284
285 /* Ensure location_of_colon is not the last character before checking *(location_of_colon + 1) */
286 if ((size_t)(location_of_colon - str->ptr) + 1 >= str->len || *(location_of_colon + 1) != '/') {
287 /* make sure we didn't just pick up the port by mistake */
288 parser->state = ON_AUTHORITY;
289 return;
290 }
291
292 const size_t scheme_len = location_of_colon - str->ptr;
293 parser->uri->scheme = aws_byte_cursor_advance(str, scheme_len);
294
295 if (str->len < 3 || str->ptr[0] != ':' || str->ptr[1] != '/' || str->ptr[2] != '/') {
296 aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
297 parser->state = ERROR;
298 return;
299 }
300
301 /* advance past the "://" */
302 aws_byte_cursor_advance(str, 3);
303 parser->state = ON_AUTHORITY;
304 }
305
306 static void s_parse_authority(struct uri_parser *parser, struct aws_byte_cursor *str) {
307 const uint8_t *location_of_slash = memchr(str->ptr, '/', str->len);
308 const uint8_t *location_of_qmark = memchr(str->ptr, '?', str->len);
309
310 if (!location_of_slash && !location_of_qmark && str->len) {
311 parser->uri->authority.ptr = str->ptr;
312 parser->uri->authority.len = str->len;
313
314 parser->uri->path.ptr = NULL;
315 parser->uri->path.len = 0;
316 parser->uri->path_and_query = parser->uri->path;
317 parser->state = FINISHED;
318 aws_byte_cursor_advance(str, parser->uri->authority.len);
319 } else if (!str->len) {
320 parser->state = ERROR;
321 aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
322 return;
323 } else {
324 const uint8_t *end = str->ptr + str->len;
325 if (location_of_slash) {
326 parser->state = ON_PATH;
327 end = location_of_slash;
328 } else if (location_of_qmark) {
329 parser->state = ON_QUERY_STRING;
330 end = location_of_qmark;
331 }
332
333 parser->uri->authority = aws_byte_cursor_advance(str, end - str->ptr);
334 }
335
336 struct aws_byte_cursor authority_parse_csr = parser->uri->authority;
337
338 if (authority_parse_csr.len) {
339 /* RFC-3986 section 3.2: authority = [ userinfo "@" ] host [ ":" port ] */
340 const uint8_t *userinfo_delim = memchr(authority_parse_csr.ptr, '@', authority_parse_csr.len);
341 if (userinfo_delim) {
342
343 parser->uri->userinfo =
344 aws_byte_cursor_advance(&authority_parse_csr, userinfo_delim - authority_parse_csr.ptr);
345 /* For the "@" mark */
346 aws_byte_cursor_advance(&authority_parse_csr, 1);
347 struct aws_byte_cursor userinfo_parse_csr = parser->uri->userinfo;
348 uint8_t *info_delim = memchr(userinfo_parse_csr.ptr, ':', userinfo_parse_csr.len);
349 /* RFC-3986 section 3.2.1: Use of the format "user:password" in the userinfo field is deprecated. But we
350 * treat the userinfo as URL here, also, if the format is not following URL pattern, you have the whole
351 * userinfo */
352 /* RFC-1738 section 3.1: <user>:<password> */
353 if (info_delim) {
354 parser->uri->user.ptr = userinfo_parse_csr.ptr;
355 parser->uri->user.len = info_delim - userinfo_parse_csr.ptr;
356 parser->uri->password.ptr = info_delim + 1;
357 parser->uri->password.len = parser->uri->userinfo.len - parser->uri->user.len - 1;
358 } else {
359 parser->uri->user = userinfo_parse_csr;
360 }
361 }
362
363 /* RFC-3986 section 3.2: host identified by IPv6 literal address is
364 * enclosed within square brackets. We must ignore any colons within
365 * IPv6 literals and only search for port delimiter after closing bracket.*/
366 const uint8_t *port_search_start = authority_parse_csr.ptr;
367 size_t port_search_len = authority_parse_csr.len;
368 bool is_IPv6_literal = false;
369 if (authority_parse_csr.len > 0 && authority_parse_csr.ptr[0] == '[') {
370 is_IPv6_literal = true;
371 port_search_start = memchr(authority_parse_csr.ptr, ']', authority_parse_csr.len);
372 if (!port_search_start) {
373 parser->state = ERROR;
374 aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
375 return;
376 }
377 port_search_len = authority_parse_csr.len - (port_search_start - authority_parse_csr.ptr);
378 }
379
380 const uint8_t *port_delim = memchr(port_search_start, ':', port_search_len);
381 /*
382 * RFC-3986 section 3.2.2: A host identified by an IPv6 literal address is represented inside square
383 * brackets.
384 * Ignore the square brackets.
385 */
386 parser->uri->host_name = authority_parse_csr;
387 if (is_IPv6_literal) {
388 aws_byte_cursor_advance(&parser->uri->host_name, 1);
389 parser->uri->host_name.len--;
390 }
391 if (!port_delim) {
392 parser->uri->port = 0;
393 return;
394 }
395
396 size_t host_name_length_correction = is_IPv6_literal ? 2 : 0;
397 parser->uri->host_name.len = port_delim - authority_parse_csr.ptr - host_name_length_correction;
398 size_t port_len = authority_parse_csr.len - parser->uri->host_name.len - 1 - host_name_length_correction;
399 port_delim += 1;
400
401 uint64_t port_u64 = 0;
402 if (port_len > 0) {
403 struct aws_byte_cursor port_cursor = aws_byte_cursor_from_array(port_delim, port_len);
404 if (aws_byte_cursor_utf8_parse_u64(port_cursor, &port_u64)) {
405 parser->state = ERROR;
406 aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
407 return;
408 }
409 if (port_u64 > UINT32_MAX) {
410 parser->state = ERROR;
411 aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
412 return;
413 }
414 }
415
416 parser->uri->port = (uint32_t)port_u64;
417 }
418 }
419
420 static void s_parse_path(struct uri_parser *parser, struct aws_byte_cursor *str) {
421 parser->uri->path_and_query = *str;
422
423 const uint8_t *location_of_q_mark = memchr(str->ptr, '?', str->len);
424
425 if (!location_of_q_mark) {
426 parser->uri->path.ptr = str->ptr;
427 parser->uri->path.len = str->len;
428 parser->state = FINISHED;
429 aws_byte_cursor_advance(str, parser->uri->path.len);
430 return;
431 }
432
433 if (!str->len) {
434 parser->state = ERROR;
435 aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
436 return;
437 }
438
439 parser->uri->path.ptr = str->ptr;
440 parser->uri->path.len = location_of_q_mark - str->ptr;
441 aws_byte_cursor_advance(str, parser->uri->path.len);
442 parser->state = ON_QUERY_STRING;
443 }
444
445 static void s_parse_query_string(struct uri_parser *parser, struct aws_byte_cursor *str) {
446 if (!parser->uri->path_and_query.ptr) {
447 parser->uri->path_and_query = *str;
448 }
449 /* we don't want the '?' character. */
450 if (str->len) {
451 parser->uri->query_string.ptr = str->ptr + 1;
452 parser->uri->query_string.len = str->len - 1;
453 }
454
455 aws_byte_cursor_advance(str, parser->uri->query_string.len + 1);
456 parser->state = FINISHED;
457 }
458
459 static uint8_t s_to_uppercase_hex(uint8_t value) {
460 AWS_ASSERT(value < 16);
461
462 if (value < 10) {
463 return (uint8_t)('0' + value);
464 }
465
466 return (uint8_t)('A' + value - 10);
467 }
468
469 typedef void(unchecked_append_canonicalized_character_fn)(struct aws_byte_buf *buffer, uint8_t value);
470
471 /*
472 * Appends a character or its hex encoding to the buffer. We reserve enough space up front so that
473 * we can do this with raw pointers rather than multiple function calls/cursors/etc...
474 *
475 * This function is for the uri path
476 */
477 static void s_unchecked_append_canonicalized_path_character(struct aws_byte_buf *buffer, uint8_t value) {
478 AWS_ASSERT(buffer->len + 3 <= buffer->capacity);
479
480 uint8_t *dest_ptr = buffer->buffer + buffer->len;
481
482 if (aws_isalnum(value)) {
483 ++buffer->len;
484 *dest_ptr = value;
485 return;
486 }
487
488 switch (value) {
489 /* non-alpha-numeric unreserved, don't % encode them */
490 case '-':
491 case '_':
492 case '.':
493 case '~':
494
495 /* reserved characters that we should not % encode in the path component */
496 case '/':
497 ++buffer->len;
498 *dest_ptr = value;
499 return;
500
501 /*
502 * everything else we should % encode, including from the reserved list
503 */
504 default:
505 buffer->len += 3;
506 *dest_ptr++ = '%';
507 *dest_ptr++ = s_to_uppercase_hex(value >> 4);
508 *dest_ptr = s_to_uppercase_hex(value & 0x0F);
509 return;
510 }
511 }
512
513 /*
514 * Appends a character or its hex encoding to the buffer. We reserve enough space up front so that
515 * we can do this with raw pointers rather than multiple function calls/cursors/etc...
516 *
517 * This function is for query params
518 */
519 static void s_raw_append_canonicalized_param_character(struct aws_byte_buf *buffer, uint8_t value) {
520 AWS_ASSERT(buffer->len + 3 <= buffer->capacity);
521
522 uint8_t *dest_ptr = buffer->buffer + buffer->len;
523
524 if (aws_isalnum(value)) {
525 ++buffer->len;
526 *dest_ptr = value;
527 return;
528 }
529
530 switch (value) {
531 case '-':
532 case '_':
533 case '.':
534 case '~': {
535 ++buffer->len;
536 *dest_ptr = value;
537 return;
538 }
539
540 default:
541 buffer->len += 3;
542 *dest_ptr++ = '%';
543 *dest_ptr++ = s_to_uppercase_hex(value >> 4);
544 *dest_ptr = s_to_uppercase_hex(value & 0x0F);
545 return;
546 }
547 }
548
549 /*
550 * Writes a cursor to a buffer using the supplied encoding function.
551 */
552 static int s_encode_cursor_to_buffer(
553 struct aws_byte_buf *buffer,
554 const struct aws_byte_cursor *cursor,
555 unchecked_append_canonicalized_character_fn *append_canonicalized_character) {
556 const uint8_t *current_ptr = cursor->ptr;
557 const uint8_t *end_ptr = cursor->ptr + cursor->len;
558
559 /*
560 * reserve room up front for the worst possible case: everything gets % encoded
561 */
562 size_t capacity_needed = 0;
563 if (AWS_UNLIKELY(aws_mul_size_checked(3, cursor->len, &capacity_needed))) {
564 return AWS_OP_ERR;
565 }
566
567 if (aws_byte_buf_reserve_relative(buffer, capacity_needed)) {
568 return AWS_OP_ERR;
569 }
570
571 while (current_ptr < end_ptr) {
572 append_canonicalized_character(buffer, *current_ptr);
573 ++current_ptr;
574 }
575
576 return AWS_OP_SUCCESS;
577 }
578
579 int aws_byte_buf_append_encoding_uri_path(struct aws_byte_buf *buffer, const struct aws_byte_cursor *cursor) {
580 return s_encode_cursor_to_buffer(buffer, cursor, s_unchecked_append_canonicalized_path_character);
581 }
582
583 int aws_byte_buf_append_encoding_uri_param(struct aws_byte_buf *buffer, const struct aws_byte_cursor *cursor) {
584 return s_encode_cursor_to_buffer(buffer, cursor, s_raw_append_canonicalized_param_character);
585 }
586
587 int aws_byte_buf_append_decoding_uri(struct aws_byte_buf *buffer, const struct aws_byte_cursor *cursor) {
588 /* reserve room up front for worst possible case: no % and everything copies over 1:1 */
589 if (aws_byte_buf_reserve_relative(buffer, cursor->len)) {
590 return AWS_OP_ERR;
591 }
592
593 /* advance over cursor */
594 struct aws_byte_cursor advancing = *cursor;
595 uint8_t c;
596 while (aws_byte_cursor_read_u8(&advancing, &c)) {
597
598 if (c == '%') {
599 /* two hex characters following '%' are the byte's value */
600 if (AWS_UNLIKELY(aws_byte_cursor_read_hex_u8(&advancing, &c) == false)) {
601 return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
602 }
603 }
604
605 buffer->buffer[buffer->len++] = c;
606 }
607
608 return AWS_OP_SUCCESS;
609 }