1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-04-20 06:54:46 +00:00

response can have a different mime type than json

This commit is contained in:
Dominic 2019-10-19 20:23:25 +02:00
parent bb5f58e97d
commit 656595711c
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
2 changed files with 90 additions and 39 deletions

View file

@ -1,6 +1,6 @@
use crate::{
resource::*,
result::{ResourceError, ResourceResult},
result::{ResourceError, ResourceResult, Response},
ResourceType,
StatusCode
};
@ -14,7 +14,7 @@ use futures::{
use gotham::{
extractor::QueryStringExtractor,
handler::{HandlerFuture, IntoHandlerError},
helpers::http::response::create_response,
helpers::http::response::{create_empty_response, create_response},
pipeline::chain::PipelineHandleChain,
router::{
builder::*,
@ -23,7 +23,11 @@ use gotham::{
},
state::{FromState, State}
};
use hyper::Body;
use hyper::{
header::CONTENT_TYPE,
Body,
Method
};
use mime::{Mime, APPLICATION_JSON};
use serde::de::DeserializeOwned;
use std::panic::RefUnwindSafe;
@ -108,6 +112,20 @@ pub trait DrawResourceRoutes
Handler : ResourceDelete<ID, Res>;
}
fn response_from(res : Response, state : &State) -> hyper::Response<Body>
{
let mut r = create_empty_response(state, res.status);
if let Some(mime) = res.mime
{
r.headers_mut().insert(CONTENT_TYPE, mime.as_ref().parse().unwrap());
}
if Method::borrow_from(state) != Method::HEAD
{
*r.body_mut() = res.body.into();
}
r
}
fn to_handler_future<F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
where
F : FnOnce(&mut State) -> R,
@ -115,9 +133,9 @@ where
{
let res = get_result(&mut state).to_response();
match res {
Ok((status, body)) => {
let res = create_response(&state, status, APPLICATION_JSON, body);
Box::new(ok((state, res)))
Ok(res) => {
let r = response_from(res, &state);
Box::new(ok((state, r)))
},
Err(e) => Box::new(err((state, e.into_handler_error())))
}
@ -154,9 +172,9 @@ where
let res = get_result(&mut state, body).to_response();
match res {
Ok((status, body)) => {
let res = create_response(&state, status, APPLICATION_JSON, body);
ok((state, res))
Ok(res) => {
let r = response_from(res, &state);
ok((state, r))
},
Err(e) => err((state, e.into_handler_error()))
}