//! Json extractor/responder use std::rc::Rc; use std::{fmt, ops}; use bytes::{Bytes, BytesMut}; use futures::{Future, Poll, Stream}; use serde::de::DeserializeOwned; use serde::Serialize; use serde_json; use actix_http::http::{header::CONTENT_LENGTH, StatusCode}; use actix_http::{HttpMessage, Payload, Response}; use crate::error::{Error, JsonPayloadError, PayloadError}; use crate::extract::FromRequest; use crate::request::HttpRequest; use crate::responder::Responder; use crate::service::ServiceFromRequest; /// Json helper /// /// Json can be used for two different purpose. First is for json response /// generation and second is for extracting typed information from request's /// payload. /// /// To extract typed information from request's body, the type `T` must /// implement the `Deserialize` trait from *serde*. /// /// [**JsonConfig**](struct.JsonConfig.html) allows to configure extraction /// process. /// /// ## Example /// /// ```rust /// #[macro_use] extern crate serde_derive; /// use actix_web::{web, App}; /// /// #[derive(Deserialize)] /// struct Info { /// username: String, /// } /// /// /// deserialize `Info` from request's body /// fn index(info: web::Json) -> String { /// format!("Welcome {}!", info.username) /// } /// /// fn main() { /// let app = App::new().service( /// web::resource("/index.html").route( /// web::post().to(index)) /// ); /// } /// ``` /// /// The `Json` type allows you to respond with well-formed JSON data: simply /// return a value of type Json where T is the type of a structure /// to serialize into *JSON*. The type `T` must implement the `Serialize` /// trait from *serde*. /// /// ```rust /// # #[macro_use] extern crate serde_derive; /// # use actix_web::*; /// # /// #[derive(Serialize)] /// struct MyObj { /// name: String, /// } /// /// fn index(req: HttpRequest) -> Result> { /// Ok(web::Json(MyObj { /// name: req.match_info().get("name").unwrap().to_string(), /// })) /// } /// # fn main() {} /// ``` pub struct Json(pub T); impl Json { /// Deconstruct to an inner value pub fn into_inner(self) -> T { self.0 } } impl ops::Deref for Json { type Target = T; fn deref(&self) -> &T { &self.0 } } impl ops::DerefMut for Json { fn deref_mut(&mut self) -> &mut T { &mut self.0 } } impl fmt::Debug for Json where T: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Json: {:?}", self.0) } } impl fmt::Display for Json where T: fmt::Display, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) } } impl Responder for Json { type Error = Error; type Future = Result; fn respond_to(self, _: &HttpRequest) -> Self::Future { let body = match serde_json::to_string(&self.0) { Ok(body) => body, Err(e) => return Err(e.into()), }; Ok(Response::build(StatusCode::OK) .content_type("application/json") .body(body)) } } /// Json extractor. Allow to extract typed information from request's /// payload. /// /// To extract typed information from request's body, the type `T` must /// implement the `Deserialize` trait from *serde*. /// /// [**JsonConfig**](struct.JsonConfig.html) allows to configure extraction /// process. /// /// ## Example /// /// ```rust /// #[macro_use] extern crate serde_derive; /// use actix_web::{web, App}; /// /// #[derive(Deserialize)] /// struct Info { /// username: String, /// } /// /// /// deserialize `Info` from request's body /// fn index(info: web::Json) -> String { /// format!("Welcome {}!", info.username) /// } /// /// fn main() { /// let app = App::new().service( /// web::resource("/index.html").route( /// web::post().to(index)) /// ); /// } /// ``` impl FromRequest

