Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a query string helper from byte cursor directly #1080

Merged
merged 4 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions include/aws/common/uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ AWS_COMMON_API uint16_t aws_uri_port(const struct aws_uri *uri);
*/
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 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_query_string_next_param(struct aws_byte_cursor query_string, 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_query_string_params(struct aws_byte_cursor query_string, struct aws_array_list *out_params);

/**
* 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.
Expand Down
16 changes: 12 additions & 4 deletions source/uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ 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) {
bool aws_query_string_next_param(struct aws_byte_cursor query_string, struct aws_uri_param *param) {
/* If param is zeroed, then this is the first run. */
bool first_run = param->value.ptr == NULL;

Expand All @@ -230,7 +230,7 @@ bool aws_uri_query_string_next_param(const struct aws_uri *uri, struct aws_uri_p

/* The do-while is to skip over any empty substrings */
do {
if (!aws_byte_cursor_next_split(&uri->query_string, '&', &substr)) {
if (!aws_byte_cursor_next_split(&query_string, '&', &substr)) {
/* no more splits, done iterating */
return false;
}
Expand All @@ -252,10 +252,10 @@ bool aws_uri_query_string_next_param(const struct aws_uri *uri, struct aws_uri_p
return true;
}

int aws_uri_query_string_params(const struct aws_uri *uri, struct aws_array_list *out_params) {
int aws_query_string_params(struct aws_byte_cursor query_string_cursor, struct aws_array_list *out_params) {
struct aws_uri_param param;
AWS_ZERO_STRUCT(param);
while (aws_uri_query_string_next_param(uri, &param)) {
while (aws_query_string_next_param(query_string_cursor, &param)) {
if (aws_array_list_push_back(out_params, &param)) {
return AWS_OP_ERR;
}
Expand All @@ -264,6 +264,14 @@ int aws_uri_query_string_params(const struct aws_uri *uri, struct aws_array_list
return AWS_OP_SUCCESS;
}

bool aws_uri_query_string_next_param(const struct aws_uri *uri, struct aws_uri_param *param) {
return aws_query_string_next_param(uri->query_string, param);
}

int aws_uri_query_string_params(const struct aws_uri *uri, struct aws_array_list *out_params) {
return aws_query_string_params(uri->query_string, out_params);
}

static void s_parse_scheme(struct uri_parser *parser, struct aws_byte_cursor *str) {
const uint8_t *location_of_colon = memchr(str->ptr, ':', str->len);

Expand Down