Skip to content

Commit

Permalink
wasi-http: add the port to authority when opening a TCP connection (#…
Browse files Browse the repository at this point in the history
…8671)

* wasi-http: add the port to authority when opening a TCP connection

* Ignore test on riscv64 and s390x

---------

Co-authored-by: Alex Crichton <[email protected]>
  • Loading branch information
iawia002 and alexcrichton authored May 22, 2024
1 parent ac743dd commit f1fe2af
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
42 changes: 42 additions & 0 deletions crates/test-programs/src/bin/api_proxy_forward_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use test_programs::wasi::http::types::{
Headers, IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam,
};

struct T;

test_programs::proxy::export!(T);

impl test_programs::proxy::exports::wasi::http::incoming_handler::Guest for T {
fn handle(request: IncomingRequest, outparam: ResponseOutparam) {
let res = test_programs::http::request(
request.method(),
request.scheme().unwrap(),
request.authority().unwrap().as_str(),
request.path_with_query().unwrap().as_str(),
None,
None,
None,
None,
None,
)
.unwrap();

let hdrs = Headers::from_list(&res.headers).unwrap();
let resp = OutgoingResponse::new(hdrs);
resp.set_status_code(res.status).expect("status code");
let body = resp.body().expect("outgoing response");

ResponseOutparam::set(outparam, Ok(resp));

let out = body.write().expect("outgoing stream");
out.blocking_write_and_flush(res.body.as_ref())
.expect("writing response");

drop(out);
OutgoingBody::finish(body, None).expect("outgoing-body.finish");
}
}

// Technically this should not be here for a proxy, but given the current
// framework for tests it's required since this file is built as a `bin`
fn main() {}
9 changes: 8 additions & 1 deletion crates/wasi-http/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,14 @@ pub async fn default_send_request_handler(
between_bytes_timeout,
}: OutgoingRequestConfig,
) -> Result<IncomingResponse, types::ErrorCode> {
let Some(authority) = request.uri().authority().map(ToString::to_string) else {
let authority = if let Some(authority) = request.uri().authority() {
if authority.port().is_some() {
authority.to_string()
} else {
let port = if use_tls { 443 } else { 80 };
format!("{}:{port}", authority.to_string())
}
} else {
return Err(types::ErrorCode::HttpRequestUriInvalid);
};
let tcp_stream = timeout(connect_timeout, TcpStream::connect(&authority))
Expand Down
21 changes: 21 additions & 0 deletions crates/wasi-http/tests/all/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,27 @@ async fn do_wasi_http_echo(uri: &str, url_header: Option<&str>) -> Result<()> {
Ok(())
}

#[test_log::test(tokio::test)]
// test uses TLS but riscv/s390x don't support that yet
#[cfg_attr(any(target_arch = "riscv64", target_arch = "s390x"), ignore)]
async fn wasi_http_without_port() -> Result<()> {
let req = hyper::Request::builder()
.method(http::Method::GET)
.uri("https://httpbin.org/get");

let response = run_wasi_http(
test_programs_artifacts::API_PROXY_FORWARD_REQUEST_COMPONENT,
req.body(body::empty())?,
None,
None,
)
.await??;

assert_eq!(response.status(), StatusCode::OK);

Ok(())
}

mod body {
use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full};
use hyper::body::Bytes;
Expand Down
5 changes: 5 additions & 0 deletions crates/wasi/tests/all/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ fn api_proxy() {}
#[allow(dead_code)]
fn api_proxy_streaming() {}

// This is tested in the wasi-http crate, but need to satisfy the `foreach_api!`
// macro above.
#[allow(dead_code)]
fn api_proxy_forward_request() {}

wasmtime::component::bindgen!({
world: "test-reactor",
async: true,
Expand Down

0 comments on commit f1fe2af

Please sign in to comment.