Inconsistent behaviour when responding to requests w/ invalid Content-Type
#2886
-
Hi there, The following code shows two requests where the use rocket::{http::Status, post, routes};
use rocket_multipart::MultipartReader;
#[post("/", data = "<data>", format = "multipart/mixed")]
fn post_mixed(data: MultipartReader<'_>) -> Result<String, Status> {
todo!()
}
#[rocket::launch]
fn rocket() -> _ {
rocket::build().mount("/statements", routes![post_mixed])
}
#[cfg(test)]
mod tests {
use super::*;
use rocket::{http::Header, local::blocking::Client};
#[test]
fn test_invalid_ct() {
let client = Client::tracked(rocket()).expect("valid rocket instance");
let req = client
.post("/statements")
.header(Header::new("Content-Type", "multipart/mixed"));
let resp = req.dispatch();
assert_eq!(resp.status(), Status::BadRequest);
let req = client
.post("/statements")
.header(Header::new("Content-Type", "multipart/mixed;"));
let resp = req.dispatch();
assert_eq!(resp.status(), Status::NotFound);
}
} The 2 requests use invalid syntax for the CT header. The first is missing the mandatory is there a way of configuring Rocket to treat the two cases similarly and return a TIA + cheers; |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
There are two options. You could add a secondary route (or a blanket, low ranked route) that catches invalid content-types, and returns a 400. The other option, and the one I think makes more sense long term, is to add support for this in Rocket. Rocket parses a number of headers by default, and I think a parse error in any of them should cause the default error type to be a 400, rather than the current default of 404. If you're interested, we would accept a PR that implements this behavior. |
Beta Was this translation helpful? Give feedback.
-
hi @the10thWiz , thanks for taking the time to respond! adding a "last chance" handler w/ the appropriate totally agree w/ you that making Rocket consistent in its treatment of invalid requests so it returns unfortunately however i do not have the time to submit a PR for this any time soon. i'll close this question w/ this comment and leave you the option to raise this issue as a bug. thanks again for your time + effort. |
Beta Was this translation helpful? Give feedback.
There are two options. You could add a secondary route (or a blanket, low ranked route) that catches invalid content-types, and returns a 400.
The other option, and the one I think makes more sense long term, is to add support for this in Rocket. Rocket parses a number of headers by default, and I think a parse error in any of them should cause the default error type to be a 400, rather than the current default of 404. If you're interested, we would accept a PR that implements this behavior.