Debian Codegraph @

crt/aws-c-sdkutils/source/endpoints_types_impl.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
6 #include <aws/common/array_list.h>
7 #include <aws/common/hash_table.h>
8 #include <aws/common/json.h>
9 #include <aws/common/string.h>
10 #include <aws/sdkutils/private/endpoints_regex.h>
11 #include <aws/sdkutils/private/endpoints_types_impl.h>
12 #include <aws/sdkutils/private/endpoints_util.h>
13
14 void s_endpoints_value_clean_up_cb(void *value);
15
16 uint64_t aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_LAST];
17
18 void aws_endpoints_rule_engine_init(void) {
19 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_IS_SET] = aws_hash_c_string("isSet");
20 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_NOT] = aws_hash_c_string("not");
21 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_GET_ATTR] = aws_hash_c_string("getAttr");
22 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_SUBSTRING] = aws_hash_c_string("substring");
23 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_STRING_EQUALS] = aws_hash_c_string("stringEquals");
24 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_BOOLEAN_EQUALS] = aws_hash_c_string("booleanEquals");
25 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_SPLIT] = aws_hash_c_string("split");
26 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_ITE] = aws_hash_c_string("ite");
27 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_COALESCE] = aws_hash_c_string("coalesce");
28 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_URI_ENCODE] = aws_hash_c_string("uriEncode");
29 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_PARSE_URL] = aws_hash_c_string("parseURL");
30 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_IS_VALID_HOST_LABEL] = aws_hash_c_string("isValidHostLabel");
31 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_AWS_PARTITION] = aws_hash_c_string("aws.partition");
32 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_AWS_PARSE_ARN] = aws_hash_c_string("aws.parseArn");
33 aws_endpoints_fn_name_hash[AWS_ENDPOINTS_FN_AWS_IS_VIRTUAL_HOSTABLE_S3_BUCKET] =
34 aws_hash_c_string("aws.isVirtualHostableS3Bucket");
35 }
36
37 static void s_on_condition_array_element_clean_up(void *element) {
38 struct aws_endpoints_condition *condition = element;
39 aws_endpoints_condition_clean_up(condition);
40 }
41
42 static void s_on_rule_array_element_clean_up(void *element) {
43 struct aws_endpoints_rule *rule = element;
44 aws_endpoints_rule_clean_up(rule);
45 }
46
47 struct aws_partition_info *aws_partition_info_new(struct aws_allocator *allocator, struct aws_byte_cursor name) {
48 AWS_PRECONDITION(allocator);
49 struct aws_partition_info *partition_info = aws_mem_calloc(allocator, 1, sizeof(struct aws_partition_info));
50
51 partition_info->allocator = allocator;
52 partition_info->name = name;
53
54 return partition_info;
55 }
56
57 void aws_partition_info_destroy(struct aws_partition_info *partition_info) {
58 if (partition_info == NULL) {
59 return;
60 }
61
62 if (!partition_info->is_copy) {
63 aws_string_destroy(partition_info->info);
64 }
65
66 aws_endpoints_regex_destroy(partition_info->region_regex);
67
68 aws_mem_release(partition_info->allocator, partition_info);
69 }
70
71 struct aws_endpoints_parameter *aws_endpoints_parameter_new(
72 struct aws_allocator *allocator,
73 struct aws_byte_cursor name) {
74 AWS_PRECONDITION(allocator);
75 struct aws_endpoints_parameter *parameter = aws_mem_calloc(allocator, 1, sizeof(struct aws_endpoints_parameter));
76
77 parameter->allocator = allocator;
78 parameter->name = name;
79
80 return parameter;
81 }
82
83 void aws_endpoints_parameter_destroy(struct aws_endpoints_parameter *parameter) {
84 if (parameter == NULL) {
85 return;
86 }
87
88 if (parameter->has_default_value && parameter->type == AWS_ENDPOINTS_PARAMETER_STRING_ARRAY) {
89 aws_array_list_deep_clean_up(&parameter->default_value.v.array, s_endpoints_value_clean_up_cb);
90 }
91
92 aws_mem_release(parameter->allocator, parameter);
93 }
94
95 void aws_endpoints_rule_clean_up(struct aws_endpoints_rule *rule) {
96 AWS_PRECONDITION(rule);
97
98 aws_array_list_deep_clean_up(&rule->conditions, s_on_condition_array_element_clean_up);
99
100 switch (rule->type) {
101 case AWS_ENDPOINTS_RULE_ENDPOINT:
102 aws_endpoints_rule_data_endpoint_clean_up(&rule->rule_data.endpoint);
103 break;
104 case AWS_ENDPOINTS_RULE_ERROR:
105 aws_endpoints_rule_data_error_clean_up(&rule->rule_data.error);
106 break;
107 case AWS_ENDPOINTS_RULE_TREE:
108 aws_endpoints_rule_data_tree_clean_up(&rule->rule_data.tree);
109 break;
110 default:
111 AWS_FATAL_ASSERT(false);
112 }
113
114 AWS_ZERO_STRUCT(*rule);
115 }
116
117 void aws_endpoints_rule_data_endpoint_clean_up(struct aws_endpoints_rule_data_endpoint *rule_data) {
118 AWS_PRECONDITION(rule_data);
119
120 aws_byte_buf_clean_up(&rule_data->properties);
121 aws_hash_table_clean_up(&rule_data->headers);
122
123 AWS_ZERO_STRUCT(*rule_data);
124 }
125
126 void aws_endpoints_rule_data_error_clean_up(struct aws_endpoints_rule_data_error *rule_data) {
127 AWS_PRECONDITION(rule_data);
128
129 AWS_ZERO_STRUCT(*rule_data);
130 }
131
132 void aws_endpoints_rule_data_tree_clean_up(struct aws_endpoints_rule_data_tree *rule_data) {
133 AWS_PRECONDITION(rule_data);
134
135 aws_array_list_deep_clean_up(&rule_data->rules, s_on_rule_array_element_clean_up);
136 AWS_ZERO_STRUCT(*rule_data);
137 }
138
139 void aws_endpoints_condition_clean_up(struct aws_endpoints_condition *condition) {
140 AWS_PRECONDITION(condition);
141
142 AWS_ZERO_STRUCT(*condition);
143 }
144
145 void aws_endpoints_expr_clean_up(struct aws_endpoints_expr *expr) {
146 AWS_PRECONDITION(expr);
147
148 switch (expr->type) {
149 case AWS_ENDPOINTS_EXPR_STRING:
150 case AWS_ENDPOINTS_EXPR_TEMPLATE_STRING:
151 case AWS_ENDPOINTS_EXPR_BOOLEAN:
152 case AWS_ENDPOINTS_EXPR_NUMBER:
153 case AWS_ENDPOINTS_EXPR_REFERENCE:
154 break;
155 case AWS_ENDPOINTS_EXPR_FUNCTION:
156 break;
157 case AWS_ENDPOINTS_EXPR_ARRAY:
158 break;
159 case AWS_ENDPOINTS_EXPR_OBJECT:
160 break;
161 default:
162 AWS_FATAL_ASSERT(false);
163 }
164
165 AWS_ZERO_STRUCT(*expr);
166 }
167
168 struct aws_endpoints_scope_value *aws_endpoints_scope_value_new(
169 struct aws_allocator *allocator,
170 struct aws_byte_cursor name_cur,
171 bool should_own) {
172 AWS_PRECONDITION(allocator);
173 struct aws_endpoints_scope_value *value = aws_mem_calloc(allocator, 1, sizeof(struct aws_endpoints_scope_value));
174
175 value->allocator = allocator;
176 value->name = should_own ? aws_endpoints_owning_cursor_from_cursor(allocator, name_cur)
177 : aws_endpoints_non_owning_cursor_create(name_cur);
178
179 return value;
180 }
181
182 void aws_endpoints_scope_value_destroy(struct aws_endpoints_scope_value *scope_value) {
183 if (scope_value == NULL) {
184 return;
185 }
186 aws_string_destroy(scope_value->name.string);
187
188 aws_endpoints_value_clean_up(&scope_value->value);
189 aws_mem_release(scope_value->allocator, scope_value);
190 }
191
192 void aws_endpoints_value_clean_up_cb(void *value);
193
194 void aws_endpoints_value_clean_up(struct aws_endpoints_value *aws_endpoints_value) {
195 AWS_PRECONDITION(aws_endpoints_value);
196
197 if (aws_endpoints_value->is_ref) {
198 goto on_done;
199 }
200
201 if (aws_endpoints_value->type == AWS_ENDPOINTS_VALUE_STRING) {
202 aws_string_destroy(aws_endpoints_value->v.owning_cursor_string.string);
203 }
204
205 if (aws_endpoints_value->type == AWS_ENDPOINTS_VALUE_OBJECT) {
206 aws_string_destroy(aws_endpoints_value->v.owning_cursor_object.string);
207 }
208
209 if (aws_endpoints_value->type == AWS_ENDPOINTS_VALUE_ARRAY) {
210 aws_array_list_deep_clean_up(&aws_endpoints_value->v.array, s_endpoints_value_clean_up_cb);
211 }
212
213 on_done:
214 AWS_ZERO_STRUCT(*aws_endpoints_value);
215 }
216
217 void s_endpoints_value_clean_up_cb(void *value) {
218 struct aws_endpoints_value *aws_endpoints_value = value;
219 aws_endpoints_value_clean_up(aws_endpoints_value);
220 }
221
222 int aws_endpoints_deep_copy_parameter_value(
223 struct aws_allocator *allocator,
224 const struct aws_endpoints_value *from,
225 struct aws_endpoints_value *to) {
226
227 to->type = from->type;
228 to->is_ref = false;
229
230 if (to->type == AWS_ENDPOINTS_VALUE_STRING) {
231 to->v.owning_cursor_string =
232 aws_endpoints_owning_cursor_from_cursor(allocator, from->v.owning_cursor_string.cur);
233 } else if (to->type == AWS_ENDPOINTS_VALUE_BOOLEAN) {
234 to->v.boolean = from->v.boolean;
235 } else if (to->type == AWS_ENDPOINTS_VALUE_ARRAY) {
236 size_t len = aws_array_list_length(&from->v.array);
237 aws_array_list_init_dynamic(&to->v.array, allocator, len, sizeof(struct aws_endpoints_value));
238 for (size_t i = 0; i < len; ++i) {
239 struct aws_endpoints_value val;
240 aws_array_list_get_at(&from->v.array, &val, i);
241
242 struct aws_endpoints_value to_val;
243 if (aws_endpoints_deep_copy_parameter_value(allocator, &val, &to_val)) {
244 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unexpected array element type.");
245 goto on_error;
246 }
247
248 aws_array_list_set_at(&to->v.array, &to_val, i);
249 }
250 } else {
251 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unexpected value type.");
252 return aws_raise_error(AWS_ERROR_INVALID_STATE);
253 }
254 return AWS_OP_SUCCESS;
255
256 on_error:
257 aws_endpoints_value_clean_up(to);
258 return AWS_OP_ERR;
259 }
260
261 static int s_resolve_templated_value_with_pathing(
262 struct aws_allocator *allocator,
263 struct aws_endpoints_resolution_scope *scope,
264 struct aws_byte_cursor template_cur,
265 struct aws_owning_cursor *out_owning_cursor) {
266
267 struct aws_endpoints_value resolved_value = {0};
268 struct aws_byte_cursor split = {0};
269 if (!aws_byte_cursor_next_split(&template_cur, '#', &split) || split.len == 0) {
270 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Invalid value in template string.");
271 goto on_error;
272 }
273
274 struct aws_hash_element *elem = NULL;
275 if (aws_hash_table_find(&scope->values, &split, &elem) || elem == NULL) {
276 AWS_LOGF_ERROR(
277 AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Templated value does not exist: " PRInSTR, AWS_BYTE_CURSOR_PRI(split));
278 goto on_error;
279 }
280
281 struct aws_endpoints_scope_value *scope_value = elem->value;
282 if (!aws_byte_cursor_next_split(&template_cur, '#', &split)) {
283 if (scope_value->value.type != AWS_ENDPOINTS_VALUE_STRING) {
284 AWS_LOGF_ERROR(
285 AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unexpected type: must be string if pathing is not provided");
286 goto on_error;
287 }
288
289 *out_owning_cursor = aws_endpoints_non_owning_cursor_create(scope_value->value.v.owning_cursor_string.cur);
290 return AWS_OP_SUCCESS;
291 }
292
293 if (scope_value->value.type == AWS_ENDPOINTS_VALUE_OBJECT) {
294 if (aws_endpoints_path_through_object(allocator, &scope_value->value, split, &resolved_value)) {
295 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to path through object.");
296 goto on_error;
297 }
298 } else if (scope_value->value.type == AWS_ENDPOINTS_VALUE_ARRAY) {
299 if (aws_endpoints_path_through_array(allocator, scope, &scope_value->value, split, &resolved_value)) {
300 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to path through array.");
301 goto on_error;
302 }
303 } else {
304 AWS_LOGF_ERROR(
305 AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE,
306 "Invalid value type for pathing through. type %d",
307 scope_value->value.type);
308 goto on_error;
309 }
310
311 if (resolved_value.type != AWS_ENDPOINTS_VALUE_STRING) {
312 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Templated string didn't resolve to string");
313 goto on_error;
314 }
315
316 if (resolved_value.v.owning_cursor_string.string != NULL) {
317 /* Transfer ownership of the underlying string. */
318 *out_owning_cursor = aws_endpoints_owning_cursor_from_string(resolved_value.v.owning_cursor_string.string);
319 resolved_value.v.owning_cursor_string.string = NULL;
320 } else {
321 /* Unlikely to get here since current pathing always return new string. */
322 *out_owning_cursor = aws_endpoints_non_owning_cursor_create(resolved_value.v.owning_cursor_string.cur);
323 }
324
325 aws_endpoints_value_clean_up(&resolved_value);
326
327 return AWS_OP_SUCCESS;
328
329 on_error:
330 aws_endpoints_value_clean_up(&resolved_value);
331 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
332 }
333
334 int aws_endpoints_resolve_template(
335 struct aws_byte_cursor template,
336 void *user_data,
337 struct aws_owning_cursor *out_cursor) {
338
339 struct resolve_template_callback_data *data = user_data;
340
341 if (s_resolve_templated_value_with_pathing(data->allocator, data->scope, template, out_cursor)) {
342 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve template value.");
343 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
344 ;
345 }
346
347 return AWS_OP_SUCCESS;
348 }
349
350 int aws_endpoints_resolve_expr(
351 struct aws_allocator *allocator,
352 uint16_t expr_ref,
353 struct aws_endpoints_resolution_scope *scope,
354 struct aws_endpoints_value *out_value) {
355
356 struct aws_endpoints_expr *expr;
357 if (aws_array_list_get_at_ptr(&scope->expr_index, (void **)&expr, expr_ref)) {
358 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
359 }
360
361 AWS_ZERO_STRUCT(*out_value);
362 switch (expr->type) {
363 case AWS_ENDPOINTS_EXPR_TEMPLATE_STRING: {
364 struct aws_byte_buf buf;
365 struct resolve_template_callback_data data = {.allocator = allocator, .scope = scope};
366 if (aws_byte_buf_init_from_resolved_templated_string(
367 allocator, &buf, expr->e.string, aws_endpoints_resolve_template, &data, false)) {
368 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve templated string.");
369 goto on_error;
370 }
371
372 out_value->type = AWS_ENDPOINTS_VALUE_STRING;
373 out_value->v.owning_cursor_string =
374 aws_endpoints_owning_cursor_from_string(aws_string_new_from_buf(allocator, &buf));
375 aws_byte_buf_clean_up(&buf);
376 break;
377 }
378 case AWS_ENDPOINTS_EXPR_STRING: {
379 out_value->type = AWS_ENDPOINTS_VALUE_STRING;
380 out_value->v.owning_cursor_string = aws_endpoints_non_owning_cursor_create(expr->e.string);
381 out_value->is_ref = true;
382 break;
383 }
384 case AWS_ENDPOINTS_EXPR_BOOLEAN: {
385 out_value->type = AWS_ENDPOINTS_VALUE_BOOLEAN;
386 out_value->v.boolean = expr->e.boolean;
387 break;
388 }
389 case AWS_ENDPOINTS_EXPR_NUMBER: {
390 out_value->type = AWS_ENDPOINTS_VALUE_NUMBER;
391 out_value->v.number = expr->e.number;
392 break;
393 }
394 case AWS_ENDPOINTS_EXPR_ARRAY: {
395 out_value->type = AWS_ENDPOINTS_VALUE_ARRAY;
396 {
397 size_t len = expr->e.array.len;
398 aws_array_list_init_dynamic(&out_value->v.array, allocator, len, sizeof(struct aws_endpoints_value));
399 for (size_t i = 0; i < len; ++i) {
400 struct aws_endpoints_value val;
401 if (aws_endpoints_resolve_expr(allocator, expr->e.array.ptr[i], scope, &val)) {
402 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve array element.");
403 aws_endpoints_value_clean_up(out_value);
404 goto on_error;
405 }
406 aws_array_list_set_at(&out_value->v.array, &val, i);
407 }
408 }
409 break;
410 }
411 case AWS_ENDPOINTS_EXPR_REFERENCE: {
412 struct aws_hash_element *element;
413 if (aws_hash_table_find(&scope->values, &expr->e.reference, &element)) {
414 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to deref.");
415 goto on_error;
416 }
417
418 if (element == NULL) {
419 out_value->type = AWS_ENDPOINTS_VALUE_NONE;
420 } else {
421 struct aws_endpoints_scope_value *aws_endpoints_scope_value = element->value;
422
423 *out_value = aws_endpoints_scope_value->value;
424 out_value->is_ref = true;
425 }
426 break;
427 }
428 case AWS_ENDPOINTS_EXPR_FUNCTION: {
429 if (aws_endpoints_dispatch_standard_lib_fn_resolve(
430 expr->e.function.fn, allocator, expr->e.function.args, scope, out_value)) {
431 goto on_error;
432 }
433 break;
434 }
435 case AWS_ENDPOINTS_EXPR_OBJECT: {
436 /* Object expressions are only used by the BDD engine, not the v1.0 tree engine. */
437 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Object expressions are not supported in v1.0 engine.");
438 goto on_error;
439 }
440 }
441
442 return AWS_OP_SUCCESS;
443
444 on_error:
445 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
446 }
447
448 static void s_endpoints_resolved_endpoint_destroy(void *data) {
449 if (data == NULL) {
450 return;
451 }
452
453 struct aws_endpoints_resolved_endpoint *resolved = data;
454 if (resolved->type == AWS_ENDPOINTS_RESOLVED_ENDPOINT) {
455 aws_byte_buf_clean_up(&resolved->r.endpoint.url);
456 aws_byte_buf_clean_up(&resolved->r.endpoint.properties);
457 aws_hash_table_clean_up(&resolved->r.endpoint.headers);
458 } else if (resolved->type == AWS_ENDPOINTS_RESOLVED_ERROR) {
459 aws_byte_buf_clean_up(&resolved->r.error);
460 }
461 aws_mem_release(resolved->allocator, resolved);
462 }
463
464 struct aws_endpoints_resolved_endpoint *aws_endpoints_resolved_endpoint_new(struct aws_allocator *allocator) {
465 AWS_PRECONDITION(allocator);
466
467 struct aws_endpoints_resolved_endpoint *resolved =
468 aws_mem_calloc(allocator, 1, sizeof(struct aws_endpoints_resolved_endpoint));
469 resolved->allocator = allocator;
470
471 aws_ref_count_init(&resolved->ref_count, resolved, s_endpoints_resolved_endpoint_destroy);
472
473 return resolved;
474 }
475
476 static void s_on_string_array_element_destroy(void *element) {
477 struct aws_string *str = *(struct aws_string **)element;
478 aws_string_destroy(str);
479 }
480
481 static void s_callback_headers_destroy(void *data) {
482 struct aws_array_list *array = data;
483 struct aws_allocator *alloc = array->alloc;
484 aws_array_list_deep_clean_up(array, s_on_string_array_element_destroy);
485 aws_mem_release(alloc, array);
486 }
487
488 int aws_endpoints_resolve_headers(
489 struct aws_allocator *allocator,
490 struct aws_endpoints_resolution_scope *scope,
491 struct aws_hash_table *headers,
492 struct aws_hash_table *out_headers) {
493
494 struct aws_endpoints_value value;
495 struct aws_array_list *resolved_headers = NULL;
496
497 if (aws_hash_table_init(
498 out_headers,
499 allocator,
500 aws_hash_table_get_entry_count(headers),
501 aws_hash_string,
502 aws_hash_callback_string_eq,
503 aws_hash_callback_string_destroy,
504 s_callback_headers_destroy)) {
505 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to init table for resolved headers");
506 goto on_error;
507 }
508
509 for (struct aws_hash_iter iter = aws_hash_iter_begin(headers); !aws_hash_iter_done(&iter);
510 aws_hash_iter_next(&iter)) {
511
512 struct aws_string *key = (struct aws_string *)iter.element.key;
513 struct aws_array_list *header_list = (struct aws_array_list *)iter.element.value;
514
515 resolved_headers = aws_mem_calloc(allocator, 1, sizeof(struct aws_array_list));
516 aws_array_list_init_dynamic(
517 resolved_headers, allocator, aws_array_list_length(header_list), sizeof(struct aws_string *));
518
519 for (size_t i = 0; i < aws_array_list_length(header_list); ++i) {
520 uint16_t ref;
521 if (aws_array_list_get_at(header_list, &ref, i)) {
522 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to get header.");
523 goto on_error;
524 }
525
526 if (aws_endpoints_resolve_expr(allocator, ref, scope, &value) || value.type != AWS_ENDPOINTS_VALUE_STRING) {
527 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve header expr.");
528 goto on_error;
529 }
530
531 struct aws_string *str = aws_string_new_from_cursor(allocator, &value.v.owning_cursor_string.cur);
532 if (aws_array_list_push_back(resolved_headers, &str)) {
533 aws_string_destroy(str);
534 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to add resolved header to result.");
535 goto on_error;
536 }
537
538 aws_endpoints_value_clean_up(&value);
539 }
540
541 if (aws_hash_table_put(out_headers, aws_string_clone_or_reuse(allocator, key), resolved_headers, NULL)) {
542 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to add resolved header to result.");
543 goto on_error;
544 }
545 }
546
547 return AWS_OP_SUCCESS;
548
549 on_error:
550 aws_endpoints_value_clean_up(&value);
551 if (resolved_headers != NULL) {
552 s_callback_headers_destroy(resolved_headers);
553 }
554 aws_hash_table_clean_up(out_headers);
555 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
556 }
557
558 AWS_STATIC_ASSERT(AWS_ENDPOINTS_VALUE_SIZE == 7);
559 bool aws_endpoints_is_value_truthy(const struct aws_endpoints_value *value) {
560 switch (value->type) {
561 case AWS_ENDPOINTS_VALUE_NONE:
562 return false;
563 case AWS_ENDPOINTS_VALUE_BOOLEAN:
564 return value->v.boolean;
565 case AWS_ENDPOINTS_VALUE_ARRAY:
566 case AWS_ENDPOINTS_VALUE_STRING:
567 case AWS_ENDPOINTS_VALUE_OBJECT:
568 return true;
569 case AWS_ENDPOINTS_VALUE_NUMBER:
570 return value->v.number != 0;
571 default:
572 AWS_ASSERT(false);
573 return false;
574 }
575 }
576
577 int aws_endpoints_argv_expect(
578 struct aws_allocator *allocator,
579 struct aws_endpoints_resolution_scope *scope,
580 struct aws_endpoints_args args,
581 size_t idx,
582 enum aws_endpoints_value_type expected_type,
583 struct aws_endpoints_value *out_value) {
584
585 AWS_ZERO_STRUCT(*out_value);
586 struct aws_endpoints_value argv_value = {0};
587
588 if (idx >= args.argc) {
589 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to parse argv");
590 goto on_error;
591 }
592
593 uint16_t expr_ref = args.argv[idx];
594
595 if (aws_endpoints_resolve_expr(allocator, expr_ref, scope, &argv_value)) {
596 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve argv.");
597 goto on_error;
598 }
599
600 if (expected_type != AWS_ENDPOINTS_VALUE_ANY && argv_value.type != expected_type) {
601 AWS_LOGF_ERROR(
602 AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE,
603 "Unexpected arg type actual: %u expected %u.",
604 argv_value.type,
605 expected_type);
606 goto on_error;
607 }
608
609 *out_value = argv_value;
610 return AWS_OP_SUCCESS;
611
612 on_error:
613 aws_endpoints_value_clean_up(&argv_value);
614 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
615 }