Skip to content
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
3 changes: 3 additions & 0 deletions crates/test-programs/src/bin/http_outbound_request_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ fn main() {
"/get?some=arg&goes=here",
None,
None,
None,
None,
None,
)
.context("/get")
.unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ fn main() {
"/",
None,
None,
None,
None,
None,
);

let e = res.unwrap_err();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ fn main() {
"/",
None,
None,
None,
None,
None,
);

assert!(res.is_err());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ use test_programs::wasi::http::types::{ErrorCode, Method, Scheme};

fn main() {
let addr = std::env::var("HTTP_SERVER").unwrap();
let res =
test_programs::http::request(Method::Connect, Scheme::Http, &addr, "/", None, Some(&[]));
let res = test_programs::http::request(
Method::Connect,
Scheme::Http,
&addr,
"/",
None,
Some(&[]),
None,
None,
None,
);

assert!(matches!(
res.unwrap_err()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ fn main() {
"/post",
Some(&buffer),
None,
None,
None,
None,
)
.context("/post large")
.unwrap();
Expand Down
3 changes: 3 additions & 0 deletions crates/test-programs/src/bin/http_outbound_request_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ fn main() {
"/post",
Some(b"{\"foo\": \"bar\"}"),
None,
None,
None,
None,
)
.context("/post")
.unwrap();
Expand Down
17 changes: 13 additions & 4 deletions crates/test-programs/src/bin/http_outbound_request_put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ use test_programs::wasi::http::types::{Method, Scheme};

fn main() {
let addr = std::env::var("HTTP_SERVER").unwrap();
let res =
test_programs::http::request(Method::Put, Scheme::Http, &addr, "/put", Some(&[]), None)
.context("/put")
.unwrap();
let res = test_programs::http::request(
Method::Put,
Scheme::Http,
&addr,
"/put",
Some(&[]),
None,
None,
None,
None,
)
.context("/put")
.unwrap();

println!("/put: {res:?}");
assert_eq!(res.status, 200);
Expand Down
32 changes: 32 additions & 0 deletions crates/test-programs/src/bin/http_outbound_request_timeout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use anyhow::Context;
use std::net::SocketAddr;
use std::time::Duration;
use test_programs::wasi::http::types::{ErrorCode, Method, Scheme};

fn main() {
// This address inside the TEST-NET-3 address block is expected to time out.
let addr = SocketAddr::from(([203, 0, 113, 12], 80)).to_string();
let timeout = Duration::from_millis(200);
let connect_timeout: Option<u64> = Some(timeout.as_nanos() as u64);
let res = test_programs::http::request(
Method::Get,
Scheme::Http,
&addr,
"/get?some=arg&goes=here",
None,
None,
connect_timeout,
None,
None,
)
.context("/get");

assert!(res.is_err());
assert!(
matches!(
res.unwrap_err().downcast_ref::<ErrorCode>(),
Some(ErrorCode::ConnectionTimeout)
),
"expected connection timeout"
);
Comment on lines +25 to +31
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for matching the timeout explicitly here!

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ fn main() {
"/",
None,
None,
None,
None,
None,
);

// This error arises from input validation in the `set_method` function on `OutgoingRequest`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ fn main() {
"/",
None,
None,
None,
None,
None,
);

assert!(matches!(
Expand Down
17 changes: 16 additions & 1 deletion crates/test-programs/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub fn request(
path_with_query: &str,
body: Option<&[u8]>,
additional_headers: Option<&[(String, Vec<u8>)]>,
connect_timeout: Option<u64>,
first_by_timeout: Option<u64>,
between_bytes_timeout: Option<u64>,
) -> Result<Response> {
fn header_val(v: &str) -> Vec<u8> {
v.to_string().into_bytes()
Expand Down Expand Up @@ -108,7 +111,19 @@ pub fn request(
};
}

let future_response = outgoing_handler::handle(request, None)?;
let options = http_types::RequestOptions::new();
options
.set_connect_timeout(connect_timeout)
.map_err(|()| anyhow!("failed to set connect_timeout"))?;
options
.set_first_byte_timeout(first_by_timeout)
.map_err(|()| anyhow!("failed to set first_byte_timeout"))?;
options
.set_between_bytes_timeout(between_bytes_timeout)
.map_err(|()| anyhow!("failed to set between_bytes_timeout"))?;
let options = Some(options);

let future_response = outgoing_handler::handle(request, options)?;

http_types::OutgoingBody::finish(outgoing_body, None)?;

Expand Down
6 changes: 3 additions & 3 deletions crates/wasi-http/src/http_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ impl<T: WasiHttpView> outgoing_handler::Host for T {

let connect_timeout = opts
.and_then(|opts| opts.connect_timeout)
.unwrap_or(std::time::Duration::from_millis(600 * 1000));
.unwrap_or(std::time::Duration::from_secs(600));

let first_byte_timeout = opts
.and_then(|opts| opts.first_byte_timeout)
.unwrap_or(std::time::Duration::from_millis(600 * 1000));
.unwrap_or(std::time::Duration::from_secs(600));

let between_bytes_timeout = opts
.and_then(|opts| opts.between_bytes_timeout)
.unwrap_or(std::time::Duration::from_millis(600 * 1000));
.unwrap_or(std::time::Duration::from_secs(600));

let req = self.table().delete(request_id)?;
let mut builder = hyper::Request::builder();
Expand Down
3 changes: 2 additions & 1 deletion crates/wasi-http/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ async fn handler(
mut request: http::Request<HyperOutgoingBody>,
between_bytes_timeout: Duration,
) -> Result<IncomingResponseInternal, types::ErrorCode> {
let tcp_stream = TcpStream::connect(authority.clone())
let tcp_stream = timeout(connect_timeout, TcpStream::connect(authority.clone()))
.await
.map_err(|_| types::ErrorCode::ConnectionTimeout)?
.map_err(|e| match e.kind() {
std::io::ErrorKind::AddrNotAvailable => {
dns_error("address not available".to_string(), 0)
Expand Down
6 changes: 6 additions & 0 deletions crates/wasi-http/tests/all/async_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ async fn http_outbound_request_get() -> Result<()> {
run(HTTP_OUTBOUND_REQUEST_GET_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn http_outbound_request_timeout() -> Result<()> {
let server = Server::http1()?;
run(HTTP_OUTBOUND_REQUEST_TIMEOUT_COMPONENT, &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn http_outbound_request_post() -> Result<()> {
let server = Server::http1()?;
Expand Down
6 changes: 6 additions & 0 deletions crates/wasi-http/tests/all/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ fn http_outbound_request_get() -> Result<()> {
run(HTTP_OUTBOUND_REQUEST_GET_COMPONENT, &server)
}

#[test_log::test]
fn http_outbound_request_timeout() -> Result<()> {
let server = Server::http1()?;
run(HTTP_OUTBOUND_REQUEST_TIMEOUT_COMPONENT, &server)
}

#[test_log::test]
fn http_outbound_request_post() -> Result<()> {
let server = Server::http1()?;
Expand Down