for Json where T: DeserializeOwned + 'static, P: Stream + 'static, { type Error = Error; type Future = Box>; #[inline] fn from_request(req: &mut ServiceFromRequest

) -> Self::Future { let req2 = req.clone(); let (limit, err) = req .route_data::() .map(|c| (c.limit, c.ehandler.clone())) .unwrap_or((32768, None)); Box::new( JsonBody::new(req) .limit(limit) .map_err(move |e| { if let Some(err) = err { (*err)(e, &req2) } else { e.into() } }) .map(Json), ) } } /// Json extractor configuration /// /// ```rust /// #[macro_use] extern crate serde_derive; /// use actix_web::{error, web, App, HttpResponse}; /// /// #[derive(Deserialize)] /// struct Info { /// username: String, /// } /// /// /// deserialize `Info` from request's body, max payload size is 4kb /// fn index(info: web::Json) -> String { /// format!("Welcome {}!", info.username) /// } /// /// fn main() { /// let app = App::new().service( /// web::resource("/index.html").route( /// web::post().data( /// // change json extractor configuration /// web::JsonConfig::default().limit(4096) /// .error_handler(|err, req| { // <- create custom error response /// error::InternalError::from_response( /// err, HttpResponse::Conflict().finish()).into() /// })) /// .to(index)) /// ); /// } /// ``` #[derive(Clone)] pub struct JsonConfig { limit: usize, ehandler: Option Error>>, } impl JsonConfig { /// Change max size of payload. By default max size is 32Kb pub fn limit(mut self, limit: usize) -> Self { self.limit = limit; self } /// Set custom error handler pub fn error_handler(mut self, f: F) -> Self where F: Fn(JsonPayloadError, &HttpRequest) -> Error + 'static, { self.ehandler = Some(Rc::new(f)); self } } impl Default for JsonConfig { fn default() -> Self { JsonConfig { limit: 32768, ehandler: None, } } } /// Request's payload json parser, it resolves to a deserialized `T` value. /// This future could be used with `ServiceRequest` and `ServiceFromRequest`. /// /// Returns error: /// /// * content type is not `application/json` /// * content length is greater than 256k pub struct JsonBody { limit: usize, length: Option, stream: Payload, err: Option, fut: Option>>, } impl JsonBody where T: HttpMessage, T::Stream: Stream + 'static, U: DeserializeOwned + 'static, { /// Create `JsonBody` for request. pub fn new(req: &mut T) -> Self { // check content-type let json = if let Ok(Some(mime)) = req.mime_type() { mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON) } else { false }; if !json { return JsonBody { limit: 262_144, length: None, stream: Payload::None, fut: None, err: Some(JsonPayloadError::ContentType), }; } let mut len = None; if let Some(l) = req.headers().get(CONTENT_LENGTH) { if let Ok(s) = l.to_str() { if let Ok(l) = s.parse::() { len = Some(l) } } } JsonBody { limit: 262_144, length: len, stream: req.take_payload(), fut: None, err: None, } } /// Change max size of payload. By default max size is 256Kb pub fn limit(mut self, limit: usize) -> Self { self.limit = limit; self } } impl Future for JsonBody where T: HttpMessage, T::Stream: Stream + 'static, U: DeserializeOwned + 'static, { type Item = U; type Error = JsonPayloadError; fn poll(&mut self) -> Poll { if let Some(ref mut fut) = self.fut { return fut.poll(); } if let Some(err) = self.err.take() { return Err(err); } let limit = self.limit; if let Some(len) = self.length.take() { if len > limit { return Err(JsonPayloadError::Overflow); } } let fut = std::mem::replace(&mut self.stream, Payload::None) .from_err() .fold(BytesMut::with_capacity(8192), move |mut body, chunk| { if (body.len() + chunk.len()) > limit { Err(JsonPayloadError::Overflow) } else { body.extend_from_slice(&chunk); Ok(body) } }) .and_then(|body| Ok(serde_json::from_slice::(&body)?)); self.fut = Some(Box::new(fut)); self.poll() } } #[cfg(test)] mod tests { use bytes::Bytes; use serde_derive::{Deserialize, Serialize}; use super::*; use crate::http::header; use crate::test::{block_on, TestRequest}; fn json_eq(err: JsonPayloadError, other: JsonPayloadError) -> bool { match err { JsonPayloadError::Overflow => match other { JsonPayloadError::Overflow => true, _ => false, }, JsonPayloadError::ContentType => match other { JsonPayloadError::ContentType => true, _ => false, }, _ => false, } } #[derive(Serialize, Deserialize, PartialEq, Debug)] struct MyObject { name: String, } #[test] fn test_json_body() { let mut req = TestRequest::default().to_request(); let json = block_on(JsonBody::<_, MyObject>::new(&mut req)); assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType)); let mut req = TestRequest::default() .header( header::CONTENT_TYPE, header::HeaderValue::from_static("application/text"), ) .to_request(); let json = block_on(JsonBody::<_, MyObject>::new(&mut req)); assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType)); let mut req = TestRequest::default() .header( header::CONTENT_TYPE, header::HeaderValue::from_static("application/json"), ) .header( header::CONTENT_LENGTH, header::HeaderValue::from_static("10000"), ) .to_request(); let json = block_on(JsonBody::<_, MyObject>::new(&mut req).limit(100)); assert!(json_eq(json.err().unwrap(), JsonPayloadError::Overflow)); let mut req = TestRequest::default() .header( header::CONTENT_TYPE, header::HeaderValue::from_static("application/json"), ) .header( header::CONTENT_LENGTH, header::HeaderValue::from_static("16"), ) .set_payload(Bytes::from_static(b"{\"name\": \"test\"}")) .to_request(); let json = block_on(JsonBody::<_, MyObject>::new(&mut req)); assert_eq!( json.ok().unwrap(), MyObject { name: "test".to_owned() } ); } }