Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

6.9 KB
Raw Blame
use futures::join;
use test_programs::p3::wasi::http::client;
use test_programs::p3::wasi::http::types::{ErrorCode, Headers, Method, Request, Scheme, Trailers};
use test_programs::p3::{wit_future, wit_stream};
use wit_bindgen::{FutureReader, FutureWriter, StreamReader, StreamWriter};

struct Component;

test_programs::p3::export!(Component);

fn make_request() -> (
    Request,
    StreamWriter<u8>,
    FutureWriter<Result<Option<Trailers>, ErrorCode>>,
    FutureReader<Result<(), ErrorCode>>,
) {
    let (contents_tx, contents_rx) = wit_stream::new();
    let (request, trailers_tx, transmit) = make_request_with_contents(Some(contents_rx));
    (request, contents_tx, trailers_tx, transmit)
}

fn make_request_with_contents(
    contents_rx: Option<StreamReader<u8>>,
) -> (
    Request,
    FutureWriter<Result<Option<Trailers>, ErrorCode>>,
    FutureReader<Result<(), ErrorCode>>,
) {
    let (trailers_tx, trailers_rx) = wit_future::new(|| todo!());
    let (request, transmit) = Request::new(
        Headers::from_list(&[("Content-Length".to_string(), b"11".to_vec())]).unwrap(),
        contents_rx,
        trailers_rx,
        None,
    );

    request.set_method(&Method::Post).expect("setting method");
    request
        .set_scheme(Some(&Scheme::Http))
        .expect("setting scheme");
    let addr = test_programs::p3::wasi::cli::environment::get_environment()
        .into_iter()
        .find_map(|(k, v)| k.eq("HTTP_SERVER").then_some(v))
        .unwrap();
    request
        .set_authority(Some(&addr))
        .expect("setting authority");
    request
        .set_path_with_query(Some("/"))
        .expect("setting path with query");

    (request, trailers_tx, transmit)
}

impl test_programs::p3::exports::wasi::cli::run::Guest for Component {
    async fn run() -> Result<(), ()> {
        println!("writing enough");
        {
            let (request, mut contents_tx, trailers_tx, transmit) = make_request();
            let (handle, transmit, ()) = join!(
                async { client::send(request).await },
                async { transmit.await },
                async {
                    let remaining = contents_tx.write_all(b"long enough".to_vec()).await;
                    assert_eq!(String::from_utf8_lossy(&remaining), "");
                    trailers_tx.write(Ok(None)).await.unwrap();
                    drop(contents_tx);
                },
            );
            let _res = handle.expect("failed to send request");
            transmit.expect("failed to transmit request");
        }

        println!("writing too little");
        {
            let (request, mut contents_tx, trailers_tx, transmit) = make_request();
            let (_handle, transmit, ()) = join!(
                async { client::send(request).await },
                async { transmit.await },
                async {
                    let remaining = contents_tx.write_all(b"msg".to_vec()).await;
                    assert_eq!(String::from_utf8_lossy(&remaining), "");
                    trailers_tx.write(Ok(None)).await.unwrap();
                    drop(contents_tx);
                },
            );

            // Note: The request body will be polled before `handle` returns.
            // Due to the way implementation is structured, by the time it happens
            // the error will be already available in most cases and `handle` will fail,
            // but it is a race condition, since `handle` may also succeed if
            // polling body returns `Poll::Pending`
            //
            // In those cases, the `ErrorCode::HttpProtocolError` would be observable
            // as the handle.
            //
            // Rather than checking for this condition, which may race, we check the
            // transmit directly below
            let err = transmit.expect_err("request transmission should have failed");
            assert!(
                matches!(
                    err,
                    // Depending on timing, other non-wasmtime hosts may already
                    // have reported the request body size error
                    ErrorCode::HttpRequestBodySize(Some(3))
                ),
                "unexpected error: {err:#?}"
            );
        }

        println!("writing too much");
        {
            let (request, mut contents_tx, trailers_tx, transmit) = make_request();
            let (handle, transmit, ()) = join!(
                async { client::send(request).await },
                async { transmit.await },
                async {
                    let remaining = contents_tx.write_all(b"more than 11 bytes".to_vec()).await;
                    assert_eq!(String::from_utf8_lossy(&remaining), "more than 11 bytes");
                    _ = trailers_tx.write(Ok(None)).await;
                },
            );
            // The request body will be polled before `handle` returns.
            // Due to the way implementation is structured, by the time it happens
            // the error will be already available in most cases and `handle` will fail,
            // but it is a race condition, since `handle` may also succeed if
            // polling body returns `Poll::Pending`
            assert!(
                matches!(
                    handle,
                    Ok(..) | Err(ErrorCode::HttpRequestBodySize(Some(18)))
                ),
                "unexpected handle result: {handle:#?}"
            );
            let err = transmit.expect_err("request transmission should have failed");
            assert!(
                matches!(err, ErrorCode::HttpRequestBodySize(Some(18))),
                "unexpected error: {err:#?}"
            );
        }

        println!("sending with nonzero content-length and without a body");
        {
            let (request, trailers_tx, transmit) = make_request_with_contents(None);
            let (handle, transmit, ()) = join!(
                async { client::send(request).await },
                async { transmit.await },
                async {
                    _ = trailers_tx.write(Ok(None)).await;
                },
            );
            // As above, whether `handle` observes the error races with the
            // body being polled, so it may also succeed.
            assert!(
                matches!(
                    handle,
                    Ok(..)
                        | Err(ErrorCode::HttpRequestBodySize(Some(0)))
                        | Err(ErrorCode::HttpProtocolError)
                ),
                "unexpected handle result: {handle:#?}"
            );
            // With no contents stream the body ends before reaching the
            // declared length, so the host aborts the request body write.
            let err = transmit.expect_err("request transmission should have failed");
            assert!(
                matches!(
                    err,
                    ErrorCode::HttpProtocolError | ErrorCode::HttpRequestBodySize(Some(0))
                ),
                "unexpected error: {err:#?}"
            );
        }
        Ok(())
    }
}

fn main() {}