| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/byte_buf.h> |
| 7 |
#include <aws/common/system_info.h> |
| 8 |
#include <aws/common/uri.h> |
| 9 |
#include <aws/mqtt/mqtt.h> |
| 10 |
#include <aws/mqtt/private/mqtt_iot_metrics.h> |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
|
| 14 |
|
| 15 |
const size_t AWS_IOT_MAX_USERNAME_SIZE = UINT16_MAX; |
| 16 |
const size_t DEFAULT_QUERY_PARAM_COUNT = 10; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
static struct aws_uri_param *s_find_param_by_key( |
| 31 |
const struct aws_array_list *params_list, |
| 32 |
const struct aws_byte_cursor key, |
| 33 |
size_t *out_index) { |
| 34 |
|
| 35 |
size_t params_count = aws_array_list_length(params_list); |
| 36 |
for (size_t i = 0; i < params_count; ++i) { |
| 37 |
struct aws_uri_param *param = NULL; |
| 38 |
aws_array_list_get_at_ptr(params_list, (void **)¶m, i); |
| 39 |
if (aws_byte_cursor_eq(¶m->key, &key)) { |
| 40 |
if (out_index) { |
| 41 |
*out_index = i; |
| 42 |
} |
| 43 |
return param; |
| 44 |
} |
| 45 |
} |
| 46 |
return NULL; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static bool s_add_param_if_not_exists( |
| 58 |
struct aws_array_list *params_list, |
| 59 |
const struct aws_byte_cursor key, |
| 60 |
const struct aws_byte_cursor value) { |
| 61 |
|
| 62 |
if (s_find_param_by_key(params_list, key, NULL) != NULL) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
struct aws_uri_param param = { |
| 67 |
.key = key, |
| 68 |
.value = value, |
| 69 |
}; |
| 70 |
aws_array_list_push_back(params_list, ¶m); |
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
static int s_parse_delimited_entries( |
| 84 |
const struct aws_byte_cursor content, |
| 85 |
const struct aws_byte_cursor delimiter, |
| 86 |
struct aws_array_list *out_entries) { |
| 87 |
|
| 88 |
AWS_ASSERT(delimiter.len > 0); |
| 89 |
|
| 90 |
struct aws_byte_cursor equals_delim = aws_byte_cursor_from_c_str("="); |
| 91 |
struct aws_byte_cursor entry_cursor; |
| 92 |
AWS_ZERO_STRUCT(entry_cursor); |
| 93 |
|
| 94 |
while (aws_byte_cursor_next_split_on_cursor(&content, delimiter, &entry_cursor)) { |
| 95 |
|
| 96 |
if (entry_cursor.len == 0) { |
| 97 |
continue; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
struct aws_uri_param entry_param; |
| 102 |
AWS_ZERO_STRUCT(entry_param); |
| 103 |
|
| 104 |
|
| 105 |
struct aws_byte_cursor working_cursor = entry_cursor; |
| 106 |
struct aws_byte_cursor equals_pos; |
| 107 |
if (aws_byte_cursor_find_exact(&working_cursor, &equals_delim, &equals_pos) == AWS_OP_SUCCESS) { |
| 108 |
size_t equals_offset = equals_pos.ptr - working_cursor.ptr; |
| 109 |
entry_param.key = aws_byte_cursor_advance(&working_cursor, equals_offset); |
| 110 |
|
| 111 |
aws_byte_cursor_advance(&working_cursor, 1); |
| 112 |
entry_param.value = working_cursor; |
| 113 |
} else { |
| 114 |
|
| 115 |
entry_param.key = entry_cursor; |
| 116 |
} |
| 117 |
|
| 118 |
aws_array_list_push_back(out_entries, &entry_param); |
| 119 |
} |
| 120 |
|
| 121 |
return AWS_OP_SUCCESS; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
static void s_merge_metadata_entries_no_overwrite( |
| 132 |
struct aws_array_list *existing_metadata_list, |
| 133 |
const struct aws_mqtt_metadata_entry *new_entries_list, |
| 134 |
const size_t new_entries_list_count) { |
| 135 |
|
| 136 |
for (size_t i = 0; i < new_entries_list_count; ++i) { |
| 137 |
const struct aws_mqtt_metadata_entry *new_entry = &new_entries_list[i]; |
| 138 |
s_add_param_if_not_exists(existing_metadata_list, new_entry->key, new_entry->value); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
static int s_build_delimited_params( |
| 158 |
const struct aws_array_list *params_list, |
| 159 |
const struct aws_byte_cursor *prefix, |
| 160 |
const struct aws_byte_cursor *suffix, |
| 161 |
const struct aws_byte_cursor entry_delimiter, |
| 162 |
struct aws_byte_buf *output_buf, |
| 163 |
size_t *out_size) { |
| 164 |
|
| 165 |
AWS_ASSERT(entry_delimiter.len > 0); |
| 166 |
|
| 167 |
size_t local_out_size = 0; |
| 168 |
struct aws_byte_cursor key_value_delim = aws_byte_cursor_from_c_str("="); |
| 169 |
|
| 170 |
size_t params_count = aws_array_list_length(params_list); |
| 171 |
|
| 172 |
|
| 173 |
if (params_count == 0) { |
| 174 |
if (out_size) { |
| 175 |
*out_size = 0; |
| 176 |
} |
| 177 |
return AWS_OP_SUCCESS; |
| 178 |
} |
| 179 |
|
| 180 |
if (prefix) { |
| 181 |
local_out_size += prefix->len; |
| 182 |
} |
| 183 |
if (suffix) { |
| 184 |
local_out_size += suffix->len; |
| 185 |
} |
| 186 |
|
| 187 |
if (output_buf && prefix && prefix->len > 0) { |
| 188 |
if (aws_byte_buf_append(output_buf, prefix)) { |
| 189 |
return AWS_OP_ERR; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
for (size_t i = 0; i < params_count; ++i) { |
| 194 |
struct aws_uri_param param; |
| 195 |
AWS_ZERO_STRUCT(param); |
| 196 |
aws_array_list_get_at(params_list, ¶m, i); |
| 197 |
|
| 198 |
|
| 199 |
if (i > 0) { |
| 200 |
if (output_buf) { |
| 201 |
if (aws_byte_buf_append(output_buf, &entry_delimiter)) { |
| 202 |
return AWS_OP_ERR; |
| 203 |
} |
| 204 |
} |
| 205 |
local_out_size += entry_delimiter.len; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
if (output_buf && param.key.len > 0) { |
| 210 |
if (aws_byte_buf_append(output_buf, ¶m.key)) { |
| 211 |
return AWS_OP_ERR; |
| 212 |
} |
| 213 |
} |
| 214 |
local_out_size += param.key.len; |
| 215 |
|
| 216 |
|
| 217 |
if (param.value.len > 0) { |
| 218 |
if (output_buf) { |
| 219 |
if (aws_byte_buf_append(output_buf, &key_value_delim) || |
| 220 |
aws_byte_buf_append(output_buf, ¶m.value)) { |
| 221 |
return AWS_OP_ERR; |
| 222 |
} |
| 223 |
} |
| 224 |
local_out_size += 1 + param.value.len; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if (output_buf && suffix && suffix->len > 0) { |
| 229 |
if (aws_byte_buf_append(output_buf, suffix)) { |
| 230 |
return AWS_OP_ERR; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
if (out_size) { |
| 235 |
*out_size = local_out_size; |
| 236 |
} |
| 237 |
|
| 238 |
return AWS_OP_SUCCESS; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
static int s_build_metadata_value( |
| 251 |
const struct aws_array_list *entries, |
| 252 |
struct aws_byte_buf *output_buf, |
| 253 |
size_t *out_size) { |
| 254 |
|
| 255 |
struct aws_byte_cursor open_paren = aws_byte_cursor_from_c_str("("); |
| 256 |
struct aws_byte_cursor close_paren = aws_byte_cursor_from_c_str(")"); |
| 257 |
struct aws_byte_cursor semicolon_delim = aws_byte_cursor_from_c_str(";"); |
| 258 |
|
| 259 |
return s_build_delimited_params(entries, &open_paren, &close_paren, semicolon_delim, output_buf, out_size); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
static int s_build_username_query( |
| 274 |
const struct aws_byte_cursor base_username, |
| 275 |
const size_t base_username_length, |
| 276 |
const struct aws_array_list *params_list, |
| 277 |
struct aws_byte_buf *output_username, |
| 278 |
size_t *out_final_username_size) { |
| 279 |
|
| 280 |
|
| 281 |
if (output_username) { |
| 282 |
if (!aws_byte_buf_write(output_username, base_username.ptr, base_username_length)) { |
| 283 |
return AWS_OP_ERR; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
if (out_final_username_size) { |
| 288 |
*out_final_username_size = base_username_length; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
struct aws_byte_cursor query_prefix = aws_byte_cursor_from_c_str("?"); |
| 293 |
struct aws_byte_cursor ampersand_delim = aws_byte_cursor_from_c_str("&"); |
| 294 |
|
| 295 |
size_t params_size = 0; |
| 296 |
if (s_build_delimited_params(params_list, &query_prefix, NULL, ampersand_delim, output_username, ¶ms_size)) { |
| 297 |
return AWS_OP_ERR; |
| 298 |
} |
| 299 |
|
| 300 |
if (out_final_username_size) { |
| 301 |
*out_final_username_size += params_size; |
| 302 |
} |
| 303 |
|
| 304 |
return AWS_OP_SUCCESS; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
int aws_mqtt_append_sdk_metrics_to_username( |
| 337 |
struct aws_allocator *allocator, |
| 338 |
const struct aws_byte_cursor *original_username, |
| 339 |
const struct aws_mqtt_iot_metrics *metrics, |
| 340 |
struct aws_byte_buf *output_username, |
| 341 |
size_t *out_full_username_size) { |
| 342 |
AWS_PRECONDITION( |
| 343 |
output_username == NULL || |
| 344 |
(aws_byte_buf_is_valid(output_username) && (output_username && output_username->buffer == NULL))); |
| 345 |
|
| 346 |
if (!allocator) { |
| 347 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 348 |
} |
| 349 |
const struct aws_byte_cursor local_original_username = |
| 350 |
original_username == NULL ? aws_byte_cursor_from_c_str("") : *original_username; |
| 351 |
|
| 352 |
if (!metrics) { |
| 353 |
if (out_full_username_size) { |
| 354 |
*out_full_username_size = local_original_username.len; |
| 355 |
} |
| 356 |
|
| 357 |
if (output_username) { |
| 358 |
return aws_byte_buf_init_copy_from_cursor(output_username, allocator, local_original_username); |
| 359 |
} |
| 360 |
|
| 361 |
return AWS_OP_SUCCESS; |
| 362 |
} |
| 363 |
|
| 364 |
if (aws_mqtt_validate_iot_metrics(metrics)) { |
| 365 |
return AWS_OP_ERR; |
| 366 |
} |
| 367 |
|
| 368 |
int result = AWS_OP_ERR; |
| 369 |
size_t base_username_length = 0; |
| 370 |
struct aws_byte_cursor question_mark_str = aws_byte_cursor_from_c_str("?"); |
| 371 |
struct aws_byte_cursor sdk_str = aws_byte_cursor_from_c_str("SDK"); |
| 372 |
struct aws_byte_cursor platform_str = aws_byte_cursor_from_c_str("Platform"); |
| 373 |
struct aws_byte_cursor metadata_str = aws_byte_cursor_from_c_str("Metadata"); |
| 374 |
|
| 375 |
struct aws_array_list username_params_list; |
| 376 |
aws_array_list_init_dynamic( |
| 377 |
&username_params_list, allocator, DEFAULT_QUERY_PARAM_COUNT, sizeof(struct aws_uri_param)); |
| 378 |
|
| 379 |
struct aws_byte_buf metadata_value_buf; |
| 380 |
AWS_ZERO_STRUCT(metadata_value_buf); |
| 381 |
|
| 382 |
struct aws_array_list metadata_param_list; |
| 383 |
AWS_ZERO_STRUCT(metadata_param_list); |
| 384 |
|
| 385 |
|
| 386 |
if (local_original_username.len > 0) { |
| 387 |
struct aws_byte_cursor question_mark_find = local_original_username; |
| 388 |
|
| 389 |
bool found_query = false; |
| 390 |
|
| 391 |
|
| 392 |
while (AWS_OP_SUCCESS == |
| 393 |
aws_byte_cursor_find_exact(&question_mark_find, &question_mark_str, &question_mark_find)) { |
| 394 |
|
| 395 |
aws_byte_cursor_advance(&question_mark_find, 1); |
| 396 |
found_query = true; |
| 397 |
} |
| 398 |
|
| 399 |
if (found_query) { |
| 400 |
|
| 401 |
base_username_length = question_mark_find.ptr - 1 - local_original_username.ptr; |
| 402 |
aws_query_string_params(question_mark_find, &username_params_list); |
| 403 |
} else { |
| 404 |
base_username_length = local_original_username.len; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
struct aws_byte_cursor sdk_value = |
| 410 |
metrics->library_name.len > 0 ? metrics->library_name : aws_byte_cursor_from_c_str("IoTDeviceSDK/C"); |
| 411 |
s_add_param_if_not_exists(&username_params_list, sdk_str, sdk_value); |
| 412 |
|
| 413 |
|
| 414 |
s_add_param_if_not_exists(&username_params_list, platform_str, aws_get_platform_build_os_string()); |
| 415 |
|
| 416 |
|
| 417 |
bool has_new_metadata = metrics->metadata_entries != NULL && metrics->metadata_count > 0; |
| 418 |
|
| 419 |
if (has_new_metadata) { |
| 420 |
struct aws_byte_cursor semicolon_delim = aws_byte_cursor_from_c_str(";"); |
| 421 |
bool should_merge_metadata = true; |
| 422 |
|
| 423 |
aws_array_list_init_dynamic( |
| 424 |
&metadata_param_list, allocator, DEFAULT_QUERY_PARAM_COUNT, sizeof(struct aws_uri_param)); |
| 425 |
|
| 426 |
|
| 427 |
size_t existing_metadata_index = 0; |
| 428 |
struct aws_uri_param *existing_metadata_param = |
| 429 |
s_find_param_by_key(&username_params_list, metadata_str, &existing_metadata_index); |
| 430 |
|
| 431 |
if (existing_metadata_param != NULL && existing_metadata_param->value.len > 0) { |
| 432 |
|
| 433 |
struct aws_byte_cursor existing_content = existing_metadata_param->value; |
| 434 |
if (existing_content.len >= 2 && existing_content.ptr[0] == '(' && |
| 435 |
existing_content.ptr[existing_content.len - 1] == ')') { |
| 436 |
aws_byte_cursor_advance(&existing_content, 1); |
| 437 |
existing_content.len -= 1; |
| 438 |
|
| 439 |
|
| 440 |
if (existing_content.len > 0) { |
| 441 |
s_parse_delimited_entries(existing_content, semicolon_delim, &metadata_param_list); |
| 442 |
} |
| 443 |
} else { |
| 444 |
|
| 445 |
AWS_LOGF_DEBUG( |
| 446 |
AWS_LS_MQTT_GENERAL, |
| 447 |
"Existing Metadata parameter has invalid format (expected parentheses). " |
| 448 |
"Keeping original value and skipping new metadata entries."); |
| 449 |
should_merge_metadata = false; |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
if (should_merge_metadata) { |
| 454 |
|
| 455 |
s_merge_metadata_entries_no_overwrite( |
| 456 |
&metadata_param_list, metrics->metadata_entries, metrics->metadata_count); |
| 457 |
|
| 458 |
|
| 459 |
size_t metadata_value_size = 0; |
| 460 |
s_build_metadata_value(&metadata_param_list, NULL, &metadata_value_size); |
| 461 |
|
| 462 |
|
| 463 |
if (aws_byte_buf_init(&metadata_value_buf, allocator, metadata_value_size)) { |
| 464 |
goto cleanup; |
| 465 |
} |
| 466 |
|
| 467 |
if (s_build_metadata_value(&metadata_param_list, &metadata_value_buf, NULL)) { |
| 468 |
goto cleanup; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
struct aws_uri_param metadata_params = { |
| 473 |
.key = metadata_str, |
| 474 |
.value = aws_byte_cursor_from_buf(&metadata_value_buf), |
| 475 |
}; |
| 476 |
if (existing_metadata_param != NULL) { |
| 477 |
|
| 478 |
aws_array_list_set_at(&username_params_list, &metadata_params, existing_metadata_index); |
| 479 |
} else { |
| 480 |
aws_array_list_push_back(&username_params_list, &metadata_params); |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
size_t total_size = 0; |
| 488 |
s_build_username_query(local_original_username, base_username_length, &username_params_list, NULL, &total_size); |
| 489 |
|
| 490 |
if (total_size > AWS_IOT_MAX_USERNAME_SIZE) { |
| 491 |
AWS_LOGF_ERROR( |
| 492 |
AWS_LS_MQTT_GENERAL, |
| 493 |
"Failed to append SDK metrics to username: resulting username exceeds max username size."); |
| 494 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 495 |
goto cleanup; |
| 496 |
} |
| 497 |
|
| 498 |
if (output_username && aws_byte_buf_init(output_username, allocator, total_size)) { |
| 499 |
goto cleanup; |
| 500 |
} |
| 501 |
|
| 502 |
if (s_build_username_query( |
| 503 |
local_original_username, |
| 504 |
base_username_length, |
| 505 |
&username_params_list, |
| 506 |
output_username, |
| 507 |
out_full_username_size)) { |
| 508 |
goto cleanup; |
| 509 |
} |
| 510 |
|
| 511 |
result = AWS_OP_SUCCESS; |
| 512 |
|
| 513 |
cleanup: |
| 514 |
aws_byte_buf_clean_up(&metadata_value_buf); |
| 515 |
aws_array_list_clean_up(&username_params_list); |
| 516 |
if (aws_array_list_is_valid(&metadata_param_list)) { |
| 517 |
aws_array_list_clean_up(&metadata_param_list); |
| 518 |
} |
| 519 |
|
| 520 |
if (result == AWS_OP_ERR) { |
| 521 |
aws_byte_buf_clean_up(output_username); |
| 522 |
} |
| 523 |
return result; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
size_t aws_mqtt_iot_metrics_compute_storage_size(const struct aws_mqtt_iot_metrics *metrics) { |
| 531 |
if (metrics == NULL) { |
| 532 |
return 0; |
| 533 |
} |
| 534 |
|
| 535 |
size_t storage_size = 0; |
| 536 |
storage_size += metrics->library_name.len; |
| 537 |
|
| 538 |
|
| 539 |
if (metrics->metadata_entries != NULL && metrics->metadata_count > 0) { |
| 540 |
for (size_t i = 0; i < metrics->metadata_count; ++i) { |
| 541 |
const struct aws_mqtt_metadata_entry *entry = &metrics->metadata_entries[i]; |
| 542 |
storage_size += entry->key.len; |
| 543 |
storage_size += entry->value.len; |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
return storage_size; |
| 548 |
} |
| 549 |
|
| 550 |
struct aws_mqtt_iot_metrics_storage *aws_mqtt_iot_metrics_storage_new( |
| 551 |
struct aws_allocator *allocator, |
| 552 |
const struct aws_mqtt_iot_metrics *metrics_options) { |
| 553 |
|
| 554 |
if (allocator == NULL || metrics_options == NULL) { |
| 555 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 556 |
return NULL; |
| 557 |
} |
| 558 |
|
| 559 |
struct aws_mqtt_iot_metrics_storage *metrics_storage = |
| 560 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt_iot_metrics_storage)); |
| 561 |
|
| 562 |
size_t storage_capacity = aws_mqtt_iot_metrics_compute_storage_size(metrics_options); |
| 563 |
if (aws_byte_buf_init(&metrics_storage->storage, allocator, storage_capacity)) { |
| 564 |
goto cleanup_storage; |
| 565 |
} |
| 566 |
|
| 567 |
metrics_storage->allocator = allocator; |
| 568 |
|
| 569 |
struct aws_mqtt_iot_metrics *storage_view = &metrics_storage->storage_view; |
| 570 |
|
| 571 |
if (metrics_options->library_name.len > 0) { |
| 572 |
metrics_storage->library_name = metrics_options->library_name; |
| 573 |
if (aws_byte_buf_append_and_update(&metrics_storage->storage, &metrics_storage->library_name)) { |
| 574 |
goto cleanup_storage; |
| 575 |
} |
| 576 |
storage_view->library_name = metrics_storage->library_name; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
if (metrics_options->metadata_entries != NULL && metrics_options->metadata_count > 0) { |
| 581 |
if (aws_array_list_init_dynamic( |
| 582 |
&metrics_storage->metadata_entries, |
| 583 |
allocator, |
| 584 |
metrics_options->metadata_count, |
| 585 |
sizeof(struct aws_mqtt_metadata_entry))) { |
| 586 |
goto cleanup_storage; |
| 587 |
} |
| 588 |
|
| 589 |
for (size_t i = 0; i < metrics_options->metadata_count; ++i) { |
| 590 |
struct aws_mqtt_metadata_entry entry; |
| 591 |
entry.key = metrics_options->metadata_entries[i].key; |
| 592 |
entry.value = metrics_options->metadata_entries[i].value; |
| 593 |
|
| 594 |
|
| 595 |
if (entry.key.len > 0) { |
| 596 |
if (aws_byte_buf_append_and_update(&metrics_storage->storage, &entry.key)) { |
| 597 |
goto cleanup_storage; |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
if (entry.value.len > 0) { |
| 603 |
if (aws_byte_buf_append_and_update(&metrics_storage->storage, &entry.value)) { |
| 604 |
goto cleanup_storage; |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
aws_array_list_push_back(&metrics_storage->metadata_entries, &entry); |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
storage_view->metadata_count = aws_array_list_length(&metrics_storage->metadata_entries); |
| 613 |
storage_view->metadata_entries = metrics_storage->metadata_entries.data; |
| 614 |
} |
| 615 |
|
| 616 |
return metrics_storage; |
| 617 |
|
| 618 |
cleanup_storage: |
| 619 |
if (aws_array_list_is_valid(&metrics_storage->metadata_entries)) { |
| 620 |
aws_array_list_clean_up(&metrics_storage->metadata_entries); |
| 621 |
} |
| 622 |
|
| 623 |
aws_byte_buf_clean_up(&metrics_storage->storage); |
| 624 |
aws_mem_release(allocator, metrics_storage); |
| 625 |
return NULL; |
| 626 |
} |
| 627 |
|
| 628 |
void aws_mqtt_iot_metrics_storage_destroy(struct aws_mqtt_iot_metrics_storage *metrics_storage) { |
| 629 |
if (metrics_storage == NULL) { |
| 630 |
return; |
| 631 |
} |
| 632 |
|
| 633 |
if (aws_array_list_is_valid(&metrics_storage->metadata_entries)) { |
| 634 |
aws_array_list_clean_up(&metrics_storage->metadata_entries); |
| 635 |
} |
| 636 |
|
| 637 |
aws_byte_buf_clean_up(&metrics_storage->storage); |
| 638 |
|
| 639 |
aws_mem_release(metrics_storage->allocator, metrics_storage); |
| 640 |
} |
| 641 |
|
| 642 |
int aws_mqtt_validate_iot_metrics(const struct aws_mqtt_iot_metrics *metrics) { |
| 643 |
if (metrics == NULL) { |
| 644 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 645 |
} |
| 646 |
|
| 647 |
if (aws_mqtt_validate_utf8_text(metrics->library_name)) { |
| 648 |
return AWS_OP_ERR; |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
if (metrics->metadata_entries != NULL && metrics->metadata_count > 0) { |
| 653 |
for (size_t i = 0; i < metrics->metadata_count; ++i) { |
| 654 |
if (aws_mqtt_validate_utf8_text(metrics->metadata_entries[i].key)) { |
| 655 |
return AWS_OP_ERR; |
| 656 |
} |
| 657 |
if (aws_mqtt_validate_utf8_text(metrics->metadata_entries[i].value)) { |
| 658 |
return AWS_OP_ERR; |
| 659 |
} |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
return AWS_OP_SUCCESS; |
| 664 |
} |
| 665 |
|
| 666 |
bool aws_mqtt_has_non_empty_username( |
| 667 |
const struct aws_byte_cursor *username, |
| 668 |
const struct aws_mqtt_iot_metrics_storage *metrics_storage) { |
| 669 |
return (username != NULL && username->len > 0) || metrics_storage != NULL; |
| 670 |
} |