Skip to content

Commit

Permalink
Merge pull request #1940 from fermyon/response-out-improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
rylev authored Oct 25, 2023
2 parents 7a95022 + 2fbba1b commit 5376331
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
16 changes: 8 additions & 8 deletions examples/wasi-http-rust-streaming-outgoing-body/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{bail, Result};
use futures::{stream, SinkExt, StreamExt, TryStreamExt};
use spin_sdk::http::send;
use spin_sdk::http::{
Fields, IncomingRequest, IncomingResponse, Method, OutgoingBody, OutgoingRequest,
Headers, IncomingRequest, IncomingResponse, Method, OutgoingBody, OutgoingRequest,
OutgoingResponse, ResponseOutparam, Scheme,
};
use spin_sdk::http_component;
Expand Down Expand Up @@ -32,12 +32,12 @@ async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam

let response = OutgoingResponse::new(
200,
&Fields::new(&[("content-type".to_string(), b"text/plain".to_vec())]),
&Headers::new(&[("content-type".to_string(), b"text/plain".to_vec())]),
);

let mut body = response.take_body();

ResponseOutparam::set(response_out, Ok(response));
response_out.set(response);

while let Some((url, result)) = results.next().await {
let payload = match result {
Expand All @@ -54,7 +54,7 @@ async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam
(Method::Post, Some("/echo")) => {
let response = OutgoingResponse::new(
200,
&Fields::new(
&Headers::new(
&headers
.into_iter()
.filter_map(|(k, v)| (k == "content-type").then_some((k, v)))
Expand All @@ -64,7 +64,7 @@ async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam

let mut body = response.take_body();

ResponseOutparam::set(response_out, Ok(response));
response_out.set(response);

let mut stream = request.into_body_stream();
while let Some(chunk) = stream.next().await {
Expand All @@ -84,11 +84,11 @@ async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam
}

_ => {
let response = OutgoingResponse::new(405, &Fields::new(&[]));
let response = OutgoingResponse::new(405, &Headers::new(&[]));

let body = response.write().expect("response should be writable");

ResponseOutparam::set(response_out, Ok(response));
response_out.set(response);

OutgoingBody::finish(body, None);
}
Expand All @@ -105,7 +105,7 @@ async fn hash(url: &Url) -> Result<String> {
scheme => Scheme::Other(scheme.into()),
}),
Some(url.authority()),
&Fields::new(&[]),
&Headers::new(&[]),
);

let response: IncomingResponse = send(request).await?;
Expand Down
6 changes: 2 additions & 4 deletions sdk/rust/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ pub fn http_component(_attr: TokenStream, item: TokenStream) -> TokenStream {

impl From<self::wasi::http::types::IncomingRequest> for ::spin_sdk::http::IncomingRequest {
fn from(req: self::wasi::http::types::IncomingRequest) -> Self {
let req = ::std::mem::ManuallyDrop::new(req);
unsafe { Self::from_handle(req.handle()) }
unsafe { Self::from_handle(req.into_handle()) }
}
}

Expand All @@ -134,8 +133,7 @@ pub fn http_component(_attr: TokenStream, item: TokenStream) -> TokenStream {

impl From<self::wasi::http::types::ResponseOutparam> for ::spin_sdk::http::ResponseOutparam {
fn from(resp: self::wasi::http::types::ResponseOutparam) -> Self {
let resp = ::std::mem::ManuallyDrop::new(resp);
unsafe { Self::from_handle(resp.handle()) }
unsafe { Self::from_handle(resp.into_handle()) }
}
}
}
Expand Down
23 changes: 21 additions & 2 deletions sdk/rust/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ pub use conversions::IntoResponse;

use self::conversions::TryFromIncomingResponse;

use super::wit::wasi::http::types;
#[doc(inline)]
pub use super::wit::wasi::http::types::*;
pub use types::{
Error, Fields, Headers, IncomingRequest, IncomingResponse, Method, OutgoingBody,
OutgoingRequest, OutgoingResponse, Scheme, StatusCode, Trailers,
};

/// A unified request object that can represent both incoming and outgoing requests.
///
Expand Down Expand Up @@ -455,7 +459,22 @@ impl OutgoingResponse {
}
}

/// The out param for setting an `OutgoingResponse`
pub struct ResponseOutparam(types::ResponseOutparam);

impl ResponseOutparam {
#[doc(hidden)]
// This is needed for the macro so we can transfrom the macro's
// `ResponseOutparam` to this `ResponseOutparam`
pub unsafe fn from_handle(handle: u32) -> Self {
Self(types::ResponseOutparam::from_handle(handle))
}

/// Set the outgoing response
pub fn set(self, response: OutgoingResponse) {
types::ResponseOutparam::set(self.0, Ok(response));
}

/// Set with the outgoing response and the supplied buffer
///
/// Will panic if response body has already been taken
Expand All @@ -466,7 +485,7 @@ impl ResponseOutparam {
) -> anyhow::Result<()> {
use futures::SinkExt;
let mut body = response.take_body();
ResponseOutparam::set(self, Ok(response));
self.set(response);
body.send(buffer).await
}
}
Expand Down
6 changes: 2 additions & 4 deletions sdk/rust/src/http/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use std::collections::HashMap;

use async_trait::async_trait;

use super::{
Fields, Headers, IncomingRequest, IncomingResponse, OutgoingRequest, OutgoingResponse,
};
use super::{Headers, IncomingRequest, IncomingResponse, OutgoingRequest, OutgoingResponse};

use super::{responses, NonUtf8BodyError, Request, Response};

Expand Down Expand Up @@ -519,7 +517,7 @@ where
})
.as_ref(),
req.uri().authority().map(|a| a.as_str()),
&Fields::new(&headers),
&Headers::new(&headers),
))
}
}
Expand Down

0 comments on commit 5376331

Please sign in to comment.