Skip to content
Draft
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
61 changes: 58 additions & 3 deletions src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,28 @@ impl HttpRequestBuilder {
pub(crate) fn query<T: serde::Serialize + ?Sized>(mut self, query: &T) -> Self {
let mut error = None;
if let Ok(ref mut req) = self.request {
let mut out = format!("{}?", req.uri().path());
let start_position = out.len();
let mut encoder = form_urlencoded::Serializer::for_suffix(&mut out, start_position);
// Build the query string, preserving any existing query parameters.
// This prevents malformed URIs like "/bucket?&list-type=2" when the URI
// already contains query parameters.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to handle this case, we didn't before - we solely used the path?

Was this AI generated or something?

Choose a reason for hiding this comment

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

Not sure if it's really a bug in the library. A query string that starts with an ampersand works fine in php($_GET["var"]) and python3 urllib parse_qs. I am also unable to find any RFC or protocol specification that outlines that a query string should not/cannot begin with an ampersand.

let mut out = match req.uri().query() {
// If URI already has query params (e.g., "/bucket?existing=param"),
// preserve them: "/bucket?existing=param"
Some(existing_query) => format!("{}?{}", req.uri().path(), existing_query),
// If no existing query params, just add the "?": "/bucket?"
None => format!("{}?", req.uri().path()),
};

// Choose the appropriate form encoder based on whether we have existing parameters:
let mut encoder = if out.ends_with('?') {
// No existing params - use for_suffix to append after the "?"
// Result: "/bucket?list-type=2"
let start_position = out.len();
form_urlencoded::Serializer::for_suffix(&mut out, start_position)
} else {
// Has existing params - use new() to properly add "&" separators
// Result: "/bucket?existing=param&list-type=2"
form_urlencoded::Serializer::new(&mut out)
};
let serializer = serde_urlencoded::Serializer::new(&mut encoder);

if let Err(err) = query.serialize(serializer) {
Expand Down Expand Up @@ -321,6 +340,42 @@ mod tests {
.query(&[("foo", "1")]),
"http://example.com/?foo=1",
);

// Test that adding query parameters to a URI that already has query parameters
// doesn't create malformed URIs like "/bucket?&list-type=2"
assert_request_uri(
HttpRequestBuilder::new(client.clone())
.uri("http://example.com/bucket?existing=param")
.query(&[("list-type", "2")]),
"http://example.com/bucket?existing=param&list-type=2",
);

// Test multiple parameters
assert_request_uri(
HttpRequestBuilder::new(client.clone())
.uri("http://example.com/bucket")
.query(&[("list-type", "2"),
("prefix", "foo/")]),
"http://example.com/bucket?list-type=2&prefix=foo%2F",
);

// Test multiple parameters to existing URI
assert_request_uri(
HttpRequestBuilder::new(client.clone())
.uri("http://example.com/bucket?existing=param")
.query(&[("list-type", "2"),
("prefix", "foo/")]),
"http://example.com/bucket?existing=param&list-type=2&prefix=foo%2F",
);

// Test multiple appends
assert_request_uri(
HttpRequestBuilder::new(client.clone())
.uri("http://example.com/bucket?existing=param")
.query(&[("list-type", "2")])
.query(&[("prefix", "foo/")]),
"http://example.com/bucket?existing=param&list-type=2&prefix=foo%2F",
);
}

fn assert_request_uri(builder: HttpRequestBuilder, expected: &str) {
Expand Down
Loading