From 6e69a1fffc715c752a399750c1e26aa46683dbb2 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 5 Feb 2024 14:44:12 +0100 Subject: [dep-upgrade-202402] prepare migration to http/hyper 1.0 --- src/api/helpers.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/api/helpers.rs') diff --git a/src/api/helpers.rs b/src/api/helpers.rs index 1d55ebd5..8efaa231 100644 --- a/src/api/helpers.rs +++ b/src/api/helpers.rs @@ -1,4 +1,4 @@ -use hyper::{Body, Request, Response}; +use hyper::{body::HttpBody, Body, Request, Response}; use idna::domain_to_unicode; use serde::{Deserialize, Serialize}; @@ -139,7 +139,7 @@ pub fn key_after_prefix(pfx: &str) -> Option { } pub async fn parse_json_body Deserialize<'de>>(req: Request) -> Result { - let body = hyper::body::to_bytes(req.into_body()).await?; + let body = req.into_body().collect().await?.to_bytes(); let resp: T = serde_json::from_slice(&body).ok_or_bad_request("Invalid JSON")?; Ok(resp) } -- cgit v1.2.3 From 0bb5b77530ad432e4c77f13b395fe74613812337 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 5 Feb 2024 18:49:54 +0100 Subject: [dep-upgrade-202402] wip: port to http/hyper crates v1 --- src/api/helpers.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'src/api/helpers.rs') diff --git a/src/api/helpers.rs b/src/api/helpers.rs index 8efaa231..541b2def 100644 --- a/src/api/helpers.rs +++ b/src/api/helpers.rs @@ -1,4 +1,5 @@ -use hyper::{body::HttpBody, Body, Request, Response}; +use http_body_util::{BodyExt, Full as FullBody}; +use hyper::{body::Incoming as IncomingBody, Request, Response}; use idna::domain_to_unicode; use serde::{Deserialize, Serialize}; @@ -138,18 +139,36 @@ pub fn key_after_prefix(pfx: &str) -> Option { None } -pub async fn parse_json_body Deserialize<'de>>(req: Request) -> Result { +// =============== body helpers ================= + +pub type BytesBody = FullBody; +pub type BoxBody = http_body_util::combinators::BoxBody; + +pub fn string_body(s: String) -> BoxBody { + bytes_body(bytes::Bytes::from(s.into_bytes())) +} +pub fn bytes_body(b: bytes::Bytes) -> BoxBody { + BoxBody::new(FullBody::new(b).map_err(|_| unreachable!())) +} +pub fn empty_body() -> BoxBody { + BoxBody::new(http_body_util::Empty::new().map_err(|_| unreachable!())) +} + +pub async fn parse_json_body(req: Request) -> Result +where + T: for<'de> Deserialize<'de>, +{ let body = req.into_body().collect().await?.to_bytes(); let resp: T = serde_json::from_slice(&body).ok_or_bad_request("Invalid JSON")?; Ok(resp) } -pub fn json_ok_response(res: &T) -> Result, Error> { +pub fn json_ok_response(res: &T) -> Result>, 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))?) + .body(string_body(resp_json))?) } pub fn is_default(v: &T) -> bool { -- cgit v1.2.3 From a22bd319202f05bce4ad13072238c7ba81d518fb Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 5 Feb 2024 19:27:12 +0100 Subject: [dep-upgrade-202402] migration to http/hyper 1.0 for k2v api --- src/api/helpers.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'src/api/helpers.rs') diff --git a/src/api/helpers.rs b/src/api/helpers.rs index 541b2def..57aa1ea1 100644 --- a/src/api/helpers.rs +++ b/src/api/helpers.rs @@ -1,8 +1,10 @@ use http_body_util::{BodyExt, Full as FullBody}; -use hyper::{body::Incoming as IncomingBody, Request, Response}; +use hyper::{body::Body, Request, Response}; use idna::domain_to_unicode; use serde::{Deserialize, Serialize}; +use garage_util::error::Error as GarageError; + use crate::common_error::{CommonError as Error, *}; /// What kind of authorization is required to perform a given action @@ -141,6 +143,7 @@ pub fn key_after_prefix(pfx: &str) -> Option { // =============== body helpers ================= +pub type EmptyBody = http_body_util::Empty; pub type BytesBody = FullBody; pub type BoxBody = http_body_util::combinators::BoxBody; @@ -153,22 +156,33 @@ pub fn bytes_body(b: bytes::Bytes) -> BoxBody { pub fn empty_body() -> BoxBody { BoxBody::new(http_body_util::Empty::new().map_err(|_| unreachable!())) } +pub fn string_bytes_body(s: String) -> BytesBody { + BytesBody::from(bytes::Bytes::from(s.into_bytes())) +} -pub async fn parse_json_body(req: Request) -> Result +pub async fn parse_json_body(req: Request) -> Result where T: for<'de> Deserialize<'de>, + B: Body, + E: From<::Error> + From, { let body = req.into_body().collect().await?.to_bytes(); let resp: T = serde_json::from_slice(&body).ok_or_bad_request("Invalid JSON")?; Ok(resp) } -pub fn json_ok_response(res: &T) -> Result>, Error> { - let resp_json = serde_json::to_string_pretty(res).map_err(garage_util::error::Error::from)?; +pub fn json_ok_response(res: &T) -> Result>, E> +where + E: From, +{ + let resp_json = serde_json::to_string_pretty(res) + .map_err(GarageError::from) + .map_err(Error::from)?; Ok(Response::builder() .status(hyper::StatusCode::OK) .header(http::header::CONTENT_TYPE, "application/json") - .body(string_body(resp_json))?) + .body(string_body(resp_json)) + .unwrap()) } pub fn is_default(v: &T) -> bool { -- cgit v1.2.3 From e524e7a30d4d81c84f1c110017ad972dc5617bf6 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 7 Feb 2024 14:45:52 +0100 Subject: [dep-upgrade-202402] rename BytesBody into ErrorBody for clarity --- src/api/helpers.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/api/helpers.rs') diff --git a/src/api/helpers.rs b/src/api/helpers.rs index 57aa1ea1..66f4c465 100644 --- a/src/api/helpers.rs +++ b/src/api/helpers.rs @@ -144,7 +144,7 @@ pub fn key_after_prefix(pfx: &str) -> Option { // =============== body helpers ================= pub type EmptyBody = http_body_util::Empty; -pub type BytesBody = FullBody; +pub type ErrorBody = FullBody; pub type BoxBody = http_body_util::combinators::BoxBody; pub fn string_body(s: String) -> BoxBody { @@ -156,8 +156,8 @@ pub fn bytes_body(b: bytes::Bytes) -> BoxBody { pub fn empty_body() -> BoxBody { BoxBody::new(http_body_util::Empty::new().map_err(|_| unreachable!())) } -pub fn string_bytes_body(s: String) -> BytesBody { - BytesBody::from(bytes::Bytes::from(s.into_bytes())) +pub fn error_body(s: String) -> ErrorBody { + ErrorBody::from(bytes::Bytes::from(s.into_bytes())) } pub async fn parse_json_body(req: Request) -> Result -- cgit v1.2.3 From 53746b59e525ff5f518ed59d7831b05e2732785d Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 7 Feb 2024 14:53:13 +0100 Subject: [dep-upgrade-202402] slightly more explicit error management --- src/api/helpers.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/api/helpers.rs') diff --git a/src/api/helpers.rs b/src/api/helpers.rs index 66f4c465..ba7b1599 100644 --- a/src/api/helpers.rs +++ b/src/api/helpers.rs @@ -1,3 +1,5 @@ +use std::convert::Infallible; + use http_body_util::{BodyExt, Full as FullBody}; use hyper::{body::Body, Request, Response}; use idna::domain_to_unicode; @@ -151,10 +153,10 @@ pub fn string_body(s: String) -> BoxBody { bytes_body(bytes::Bytes::from(s.into_bytes())) } pub fn bytes_body(b: bytes::Bytes) -> BoxBody { - BoxBody::new(FullBody::new(b).map_err(|_| unreachable!())) + BoxBody::new(FullBody::new(b).map_err(|_: Infallible| unreachable!())) } pub fn empty_body() -> BoxBody { - BoxBody::new(http_body_util::Empty::new().map_err(|_| unreachable!())) + BoxBody::new(http_body_util::Empty::new().map_err(|_: Infallible| unreachable!())) } pub fn error_body(s: String) -> ErrorBody { ErrorBody::from(bytes::Bytes::from(s.into_bytes())) -- cgit v1.2.3 From e011941964b1c1e0b90f85014d166d64a83ae8e2 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 7 Feb 2024 15:25:49 +0100 Subject: [dep-upgrade-202402] refactor use of BodyStream --- src/api/helpers.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src/api/helpers.rs') diff --git a/src/api/helpers.rs b/src/api/helpers.rs index ba7b1599..5f488912 100644 --- a/src/api/helpers.rs +++ b/src/api/helpers.rs @@ -1,7 +1,12 @@ use std::convert::Infallible; +use futures::{Stream, StreamExt, TryStreamExt}; + use http_body_util::{BodyExt, Full as FullBody}; -use hyper::{body::Body, Request, Response}; +use hyper::{ + body::{Body, Bytes}, + Request, Response, +}; use idna::domain_to_unicode; use serde::{Deserialize, Serialize}; @@ -187,6 +192,22 @@ where .unwrap()) } +pub fn body_stream(body: B) -> impl Stream> +where + B: Body, + ::Error: Into, + E: From, +{ + let stream = http_body_util::BodyStream::new(body); + let stream = TryStreamExt::map_err(stream, Into::into); + stream.map(|x| { + x.and_then(|f| { + f.into_data() + .map_err(|_| E::from(Error::bad_request("non-data frame"))) + }) + }) +} + pub fn is_default(v: &T) -> bool { *v == T::default() } -- cgit v1.2.3