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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/common/uri.h>
#include <aws/common/common.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#if _MSC_VER
# pragma warning(disable : 4221) /* aggregate initializer using local variable addresses */
# pragma warning(disable : 4204) /* non-constant aggregate initializer */
#endif
enum parser_state {
ON_SCHEME,
ON_AUTHORITY,
ON_PATH,
ON_QUERY_STRING,
FINISHED,
ERROR,
};
struct uri_parser {
struct aws_uri *uri;
enum parser_state state;
};
typedef void(parse_fn)(struct uri_parser *parser, struct aws_byte_cursor *str);
static void s_parse_scheme(struct uri_parser *parser, struct aws_byte_cursor *str);
static void s_parse_authority(struct uri_parser *parser, struct aws_byte_cursor *str);
static void s_parse_path(struct uri_parser *parser, struct aws_byte_cursor *str);
static void s_parse_query_string(struct uri_parser *parser, struct aws_byte_cursor *str);
static parse_fn *s_states[] = {
[ON_SCHEME] = s_parse_scheme,
[ON_AUTHORITY] = s_parse_authority,
[ON_PATH] = s_parse_path,
[ON_QUERY_STRING] = s_parse_query_string,
};
static int s_init_from_uri_str(struct aws_uri *uri) {
struct uri_parser parser = {
.state = ON_SCHEME,
.uri = uri,
};
struct aws_byte_cursor uri_cur = aws_byte_cursor_from_buf(&uri->uri_str);
while (parser.state < FINISHED) {
s_states[parser.state](&parser, &uri_cur);
}
/* Each state function sets the next state, if something goes wrong it sets it to ERROR which is > FINISHED */
if (parser.state == FINISHED) {
return AWS_OP_SUCCESS;
}
aws_byte_buf_clean_up(&uri->uri_str);
AWS_ZERO_STRUCT(*uri);
return AWS_OP_ERR;
}
int aws_uri_init_parse(struct aws_uri *uri, struct aws_allocator *allocator, const struct aws_byte_cursor *uri_str) {
AWS_ZERO_STRUCT(*uri);
uri->self_size = sizeof(struct aws_uri);
uri->allocator = allocator;
if (aws_byte_buf_init_copy_from_cursor(&uri->uri_str, allocator, *uri_str)) {
return AWS_OP_ERR;
}
return s_init_from_uri_str(uri);
}
int aws_uri_init_from_builder_options(
struct aws_uri *uri,
struct aws_allocator *allocator,
struct aws_uri_builder_options *options) {
AWS_ZERO_STRUCT(*uri);
if (options->query_string.len && options->query_params) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
uri->self_size = sizeof(struct aws_uri);
uri->allocator = allocator;
size_t buffer_size = 0;
if (options->scheme.len) {
/* 3 for :// */
buffer_size += options->scheme.len + 3;
}
buffer_size += options->host_name.len;
if (options->port) {
/* max strlen of a 16 bit integer is 5 */
buffer_size += 6;
}
buffer_size += options->path.len;
if (options->query_params) {
size_t query_len = aws_array_list_length(options->query_params);
if (query_len) {
/* for the '?' */
buffer_size += 1;
for (size_t i = 0; i < query_len; ++i) {
struct aws_uri_param *uri_param_ptr = NULL;
aws_array_list_get_at_ptr(options->query_params, (void **)&uri_param_ptr, i);
/* 2 == 1 for '&' and 1 for '='. who cares if we over-allocate a little? */
buffer_size += uri_param_ptr->key.len + uri_param_ptr->value.len + 2;
}
}
} else if (options->query_string.len) {
/* for the '?' */
buffer_size += 1;
buffer_size += options->query_string.len;
}
if (aws_byte_buf_init(&uri->uri_str, allocator, buffer_size)) {
return AWS_OP_ERR;
}
uri->uri_str.len = 0;
if (options->scheme.len) {
aws_byte_buf_append(&uri->uri_str, &options->scheme);
struct aws_byte_cursor scheme_app = aws_byte_cursor_from_c_str("://");
aws_byte_buf_append(&uri->uri_str, &scheme_app);
}
aws_byte_buf_append(&uri->uri_str, &options->host_name);
struct aws_byte_cursor port_app = aws_byte_cursor_from_c_str(":");
if (options->port) {
aws_byte_buf_append(&uri->uri_str, &port_app);
char port_arr[6] = {0};
snprintf(port_arr, sizeof(port_arr), "%" PRIu16, options->port);
struct aws_byte_cursor port_csr = aws_byte_cursor_from_c_str(port_arr);
aws_byte_buf_append(&uri->uri_str, &port_csr);
}
aws_byte_buf_append(&uri->uri_str, &options->path);
struct aws_byte_cursor query_app = aws_byte_cursor_from_c_str("?");
if (options->query_params) {
struct aws_byte_cursor query_param_app = aws_byte_cursor_from_c_str("&");
struct aws_byte_cursor key_value_delim = aws_byte_cursor_from_c_str("=");
aws_byte_buf_append(&uri->uri_str, &query_app);
size_t query_len = aws_array_list_length(options->query_params);
for (size_t i = 0; i < query_len; ++i) {
struct aws_uri_param *uri_param_ptr = NULL;
aws_array_list_get_at_ptr(options->query_params, (void **)&uri_param_ptr, i);
aws_byte_buf_append(&uri->uri_str, &uri_param_ptr->key);
aws_byte_buf_append(&uri->uri_str, &key_value_delim);
aws_byte_buf_append(&uri->uri_str, &uri_param_ptr->value);
if (i < query_len - 1) {
aws_byte_buf_append(&uri->uri_str, &query_param_app);
}
}
} else if (options->query_string.len) {
aws_byte_buf_append(&uri->uri_str, &query_app);
aws_byte_buf_append(&uri->uri_str, &options->query_string);
}
return s_init_from_uri_str(uri);
}
void aws_uri_clean_up(struct aws_uri *uri) {
if (uri->uri_str.allocator) {
aws_byte_buf_clean_up(&uri->uri_str);
}
AWS_ZERO_STRUCT(*uri);
}
const struct aws_byte_cursor *aws_uri_scheme(const struct aws_uri *uri) {
return &uri->scheme;
}
const struct aws_byte_cursor *aws_uri_authority(const struct aws_uri *uri) {
return &uri->authority;
}
const struct aws_byte_cursor *aws_uri_path(const struct aws_uri *uri) {
return &uri->path;
}
const struct aws_byte_cursor *aws_uri_query_string(const struct aws_uri *uri) {
return &uri->query_string;
}
const struct aws_byte_cursor *aws_uri_path_and_query(const struct aws_uri *uri) {
return &uri->path_and_query;
}
const struct aws_byte_cursor *aws_uri_host_name(const struct aws_uri *uri) {
return &uri->host_name;
}
uint16_t aws_uri_port(const struct aws_uri *uri) {
return uri->port;
}
bool aws_uri_query_string_next_param(const struct aws_uri *uri, struct aws_uri_param *param) {
/* If param is zeroed, then this is the first run. */
bool first_run = param->value.ptr == NULL;
/* aws_byte_cursor_next_split() is used to iterate over params in the query string.
* It takes an in/out substring arg similar to how this function works */
struct aws_byte_cursor substr;
if (first_run) {
/* substring must be zeroed to start */
AWS_ZERO_STRUCT(substr);
} else {
/* re-assemble substring which contained key and value */
substr.ptr = param->key.ptr;
substr.len = (param->value.ptr - param->key.ptr) + param->value.len;
}
/* The do-while is to skip over any empty substrings */
do {
if (!aws_byte_cursor_next_split(&uri->query_string, '&', &substr)) {
/* no more splits, done iterating */
return false;
}
} while (substr.len == 0);
uint8_t *delim = memchr(substr.ptr, '=', substr.len);
if (delim) {
param->key.ptr = substr.ptr;
param->key.len = delim - substr.ptr;
param->value.ptr = delim + 1;
param->value.len = substr.len - param->key.len - 1;
} else {
/* no '=', key gets substring, value is blank */
param->key = substr;
param->value.ptr = substr.ptr + substr.len;
param->value.len = 0;
}
return true;
}
int aws_uri_query_string_params(const struct aws_uri *uri, struct aws_array_list *out_params) {
struct aws_uri_param param;
AWS_ZERO_STRUCT(param);
while (aws_uri_query_string_next_param(uri, ¶m)) {
if (aws_array_list_push_back(out_params, ¶m)) {
return AWS_OP_ERR;
}
}
return AWS_OP_SUCCESS;
}
static void s_parse_scheme(struct uri_parser *parser, struct aws_byte_cursor *str) {
uint8_t *location_of_colon = memchr(str->ptr, ':', str->len);
if (!location_of_colon) {
parser->state = ON_AUTHORITY;
return;
}
/* make sure we didn't just pick up the port by mistake */
if ((size_t)(location_of_colon - str->ptr) < str->len && *(location_of_colon + 1) != '/') {
parser->state = ON_AUTHORITY;
return;
}
const size_t scheme_len = location_of_colon - str->ptr;
parser->uri->scheme = aws_byte_cursor_advance(str, scheme_len);
if (str->len < 3 || str->ptr[0] != ':' || str->ptr[1] != '/' || str->ptr[2] != '/') {
aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
parser->state = ERROR;
return;
}
/* advance past the "://" */
aws_byte_cursor_advance(str, 3);
parser->state = ON_AUTHORITY;
}
static void s_parse_authority(struct uri_parser *parser, struct aws_byte_cursor *str) {
uint8_t *location_of_slash = memchr(str->ptr, '/', str->len);
uint8_t *location_of_qmark = memchr(str->ptr, '?', str->len);
if (!location_of_slash && !location_of_qmark && str->len) {
parser->uri->authority.ptr = str->ptr;
parser->uri->authority.len = str->len;
parser->uri->path.ptr = NULL;
parser->uri->path.len = 0;
parser->uri->path_and_query = parser->uri->path;
parser->state = FINISHED;
aws_byte_cursor_advance(str, parser->uri->authority.len);
} else if (!str->len) {
parser->state = ERROR;
aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
return;
} else {
uint8_t *end = str->ptr + str->len;
if (location_of_slash) {
parser->state = ON_PATH;
end = location_of_slash;
} else if (location_of_qmark) {
parser->state = ON_QUERY_STRING;
end = location_of_qmark;
}
parser->uri->authority = aws_byte_cursor_advance(str, end - str->ptr);
}
struct aws_byte_cursor authority_parse_csr = parser->uri->authority;
if (authority_parse_csr.len) {
/* RFC-3986 section 3.2: authority = [ userinfo "@" ] host [ ":" port ] */
uint8_t *userinfo_delim = memchr(authority_parse_csr.ptr, '@', authority_parse_csr.len);
if (userinfo_delim) {
parser->uri->userinfo =
aws_byte_cursor_advance(&authority_parse_csr, userinfo_delim - authority_parse_csr.ptr);
/* For the "@" mark */
aws_byte_cursor_advance(&authority_parse_csr, 1);
struct aws_byte_cursor userinfo_parse_csr = parser->uri->userinfo;
uint8_t *info_delim = memchr(userinfo_parse_csr.ptr, ':', userinfo_parse_csr.len);
/* RFC-3986 section 3.2.1: Use of the format "user:password" in the userinfo field is deprecated. But we
* treat the userinfo as URL here, also, if the format is not following URL pattern, you have the whole
* userinfo */
/* RFC-1738 section 3.1: <user>:<password> */
if (info_delim) {
parser->uri->user.ptr = userinfo_parse_csr.ptr;
parser->uri->user.len = info_delim - userinfo_parse_csr.ptr;
parser->uri->password.ptr = info_delim + 1;
parser->uri->password.len = parser->uri->userinfo.len - parser->uri->user.len - 1;
} else {
parser->uri->user = userinfo_parse_csr;
}
}
/* RFC-3986 section 3.2: host identified by IPv6 literal address is
* enclosed within square brackets. We must ignore any colons within
* IPv6 literals and only search for port delimiter after closing bracket.*/
uint8_t *port_search_start = authority_parse_csr.ptr;
size_t port_search_len = authority_parse_csr.len;
if (authority_parse_csr.len > 0 && authority_parse_csr.ptr[0] == '[') {
port_search_start = memchr(authority_parse_csr.ptr, ']', authority_parse_csr.len);
if (!port_search_start) {
parser->state = ERROR;
aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
return;
}
port_search_len = authority_parse_csr.len - (port_search_start - authority_parse_csr.ptr);
}
uint8_t *port_delim = memchr(port_search_start, ':', port_search_len);
if (!port_delim) {
parser->uri->port = 0;
parser->uri->host_name = authority_parse_csr;
return;
}
parser->uri->host_name.ptr = authority_parse_csr.ptr;
parser->uri->host_name.len = port_delim - authority_parse_csr.ptr;
size_t port_len = authority_parse_csr.len - parser->uri->host_name.len - 1;
port_delim += 1;
for (size_t i = 0; i < port_len; ++i) {
if (!aws_isdigit(port_delim[i])) {
parser->state = ERROR;
aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
return;
}
}
if (port_len > 5) {
parser->state = ERROR;
aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
return;
}
/* why 6? because the port is a 16-bit unsigned integer*/
char atoi_buf[6] = {0};
memcpy(atoi_buf, port_delim, port_len);
int port_int = atoi(atoi_buf);
if (port_int > UINT16_MAX) {
parser->state = ERROR;
aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
return;
}
parser->uri->port = (uint16_t)port_int;
}
}
static void s_parse_path(struct uri_parser *parser, struct aws_byte_cursor *str) {
parser->uri->path_and_query = *str;
uint8_t *location_of_q_mark = memchr(str->ptr, '?', str->len);
if (!location_of_q_mark) {
parser->uri->path.ptr = str->ptr;
parser->uri->path.len = str->len;
parser->state = FINISHED;
aws_byte_cursor_advance(str, parser->uri->path.len);
return;
}
if (!str->len) {
parser->state = ERROR;
aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
return;
}
parser->uri->path.ptr = str->ptr;
parser->uri->path.len = location_of_q_mark - str->ptr;
aws_byte_cursor_advance(str, parser->uri->path.len);
parser->state = ON_QUERY_STRING;
}
static void s_parse_query_string(struct uri_parser *parser, struct aws_byte_cursor *str) {
if (!parser->uri->path_and_query.ptr) {
parser->uri->path_and_query = *str;
}
/* we don't want the '?' character. */
if (str->len) {
parser->uri->query_string.ptr = str->ptr + 1;
parser->uri->query_string.len = str->len - 1;
}
aws_byte_cursor_advance(str, parser->uri->query_string.len + 1);
parser->state = FINISHED;
}
static uint8_t s_to_uppercase_hex(uint8_t value) {
AWS_ASSERT(value < 16);
if (value < 10) {
return (uint8_t)('0' + value);
}
return (uint8_t)('A' + value - 10);
}
typedef void(unchecked_append_canonicalized_character_fn)(struct aws_byte_buf *buffer, uint8_t value);
/*
* Appends a character or its hex encoding to the buffer. We reserve enough space up front so that
* we can do this with raw pointers rather than multiple function calls/cursors/etc...
*
* This function is for the uri path
*/
static void s_unchecked_append_canonicalized_path_character(struct aws_byte_buf *buffer, uint8_t value) {
AWS_ASSERT(buffer->len + 3 <= buffer->capacity);
uint8_t *dest_ptr = buffer->buffer + buffer->len;
if (aws_isalnum(value)) {
++buffer->len;
*dest_ptr = value;
return;
}
switch (value) {
/* non-alpha-numeric unreserved, don't % encode them */
case '-':
case '_':
case '.':
case '~':
/* reserved characters that we should not % encode in the path component */
case '/':
++buffer->len;
*dest_ptr = value;
return;
/*
* everything else we should % encode, including from the reserved list
*/
default:
buffer->len += 3;
*dest_ptr++ = '%';
*dest_ptr++ = s_to_uppercase_hex(value >> 4);
*dest_ptr = s_to_uppercase_hex(value & 0x0F);
return;
}
}
/*
* Appends a character or its hex encoding to the buffer. We reserve enough space up front so that
* we can do this with raw pointers rather than multiple function calls/cursors/etc...
*
* This function is for query params
*/
static void s_raw_append_canonicalized_param_character(struct aws_byte_buf *buffer, uint8_t value) {
AWS_ASSERT(buffer->len + 3 <= buffer->capacity);
uint8_t *dest_ptr = buffer->buffer + buffer->len;
if (aws_isalnum(value)) {
++buffer->len;
*dest_ptr = value;
return;
}
switch (value) {
case '-':
case '_':
case '.':
case '~': {
++buffer->len;
*dest_ptr = value;
return;
}
default:
buffer->len += 3;
*dest_ptr++ = '%';
*dest_ptr++ = s_to_uppercase_hex(value >> 4);
*dest_ptr = s_to_uppercase_hex(value & 0x0F);
return;
}
}
/*
* Writes a cursor to a buffer using the supplied encoding function.
*/
static int s_encode_cursor_to_buffer(
struct aws_byte_buf *buffer,
const struct aws_byte_cursor *cursor,
unchecked_append_canonicalized_character_fn *append_canonicalized_character) {
uint8_t *current_ptr = cursor->ptr;
uint8_t *end_ptr = cursor->ptr + cursor->len;
/*
* reserve room up front for the worst possible case: everything gets % encoded
*/
size_t capacity_needed = 0;
if (AWS_UNLIKELY(aws_mul_size_checked(3, cursor->len, &capacity_needed))) {
return AWS_OP_ERR;
}
if (aws_byte_buf_reserve_relative(buffer, capacity_needed)) {
return AWS_OP_ERR;
}
while (current_ptr < end_ptr) {
append_canonicalized_character(buffer, *current_ptr);
++current_ptr;
}
return AWS_OP_SUCCESS;
}
int aws_byte_buf_append_encoding_uri_path(struct aws_byte_buf *buffer, const struct aws_byte_cursor *cursor) {
return s_encode_cursor_to_buffer(buffer, cursor, s_unchecked_append_canonicalized_path_character);
}
int aws_byte_buf_append_encoding_uri_param(struct aws_byte_buf *buffer, const struct aws_byte_cursor *cursor) {
return s_encode_cursor_to_buffer(buffer, cursor, s_raw_append_canonicalized_param_character);
}
int aws_byte_buf_append_decoding_uri(struct aws_byte_buf *buffer, const struct aws_byte_cursor *cursor) {
/* reserve room up front for worst possible case: no % and everything copies over 1:1 */
if (aws_byte_buf_reserve_relative(buffer, cursor->len)) {
return AWS_OP_ERR;
}
/* advance over cursor */
struct aws_byte_cursor advancing = *cursor;
uint8_t c;
while (aws_byte_cursor_read_u8(&advancing, &c)) {
if (c == '%') {
/* two hex characters following '%' are the byte's value */
if (AWS_UNLIKELY(aws_byte_cursor_read_hex_u8(&advancing, &c) == false)) {
return aws_raise_error(AWS_ERROR_MALFORMED_INPUT_STRING);
}
}
buffer->buffer[buffer->len++] = c;
}
return AWS_OP_SUCCESS;
}
|