diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 33f9d2017..c1db85509 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -2,6 +2,10 @@ ## Unreleased +## 3.11.3 + +- Update `HttpMessage`s `content_type` method to return `Option<&str>` instead of `&str`. + ## 3.11.2 - Properly wake Payload receivers when feeding errors or EOF. diff --git a/actix-http/src/http_message.rs b/actix-http/src/http_message.rs index 2800f40ba..8c38e3cff 100644 --- a/actix-http/src/http_message.rs +++ b/actix-http/src/http_message.rs @@ -46,13 +46,11 @@ pub trait HttpMessage: Sized { /// Read the request content type. If request did not contain a *Content-Type* header, an empty /// string is returned. - fn content_type(&self) -> &str { - if let Some(content_type) = self.headers().get(header::CONTENT_TYPE) { - if let Ok(content_type) = content_type.to_str() { - return content_type.split(';').next().unwrap().trim(); - } - } - "" + fn content_type(&self) -> Option<&str> { + self.headers() + .get(header::CONTENT_TYPE) + .and_then(|ct| ct.to_str().ok()) + .map(|ct| ct.split(';').next().unwrap().trim()) } /// Get content type encoding. @@ -142,13 +140,13 @@ mod tests { let req = TestRequest::default() .insert_header(("content-type", "text/plain")) .finish(); - assert_eq!(req.content_type(), "text/plain"); + assert_eq!(req.content_type(), Some("text/plain")); let req = TestRequest::default() .insert_header(("content-type", "application/json; charset=utf-8")) .finish(); - assert_eq!(req.content_type(), "application/json"); + assert_eq!(req.content_type(), Some("application/json")); let req = TestRequest::default().finish(); - assert_eq!(req.content_type(), ""); + assert_eq!(req.content_type(), None); } #[test] diff --git a/actix-web/src/test/test_utils.rs b/actix-web/src/test/test_utils.rs index 4540d8a6b..5c8c4e144 100644 --- a/actix-web/src/test/test_utils.rs +++ b/actix-web/src/test/test_utils.rs @@ -505,7 +505,10 @@ mod tests { .set_form(&payload) .to_request(); - assert_eq!(req.content_type(), "application/x-www-form-urlencoded"); + assert_eq!( + req.content_type().unwrap(), + "application/x-www-form-urlencoded" + ); let result: Person = call_and_read_body_json(&app, req).await; assert_eq!(&result.id, "12345"); @@ -549,7 +552,7 @@ mod tests { .set_json(&payload) .to_request(); - assert_eq!(req.content_type(), "application/json"); + assert_eq!(req.content_type(), Some("application/json")); let result: Person = call_and_read_body_json(&app, req).await; assert_eq!(&result.id, "12345"); diff --git a/actix-web/src/types/form.rs b/actix-web/src/types/form.rs index d6381b990..109d220e8 100644 --- a/actix-web/src/types/form.rs +++ b/actix-web/src/types/form.rs @@ -289,14 +289,17 @@ impl UrlEncoded { /// Create a new future to decode a URL encoded request payload. pub fn new(req: &HttpRequest, payload: &mut Payload) -> Self { // check content type - if req.content_type().to_lowercase() != "application/x-www-form-urlencoded" { + let encoding_error = req + .content_type() + .map(|ct| !ct.eq_ignore_ascii_case("application/x-www-form-urlencoded")) + .unwrap_or(true); + if encoding_error { return Self::err(UrlencodedError::ContentType); } let encoding = match req.encoding() { Ok(enc) => enc, Err(_) => return Self::err(UrlencodedError::ContentType), }; - let mut len = None; if let Some(l) = req.headers().get(&CONTENT_LENGTH) { if let Ok(s) = l.to_str() {