Skip to content

Commit 43c89de

Browse files
committed
update how to find query
1 parent 45f9fe3 commit 43c89de

1 file changed

Lines changed: 11 additions & 13 deletions

File tree

source/mqtt_iot_sdk_metrics.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,6 @@ int aws_mqtt_append_sdk_metrics_to_username(
104104
return AWS_OP_ERR;
105105
}
106106

107-
/* Build metrics string */
108-
struct aws_byte_buf metrics_string;
109-
if (aws_byte_buf_init(&metrics_string, allocator, AWS_IOT_MAX_USERNAME_SIZE)) {
110-
return AWS_OP_ERR;
111-
}
112-
113107
int result = AWS_OP_ERR;
114108
// The length of the base username part not including query parameters
115109
size_t base_username_length = 0;
@@ -121,14 +115,19 @@ int aws_mqtt_append_sdk_metrics_to_username(
121115
aws_array_list_init_dynamic(&params_list, allocator, DEFAULT_QUERY_PARAM_COUNT, sizeof(struct aws_uri_param));
122116

123117
if (local_original_username.len > 0) {
124-
struct aws_byte_cursor question_mark_find;
118+
struct aws_byte_cursor question_mark_find = local_original_username;
125119

126-
if (AWS_OP_SUCCESS ==
127-
aws_byte_cursor_find_exact(&local_original_username, &question_mark_str, &question_mark_find)) {
128-
base_username_length = question_mark_find.ptr - local_original_username.ptr;
120+
bool found_query = false;
121+
// Find last question mark
122+
while (AWS_OP_SUCCESS == aws_byte_cursor_find_exact(
123+
question_mark_find, &question_mark_str, &question_mark_find)) {
129124
// Advance cursor to skip the "?" character
130125
aws_byte_cursor_advance(&question_mark_find, 1);
131-
aws_byte_buf_append(&metrics_string, &question_mark_find);
126+
found_query = true;
127+
}
128+
129+
if (found_query) {
130+
base_username_length = question_mark_find.ptr - local_original_username.ptr;
132131
aws_query_string_params(question_mark_find, &params_list);
133132
} else {
134133
base_username_length = local_original_username.len;
@@ -168,7 +167,7 @@ int aws_mqtt_append_sdk_metrics_to_username(
168167
}
169168

170169
// Rebuild metrics string from params_list
171-
// First path to calculate total size
170+
// First parse to calculate total size
172171
size_t total_size = 0;
173172
s_build_username_query(&local_original_username, base_username_length, &params_list, NULL, &total_size);
174173

@@ -196,7 +195,6 @@ int aws_mqtt_append_sdk_metrics_to_username(
196195
if (result == AWS_OP_ERR && aws_byte_buf_is_valid(output_username)) {
197196
aws_byte_buf_clean_up(output_username);
198197
}
199-
aws_byte_buf_clean_up(&metrics_string);
200198
return result;
201199
}
202200

0 commit comments

Comments
 (0)