| 1 |
|
| 2 |
|
| 3 |
|
| 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/ref_count.h> |
| 10 |
#include <aws/common/string.h> |
| 11 |
#include <aws/sdkutils/private/endpoints_types_impl.h> |
| 12 |
#include <aws/sdkutils/private/endpoints_util.h> |
| 13 |
|
| 14 |
enum { |
| 15 |
|
| 16 |
s_expr_array_initial_size = 256 |
| 17 |
}; |
| 18 |
|
| 19 |
|
| 20 |
static struct aws_byte_cursor s_string_type_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("string"); |
| 21 |
static struct aws_byte_cursor s_boolean_type_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("boolean"); |
| 22 |
static struct aws_byte_cursor s_string_array_type_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("stringArray"); |
| 23 |
|
| 24 |
|
| 25 |
static struct aws_byte_cursor s_endpoint_type_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("endpoint"); |
| 26 |
static struct aws_byte_cursor s_error_type_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("error"); |
| 27 |
static struct aws_byte_cursor s_tree_type_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("tree"); |
| 28 |
|
| 29 |
static struct aws_byte_cursor s_supported_version = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("1.0"); |
| 30 |
|
| 31 |
static struct aws_byte_cursor s_empty_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(""); |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
struct aws_byte_cursor aws_endpoints_get_supported_ruleset_version(void) { |
| 36 |
return s_supported_version; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
enum aws_endpoints_parameter_type aws_endpoints_parameter_get_type(const struct aws_endpoints_parameter *parameter) { |
| 45 |
AWS_PRECONDITION(parameter); |
| 46 |
return parameter->type; |
| 47 |
} |
| 48 |
|
| 49 |
struct aws_byte_cursor aws_endpoints_parameter_get_built_in(const struct aws_endpoints_parameter *parameter) { |
| 50 |
AWS_PRECONDITION(parameter); |
| 51 |
return parameter->built_in; |
| 52 |
} |
| 53 |
|
| 54 |
int aws_endpoints_parameter_get_default_string( |
| 55 |
const struct aws_endpoints_parameter *parameter, |
| 56 |
struct aws_byte_cursor *out_cursor) { |
| 57 |
AWS_PRECONDITION(parameter); |
| 58 |
AWS_PRECONDITION(out_cursor); |
| 59 |
|
| 60 |
if (parameter->type == AWS_ENDPOINTS_PARAMETER_STRING) { |
| 61 |
*out_cursor = parameter->default_value.v.owning_cursor_string.cur; |
| 62 |
return AWS_OP_SUCCESS; |
| 63 |
}; |
| 64 |
|
| 65 |
*out_cursor = s_empty_cursor; |
| 66 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 67 |
} |
| 68 |
|
| 69 |
int aws_endpoints_parameter_get_default_boolean( |
| 70 |
const struct aws_endpoints_parameter *parameter, |
| 71 |
const bool **out_bool) { |
| 72 |
AWS_PRECONDITION(parameter); |
| 73 |
AWS_PRECONDITION(out_bool); |
| 74 |
|
| 75 |
if (parameter->type == AWS_ENDPOINTS_PARAMETER_BOOLEAN) { |
| 76 |
*out_bool = ¶meter->default_value.v.boolean; |
| 77 |
return AWS_OP_SUCCESS; |
| 78 |
}; |
| 79 |
|
| 80 |
*out_bool = NULL; |
| 81 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 82 |
} |
| 83 |
|
| 84 |
bool aws_endpoints_parameters_get_is_required(const struct aws_endpoints_parameter *parameter) { |
| 85 |
AWS_PRECONDITION(parameter); |
| 86 |
return parameter->is_required; |
| 87 |
} |
| 88 |
|
| 89 |
struct aws_byte_cursor aws_endpoints_parameter_get_documentation(const struct aws_endpoints_parameter *parameter) { |
| 90 |
AWS_PRECONDITION(parameter); |
| 91 |
return parameter->documentation; |
| 92 |
} |
| 93 |
|
| 94 |
bool aws_endpoints_parameters_get_is_deprecated(const struct aws_endpoints_parameter *parameter) { |
| 95 |
AWS_PRECONDITION(parameter); |
| 96 |
return parameter->is_deprecated; |
| 97 |
} |
| 98 |
|
| 99 |
struct aws_byte_cursor aws_endpoints_parameter_get_deprecated_message(const struct aws_endpoints_parameter *parameter) { |
| 100 |
AWS_PRECONDITION(parameter); |
| 101 |
return parameter->deprecated_message; |
| 102 |
} |
| 103 |
|
| 104 |
struct aws_byte_cursor aws_endpoints_parameter_get_deprecated_since(const struct aws_endpoints_parameter *parameter) { |
| 105 |
AWS_PRECONDITION(parameter); |
| 106 |
return parameter->deprecated_since; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
const struct aws_hash_table *aws_endpoints_ruleset_get_parameters(struct aws_endpoints_ruleset *ruleset) { |
| 116 |
AWS_PRECONDITION(ruleset); |
| 117 |
return &ruleset->parameters; |
| 118 |
} |
| 119 |
|
| 120 |
struct aws_byte_cursor aws_endpoints_ruleset_get_version(const struct aws_endpoints_ruleset *ruleset) { |
| 121 |
AWS_PRECONDITION(ruleset); |
| 122 |
return ruleset->version; |
| 123 |
} |
| 124 |
|
| 125 |
struct aws_byte_cursor aws_endpoints_ruleset_get_service_id(const struct aws_endpoints_ruleset *ruleset) { |
| 126 |
AWS_PRECONDITION(ruleset); |
| 127 |
return ruleset->service_id; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
static void s_on_rule_array_element_clean_up(void *element) { |
| 137 |
struct aws_endpoints_rule *rule = element; |
| 138 |
aws_endpoints_rule_clean_up(rule); |
| 139 |
} |
| 140 |
|
| 141 |
static void s_on_expr_element_clean_up(void *data) { |
| 142 |
struct aws_endpoints_expr *expr = data; |
| 143 |
aws_endpoints_expr_clean_up(expr); |
| 144 |
} |
| 145 |
|
| 146 |
static void s_callback_endpoints_parameter_destroy(void *data) { |
| 147 |
struct aws_endpoints_parameter *parameter = data; |
| 148 |
aws_endpoints_parameter_destroy(parameter); |
| 149 |
} |
| 150 |
|
| 151 |
static void s_callback_headers_destroy(void *data) { |
| 152 |
struct aws_array_list *array = data; |
| 153 |
struct aws_allocator *alloc = array->alloc; |
| 154 |
aws_array_list_clean_up(array); |
| 155 |
aws_mem_release(alloc, array); |
| 156 |
} |
| 157 |
|
| 158 |
struct array_parser_wrapper { |
| 159 |
struct aws_allocator *allocator; |
| 160 |
struct aws_array_list *array; |
| 161 |
struct aws_endpoints_ruleset *ruleset; |
| 162 |
}; |
| 163 |
|
| 164 |
static int s_init_arraylist_from_json( |
| 165 |
struct aws_endpoints_ruleset *ruleset, |
| 166 |
const struct aws_json_value *value_node, |
| 167 |
struct aws_array_list *values, |
| 168 |
aws_json_on_value_encountered_const_fn *value_fn) { |
| 169 |
AWS_PRECONDITION(ruleset); |
| 170 |
AWS_PRECONDITION(values); |
| 171 |
AWS_PRECONDITION(value_node); |
| 172 |
AWS_PRECONDITION(value_fn); |
| 173 |
|
| 174 |
struct array_parser_wrapper wrapper = { |
| 175 |
.allocator = ruleset->allocator, |
| 176 |
.array = values, |
| 177 |
.ruleset = ruleset, |
| 178 |
}; |
| 179 |
|
| 180 |
if (aws_json_const_iterate_array(value_node, value_fn, &wrapper)) { |
| 181 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to iterate through array."); |
| 182 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 183 |
} |
| 184 |
|
| 185 |
return AWS_OP_SUCCESS; |
| 186 |
} |
| 187 |
|
| 188 |
struct ref_array_parser_wrapper { |
| 189 |
struct aws_allocator *allocator; |
| 190 |
uint16_t *ref_ptr; |
| 191 |
uint16_t len; |
| 192 |
struct aws_endpoints_ruleset *ruleset; |
| 193 |
}; |
| 194 |
|
| 195 |
static int s_init_ref_array_from_json( |
| 196 |
struct aws_endpoints_ruleset *ruleset, |
| 197 |
const struct aws_json_value *value_node, |
| 198 |
uint16_t *ref_ptr, |
| 199 |
uint16_t len, |
| 200 |
aws_json_on_value_encountered_const_fn *value_fn) { |
| 201 |
AWS_PRECONDITION(ruleset); |
| 202 |
AWS_PRECONDITION(ref_ptr); |
| 203 |
AWS_PRECONDITION(value_node); |
| 204 |
AWS_PRECONDITION(value_fn); |
| 205 |
|
| 206 |
struct ref_array_parser_wrapper wrapper = { |
| 207 |
.allocator = ruleset->allocator, |
| 208 |
.ref_ptr = ref_ptr, |
| 209 |
.len = len, |
| 210 |
.ruleset = ruleset, |
| 211 |
}; |
| 212 |
|
| 213 |
if (aws_json_const_iterate_array(value_node, value_fn, &wrapper)) { |
| 214 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to iterate through array."); |
| 215 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 216 |
} |
| 217 |
|
| 218 |
return AWS_OP_SUCCESS; |
| 219 |
} |
| 220 |
|
| 221 |
struct member_parser_wrapper { |
| 222 |
struct aws_allocator *allocator; |
| 223 |
struct aws_hash_table *table; |
| 224 |
struct aws_endpoints_ruleset *ruleset; |
| 225 |
}; |
| 226 |
|
| 227 |
static int s_init_members_from_json( |
| 228 |
struct aws_endpoints_ruleset *ruleset, |
| 229 |
struct aws_json_value *node, |
| 230 |
struct aws_hash_table *table, |
| 231 |
aws_json_on_member_encountered_const_fn *member_fn) { |
| 232 |
AWS_PRECONDITION(ruleset); |
| 233 |
AWS_PRECONDITION(node); |
| 234 |
AWS_PRECONDITION(table); |
| 235 |
|
| 236 |
struct member_parser_wrapper wrapper = { |
| 237 |
.allocator = ruleset->allocator, |
| 238 |
.table = table, |
| 239 |
.ruleset = ruleset, |
| 240 |
}; |
| 241 |
|
| 242 |
if (aws_json_const_iterate_object(node, member_fn, &wrapper)) { |
| 243 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to iterate through member fields."); |
| 244 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 245 |
} |
| 246 |
|
| 247 |
return AWS_OP_SUCCESS; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
static int s_parse_function( |
| 257 |
struct aws_endpoints_ruleset *ruleset, |
| 258 |
const struct aws_json_value *node, |
| 259 |
struct aws_endpoints_function *function); |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
static int s_try_parse_reference(const struct aws_json_value *node, struct aws_byte_cursor *out_reference) { |
| 266 |
AWS_PRECONDITION(node); |
| 267 |
|
| 268 |
AWS_ZERO_STRUCT(*out_reference); |
| 269 |
|
| 270 |
struct aws_json_value *ref_node = aws_json_value_get_from_object_c_str(node, "ref"); |
| 271 |
if (ref_node != NULL && aws_json_value_get_string(ref_node, out_reference)) { |
| 272 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to parse ref."); |
| 273 |
AWS_ZERO_STRUCT(*out_reference); |
| 274 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 275 |
} |
| 276 |
|
| 277 |
return AWS_OP_SUCCESS; |
| 278 |
} |
| 279 |
|
| 280 |
static int s_parse_expr( |
| 281 |
struct aws_endpoints_ruleset *ruleset, |
| 282 |
const struct aws_json_value *node, |
| 283 |
struct aws_endpoints_expr *expr); |
| 284 |
|
| 285 |
static int s_on_expr_element( |
| 286 |
size_t idx, |
| 287 |
const struct aws_json_value *value_node, |
| 288 |
bool *out_should_continue, |
| 289 |
void *user_data) { |
| 290 |
(void)idx; |
| 291 |
(void)out_should_continue; |
| 292 |
AWS_PRECONDITION(value_node); |
| 293 |
AWS_PRECONDITION(user_data); |
| 294 |
|
| 295 |
struct ref_array_parser_wrapper *wrapper = user_data; |
| 296 |
|
| 297 |
struct aws_endpoints_expr expr; |
| 298 |
if (s_parse_expr(wrapper->ruleset, value_node, &expr)) { |
| 299 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to parse expr."); |
| 300 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 301 |
} |
| 302 |
|
| 303 |
uint16_t ref_idx = (uint16_t)aws_array_list_length(&wrapper->ruleset->exprs); |
| 304 |
aws_array_list_push_back(&wrapper->ruleset->exprs, &expr); |
| 305 |
|
| 306 |
if (AWS_UNLIKELY(idx >= wrapper->len)) { |
| 307 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected ref idx."); |
| 308 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 309 |
} |
| 310 |
wrapper->ref_ptr[idx] = ref_idx; |
| 311 |
|
| 312 |
return AWS_OP_SUCCESS; |
| 313 |
} |
| 314 |
|
| 315 |
static bool s_is_template_string(struct aws_byte_cursor cur) { |
| 316 |
for (size_t i = 0; i < cur.len; ++i) { |
| 317 |
if (cur.ptr[i] == '{') { |
| 318 |
return true; |
| 319 |
} |
| 320 |
} |
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
static int s_parse_expr( |
| 325 |
struct aws_endpoints_ruleset *ruleset, |
| 326 |
const struct aws_json_value *node, |
| 327 |
struct aws_endpoints_expr *expr) { |
| 328 |
AWS_PRECONDITION(ruleset); |
| 329 |
AWS_PRECONDITION(node); |
| 330 |
AWS_PRECONDITION(expr); |
| 331 |
|
| 332 |
AWS_ZERO_STRUCT(*expr); |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
if (aws_json_value_is_string(node) && !aws_json_value_get_string(node, &expr->e.string)) { |
| 337 |
expr->type = |
| 338 |
s_is_template_string(expr->e.string) ? AWS_ENDPOINTS_EXPR_TEMPLATE_STRING : AWS_ENDPOINTS_EXPR_STRING; |
| 339 |
return AWS_OP_SUCCESS; |
| 340 |
} else if (aws_json_value_is_number(node) && !aws_json_value_get_number(node, &expr->e.number)) { |
| 341 |
expr->type = AWS_ENDPOINTS_EXPR_NUMBER; |
| 342 |
return AWS_OP_SUCCESS; |
| 343 |
} else if (aws_json_value_is_boolean(node) && !aws_json_value_get_boolean(node, &expr->e.boolean)) { |
| 344 |
expr->type = AWS_ENDPOINTS_EXPR_BOOLEAN; |
| 345 |
return AWS_OP_SUCCESS; |
| 346 |
} else if (aws_json_value_is_array(node)) { |
| 347 |
expr->type = AWS_ENDPOINTS_EXPR_ARRAY; |
| 348 |
size_t num_elements = aws_json_get_array_size(node); |
| 349 |
expr->e.array.len = (uint16_t)num_elements; |
| 350 |
if (s_init_ref_array_from_json(ruleset, node, expr->e.array.ptr, (uint16_t)num_elements, s_on_expr_element)) { |
| 351 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to parse array value type."); |
| 352 |
goto on_error; |
| 353 |
} |
| 354 |
return AWS_OP_SUCCESS; |
| 355 |
} |
| 356 |
|
| 357 |
struct aws_byte_cursor reference; |
| 358 |
if (s_try_parse_reference(node, &reference)) { |
| 359 |
goto on_error; |
| 360 |
} |
| 361 |
|
| 362 |
if (reference.len > 0) { |
| 363 |
expr->type = AWS_ENDPOINTS_EXPR_REFERENCE; |
| 364 |
expr->e.reference = reference; |
| 365 |
return AWS_OP_SUCCESS; |
| 366 |
} |
| 367 |
|
| 368 |
expr->type = AWS_ENDPOINTS_EXPR_FUNCTION; |
| 369 |
if (s_parse_function(ruleset, node, &expr->e.function)) { |
| 370 |
goto on_error; |
| 371 |
} |
| 372 |
|
| 373 |
return AWS_OP_SUCCESS; |
| 374 |
|
| 375 |
on_error: |
| 376 |
aws_endpoints_expr_clean_up(expr); |
| 377 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to parse expr type"); |
| 378 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 379 |
} |
| 380 |
|
| 381 |
static int s_parse_function( |
| 382 |
struct aws_endpoints_ruleset *ruleset, |
| 383 |
const struct aws_json_value *node, |
| 384 |
struct aws_endpoints_function *function) { |
| 385 |
AWS_PRECONDITION(ruleset); |
| 386 |
AWS_PRECONDITION(node); |
| 387 |
|
| 388 |
AWS_ZERO_STRUCT(*function); |
| 389 |
|
| 390 |
struct aws_json_value *fn_node = aws_json_value_get_from_object_c_str(node, "fn"); |
| 391 |
if (fn_node == NULL) { |
| 392 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Node is not a function."); |
| 393 |
goto on_error; |
| 394 |
} |
| 395 |
|
| 396 |
struct aws_byte_cursor fn_cur; |
| 397 |
if (aws_json_value_get_string(fn_node, &fn_cur)) { |
| 398 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract fn name."); |
| 399 |
goto on_error; |
| 400 |
} |
| 401 |
|
| 402 |
function->fn = AWS_ENDPOINTS_FN_LAST; |
| 403 |
uint64_t hash = aws_hash_byte_cursor_ptr(&fn_cur); |
| 404 |
for (int idx = AWS_ENDPOINTS_FN_FIRST; idx < AWS_ENDPOINTS_FN_LAST; ++idx) { |
| 405 |
if (aws_endpoints_fn_name_hash[idx] == hash) { |
| 406 |
function->fn = idx; |
| 407 |
break; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
if (function->fn == AWS_ENDPOINTS_FN_LAST) { |
| 412 |
AWS_LOGF_ERROR( |
| 413 |
AWS_LS_SDKUTILS_ENDPOINTS_PARSING, |
| 414 |
"Could not map function name to function type: " PRInSTR, |
| 415 |
AWS_BYTE_CURSOR_PRI(fn_cur)); |
| 416 |
goto on_error; |
| 417 |
} |
| 418 |
|
| 419 |
struct aws_json_value *argv_node = aws_json_value_get_from_object_c_str(node, "argv"); |
| 420 |
if (argv_node == NULL || !aws_json_value_is_array(argv_node)) { |
| 421 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "No argv or unexpected type."); |
| 422 |
goto on_error; |
| 423 |
} |
| 424 |
|
| 425 |
function->args.argc = (uint16_t)aws_json_get_array_size(argv_node); |
| 426 |
|
| 427 |
if (s_init_ref_array_from_json(ruleset, argv_node, function->args.argv, function->args.argc, s_on_expr_element)) { |
| 428 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to parse argv."); |
| 429 |
goto on_error; |
| 430 |
} |
| 431 |
|
| 432 |
return AWS_OP_SUCCESS; |
| 433 |
|
| 434 |
on_error: |
| 435 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 436 |
} |
| 437 |
|
| 438 |
static int s_on_parameter_key( |
| 439 |
const struct aws_byte_cursor *key, |
| 440 |
const struct aws_json_value *value, |
| 441 |
bool *out_should_continue, |
| 442 |
void *user_data) { |
| 443 |
(void)out_should_continue; |
| 444 |
AWS_PRECONDITION(key); |
| 445 |
AWS_PRECONDITION(value); |
| 446 |
AWS_PRECONDITION(user_data); |
| 447 |
|
| 448 |
struct member_parser_wrapper *wrapper = user_data; |
| 449 |
|
| 450 |
struct aws_endpoints_parameter *parameter = aws_endpoints_parameter_new(wrapper->allocator, *key); |
| 451 |
|
| 452 |
|
| 453 |
struct aws_byte_cursor type_cur; |
| 454 |
struct aws_json_value *type_node = aws_json_value_get_from_object_c_str(value, "type"); |
| 455 |
if (type_node == NULL || aws_json_value_get_string(type_node, &type_cur)) { |
| 456 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract parameter type."); |
| 457 |
goto on_error; |
| 458 |
} |
| 459 |
|
| 460 |
enum aws_endpoints_parameter_type type; |
| 461 |
if (aws_byte_cursor_eq_ignore_case(&type_cur, &s_string_type_cur)) { |
| 462 |
type = AWS_ENDPOINTS_PARAMETER_STRING; |
| 463 |
} else if (aws_byte_cursor_eq_ignore_case(&type_cur, &s_boolean_type_cur)) { |
| 464 |
type = AWS_ENDPOINTS_PARAMETER_BOOLEAN; |
| 465 |
} else if (aws_byte_cursor_eq_ignore_case(&type_cur, &s_string_array_type_cur)) { |
| 466 |
type = AWS_ENDPOINTS_PARAMETER_STRING_ARRAY; |
| 467 |
} else { |
| 468 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected type for parameter."); |
| 469 |
goto on_error; |
| 470 |
} |
| 471 |
|
| 472 |
parameter->type = type; |
| 473 |
|
| 474 |
struct aws_json_value *documentation_node = aws_json_value_get_from_object_c_str(value, "documentation"); |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
if (documentation_node != NULL) { |
| 479 |
if (aws_json_value_get_string(documentation_node, ¶meter->documentation)) { |
| 480 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract parameter documentation."); |
| 481 |
goto on_error; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
struct aws_json_value *built_in_node = aws_json_value_get_from_object_c_str(value, "builtIn"); |
| 487 |
if (built_in_node != NULL) { |
| 488 |
if (aws_json_value_get_string(built_in_node, ¶meter->built_in)) { |
| 489 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected type for built-in parameter field."); |
| 490 |
goto on_error; |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
struct aws_json_value *required_node = aws_json_value_get_from_object_c_str(value, "required"); |
| 495 |
if (required_node != NULL) { |
| 496 |
if (!aws_json_value_is_boolean(required_node)) { |
| 497 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected type for required parameter field."); |
| 498 |
goto on_error; |
| 499 |
} |
| 500 |
aws_json_value_get_boolean(required_node, ¶meter->is_required); |
| 501 |
} |
| 502 |
|
| 503 |
struct aws_json_value *default_node = aws_json_value_get_from_object_c_str(value, "default"); |
| 504 |
parameter->has_default_value = default_node != NULL; |
| 505 |
if (default_node != NULL) { |
| 506 |
if (type == AWS_ENDPOINTS_PARAMETER_STRING && aws_json_value_is_string(default_node)) { |
| 507 |
struct aws_byte_cursor cur; |
| 508 |
aws_json_value_get_string(default_node, &cur); |
| 509 |
parameter->default_value.type = AWS_ENDPOINTS_VALUE_STRING; |
| 510 |
parameter->default_value.v.owning_cursor_string = aws_endpoints_non_owning_cursor_create(cur); |
| 511 |
} else if (type == AWS_ENDPOINTS_PARAMETER_BOOLEAN && aws_json_value_is_boolean(default_node)) { |
| 512 |
parameter->default_value.type = AWS_ENDPOINTS_VALUE_BOOLEAN; |
| 513 |
aws_json_value_get_boolean(default_node, ¶meter->default_value.v.boolean); |
| 514 |
} else if (type == AWS_ENDPOINTS_PARAMETER_STRING_ARRAY && aws_json_value_is_array(default_node)) { |
| 515 |
parameter->default_value.type = AWS_ENDPOINTS_VALUE_ARRAY; |
| 516 |
size_t len = aws_json_get_array_size(default_node); |
| 517 |
aws_array_list_init_dynamic( |
| 518 |
¶meter->default_value.v.array, wrapper->allocator, len, sizeof(struct aws_endpoints_value)); |
| 519 |
for (size_t i = 0; i < len; ++i) { |
| 520 |
struct aws_json_value *element = aws_json_get_array_element(default_node, i); |
| 521 |
if (!aws_json_value_is_string(element)) { |
| 522 |
AWS_LOGF_ERROR( |
| 523 |
AWS_LS_SDKUTILS_ENDPOINTS_PARSING, |
| 524 |
"Unexpected type for default parameter value. String array parameter must have string " |
| 525 |
"elements"); |
| 526 |
goto on_error; |
| 527 |
} |
| 528 |
|
| 529 |
struct aws_byte_cursor cur; |
| 530 |
aws_json_value_get_string(element, &cur); |
| 531 |
|
| 532 |
struct aws_endpoints_value val = { |
| 533 |
.is_ref = false, |
| 534 |
.type = AWS_ENDPOINTS_VALUE_STRING, |
| 535 |
.v.owning_cursor_string = aws_endpoints_non_owning_cursor_create(cur)}; |
| 536 |
|
| 537 |
aws_array_list_set_at(¶meter->default_value.v.array, &val, i); |
| 538 |
} |
| 539 |
} else { |
| 540 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected type for default parameter value."); |
| 541 |
goto on_error; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
struct aws_json_value *deprecated_node = aws_json_value_get_from_object_c_str(value, "deprecated"); |
| 546 |
if (deprecated_node != NULL) { |
| 547 |
struct aws_json_value *deprecated_message_node = |
| 548 |
aws_json_value_get_from_object_c_str(deprecated_node, "message"); |
| 549 |
if (deprecated_message_node != NULL && |
| 550 |
aws_json_value_get_string(deprecated_message_node, ¶meter->deprecated_message)) { |
| 551 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected value for deprecated message."); |
| 552 |
goto on_error; |
| 553 |
} |
| 554 |
|
| 555 |
struct aws_json_value *deprecated_since_node = aws_json_value_get_from_object_c_str(deprecated_node, "since"); |
| 556 |
if (deprecated_since_node != NULL && |
| 557 |
aws_json_value_get_string(deprecated_since_node, ¶meter->deprecated_since)) { |
| 558 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected value for deprecated since."); |
| 559 |
goto on_error; |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
if (aws_hash_table_put(wrapper->table, ¶meter->name, parameter, NULL)) { |
| 564 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to add parameter."); |
| 565 |
goto on_error; |
| 566 |
} |
| 567 |
return AWS_OP_SUCCESS; |
| 568 |
|
| 569 |
on_error: |
| 570 |
aws_endpoints_parameter_destroy(parameter); |
| 571 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 572 |
} |
| 573 |
|
| 574 |
static int s_on_condition_element( |
| 575 |
size_t idx, |
| 576 |
const struct aws_json_value *condition_node, |
| 577 |
bool *out_should_continue, |
| 578 |
void *user_data) { |
| 579 |
(void)idx; |
| 580 |
(void)out_should_continue; |
| 581 |
AWS_PRECONDITION(condition_node); |
| 582 |
AWS_PRECONDITION(user_data); |
| 583 |
|
| 584 |
struct array_parser_wrapper *wrapper = user_data; |
| 585 |
|
| 586 |
struct aws_endpoints_condition condition; |
| 587 |
AWS_ZERO_STRUCT(condition); |
| 588 |
|
| 589 |
struct aws_endpoints_expr expr = {0}; |
| 590 |
expr.type = AWS_ENDPOINTS_EXPR_FUNCTION; |
| 591 |
|
| 592 |
if (s_parse_function(wrapper->ruleset, condition_node, &expr.e.function)) { |
| 593 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to parse function."); |
| 594 |
goto on_error; |
| 595 |
} |
| 596 |
|
| 597 |
aws_array_list_push_back(&wrapper->ruleset->exprs, &expr); |
| 598 |
condition.expr_ref = (uint16_t)aws_array_list_length(&wrapper->ruleset->exprs) - 1; |
| 599 |
|
| 600 |
struct aws_json_value *assign_node = aws_json_value_get_from_object_c_str(condition_node, "assign"); |
| 601 |
if (assign_node != NULL && aws_json_value_get_string(assign_node, &condition.assign)) { |
| 602 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected value for assign."); |
| 603 |
goto on_error; |
| 604 |
} |
| 605 |
|
| 606 |
aws_array_list_push_back(wrapper->array, &condition); |
| 607 |
return AWS_OP_SUCCESS; |
| 608 |
|
| 609 |
on_error: |
| 610 |
aws_endpoints_condition_clean_up(&condition); |
| 611 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 612 |
} |
| 613 |
|
| 614 |
static int s_on_header_element( |
| 615 |
size_t idx, |
| 616 |
const struct aws_json_value *value, |
| 617 |
bool *out_should_continue, |
| 618 |
void *user_data) { |
| 619 |
(void)idx; |
| 620 |
(void)out_should_continue; |
| 621 |
AWS_PRECONDITION(value); |
| 622 |
AWS_PRECONDITION(user_data); |
| 623 |
struct array_parser_wrapper *wrapper = user_data; |
| 624 |
|
| 625 |
struct aws_endpoints_expr expr; |
| 626 |
if (s_parse_expr(wrapper->ruleset, value, &expr)) { |
| 627 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected format for header element."); |
| 628 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 629 |
} |
| 630 |
|
| 631 |
aws_array_list_push_back(&wrapper->ruleset->exprs, &expr); |
| 632 |
uint16_t ref = (uint16_t)aws_array_list_length(&wrapper->ruleset->exprs) - 1; |
| 633 |
aws_array_list_push_back(wrapper->array, &ref); |
| 634 |
return AWS_OP_SUCCESS; |
| 635 |
} |
| 636 |
|
| 637 |
static int s_on_headers_key( |
| 638 |
const struct aws_byte_cursor *key, |
| 639 |
const struct aws_json_value *value, |
| 640 |
bool *out_should_continue, |
| 641 |
void *user_data) { |
| 642 |
(void)out_should_continue; |
| 643 |
AWS_PRECONDITION(key); |
| 644 |
AWS_PRECONDITION(value); |
| 645 |
AWS_PRECONDITION(user_data); |
| 646 |
struct member_parser_wrapper *wrapper = user_data; |
| 647 |
|
| 648 |
if (!aws_json_value_is_array(value)) { |
| 649 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected format for header value."); |
| 650 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 651 |
} |
| 652 |
|
| 653 |
size_t num_elements = aws_json_get_array_size(value); |
| 654 |
struct aws_array_list *headers = aws_mem_calloc(wrapper->allocator, 1, sizeof(struct aws_array_list)); |
| 655 |
aws_array_list_init_dynamic(headers, wrapper->allocator, num_elements, sizeof(uint16_t)); |
| 656 |
if (s_init_arraylist_from_json(wrapper->ruleset, value, headers, s_on_header_element)) { |
| 657 |
goto on_error; |
| 658 |
} |
| 659 |
|
| 660 |
aws_hash_table_put(wrapper->table, aws_string_new_from_cursor(wrapper->allocator, key), headers, NULL); |
| 661 |
|
| 662 |
return AWS_OP_SUCCESS; |
| 663 |
|
| 664 |
on_error: |
| 665 |
if (headers) { |
| 666 |
s_callback_headers_destroy(headers); |
| 667 |
} |
| 668 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 669 |
} |
| 670 |
|
| 671 |
static int s_parse_endpoints_rule_data_endpoint( |
| 672 |
struct aws_endpoints_ruleset *ruleset, |
| 673 |
const struct aws_json_value *rule_node, |
| 674 |
struct aws_endpoints_rule_data_endpoint *data_rule) { |
| 675 |
AWS_PRECONDITION(ruleset); |
| 676 |
AWS_PRECONDITION(rule_node); |
| 677 |
AWS_PRECONDITION(data_rule); |
| 678 |
|
| 679 |
data_rule->allocator = ruleset->allocator; |
| 680 |
struct aws_json_value *url_node = aws_json_value_get_from_object_c_str(rule_node, "url"); |
| 681 |
if (url_node == NULL || aws_json_value_is_string(url_node)) { |
| 682 |
struct aws_endpoints_expr expr = {0}; |
| 683 |
aws_json_value_get_string(url_node, &expr.e.string); |
| 684 |
expr.type = |
| 685 |
s_is_template_string(expr.e.string) ? AWS_ENDPOINTS_EXPR_TEMPLATE_STRING : AWS_ENDPOINTS_EXPR_STRING; |
| 686 |
aws_array_list_push_back(&ruleset->exprs, &expr); |
| 687 |
data_rule->url_expr_ref = (uint16_t)aws_array_list_length(&ruleset->exprs) - 1; |
| 688 |
} else { |
| 689 |
struct aws_byte_cursor reference; |
| 690 |
if (s_try_parse_reference(url_node, &reference)) { |
| 691 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to parse reference."); |
| 692 |
goto on_error; |
| 693 |
} |
| 694 |
|
| 695 |
struct aws_endpoints_expr expr = {0}; |
| 696 |
|
| 697 |
if (reference.len > 0) { |
| 698 |
expr.type = AWS_ENDPOINTS_EXPR_REFERENCE; |
| 699 |
expr.e.reference = reference; |
| 700 |
} else { |
| 701 |
expr.type = AWS_ENDPOINTS_EXPR_FUNCTION; |
| 702 |
if (s_parse_function(ruleset, url_node, &expr.e.function)) { |
| 703 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to function."); |
| 704 |
goto on_error; |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
aws_array_list_push_back(&ruleset->exprs, &expr); |
| 709 |
data_rule->url_expr_ref = (uint16_t)aws_array_list_length(&ruleset->exprs) - 1; |
| 710 |
} |
| 711 |
|
| 712 |
struct aws_json_value *properties_node = aws_json_value_get_from_object_c_str(rule_node, "properties"); |
| 713 |
if (properties_node != NULL) { |
| 714 |
aws_byte_buf_init(&data_rule->properties, ruleset->allocator, 0); |
| 715 |
|
| 716 |
if (aws_byte_buf_append_json_string(properties_node, &data_rule->properties)) { |
| 717 |
aws_byte_buf_clean_up(&data_rule->properties); |
| 718 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract properties."); |
| 719 |
goto on_error; |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
aws_hash_table_init( |
| 729 |
&data_rule->headers, |
| 730 |
ruleset->allocator, |
| 731 |
20, |
| 732 |
aws_hash_string, |
| 733 |
aws_hash_callback_string_eq, |
| 734 |
aws_hash_callback_string_destroy, |
| 735 |
s_callback_headers_destroy); |
| 736 |
|
| 737 |
struct aws_json_value *headers_node = aws_json_value_get_from_object_c_str(rule_node, "headers"); |
| 738 |
if (headers_node != NULL) { |
| 739 |
|
| 740 |
if (s_init_members_from_json(ruleset, headers_node, &data_rule->headers, s_on_headers_key)) { |
| 741 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract headers."); |
| 742 |
goto on_error; |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
return AWS_OP_SUCCESS; |
| 747 |
|
| 748 |
on_error: |
| 749 |
aws_endpoints_rule_data_endpoint_clean_up(data_rule); |
| 750 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 751 |
} |
| 752 |
|
| 753 |
static int s_parse_endpoints_rule_data_error( |
| 754 |
struct aws_endpoints_ruleset *ruleset, |
| 755 |
const struct aws_json_value *error_node, |
| 756 |
struct aws_endpoints_rule_data_error *data_rule) { |
| 757 |
AWS_PRECONDITION(ruleset); |
| 758 |
AWS_PRECONDITION(error_node); |
| 759 |
AWS_PRECONDITION(data_rule); |
| 760 |
|
| 761 |
struct aws_endpoints_expr expr = {0}; |
| 762 |
|
| 763 |
if (aws_json_value_is_string(error_node)) { |
| 764 |
aws_json_value_get_string(error_node, &expr.e.string); |
| 765 |
expr.type = |
| 766 |
s_is_template_string(expr.e.string) ? AWS_ENDPOINTS_EXPR_TEMPLATE_STRING : AWS_ENDPOINTS_EXPR_STRING; |
| 767 |
aws_array_list_push_back(&ruleset->exprs, &expr); |
| 768 |
data_rule->error_expr_ref = (uint16_t)aws_array_list_length(&ruleset->exprs) - 1; |
| 769 |
|
| 770 |
return AWS_OP_SUCCESS; |
| 771 |
} |
| 772 |
|
| 773 |
struct aws_byte_cursor reference; |
| 774 |
if (s_try_parse_reference(error_node, &reference)) { |
| 775 |
goto on_error; |
| 776 |
} |
| 777 |
|
| 778 |
if (reference.len > 0) { |
| 779 |
expr.type = AWS_ENDPOINTS_EXPR_REFERENCE; |
| 780 |
expr.e.reference = reference; |
| 781 |
aws_array_list_push_back(&ruleset->exprs, &expr); |
| 782 |
data_rule->error_expr_ref = (uint16_t)aws_array_list_length(&ruleset->exprs) - 1; |
| 783 |
return AWS_OP_SUCCESS; |
| 784 |
} |
| 785 |
|
| 786 |
expr.type = AWS_ENDPOINTS_EXPR_FUNCTION; |
| 787 |
if (s_parse_function(ruleset, error_node, &expr.e.function)) { |
| 788 |
goto on_error; |
| 789 |
} |
| 790 |
|
| 791 |
aws_array_list_push_back(&ruleset->exprs, &expr); |
| 792 |
data_rule->error_expr_ref = (uint16_t)aws_array_list_length(&ruleset->exprs) - 1; |
| 793 |
|
| 794 |
return AWS_OP_SUCCESS; |
| 795 |
|
| 796 |
on_error: |
| 797 |
aws_endpoints_rule_data_error_clean_up(data_rule); |
| 798 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to parse error rule."); |
| 799 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 800 |
} |
| 801 |
|
| 802 |
static int s_on_rule_element( |
| 803 |
size_t idx, |
| 804 |
const struct aws_json_value *value, |
| 805 |
bool *out_should_continue, |
| 806 |
void *user_data); |
| 807 |
|
| 808 |
static int s_parse_endpoints_rule_data_tree( |
| 809 |
struct aws_endpoints_ruleset *ruleset, |
| 810 |
const struct aws_json_value *rule_node, |
| 811 |
struct aws_endpoints_rule_data_tree *rule_data) { |
| 812 |
AWS_PRECONDITION(ruleset); |
| 813 |
AWS_PRECONDITION(rule_node); |
| 814 |
AWS_PRECONDITION(rule_data); |
| 815 |
|
| 816 |
struct aws_json_value *rules_node = aws_json_value_get_from_object_c_str(rule_node, "rules"); |
| 817 |
if (rules_node == NULL || !aws_json_value_is_array(rules_node)) { |
| 818 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Rules node is missing or unexpected type."); |
| 819 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 820 |
} |
| 821 |
|
| 822 |
size_t num_rules = aws_json_get_array_size(rules_node); |
| 823 |
aws_array_list_init_dynamic(&rule_data->rules, ruleset->allocator, num_rules, sizeof(struct aws_endpoints_rule)); |
| 824 |
if (s_init_arraylist_from_json(ruleset, rules_node, &rule_data->rules, s_on_rule_element)) { |
| 825 |
aws_endpoints_rule_data_tree_clean_up(rule_data); |
| 826 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to parse rules."); |
| 827 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 828 |
} |
| 829 |
|
| 830 |
return AWS_OP_SUCCESS; |
| 831 |
} |
| 832 |
|
| 833 |
static int s_on_rule_element( |
| 834 |
size_t idx, |
| 835 |
const struct aws_json_value *value, |
| 836 |
bool *out_should_continue, |
| 837 |
void *user_data) { |
| 838 |
(void)idx; |
| 839 |
(void)out_should_continue; |
| 840 |
AWS_PRECONDITION(value); |
| 841 |
AWS_PRECONDITION(user_data); |
| 842 |
|
| 843 |
struct array_parser_wrapper *wrapper = user_data; |
| 844 |
|
| 845 |
|
| 846 |
struct aws_byte_cursor type_cur; |
| 847 |
struct aws_json_value *type_node = aws_json_value_get_from_object_c_str(value, "type"); |
| 848 |
if (type_node == NULL || aws_json_value_get_string(type_node, &type_cur)) { |
| 849 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract rule type."); |
| 850 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 851 |
} |
| 852 |
|
| 853 |
enum aws_endpoints_rule_type type; |
| 854 |
if (aws_byte_cursor_eq_ignore_case(&type_cur, &s_endpoint_type_cur)) { |
| 855 |
type = AWS_ENDPOINTS_RULE_ENDPOINT; |
| 856 |
} else if (aws_byte_cursor_eq_ignore_case(&type_cur, &s_error_type_cur)) { |
| 857 |
type = AWS_ENDPOINTS_RULE_ERROR; |
| 858 |
} else if (aws_byte_cursor_eq_ignore_case(&type_cur, &s_tree_type_cur)) { |
| 859 |
type = AWS_ENDPOINTS_RULE_TREE; |
| 860 |
} else { |
| 861 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected rule type."); |
| 862 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 863 |
} |
| 864 |
|
| 865 |
struct aws_endpoints_rule rule; |
| 866 |
AWS_ZERO_STRUCT(rule); |
| 867 |
rule.type = type; |
| 868 |
|
| 869 |
struct aws_json_value *conditions_node = aws_json_value_get_from_object_c_str(value, "conditions"); |
| 870 |
if (conditions_node == NULL || !aws_json_value_is_array(conditions_node)) { |
| 871 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Conditions node missing."); |
| 872 |
goto on_error; |
| 873 |
} |
| 874 |
|
| 875 |
size_t num_conditions = aws_json_get_array_size(conditions_node); |
| 876 |
aws_array_list_init_dynamic( |
| 877 |
&rule.conditions, wrapper->allocator, num_conditions, sizeof(struct aws_endpoints_condition)); |
| 878 |
|
| 879 |
if (s_init_arraylist_from_json(wrapper->ruleset, conditions_node, &rule.conditions, s_on_condition_element)) { |
| 880 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract conditions."); |
| 881 |
goto on_error; |
| 882 |
} |
| 883 |
|
| 884 |
switch (type) { |
| 885 |
case AWS_ENDPOINTS_RULE_ENDPOINT: { |
| 886 |
struct aws_json_value *endpoint_node = aws_json_value_get_from_object_c_str(value, "endpoint"); |
| 887 |
if (endpoint_node == NULL || |
| 888 |
s_parse_endpoints_rule_data_endpoint(wrapper->ruleset, endpoint_node, &rule.rule_data.endpoint)) { |
| 889 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract endpoint rule data."); |
| 890 |
goto on_error; |
| 891 |
} |
| 892 |
break; |
| 893 |
} |
| 894 |
case AWS_ENDPOINTS_RULE_ERROR: { |
| 895 |
struct aws_json_value *error_node = aws_json_value_get_from_object_c_str(value, "error"); |
| 896 |
if (error_node == NULL || |
| 897 |
s_parse_endpoints_rule_data_error(wrapper->ruleset, error_node, &rule.rule_data.error)) { |
| 898 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract error rule data."); |
| 899 |
goto on_error; |
| 900 |
} |
| 901 |
break; |
| 902 |
} |
| 903 |
case AWS_ENDPOINTS_RULE_TREE: { |
| 904 |
if (s_parse_endpoints_rule_data_tree(wrapper->ruleset, value, &rule.rule_data.tree)) { |
| 905 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract tree rule data."); |
| 906 |
goto on_error; |
| 907 |
} |
| 908 |
break; |
| 909 |
} |
| 910 |
default: |
| 911 |
AWS_FATAL_ASSERT(false); |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
struct aws_json_value *documentation_node = aws_json_value_get_from_object_c_str(value, "documentation"); |
| 916 |
if (documentation_node != NULL) { |
| 917 |
if (aws_json_value_get_string(documentation_node, &rule.documentation)) { |
| 918 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract parameter documentation."); |
| 919 |
goto on_error; |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
aws_array_list_push_back(wrapper->array, &rule); |
| 924 |
|
| 925 |
return AWS_OP_SUCCESS; |
| 926 |
|
| 927 |
on_error: |
| 928 |
aws_endpoints_rule_clean_up(&rule); |
| 929 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 930 |
} |
| 931 |
|
| 932 |
static int s_init_ruleset_from_json( |
| 933 |
struct aws_allocator *allocator, |
| 934 |
struct aws_endpoints_ruleset *ruleset, |
| 935 |
struct aws_byte_cursor json) { |
| 936 |
AWS_PRECONDITION(allocator); |
| 937 |
AWS_PRECONDITION(ruleset); |
| 938 |
AWS_PRECONDITION(aws_byte_cursor_is_valid(&json)); |
| 939 |
|
| 940 |
struct aws_json_value *root = aws_json_value_new_from_string(allocator, json); |
| 941 |
|
| 942 |
if (root == NULL) { |
| 943 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to parse provided string as json."); |
| 944 |
return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 945 |
} |
| 946 |
|
| 947 |
ruleset->json_root = root; |
| 948 |
|
| 949 |
struct aws_json_value *version_node = aws_json_value_get_from_object_c_str(root, "version"); |
| 950 |
if (version_node == NULL || aws_json_value_get_string(version_node, &ruleset->version)) { |
| 951 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract version."); |
| 952 |
aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_UNSUPPORTED_RULESET); |
| 953 |
goto on_error; |
| 954 |
} |
| 955 |
|
| 956 |
#ifdef ENDPOINTS_VERSION_CHECK |
| 957 |
if (!aws_byte_cursor_eq_c_str(&ruleset->version, &s_supported_version)) { |
| 958 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unsupported ruleset version."); |
| 959 |
aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_UNSUPPORTED_RULESET); |
| 960 |
goto on_error; |
| 961 |
} |
| 962 |
#endif |
| 963 |
|
| 964 |
struct aws_json_value *service_id_node = aws_json_value_get_from_object_c_str(root, "serviceId"); |
| 965 |
|
| 966 |
if (service_id_node != NULL && aws_json_value_get_string(service_id_node, &ruleset->service_id)) { |
| 967 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract serviceId."); |
| 968 |
aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_UNSUPPORTED_RULESET); |
| 969 |
goto on_error; |
| 970 |
} |
| 971 |
|
| 972 |
aws_array_list_init_dynamic( |
| 973 |
&ruleset->exprs, allocator, s_expr_array_initial_size, sizeof(struct aws_endpoints_expr)); |
| 974 |
|
| 975 |
aws_hash_table_init( |
| 976 |
&ruleset->parameters, |
| 977 |
allocator, |
| 978 |
20, |
| 979 |
aws_hash_byte_cursor_ptr, |
| 980 |
aws_endpoints_byte_cursor_eq, |
| 981 |
NULL, |
| 982 |
s_callback_endpoints_parameter_destroy); |
| 983 |
|
| 984 |
struct aws_json_value *parameters_node = aws_json_value_get_from_object_c_str(root, "parameters"); |
| 985 |
if (parameters_node == NULL || |
| 986 |
s_init_members_from_json(ruleset, parameters_node, &ruleset->parameters, s_on_parameter_key)) { |
| 987 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract parameters."); |
| 988 |
aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 989 |
goto on_error; |
| 990 |
} |
| 991 |
|
| 992 |
struct aws_json_value *rules_node = aws_json_value_get_from_object_c_str(root, "rules"); |
| 993 |
if (rules_node == NULL || !aws_json_value_is_array(rules_node)) { |
| 994 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Unexpected type for rules node."); |
| 995 |
aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 996 |
goto on_error; |
| 997 |
} |
| 998 |
size_t num_rules = aws_json_get_array_size(rules_node); |
| 999 |
aws_array_list_init_dynamic(&ruleset->rules, allocator, num_rules, sizeof(struct aws_endpoints_rule)); |
| 1000 |
if (s_init_arraylist_from_json(ruleset, rules_node, &ruleset->rules, s_on_rule_element)) { |
| 1001 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to extract rules."); |
| 1002 |
aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_PARSE_FAILED); |
| 1003 |
goto on_error; |
| 1004 |
} |
| 1005 |
|
| 1006 |
return AWS_OP_SUCCESS; |
| 1007 |
|
| 1008 |
on_error: |
| 1009 |
return AWS_OP_ERR; |
| 1010 |
} |
| 1011 |
|
| 1012 |
static void s_endpoints_ruleset_destroy(void *data) { |
| 1013 |
if (data == NULL) { |
| 1014 |
return; |
| 1015 |
} |
| 1016 |
|
| 1017 |
struct aws_endpoints_ruleset *ruleset = data; |
| 1018 |
|
| 1019 |
aws_hash_table_clean_up(&ruleset->parameters); |
| 1020 |
|
| 1021 |
aws_array_list_deep_clean_up(&ruleset->rules, s_on_rule_array_element_clean_up); |
| 1022 |
aws_array_list_deep_clean_up(&ruleset->exprs, s_on_expr_element_clean_up); |
| 1023 |
|
| 1024 |
aws_json_value_destroy(ruleset->json_root); |
| 1025 |
|
| 1026 |
aws_mem_release(ruleset->allocator, ruleset); |
| 1027 |
} |
| 1028 |
|
| 1029 |
struct aws_endpoints_ruleset *aws_endpoints_ruleset_new_from_string( |
| 1030 |
struct aws_allocator *allocator, |
| 1031 |
struct aws_byte_cursor ruleset_json) { |
| 1032 |
AWS_PRECONDITION(allocator); |
| 1033 |
AWS_PRECONDITION(aws_byte_cursor_is_valid(&ruleset_json)); |
| 1034 |
|
| 1035 |
struct aws_endpoints_ruleset *ruleset = aws_mem_calloc(allocator, 1, sizeof(struct aws_endpoints_ruleset)); |
| 1036 |
ruleset->allocator = allocator; |
| 1037 |
|
| 1038 |
if (s_init_ruleset_from_json(allocator, ruleset, ruleset_json)) { |
| 1039 |
s_endpoints_ruleset_destroy(ruleset); |
| 1040 |
return NULL; |
| 1041 |
} |
| 1042 |
|
| 1043 |
aws_ref_count_init(&ruleset->ref_count, ruleset, s_endpoints_ruleset_destroy); |
| 1044 |
|
| 1045 |
return ruleset; |
| 1046 |
} |
| 1047 |
|
| 1048 |
struct aws_endpoints_ruleset *aws_endpoints_ruleset_acquire(struct aws_endpoints_ruleset *ruleset) { |
| 1049 |
AWS_PRECONDITION(ruleset); |
| 1050 |
if (ruleset) { |
| 1051 |
aws_ref_count_acquire(&ruleset->ref_count); |
| 1052 |
} |
| 1053 |
return ruleset; |
| 1054 |
} |
| 1055 |
|
| 1056 |
struct aws_endpoints_ruleset *aws_endpoints_ruleset_release(struct aws_endpoints_ruleset *ruleset) { |
| 1057 |
if (ruleset) { |
| 1058 |
aws_ref_count_release(&ruleset->ref_count); |
| 1059 |
} |
| 1060 |
return NULL; |
| 1061 |
} |