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 2 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 @@ -136,6 +136,28 @@ AWS_COMMON_API bool aws_uri_query_string_next_param(const struct aws_uri *uri, s
*/
AWS_COMMON_API int aws_uri_query_string_params(const struct aws_uri *uri, struct aws_array_list *out_params);

/**
* 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in that other PR how do you plan to go from path_and_query to just query? Does that necessitate another helper?


/**
* 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);

/**
* Writes the uri path encoding of a cursor to a buffer. This is the modified version of rfc3986 used by
* sigv4 signing.
Expand Down
20 changes: 18 additions & 2 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_cursor, 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_cursor, '&', &substr)) {
/* no more splits, done iterating */
return false;
}
Expand All @@ -252,6 +252,22 @@ bool aws_uri_query_string_next_param(const struct aws_uri *uri, struct aws_uri_p
return true;
}

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_query_string_next_param(query_string_cursor, &param)) {
if (aws_array_list_push_back(out_params, &param)) {
return AWS_OP_ERR;
}
}

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) {
struct aws_uri_param param;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if possible, just have aws_uri_query_string_params() call into aws_query_string_params()

AWS_ZERO_STRUCT(param);
Expand Down