From 187d9c08dcc3173b7114c7bdf33d233e2c6c12cd Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 4 Jul 2022 01:00:39 +0100 Subject: [PATCH] =?UTF-8?q?Allow=20=E2=80=98application/octet-stream?= =?UTF-8?q?=E2=80=99=20content-type=20when=20importing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/net/import.rs | 1 + src/net/output.rs | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/net/import.rs b/src/net/import.rs index 51627444..6bfe122b 100644 --- a/src/net/import.rs +++ b/src/net/import.rs @@ -38,6 +38,7 @@ async fn handler( "application/json" => Ok(output::json(&res)), "application/cbor" => Ok(output::cbor(&res)), "application/msgpack" => Ok(output::pack(&res)), + "application/octet-stream" => Ok(output::none()), _ => Err(warp::reject::not_found()), }, Err(err) => Err(warp::reject::custom(Error::from(err))), diff --git a/src/net/output.rs b/src/net/output.rs index 38778a84..e233efdf 100644 --- a/src/net/output.rs +++ b/src/net/output.rs @@ -4,18 +4,23 @@ use serde::Serialize; pub enum Output { None, + Fail, Json(Vec), // JSON Cbor(Vec), // CBOR Pack(Vec), // MessagePack } +pub fn none() -> Output { + Output::None +} + pub fn json(val: &T) -> Output where T: Serialize, { match serde_json::to_vec(val) { Ok(v) => Output::Json(v), - Err(_) => Output::None, + Err(_) => Output::Fail, } } @@ -25,7 +30,7 @@ where { match serde_cbor::to_vec(val) { Ok(v) => Output::Cbor(v), - Err(_) => Output::None, + Err(_) => Output::Fail, } } @@ -35,7 +40,7 @@ where { match serde_pack::to_vec(val) { Ok(v) => Output::Pack(v), - Err(_) => Output::None, + Err(_) => Output::Fail, } } @@ -60,7 +65,8 @@ impl warp::Reply for Output { res.headers_mut().insert(CONTENT_TYPE, con); res } - Output::None => StatusCode::INTERNAL_SERVER_ERROR.into_response(), + Output::None => StatusCode::OK.into_response(), + Output::Fail => StatusCode::INTERNAL_SERVER_ERROR.into_response(), } } }