mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-25 00:12:59 +01:00
520 lines
14 KiB
Rust
520 lines
14 KiB
Rust
//! 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<Info>) -> 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<T> 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<web::Json<MyObj>> {
|
|
/// Ok(web::Json(MyObj {
|
|
/// name: req.match_info().get("name").unwrap().to_string(),
|
|
/// }))
|
|
/// }
|
|
/// # fn main() {}
|
|
/// ```
|
|
pub struct Json<T>(pub T);
|
|
|
|
impl<T> Json<T> {
|
|
/// Deconstruct to an inner value
|
|
pub fn into_inner(self) -> T {
|
|
self.0
|
|
}
|
|
}
|
|
|
|
impl<T> ops::Deref for Json<T> {
|
|
type Target = T;
|
|
|
|
fn deref(&self) -> &T {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl<T> ops::DerefMut for Json<T> {
|
|
fn deref_mut(&mut self) -> &mut T {
|
|
&mut self.0
|
|
}
|
|
}
|
|
|
|
impl<T> fmt::Debug for Json<T>
|
|
where
|
|
T: fmt::Debug,
|
|
{
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "Json: {:?}", self.0)
|
|
}
|
|
}
|
|
|
|
impl<T> fmt::Display for Json<T>
|
|
where
|
|
T: fmt::Display,
|
|
{
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
fmt::Display::fmt(&self.0, f)
|
|
}
|
|
}
|
|
|
|
impl<T: Serialize> Responder for Json<T> {
|
|
type Error = Error;
|
|
type Future = Result<Response, Error>;
|
|
|
|
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<Info>) -> String {
|
|
/// format!("Welcome {}!", info.username)
|
|
/// }
|
|
///
|
|
/// fn main() {
|
|
/// let app = App::new().service(
|
|
/// web::resource("/index.html").route(
|
|
/// web::post().to(index))
|
|
/// );
|
|
/// }
|
|
/// ```
|
|
impl<T, P> FromRequest<P> for Json<T>
|
|
where
|
|
T: DeserializeOwned + 'static,
|
|
P: Stream<Item = Bytes, Error = crate::error::PayloadError> + 'static,
|
|
{
|
|
type Error = Error;
|
|
type Future = Box<Future<Item = Self, Error = Error>>;
|
|
|
|
#[inline]
|
|
fn from_request(req: &mut ServiceFromRequest<P>) -> Self::Future {
|
|
let req2 = req.clone();
|
|
let (limit, err) = req
|
|
.route_data::<JsonConfig>()
|
|
.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<Info>) -> 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<Rc<Fn(JsonPayloadError, &HttpRequest) -> 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<F>(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<T: HttpMessage, U> {
|
|
limit: usize,
|
|
length: Option<usize>,
|
|
stream: Payload<T::Stream>,
|
|
err: Option<JsonPayloadError>,
|
|
fut: Option<Box<Future<Item = U, Error = JsonPayloadError>>>,
|
|
}
|
|
|
|
impl<T, U> JsonBody<T, U>
|
|
where
|
|
T: HttpMessage,
|
|
T::Stream: Stream<Item = Bytes, Error = PayloadError> + '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::<usize>() {
|
|
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<T, U> Future for JsonBody<T, U>
|
|
where
|
|
T: HttpMessage,
|
|
T::Stream: Stream<Item = Bytes, Error = PayloadError> + 'static,
|
|
U: DeserializeOwned + 'static,
|
|
{
|
|
type Item = U;
|
|
type Error = JsonPayloadError;
|
|
|
|
fn poll(&mut self) -> Poll<U, JsonPayloadError> {
|
|
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::<U>(&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};
|
|
|
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
|
struct MyObject {
|
|
name: String,
|
|
}
|
|
|
|
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,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_responder() {
|
|
let req = TestRequest::default().to_http_request();
|
|
|
|
let j = Json(MyObject {
|
|
name: "test".to_string(),
|
|
});
|
|
let resp = j.respond_to(&req).unwrap();
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
assert_eq!(
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
header::HeaderValue::from_static("application/json")
|
|
);
|
|
|
|
use crate::responder::tests::BodyTest;
|
|
assert_eq!(resp.body().bin_ref(), b"{\"name\":\"test\"}");
|
|
}
|
|
|
|
#[test]
|
|
fn test_extract() {
|
|
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_from();
|
|
|
|
let s = block_on(Json::<MyObject>::from_request(&mut req)).unwrap();
|
|
assert_eq!(s.name, "test");
|
|
assert_eq!(
|
|
s.into_inner(),
|
|
MyObject {
|
|
name: "test".to_string()
|
|
}
|
|
);
|
|
|
|
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\"}"))
|
|
.route_data(JsonConfig::default().limit(10))
|
|
.to_from();
|
|
let s = block_on(Json::<MyObject>::from_request(&mut req));
|
|
assert!(format!("{}", s.err().unwrap())
|
|
.contains("Json payload size is bigger than allowed."));
|
|
|
|
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\"}"))
|
|
.route_data(
|
|
JsonConfig::default()
|
|
.limit(10)
|
|
.error_handler(|_, _| JsonPayloadError::ContentType.into()),
|
|
)
|
|
.to_from();
|
|
let s = block_on(Json::<MyObject>::from_request(&mut req));
|
|
assert!(format!("{}", s.err().unwrap()).contains("Content type error"));
|
|
}
|
|
|
|
#[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()
|
|
}
|
|
);
|
|
}
|
|
}
|