1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

Fix json content type detection

This commit is contained in:
Nikolay Kim 2018-03-10 10:27:29 -08:00
parent ac9eba8261
commit 71b4c07ea4
2 changed files with 10 additions and 12 deletions

View File

@ -6,6 +6,8 @@
* Fix CORS middleware #117
* Fix json content type detection
* Optimize websockets stream support

View File

@ -2,6 +2,7 @@ use bytes::{Bytes, BytesMut};
use futures::{Poll, Future, Stream};
use http::header::CONTENT_LENGTH;
use mime;
use serde_json;
use serde::Serialize;
use serde::de::DeserializeOwned;
@ -82,7 +83,6 @@ impl<T: Serialize> Responder for Json<T> {
/// ```
pub struct JsonBody<T, U: DeserializeOwned>{
limit: usize,
ct: &'static str,
req: Option<T>,
fut: Option<Box<Future<Item=U, Error=JsonPayloadError>>>,
}
@ -95,7 +95,6 @@ impl<T, U: DeserializeOwned> JsonBody<T, U> {
limit: 262_144,
req: Some(req),
fut: None,
ct: "application/json",
}
}
@ -104,15 +103,6 @@ impl<T, U: DeserializeOwned> JsonBody<T, U> {
self.limit = limit;
self
}
/// Set allowed content type.
///
/// By default *application/json* content type is used. Set content type
/// to empty string if you want to disable content type check.
pub fn content_type(mut self, ct: &'static str) -> Self {
self.ct = ct;
self
}
}
impl<T, U: DeserializeOwned + 'static> Future for JsonBody<T, U>
@ -135,7 +125,13 @@ impl<T, U: DeserializeOwned + 'static> Future for JsonBody<T, U>
}
}
// check content-type
if !self.ct.is_empty() && req.content_type() != self.ct {
let json = if let Ok(Some(mime)) = req.mime_type() {
mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON)
} else {
false
};
if !json {
return Err(JsonPayloadError::ContentType)
}