Skip to content

Commit

Permalink
optimize accept content type matching
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed Nov 9, 2024
1 parent eb77101 commit 10ac36c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,17 @@ async fn server(
let accept = headers.get("accept").map(|s| s.to_str()).transpose()?;

Ok(match accept {
Some("application/json") => Json(resp).into_response(),
Some("text/html" | "*/*") | None => Server::from(resp).into_response(),
Some(v)
if v.split(',')
.map(str::trim)
.any(|v| v == "text/html" || v == "*/*") =>
{
Server::from(resp).into_response()
}
Some(v) if v.split(',').map(str::trim).any(|v| v == "application/json") => {
Json(resp).into_response()
}
None => Server::from(resp).into_response(),
_ => ErrorResponse::new(
StatusCode::BAD_REQUEST,
"unsupported requested content type",
Expand Down

0 comments on commit 10ac36c

Please sign in to comment.