diff options
author | Alex Auvolat <alex@adnab.me> | 2022-05-25 17:05:56 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2022-05-25 17:09:33 +0200 |
commit | ff06d3f0829464863e64ed55471f2caa13bed191 (patch) | |
tree | 2ba08f439721c28d326badc0a33f148ee1fbdce1 | |
parent | 93eab8eaa3927b99626fee4a747a0f9f041cafdb (diff) | |
download | garage-ff06d3f0829464863e64ed55471f2caa13bed191.tar.gz garage-ff06d3f0829464863e64ed55471f2caa13bed191.zip |
Fix Content-Type headers for {admin,k2v} errors and admin responsesfix/admin-api-content-type
Fix #315
-rw-r--r-- | src/api/admin/bucket.rs | 13 | ||||
-rw-r--r-- | src/api/admin/cluster.rs | 19 | ||||
-rw-r--r-- | src/api/admin/error.rs | 5 | ||||
-rw-r--r-- | src/api/admin/key.rs | 14 | ||||
-rw-r--r-- | src/api/generic_server.rs | 4 | ||||
-rw-r--r-- | src/api/helpers.rs | 10 | ||||
-rw-r--r-- | src/api/k2v/error.rs | 5 | ||||
-rw-r--r-- | src/api/s3/error.rs | 3 |
8 files changed, 30 insertions, 43 deletions
diff --git a/src/api/admin/bucket.rs b/src/api/admin/bucket.rs index 849d28ac..7f9a813f 100644 --- a/src/api/admin/bucket.rs +++ b/src/api/admin/bucket.rs @@ -6,7 +6,6 @@ use serde::{Deserialize, Serialize}; use garage_util::crdt::*; use garage_util::data::*; -use garage_util::error::Error as GarageError; use garage_util::time::*; use garage_table::*; @@ -19,7 +18,7 @@ use garage_model::permission::*; use crate::admin::error::*; use crate::admin::key::ApiBucketKeyPerm; use crate::common_error::CommonError; -use crate::helpers::parse_json_body; +use crate::helpers::{json_ok_response, parse_json_body}; pub async fn handle_list_buckets(garage: &Arc<Garage>) -> Result<Response<Body>, Error> { let buckets = garage @@ -60,10 +59,7 @@ pub async fn handle_list_buckets(garage: &Arc<Garage>) -> Result<Response<Body>, }) .collect::<Vec<_>>(); - let resp_json = serde_json::to_string_pretty(&res).map_err(GarageError::from)?; - Ok(Response::builder() - .status(StatusCode::OK) - .body(Body::from(resp_json))?) + Ok(json_ok_response(&res)?) } #[derive(Serialize)] @@ -197,10 +193,7 @@ async fn bucket_info_results( .collect::<Vec<_>>(), }; - let resp_json = serde_json::to_string_pretty(&res).map_err(GarageError::from)?; - Ok(Response::builder() - .status(StatusCode::OK) - .body(Body::from(resp_json))?) + Ok(json_ok_response(&res)?) } #[derive(Serialize)] diff --git a/src/api/admin/cluster.rs b/src/api/admin/cluster.rs index 3401be42..6d01317d 100644 --- a/src/api/admin/cluster.rs +++ b/src/api/admin/cluster.rs @@ -7,14 +7,13 @@ use serde::{Deserialize, Serialize}; use garage_util::crdt::*; use garage_util::data::*; -use garage_util::error::Error as GarageError; use garage_rpc::layout::*; use garage_model::garage::Garage; use crate::admin::error::*; -use crate::helpers::parse_json_body; +use crate::helpers::{json_ok_response, parse_json_body}; pub async fn handle_get_cluster_status(garage: &Arc<Garage>) -> Result<Response<Body>, Error> { let res = GetClusterStatusResponse { @@ -39,10 +38,7 @@ pub async fn handle_get_cluster_status(garage: &Arc<Garage>) -> Result<Response< layout: get_cluster_layout(garage), }; - let resp_json = serde_json::to_string_pretty(&res).map_err(GarageError::from)?; - Ok(Response::builder() - .status(StatusCode::OK) - .body(Body::from(resp_json))?) + Ok(json_ok_response(&res)?) } pub async fn handle_connect_cluster_nodes( @@ -66,18 +62,13 @@ pub async fn handle_connect_cluster_nodes( }) .collect::<Vec<_>>(); - let resp_json = serde_json::to_string_pretty(&res).map_err(GarageError::from)?; - Ok(Response::builder() - .status(StatusCode::OK) - .body(Body::from(resp_json))?) + Ok(json_ok_response(&res)?) } pub async fn handle_get_cluster_layout(garage: &Arc<Garage>) -> Result<Response<Body>, Error> { let res = get_cluster_layout(garage); - let resp_json = serde_json::to_string_pretty(&res).map_err(GarageError::from)?; - Ok(Response::builder() - .status(StatusCode::OK) - .body(Body::from(resp_json))?) + + Ok(json_ok_response(&res)?) } fn get_cluster_layout(garage: &Arc<Garage>) -> GetClusterLayoutResponse { diff --git a/src/api/admin/error.rs b/src/api/admin/error.rs index c4613cb3..ed1a07bd 100644 --- a/src/api/admin/error.rs +++ b/src/api/admin/error.rs @@ -72,8 +72,9 @@ impl ApiError for Error { } } - fn add_http_headers(&self, _header_map: &mut HeaderMap<HeaderValue>) { - // nothing + fn add_http_headers(&self, header_map: &mut HeaderMap<HeaderValue>) { + use hyper::header; + header_map.append(header::CONTENT_TYPE, "application/json".parse().unwrap()); } fn http_body(&self, garage_region: &str, path: &str) -> Body { diff --git a/src/api/admin/key.rs b/src/api/admin/key.rs index f30b5dbb..2bbabb7b 100644 --- a/src/api/admin/key.rs +++ b/src/api/admin/key.rs @@ -4,15 +4,13 @@ use std::sync::Arc; use hyper::{Body, Request, Response, StatusCode}; use serde::{Deserialize, Serialize}; -use garage_util::error::Error as GarageError; - use garage_table::*; use garage_model::garage::Garage; use garage_model::key_table::*; use crate::admin::error::*; -use crate::helpers::parse_json_body; +use crate::helpers::{json_ok_response, parse_json_body}; pub async fn handle_list_keys(garage: &Arc<Garage>) -> Result<Response<Body>, Error> { let res = garage @@ -32,10 +30,7 @@ pub async fn handle_list_keys(garage: &Arc<Garage>) -> Result<Response<Body>, Er }) .collect::<Vec<_>>(); - let resp_json = serde_json::to_string_pretty(&res).map_err(GarageError::from)?; - Ok(Response::builder() - .status(StatusCode::OK) - .body(Body::from(resp_json))?) + Ok(json_ok_response(&res)?) } #[derive(Serialize)] @@ -221,10 +216,7 @@ async fn key_info_results(garage: &Arc<Garage>, key: Key) -> Result<Response<Bod .collect::<Vec<_>>(), }; - let resp_json = serde_json::to_string_pretty(&res).map_err(GarageError::from)?; - Ok(Response::builder() - .status(StatusCode::OK) - .body(Body::from(resp_json))?) + Ok(json_ok_response(&res)?) } #[derive(Serialize)] diff --git a/src/api/generic_server.rs b/src/api/generic_server.rs index 77278908..a48be1bc 100644 --- a/src/api/generic_server.rs +++ b/src/api/generic_server.rs @@ -150,9 +150,7 @@ impl<A: ApiHandler> ApiServer<A> { } Err(e) => { let body: Body = e.http_body(&self.region, uri.path()); - let mut http_error_builder = Response::builder() - .status(e.http_status_code()) - .header("Content-Type", "application/xml"); + let mut http_error_builder = Response::builder().status(e.http_status_code()); if let Some(header_map) = http_error_builder.headers_mut() { e.add_http_headers(header_map) diff --git a/src/api/helpers.rs b/src/api/helpers.rs index 9fb12dbe..642dbc42 100644 --- a/src/api/helpers.rs +++ b/src/api/helpers.rs @@ -1,4 +1,4 @@ -use hyper::{Body, Request}; +use hyper::{Body, Request, Response}; use idna::domain_to_unicode; use serde::{Deserialize, Serialize}; @@ -144,6 +144,14 @@ pub async fn parse_json_body<T: for<'de> Deserialize<'de>>(req: Request<Body>) - Ok(resp) } +pub fn json_ok_response<T: Serialize>(res: &T) -> Result<Response<Body>, Error> { + let resp_json = serde_json::to_string_pretty(res).map_err(garage_util::error::Error::from)?; + Ok(Response::builder() + .status(hyper::StatusCode::OK) + .header(http::header::CONTENT_TYPE, "application/json") + .body(Body::from(resp_json))?) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/api/k2v/error.rs b/src/api/k2v/error.rs index 4c55d8b5..42491466 100644 --- a/src/api/k2v/error.rs +++ b/src/api/k2v/error.rs @@ -110,8 +110,9 @@ impl ApiError for Error { } } - fn add_http_headers(&self, _header_map: &mut HeaderMap<HeaderValue>) { - // nothing + fn add_http_headers(&self, header_map: &mut HeaderMap<HeaderValue>) { + use hyper::header; + header_map.append(header::CONTENT_TYPE, "application/json".parse().unwrap()); } fn http_body(&self, garage_region: &str, path: &str) -> Body { diff --git a/src/api/s3/error.rs b/src/api/s3/error.rs index ac632540..67009d63 100644 --- a/src/api/s3/error.rs +++ b/src/api/s3/error.rs @@ -172,6 +172,9 @@ impl ApiError for Error { fn add_http_headers(&self, header_map: &mut HeaderMap<HeaderValue>) { use hyper::header; + + header_map.append(header::CONTENT_TYPE, "application/xml".parse().unwrap()); + #[allow(clippy::single_match)] match self { Error::InvalidRange((_, len)) => { |