From 33a3b367823f5d7d44d325438aa463ac6e0e5653 Mon Sep 17 00:00:00 2001 From: Herr Seppia Date: Mon, 14 Oct 2024 11:53:09 +0200 Subject: [PATCH] rusk: RUES - handle `Accept` request --- rusk/src/lib/http.rs | 4 ++-- rusk/src/lib/http/event.rs | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/rusk/src/lib/http.rs b/rusk/src/lib/http.rs index 5eaa984af4..e441f1db3a 100644 --- a/rusk/src/lib/http.rs +++ b/rusk/src/lib/http.rs @@ -702,7 +702,7 @@ async fn handle_request_rues( Ok(response.map(Into::into)) } else if req.method() == Method::POST { - let event = RuesDispatchEvent::from_request(req).await?; + let (event, binary_resp) = RuesDispatchEvent::from_request(req).await?; let is_binary = event.is_binary(); let mut resp_headers = event.x_headers(); let (responder, mut receiver) = mpsc::unbounded_channel(); @@ -713,7 +713,7 @@ async fn handle_request_rues( .await .expect("An execution should always return a response"); resp_headers.extend(execution_response.headers.clone()); - let mut resp = execution_response.into_http(is_binary)?; + let mut resp = execution_response.into_http(binary_resp)?; for (k, v) in resp_headers { let k = HeaderName::from_str(&k)?; diff --git a/rusk/src/lib/http/event.rs b/rusk/src/lib/http/event.rs index e06e95fe6d..56ad7f27d9 100644 --- a/rusk/src/lib/http/event.rs +++ b/rusk/src/lib/http/event.rs @@ -836,7 +836,9 @@ impl RuesDispatchEvent { .map(|v| v.eq_ignore_ascii_case(CONTENT_TYPE_BINARY)) .unwrap_or_default() } - pub async fn from_request(req: Request) -> anyhow::Result { + pub async fn from_request( + req: Request, + ) -> anyhow::Result<(Self, bool)> { let (parts, body) = req.into_parts(); let uri = RuesEventUri::parse_from_path(parts.uri.path()) @@ -865,9 +867,19 @@ impl RuesDispatchEvent { .and_then(|h| h.to_str().ok()) .unwrap_or_default(); + let binary_request = content_type == CONTENT_TYPE_BINARY; + + let binary_response = binary_request + || parts + .headers + .get(ACCEPT) + .and_then(|h| h.to_str().ok()) + .map(|v| v.eq_ignore_ascii_case(CONTENT_TYPE_BINARY)) + .unwrap_or_default(); + let bytes = body.collect().await?.to_bytes().to_vec(); - let data = match content_type { - CONTENT_TYPE_BINARY => bytes.into(), + let data = match binary_request { + true => bytes.into(), _ => { let text = String::from_utf8(bytes) .map_err(|e| anyhow::anyhow!("Invalid utf8"))?; @@ -885,7 +897,7 @@ impl RuesDispatchEvent { let ret = RuesDispatchEvent { headers, data, uri }; - Ok(ret) + Ok((ret, binary_response)) } }