Skip to content

Commit 5376331

Browse files
authored
Merge pull request #1940 from fermyon/response-out-improvement
2 parents 7a95022 + 2fbba1b commit 5376331

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

examples/wasi-http-rust-streaming-outgoing-body/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::{bail, Result};
22
use futures::{stream, SinkExt, StreamExt, TryStreamExt};
33
use spin_sdk::http::send;
44
use spin_sdk::http::{
5-
Fields, IncomingRequest, IncomingResponse, Method, OutgoingBody, OutgoingRequest,
5+
Headers, IncomingRequest, IncomingResponse, Method, OutgoingBody, OutgoingRequest,
66
OutgoingResponse, ResponseOutparam, Scheme,
77
};
88
use spin_sdk::http_component;
@@ -32,12 +32,12 @@ async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam
3232

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

3838
let mut body = response.take_body();
3939

40-
ResponseOutparam::set(response_out, Ok(response));
40+
response_out.set(response);
4141

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

6565
let mut body = response.take_body();
6666

67-
ResponseOutparam::set(response_out, Ok(response));
67+
response_out.set(response);
6868

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

8686
_ => {
87-
let response = OutgoingResponse::new(405, &Fields::new(&[]));
87+
let response = OutgoingResponse::new(405, &Headers::new(&[]));
8888

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

91-
ResponseOutparam::set(response_out, Ok(response));
91+
response_out.set(response);
9292

9393
OutgoingBody::finish(body, None);
9494
}
@@ -105,7 +105,7 @@ async fn hash(url: &Url) -> Result<String> {
105105
scheme => Scheme::Other(scheme.into()),
106106
}),
107107
Some(url.authority()),
108-
&Fields::new(&[]),
108+
&Headers::new(&[]),
109109
);
110110

111111
let response: IncomingResponse = send(request).await?;

sdk/rust/macro/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ pub fn http_component(_attr: TokenStream, item: TokenStream) -> TokenStream {
121121

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

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

135134
impl From<self::wasi::http::types::ResponseOutparam> for ::spin_sdk::http::ResponseOutparam {
136135
fn from(resp: self::wasi::http::types::ResponseOutparam) -> Self {
137-
let resp = ::std::mem::ManuallyDrop::new(resp);
138-
unsafe { Self::from_handle(resp.handle()) }
136+
unsafe { Self::from_handle(resp.into_handle()) }
139137
}
140138
}
141139
}

sdk/rust/src/http.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ pub use conversions::IntoResponse;
88

99
use self::conversions::TryFromIncomingResponse;
1010

11+
use super::wit::wasi::http::types;
1112
#[doc(inline)]
12-
pub use super::wit::wasi::http::types::*;
13+
pub use types::{
14+
Error, Fields, Headers, IncomingRequest, IncomingResponse, Method, OutgoingBody,
15+
OutgoingRequest, OutgoingResponse, Scheme, StatusCode, Trailers,
16+
};
1317

1418
/// A unified request object that can represent both incoming and outgoing requests.
1519
///
@@ -455,7 +459,22 @@ impl OutgoingResponse {
455459
}
456460
}
457461

462+
/// The out param for setting an `OutgoingResponse`
463+
pub struct ResponseOutparam(types::ResponseOutparam);
464+
458465
impl ResponseOutparam {
466+
#[doc(hidden)]
467+
// This is needed for the macro so we can transfrom the macro's
468+
// `ResponseOutparam` to this `ResponseOutparam`
469+
pub unsafe fn from_handle(handle: u32) -> Self {
470+
Self(types::ResponseOutparam::from_handle(handle))
471+
}
472+
473+
/// Set the outgoing response
474+
pub fn set(self, response: OutgoingResponse) {
475+
types::ResponseOutparam::set(self.0, Ok(response));
476+
}
477+
459478
/// Set with the outgoing response and the supplied buffer
460479
///
461480
/// Will panic if response body has already been taken
@@ -466,7 +485,7 @@ impl ResponseOutparam {
466485
) -> anyhow::Result<()> {
467486
use futures::SinkExt;
468487
let mut body = response.take_body();
469-
ResponseOutparam::set(self, Ok(response));
488+
self.set(response);
470489
body.send(buffer).await
471490
}
472491
}

sdk/rust/src/http/conversions.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use std::collections::HashMap;
22

33
use async_trait::async_trait;
44

5-
use super::{
6-
Fields, Headers, IncomingRequest, IncomingResponse, OutgoingRequest, OutgoingResponse,
7-
};
5+
use super::{Headers, IncomingRequest, IncomingResponse, OutgoingRequest, OutgoingResponse};
86

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

@@ -519,7 +517,7 @@ where
519517
})
520518
.as_ref(),
521519
req.uri().authority().map(|a| a.as_str()),
522-
&Fields::new(&headers),
520+
&Headers::new(&headers),
523521
))
524522
}
525523
}

0 commit comments

Comments
 (0)