mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 16:02:59 +01:00
Fix json content type detection
This commit is contained in:
parent
ac9eba8261
commit
71b4c07ea4
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
* Fix CORS middleware #117
|
* Fix CORS middleware #117
|
||||||
|
|
||||||
|
* Fix json content type detection
|
||||||
|
|
||||||
* Optimize websockets stream support
|
* Optimize websockets stream support
|
||||||
|
|
||||||
|
|
||||||
|
20
src/json.rs
20
src/json.rs
@ -2,6 +2,7 @@ use bytes::{Bytes, BytesMut};
|
|||||||
use futures::{Poll, Future, Stream};
|
use futures::{Poll, Future, Stream};
|
||||||
use http::header::CONTENT_LENGTH;
|
use http::header::CONTENT_LENGTH;
|
||||||
|
|
||||||
|
use mime;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
@ -82,7 +83,6 @@ impl<T: Serialize> Responder for Json<T> {
|
|||||||
/// ```
|
/// ```
|
||||||
pub struct JsonBody<T, U: DeserializeOwned>{
|
pub struct JsonBody<T, U: DeserializeOwned>{
|
||||||
limit: usize,
|
limit: usize,
|
||||||
ct: &'static str,
|
|
||||||
req: Option<T>,
|
req: Option<T>,
|
||||||
fut: Option<Box<Future<Item=U, Error=JsonPayloadError>>>,
|
fut: Option<Box<Future<Item=U, Error=JsonPayloadError>>>,
|
||||||
}
|
}
|
||||||
@ -95,7 +95,6 @@ impl<T, U: DeserializeOwned> JsonBody<T, U> {
|
|||||||
limit: 262_144,
|
limit: 262_144,
|
||||||
req: Some(req),
|
req: Some(req),
|
||||||
fut: None,
|
fut: None,
|
||||||
ct: "application/json",
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,15 +103,6 @@ impl<T, U: DeserializeOwned> JsonBody<T, U> {
|
|||||||
self.limit = limit;
|
self.limit = limit;
|
||||||
self
|
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>
|
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
|
// 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)
|
return Err(JsonPayloadError::ContentType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user