Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync with upstream wasi-http #7406

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/test-programs/src/bin/api_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ struct T;
impl bindings::exports::wasi::http::incoming_handler::Guest for T {
fn handle(_request: IncomingRequest, outparam: ResponseOutparam) {
let hdrs = bindings::wasi::http::types::Headers::new(&[]);
let resp = bindings::wasi::http::types::OutgoingResponse::new(200, &hdrs);
let body = resp.write().expect("outgoing response");
let resp = bindings::wasi::http::types::OutgoingResponse::new(200, hdrs);
let body = resp.body().expect("outgoing response");

bindings::wasi::http::types::ResponseOutparam::set(outparam, Ok(resp));

Expand Down
14 changes: 7 additions & 7 deletions crates/test-programs/src/bin/api_proxy_streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ 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())]),
Fields::new(&[("content-type".to_string(), b"text/plain".to_vec())]),
);

let mut body =
executor::outgoing_body(response.write().expect("response should be writable"));
executor::outgoing_body(response.body().expect("response should be writable"));

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

Expand All @@ -75,7 +75,7 @@ async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam
(Method::Post, Some("/echo")) => {
let response = OutgoingResponse::new(
200,
&Fields::new(
Fields::new(
&headers
.into_iter()
.filter_map(|(k, v)| (k == "content-type").then_some((k, v)))
Expand All @@ -84,7 +84,7 @@ async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam
);

let mut body =
executor::outgoing_body(response.write().expect("response should be writable"));
executor::outgoing_body(response.body().expect("response should be writable"));

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

Expand All @@ -108,9 +108,9 @@ async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam
}

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

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

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

Expand All @@ -137,7 +137,7 @@ async fn hash(url: &Url) -> Result<String> {
String::new()
}
)),
&Fields::new(&[]),
Fields::new(&[]),
);

let response = executor::outgoing_request_send(request).await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ fn main() {
None,
Some(&http_types::Scheme::Https),
Some("www.example.com"),
&headers,
headers,
);
let outgoing_body = request.write().unwrap();
let outgoing_body = request.body().unwrap();
let request_body = outgoing_body.write().unwrap();
request_body
.blocking_write_and_flush("request-body".as_bytes())
Expand All @@ -25,8 +25,8 @@ fn main() {
"Content-Type".to_string(),
"application/text".to_string().into_bytes(),
)]);
let response = http_types::OutgoingResponse::new(200, &headers);
let outgoing_body = response.write().unwrap();
let response = http_types::OutgoingResponse::new(200, headers);
let outgoing_body = response.body().unwrap();
let response_body = outgoing_body.write().unwrap();
response_body
.blocking_write_and_flush("response-body".as_bytes())
Expand Down
4 changes: 2 additions & 2 deletions crates/test-programs/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ pub fn request(
Some(path_with_query),
Some(&scheme),
Some(authority),
&headers,
headers,
);

let outgoing_body = request
.write()
.body()
.map_err(|_| anyhow!("outgoing request write failed"))?;

if let Some(mut buf) = body {
Expand Down
15 changes: 11 additions & 4 deletions crates/wasi-http/src/http_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,22 @@ impl<T: WasiHttpView> outgoing_handler::Host for T {
}
};

let authority = if req.authority.find(':').is_some() {
req.authority.clone()
let authority = if let Some(authority) = req.authority {
if authority.find(':').is_some() {
authority
} else {
format!("{}:{port}", authority)
}
} else {
format!("{}:{port}", req.authority)
String::new()
};

let mut builder = hyper::Request::builder()
.method(method)
.uri(format!("{scheme}{authority}{}", req.path_with_query))
.uri(format!(
"{scheme}{authority}{}",
req.path_with_query.as_ref().map_or("", String::as_ref)
))
.header(hyper::header::HOST, &authority);

for (k, v) in req.headers.iter() {
Expand Down
49 changes: 47 additions & 2 deletions crates/wasi-http/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,51 @@ fn invalid_url(e: std::io::Error) -> anyhow::Error {
))
}

impl From<http::Method> for types::Method {
fn from(method: http::Method) -> Self {
if method == http::Method::GET {
types::Method::Get
} else if method == hyper::Method::HEAD {
types::Method::Head
} else if method == hyper::Method::POST {
types::Method::Post
} else if method == hyper::Method::PUT {
types::Method::Put
} else if method == hyper::Method::DELETE {
types::Method::Delete
} else if method == hyper::Method::CONNECT {
types::Method::Connect
} else if method == hyper::Method::OPTIONS {
types::Method::Options
} else if method == hyper::Method::TRACE {
types::Method::Trace
} else if method == hyper::Method::PATCH {
types::Method::Patch
} else {
types::Method::Other(method.to_string())
}
}
}

impl TryInto<http::Method> for types::Method {
type Error = http::method::InvalidMethod;

fn try_into(self) -> Result<http::Method, Self::Error> {
match self {
Method::Get => Ok(http::Method::GET),
Method::Head => Ok(http::Method::HEAD),
Method::Post => Ok(http::Method::POST),
Method::Put => Ok(http::Method::PUT),
Method::Delete => Ok(http::Method::DELETE),
Method::Connect => Ok(http::Method::CONNECT),
Method::Options => Ok(http::Method::OPTIONS),
Method::Trace => Ok(http::Method::TRACE),
Method::Patch => Ok(http::Method::PATCH),
Method::Other(s) => http::Method::from_bytes(s.as_bytes()),
}
}
}

pub struct HostIncomingRequest {
pub parts: http::request::Parts,
pub body: Option<HostIncomingBodyBuilder>,
Expand All @@ -206,8 +251,8 @@ pub struct HostResponseOutparam {
pub struct HostOutgoingRequest {
pub method: Method,
pub scheme: Option<Scheme>,
pub path_with_query: String,
pub authority: String,
pub path_with_query: Option<String>,
pub authority: Option<String>,
pub headers: FieldMap,
pub body: Option<HyperOutgoingBody>,
}
Expand Down
Loading