1515const size_t AWS_IOT_MAX_USERNAME_SIZE = UINT16_MAX ;
1616const size_t DEFAULT_QUERY_PARAM_COUNT = 10 ;
1717
18- // Build username query string from params_list, the caller is responsible to init and clean up output_username
19- // If output_username is NULL, the function will just calculate the full username size and return it in
20- // out_full_username_size
18+ /**
19+ * Builds final username with query parameters appended.
20+ *
21+ * @param base_username The original username cursor
22+ * @param base_username_length Length of base username to use (may differ from cursor length)
23+ * @param params_list List of query parameters to append
24+ * @param output_username [Optional] Buffer to write result. Caller must init/cleanup the buffer.
25+ * @param out_final_username_size [Optional] Outputs the final username size
26+ *
27+ * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
28+ */
2129int s_build_username_query (
2230 const struct aws_byte_cursor * base_username ,
2331 size_t base_username_length ,
2432 const struct aws_array_list * params_list ,
2533 struct aws_byte_buf * output_username ,
26- size_t * out_full_username_size ) {
34+ size_t * out_final_username_size ) {
2735
2836 AWS_ASSERT (base_username );
2937 AWS_ASSERT (params_list );
@@ -34,8 +42,8 @@ int s_build_username_query(
3442 }
3543 }
3644
37- if (out_full_username_size ) {
38- * out_full_username_size = base_username_length ;
45+ if (out_final_username_size ) {
46+ * out_final_username_size = base_username_length ;
3947 }
4048
4149 struct aws_byte_cursor query_delim = aws_byte_cursor_from_c_str ("?" );
@@ -56,8 +64,8 @@ int s_build_username_query(
5664 }
5765 }
5866
59- if (out_full_username_size ) {
60- * out_full_username_size += 1 ;
67+ if (out_final_username_size ) {
68+ * out_final_username_size += 1 ;
6169 }
6270
6371 if (output_username ) {
@@ -78,8 +86,8 @@ int s_build_username_query(
7886 }
7987 }
8088
81- if (out_full_username_size ) {
82- * out_full_username_size += param .key .len + (param .value .len > 0 ? 1 : 0 ) + param .value .len ;
89+ if (out_final_username_size ) {
90+ * out_final_username_size += param .key .len + (param .value .len > 0 ? 1 : 0 ) + param .value .len ;
8391 }
8492 }
8593
@@ -93,6 +101,7 @@ int aws_mqtt_append_sdk_metrics_to_username(
93101 const struct aws_mqtt_iot_sdk_metrics * metrics ,
94102 struct aws_byte_buf * output_username ,
95103 size_t * out_full_username_size ) {
104+ AWS_PRECONDITION (aws_byte_buf_is_valid (output_username ) && output_username -> buffer == NULL );
96105
97106 if (!allocator ) {
98107 return aws_raise_error (AWS_ERROR_INVALID_ARGUMENT );
@@ -126,11 +135,13 @@ int aws_mqtt_append_sdk_metrics_to_username(
126135 struct aws_array_list params_list ;
127136 aws_array_list_init_dynamic (& params_list , allocator , DEFAULT_QUERY_PARAM_COUNT , sizeof (struct aws_uri_param ));
128137
138+ // Looking for any existing query in the original username
129139 if (local_original_username .len > 0 ) {
130140 struct aws_byte_cursor question_mark_find = local_original_username ;
131141
132142 bool found_query = false;
133- // Find last question mark
143+ // Find last question mark. The IoT service will trim string after last question mark and handle it as metrics
144+ // metadata
134145 while (AWS_OP_SUCCESS ==
135146 aws_byte_cursor_find_exact (& question_mark_find , & question_mark_str , & question_mark_find )) {
136147 // Advance cursor to skip the "?" character
@@ -147,6 +158,7 @@ int aws_mqtt_append_sdk_metrics_to_username(
147158 }
148159 }
149160
161+ // Verify if the username already contains "SDK" and "Platform" fields.
150162 bool found_sdk = false;
151163 bool found_platform = false;
152164
@@ -180,7 +192,7 @@ int aws_mqtt_append_sdk_metrics_to_username(
180192 }
181193
182194 // Rebuild metrics string from params_list
183- // First parse to calculate total size
195+ // First parse to get final username size
184196 size_t total_size = 0 ;
185197 s_build_username_query (& local_original_username , base_username_length , & params_list , NULL , & total_size );
186198
@@ -288,7 +300,7 @@ void aws_mqtt_iot_sdk_metrics_storage_destroy(struct aws_mqtt_iot_sdk_metrics_st
288300
289301int aws_mqtt_validate_iot_sdk_metrics (const struct aws_mqtt_iot_sdk_metrics * metrics ) {
290302 if (metrics == NULL ) {
291- return AWS_OP_SUCCESS ;
303+ return aws_raise_error ( AWS_ERROR_INVALID_ARGUMENT ) ;
292304 }
293305
294306 if (aws_mqtt_validate_utf8_text (metrics -> library_name )) {
0 commit comments