Skip to content

Fixed media type suffix detection #714

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

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Changes from 1 commit
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: 32 additions & 1 deletion lambda-http/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,20 @@ where
}

for suffix in TEXT_ENCODING_SUFFIXES {
if content_type.ends_with(suffix) {
let parts = content_type.trim().split(';');

let mut media_type = String::new();
for part in parts {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm pretty sure that the mime type is always the first value if you split the header value with ;. You might not need to iterate over all the parts, you can take the first element.

As far as I know the value's syntax is MIMETYPE; PARAMETERS

Copy link
Contributor

Choose a reason for hiding this comment

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

If that's not the only valid syntax, we should test other possible values.

Copy link
Contributor Author

@bnusunny bnusunny Oct 30, 2023

Choose a reason for hiding this comment

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

Right. I simplified the code to take the first element.

let trimmed_part = part.trim();

// Check if the part is a media type
if trimmed_part.contains('/') {
media_type = trimmed_part.to_string();
break;
}
}

if media_type.ends_with(suffix) {
return convert_to_text(self, content_type);
}
}
Expand Down Expand Up @@ -484,6 +497,24 @@ mod tests {
)
}

#[tokio::test]
async fn charset_content_type_header_suffix() {
// Drive the implementation by using `hyper::Body` instead of
// of `aws_lambda_events::encodings::Body`
let response = Response::builder()
.header(CONTENT_TYPE, "application/graphql-response+json; charset=utf-16")
.body(HyperBody::from("000000".as_bytes()))
.expect("unable to build http::Response");
let response = response.into_response().await;
let response = LambdaResponse::from_response(&RequestOrigin::ApiGatewayV2, response);

let json = serde_json::to_string(&response).expect("failed to serialize to json");
assert_eq!(
json,
r#"{"statusCode":200,"headers":{"content-type":"application/graphql-response+json; charset=utf-16"},"multiValueHeaders":{"content-type":["application/graphql-response+json; charset=utf-16"]},"body":"〰〰〰","isBase64Encoded":false,"cookies":[]}"#
)
}

#[tokio::test]
async fn content_headers_unset() {
// Drive the implementation by using `hyper::Body` instead of
Expand Down