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

Fix request query strings #395

Merged
merged 1 commit into from
Jan 13, 2022
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
33 changes: 28 additions & 5 deletions lambda-http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ impl<'a> From<LambdaRequest<'a>> for http::Request<Body> {
.method(http_method)
.uri({
let host = headers.get(http::header::HOST).and_then(|val| val.to_str().ok());
match host {
let mut uri = match host {
Some(host) => {
format!(
"{}://{}{}",
Expand All @@ -454,7 +454,17 @@ impl<'a> From<LambdaRequest<'a>> for http::Request<Body> {
)
}
None => path.to_string(),
};

if !multi_value_query_string_parameters.is_empty() {
uri.push('?');
uri.push_str(multi_value_query_string_parameters.to_query_string().as_str());
} else if !query_string_parameters.is_empty() {
uri.push('?');
uri.push_str(query_string_parameters.to_query_string().as_str());
}

uri
})
// multi-valued query string parameters are always a super
// set of singly valued query string parameters,
Expand Down Expand Up @@ -507,7 +517,7 @@ impl<'a> From<LambdaRequest<'a>> for http::Request<Body> {
.method(http_method)
.uri({
let host = headers.get(http::header::HOST).and_then(|val| val.to_str().ok());
match host {
let mut uri = match host {
Some(host) => {
format!(
"{}://{}{}",
Expand All @@ -520,7 +530,17 @@ impl<'a> From<LambdaRequest<'a>> for http::Request<Body> {
)
}
None => path.to_string(),
};

if !multi_value_query_string_parameters.is_empty() {
uri.push('?');
uri.push_str(multi_value_query_string_parameters.to_query_string().as_str());
} else if !query_string_parameters.is_empty() {
uri.push('?');
uri.push_str(query_string_parameters.to_query_string().as_str());
}

uri
})
// multi valued query string parameters are always a super
// set of singly valued query string parameters,
Expand Down Expand Up @@ -698,7 +718,7 @@ mod tests {
assert_eq!(req.method(), "GET");
assert_eq!(
req.uri(),
"https://wt6mne2s9k.execute-api.us-west-2.amazonaws.com/test/hello"
"https://wt6mne2s9k.execute-api.us-west-2.amazonaws.com/test/hello?name=me"
);

// Ensure this is an APIGW request
Expand Down Expand Up @@ -727,7 +747,10 @@ mod tests {
);
let req = result.expect("failed to parse request");
assert_eq!(req.method(), "GET");
assert_eq!(req.uri(), "https://lambda-846800462-us-east-2.elb.amazonaws.com/");
assert_eq!(
req.uri(),
"https://lambda-846800462-us-east-2.elb.amazonaws.com/?myKey=val2"
);

// Ensure this is an ALB request
let req_context = req.request_context();
Expand Down Expand Up @@ -822,7 +845,7 @@ mod tests {
);
let req = result.expect("failed to parse request");
assert_eq!(req.method(), "GET");
assert_eq!(req.uri(), "/test/hello");
assert_eq!(req.uri(), "/test/hello?name=me");
}

#[test]
Expand Down
62 changes: 61 additions & 1 deletion lambda-http/src/strmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ impl StrMap {
StrMapIter {
data: self,
keys: self.0.keys(),
current: None,
next_idx: 0,
}
}

/// Return the URI query representation for this map
pub fn to_query_string(&self) -> String {
if self.is_empty() {
"".into()
} else {
self.iter()
.map(|(k, v)| format!("{}={}", k, v))
.collect::<Vec<_>>()
.join("&")
}
}
}
Expand All @@ -62,14 +76,40 @@ impl From<HashMap<String, Vec<String>>> for StrMap {
pub struct StrMapIter<'a> {
data: &'a StrMap,
keys: Keys<'a, String, Vec<String>>,
current: Option<(&'a String, Vec<&'a str>)>,
next_idx: usize,
}

impl<'a> Iterator for StrMapIter<'a> {
type Item = (&'a str, &'a str);

#[inline]
fn next(&mut self) -> Option<(&'a str, &'a str)> {
self.keys.next().and_then(|k| self.data.get(k).map(|v| (k.as_str(), v)))
if self.current.is_none() {
self.current = self.keys.next().map(|k| (k, self.data.get_all(k).unwrap_or_default()));
};

let mut reset = false;
let ret = if let Some((key, values)) = &self.current {
let value = values[self.next_idx];

if self.next_idx + 1 < values.len() {
self.next_idx += 1;
} else {
reset = true;
}

Some((key.as_str(), value))
} else {
None
};

if reset {
self.current = None;
self.next_idx = 0;
}

ret
}
}

Expand Down Expand Up @@ -158,4 +198,24 @@ mod tests {
values.sort();
assert_eq!(values, vec!["bar", "boom"]);
}

#[test]
fn test_empty_str_map_to_query_string() {
let data = HashMap::new();
let strmap = StrMap(data.into());
let query = strmap.to_query_string();
assert_eq!("", &query);
}

#[test]
fn test_str_map_to_query_string() {
let mut data = HashMap::new();
data.insert("foo".into(), vec!["bar".into(), "qux".into()]);
data.insert("baz".into(), vec!["quux".into()]);

let strmap = StrMap(data.into());
let query = strmap.to_query_string();
assert!(query.contains("foo=bar&foo=qux"));
assert!(query.contains("baz=quux"));
}
}