From 71b4c07ea47b60dc4871fc0e16078b6452ace4cb Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 10 Mar 2018 10:27:29 -0800 Subject: [PATCH] Fix json content type detection --- CHANGES.md | 2 ++ src/json.rs | 20 ++++++++------------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 8a9d029f..6bfb3b77 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,8 @@ * Fix CORS middleware #117 +* Fix json content type detection + * Optimize websockets stream support diff --git a/src/json.rs b/src/json.rs index a41125b4..e655c5f4 100644 --- a/src/json.rs +++ b/src/json.rs @@ -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 Responder for Json { /// ``` pub struct JsonBody{ limit: usize, - ct: &'static str, req: Option, fut: Option>>, } @@ -95,7 +95,6 @@ impl JsonBody { limit: 262_144, req: Some(req), fut: None, - ct: "application/json", } } @@ -104,15 +103,6 @@ impl JsonBody { 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 Future for JsonBody @@ -135,7 +125,13 @@ impl Future for JsonBody } } // 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) }