Skip to content

Commit

Permalink
Merge pull request #2663 from dusk-network/rues-accept-binary
Browse files Browse the repository at this point in the history
rusk: RUES - handle `Accept` request
  • Loading branch information
herr-seppia authored Oct 14, 2024
2 parents 6d235a8 + 33a3b36 commit ee90242
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions rusk/src/lib/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ async fn handle_request_rues<H: HandleRequest>(

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();
Expand All @@ -713,7 +713,7 @@ async fn handle_request_rues<H: HandleRequest>(
.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)?;
Expand Down
20 changes: 16 additions & 4 deletions rusk/src/lib/http/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Incoming>) -> anyhow::Result<Self> {
pub async fn from_request(
req: Request<Incoming>,
) -> anyhow::Result<(Self, bool)> {
let (parts, body) = req.into_parts();

let uri = RuesEventUri::parse_from_path(parts.uri.path())
Expand Down Expand Up @@ -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"))?;
Expand All @@ -885,7 +897,7 @@ impl RuesDispatchEvent {

let ret = RuesDispatchEvent { headers, data, uri };

Ok(ret)
Ok((ret, binary_response))
}
}

Expand Down

0 comments on commit ee90242

Please sign in to comment.