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