diff options
author | robot-contrib <robot-contrib@yandex-team.com> | 2022-10-28 07:50:04 +0300 |
---|---|---|
committer | robot-contrib <robot-contrib@yandex-team.com> | 2022-10-28 07:50:04 +0300 |
commit | 9b6cded108571fe7983a855c40c7545bf4b7aad2 (patch) | |
tree | bf5fe3a440a47d1a49f05eae25af134c0c831408 /contrib/restricted/aws | |
parent | e86badd466683de764e9ca22bc185566b43b82e6 (diff) | |
download | ydb-9b6cded108571fe7983a855c40c7545bf4b7aad2.tar.gz |
Update contrib/restricted/aws/aws-c-common to 0.8.3
Diffstat (limited to 'contrib/restricted/aws')
12 files changed, 985 insertions, 5 deletions
diff --git a/contrib/restricted/aws/aws-c-common/CMakeLists.darwin.txt b/contrib/restricted/aws/aws-c-common/CMakeLists.darwin.txt index 53563bd504..30a2c631ca 100644 --- a/contrib/restricted/aws/aws-c-common/CMakeLists.darwin.txt +++ b/contrib/restricted/aws/aws-c-common/CMakeLists.darwin.txt @@ -77,6 +77,7 @@ target_sources(restricted-aws-aws-c-common PRIVATE ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/task_scheduler.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/thread_scheduler.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/thread_shared.c + ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/uri.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/uuid.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/xml_parser.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/arch/intel/asm/cpuid.c diff --git a/contrib/restricted/aws/aws-c-common/CMakeLists.linux-aarch64.txt b/contrib/restricted/aws/aws-c-common/CMakeLists.linux-aarch64.txt index a65fdbb87c..b7fd181790 100644 --- a/contrib/restricted/aws/aws-c-common/CMakeLists.linux-aarch64.txt +++ b/contrib/restricted/aws/aws-c-common/CMakeLists.linux-aarch64.txt @@ -73,6 +73,7 @@ target_sources(restricted-aws-aws-c-common PRIVATE ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/task_scheduler.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/thread_scheduler.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/thread_shared.c + ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/uri.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/uuid.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/xml_parser.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/arch/arm/asm/cpuid.c diff --git a/contrib/restricted/aws/aws-c-common/CMakeLists.linux.txt b/contrib/restricted/aws/aws-c-common/CMakeLists.linux.txt index ce39ff5ab6..a81abf45ae 100644 --- a/contrib/restricted/aws/aws-c-common/CMakeLists.linux.txt +++ b/contrib/restricted/aws/aws-c-common/CMakeLists.linux.txt @@ -73,6 +73,7 @@ target_sources(restricted-aws-aws-c-common PRIVATE ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/task_scheduler.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/thread_scheduler.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/thread_shared.c + ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/uri.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/uuid.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/xml_parser.c ${CMAKE_SOURCE_DIR}/contrib/restricted/aws/aws-c-common/source/arch/intel/asm/cpuid.c diff --git a/contrib/restricted/aws/aws-c-common/include/aws/common/json.h b/contrib/restricted/aws/aws-c-common/include/aws/common/json.h index c8ee952f13..2061db173b 100644 --- a/contrib/restricted/aws/aws-c-common/include/aws/common/json.h +++ b/contrib/restricted/aws/aws-c-common/include/aws/common/json.h @@ -93,7 +93,7 @@ struct aws_json_value *aws_json_value_new_object(struct aws_allocator *allocator * Gets the string of a string aws_json_value. * @param value The string aws_json_value. * @param output The string - * @return AWS_OP_SUCESS if the value is a string, otherwise AWS_OP_ERR. + * @return AWS_OP_SUCCESS if the value is a string, otherwise AWS_OP_ERR. */ AWS_COMMON_API int aws_json_value_get_string(const struct aws_json_value *value, struct aws_byte_cursor *output); @@ -102,7 +102,7 @@ int aws_json_value_get_string(const struct aws_json_value *value, struct aws_byt * Gets the number of a number aws_json_value. * @param value The number aws_json_value. * @param output The number - * @return AWS_OP_SUCESS if the value is a number, otherwise AWS_OP_ERR. + * @return AWS_OP_SUCCESS if the value is a number, otherwise AWS_OP_ERR. */ AWS_COMMON_API int aws_json_value_get_number(const struct aws_json_value *value, double *output); @@ -111,7 +111,7 @@ int aws_json_value_get_number(const struct aws_json_value *value, double *output * Gets the boolean of a boolean aws_json_value. * @param value The boolean aws_json_value. * @param output The boolean - * @return AWS_OP_SUCESS if the value is a boolean, otherwise AWS_OP_ERR. + * @return AWS_OP_SUCCESS if the value is a boolean, otherwise AWS_OP_ERR. */ AWS_COMMON_API int aws_json_value_get_boolean(const struct aws_json_value *value, bool *output); @@ -166,6 +166,37 @@ bool aws_json_value_has_key(const struct aws_json_value *object, struct aws_byte */ AWS_COMMON_API int aws_json_value_remove_from_object(struct aws_json_value *object, struct aws_byte_cursor key); + +/** + * @brief callback for iterating members of an object + * Iteration can be controlled as follows: + * - return AWS_OP_SUCCESS and out_should_continue is set to true (default value) - + * continue iteration without error + * - return AWS_OP_SUCCESS and out_continue is set to false - + * stop iteration without error + * - return AWS_OP_ERR - stop iteration with error + */ +typedef int(aws_json_on_member_encountered_const_fn)( + const struct aws_byte_cursor *key, + const struct aws_json_value *value, + bool *out_should_continue, + void *user_data); + +/** + * @brief iterates through members of the object. + * iteration is sequential in order fields were initially parsed. + * @param object object to iterate over. + * @param on_member callback for when member is encountered. + * @param user_data user data to pass back in callback. + * @return AWS_OP_SUCCESS when iteration finishes completely or exits early, + * AWS_OP_ERR if value is not an object. + */ +AWS_COMMON_API +int aws_json_const_iterate_object( + const struct aws_json_value *object, + aws_json_on_member_encountered_const_fn *on_member, + void *user_data); + // ==================== // ==================== @@ -211,12 +242,61 @@ size_t aws_json_get_array_size(const struct aws_json_value *array); */ AWS_COMMON_API int aws_json_value_remove_array_element(struct aws_json_value *array, size_t index); + +/** + * @brief callback for iterating values of an array. + * Iteration can be controlled as follows: + * - return AWS_OP_SUCCESS and out_should_continue is set to true (default value) - + * continue iteration without error + * - return AWS_OP_SUCCESS and out_continue is set to false - + * stop iteration without error + * - return AWS_OP_ERR - stop iteration with error + */ +typedef int(aws_json_on_value_encountered_const_fn)( + size_t index, + const struct aws_json_value *value, + bool *out_should_continue, + void *user_data); + +/** + * @brief iterates through values of an array. + * iteration is sequential starting with 0th element. + * @param array array to iterate over. + * @param on_value callback for when value is encountered. + * @param user_data user data to pass back in callback. + * @return AWS_OP_SUCCESS when iteration finishes completely or exits early, + * AWS_OP_ERR if value is not an array. + */ +AWS_COMMON_API +int aws_json_const_iterate_array( + const struct aws_json_value *array, + aws_json_on_value_encountered_const_fn *on_value, + void *user_data); + // ==================== // ==================== // Checks /** + * Checks whether two json values are equivalent. + * @param a first value to compare. + * @param b second value to compare. + * @param is_case_sensitive case sensitive compare or not. + * @return True is values are equal, false otherwise + */ +AWS_COMMON_API +bool aws_json_value_compare(const struct aws_json_value *a, const struct aws_json_value *b, bool is_case_sensitive); + +/** + * Duplicates json value. + * @param value first value to compare. + * @return duplicated value. NULL and last error set if value cannot be duplicated. + */ +AWS_COMMON_API +struct aws_json_value *aws_json_value_duplicate(const struct aws_json_value *value); + +/** * Checks if the aws_json_value is a string. * @param value The aws_json_value to check. * @return True if the aws_json_value is a string aws_json_value, otherwise false. diff --git a/contrib/restricted/aws/aws-c-common/include/aws/common/linked_hash_table.h b/contrib/restricted/aws/aws-c-common/include/aws/common/linked_hash_table.h index 8f16034f49..ba17c9799c 100644 --- a/contrib/restricted/aws/aws-c-common/include/aws/common/linked_hash_table.h +++ b/contrib/restricted/aws/aws-c-common/include/aws/common/linked_hash_table.h @@ -17,6 +17,7 @@ struct aws_linked_hash_table { struct aws_linked_list list; struct aws_hash_table table; aws_hash_callback_destroy_fn *user_on_value_destroy; + aws_hash_callback_destroy_fn *user_on_key_destroy; }; /** diff --git a/contrib/restricted/aws/aws-c-common/include/aws/common/logging.h b/contrib/restricted/aws/aws-c-common/include/aws/common/logging.h index d80ffba410..9a5bc8fad4 100644 --- a/contrib/restricted/aws/aws-c-common/include/aws/common/logging.h +++ b/contrib/restricted/aws/aws-c-common/include/aws/common/logging.h @@ -84,6 +84,7 @@ enum aws_common_log_subject { AWS_LS_COMMON_IO, AWS_LS_COMMON_BUS, AWS_LS_COMMON_TEST, + AWS_LS_COMMON_JSON_PARSER, AWS_LS_COMMON_LAST = AWS_LOG_SUBJECT_END_RANGE(AWS_C_COMMON_PACKAGE_ID) }; @@ -137,6 +138,13 @@ struct aws_logger { } /** + * Unconditional logging macro that takes a logger and does not do a level check or a null check. Intended for + * situations when you need to log many things and do a single manual level check before beginning. + */ +#define AWS_LOGUF(logger, log_level, subject, ...) \ + { logger->vtable->log(logger, log_level, subject, __VA_ARGS__); } + +/** * LOGF_<level> variants for each level. These are what should be used directly to do all logging. * * i.e. @@ -223,6 +231,16 @@ AWS_COMMON_API struct aws_logger *aws_logger_get(void); /** + * Gets the aws logger used globally across the process if the logging level is at least the inputted level. + * + * @param subject log subject to perform the level check versus, not currently used + * @param level logging level to check against in order to return the logger + * @return the current logger if the current logging level is at or more detailed then the supplied logging level + */ +AWS_COMMON_API +struct aws_logger *aws_logger_get_conditional(aws_log_subject_t subject, enum aws_log_level level); + +/** * Cleans up all resources used by the logger; simply invokes the clean_up v-function */ AWS_COMMON_API diff --git a/contrib/restricted/aws/aws-c-common/include/aws/common/uri.h b/contrib/restricted/aws/aws-c-common/include/aws/common/uri.h new file mode 100644 index 0000000000..c2a55372cf --- /dev/null +++ b/contrib/restricted/aws/aws-c-common/include/aws/common/uri.h @@ -0,0 +1,162 @@ +#ifndef AWS_COMMON_URI_H +#define AWS_COMMON_URI_H +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include <aws/common/byte_buf.h> + +/** + * Data representing a URI. uri_str is always allocated and filled in. + * The other portions are merely storing offsets into uri_str. + */ +struct aws_uri { + size_t self_size; + struct aws_allocator *allocator; + struct aws_byte_buf uri_str; + struct aws_byte_cursor scheme; + struct aws_byte_cursor authority; + struct aws_byte_cursor userinfo; + struct aws_byte_cursor user; + struct aws_byte_cursor password; + struct aws_byte_cursor host_name; + uint16_t port; + struct aws_byte_cursor path; + struct aws_byte_cursor query_string; + struct aws_byte_cursor path_and_query; +}; + +/** + * key/value pairs for a query string. If the query fragment was not in format key=value, the fragment value + * will be stored in key + */ +struct aws_uri_param { + struct aws_byte_cursor key; + struct aws_byte_cursor value; +}; + +/** + * Arguments for building a URI instance. All members must + * be initialized before passing them to aws_uri_init(). + * + * query_string and query_params are exclusive to each other. If you set + * query_string, do not prepend it with '?' + */ +struct aws_uri_builder_options { + struct aws_byte_cursor scheme; + struct aws_byte_cursor path; + struct aws_byte_cursor host_name; + uint16_t port; + struct aws_array_list *query_params; + struct aws_byte_cursor query_string; +}; + +AWS_EXTERN_C_BEGIN + +/** + * Parses 'uri_str' and initializes uri. Returns AWS_OP_SUCCESS, on success, AWS_OP_ERR on failure. + * After calling this function, the parts can be accessed. + */ +AWS_COMMON_API int aws_uri_init_parse( + struct aws_uri *uri, + struct aws_allocator *allocator, + const struct aws_byte_cursor *uri_str); + +/** + * Initializes uri to values specified in options. Returns AWS_OP_SUCCESS, on success, AWS_OP_ERR on failure. + * After calling this function, the parts can be accessed. + */ +AWS_COMMON_API int aws_uri_init_from_builder_options( + struct aws_uri *uri, + struct aws_allocator *allocator, + struct aws_uri_builder_options *options); +AWS_COMMON_API void aws_uri_clean_up(struct aws_uri *uri); + +/** + * Returns the scheme portion of the uri (e.g. http, https, ftp, ftps, etc...). If the scheme was not present + * in the uri, the returned value will be empty. It is the users job to determine the appropriate defaults + * if this field is empty, based on protocol, port, etc... + */ +AWS_COMMON_API const struct aws_byte_cursor *aws_uri_scheme(const struct aws_uri *uri); + +/** + * Returns the authority portion of the uri (host[:port]). If it was not present, this was a request uri. In that + * case, the value will be empty. + */ +AWS_COMMON_API const struct aws_byte_cursor *aws_uri_authority(const struct aws_uri *uri); + +/** + * Returns the path portion of the uri. If the original value was empty, this value will be "/". + */ +AWS_COMMON_API const struct aws_byte_cursor *aws_uri_path(const struct aws_uri *uri); + +/** + * Returns the query string portion of the uri, minus the '?'. If not present, this value will be empty. + */ +AWS_COMMON_API const struct aws_byte_cursor *aws_uri_query_string(const struct aws_uri *uri); + +/** + * Returns the 'host_name' portion of the authority. If no authority was present, this value will be empty. + */ +AWS_COMMON_API const struct aws_byte_cursor *aws_uri_host_name(const struct aws_uri *uri); + +/** + * Returns the port portion of the authority if it was present, otherwise, returns 0. + * If this is 0, it is the users job to determine the correct port based on scheme and protocol. + */ +AWS_COMMON_API uint16_t aws_uri_port(const struct aws_uri *uri); + +/** + * Returns the path and query portion of the uri (i.e., the thing you send across the wire). + */ +AWS_COMMON_API const struct aws_byte_cursor *aws_uri_path_and_query(const struct aws_uri *uri); + +/** + * For iterating over the params in the uri query string. + * `param` is an in/out argument used to track progress, it MUST be zeroed out to start. + * If true is returned, `param` contains the value of the next param. + * If false is returned, there are no further params. + * + * Edge cases: + * 1) Entries without '=' sign are treated as having a key and no value. + * Example: First param in query string "a&b=c" has key="a" value="" + * + * 2) Blank entries are skipped. + * Example: The only param in query string "&&a=b" is key="a" value="b" + */ +AWS_COMMON_API bool aws_uri_query_string_next_param(const struct aws_uri *uri, struct aws_uri_param *param); + +/** + * Parses query string and stores the parameters in 'out_params'. Returns AWS_OP_SUCCESS on success and + * AWS_OP_ERR on failure. The user is responsible for initializing out_params with item size of struct aws_query_param. + * The user is also responsible for cleaning up out_params when finished. + */ +AWS_COMMON_API int aws_uri_query_string_params(const struct aws_uri *uri, struct aws_array_list *out_params); + +/** + * Writes the uri path encoding of a cursor to a buffer. This is the modified version of rfc3986 used by + * sigv4 signing. + */ +AWS_COMMON_API int aws_byte_buf_append_encoding_uri_path( + struct aws_byte_buf *buffer, + const struct aws_byte_cursor *cursor); + +/** + * Writes the uri query param encoding (passthrough alnum + '-' '_' '~' '.') of a UTF-8 cursor to a buffer + * For example, reading "a b_c" would write "a%20b_c". + */ +AWS_COMMON_API int aws_byte_buf_append_encoding_uri_param( + struct aws_byte_buf *buffer, + const struct aws_byte_cursor *cursor); + +/** + * Writes the uri decoding of a UTF-8 cursor to a buffer, + * replacing %xx escapes by their single byte equivalent. + * For example, reading "a%20b_c" would write "a b_c". + */ +AWS_COMMON_API int aws_byte_buf_append_decoding_uri(struct aws_byte_buf *buffer, const struct aws_byte_cursor *cursor); + +AWS_EXTERN_C_END + +#endif /* AWS_COMMON_URI_H */ diff --git a/contrib/restricted/aws/aws-c-common/source/json.c b/contrib/restricted/aws/aws-c-common/source/json.c index 7f700af1fb..77e7c1d661 100644 --- a/contrib/restricted/aws/aws-c-common/source/json.c +++ b/contrib/restricted/aws/aws-c-common/source/json.c @@ -166,6 +166,37 @@ done: return result; } +int aws_json_const_iterate_object( + const struct aws_json_value *object, + aws_json_on_member_encountered_const_fn *on_member, + void *user_data) { + int result = AWS_OP_ERR; + + struct cJSON *cjson = (struct cJSON *)object; + if (!cJSON_IsObject(cjson)) { + aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); + goto done; + } + + const cJSON *key = NULL; + cJSON_ArrayForEach(key, cjson) { + bool should_continue = true; + struct aws_byte_cursor key_cur = aws_byte_cursor_from_c_str(key->string); + if (on_member(&key_cur, (struct aws_json_value *)key, &should_continue, user_data)) { + goto done; + } + + if (!should_continue) { + break; + } + } + + result = AWS_OP_SUCCESS; + +done: + return result; +} + int aws_json_value_add_array_element(struct aws_json_value *array, const struct aws_json_value *value) { struct cJSON *cjson = (struct cJSON *)array; @@ -222,6 +253,59 @@ int aws_json_value_remove_array_element(struct aws_json_value *array, size_t ind return AWS_OP_SUCCESS; } +int aws_json_const_iterate_array( + const struct aws_json_value *array, + aws_json_on_value_encountered_const_fn *on_value, + void *user_data) { + int result = AWS_OP_ERR; + + struct cJSON *cjson = (struct cJSON *)array; + if (!cJSON_IsArray(cjson)) { + aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); + goto done; + } + + size_t idx = 0; + const cJSON *value = NULL; + cJSON_ArrayForEach(value, cjson) { + bool should_continue = true; + if (on_value(idx, (struct aws_json_value *)value, &should_continue, user_data)) { + goto done; + } + + if (!should_continue) { + break; + } + ++idx; + } + + result = AWS_OP_SUCCESS; + +done: + return result; +} + +bool aws_json_value_compare(const struct aws_json_value *a, const struct aws_json_value *b, bool is_case_sensitive) { + struct cJSON *cjson_a = (struct cJSON *)a; + struct cJSON *cjson_b = (struct cJSON *)b; + return cJSON_Compare(cjson_a, cjson_b, is_case_sensitive); +} + +struct aws_json_value *aws_json_value_duplicate(const struct aws_json_value *value) { + struct cJSON *cjson = (struct cJSON *)value; + if (cJSON_IsInvalid(cjson)) { + aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); + return NULL; + } + + struct cJSON *ret = cJSON_Duplicate(cjson, true); + if (ret == NULL) { + aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); + } + + return (void *)ret; +} + bool aws_json_value_is_string(const struct aws_json_value *value) { struct cJSON *cjson = (struct cJSON *)value; if (cJSON_IsInvalid(cjson)) { diff --git a/contrib/restricted/aws/aws-c-common/source/linked_hash_table.c b/contrib/restricted/aws/aws-c-common/source/linked_hash_table.c index 42c6a1b530..02452bbcc7 100644 --- a/contrib/restricted/aws/aws-c-common/source/linked_hash_table.c +++ b/contrib/restricted/aws/aws-c-common/source/linked_hash_table.c @@ -30,6 +30,7 @@ int aws_linked_hash_table_init( table->allocator = allocator; table->user_on_value_destroy = destroy_value_fn; + table->user_on_key_destroy = destroy_key_fn; aws_linked_list_init(&table->list); return aws_hash_table_init( @@ -95,7 +96,29 @@ int aws_linked_hash_table_put(struct aws_linked_hash_table *table, const void *k } if (element->value) { + AWS_ASSERT(!was_added); + + /* + * There's an existing element with a key that is "equal" to the submitted key. We need to destroy that + * existing element's value if applicable. + */ s_element_destroy(element->value); + + /* + * We're reusing an old element. The keys might be different references but "equal" via comparison. In that + * case we need to destroy the key (if appropriate) and point the element to the new key. This underhanded + * mutation of the element is safe with respect to the hash table because the keys are "equal." + */ + if (table->user_on_key_destroy && element->key != key) { + table->user_on_key_destroy((void *)element->key); + } + + /* + * Potentially a NOOP, but under certain circumstances (when the key and value are a part of the same structure + * and we're overwriting the existing entry, for example), this is necessary. Equality via function does not + * imply equal pointers. + */ + element->key = key; } node->value = p_value; diff --git a/contrib/restricted/aws/aws-c-common/source/logging.c b/contrib/restricted/aws/aws-c-common/source/logging.c index 8df9078cd3..f9a6c5a2e6 100644 --- a/contrib/restricted/aws/aws-c-common/source/logging.c +++ b/contrib/restricted/aws/aws-c-common/source/logging.c @@ -283,6 +283,18 @@ struct aws_logger *aws_logger_get(void) { return s_root_logger_ptr; } +struct aws_logger *aws_logger_get_conditional(aws_log_subject_t subject, enum aws_log_level level) { + if (s_root_logger_ptr == NULL) { + return NULL; + } + + if (s_root_logger_ptr->vtable->get_log_level(s_root_logger_ptr, subject) < level) { + return NULL; + } + + return s_root_logger_ptr; +} + void aws_logger_clean_up(struct aws_logger *logger) { AWS_ASSERT(logger->vtable->clean_up != NULL); diff --git a/contrib/restricted/aws/aws-c-common/source/posix/system_info.c b/contrib/restricted/aws/aws-c-common/source/posix/system_info.c index 4a856e5252..54bb502d80 100644 --- a/contrib/restricted/aws/aws-c-common/source/posix/system_info.c +++ b/contrib/restricted/aws/aws-c-common/source/posix/system_info.c @@ -421,9 +421,9 @@ void aws_backtrace_print(FILE *fp, void *call_site_data) { fprintf(fp, "No call stack information available\n"); } -size_t aws_backtrace(void **stack_frames, size_t size) { +size_t aws_backtrace(void **stack_frames, size_t num_frames) { (void)stack_frames; - (void)size; + (void)num_frames; return 0; } diff --git a/contrib/restricted/aws/aws-c-common/source/uri.c b/contrib/restricted/aws/aws-c-common/source/uri.c new file mode 100644 index 0000000000..f91f2921e3 --- /dev/null +++ b/contrib/restricted/aws/aws-c-common/source/uri.c @@ -0,0 +1,597 @@ +/** + * 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 */ +# pragma warning(disable : 4996) /* sprintf */ +#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}; + sprintf(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; +} |