kobako 0.30.0 → 0.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.githooks/linux-tests.sh +29 -0
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +460 -0
- data/Cargo.lock +868 -447
- data/README.md +43 -4
- data/ROADMAP.md +1 -2
- data/crates/kobako-runtime/CHANGELOG.md +309 -0
- data/crates/kobako-runtime/Cargo.toml +58 -11
- data/crates/kobako-runtime/README.md +7 -4
- data/crates/kobako-runtime/src/cred.rs +136 -0
- data/crates/kobako-runtime/src/dispatch.rs +4 -5
- data/crates/kobako-runtime/src/endpoint/buffer.rs +153 -44
- data/crates/kobako-runtime/src/endpoint/dispatch.rs +80 -41
- data/crates/kobako-runtime/src/endpoint/invoke.rs +23 -36
- data/crates/kobako-runtime/src/endpoint.rs +137 -36
- data/crates/kobako-runtime/src/errno.rs +391 -0
- data/crates/kobako-runtime/src/error.rs +31 -10
- data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
- data/crates/kobako-runtime/src/filesystem/bind/fid.rs +300 -22
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +101 -29
- data/crates/kobako-runtime/src/filesystem/bind/unicode.rs +109 -0
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +52 -34
- data/crates/kobako-runtime/src/filesystem/bind.rs +812 -447
- data/crates/kobako-runtime/src/filesystem/overlay/file.rs +6 -6
- data/crates/kobako-runtime/src/filesystem/overlay/path.rs +43 -65
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +11 -16
- data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +47 -72
- data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +12 -12
- data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +4 -7
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +120 -37
- data/crates/kobako-runtime/src/filesystem/overlay.rs +578 -388
- data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
- data/crates/kobako-runtime/src/filesystem/rdir.rs +14 -50
- data/crates/kobako-runtime/src/filesystem/testing.rs +31 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +3 -3
- data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +57 -35
- data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +2 -2
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +219 -173
- data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
- data/crates/kobako-runtime/src/filesystem.rs +8 -3
- data/crates/kobako-runtime/src/fork.rs +319 -0
- data/crates/kobako-runtime/src/grant.rs +3 -0
- data/crates/kobako-runtime/src/hashtable.rs +4 -2
- data/crates/kobako-runtime/src/lib.rs +18 -7
- data/crates/kobako-runtime/src/limits.rs +171 -0
- data/crates/kobako-runtime/src/locking/seqlock.rs +29 -3
- data/crates/kobako-runtime/src/locking.rs +518 -113
- data/crates/kobako-runtime/src/memcg.rs +306 -0
- data/crates/kobako-runtime/src/mount.rs +37 -9
- data/crates/kobako-runtime/src/net/backends.rs +43 -0
- data/crates/kobako-runtime/src/net/connections.rs +219 -0
- data/crates/kobako-runtime/src/net/credential.rs +125 -0
- data/crates/kobako-runtime/src/net/egress.rs +180 -0
- data/crates/kobako-runtime/src/net/family.rs +181 -0
- data/crates/kobako-runtime/src/net/http.rs +3165 -0
- data/crates/kobako-runtime/src/net/lookup.rs +385 -0
- data/crates/kobako-runtime/src/net/mock.rs +251 -0
- data/crates/kobako-runtime/src/net/namespace.rs +1368 -0
- data/crates/kobako-runtime/src/net/reactor.rs +626 -0
- data/crates/kobako-runtime/src/net/resolv/darwin.rs +280 -0
- data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +519 -0
- data/crates/kobako-runtime/src/net/resolv/stub.rs +542 -0
- data/crates/kobako-runtime/src/net/resolv/windows.rs +218 -0
- data/crates/kobako-runtime/src/net/resolv.rs +251 -0
- data/crates/kobako-runtime/src/net/skbuff.rs +109 -0
- data/crates/kobako-runtime/src/net/socket.rs +869 -0
- data/crates/kobako-runtime/src/net/task.rs +223 -0
- data/crates/kobako-runtime/src/net/tcp.rs +1840 -0
- data/crates/kobako-runtime/src/net/tls.rs +120 -0
- data/crates/kobako-runtime/src/net/transport.rs +304 -0
- data/crates/kobako-runtime/src/net/udp.rs +1105 -0
- data/crates/kobako-runtime/src/net.rs +45 -0
- data/crates/kobako-runtime/src/network/builder.rs +177 -0
- data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
- data/crates/kobako-runtime/src/network.rs +551 -0
- data/crates/kobako-runtime/src/policy.rs +16 -0
- data/crates/kobako-runtime/src/profile.rs +29 -144
- data/crates/kobako-runtime/src/runtime.rs +3 -5
- data/crates/kobako-runtime/src/sched.rs +338 -51
- data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
- data/crates/kobako-runtime/src/{vfs/cred.rs → security/hooks.rs} +52 -144
- data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
- data/crates/kobako-runtime/src/security.rs +125 -209
- data/crates/kobako-runtime/src/snapshot.rs +121 -2
- data/crates/kobako-runtime/src/vfs/attr.rs +5 -5
- data/crates/kobako-runtime/src/vfs/dentry.rs +210 -117
- data/crates/kobako-runtime/src/vfs/file.rs +243 -248
- data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
- data/crates/kobako-runtime/src/vfs/kern_path.rs +62 -53
- data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
- data/crates/kobako-runtime/src/vfs/namei.rs +1162 -818
- data/crates/kobako-runtime/src/vfs/namespace.rs +245 -0
- data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
- data/crates/kobako-runtime/src/vfs/openat2.rs +3 -3
- data/crates/kobako-runtime/src/vfs/path.rs +6 -6
- data/crates/kobako-runtime/src/vfs/readdir.rs +6 -6
- data/crates/kobako-runtime/src/vfs/stat.rs +248 -23
- data/crates/kobako-runtime/src/vfs/super_block.rs +18 -14
- data/crates/kobako-runtime/src/vfs/utimes.rs +37 -27
- data/crates/kobako-runtime/src/vfs.rs +10 -6
- data/crates/kobako-runtime/src/wait.rs +381 -0
- data/crates/kobako-transport/CHANGELOG.md +38 -0
- data/crates/kobako-transport/Cargo.toml +1 -1
- data/crates/kobako-transport/README.md +3 -4
- data/crates/kobako-transport/src/abi.rs +78 -72
- data/crates/kobako-transport/src/envelope/bytes.rs +17 -6
- data/crates/kobako-transport/src/envelope/call.rs +32 -4
- data/crates/kobako-transport/src/envelope/error_record.rs +21 -0
- data/crates/kobako-transport/src/envelope/name.rs +99 -0
- data/crates/kobako-transport/src/envelope/outcome.rs +9 -9
- data/crates/kobako-transport/src/envelope/preamble.rs +24 -21
- data/crates/kobako-transport/src/envelope/reply.rs +27 -27
- data/crates/kobako-transport/src/envelope/unsupported.rs +10 -10
- data/crates/kobako-transport/src/envelope.rs +13 -8
- data/crates/kobako-wasmtime/CHANGELOG.md +208 -0
- data/crates/kobako-wasmtime/Cargo.toml +47 -7
- data/crates/kobako-wasmtime/README.md +4 -3
- data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
- data/crates/kobako-wasmtime/build.rs +16 -0
- data/crates/kobako-wasmtime/src/abi.rs +18 -37
- data/crates/kobako-wasmtime/src/ambient.rs +43 -19
- data/crates/kobako-wasmtime/src/cache/unix.rs +95 -0
- data/crates/kobako-wasmtime/src/cache/windows.rs +359 -0
- data/crates/kobako-wasmtime/src/cache.rs +106 -113
- data/crates/kobako-wasmtime/src/config.rs +52 -68
- data/crates/kobako-wasmtime/src/driver.rs +80 -82
- data/crates/kobako-wasmtime/src/exports.rs +143 -17
- data/crates/kobako-wasmtime/src/guest.rs +12 -6
- data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
- data/crates/kobako-wasmtime/src/imports.rs +42 -18
- data/crates/kobako-wasmtime/src/instance_pre.rs +4 -4
- data/crates/kobako-wasmtime/src/invocation.rs +91 -15
- data/crates/kobako-wasmtime/src/lib.rs +7 -3
- data/crates/kobako-wasmtime/src/limiter.rs +130 -12
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +305 -0
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +9 -20
- data/crates/kobako-wasmtime/src/preview1/differential.rs +27 -31
- data/crates/kobako-wasmtime/src/preview1/errno.rs +55 -66
- data/crates/kobako-wasmtime/src/preview1/fd.rs +347 -312
- data/crates/kobako-wasmtime/src/preview1/memory.rs +21 -12
- data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +34 -53
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +335 -388
- data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
- data/crates/kobako-wasmtime/src/preview1/poll.rs +255 -125
- data/crates/kobako-wasmtime/src/preview1/random.rs +125 -0
- data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
- data/crates/kobako-wasmtime/src/preview1/testing.rs +31 -0
- data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
- data/crates/kobako-wasmtime/src/preview1.rs +131 -51
- data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
- data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
- data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
- data/crates/kobako-wasmtime/src/preview2/host.rs +4319 -0
- data/crates/kobako-wasmtime/src/preview2/http.rs +1587 -0
- data/crates/kobako-wasmtime/src/preview2/io.rs +328 -0
- data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
- data/crates/kobako-wasmtime/src/preview2/table.rs +257 -0
- data/crates/kobako-wasmtime/src/preview2.rs +57 -0
- data/crates/kobako-wasmtime/src/trap.rs +88 -28
- data/crates/kobako-wasmtime/src/waits.rs +59 -0
- data/crates/kobako-wasmtime/src/wasi.rs +291 -81
- data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
- data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
- data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
- data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
- data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
- data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
- data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
- data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +13 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +6 -2
- data/ext/kobako/src/{runtime/errors.rs → errors.rs} +34 -26
- data/ext/kobako/src/escape.rs +404 -0
- data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +34 -32
- data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +158 -170
- data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +71 -23
- data/ext/kobako/src/fork.rs +72 -0
- data/ext/kobako/src/gvl.rs +729 -0
- data/ext/kobako/src/handoff.rs +490 -0
- data/ext/kobako/src/lib.rs +74 -3
- data/ext/kobako/src/network/mock.rs +187 -0
- data/ext/kobako/src/network.rs +593 -0
- data/ext/kobako/src/pinned_value.rs +44 -0
- data/ext/kobako/src/runtime/bridge.rs +58 -50
- data/ext/kobako/src/runtime/snapshot.rs +4 -4
- data/ext/kobako/src/runtime.rs +272 -163
- data/ext/kobako/src/wire.rs +42 -0
- data/lib/kobako/catalog/extensions.rb +14 -2
- data/lib/kobako/catalog/flags.rb +3 -5
- data/lib/kobako/catalog/grants.rb +5 -4
- data/lib/kobako/catalog/handles.rb +14 -32
- data/lib/kobako/catalog/mount.rb +11 -3
- data/lib/kobako/catalog/network.rb +35 -0
- data/lib/kobako/catalog/replacements.rb +28 -0
- data/lib/kobako/catalog/services.rb +5 -19
- data/lib/kobako/catalog/snippets.rb +3 -18
- data/lib/kobako/catalog.rb +3 -0
- data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
- data/lib/kobako/codec/decoder.rb +5 -5
- data/lib/kobako/codec/ext_types.rb +3 -3
- data/lib/kobako/codec/handle_walk.rb +3 -43
- data/lib/kobako/codec/state.rb +2 -18
- data/lib/kobako/codec/value_budget.rb +133 -41
- data/lib/kobako/codec.rb +6 -7
- data/lib/kobako/context.rb +24 -11
- data/lib/kobako/errors.rb +18 -18
- data/lib/kobako/extension.rb +3 -2
- data/lib/kobako/filesystem.rb +10 -3
- data/lib/kobako/fork.rb +42 -0
- data/lib/kobako/handle.rb +2 -3
- data/lib/kobako/names.rb +28 -0
- data/lib/kobako/network.rb +210 -0
- data/lib/kobako/outcome.rb +7 -7
- data/lib/kobako/runtime.rb +3 -2
- data/lib/kobako/sandbox.rb +73 -35
- data/lib/kobako/sandbox_options.rb +42 -109
- data/lib/kobako/transport/argument_wrap.rb +64 -0
- data/lib/kobako/transport/block_failure.rb +1 -1
- data/lib/kobako/transport/dispatcher.rb +6 -6
- data/lib/kobako/transport/exposure.rb +26 -15
- data/lib/kobako/transport/reflection.rb +21 -4
- data/lib/kobako/transport/run.rb +8 -23
- data/lib/kobako/transport/yielder.rb +2 -9
- data/lib/kobako/transport.rb +1 -0
- data/lib/kobako/usage.rb +4 -2
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +8 -4
- data/release-please-config.json +15 -0
- data/sig/kobako/catalog/extensions.rbs +2 -0
- data/sig/kobako/catalog/grants.rbs +7 -4
- data/sig/kobako/catalog/handles.rbs +2 -2
- data/sig/kobako/catalog/mount.rbs +3 -1
- data/sig/kobako/catalog/network.rbs +20 -0
- data/sig/kobako/catalog/replacements.rbs +14 -0
- data/sig/kobako/catalog/services.rbs +0 -4
- data/sig/kobako/catalog/snippets.rbs +0 -2
- data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
- data/sig/kobako/codec/handle_walk.rbs +0 -6
- data/sig/kobako/codec/state.rbs +0 -4
- data/sig/kobako/codec/value_budget.rbs +48 -5
- data/sig/kobako/codec.rbs +2 -2
- data/sig/kobako/context.rbs +3 -1
- data/sig/kobako/errors.rbs +4 -7
- data/sig/kobako/filesystem.rbs +21 -3
- data/sig/kobako/fork.rbs +17 -0
- data/sig/kobako/network.rbs +115 -0
- data/sig/kobako/outcome.rbs +1 -1
- data/sig/kobako/runtime.rbs +32 -3
- data/sig/kobako/sandbox.rbs +10 -2
- data/sig/kobako/sandbox_options.rbs +29 -40
- data/sig/kobako/transport/argument_wrap.rbs +15 -0
- data/sig/kobako/transport/error.rbs +1 -1
- data/sig/kobako/transport/exposure.rbs +2 -4
- data/sig/kobako/transport/reflection.rbs +10 -0
- data/sig/kobako/transport/run.rbs +0 -2
- data/sig/kobako/transport/yielder.rbs +0 -3
- data/sig/kobako/transport.rbs +1 -1
- data/sig/kobako.rbs +13 -0
- metadata +97 -37
- data/crates/kobako-runtime/src/endpoint/capture.rs +0 -115
- data/crates/kobako-runtime/src/endpoint/memory_cap.rs +0 -238
- data/crates/kobako-runtime/src/filesystem/path.rs +0 -21
- data/crates/kobako-runtime/src/vfs/errno.rs +0 -153
- data/crates/kobako-runtime/tests/linux_baseline/calls.rs +0 -171
- data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +0 -276
- data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +0 -323
- data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
- data/crates/kobako-runtime/tests/linux_baseline/linux.rs +0 -449
- data/crates/kobako-runtime/tests/linux_baseline/main.rs +0 -611
- data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +0 -236
- data/crates/kobako-wasmtime/src/descriptors.rs +0 -200
- data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +0 -43
- data/crates/kobako-wasmtime/tests/integration/guest_file.rs +0 -1085
- data/crates/kobako-wasmtime/tests/integration/main.rs +0 -29
- data/crates/kobako-wasmtime/tests/integration/preamble_flags.rs +0 -70
- data/crates/kobako-wasmtime/tests/integration/snippet_compile.rs +0 -95
- data/crates/kobako-wasmtime/tests/integration/support/mod.rs +0 -30
- data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +0 -86
- data/crates/kobako-wasmtime/tests/integration/unsupported_call.rs +0 -51
- data/ext/kobako/src/runtime/escape.rs +0 -67
- data/ext/kobako/src/runtime/gvl.rs +0 -147
- data/ext/kobako/src/runtime/pinned_value.rs +0 -31
- data/lib/kobako/constant_name.rb +0 -8
- data/lib/kobako/pool.rb +0 -140
- data/sig/kobako/constant_name.rbs +0 -3
- data/sig/kobako/pool.rbs +0 -42
|
@@ -0,0 +1,1587 @@
|
|
|
1
|
+
//! What the Wasm Driver answers each `wasi:http` import a client makes. A
|
|
2
|
+
//! request the guest builds is handed to the Invocation's namespace, which
|
|
3
|
+
//! judges it on the guest thread and sends it through the outbound seam; until
|
|
4
|
+
//! a Grant gives the guest the network, `handle` answers `HTTP-request-denied`,
|
|
5
|
+
//! as a host that sends nothing does.
|
|
6
|
+
|
|
7
|
+
use std::sync::atomic::{AtomicBool, Ordering};
|
|
8
|
+
use std::sync::{Arc, Mutex};
|
|
9
|
+
use std::time::Duration;
|
|
10
|
+
|
|
11
|
+
use kobako_runtime::net::{self, Exchange, FieldError, RequestBody};
|
|
12
|
+
|
|
13
|
+
use super::bindings::wasi_http_types_0_2_12::{
|
|
14
|
+
DNSErrorPayload, ErrorCode, Fields, FutureIncomingResponse, FutureTrailers, HeaderError,
|
|
15
|
+
IncomingBody, IncomingResponse, Method, OutgoingBody, OutgoingRequest, RequestOptions, Scheme,
|
|
16
|
+
TLSAlertReceivedPayload,
|
|
17
|
+
};
|
|
18
|
+
use super::bindings::wasi_io_poll_0_2_12::Pollable;
|
|
19
|
+
use super::bindings::wasi_io_streams_0_2_12::{InputStream, OutputStream};
|
|
20
|
+
use super::bindings::HttpHost;
|
|
21
|
+
use super::error;
|
|
22
|
+
use super::io::{self, SharedFile, Stream};
|
|
23
|
+
use super::table;
|
|
24
|
+
use super::to_nanos;
|
|
25
|
+
use crate::invocation::Invocation;
|
|
26
|
+
|
|
27
|
+
/// A request's body and whether the guest asked for it, shared between the
|
|
28
|
+
/// request and the `outgoing-body` its `body` hands out.
|
|
29
|
+
pub(crate) struct BodyLink {
|
|
30
|
+
body: Arc<RequestBody>,
|
|
31
|
+
handed: AtomicBool,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/// A request the guest is building.
|
|
35
|
+
pub(crate) struct Request {
|
|
36
|
+
method: Method,
|
|
37
|
+
scheme: Option<Scheme>,
|
|
38
|
+
authority: Option<String>,
|
|
39
|
+
path_with_query: Option<String>,
|
|
40
|
+
headers: Arc<net::Fields>,
|
|
41
|
+
link: Arc<BodyLink>,
|
|
42
|
+
/// The options `handle` was given.
|
|
43
|
+
timeouts: net::Timeouts,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
impl Request {
|
|
47
|
+
/// The room its method, authority, path, and headers take together, as
|
|
48
|
+
/// a server's header buffer holds the request line beside the headers.
|
|
49
|
+
fn room(&self) -> usize {
|
|
50
|
+
let method = match &self.method {
|
|
51
|
+
Method::Other(other) => other.len(),
|
|
52
|
+
_ => 0,
|
|
53
|
+
};
|
|
54
|
+
method
|
|
55
|
+
+ self.authority.as_ref().map_or(0, String::len)
|
|
56
|
+
+ self.path_with_query.as_ref().map_or(0, String::len)
|
|
57
|
+
+ self.headers.room()
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// Where a `future-incoming-response` stands.
|
|
62
|
+
pub(crate) enum FutureState {
|
|
63
|
+
/// Sent, with the file its response's arrival wakes, made as it leaves.
|
|
64
|
+
Sent(Arc<Exchange>, SharedFile),
|
|
65
|
+
Refused(ErrorCode),
|
|
66
|
+
/// Its response was taken.
|
|
67
|
+
Taken,
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// The host type each `wasi:http` resource the guest holds stands for, named
|
|
71
|
+
/// as its WIT resource is, apart from the bindings' handles of those names.
|
|
72
|
+
pub(crate) mod resource {
|
|
73
|
+
use std::sync::Arc;
|
|
74
|
+
|
|
75
|
+
use kobako_runtime::net::{self, Exchange};
|
|
76
|
+
|
|
77
|
+
use super::{BodyLink, FutureState, Request};
|
|
78
|
+
|
|
79
|
+
pub(crate) struct Fields {
|
|
80
|
+
pub(crate) entries: Arc<net::Fields>,
|
|
81
|
+
pub(crate) mutable: bool,
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
pub(crate) struct OutgoingRequest(pub(crate) Request);
|
|
85
|
+
|
|
86
|
+
pub(crate) struct OutgoingBody {
|
|
87
|
+
pub(crate) link: Arc<BodyLink>,
|
|
88
|
+
/// Whether its stream was handed out.
|
|
89
|
+
pub(crate) stream_handed: bool,
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
pub(crate) struct FutureIncomingResponse(pub(crate) FutureState);
|
|
93
|
+
|
|
94
|
+
/// The timeouts a request is handled with.
|
|
95
|
+
#[derive(Default)]
|
|
96
|
+
pub(crate) struct RequestOptions(pub(crate) net::Timeouts);
|
|
97
|
+
|
|
98
|
+
/// A response whose head its exchange holds once.
|
|
99
|
+
pub(crate) struct IncomingResponse {
|
|
100
|
+
pub(crate) status: u16,
|
|
101
|
+
pub(crate) exchange: Arc<Exchange>,
|
|
102
|
+
/// Whether its body was handed out.
|
|
103
|
+
pub(crate) consumed: bool,
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
pub(crate) struct IncomingBody {
|
|
107
|
+
pub(crate) exchange: Arc<Exchange>,
|
|
108
|
+
/// Whether its stream was handed out.
|
|
109
|
+
pub(crate) stream_handed: bool,
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
pub(crate) struct FutureTrailers;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// The `header-error` a refused header answers.
|
|
116
|
+
fn to_header_error(err: FieldError) -> HeaderError {
|
|
117
|
+
match err {
|
|
118
|
+
FieldError::Forbidden => HeaderError::Forbidden,
|
|
119
|
+
_ => HeaderError::InvalidSyntax,
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/// The error code a request's failure answers, carrying no host text.
|
|
124
|
+
fn to_error_code(err: net::HttpError) -> ErrorCode {
|
|
125
|
+
match err {
|
|
126
|
+
net::HttpError::Denied => ErrorCode::HTTPRequestDenied,
|
|
127
|
+
// wasmtime-wasi-http answers a lookup that failed this way.
|
|
128
|
+
net::HttpError::DnsError => ErrorCode::DNSError(DNSErrorPayload {
|
|
129
|
+
rcode: Some("address not available".to_string()),
|
|
130
|
+
info_code: None,
|
|
131
|
+
}),
|
|
132
|
+
net::HttpError::DestinationUnavailable => ErrorCode::DestinationUnavailable,
|
|
133
|
+
net::HttpError::ConnectionRefused => ErrorCode::ConnectionRefused,
|
|
134
|
+
net::HttpError::ConnectionTimeout => ErrorCode::ConnectionTimeout,
|
|
135
|
+
net::HttpError::ConnectionReadTimeout => ErrorCode::ConnectionReadTimeout,
|
|
136
|
+
net::HttpError::ConnectionTerminated => ErrorCode::ConnectionTerminated,
|
|
137
|
+
net::HttpError::ProtocolError => ErrorCode::HTTPProtocolError,
|
|
138
|
+
net::HttpError::ConnectionLimitReached => ErrorCode::ConnectionLimitReached,
|
|
139
|
+
net::HttpError::TlsCertificate => ErrorCode::TLSCertificateError,
|
|
140
|
+
net::HttpError::TlsAlertReceived(alert_id) => {
|
|
141
|
+
ErrorCode::TLSAlertReceived(TLSAlertReceivedPayload {
|
|
142
|
+
alert_id: Some(alert_id),
|
|
143
|
+
alert_message: None,
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
net::HttpError::TlsProtocol => ErrorCode::TLSProtocolError,
|
|
147
|
+
_ => ErrorCode::InternalError(None),
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
fn name_of(method: &Method) -> &str {
|
|
152
|
+
match method {
|
|
153
|
+
Method::Get => "GET",
|
|
154
|
+
Method::Head => "HEAD",
|
|
155
|
+
Method::Post => "POST",
|
|
156
|
+
Method::Put => "PUT",
|
|
157
|
+
Method::Delete => "DELETE",
|
|
158
|
+
Method::Connect => "CONNECT",
|
|
159
|
+
Method::Options => "OPTIONS",
|
|
160
|
+
Method::Trace => "TRACE",
|
|
161
|
+
Method::Patch => "PATCH",
|
|
162
|
+
Method::Other(other) => other,
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
impl Invocation {
|
|
167
|
+
fn request_mut(&mut self, request: OutgoingRequest) -> wasmtime::Result<&mut Request> {
|
|
168
|
+
self.table_mut()
|
|
169
|
+
.get_mut(request)
|
|
170
|
+
.map(|request| &mut request.0)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/// Change `request` through `change`; past `MAX_FIELD_SIZE` for its
|
|
174
|
+
/// parts and headers together the call traps, as a header list's append
|
|
175
|
+
/// does.
|
|
176
|
+
fn change_request(
|
|
177
|
+
&mut self,
|
|
178
|
+
request: OutgoingRequest,
|
|
179
|
+
change: impl FnOnce(&mut Request),
|
|
180
|
+
) -> wasmtime::Result<()> {
|
|
181
|
+
let request = self.request_mut(request)?;
|
|
182
|
+
change(request);
|
|
183
|
+
match request.room() > net::MAX_FIELD_SIZE {
|
|
184
|
+
true => Err(wasmtime::Error::msg(
|
|
185
|
+
"the guest's request outgrows the most one request's head may hold",
|
|
186
|
+
)),
|
|
187
|
+
false => Ok(()),
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/// Room for a header list or a request the host holds for the guest, which
|
|
192
|
+
/// takes an open file as a host-side producer does; none left traps,
|
|
193
|
+
/// since no constructor has an error for it.
|
|
194
|
+
fn reserve_held(&self) -> wasmtime::Result<table::FileRoom> {
|
|
195
|
+
self.wasi().table().reserve_producer().ok_or_else(|| {
|
|
196
|
+
wasmtime::Error::msg("the guest's open files leave no room for what the host holds")
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
fn future_mut(&mut self, future: FutureIncomingResponse) -> wasmtime::Result<&mut FutureState> {
|
|
201
|
+
self.table_mut().get_mut(future).map(|future| &mut future.0)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/// Hand `request` to the namespace, which judges it here on the guest
|
|
205
|
+
/// thread before it is sent. Its body streams as the guest writes it; one
|
|
206
|
+
/// the guest never asked for is empty.
|
|
207
|
+
fn send(&mut self, request: Request) -> FutureState {
|
|
208
|
+
if !request.link.handed.load(Ordering::Acquire) {
|
|
209
|
+
request.link.body.finish();
|
|
210
|
+
}
|
|
211
|
+
let method = name_of(&request.method).to_string();
|
|
212
|
+
let scheme = match request.scheme.as_ref() {
|
|
213
|
+
Some(Scheme::HTTP) => net::Scheme::Http,
|
|
214
|
+
Some(Scheme::HTTPS) | None => net::Scheme::Https,
|
|
215
|
+
Some(Scheme::Other(other)) => match other.as_str() {
|
|
216
|
+
"http" => net::Scheme::Http,
|
|
217
|
+
"https" => net::Scheme::Https,
|
|
218
|
+
_ => return FutureState::Refused(ErrorCode::HTTPProtocolError),
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
let Some(authority) = request.authority else {
|
|
222
|
+
return FutureState::Refused(ErrorCode::HTTPRequestURIInvalid);
|
|
223
|
+
};
|
|
224
|
+
let mut outgoing = net::OutgoingRequest::new(method, scheme, authority);
|
|
225
|
+
if let Some(path) = request.path_with_query {
|
|
226
|
+
outgoing.path_with_query = path;
|
|
227
|
+
}
|
|
228
|
+
outgoing.headers = request.headers.entries();
|
|
229
|
+
outgoing.body = request.link.body.clone();
|
|
230
|
+
outgoing.timeouts = request.timeouts;
|
|
231
|
+
let cred = self.wasi().cred();
|
|
232
|
+
match net::send(self.network(), cred, outgoing) {
|
|
233
|
+
Ok(exchange) => {
|
|
234
|
+
let file = Arc::new(Mutex::new(exchange.alloc_response_file(cred)));
|
|
235
|
+
FutureState::Sent(exchange, file)
|
|
236
|
+
}
|
|
237
|
+
Err(err) => FutureState::Refused(to_error_code(err)),
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
impl HttpHost for Invocation {
|
|
243
|
+
fn types_convert_error_code(&mut self, err: error::HttpError) -> wasmtime::Result<ErrorCode> {
|
|
244
|
+
err.downcast()
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
fn types_convert_header_error(
|
|
248
|
+
&mut self,
|
|
249
|
+
err: error::HeaderError,
|
|
250
|
+
) -> wasmtime::Result<HeaderError> {
|
|
251
|
+
err.downcast()
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
fn types_fields_new(&mut self) -> wasmtime::Result<Fields> {
|
|
255
|
+
let room = self.reserve_held()?;
|
|
256
|
+
Ok(self.table_mut().install(
|
|
257
|
+
room,
|
|
258
|
+
resource::Fields {
|
|
259
|
+
entries: net::Fields::new(),
|
|
260
|
+
mutable: true,
|
|
261
|
+
},
|
|
262
|
+
))
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/// A header appended, or the `header-error` refusing it; a list past
|
|
266
|
+
/// `MAX_FIELD_SIZE` traps, as wasmtime's field limit does.
|
|
267
|
+
fn types_fields_append(
|
|
268
|
+
&mut self,
|
|
269
|
+
this: Fields,
|
|
270
|
+
name: String,
|
|
271
|
+
value: Vec<u8>,
|
|
272
|
+
) -> Result<(), error::HeaderError> {
|
|
273
|
+
let fields = self.table_mut().get(this)?;
|
|
274
|
+
if !fields.mutable {
|
|
275
|
+
// The header is read before the list is found immutable, as
|
|
276
|
+
// wasmtime parses one before it asks whether it may change.
|
|
277
|
+
let read = net::to_field_name(&name).and_then(|_| net::to_field_value(value));
|
|
278
|
+
return Err(read
|
|
279
|
+
.map_or_else(to_header_error, |_| HeaderError::Immutable)
|
|
280
|
+
.into());
|
|
281
|
+
}
|
|
282
|
+
match fields.entries.append(&name, value) {
|
|
283
|
+
Ok(()) => Ok(()),
|
|
284
|
+
Err(FieldError::TooLarge) => Err(wasmtime::Error::msg(
|
|
285
|
+
"the guest's headers outgrow the most one list may hold",
|
|
286
|
+
)
|
|
287
|
+
.into()),
|
|
288
|
+
Err(err) => Err(to_header_error(err).into()),
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
fn types_fields_entries(&mut self, this: Fields) -> wasmtime::Result<Vec<(String, Vec<u8>)>> {
|
|
293
|
+
Ok(self.table_mut().get(this)?.entries.entries())
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
fn types_fields_drop(&mut self, handle: Fields) -> wasmtime::Result<()> {
|
|
297
|
+
self.table_mut().delete(handle).map(drop)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/// A GET with `headers`, which it takes, and a body the guest may ask for.
|
|
301
|
+
fn types_outgoing_request_new(
|
|
302
|
+
&mut self,
|
|
303
|
+
headers: resource::Fields,
|
|
304
|
+
) -> wasmtime::Result<OutgoingRequest> {
|
|
305
|
+
// The list's own place went with its handle, so the request takes it.
|
|
306
|
+
let room = self.reserve_held()?;
|
|
307
|
+
let link = Arc::new(BodyLink {
|
|
308
|
+
body: RequestBody::new(),
|
|
309
|
+
handed: AtomicBool::new(false),
|
|
310
|
+
});
|
|
311
|
+
let request = Request {
|
|
312
|
+
method: Method::Get,
|
|
313
|
+
scheme: None,
|
|
314
|
+
authority: None,
|
|
315
|
+
path_with_query: None,
|
|
316
|
+
headers: headers.entries,
|
|
317
|
+
link,
|
|
318
|
+
timeouts: net::Timeouts::default(),
|
|
319
|
+
};
|
|
320
|
+
Ok(self
|
|
321
|
+
.table_mut()
|
|
322
|
+
.install(room, resource::OutgoingRequest(request)))
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/// The body, handed out once.
|
|
326
|
+
fn types_outgoing_request_body(
|
|
327
|
+
&mut self,
|
|
328
|
+
this: OutgoingRequest,
|
|
329
|
+
) -> wasmtime::Result<Result<OutgoingBody, ()>> {
|
|
330
|
+
let link = self.request_mut(this)?.link.clone();
|
|
331
|
+
if link.handed.swap(true, Ordering::AcqRel) {
|
|
332
|
+
return Ok(Err(()));
|
|
333
|
+
}
|
|
334
|
+
self.table_mut()
|
|
335
|
+
.push(resource::OutgoingBody {
|
|
336
|
+
link,
|
|
337
|
+
stream_handed: false,
|
|
338
|
+
})
|
|
339
|
+
.map(Ok)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
fn types_outgoing_request_set_method(
|
|
343
|
+
&mut self,
|
|
344
|
+
this: OutgoingRequest,
|
|
345
|
+
method: Method,
|
|
346
|
+
) -> wasmtime::Result<Result<(), ()>> {
|
|
347
|
+
if !net::is_token(name_of(&method)) {
|
|
348
|
+
return Ok(Err(()));
|
|
349
|
+
}
|
|
350
|
+
self.change_request(this, |request| request.method = method)?;
|
|
351
|
+
Ok(Ok(()))
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
fn types_outgoing_request_set_path_with_query(
|
|
355
|
+
&mut self,
|
|
356
|
+
this: OutgoingRequest,
|
|
357
|
+
path_with_query: Option<String>,
|
|
358
|
+
) -> wasmtime::Result<Result<(), ()>> {
|
|
359
|
+
let target = match path_with_query.as_deref().map(net::to_origin_form) {
|
|
360
|
+
Some(None) => return Ok(Err(())),
|
|
361
|
+
target => target.flatten(),
|
|
362
|
+
};
|
|
363
|
+
self.change_request(this, |request| request.path_with_query = target)?;
|
|
364
|
+
Ok(Ok(()))
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/// Any scheme hyper parses, as wasmtime's; one that is neither HTTP nor
|
|
368
|
+
/// HTTPS answers `HTTP-protocol-error` once the request is sent.
|
|
369
|
+
fn types_outgoing_request_set_scheme(
|
|
370
|
+
&mut self,
|
|
371
|
+
this: OutgoingRequest,
|
|
372
|
+
scheme: Option<Scheme>,
|
|
373
|
+
) -> wasmtime::Result<Result<(), ()>> {
|
|
374
|
+
if let Some(Scheme::Other(other)) = &scheme {
|
|
375
|
+
if !net::is_scheme(other) {
|
|
376
|
+
return Ok(Err(()));
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
self.request_mut(this)?.scheme = scheme;
|
|
380
|
+
Ok(Ok(()))
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
fn types_outgoing_request_set_authority(
|
|
384
|
+
&mut self,
|
|
385
|
+
this: OutgoingRequest,
|
|
386
|
+
authority: Option<String>,
|
|
387
|
+
) -> wasmtime::Result<Result<(), ()>> {
|
|
388
|
+
if authority
|
|
389
|
+
.as_deref()
|
|
390
|
+
.is_some_and(|authority| !net::is_authority(authority))
|
|
391
|
+
{
|
|
392
|
+
return Ok(Err(()));
|
|
393
|
+
}
|
|
394
|
+
self.change_request(this, |request| request.authority = authority)?;
|
|
395
|
+
Ok(Ok(()))
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
fn types_outgoing_request_drop(&mut self, handle: OutgoingRequest) -> wasmtime::Result<()> {
|
|
399
|
+
self.table_mut().delete(handle).map(drop)
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/// The stream writing the body, handed out once.
|
|
403
|
+
fn types_outgoing_body_write(
|
|
404
|
+
&mut self,
|
|
405
|
+
this: OutgoingBody,
|
|
406
|
+
) -> wasmtime::Result<Result<OutputStream, ()>> {
|
|
407
|
+
let body = self.table_mut().get_mut(this)?;
|
|
408
|
+
if std::mem::replace(&mut body.stream_handed, true) {
|
|
409
|
+
return Ok(Err(()));
|
|
410
|
+
}
|
|
411
|
+
let link = body.link.clone();
|
|
412
|
+
let file = link.body.alloc_file(self.wasi().cred());
|
|
413
|
+
let stream = Stream::RequestBody(link.body.clone(), Arc::new(Mutex::new(file)));
|
|
414
|
+
self.table_mut()
|
|
415
|
+
.push_child(io::OutputStream::new(stream), this)
|
|
416
|
+
.map(Ok)
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/// End the body, so its request ends once the seam sends what is left.
|
|
420
|
+
/// Trailers holding a field answer `HTTP-request-denied` and fail the
|
|
421
|
+
/// request: an egress proxy sends none by default, as Envoy drops HTTP/1
|
|
422
|
+
/// trailers, and the request policies never see them, so dropping them
|
|
423
|
+
/// would succeed differently.
|
|
424
|
+
fn types_outgoing_body_finish(
|
|
425
|
+
&mut self,
|
|
426
|
+
this: resource::OutgoingBody,
|
|
427
|
+
trailers: Option<resource::Fields>,
|
|
428
|
+
) -> Result<(), error::HttpError> {
|
|
429
|
+
if trailers.is_some_and(|trailers| !trailers.entries.entries().is_empty()) {
|
|
430
|
+
this.link.body.fail(net::HttpError::ProtocolError);
|
|
431
|
+
return Err(ErrorCode::HTTPRequestDenied.into());
|
|
432
|
+
}
|
|
433
|
+
this.link.body.finish();
|
|
434
|
+
Ok(())
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/// A body dropped unfinished fails its request, as wasmtime-wasi-http aborts
|
|
438
|
+
/// it with an HTTP protocol error.
|
|
439
|
+
fn types_outgoing_body_drop(&mut self, handle: OutgoingBody) -> wasmtime::Result<()> {
|
|
440
|
+
let link = self.table_mut().delete(handle)?.link;
|
|
441
|
+
link.body.fail(net::HttpError::ProtocolError);
|
|
442
|
+
Ok(())
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/// Ready once the response's head arrives; one already answered is ready
|
|
446
|
+
/// at once.
|
|
447
|
+
fn types_future_incoming_response_subscribe(
|
|
448
|
+
&mut self,
|
|
449
|
+
this: FutureIncomingResponse,
|
|
450
|
+
) -> wasmtime::Result<Pollable> {
|
|
451
|
+
let pollable = match self.future_mut(this)? {
|
|
452
|
+
FutureState::Sent(exchange, file) => {
|
|
453
|
+
io::Pollable::Exchange(file.clone(), exchange.clone())
|
|
454
|
+
}
|
|
455
|
+
_ => io::Pollable::Ready,
|
|
456
|
+
};
|
|
457
|
+
self.table_mut().push_child(pollable, this)
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/// The response once its head arrives, handed out once.
|
|
461
|
+
#[allow(clippy::type_complexity)]
|
|
462
|
+
fn types_future_incoming_response_get(
|
|
463
|
+
&mut self,
|
|
464
|
+
this: FutureIncomingResponse,
|
|
465
|
+
) -> wasmtime::Result<Option<Result<Result<IncomingResponse, ErrorCode>, ()>>> {
|
|
466
|
+
let state = self.future_mut(this)?;
|
|
467
|
+
let answer = match state {
|
|
468
|
+
FutureState::Taken => return Ok(Some(Err(()))),
|
|
469
|
+
FutureState::Refused(code) => Err(code.clone()),
|
|
470
|
+
FutureState::Sent(exchange, _) => match exchange.response() {
|
|
471
|
+
None => return Ok(None),
|
|
472
|
+
Some(Ok(head)) => Ok((head.status, exchange.clone())),
|
|
473
|
+
Some(Err(err)) => Err(to_error_code(err)),
|
|
474
|
+
},
|
|
475
|
+
};
|
|
476
|
+
*state = FutureState::Taken;
|
|
477
|
+
match answer {
|
|
478
|
+
Ok((status, exchange)) => self
|
|
479
|
+
.table_mut()
|
|
480
|
+
.push(resource::IncomingResponse {
|
|
481
|
+
status,
|
|
482
|
+
exchange,
|
|
483
|
+
consumed: false,
|
|
484
|
+
})
|
|
485
|
+
.map(|response| Some(Ok(Ok(response)))),
|
|
486
|
+
Err(code) => Ok(Some(Ok(Err(code)))),
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
fn types_future_incoming_response_drop(
|
|
491
|
+
&mut self,
|
|
492
|
+
handle: FutureIncomingResponse,
|
|
493
|
+
) -> wasmtime::Result<()> {
|
|
494
|
+
// A sent future holds its exchange's last reference beside its own
|
|
495
|
+
// child pollable, so dropping it lets the exchange go.
|
|
496
|
+
self.table_mut().delete(handle).map(drop)
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
fn types_incoming_response_status(&mut self, this: IncomingResponse) -> wasmtime::Result<u16> {
|
|
500
|
+
Ok(self.table_mut().get(this)?.status)
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/// The response's headers, which the guest cannot change; a copy that
|
|
504
|
+
/// outlives the response, as wasmtime's.
|
|
505
|
+
fn types_incoming_response_headers(
|
|
506
|
+
&mut self,
|
|
507
|
+
this: IncomingResponse,
|
|
508
|
+
) -> wasmtime::Result<Fields> {
|
|
509
|
+
let headers = match self.table_mut().get(this)?.exchange.response() {
|
|
510
|
+
Some(Ok(head)) => head.headers,
|
|
511
|
+
_ => unreachable!("a response is handed out only once its head arrived"),
|
|
512
|
+
};
|
|
513
|
+
let room = self.reserve_held()?;
|
|
514
|
+
Ok(self.table_mut().install(
|
|
515
|
+
room,
|
|
516
|
+
resource::Fields {
|
|
517
|
+
entries: net::Fields::with_entries(headers),
|
|
518
|
+
mutable: false,
|
|
519
|
+
},
|
|
520
|
+
))
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/// The body, handed out once.
|
|
524
|
+
fn types_incoming_response_consume(
|
|
525
|
+
&mut self,
|
|
526
|
+
this: IncomingResponse,
|
|
527
|
+
) -> wasmtime::Result<Result<IncomingBody, ()>> {
|
|
528
|
+
let response = self.table_mut().get_mut(this)?;
|
|
529
|
+
if std::mem::replace(&mut response.consumed, true) {
|
|
530
|
+
return Ok(Err(()));
|
|
531
|
+
}
|
|
532
|
+
let exchange = response.exchange.clone();
|
|
533
|
+
self.table_mut()
|
|
534
|
+
.push(resource::IncomingBody {
|
|
535
|
+
exchange,
|
|
536
|
+
stream_handed: false,
|
|
537
|
+
})
|
|
538
|
+
.map(Ok)
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/// A response dropped with its body never asked for lets the exchange go.
|
|
542
|
+
fn types_incoming_response_drop(&mut self, handle: IncomingResponse) -> wasmtime::Result<()> {
|
|
543
|
+
let response = self.table_mut().delete(handle)?;
|
|
544
|
+
if !response.consumed {
|
|
545
|
+
response.exchange.release();
|
|
546
|
+
}
|
|
547
|
+
Ok(())
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/// The stream reading the body, handed out once.
|
|
551
|
+
fn types_incoming_body_stream(
|
|
552
|
+
&mut self,
|
|
553
|
+
this: IncomingBody,
|
|
554
|
+
) -> wasmtime::Result<Result<InputStream, ()>> {
|
|
555
|
+
let body = self.table_mut().get_mut(this)?;
|
|
556
|
+
if std::mem::replace(&mut body.stream_handed, true) {
|
|
557
|
+
return Ok(Err(()));
|
|
558
|
+
}
|
|
559
|
+
let exchange = body.exchange.clone();
|
|
560
|
+
let file = Arc::new(Mutex::new(exchange.alloc_body_file(self.wasi().cred())));
|
|
561
|
+
self.table_mut()
|
|
562
|
+
.push_child(
|
|
563
|
+
io::InputStream::for_body(Stream::File(file), exchange),
|
|
564
|
+
this,
|
|
565
|
+
)
|
|
566
|
+
.map(Ok)
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/// The trailers, which the outbound seam never reads, so none arrive.
|
|
570
|
+
fn types_incoming_body_finish(
|
|
571
|
+
&mut self,
|
|
572
|
+
this: resource::IncomingBody,
|
|
573
|
+
) -> wasmtime::Result<FutureTrailers> {
|
|
574
|
+
this.exchange.release();
|
|
575
|
+
self.table_mut().push(resource::FutureTrailers)
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
fn types_incoming_body_drop(&mut self, handle: IncomingBody) -> wasmtime::Result<()> {
|
|
579
|
+
self.table_mut().delete(handle).map(drop)
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
fn types_future_trailers_drop(&mut self, handle: FutureTrailers) -> wasmtime::Result<()> {
|
|
583
|
+
self.table_mut().delete(handle).map(drop)
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/// Options waiting on at every step until one is set.
|
|
587
|
+
fn types_request_options_new(&mut self) -> wasmtime::Result<RequestOptions> {
|
|
588
|
+
self.table_mut().push(resource::RequestOptions::default())
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
fn types_request_options_connect_timeout(
|
|
592
|
+
&mut self,
|
|
593
|
+
this: RequestOptions,
|
|
594
|
+
) -> wasmtime::Result<Option<u64>> {
|
|
595
|
+
Ok(self.table_mut().get(this)?.0.connect.map(to_nanos))
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/// Bound the connect, as wasmtime-wasi-http bounds it, so a request
|
|
599
|
+
/// whose connect outlasts it answers `connection-timeout`.
|
|
600
|
+
fn types_request_options_set_connect_timeout(
|
|
601
|
+
&mut self,
|
|
602
|
+
this: RequestOptions,
|
|
603
|
+
duration: Option<u64>,
|
|
604
|
+
) -> wasmtime::Result<Result<(), ()>> {
|
|
605
|
+
self.table_mut().get_mut(this)?.0.connect = duration.map(Duration::from_nanos);
|
|
606
|
+
Ok(Ok(()))
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
fn types_request_options_first_byte_timeout(
|
|
610
|
+
&mut self,
|
|
611
|
+
this: RequestOptions,
|
|
612
|
+
) -> wasmtime::Result<Option<u64>> {
|
|
613
|
+
Ok(self.table_mut().get(this)?.0.first_byte.map(to_nanos))
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/// Bound the wait for the response's head once connected, answered as
|
|
617
|
+
/// `connection-read-timeout`.
|
|
618
|
+
fn types_request_options_set_first_byte_timeout(
|
|
619
|
+
&mut self,
|
|
620
|
+
this: RequestOptions,
|
|
621
|
+
duration: Option<u64>,
|
|
622
|
+
) -> wasmtime::Result<Result<(), ()>> {
|
|
623
|
+
self.table_mut().get_mut(this)?.0.first_byte = duration.map(Duration::from_nanos);
|
|
624
|
+
Ok(Ok(()))
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
fn types_request_options_between_bytes_timeout(
|
|
628
|
+
&mut self,
|
|
629
|
+
this: RequestOptions,
|
|
630
|
+
) -> wasmtime::Result<Option<u64>> {
|
|
631
|
+
Ok(self.table_mut().get(this)?.0.between_bytes.map(to_nanos))
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/// Bound each wait for the body's next part, failing the read that
|
|
635
|
+
/// outlasts it.
|
|
636
|
+
fn types_request_options_set_between_bytes_timeout(
|
|
637
|
+
&mut self,
|
|
638
|
+
this: RequestOptions,
|
|
639
|
+
duration: Option<u64>,
|
|
640
|
+
) -> wasmtime::Result<Result<(), ()>> {
|
|
641
|
+
self.table_mut().get_mut(this)?.0.between_bytes = duration.map(Duration::from_nanos);
|
|
642
|
+
Ok(Ok(()))
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
fn types_request_options_drop(&mut self, handle: RequestOptions) -> wasmtime::Result<()> {
|
|
646
|
+
self.table_mut().delete(handle).map(drop)
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/// Send `request`, which it takes. Without a network Grant it answers
|
|
650
|
+
/// `HTTP-request-denied` at once, doing nothing first. The request leaves
|
|
651
|
+
/// at once, bound by the timeouts `options` sets, and its body streams
|
|
652
|
+
/// after it as the guest writes it, as wasi-http's backpressure asks.
|
|
653
|
+
fn outgoing_handler_handle(
|
|
654
|
+
&mut self,
|
|
655
|
+
request: resource::OutgoingRequest,
|
|
656
|
+
options: Option<resource::RequestOptions>,
|
|
657
|
+
) -> Result<FutureIncomingResponse, error::HttpError> {
|
|
658
|
+
let mut request = request.0;
|
|
659
|
+
request.timeouts = options.unwrap_or_default().0;
|
|
660
|
+
if self.wasi().network().is_none() {
|
|
661
|
+
return Err(ErrorCode::HTTPRequestDenied.into());
|
|
662
|
+
}
|
|
663
|
+
let Some(room) = self.wasi().table().reserve_producer() else {
|
|
664
|
+
return Err(ErrorCode::ConnectionLimitReached.into());
|
|
665
|
+
};
|
|
666
|
+
let future = self.send(request);
|
|
667
|
+
if let FutureState::Refused(code) = future {
|
|
668
|
+
return Err(code.into());
|
|
669
|
+
}
|
|
670
|
+
Ok(self
|
|
671
|
+
.table_mut()
|
|
672
|
+
.install(room, resource::FutureIncomingResponse(future)))
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
#[cfg(test)]
|
|
677
|
+
mod tests {
|
|
678
|
+
use std::io::{BufRead, BufReader, Write};
|
|
679
|
+
use std::net::TcpListener;
|
|
680
|
+
use std::time::Instant;
|
|
681
|
+
|
|
682
|
+
use kobako_runtime::network;
|
|
683
|
+
use kobako_runtime::sched::run_task;
|
|
684
|
+
|
|
685
|
+
use super::super::IntoAnswer;
|
|
686
|
+
use super::*;
|
|
687
|
+
use crate::preview2::bindings::{ClocksHost, Host as _, IoHost};
|
|
688
|
+
use crate::waits::MAX_WAIT;
|
|
689
|
+
|
|
690
|
+
// @behavior NW-225
|
|
691
|
+
#[test]
|
|
692
|
+
fn a_lookup_that_failed_answers_dns_error_as_wasmtime_wasi_http_does() {
|
|
693
|
+
assert!(
|
|
694
|
+
matches!(
|
|
695
|
+
to_error_code(net::HttpError::DnsError),
|
|
696
|
+
ErrorCode::DNSError(DNSErrorPayload { rcode: Some(rcode), info_code: None })
|
|
697
|
+
if rcode == "address not available"
|
|
698
|
+
),
|
|
699
|
+
"a request whose name has no address must answer DNS-error with the rcode \
|
|
700
|
+
wasmtime-wasi-http gives a failed lookup"
|
|
701
|
+
);
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
// @behavior NW-225
|
|
705
|
+
#[test]
|
|
706
|
+
fn a_tls_alert_answers_tls_alert_received_with_its_id() {
|
|
707
|
+
assert!(
|
|
708
|
+
matches!(
|
|
709
|
+
to_error_code(net::HttpError::TlsAlertReceived(40)),
|
|
710
|
+
ErrorCode::TLSAlertReceived(TLSAlertReceivedPayload {
|
|
711
|
+
alert_id: Some(40),
|
|
712
|
+
alert_message: None
|
|
713
|
+
})
|
|
714
|
+
),
|
|
715
|
+
"a request whose server sent the handshake_failure alert must answer \
|
|
716
|
+
TLS-alert-received carrying alert id 40 and no message, as wasmtime-wasi-http does"
|
|
717
|
+
);
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/// A hermetic invocation whose network holds `endpoints`, or none at all.
|
|
721
|
+
fn guest(endpoints: Option<Vec<network::Endpoint>>) -> Invocation {
|
|
722
|
+
limited_guest(endpoints, None)
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/// A guest as `guest` makes one, allotted `memory_limit` bytes.
|
|
726
|
+
fn limited_guest(
|
|
727
|
+
endpoints: Option<Vec<network::Endpoint>>,
|
|
728
|
+
memory_limit: Option<usize>,
|
|
729
|
+
) -> Invocation {
|
|
730
|
+
Invocation::hermetic(endpoints, |config| {
|
|
731
|
+
config.limits.memory_limit = memory_limit
|
|
732
|
+
})
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/// A GET for `/greeting` at `authority` over HTTP.
|
|
736
|
+
fn greeting_request(
|
|
737
|
+
guest: &mut Invocation,
|
|
738
|
+
authority: &str,
|
|
739
|
+
) -> wasmtime::Result<OutgoingRequest> {
|
|
740
|
+
let headers = guest.types_fields_new()?;
|
|
741
|
+
let headers = guest.take_handle(headers)?;
|
|
742
|
+
let request = guest.types_outgoing_request_new(headers)?;
|
|
743
|
+
guest
|
|
744
|
+
.types_outgoing_request_set_scheme(request, Some(Scheme::HTTP))?
|
|
745
|
+
.expect("http is a scheme");
|
|
746
|
+
guest
|
|
747
|
+
.types_outgoing_request_set_authority(request, Some(authority.to_string()))?
|
|
748
|
+
.expect("an authority");
|
|
749
|
+
guest
|
|
750
|
+
.types_outgoing_request_set_path_with_query(request, Some("/greeting".to_string()))?
|
|
751
|
+
.expect("a path");
|
|
752
|
+
Ok(request)
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/// A guest holding the header list `incoming-response.headers` hands out
|
|
756
|
+
/// for a head a loopback server answers.
|
|
757
|
+
fn guest_with_response_headers() -> (Invocation, Fields) {
|
|
758
|
+
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
759
|
+
let port = listener.local_addr().unwrap().port();
|
|
760
|
+
let server = std::thread::spawn(move || {
|
|
761
|
+
let (stream, _) = listener.accept().unwrap();
|
|
762
|
+
let mut reader = BufReader::new(stream.try_clone().unwrap());
|
|
763
|
+
let mut line = String::new();
|
|
764
|
+
while reader.read_line(&mut line).unwrap() > 2 {
|
|
765
|
+
line.clear();
|
|
766
|
+
}
|
|
767
|
+
(&stream)
|
|
768
|
+
.write_all(b"HTTP/1.1 200 OK\r\ncontent-length: 0\r\nx-kind: test\r\n\r\n")
|
|
769
|
+
.unwrap();
|
|
770
|
+
});
|
|
771
|
+
let endpoint = network::Endpoint::new("127.0.0.1", port).unwrap();
|
|
772
|
+
let mut guest = guest(Some(vec![endpoint]));
|
|
773
|
+
let started = Instant::now();
|
|
774
|
+
let headers = run_task(Some(started + MAX_WAIT), || {
|
|
775
|
+
let request = greeting_request(&mut guest, &format!("127.0.0.1:{port}"))?;
|
|
776
|
+
let request = guest.take_handle(request)?;
|
|
777
|
+
let future = guest
|
|
778
|
+
.outgoing_handler_handle(request, None)
|
|
779
|
+
.into_answer()?
|
|
780
|
+
.expect("an endpoint names the authority");
|
|
781
|
+
let ready = guest.types_future_incoming_response_subscribe(future)?;
|
|
782
|
+
guest.poll_pollable_block(ready)?;
|
|
783
|
+
let response = guest
|
|
784
|
+
.types_future_incoming_response_get(future)?
|
|
785
|
+
.expect("the head arrived")
|
|
786
|
+
.expect("handed out once")
|
|
787
|
+
.expect("the server answered");
|
|
788
|
+
guest.types_incoming_response_headers(response)
|
|
789
|
+
})
|
|
790
|
+
.expect("a loopback server's head answers; if not, look at the request before the headers");
|
|
791
|
+
server.join().unwrap();
|
|
792
|
+
(guest, headers)
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
// @behavior NW-039
|
|
796
|
+
#[test]
|
|
797
|
+
fn without_the_network_a_request_answers_http_request_denied() {
|
|
798
|
+
let mut guest = guest(None);
|
|
799
|
+
let request = greeting_request(&mut guest, "example.com").unwrap();
|
|
800
|
+
let request = guest.take_handle(request).unwrap();
|
|
801
|
+
assert!(
|
|
802
|
+
matches!(
|
|
803
|
+
guest.outgoing_handler_handle(request, None).into_answer(),
|
|
804
|
+
Ok(Err(ErrorCode::HTTPRequestDenied))
|
|
805
|
+
),
|
|
806
|
+
"outgoing-handler.handle with no network entry must answer HTTP-request-denied"
|
|
807
|
+
);
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// @behavior NW-206
|
|
811
|
+
#[test]
|
|
812
|
+
fn request_options_read_back_their_timeouts_and_bound_the_wait_for_the_head() {
|
|
813
|
+
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
814
|
+
let port = listener.local_addr().unwrap().port();
|
|
815
|
+
let server = std::thread::spawn(move || {
|
|
816
|
+
let (stream, _) = listener.accept().unwrap();
|
|
817
|
+
let mut line = String::new();
|
|
818
|
+
// Read the request, then hold the connection with no answer
|
|
819
|
+
// until the client lets it go.
|
|
820
|
+
let mut reader = BufReader::new(stream);
|
|
821
|
+
while reader.read_line(&mut line).unwrap_or(0) > 0 {}
|
|
822
|
+
});
|
|
823
|
+
let endpoint = network::Endpoint::new("127.0.0.1", port).unwrap();
|
|
824
|
+
let mut guest = guest(Some(vec![endpoint]));
|
|
825
|
+
let started = Instant::now();
|
|
826
|
+
let (read_back, answer) = run_task(Some(started + MAX_WAIT), || {
|
|
827
|
+
let options = guest.types_request_options_new()?;
|
|
828
|
+
let unset = [
|
|
829
|
+
guest.types_request_options_connect_timeout(options)?,
|
|
830
|
+
guest.types_request_options_first_byte_timeout(options)?,
|
|
831
|
+
guest.types_request_options_between_bytes_timeout(options)?,
|
|
832
|
+
];
|
|
833
|
+
let set = [
|
|
834
|
+
guest.types_request_options_set_connect_timeout(options, Some(7))?,
|
|
835
|
+
guest.types_request_options_set_first_byte_timeout(options, Some(0))?,
|
|
836
|
+
guest.types_request_options_set_between_bytes_timeout(options, Some(9))?,
|
|
837
|
+
];
|
|
838
|
+
let read_back = (
|
|
839
|
+
unset,
|
|
840
|
+
set,
|
|
841
|
+
[
|
|
842
|
+
guest.types_request_options_connect_timeout(options)?,
|
|
843
|
+
guest.types_request_options_first_byte_timeout(options)?,
|
|
844
|
+
guest.types_request_options_between_bytes_timeout(options)?,
|
|
845
|
+
],
|
|
846
|
+
);
|
|
847
|
+
guest
|
|
848
|
+
.types_request_options_set_connect_timeout(options, None)?
|
|
849
|
+
.unwrap();
|
|
850
|
+
let options = guest.take_handle(options)?;
|
|
851
|
+
let request = greeting_request(&mut guest, &format!("127.0.0.1:{port}"))?;
|
|
852
|
+
let request = guest.take_handle(request)?;
|
|
853
|
+
let future = guest
|
|
854
|
+
.outgoing_handler_handle(request, Some(options))
|
|
855
|
+
.into_answer()?
|
|
856
|
+
.expect("an endpoint names the authority");
|
|
857
|
+
let ready = guest.types_future_incoming_response_subscribe(future)?;
|
|
858
|
+
guest.poll_pollable_block(ready)?;
|
|
859
|
+
let answer = guest
|
|
860
|
+
.types_future_incoming_response_get(future)?
|
|
861
|
+
.expect("the wait ended with an answer")
|
|
862
|
+
.expect("the answer is handed out once")
|
|
863
|
+
.err();
|
|
864
|
+
guest.poll_pollable_drop(ready)?;
|
|
865
|
+
guest.types_future_incoming_response_drop(future)?;
|
|
866
|
+
Ok::<_, wasmtime::Error>((read_back, answer))
|
|
867
|
+
})
|
|
868
|
+
.expect(
|
|
869
|
+
"a request handled with a first-byte timeout must wake its future's pollable once \
|
|
870
|
+
the timeout passes, not at the run's deadline",
|
|
871
|
+
);
|
|
872
|
+
server.join().unwrap();
|
|
873
|
+
assert_eq!(
|
|
874
|
+
(read_back, answer),
|
|
875
|
+
(
|
|
876
|
+
(
|
|
877
|
+
[None, None, None],
|
|
878
|
+
[Ok(()), Ok(()), Ok(())],
|
|
879
|
+
[Some(7), Some(0), Some(9)]
|
|
880
|
+
),
|
|
881
|
+
Some(ErrorCode::ConnectionReadTimeout)
|
|
882
|
+
),
|
|
883
|
+
"request-options must read back each of its three timeouts as set, and none of them \
|
|
884
|
+
until set, and a request handled with a first-byte timeout must answer \
|
|
885
|
+
connection-read-timeout"
|
|
886
|
+
);
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
// @behavior NW-040
|
|
890
|
+
// @behavior NW-165
|
|
891
|
+
#[test]
|
|
892
|
+
fn a_request_through_wasi_http_reads_back_the_response() {
|
|
893
|
+
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
894
|
+
let port = listener.local_addr().unwrap().port();
|
|
895
|
+
let server = std::thread::spawn(move || {
|
|
896
|
+
let (stream, _) = listener.accept().unwrap();
|
|
897
|
+
let mut line = String::new();
|
|
898
|
+
BufReader::new(stream.try_clone().unwrap())
|
|
899
|
+
.read_line(&mut line)
|
|
900
|
+
.unwrap();
|
|
901
|
+
(&stream)
|
|
902
|
+
.write_all(
|
|
903
|
+
b"HTTP/1.1 201 Created\r\ncontent-length: 5\r\nX-Kind: test\r\n\r\nhello",
|
|
904
|
+
)
|
|
905
|
+
.unwrap();
|
|
906
|
+
line
|
|
907
|
+
});
|
|
908
|
+
let endpoint = network::Endpoint::new("127.0.0.1", port).unwrap();
|
|
909
|
+
let mut guest = guest(Some(vec![endpoint]));
|
|
910
|
+
let started = Instant::now();
|
|
911
|
+
let (status, kind, body, nothing) = run_task(Some(started + MAX_WAIT), || {
|
|
912
|
+
let request = greeting_request(&mut guest, &format!("127.0.0.1:{port}"))?;
|
|
913
|
+
let request = guest.take_handle(request)?;
|
|
914
|
+
let future = guest
|
|
915
|
+
.outgoing_handler_handle(request, None)
|
|
916
|
+
.into_answer()?
|
|
917
|
+
.expect("an endpoint names the authority");
|
|
918
|
+
let ready = guest.types_future_incoming_response_subscribe(future)?;
|
|
919
|
+
guest.poll_pollable_block(ready)?;
|
|
920
|
+
let response = guest
|
|
921
|
+
.types_future_incoming_response_get(future)?
|
|
922
|
+
.expect("the head arrived")
|
|
923
|
+
.expect("handed out once")
|
|
924
|
+
.expect("the server answered");
|
|
925
|
+
let status = guest.types_incoming_response_status(response)?;
|
|
926
|
+
let headers = guest.types_incoming_response_headers(response)?;
|
|
927
|
+
let kind = guest
|
|
928
|
+
.types_fields_entries(headers)?
|
|
929
|
+
.into_iter()
|
|
930
|
+
.filter(|(name, _)| name == "x-kind")
|
|
931
|
+
.map(|(_, value)| value)
|
|
932
|
+
.collect::<Vec<_>>();
|
|
933
|
+
let body = guest
|
|
934
|
+
.types_incoming_response_consume(response)?
|
|
935
|
+
.expect("consumed once");
|
|
936
|
+
let stream = guest.types_incoming_body_stream(body)?.expect("one stream");
|
|
937
|
+
let mut read = Vec::new();
|
|
938
|
+
while let Ok(bytes) = guest.streams_input_stream_read(stream, 64).into_answer()? {
|
|
939
|
+
if bytes.is_empty() {
|
|
940
|
+
let readable = guest.streams_input_stream_subscribe(stream)?;
|
|
941
|
+
guest.poll_pollable_block(readable)?;
|
|
942
|
+
guest.poll_pollable_drop(readable)?;
|
|
943
|
+
}
|
|
944
|
+
read.extend(bytes);
|
|
945
|
+
}
|
|
946
|
+
let nothing = matches!(
|
|
947
|
+
guest.streams_input_stream_read(stream, 0).into_answer()?,
|
|
948
|
+
Err(crate::preview2::error::StreamError::Closed)
|
|
949
|
+
);
|
|
950
|
+
Ok::<_, wasmtime::Error>((status, kind, read, nothing))
|
|
951
|
+
})
|
|
952
|
+
.expect("the request answers");
|
|
953
|
+
assert_eq!(
|
|
954
|
+
(status, kind, body, server.join().unwrap(), nothing),
|
|
955
|
+
(
|
|
956
|
+
201,
|
|
957
|
+
vec![b"test".to_vec()],
|
|
958
|
+
b"hello".to_vec(),
|
|
959
|
+
"GET /greeting HTTP/1.1\r\n".to_string(),
|
|
960
|
+
true
|
|
961
|
+
),
|
|
962
|
+
"a GET built through wasi:http to an http endpoint must reach the server and read \
|
|
963
|
+
back the status, a header the server sent as X-Kind under its lowercase name, and \
|
|
964
|
+
the body to its end; a read of nothing past the end must answer closed, as io.wit \
|
|
965
|
+
has it"
|
|
966
|
+
);
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
// @behavior NW-082
|
|
970
|
+
#[test]
|
|
971
|
+
fn an_exchange_the_guest_no_longer_holds_closes_its_connection() {
|
|
972
|
+
// Whether the server reads the connection closed within a few
|
|
973
|
+
// seconds once `leave` lets go of a response whose body stays open.
|
|
974
|
+
let closes = |leave: fn(&mut Option<Invocation>, IncomingBody)| {
|
|
975
|
+
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
976
|
+
let port = listener.local_addr().unwrap().port();
|
|
977
|
+
let server = std::thread::spawn(move || {
|
|
978
|
+
let (stream, _) = listener.accept().unwrap();
|
|
979
|
+
let mut reader = BufReader::new(stream.try_clone().unwrap());
|
|
980
|
+
let mut line = String::new();
|
|
981
|
+
while reader.read_line(&mut line).unwrap() > 2 {
|
|
982
|
+
line.clear();
|
|
983
|
+
}
|
|
984
|
+
(&stream)
|
|
985
|
+
.write_all(b"HTTP/1.1 200 OK\r\ncontent-length: 100\r\n\r\npart")
|
|
986
|
+
.unwrap();
|
|
987
|
+
stream.set_read_timeout(Some(MAX_WAIT)).unwrap();
|
|
988
|
+
let mut rest = [0; 1];
|
|
989
|
+
matches!(std::io::Read::read(&mut reader, &mut rest), Ok(0))
|
|
990
|
+
});
|
|
991
|
+
let endpoint = network::Endpoint::new("127.0.0.1", port).unwrap();
|
|
992
|
+
let mut guest = Some(guest(Some(vec![endpoint])));
|
|
993
|
+
let started = Instant::now();
|
|
994
|
+
let body = run_task(Some(started + MAX_WAIT), || {
|
|
995
|
+
let guest = guest.as_mut().unwrap();
|
|
996
|
+
let request = greeting_request(guest, &format!("127.0.0.1:{port}"))?;
|
|
997
|
+
let request = guest.take_handle(request)?;
|
|
998
|
+
let future = guest
|
|
999
|
+
.outgoing_handler_handle(request, None)
|
|
1000
|
+
.into_answer()?
|
|
1001
|
+
.expect("an endpoint names the authority");
|
|
1002
|
+
let ready = guest.types_future_incoming_response_subscribe(future)?;
|
|
1003
|
+
guest.poll_pollable_block(ready)?;
|
|
1004
|
+
guest.poll_pollable_drop(ready)?;
|
|
1005
|
+
let response = guest
|
|
1006
|
+
.types_future_incoming_response_get(future)?
|
|
1007
|
+
.expect("the head arrived")
|
|
1008
|
+
.expect("handed out once")
|
|
1009
|
+
.expect("the server answered");
|
|
1010
|
+
guest.types_future_incoming_response_drop(future)?;
|
|
1011
|
+
let body = guest
|
|
1012
|
+
.types_incoming_response_consume(response)?
|
|
1013
|
+
.expect("consumed once");
|
|
1014
|
+
guest.types_incoming_response_drop(response)?;
|
|
1015
|
+
Ok::<_, wasmtime::Error>(body)
|
|
1016
|
+
})
|
|
1017
|
+
.expect("the head answers");
|
|
1018
|
+
leave(&mut guest, body);
|
|
1019
|
+
server.join().unwrap()
|
|
1020
|
+
};
|
|
1021
|
+
let dropped = closes(|guest, body| {
|
|
1022
|
+
guest
|
|
1023
|
+
.as_mut()
|
|
1024
|
+
.unwrap()
|
|
1025
|
+
.types_incoming_body_drop(body)
|
|
1026
|
+
.unwrap();
|
|
1027
|
+
});
|
|
1028
|
+
let ended = closes(|guest, _| {
|
|
1029
|
+
guest.take();
|
|
1030
|
+
});
|
|
1031
|
+
assert_eq!(
|
|
1032
|
+
(dropped, ended),
|
|
1033
|
+
(true, true),
|
|
1034
|
+
"a server holding a response body open must read the connection closed once the \
|
|
1035
|
+
guest drops the body unfinished, and once the Invocation ends holding it"
|
|
1036
|
+
);
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
// @behavior NW-041
|
|
1040
|
+
#[test]
|
|
1041
|
+
fn a_forbidden_header_or_a_response_header_cannot_be_set() {
|
|
1042
|
+
let mut guest = guest(None);
|
|
1043
|
+
let fields = guest.types_fields_new().unwrap();
|
|
1044
|
+
let set = |guest: &mut Invocation, fields, name: &str| {
|
|
1045
|
+
guest
|
|
1046
|
+
.types_fields_append(fields, name.to_string(), b"x".to_vec())
|
|
1047
|
+
.into_answer()
|
|
1048
|
+
.unwrap()
|
|
1049
|
+
};
|
|
1050
|
+
let answers = [
|
|
1051
|
+
set(&mut guest, fields, "Host"),
|
|
1052
|
+
set(&mut guest, fields, "content-length"),
|
|
1053
|
+
set(&mut guest, fields, "bad name"),
|
|
1054
|
+
set(&mut guest, fields, "x-ok"),
|
|
1055
|
+
];
|
|
1056
|
+
let (mut held, response_headers) = guest_with_response_headers();
|
|
1057
|
+
let immutable = set(&mut held, response_headers, "x-ok");
|
|
1058
|
+
assert_eq!(
|
|
1059
|
+
(answers, immutable),
|
|
1060
|
+
(
|
|
1061
|
+
[
|
|
1062
|
+
Err(HeaderError::Forbidden),
|
|
1063
|
+
Err(HeaderError::Forbidden),
|
|
1064
|
+
Err(HeaderError::InvalidSyntax),
|
|
1065
|
+
Ok(())
|
|
1066
|
+
],
|
|
1067
|
+
Err(HeaderError::Immutable)
|
|
1068
|
+
),
|
|
1069
|
+
"fields.append must refuse a forbidden name, content-length, a name that is no \
|
|
1070
|
+
token, and any change to the list incoming-response.headers hands out"
|
|
1071
|
+
);
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
// @behavior NW-081
|
|
1075
|
+
#[test]
|
|
1076
|
+
fn a_header_list_past_its_room_traps() {
|
|
1077
|
+
let mut guest = limited_guest(None, Some(16 * 1024));
|
|
1078
|
+
let fields = guest.types_fields_new().unwrap();
|
|
1079
|
+
let mut taken = 0;
|
|
1080
|
+
let trap = loop {
|
|
1081
|
+
if let Err(trap) = guest
|
|
1082
|
+
.types_fields_append(fields, format!("x-{taken}"), vec![b'v'; 1024])
|
|
1083
|
+
.into_answer()
|
|
1084
|
+
{
|
|
1085
|
+
break trap.to_string();
|
|
1086
|
+
}
|
|
1087
|
+
taken += 1;
|
|
1088
|
+
};
|
|
1089
|
+
assert_eq!(
|
|
1090
|
+
(trap.as_str(), taken > 16 && taken < 128),
|
|
1091
|
+
(
|
|
1092
|
+
"the guest's headers outgrow the most one list may hold",
|
|
1093
|
+
true
|
|
1094
|
+
),
|
|
1095
|
+
"fields.append of 1 KiB values under a 16 KiB memory_limit must trap once the list \
|
|
1096
|
+
passes 128 KiB, whatever the memory_limit; took {taken}"
|
|
1097
|
+
);
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
// @behavior NW-156
|
|
1101
|
+
#[test]
|
|
1102
|
+
fn a_requests_line_shares_its_headers_room() {
|
|
1103
|
+
// Whether a request whose headers hold `headers` bytes takes a path of
|
|
1104
|
+
// `path` bytes.
|
|
1105
|
+
let set = |headers: usize, path: usize| {
|
|
1106
|
+
let mut guest = guest(None);
|
|
1107
|
+
let fields = guest.types_fields_new().unwrap();
|
|
1108
|
+
guest
|
|
1109
|
+
.types_fields_append(fields, "x-pad".to_string(), vec![b'v'; headers])
|
|
1110
|
+
.into_answer()
|
|
1111
|
+
.unwrap()
|
|
1112
|
+
.expect("a header within the list's room appends");
|
|
1113
|
+
let fields = guest.take_handle(fields).unwrap();
|
|
1114
|
+
let request = guest.types_outgoing_request_new(fields).unwrap();
|
|
1115
|
+
let path = format!("/{}", "p".repeat(path - 1));
|
|
1116
|
+
guest
|
|
1117
|
+
.types_outgoing_request_set_path_with_query(request, Some(path))
|
|
1118
|
+
.map(drop)
|
|
1119
|
+
.map_err(|trap| trap.to_string())
|
|
1120
|
+
};
|
|
1121
|
+
assert_eq!(
|
|
1122
|
+
(set(96 * 1024, 16 * 1024), set(96 * 1024, 40 * 1024)),
|
|
1123
|
+
(
|
|
1124
|
+
Ok(()),
|
|
1125
|
+
Err(
|
|
1126
|
+
"the guest's request outgrows the most one request's head may hold".to_string()
|
|
1127
|
+
)
|
|
1128
|
+
),
|
|
1129
|
+
"outgoing-request.set-path-with-query must take a 16 KiB path beside 96 KiB of \
|
|
1130
|
+
headers, and trap on a 40 KiB one, which passes the 128 KiB they share"
|
|
1131
|
+
);
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
// @behavior NW-093
|
|
1135
|
+
#[test]
|
|
1136
|
+
fn a_requests_parts_are_judged_as_they_are_set() {
|
|
1137
|
+
let mut guest = guest(None);
|
|
1138
|
+
let fields = guest.types_fields_new().unwrap();
|
|
1139
|
+
let control = guest
|
|
1140
|
+
.types_fields_append(fields, "x-ok".to_string(), b"a\x01b".to_vec())
|
|
1141
|
+
.into_answer()
|
|
1142
|
+
.unwrap();
|
|
1143
|
+
let fields = guest.take_handle(fields).unwrap();
|
|
1144
|
+
let request = guest.types_outgoing_request_new(fields).unwrap();
|
|
1145
|
+
let mut authority = |value: &str| {
|
|
1146
|
+
guest
|
|
1147
|
+
.types_outgoing_request_set_authority(request, Some(value.to_string()))
|
|
1148
|
+
.unwrap()
|
|
1149
|
+
.is_ok()
|
|
1150
|
+
};
|
|
1151
|
+
let authorities = [
|
|
1152
|
+
authority("example.com:"),
|
|
1153
|
+
authority("example.com:abc"),
|
|
1154
|
+
authority("example.com:80"),
|
|
1155
|
+
];
|
|
1156
|
+
let mut path = |value: &str| {
|
|
1157
|
+
guest
|
|
1158
|
+
.types_outgoing_request_set_path_with_query(request, Some(value.to_string()))
|
|
1159
|
+
.unwrap()
|
|
1160
|
+
.ok()
|
|
1161
|
+
.map(|()| guest.request_mut(request).unwrap().path_with_query.clone())
|
|
1162
|
+
};
|
|
1163
|
+
let paths = [path(""), path("?q"), path("relative")];
|
|
1164
|
+
assert_eq!(
|
|
1165
|
+
(control, authorities, paths),
|
|
1166
|
+
(
|
|
1167
|
+
Err(HeaderError::InvalidSyntax),
|
|
1168
|
+
[false, false, true],
|
|
1169
|
+
[
|
|
1170
|
+
Some(Some("/".to_string())),
|
|
1171
|
+
Some(Some("/?q".to_string())),
|
|
1172
|
+
None
|
|
1173
|
+
]
|
|
1174
|
+
),
|
|
1175
|
+
"fields.append of a value holding a control byte must answer invalid-syntax, \
|
|
1176
|
+
set-authority must refuse an empty or non-numeric port, and set-path-with-query \
|
|
1177
|
+
must take \"\" as / and \"?q\" as /?q, refusing a relative path"
|
|
1178
|
+
);
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
// @behavior NW-145
|
|
1182
|
+
#[test]
|
|
1183
|
+
fn a_rule_judges_the_target_sent_with_no_fragment() {
|
|
1184
|
+
let endpoint = network::Endpoint::new("example.com", 80)
|
|
1185
|
+
.unwrap()
|
|
1186
|
+
.with_rules(vec![network::HttpRule::new(
|
|
1187
|
+
network::Action::Deny,
|
|
1188
|
+
None,
|
|
1189
|
+
"/admin",
|
|
1190
|
+
)]);
|
|
1191
|
+
let mut guest = guest(Some(vec![endpoint]));
|
|
1192
|
+
let request = greeting_request(&mut guest, "example.com").unwrap();
|
|
1193
|
+
guest
|
|
1194
|
+
.types_outgoing_request_set_path_with_query(request, Some("/admin#x".to_string()))
|
|
1195
|
+
.unwrap()
|
|
1196
|
+
.expect("a path with a fragment");
|
|
1197
|
+
let target = guest.request_mut(request).unwrap().path_with_query.clone();
|
|
1198
|
+
let request = guest.take_handle(request).unwrap();
|
|
1199
|
+
let answer = guest.outgoing_handler_handle(request, None).into_answer();
|
|
1200
|
+
assert!(
|
|
1201
|
+
target.as_deref() == Some("/admin")
|
|
1202
|
+
&& matches!(answer, Ok(Err(ErrorCode::HTTPRequestDenied))),
|
|
1203
|
+
"set-path-with-query must take /admin#x as /admin, the target a request sends, \
|
|
1204
|
+
and a rule denying /admin must refuse it; read back {target:?}"
|
|
1205
|
+
);
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
// @behavior NW-094
|
|
1209
|
+
#[test]
|
|
1210
|
+
fn an_immutable_list_reads_a_header_before_refusing_the_change() {
|
|
1211
|
+
let (mut guest, response_headers) = guest_with_response_headers();
|
|
1212
|
+
assert_eq!(
|
|
1213
|
+
guest
|
|
1214
|
+
.types_fields_append(response_headers, "bad name".to_string(), b"x".to_vec())
|
|
1215
|
+
.into_answer()
|
|
1216
|
+
.unwrap(),
|
|
1217
|
+
Err(HeaderError::InvalidSyntax),
|
|
1218
|
+
"fields.append of a name that is no token to the list incoming-response.headers \
|
|
1219
|
+
hands out must answer invalid-syntax, as wasmtime reads the header before it finds \
|
|
1220
|
+
the list immutable"
|
|
1221
|
+
);
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
// @behavior NW-064
|
|
1225
|
+
#[test]
|
|
1226
|
+
fn a_body_dropped_or_finished_while_its_stream_lives_traps() {
|
|
1227
|
+
let mut guest = guest(None);
|
|
1228
|
+
let request = greeting_request(&mut guest, "example.com").unwrap();
|
|
1229
|
+
let body = guest
|
|
1230
|
+
.types_outgoing_request_body(request)
|
|
1231
|
+
.unwrap()
|
|
1232
|
+
.expect("the body is handed out once");
|
|
1233
|
+
let stream = guest
|
|
1234
|
+
.types_outgoing_body_write(body)
|
|
1235
|
+
.unwrap()
|
|
1236
|
+
.expect("the stream is handed out once");
|
|
1237
|
+
let dropped = guest.types_outgoing_body_drop(body).is_err();
|
|
1238
|
+
// `finish` takes the body as its glue lifts it, so the lift is where
|
|
1239
|
+
// a living child traps.
|
|
1240
|
+
let finished = guest.take_handle(body).is_err();
|
|
1241
|
+
guest.streams_output_stream_drop(stream).unwrap();
|
|
1242
|
+
let taken = guest.take_handle(body).unwrap();
|
|
1243
|
+
let after = guest
|
|
1244
|
+
.types_outgoing_body_finish(taken, None)
|
|
1245
|
+
.into_answer()
|
|
1246
|
+
.unwrap();
|
|
1247
|
+
assert_eq!(
|
|
1248
|
+
(dropped, finished, after),
|
|
1249
|
+
(true, true, Ok(())),
|
|
1250
|
+
"outgoing-body.drop and finish must trap while the body's output-stream lives, \
|
|
1251
|
+
and finish once the stream is dropped"
|
|
1252
|
+
);
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
// @behavior NW-161
|
|
1256
|
+
#[test]
|
|
1257
|
+
fn a_responses_headers_outlive_the_response() {
|
|
1258
|
+
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
1259
|
+
let port = listener.local_addr().unwrap().port();
|
|
1260
|
+
let server = std::thread::spawn(move || {
|
|
1261
|
+
let (stream, _) = listener.accept().unwrap();
|
|
1262
|
+
let mut line = String::new();
|
|
1263
|
+
BufReader::new(stream.try_clone().unwrap())
|
|
1264
|
+
.read_line(&mut line)
|
|
1265
|
+
.unwrap();
|
|
1266
|
+
(&stream)
|
|
1267
|
+
.write_all(b"HTTP/1.1 200 OK\r\ncontent-length: 0\r\nx-kind: test\r\n\r\n")
|
|
1268
|
+
.unwrap();
|
|
1269
|
+
});
|
|
1270
|
+
let endpoint = network::Endpoint::new("127.0.0.1", port).unwrap();
|
|
1271
|
+
let mut guest = guest(Some(vec![endpoint]));
|
|
1272
|
+
let started = Instant::now();
|
|
1273
|
+
let (dropped, kind) = run_task(Some(started + MAX_WAIT), || {
|
|
1274
|
+
let request = greeting_request(&mut guest, &format!("127.0.0.1:{port}"))?;
|
|
1275
|
+
let request = guest.take_handle(request)?;
|
|
1276
|
+
let future = guest
|
|
1277
|
+
.outgoing_handler_handle(request, None)
|
|
1278
|
+
.into_answer()?
|
|
1279
|
+
.expect("an endpoint names the authority");
|
|
1280
|
+
let ready = guest.types_future_incoming_response_subscribe(future)?;
|
|
1281
|
+
guest.poll_pollable_block(ready)?;
|
|
1282
|
+
guest.poll_pollable_drop(ready)?;
|
|
1283
|
+
let response = guest
|
|
1284
|
+
.types_future_incoming_response_get(future)?
|
|
1285
|
+
.expect("the head arrived")
|
|
1286
|
+
.expect("handed out once")
|
|
1287
|
+
.expect("the server answered");
|
|
1288
|
+
let headers = guest.types_incoming_response_headers(response)?;
|
|
1289
|
+
let dropped = guest.types_incoming_response_drop(response).is_ok();
|
|
1290
|
+
let kind = guest
|
|
1291
|
+
.types_fields_entries(headers)?
|
|
1292
|
+
.into_iter()
|
|
1293
|
+
.filter(|(name, _)| name == "x-kind")
|
|
1294
|
+
.map(|(_, value)| value)
|
|
1295
|
+
.collect::<Vec<_>>();
|
|
1296
|
+
Ok::<_, wasmtime::Error>((dropped, kind))
|
|
1297
|
+
})
|
|
1298
|
+
.expect("the request answers");
|
|
1299
|
+
server.join().unwrap();
|
|
1300
|
+
assert_eq!(
|
|
1301
|
+
(dropped, kind),
|
|
1302
|
+
(true, vec![b"test".to_vec()]),
|
|
1303
|
+
"incoming-response.drop while its headers live must not trap, and the headers must \
|
|
1304
|
+
still read back, as wasmtime hands out a copy"
|
|
1305
|
+
);
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
/// What sending a GET for `/` to `unnamed.example` answers, with `scheme`
|
|
1309
|
+
/// set; `None` when setting the scheme fails.
|
|
1310
|
+
fn send_unnamed(
|
|
1311
|
+
guest: &mut Invocation,
|
|
1312
|
+
scheme: Scheme,
|
|
1313
|
+
) -> wasmtime::Result<Option<Result<(), ErrorCode>>> {
|
|
1314
|
+
let headers = guest.types_fields_new()?;
|
|
1315
|
+
let headers = guest.take_handle(headers)?;
|
|
1316
|
+
let request = guest.types_outgoing_request_new(headers)?;
|
|
1317
|
+
if guest
|
|
1318
|
+
.types_outgoing_request_set_scheme(request, Some(scheme))?
|
|
1319
|
+
.is_err()
|
|
1320
|
+
{
|
|
1321
|
+
return Ok(None);
|
|
1322
|
+
}
|
|
1323
|
+
guest
|
|
1324
|
+
.types_outgoing_request_set_authority(request, Some("unnamed.example".to_string()))?
|
|
1325
|
+
.expect("an authority");
|
|
1326
|
+
let request = guest.take_handle(request)?;
|
|
1327
|
+
let sent = guest.outgoing_handler_handle(request, None).into_answer()?;
|
|
1328
|
+
Ok(Some(sent.map(drop)))
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
// @behavior NW-162
|
|
1332
|
+
#[test]
|
|
1333
|
+
fn a_requests_scheme_is_judged_where_wasmtime_judges_it() {
|
|
1334
|
+
let mut guest = guest(Some(vec![
|
|
1335
|
+
network::Endpoint::new("example.com", 80).unwrap()
|
|
1336
|
+
]));
|
|
1337
|
+
let other = |name: &str| Scheme::Other(name.to_string());
|
|
1338
|
+
let answers = [
|
|
1339
|
+
send_unnamed(&mut guest, other("not a scheme")),
|
|
1340
|
+
send_unnamed(&mut guest, other("ftp")),
|
|
1341
|
+
send_unnamed(&mut guest, other("http")),
|
|
1342
|
+
]
|
|
1343
|
+
.map(|answer| answer.expect("the guest's calls must not trap"));
|
|
1344
|
+
assert_eq!(
|
|
1345
|
+
answers,
|
|
1346
|
+
[
|
|
1347
|
+
None,
|
|
1348
|
+
Some(Err(ErrorCode::HTTPProtocolError)),
|
|
1349
|
+
Some(Err(ErrorCode::HTTPRequestDenied))
|
|
1350
|
+
],
|
|
1351
|
+
"outgoing-request.set-scheme must refuse only a scheme that does not parse; handle \
|
|
1352
|
+
must answer HTTP-protocol-error for ftp and judge http as HTTP, as wasmtime does"
|
|
1353
|
+
);
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
/// A loopback server that takes connections and never answers, and the
|
|
1357
|
+
/// guest whose one http endpoint names it.
|
|
1358
|
+
fn guest_of_a_silent_server() -> (Invocation, String, TcpListener) {
|
|
1359
|
+
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
1360
|
+
let port = listener.local_addr().unwrap().port();
|
|
1361
|
+
let guest = guest(Some(vec![
|
|
1362
|
+
network::Endpoint::new("127.0.0.1", port).unwrap()
|
|
1363
|
+
]));
|
|
1364
|
+
(guest, format!("127.0.0.1:{port}"), listener)
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
/// The error `future` answers once its request settles.
|
|
1368
|
+
fn settled_error(guest: &mut Invocation, future: FutureIncomingResponse) -> Option<ErrorCode> {
|
|
1369
|
+
run_task(Some(Instant::now() + MAX_WAIT), || {
|
|
1370
|
+
let ready = guest.types_future_incoming_response_subscribe(future)?;
|
|
1371
|
+
guest.poll_pollable_block(ready)?;
|
|
1372
|
+
guest.poll_pollable_drop(ready)?;
|
|
1373
|
+
Ok::<_, wasmtime::Error>(
|
|
1374
|
+
guest
|
|
1375
|
+
.types_future_incoming_response_get(future)?
|
|
1376
|
+
.and_then(|answer| answer.ok())
|
|
1377
|
+
.and_then(|response| response.err()),
|
|
1378
|
+
)
|
|
1379
|
+
})
|
|
1380
|
+
.expect("the request settles")
|
|
1381
|
+
}
|
|
1382
|
+
|
|
1383
|
+
/// The body a chunked request carries, read from `reader` to its last
|
|
1384
|
+
/// chunk.
|
|
1385
|
+
fn read_chunked(reader: &mut impl BufRead) -> Vec<u8> {
|
|
1386
|
+
let mut body = Vec::new();
|
|
1387
|
+
loop {
|
|
1388
|
+
let mut size = String::new();
|
|
1389
|
+
reader.read_line(&mut size).unwrap();
|
|
1390
|
+
let size = usize::from_str_radix(size.trim(), 16).unwrap();
|
|
1391
|
+
let mut chunk = vec![0; size + 2];
|
|
1392
|
+
std::io::Read::read_exact(reader, &mut chunk).unwrap();
|
|
1393
|
+
if size == 0 {
|
|
1394
|
+
return body;
|
|
1395
|
+
}
|
|
1396
|
+
body.extend_from_slice(&chunk[..size]);
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
|
|
1400
|
+
// @behavior NW-251
|
|
1401
|
+
#[test]
|
|
1402
|
+
fn a_request_body_holds_one_socket_buffer_until_the_server_takes_it() {
|
|
1403
|
+
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
1404
|
+
let port = listener.local_addr().unwrap().port();
|
|
1405
|
+
let (start_reading, reading) = std::sync::mpsc::channel::<()>();
|
|
1406
|
+
let server = std::thread::spawn(move || {
|
|
1407
|
+
let (stream, _) = listener.accept().unwrap();
|
|
1408
|
+
reading.recv().unwrap();
|
|
1409
|
+
let mut reader = BufReader::new(stream.try_clone().unwrap());
|
|
1410
|
+
let mut line = String::new();
|
|
1411
|
+
while line != "\r\n" {
|
|
1412
|
+
line.clear();
|
|
1413
|
+
reader.read_line(&mut line).unwrap();
|
|
1414
|
+
}
|
|
1415
|
+
let body = read_chunked(&mut reader);
|
|
1416
|
+
(&stream)
|
|
1417
|
+
.write_all(b"HTTP/1.1 204 No Content\r\n\r\n")
|
|
1418
|
+
.unwrap();
|
|
1419
|
+
body
|
|
1420
|
+
});
|
|
1421
|
+
let mut guest = guest(Some(vec![
|
|
1422
|
+
network::Endpoint::new("127.0.0.1", port).unwrap()
|
|
1423
|
+
]));
|
|
1424
|
+
let sent = (0..8 << 20).map(|i| (i % 251) as u8).collect::<Vec<_>>();
|
|
1425
|
+
let deadline = Instant::now() + MAX_WAIT;
|
|
1426
|
+
let (most, unready, status) = run_task(Some(deadline), || {
|
|
1427
|
+
let request = greeting_request(&mut guest, &format!("127.0.0.1:{port}"))?;
|
|
1428
|
+
guest
|
|
1429
|
+
.types_outgoing_request_set_method(request, Method::Post)?
|
|
1430
|
+
.expect("POST is a method");
|
|
1431
|
+
let body = guest
|
|
1432
|
+
.types_outgoing_request_body(request)?
|
|
1433
|
+
.expect("the body is handed out once");
|
|
1434
|
+
let output = guest
|
|
1435
|
+
.types_outgoing_body_write(body)?
|
|
1436
|
+
.expect("the stream is handed out once");
|
|
1437
|
+
let request = guest.take_handle(request)?;
|
|
1438
|
+
let future = guest
|
|
1439
|
+
.outgoing_handler_handle(request, None)
|
|
1440
|
+
.into_answer()?
|
|
1441
|
+
.expect("an http endpoint names the authority");
|
|
1442
|
+
let writable = guest.streams_output_stream_subscribe(output)?;
|
|
1443
|
+
let (mut written, mut most, mut unready) = (0, 0, false);
|
|
1444
|
+
while written < sent.len() {
|
|
1445
|
+
let permit = guest
|
|
1446
|
+
.streams_output_stream_check_write(output)
|
|
1447
|
+
.into_answer()?
|
|
1448
|
+
.expect("an open stream permits") as usize;
|
|
1449
|
+
most = most.max(permit);
|
|
1450
|
+
if permit == 0 {
|
|
1451
|
+
// The seam may still move what is queued into the
|
|
1452
|
+
// host's socket; once that is full too, nothing frees room.
|
|
1453
|
+
if !unready {
|
|
1454
|
+
let now = guest.monotonic_clock_subscribe_duration(0)?;
|
|
1455
|
+
unready = !guest.poll_poll(vec![writable, now])?.contains(&0);
|
|
1456
|
+
guest.poll_pollable_drop(now)?;
|
|
1457
|
+
if !unready {
|
|
1458
|
+
continue;
|
|
1459
|
+
}
|
|
1460
|
+
start_reading.send(()).unwrap();
|
|
1461
|
+
}
|
|
1462
|
+
guest.poll_pollable_block(writable)?;
|
|
1463
|
+
continue;
|
|
1464
|
+
}
|
|
1465
|
+
let take = permit.min(sent.len() - written);
|
|
1466
|
+
guest
|
|
1467
|
+
.streams_output_stream_write(output, sent[written..written + take].to_vec())
|
|
1468
|
+
.into_answer()?
|
|
1469
|
+
.expect("a write of the permit succeeds");
|
|
1470
|
+
written += take;
|
|
1471
|
+
}
|
|
1472
|
+
if !unready {
|
|
1473
|
+
start_reading.send(()).unwrap();
|
|
1474
|
+
}
|
|
1475
|
+
guest.poll_pollable_drop(writable)?;
|
|
1476
|
+
guest.streams_output_stream_drop(output)?;
|
|
1477
|
+
let body = guest.take_handle(body)?;
|
|
1478
|
+
guest
|
|
1479
|
+
.types_outgoing_body_finish(body, None)
|
|
1480
|
+
.into_answer()?
|
|
1481
|
+
.expect("the body ends");
|
|
1482
|
+
let ready = guest.types_future_incoming_response_subscribe(future)?;
|
|
1483
|
+
guest.poll_pollable_block(ready)?;
|
|
1484
|
+
let response = guest
|
|
1485
|
+
.types_future_incoming_response_get(future)?
|
|
1486
|
+
.expect("the head arrived")
|
|
1487
|
+
.expect("handed out once")
|
|
1488
|
+
.expect("the server answered");
|
|
1489
|
+
Ok::<_, wasmtime::Error>((
|
|
1490
|
+
most,
|
|
1491
|
+
unready,
|
|
1492
|
+
guest.types_incoming_response_status(response)?,
|
|
1493
|
+
))
|
|
1494
|
+
})
|
|
1495
|
+
.expect("the request answers");
|
|
1496
|
+
let received = server.join().unwrap();
|
|
1497
|
+
assert!(
|
|
1498
|
+
most <= 64 * 1024 && unready && status == 204 && received == sent,
|
|
1499
|
+
"an 8 MiB request body to a server that reads it late must be permitted at \
|
|
1500
|
+
most one 64 KiB socket buffer at a time, poll unready while the buffer is full, and \
|
|
1501
|
+
reach the server whole and in order; permitted at most {most}, unready {unready}, \
|
|
1502
|
+
status {status}, the server read {} bytes",
|
|
1503
|
+
received.len()
|
|
1504
|
+
);
|
|
1505
|
+
}
|
|
1506
|
+
|
|
1507
|
+
// @behavior NW-193
|
|
1508
|
+
#[test]
|
|
1509
|
+
fn a_body_finished_with_trailers_is_refused_and_fails_its_request() {
|
|
1510
|
+
let (mut guest, authority, _server) = guest_of_a_silent_server();
|
|
1511
|
+
// Whether `finish` with `trailers` refuses, and what the request's
|
|
1512
|
+
// future answers then.
|
|
1513
|
+
let finished_with = |guest: &mut Invocation, trailer: Option<(&str, &[u8])>| {
|
|
1514
|
+
let request = greeting_request(guest, &authority).unwrap();
|
|
1515
|
+
let body = guest
|
|
1516
|
+
.types_outgoing_request_body(request)
|
|
1517
|
+
.unwrap()
|
|
1518
|
+
.expect("the body is handed out once");
|
|
1519
|
+
let request = guest.take_handle(request).unwrap();
|
|
1520
|
+
let future = guest
|
|
1521
|
+
.outgoing_handler_handle(request, None)
|
|
1522
|
+
.into_answer()
|
|
1523
|
+
.unwrap()
|
|
1524
|
+
.expect("an http endpoint names the authority");
|
|
1525
|
+
let trailers = guest.types_fields_new().unwrap();
|
|
1526
|
+
if let Some((name, value)) = trailer {
|
|
1527
|
+
guest
|
|
1528
|
+
.types_fields_append(trailers, name.to_string(), value.to_vec())
|
|
1529
|
+
.into_answer()
|
|
1530
|
+
.unwrap()
|
|
1531
|
+
.expect("a trailer appends");
|
|
1532
|
+
}
|
|
1533
|
+
let trailers = guest.take_handle(trailers).unwrap();
|
|
1534
|
+
let body = guest.take_handle(body).unwrap();
|
|
1535
|
+
let finished = guest
|
|
1536
|
+
.types_outgoing_body_finish(body, Some(trailers))
|
|
1537
|
+
.into_answer()
|
|
1538
|
+
.unwrap();
|
|
1539
|
+
(finished, future)
|
|
1540
|
+
};
|
|
1541
|
+
let (refused, refused_future) = finished_with(&mut guest, Some(("cookie", b"session=1")));
|
|
1542
|
+
let refused = (refused, settled_error(&mut guest, refused_future));
|
|
1543
|
+
let empty = finished_with(&mut guest, None);
|
|
1544
|
+
assert_eq!(
|
|
1545
|
+
(refused, empty.0),
|
|
1546
|
+
(
|
|
1547
|
+
(
|
|
1548
|
+
Err(ErrorCode::HTTPRequestDenied),
|
|
1549
|
+
Some(ErrorCode::HTTPProtocolError)
|
|
1550
|
+
),
|
|
1551
|
+
Ok(())
|
|
1552
|
+
),
|
|
1553
|
+
"outgoing-body.finish with trailers holding a field must answer HTTP-request-denied \
|
|
1554
|
+
and fail its request, since an egress proxy sends no trailers and the request \
|
|
1555
|
+
policies never see them; trailers holding nothing must end the body"
|
|
1556
|
+
);
|
|
1557
|
+
}
|
|
1558
|
+
|
|
1559
|
+
// @behavior NW-071
|
|
1560
|
+
#[test]
|
|
1561
|
+
fn a_body_dropped_unfinished_fails_its_request() {
|
|
1562
|
+
let (mut guest, authority, _server) = guest_of_a_silent_server();
|
|
1563
|
+
let request = greeting_request(&mut guest, &authority).unwrap();
|
|
1564
|
+
let body = guest
|
|
1565
|
+
.types_outgoing_request_body(request)
|
|
1566
|
+
.unwrap()
|
|
1567
|
+
.expect("the body is handed out once");
|
|
1568
|
+
let request = guest.take_handle(request).unwrap();
|
|
1569
|
+
let future = guest
|
|
1570
|
+
.outgoing_handler_handle(request, None)
|
|
1571
|
+
.into_answer()
|
|
1572
|
+
.unwrap()
|
|
1573
|
+
.expect("an http endpoint names the authority");
|
|
1574
|
+
let waiting = guest
|
|
1575
|
+
.types_future_incoming_response_get(future)
|
|
1576
|
+
.unwrap()
|
|
1577
|
+
.is_none();
|
|
1578
|
+
guest.types_outgoing_body_drop(body).unwrap();
|
|
1579
|
+
let answer = settled_error(&mut guest, future);
|
|
1580
|
+
assert_eq!(
|
|
1581
|
+
(waiting, answer),
|
|
1582
|
+
(true, Some(ErrorCode::HTTPProtocolError)),
|
|
1583
|
+
"future-incoming-response.get must wait while the outgoing-body is open, and answer \
|
|
1584
|
+
HTTP-protocol-error once the body is dropped unfinished"
|
|
1585
|
+
);
|
|
1586
|
+
}
|
|
1587
|
+
}
|