Skip to content

Commit

Permalink
virtualize wasi:http/outgoing-handler (#33)
Browse files Browse the repository at this point in the history
* virtualize `wasi:http/outgoing-handler`

Virtualizing `wasi:http/types` isn't very useful unless you also virtualize
`outgoing-handler` and/or `incoming-handler`.  The latter is tricky because it's
an export; I have a plan for it, but that will come in a later PR.  Meanwhile,
this supports `outgoing-handler`, at least.

Signed-off-by: Joel Dice <[email protected]>

* add `wasi:http/outgoing-handler#handle` to `virt_deny`

Signed-off-by: Joel Dice <[email protected]>

---------

Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej authored Nov 16, 2023
1 parent 2343c26 commit 817a4a2
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 1 deletion.
Binary file modified lib/virtual_adapter.debug.wasm
Binary file not shown.
Binary file modified lib/virtual_adapter.wasm
Binary file not shown.
5 changes: 5 additions & 0 deletions src/virt_deny/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ fn get_wasi_http_fns() -> &'static Vec<(&'static str, FuncParams, FuncResults)>
vec![ValType::I32],
vec![ValType::I32],
),
(
"wasi:http/[email protected]#handle",
vec![ValType::I32; 8],
vec![ValType::I32],
),
])
})
}
Expand Down
1 change: 1 addition & 0 deletions src/virt_io/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const WASI_HTTP_EXPORTS: &[&str] = &[
"wasi:http/[email protected]#[dtor]future-incoming-response",
"wasi:http/[email protected]#[method]future-incoming-response.get",
"wasi:http/[email protected]#[method]future-incoming-response.subscribe",
"wasi:http/[email protected]#handle",
];

/// Strip exported WASI functions that implement HTTP access
Expand Down
23 changes: 22 additions & 1 deletion virtual-adapter/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ use crate::exports::wasi::filesystem::types::{
DirectoryEntry, DirectoryEntryStream, ErrorCode, Guest as FilesystemTypes, GuestDescriptor,
GuestDirectoryEntryStream, MetadataHashValue, Modes, NewTimestamp, OpenFlags, PathFlags,
};
use crate::exports::wasi::http::outgoing_handler::Guest as OutgoingHandler;
use crate::exports::wasi::http::types::{
Error as HttpError, Fields, FutureIncomingResponse, FutureTrailers, GuestFields,
GuestFutureIncomingResponse, GuestFutureTrailers, GuestIncomingBody, GuestIncomingRequest,
GuestIncomingResponse, GuestOutgoingBody, GuestOutgoingRequest, GuestOutgoingResponse,
GuestResponseOutparam, IncomingBody, IncomingRequest, IncomingResponse, Method, OutgoingBody,
OutgoingRequest, OutgoingResponse, ResponseOutparam, Scheme, StatusCode,
OutgoingRequest, OutgoingResponse, RequestOptions, ResponseOutparam, Scheme, StatusCode,
};
use crate::exports::wasi::io::poll::{Guest as Poll, Pollable};
use crate::exports::wasi::io::streams::{
Expand All @@ -43,6 +44,7 @@ use crate::wasi::io::streams;

// these are all the subsystems which touch streams + poll
use crate::wasi::clocks::monotonic_clock;
use crate::wasi::http::outgoing_handler;
use crate::wasi::http::types as http_types;
use crate::wasi::io::poll;
use crate::wasi::sockets::ip_name_lookup;
Expand Down Expand Up @@ -597,6 +599,17 @@ impl Preopens for VirtAdapter {
}
}

impl OutgoingHandler for VirtAdapter {
fn handle(
request: Resource<OutgoingRequest>,
options: Option<RequestOptions>,
) -> Result<Resource<FutureIncomingResponse>, HttpError> {
outgoing_handler::handle(Resource::take(request).0, options.map(request_options_map))
.map(|response| Resource::new(HttpFutureIncomingResponse(response)))
.map_err(http_err_map_rev)
}
}

impl Poll for VirtAdapter {
fn poll_list(list: Vec<&Pollable>) -> Vec<u32> {
debug!("CALL wasi:io/poll#poll-list PIDS={list:?}",);
Expand Down Expand Up @@ -1805,6 +1818,14 @@ fn http_err_map_rev(err: http_types::Error) -> HttpError {
}
}

fn request_options_map(options: RequestOptions) -> http_types::RequestOptions {
http_types::RequestOptions {
connect_timeout_ms: options.connect_timeout_ms,
first_byte_timeout_ms: options.first_byte_timeout_ms,
between_bytes_timeout_ms: options.between_bytes_timeout_ms,
}
}

// This function gets mutated by the virtualizer
#[no_mangle]
#[inline(never)]
Expand Down
1 change: 1 addition & 0 deletions virtual-adapter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ wit_bindgen::generate!({
"wasi:http/types/outgoing-request": io::HttpOutgoingRequest,
"wasi:http/types/outgoing-response": io::HttpOutgoingResponse,
"wasi:http/types/response-outparam": io::HttpResponseOutparam,
"wasi:http/outgoing-handler": VirtAdapter,
"wasi:sockets/ip-name-lookup": VirtAdapter,
"wasi:sockets/ip-name-lookup/resolve-address-stream": io::SocketsResolveAddressStream,
"wasi:sockets/tcp/tcp-socket": io::SocketsTcpSocket,
Expand Down
4 changes: 4 additions & 0 deletions wit/virt.wit
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ world virtual-adapter {
export wasi:clocks/monotonic-clock@0.2.0-rc-2023-10-18;
import wasi:http/types@0.2.0-rc-2023-10-18;
export wasi:http/types@0.2.0-rc-2023-10-18;
import wasi:http/outgoing-handler@0.2.0-rc-2023-10-18;
export wasi:http/outgoing-handler@0.2.0-rc-2023-10-18;
import wasi:sockets/ip-name-lookup@0.2.0-rc-2023-10-18;
export wasi:sockets/ip-name-lookup@0.2.0-rc-2023-10-18;
import wasi:sockets/tcp@0.2.0-rc-2023-10-18;
Expand Down Expand Up @@ -71,6 +73,8 @@ world virtual-io-clocks {
world virtual-io-http {
import wasi:http/types@0.2.0-rc-2023-10-18;
export wasi:http/types@0.2.0-rc-2023-10-18;
import wasi:http/outgoing-handler@0.2.0-rc-2023-10-18;
export wasi:http/outgoing-handler@0.2.0-rc-2023-10-18;
}

world virtual-fs {
Expand Down

0 comments on commit 817a4a2

Please sign in to comment.