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.
Files changed (266) hide show
  1. checksums.yaml +4 -4
  2. data/.release-please-manifest.json +1 -1
  3. data/CHANGELOG.md +335 -0
  4. data/Cargo.lock +469 -11
  5. data/README.md +43 -3
  6. data/ROADMAP.md +1 -1
  7. data/crates/kobako-runtime/CHANGELOG.md +219 -0
  8. data/crates/kobako-runtime/Cargo.toml +51 -3
  9. data/crates/kobako-runtime/README.md +7 -4
  10. data/crates/kobako-runtime/src/cred.rs +124 -0
  11. data/crates/kobako-runtime/src/dispatch.rs +4 -5
  12. data/crates/kobako-runtime/src/endpoint/buffer.rs +153 -44
  13. data/crates/kobako-runtime/src/endpoint/dispatch.rs +80 -41
  14. data/crates/kobako-runtime/src/endpoint/invoke.rs +23 -36
  15. data/crates/kobako-runtime/src/endpoint.rs +137 -36
  16. data/crates/kobako-runtime/src/errno.rs +391 -0
  17. data/crates/kobako-runtime/src/error.rs +31 -10
  18. data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
  19. data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +52 -14
  20. data/crates/kobako-runtime/src/filesystem/bind/walk.rs +19 -20
  21. data/crates/kobako-runtime/src/filesystem/bind.rs +437 -245
  22. data/crates/kobako-runtime/src/filesystem/overlay/file.rs +6 -6
  23. data/crates/kobako-runtime/src/filesystem/overlay/path.rs +43 -65
  24. data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +9 -16
  25. data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +47 -72
  26. data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +12 -12
  27. data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +4 -7
  28. data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +121 -37
  29. data/crates/kobako-runtime/src/filesystem/overlay.rs +302 -343
  30. data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
  31. data/crates/kobako-runtime/src/filesystem/rdir.rs +15 -32
  32. data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
  33. data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +3 -3
  34. data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +57 -35
  35. data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +2 -2
  36. data/crates/kobako-runtime/src/filesystem/tmpfs.rs +172 -160
  37. data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
  38. data/crates/kobako-runtime/src/filesystem.rs +6 -3
  39. data/crates/kobako-runtime/src/fork.rs +96 -0
  40. data/crates/kobako-runtime/src/grant.rs +3 -0
  41. data/crates/kobako-runtime/src/hashtable.rs +4 -2
  42. data/crates/kobako-runtime/src/lib.rs +18 -7
  43. data/crates/kobako-runtime/src/limits.rs +195 -0
  44. data/crates/kobako-runtime/src/locking.rs +10 -0
  45. data/crates/kobako-runtime/src/memcg.rs +383 -0
  46. data/crates/kobako-runtime/src/mount.rs +67 -9
  47. data/crates/kobako-runtime/src/net/backends.rs +43 -0
  48. data/crates/kobako-runtime/src/net/connections.rs +179 -0
  49. data/crates/kobako-runtime/src/net/credential.rs +125 -0
  50. data/crates/kobako-runtime/src/net/egress.rs +160 -0
  51. data/crates/kobako-runtime/src/net/family.rs +181 -0
  52. data/crates/kobako-runtime/src/net/http.rs +2733 -0
  53. data/crates/kobako-runtime/src/net/lookup.rs +285 -0
  54. data/crates/kobako-runtime/src/net/mock.rs +251 -0
  55. data/crates/kobako-runtime/src/net/namespace.rs +1366 -0
  56. data/crates/kobako-runtime/src/net/reactor.rs +531 -0
  57. data/crates/kobako-runtime/src/net/resolv/darwin.rs +275 -0
  58. data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +469 -0
  59. data/crates/kobako-runtime/src/net/resolv/stub.rs +536 -0
  60. data/crates/kobako-runtime/src/net/resolv/windows.rs +248 -0
  61. data/crates/kobako-runtime/src/net/resolv.rs +227 -0
  62. data/crates/kobako-runtime/src/net/socket.rs +870 -0
  63. data/crates/kobako-runtime/src/net/tcp.rs +1588 -0
  64. data/crates/kobako-runtime/src/net/tls.rs +58 -0
  65. data/crates/kobako-runtime/src/net/transport.rs +279 -0
  66. data/crates/kobako-runtime/src/net/udp.rs +1003 -0
  67. data/crates/kobako-runtime/src/net.rs +42 -0
  68. data/crates/kobako-runtime/src/network/builder.rs +177 -0
  69. data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
  70. data/crates/kobako-runtime/src/network.rs +551 -0
  71. data/crates/kobako-runtime/src/policy.rs +16 -0
  72. data/crates/kobako-runtime/src/profile.rs +29 -144
  73. data/crates/kobako-runtime/src/runtime.rs +3 -5
  74. data/crates/kobako-runtime/src/sched.rs +130 -29
  75. data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
  76. data/crates/kobako-runtime/src/{vfs/cred.rs → security/hooks.rs} +52 -144
  77. data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
  78. data/crates/kobako-runtime/src/security.rs +101 -196
  79. data/crates/kobako-runtime/src/snapshot.rs +121 -2
  80. data/crates/kobako-runtime/src/vfs/attr.rs +5 -5
  81. data/crates/kobako-runtime/src/vfs/dentry.rs +143 -89
  82. data/crates/kobako-runtime/src/vfs/file.rs +199 -203
  83. data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
  84. data/crates/kobako-runtime/src/vfs/kern_path.rs +38 -42
  85. data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
  86. data/crates/kobako-runtime/src/vfs/namei.rs +829 -700
  87. data/crates/kobako-runtime/src/vfs/namespace.rs +162 -0
  88. data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
  89. data/crates/kobako-runtime/src/vfs/openat2.rs +3 -3
  90. data/crates/kobako-runtime/src/vfs/path.rs +6 -6
  91. data/crates/kobako-runtime/src/vfs/readdir.rs +6 -6
  92. data/crates/kobako-runtime/src/vfs/stat.rs +159 -23
  93. data/crates/kobako-runtime/src/vfs/super_block.rs +18 -14
  94. data/crates/kobako-runtime/src/vfs/utimes.rs +37 -27
  95. data/crates/kobako-runtime/src/vfs.rs +9 -6
  96. data/crates/kobako-runtime/src/wait.rs +356 -0
  97. data/crates/kobako-transport/CHANGELOG.md +31 -0
  98. data/crates/kobako-transport/Cargo.toml +1 -1
  99. data/crates/kobako-transport/README.md +3 -4
  100. data/crates/kobako-transport/src/abi.rs +78 -72
  101. data/crates/kobako-transport/src/envelope/bytes.rs +17 -6
  102. data/crates/kobako-transport/src/envelope/call.rs +32 -4
  103. data/crates/kobako-transport/src/envelope/error_record.rs +21 -0
  104. data/crates/kobako-transport/src/envelope/name.rs +99 -0
  105. data/crates/kobako-transport/src/envelope/outcome.rs +9 -9
  106. data/crates/kobako-transport/src/envelope/preamble.rs +24 -21
  107. data/crates/kobako-transport/src/envelope/reply.rs +27 -27
  108. data/crates/kobako-transport/src/envelope/unsupported.rs +10 -10
  109. data/crates/kobako-transport/src/envelope.rs +13 -8
  110. data/crates/kobako-wasmtime/CHANGELOG.md +161 -0
  111. data/crates/kobako-wasmtime/Cargo.toml +26 -7
  112. data/crates/kobako-wasmtime/README.md +3 -2
  113. data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
  114. data/crates/kobako-wasmtime/build.rs +16 -0
  115. data/crates/kobako-wasmtime/src/abi.rs +17 -37
  116. data/crates/kobako-wasmtime/src/ambient.rs +33 -18
  117. data/crates/kobako-wasmtime/src/cache.rs +23 -15
  118. data/crates/kobako-wasmtime/src/config.rs +52 -68
  119. data/crates/kobako-wasmtime/src/driver.rs +48 -63
  120. data/crates/kobako-wasmtime/src/exports.rs +143 -17
  121. data/crates/kobako-wasmtime/src/guest.rs +12 -6
  122. data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
  123. data/crates/kobako-wasmtime/src/imports.rs +42 -18
  124. data/crates/kobako-wasmtime/src/instance_pre.rs +4 -4
  125. data/crates/kobako-wasmtime/src/invocation.rs +74 -12
  126. data/crates/kobako-wasmtime/src/lib.rs +5 -3
  127. data/crates/kobako-wasmtime/src/limiter.rs +70 -5
  128. data/crates/kobako-wasmtime/src/preview1/descriptors.rs +309 -0
  129. data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +5 -15
  130. data/crates/kobako-wasmtime/src/preview1/differential.rs +27 -31
  131. data/crates/kobako-wasmtime/src/preview1/errno.rs +55 -66
  132. data/crates/kobako-wasmtime/src/preview1/fd.rs +327 -296
  133. data/crates/kobako-wasmtime/src/preview1/memory.rs +21 -12
  134. data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +34 -53
  135. data/crates/kobako-wasmtime/src/preview1/mount_view.rs +231 -217
  136. data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
  137. data/crates/kobako-wasmtime/src/preview1/poll.rs +244 -120
  138. data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
  139. data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
  140. data/crates/kobako-wasmtime/src/preview1.rs +46 -42
  141. data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
  142. data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
  143. data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
  144. data/crates/kobako-wasmtime/src/preview2/host.rs +3959 -0
  145. data/crates/kobako-wasmtime/src/preview2/http.rs +1503 -0
  146. data/crates/kobako-wasmtime/src/preview2/io.rs +315 -0
  147. data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
  148. data/crates/kobako-wasmtime/src/preview2/table.rs +305 -0
  149. data/crates/kobako-wasmtime/src/preview2.rs +57 -0
  150. data/crates/kobako-wasmtime/src/trap.rs +24 -9
  151. data/crates/kobako-wasmtime/src/wasi.rs +210 -81
  152. data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
  153. data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
  154. data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
  155. data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
  156. data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
  157. data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
  158. data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
  159. data/data/kobako.wasm +0 -0
  160. data/ext/kobako/Cargo.toml +1 -1
  161. data/ext/kobako/src/{runtime/errors.rs → errors.rs} +34 -26
  162. data/ext/kobako/src/{runtime/escape.rs → escape.rs} +42 -11
  163. data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +30 -26
  164. data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +149 -168
  165. data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +24 -15
  166. data/ext/kobako/src/gvl.rs +306 -0
  167. data/ext/kobako/src/lib.rs +64 -3
  168. data/ext/kobako/src/network/mock.rs +187 -0
  169. data/ext/kobako/src/network.rs +591 -0
  170. data/ext/kobako/src/runtime/bridge.rs +30 -29
  171. data/ext/kobako/src/runtime/snapshot.rs +4 -4
  172. data/ext/kobako/src/runtime.rs +221 -143
  173. data/ext/kobako/src/wire.rs +42 -0
  174. data/lib/kobako/catalog/extensions.rb +14 -2
  175. data/lib/kobako/catalog/flags.rb +3 -5
  176. data/lib/kobako/catalog/grants.rb +5 -4
  177. data/lib/kobako/catalog/handles.rb +5 -27
  178. data/lib/kobako/catalog/mount.rb +11 -3
  179. data/lib/kobako/catalog/network.rb +35 -0
  180. data/lib/kobako/catalog/replacements.rb +28 -0
  181. data/lib/kobako/catalog/services.rb +5 -19
  182. data/lib/kobako/catalog/snippets.rb +3 -18
  183. data/lib/kobako/catalog.rb +3 -0
  184. data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
  185. data/lib/kobako/codec/decoder.rb +4 -4
  186. data/lib/kobako/codec/ext_types.rb +3 -3
  187. data/lib/kobako/codec/handle_walk.rb +3 -43
  188. data/lib/kobako/codec/value_budget.rb +132 -40
  189. data/lib/kobako/codec.rb +1 -1
  190. data/lib/kobako/context.rb +24 -11
  191. data/lib/kobako/errors.rb +15 -12
  192. data/lib/kobako/extension.rb +3 -2
  193. data/lib/kobako/filesystem.rb +9 -2
  194. data/lib/kobako/handle.rb +2 -2
  195. data/lib/kobako/names.rb +28 -0
  196. data/lib/kobako/network.rb +210 -0
  197. data/lib/kobako/outcome.rb +7 -7
  198. data/lib/kobako/pool.rb +45 -23
  199. data/lib/kobako/runtime.rb +3 -2
  200. data/lib/kobako/sandbox.rb +92 -35
  201. data/lib/kobako/sandbox_options.rb +41 -106
  202. data/lib/kobako/transport/argument_wrap.rb +64 -0
  203. data/lib/kobako/transport/block_failure.rb +1 -1
  204. data/lib/kobako/transport/dispatcher.rb +6 -6
  205. data/lib/kobako/transport/exposure.rb +26 -15
  206. data/lib/kobako/transport/reflection.rb +21 -4
  207. data/lib/kobako/transport/run.rb +8 -23
  208. data/lib/kobako/transport/yielder.rb +2 -9
  209. data/lib/kobako/transport.rb +1 -0
  210. data/lib/kobako/version.rb +1 -1
  211. data/lib/kobako.rb +6 -2
  212. data/release-please-config.json +15 -0
  213. data/sig/core/thread.rbs +6 -0
  214. data/sig/kobako/catalog/extensions.rbs +2 -0
  215. data/sig/kobako/catalog/grants.rbs +7 -4
  216. data/sig/kobako/catalog/handles.rbs +0 -2
  217. data/sig/kobako/catalog/mount.rbs +3 -1
  218. data/sig/kobako/catalog/network.rbs +20 -0
  219. data/sig/kobako/catalog/replacements.rbs +14 -0
  220. data/sig/kobako/catalog/services.rbs +0 -4
  221. data/sig/kobako/catalog/snippets.rbs +0 -2
  222. data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
  223. data/sig/kobako/codec/handle_walk.rbs +0 -6
  224. data/sig/kobako/codec/value_budget.rbs +47 -4
  225. data/sig/kobako/context.rbs +3 -1
  226. data/sig/kobako/errors.rbs +4 -4
  227. data/sig/kobako/filesystem.rbs +21 -3
  228. data/sig/kobako/network.rbs +115 -0
  229. data/sig/kobako/outcome.rbs +1 -1
  230. data/sig/kobako/pool.rbs +7 -3
  231. data/sig/kobako/runtime.rbs +32 -3
  232. data/sig/kobako/sandbox.rbs +19 -2
  233. data/sig/kobako/sandbox_options.rbs +29 -38
  234. data/sig/kobako/transport/argument_wrap.rbs +15 -0
  235. data/sig/kobako/transport/error.rbs +1 -1
  236. data/sig/kobako/transport/exposure.rbs +2 -4
  237. data/sig/kobako/transport/reflection.rbs +10 -0
  238. data/sig/kobako/transport/run.rbs +0 -2
  239. data/sig/kobako/transport/yielder.rbs +0 -3
  240. data/sig/kobako/transport.rbs +1 -1
  241. data/sig/kobako.rbs +13 -0
  242. metadata +84 -35
  243. data/crates/kobako-runtime/src/endpoint/capture.rs +0 -115
  244. data/crates/kobako-runtime/src/endpoint/memory_cap.rs +0 -238
  245. data/crates/kobako-runtime/src/filesystem/path.rs +0 -21
  246. data/crates/kobako-runtime/src/vfs/errno.rs +0 -153
  247. data/crates/kobako-runtime/tests/linux_baseline/calls.rs +0 -171
  248. data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +0 -276
  249. data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +0 -323
  250. data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
  251. data/crates/kobako-runtime/tests/linux_baseline/linux.rs +0 -449
  252. data/crates/kobako-runtime/tests/linux_baseline/main.rs +0 -611
  253. data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +0 -236
  254. data/crates/kobako-wasmtime/src/descriptors.rs +0 -200
  255. data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +0 -43
  256. data/crates/kobako-wasmtime/tests/integration/guest_file.rs +0 -1085
  257. data/crates/kobako-wasmtime/tests/integration/main.rs +0 -29
  258. data/crates/kobako-wasmtime/tests/integration/preamble_flags.rs +0 -70
  259. data/crates/kobako-wasmtime/tests/integration/snippet_compile.rs +0 -95
  260. data/crates/kobako-wasmtime/tests/integration/support/mod.rs +0 -30
  261. data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +0 -86
  262. data/crates/kobako-wasmtime/tests/integration/unsupported_call.rs +0 -51
  263. data/ext/kobako/src/runtime/gvl.rs +0 -147
  264. data/lib/kobako/constant_name.rb +0 -8
  265. data/sig/kobako/constant_name.rbs +0 -3
  266. /data/ext/kobako/src/{runtime/pinned_value.rs → pinned_value.rs} +0 -0
@@ -0,0 +1,2733 @@
1
+ //! An HTTP request the guest sends, as a userspace proxy forwards one: the
2
+ //! namespace judges what the guest named on the guest thread, then a task on
3
+ //! the reactor hands the request to the outbound seam, whose only dialer is
4
+ //! kobako's connector. The response's body fills a buffer the guest reads
5
+ //! through the wait queue, so a full buffer stops the task and the TCP window
6
+ //! pushes back.
7
+
8
+ use std::collections::VecDeque;
9
+ use std::future::{poll_fn, Future};
10
+ use std::io;
11
+ use std::pin::Pin;
12
+ use std::sync::{Arc, Mutex, MutexGuard, OnceLock, PoisonError};
13
+ use std::task::{Context, Poll, Waker};
14
+ use std::time::{Duration, Instant};
15
+
16
+ use bytes::Bytes;
17
+ use http_body::Body;
18
+ use http_body_util::combinators::UnsyncBoxBody;
19
+ use http_body_util::{BodyExt, Full};
20
+ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
21
+
22
+ use super::connections::{open, OpenConnections, Place};
23
+ use super::credential::Swap;
24
+ use super::reactor::{Reactor, Task};
25
+ use super::transport::Connector;
26
+ use super::{new_platform_tls, MAX_SOCKET_BUFFER};
27
+ use crate::cred::Cred;
28
+ use crate::errno::CarriedErrno;
29
+ use crate::filesystem::anon_inode_getfile;
30
+ use crate::memcg::MemoryCharge;
31
+ use crate::vfs::{FMode, File, FileOperations, Kiocb, OpenHow, PollMask};
32
+ use crate::wait::{poll_wait, PollTable, WaitQueueHead};
33
+ use crate::Errno;
34
+
35
+ /// Whether a request travels in clear or over TLS.
36
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
37
+ #[non_exhaustive]
38
+ pub enum Scheme {
39
+ Http,
40
+ Https,
41
+ }
42
+
43
+ /// The headers no guest sets: wasmtime's `DEFAULT_FORBIDDEN_HEADERS`, and
44
+ /// `content-length` and `te`, since the seam frames the body and its encoding
45
+ /// itself.
46
+ const FORBIDDEN_HEADERS: [&str; 11] = [
47
+ "connection",
48
+ "content-length",
49
+ "host",
50
+ "http2-settings",
51
+ "keep-alive",
52
+ "proxy-authenticate",
53
+ "proxy-authorization",
54
+ "proxy-connection",
55
+ "te",
56
+ "transfer-encoding",
57
+ "upgrade",
58
+ ];
59
+
60
+ /// Why a header the guest names is not one it may set.
61
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
62
+ #[non_exhaustive]
63
+ pub enum FieldError {
64
+ InvalidSyntax,
65
+ Forbidden,
66
+ /// The list would outgrow `MAX_FIELD_SIZE`.
67
+ TooLarge,
68
+ /// The list would outgrow the Invocation's memory.
69
+ OutOfMemory,
70
+ }
71
+
72
+ /// The most room one header list the guest builds may take, as wasmtime's
73
+ /// `DEFAULT_FIELD_SIZE_LIMIT`; each entry counts its name, its value, and its
74
+ /// own place, so even an empty value counts.
75
+ pub const MAX_FIELD_SIZE: usize = 128 * 1024;
76
+
77
+ /// The most a response's head, or one read of its body, takes in the seam's
78
+ /// read buffer, as hyper's `max_buf_size` bounds it, so a head the guest could
79
+ /// read as a header list fits and a longer one answers a protocol error. An
80
+ /// exchange holds it against the Invocation's memory.
81
+ const MAX_READ_BUFFER: usize = MAX_FIELD_SIZE;
82
+
83
+ /// A header list the guest builds or reads, held on its behalf against the
84
+ /// Invocation's memory, as a request's body is, so no list grows the host
85
+ /// past what the guest was allotted.
86
+ pub struct Fields {
87
+ list: Mutex<List>,
88
+ }
89
+
90
+ /// A list's entries and the room they take.
91
+ struct List {
92
+ entries: Vec<(String, Vec<u8>)>,
93
+ room: MemoryCharge,
94
+ }
95
+
96
+ /// The room `name` and `value` take in a list.
97
+ fn room_of(name: &str, value: &[u8]) -> usize {
98
+ name.len() + value.len() + std::mem::size_of::<(String, Vec<u8>)>()
99
+ }
100
+
101
+ /// The room every entry of `entries` takes in a list.
102
+ fn total_room_of(entries: &[(String, Vec<u8>)]) -> usize {
103
+ entries
104
+ .iter()
105
+ .map(|(name, value)| room_of(name, value))
106
+ .sum()
107
+ }
108
+
109
+ impl Fields {
110
+ /// An empty list, charged to `cred` as it grows.
111
+ pub fn new(cred: Arc<Cred>) -> Arc<Self> {
112
+ Arc::new(Self {
113
+ list: Mutex::new(List {
114
+ entries: Vec::new(),
115
+ room: MemoryCharge::empty(cred.memory_budget()),
116
+ }),
117
+ })
118
+ }
119
+
120
+ /// A list holding `entries`, as a response's headers are handed out, its
121
+ /// room charged to `cred`; it may outgrow `MAX_FIELD_SIZE`, which only
122
+ /// bounds what the guest appends.
123
+ pub fn with_entries(
124
+ cred: Arc<Cred>,
125
+ entries: Vec<(String, Vec<u8>)>,
126
+ ) -> Result<Arc<Self>, FieldError> {
127
+ let room = total_room_of(&entries);
128
+ let room =
129
+ MemoryCharge::new(cred.memory_budget(), room).map_err(|_| FieldError::OutOfMemory)?;
130
+ Ok(Arc::new(Self {
131
+ list: Mutex::new(List { entries, room }),
132
+ }))
133
+ }
134
+
135
+ fn list(&self) -> MutexGuard<'_, List> {
136
+ self.list.lock().unwrap_or_else(PoisonError::into_inner)
137
+ }
138
+
139
+ /// Append the header `name` with `value`, once `to_field_name` and
140
+ /// `to_field_value` take them and the list keeps within `MAX_FIELD_SIZE`
141
+ /// and the Invocation's memory.
142
+ pub fn append(&self, name: &str, value: Vec<u8>) -> Result<(), FieldError> {
143
+ let name = to_field_name(name)?;
144
+ let value = to_field_value(value)?;
145
+ let room = room_of(&name, &value);
146
+ let mut list = self.list();
147
+ let grown = list.room.bytes() + room;
148
+ if grown > MAX_FIELD_SIZE {
149
+ return Err(FieldError::TooLarge);
150
+ }
151
+ list.room
152
+ .resize(grown)
153
+ .map_err(|_| FieldError::OutOfMemory)?;
154
+ list.entries.push((name, value));
155
+ Ok(())
156
+ }
157
+
158
+ /// Every entry, in the order appended.
159
+ pub fn entries(&self) -> Vec<(String, Vec<u8>)> {
160
+ self.list().entries.clone()
161
+ }
162
+ }
163
+
164
+ /// Whether `text` is an HTTP token, as a method or a header name must be.
165
+ pub fn is_token(text: &str) -> bool {
166
+ !text.is_empty()
167
+ && text
168
+ .chars()
169
+ .all(|c| c.is_ascii_alphanumeric() || "!#$%&'*+-.^_`|~".contains(c))
170
+ }
171
+
172
+ /// Whether `text` is a URI scheme, as hyper's `Scheme` parses one.
173
+ pub fn is_scheme(text: &str) -> bool {
174
+ text.parse::<hyper::http::uri::Scheme>().is_ok()
175
+ }
176
+
177
+ /// The header name a guest may set, lowercase: a token, and none the seam
178
+ /// writes itself.
179
+ pub fn to_field_name(name: &str) -> Result<String, FieldError> {
180
+ if !is_token(name) {
181
+ return Err(FieldError::InvalidSyntax);
182
+ }
183
+ let name = name.to_ascii_lowercase();
184
+ match FORBIDDEN_HEADERS.contains(&name.as_str()) {
185
+ true => Err(FieldError::Forbidden),
186
+ false => Ok(name),
187
+ }
188
+ }
189
+
190
+ /// The header value a guest may set: one an HTTP field value holds, with no
191
+ /// control byte but a tab, as hyper's `HeaderValue` and wasmtime take one.
192
+ pub fn to_field_value(value: Vec<u8>) -> Result<Vec<u8>, FieldError> {
193
+ match hyper::header::HeaderValue::from_bytes(&value) {
194
+ Ok(_) => Ok(value),
195
+ Err(_) => Err(FieldError::InvalidSyntax),
196
+ }
197
+ }
198
+
199
+ /// Whether `authority` names a host alone, with no path or user part, and a
200
+ /// port, when it names one, as a number that fits, as wasmtime's
201
+ /// `parse_authority` asks.
202
+ pub fn is_authority(authority: &str) -> bool {
203
+ if authority.contains('@') {
204
+ return false;
205
+ }
206
+ let has_port = match authority.rfind(']') {
207
+ Some(at) => authority[at..].contains(':'),
208
+ None => authority.contains(':'),
209
+ };
210
+ hyper::http::uri::Authority::try_from(authority)
211
+ .is_ok_and(|parsed| !has_port || parsed.port_u16().is_some())
212
+ }
213
+
214
+ /// The host and port an authority names, the port defaulting by `scheme`;
215
+ /// `None` for one `is_authority` refuses. One parser reads every authority,
216
+ /// so the authority a request is judged by is the one it reaches.
217
+ pub(crate) fn split_authority(authority: &str, scheme: Scheme) -> Option<(String, u16)> {
218
+ if !is_authority(authority) {
219
+ return None;
220
+ }
221
+ let parsed = hyper::http::uri::Authority::try_from(authority).ok()?;
222
+ let host = parsed.host().trim_start_matches('[').trim_end_matches(']');
223
+ (!host.is_empty()).then(|| {
224
+ (
225
+ host.to_ascii_lowercase(),
226
+ parsed.port_u16().unwrap_or(default_port(scheme)),
227
+ )
228
+ })
229
+ }
230
+
231
+ /// The authority a request names its server by, from the `host` and `port`
232
+ /// it was judged for: the port only when it is not `scheme`'s default, as
233
+ /// RFC 9110's `Host` field writes one.
234
+ pub(crate) fn join_authority(host: &str, port: u16, scheme: Scheme) -> String {
235
+ match port == default_port(scheme) {
236
+ true => host.to_string(),
237
+ false => format!("{host}:{port}"),
238
+ }
239
+ }
240
+
241
+ /// The port `scheme` reaches when an authority names none.
242
+ fn default_port(scheme: Scheme) -> u16 {
243
+ match scheme {
244
+ Scheme::Http => 80,
245
+ Scheme::Https => 443,
246
+ }
247
+ }
248
+
249
+ /// `path` as the request target sent in origin form, as wasmtime takes one:
250
+ /// `""` is `/` and a query alone sits on `/`; anything else not starting with
251
+ /// `/`, or no URI path holds, is none. A fragment is cut, as the target sent
252
+ /// carries none, so every rule judges what the server reads.
253
+ pub fn to_origin_form(path: &str) -> Option<String> {
254
+ let target = match path.chars().next() {
255
+ None => "/".to_string(),
256
+ Some('?') => format!("/{path}"),
257
+ Some('/') => path.to_string(),
258
+ Some(_) => return None,
259
+ };
260
+ hyper::http::uri::PathAndQuery::try_from(target)
261
+ .ok()
262
+ .map(|target| target.as_str().to_string())
263
+ }
264
+
265
+ /// One request the guest sends: its method, scheme, authority, path with its
266
+ /// query, headers, and body.
267
+ #[derive(Clone, Debug, PartialEq, Eq)]
268
+ #[non_exhaustive]
269
+ pub struct OutgoingRequest {
270
+ pub method: String,
271
+ pub scheme: Scheme,
272
+ pub authority: String,
273
+ pub path_with_query: String,
274
+ pub headers: Vec<(String, Vec<u8>)>,
275
+ pub body: Vec<u8>,
276
+ pub timeouts: Timeouts,
277
+ /// The credentials bound to this request, swapped in only by the default
278
+ /// outbound seam and only on the way out, so no other seam reads a secret.
279
+ pub(crate) swaps: Vec<Swap>,
280
+ }
281
+
282
+ /// How long a request waits at each step, as `wasi:http`'s `request-options`
283
+ /// names them; `None` waits `DEFAULT_TIMEOUT`. Each bounds its own wait, as
284
+ /// wasmtime-wasi-http's does.
285
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
286
+ #[non_exhaustive]
287
+ pub struct Timeouts {
288
+ /// For the connect to the server, answered as `ConnectionTimeout`.
289
+ pub connect: Option<Duration>,
290
+ /// For the response's head once connected, answered as
291
+ /// `ConnectionReadTimeout`.
292
+ pub first_byte: Option<Duration>,
293
+ /// For each part of the body after the last, failing the body's read as
294
+ /// `ETIMEDOUT`.
295
+ pub between_bytes: Option<Duration>,
296
+ }
297
+
298
+ /// How long a wait the request sets no timeout for lasts, as
299
+ /// wasmtime-wasi-http's `default_send_request` bounds each, so a server that
300
+ /// never answers ends the wait even where the run itself has no deadline.
301
+ const DEFAULT_TIMEOUT: Duration = Duration::from_secs(600);
302
+
303
+ /// The bound a wait takes from its timeout, `DEFAULT_TIMEOUT` where none is
304
+ /// set.
305
+ fn bound(timeout: Option<Duration>) -> Duration {
306
+ timeout.unwrap_or(DEFAULT_TIMEOUT)
307
+ }
308
+
309
+ impl OutgoingRequest {
310
+ pub fn new(method: impl Into<String>, scheme: Scheme, authority: impl Into<String>) -> Self {
311
+ Self {
312
+ method: method.into(),
313
+ scheme,
314
+ authority: authority.into(),
315
+ path_with_query: "/".to_string(),
316
+ headers: Vec::new(),
317
+ body: Vec::new(),
318
+ timeouts: Timeouts::default(),
319
+ swaps: Vec::new(),
320
+ }
321
+ }
322
+
323
+ /// The bytes its method, authority, path, and headers hold, as a header
324
+ /// list counts its entries.
325
+ fn room(&self) -> usize {
326
+ self.method.len()
327
+ + self.authority.len()
328
+ + self.path_with_query.len()
329
+ + total_room_of(&self.headers)
330
+ }
331
+ }
332
+
333
+ /// The body a guest writes for a request, its bytes held on the guest's behalf
334
+ /// against the Invocation's memory, as `vm_committed_as` counts a mapping, so a
335
+ /// guest cannot grow the host past what it was allotted.
336
+ pub struct RequestBody {
337
+ cred: Arc<Cred>,
338
+ written: Mutex<WrittenBody>,
339
+ }
340
+
341
+ /// What a body holds and the room it takes.
342
+ struct WrittenBody {
343
+ bytes: Vec<u8>,
344
+ room: MemoryCharge,
345
+ }
346
+
347
+ impl RequestBody {
348
+ /// An empty body, its bytes charged to `cred`.
349
+ pub fn new(cred: Arc<Cred>) -> Arc<Self> {
350
+ let room = MemoryCharge::empty(cred.memory_budget());
351
+ Arc::new(Self {
352
+ cred,
353
+ written: Mutex::new(WrittenBody {
354
+ bytes: Vec::new(),
355
+ room,
356
+ }),
357
+ })
358
+ }
359
+
360
+ fn written(&self) -> MutexGuard<'_, WrittenBody> {
361
+ self.written.lock().unwrap_or_else(PoisonError::into_inner)
362
+ }
363
+
364
+ /// Allocate the file the guest writes the body through, as
365
+ /// `anon_inode_getfile` does; it is always ready for a write.
366
+ pub fn alloc_file(self: &Arc<Self>) -> File {
367
+ let how = OpenHow {
368
+ write: true,
369
+ nonblock: true,
370
+ ..OpenHow::default()
371
+ };
372
+ anon_inode_getfile(Box::new(RequestBodyFile(self.clone())), &how, &self.cred)
373
+ }
374
+
375
+ /// Append `buf`, refused past the Invocation's memory as `ENOMEM`.
376
+ fn write(&self, buf: &[u8]) -> Result<usize, Errno> {
377
+ let mut written = self.written();
378
+ let grown = written.bytes.len() + buf.len();
379
+ written.room.resize(grown)?;
380
+ written.bytes.extend_from_slice(buf);
381
+ Ok(buf.len())
382
+ }
383
+
384
+ /// Take what was written, given back to the Invocation's memory, as the
385
+ /// exchange that sends it reserves it again.
386
+ pub fn take(&self) -> Vec<u8> {
387
+ let mut written = self.written();
388
+ let bytes = std::mem::take(&mut written.bytes);
389
+ written.room = MemoryCharge::empty(self.cred.memory_budget());
390
+ bytes
391
+ }
392
+ }
393
+
394
+ /// What answers for a request body's file.
395
+ struct RequestBodyFile(Arc<RequestBody>);
396
+
397
+ impl FileOperations for RequestBodyFile {
398
+ fn write_iter(&mut self, _iocb: &mut Kiocb, buf: &[u8]) -> Result<usize, Errno> {
399
+ self.0.write(buf)
400
+ }
401
+
402
+ fn poll(&self, _mode: FMode, _table: Option<&mut PollTable>) -> PollMask {
403
+ PollMask::OUT
404
+ }
405
+
406
+ fn is_stream(&self) -> bool {
407
+ true
408
+ }
409
+ }
410
+
411
+ /// Why a request has no response, as WASI's `wasi:http` error codes name each
412
+ /// a client may meet; none carries host text.
413
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
414
+ #[non_exhaustive]
415
+ pub enum HttpError {
416
+ /// No entry lets the guest send it.
417
+ Denied,
418
+ /// The authority's name has no address, or no local address could be
419
+ /// assigned to reach it, as wasmtime-wasi-http reads EADDRNOTAVAIL.
420
+ DnsError,
421
+ /// No route reaches the authority's address.
422
+ DestinationUnavailable,
423
+ /// The authority refused the connection.
424
+ ConnectionRefused,
425
+ /// The connection did not open in time.
426
+ ConnectionTimeout,
427
+ /// The response's head did not arrive in time.
428
+ ConnectionReadTimeout,
429
+ /// The connection ended before the response did.
430
+ ConnectionTerminated,
431
+ /// The response was not one HTTP can read.
432
+ ProtocolError,
433
+ /// The request cannot start now, as a full cap leaves it.
434
+ ConnectionLimitReached,
435
+ /// The server's certificate is not one the host's store trusts.
436
+ TlsCertificate,
437
+ /// The server ended TLS with the alert of this id.
438
+ TlsAlertReceived(u8),
439
+ /// TLS could not be agreed with the server.
440
+ TlsProtocol,
441
+ /// The seam failed in a way no other code names.
442
+ Internal,
443
+ }
444
+
445
+ /// A response's head: its status and headers.
446
+ #[derive(Clone, Debug, PartialEq, Eq)]
447
+ pub struct ResponseHead {
448
+ pub status: u16,
449
+ pub headers: Vec<(String, Vec<u8>)>,
450
+ }
451
+
452
+ /// A response's body as the seam streams it.
453
+ pub type ResponseBody = UnsyncBoxBody<Bytes, HttpError>;
454
+
455
+ /// What the outbound seam answers: the head, and the body to read.
456
+ pub struct Response {
457
+ pub head: ResponseHead,
458
+ pub body: ResponseBody,
459
+ }
460
+
461
+ /// What the outbound seam's send runs as on the reactor.
462
+ pub type ResponseFuture = Pin<Box<dyn Future<Output = Result<Response, HttpError>> + Send>>;
463
+
464
+ /// The HTTP sender, as wasmtime's `send_request`. It dials only through the
465
+ /// connector it is handed, so every rule judges where it goes; one dialing
466
+ /// any other way would skip them, so no Host App hands one in. The future
467
+ /// runs on kobako's reactor, so it must never block.
468
+ pub(crate) trait Outbound: Send + Sync {
469
+ /// Send `request` through `connector`.
470
+ fn send_request(&self, request: OutgoingRequest, connector: Connector) -> ResponseFuture;
471
+ }
472
+
473
+ /// What a timeout ends: the request, failed as the error names, or the body's
474
+ /// next read.
475
+ #[derive(Clone, Copy)]
476
+ enum Expiry {
477
+ Fail(HttpError),
478
+ Body,
479
+ }
480
+
481
+ #[derive(Clone, Debug, PartialEq, Eq)]
482
+ enum Phase {
483
+ Sending,
484
+ Answered(ResponseHead),
485
+ Failed(HttpError),
486
+ }
487
+
488
+ struct State {
489
+ phase: Phase,
490
+ /// What the response's head holds, reserved against the Invocation's
491
+ /// memory as it arrives and given back with the head, as Linux uncharges
492
+ /// an object's memory only when the object is freed.
493
+ head_memory: Option<MemoryCharge>,
494
+ body: VecDeque<u8>,
495
+ body_ended: bool,
496
+ body_failed: bool,
497
+ /// The body waited past its timeout for its next part.
498
+ body_timed_out: bool,
499
+ /// When the head or the body's last part arrived.
500
+ last_part: Option<Instant>,
501
+ /// The task waiting for room in the body's buffer.
502
+ filler: Option<Waker>,
503
+ }
504
+
505
+ impl State {
506
+ /// Whether the body holds nothing yet and may still bring a part: it
507
+ /// neither ended, failed, nor waited past its timeout.
508
+ fn is_awaiting_body(&self) -> bool {
509
+ self.body.is_empty() && !self.body_ended && !self.body_failed && !self.body_timed_out
510
+ }
511
+ }
512
+
513
+ /// One request in flight, then its response: the head once it arrives, and
514
+ /// the body as the task reads it.
515
+ pub struct Exchange {
516
+ state: Arc<Mutex<State>>,
517
+ wait: Arc<WaitQueueHead>,
518
+ task: Mutex<Option<Arc<Task>>>,
519
+ /// The request's place against its Sandbox's cap and the process's.
520
+ place: Mutex<Option<Arc<Place>>>,
521
+ /// What the request, the seam's read buffer, and the response's buffer
522
+ /// hold, reserved against the Invocation's memory until the exchange is
523
+ /// released.
524
+ memory: Mutex<Option<MemoryCharge>>,
525
+ timeouts: Timeouts,
526
+ started: Instant,
527
+ /// When the connector's connect opened, which starts the wait for the
528
+ /// head.
529
+ connected: Arc<OnceLock<Instant>>,
530
+ }
531
+
532
+ impl Exchange {
533
+ /// Send `request` through `seam` on the reactor, dialing through the
534
+ /// connector `connector_for` makes with the request's place; it counts
535
+ /// against `open_connections` and the process until
536
+ /// released and its connection closed, and a full count answers
537
+ /// `ConnectionLimitReached`. The
538
+ /// request, the seam's read buffer, and the response's buffer count
539
+ /// against `cred`'s memory until then.
540
+ pub(crate) fn start(
541
+ seam: Arc<dyn Outbound>,
542
+ request: OutgoingRequest,
543
+ connector_for: impl FnOnce(Arc<Place>) -> Connector,
544
+ open_connections: &Arc<OpenConnections>,
545
+ cred: &Arc<Cred>,
546
+ ) -> Result<Arc<Self>, HttpError> {
547
+ let room = request.room() + request.body.len() + MAX_READ_BUFFER + MAX_SOCKET_BUFFER;
548
+ let memory =
549
+ MemoryCharge::new(cred.memory_budget(), room).map_err(|_| HttpError::Internal)?;
550
+ let place = Arc::new(open(open_connections).ok_or(HttpError::ConnectionLimitReached)?);
551
+ let mut connector = connector_for(place.clone());
552
+ let place = Some(place);
553
+ let reactor = Reactor::current().map_err(|_| HttpError::Internal)?;
554
+ let exchange = Arc::new(Self {
555
+ state: Arc::new(Mutex::new(State {
556
+ phase: Phase::Sending,
557
+ head_memory: None,
558
+ body: VecDeque::new(),
559
+ body_ended: false,
560
+ body_failed: false,
561
+ body_timed_out: false,
562
+ last_part: None,
563
+ filler: None,
564
+ })),
565
+ wait: Arc::default(),
566
+ task: Mutex::new(None),
567
+ place: Mutex::new(place),
568
+ memory: Mutex::new(Some(memory)),
569
+ timeouts: request.timeouts,
570
+ started: Instant::now(),
571
+ connected: connector.connected.clone(),
572
+ });
573
+ // The task holds what it answers into, never the exchange, so the
574
+ // exchange's last hold, which cancels the task, is never the task's.
575
+ let (state, wait) = (exchange.state.clone(), exchange.wait.clone());
576
+ let cred = cred.clone();
577
+ connector.wait = Some(wait.clone());
578
+ let sending = seam.send_request(request, connector);
579
+ let task = reactor.spawn(async move {
580
+ match sending.await {
581
+ Ok(response) => {
582
+ let room = total_room_of(&response.head.headers);
583
+ let Ok(head_memory) = MemoryCharge::new(cred.memory_budget(), room) else {
584
+ lock(&state).phase = Phase::Failed(HttpError::Internal);
585
+ wait.wake_up();
586
+ return;
587
+ };
588
+ {
589
+ let mut answered = lock(&state);
590
+ // A head arriving past its timeout finds the exchange
591
+ // failed already.
592
+ if answered.phase != Phase::Sending {
593
+ return;
594
+ }
595
+ answered.head_memory = Some(head_memory);
596
+ answered.phase = Phase::Answered(response.head);
597
+ answered.last_part = Some(Instant::now());
598
+ }
599
+ wait.wake_up();
600
+ fill_body(&state, &wait, response.body).await;
601
+ }
602
+ Err(err) => {
603
+ lock(&state).phase = Phase::Failed(err);
604
+ wait.wake_up();
605
+ }
606
+ }
607
+ });
608
+ *exchange.task.lock().unwrap_or_else(PoisonError::into_inner) = Some(task);
609
+ Ok(exchange)
610
+ }
611
+
612
+ /// The exchange's state, once any timeout that passed has ended its wait,
613
+ /// as a timeout read on its next look rather than by a timer.
614
+ fn state(&self) -> MutexGuard<'_, State> {
615
+ let mut state = lock(&self.state);
616
+ if self.expire(&mut state) {
617
+ drop(state);
618
+ self.cancel_task();
619
+ state = lock(&self.state);
620
+ }
621
+ state
622
+ }
623
+
624
+ /// Drop the task sending the request, with the connection it holds.
625
+ fn cancel_task(&self) {
626
+ let task = self
627
+ .task
628
+ .lock()
629
+ .unwrap_or_else(PoisonError::into_inner)
630
+ .take();
631
+ if let Some(task) = task {
632
+ task.cancel();
633
+ }
634
+ }
635
+
636
+ /// The soonest a timeout ends the wait under way: the connect until it
637
+ /// opens, then the head, then each part of the body; `None` while nothing
638
+ /// is awaited.
639
+ pub fn deadline(&self) -> Option<Instant> {
640
+ self.deadline_of(&lock(&self.state)).map(|(at, _)| at)
641
+ }
642
+
643
+ fn deadline_of(&self, state: &State) -> Option<(Instant, Expiry)> {
644
+ match state.phase {
645
+ Phase::Sending => match self.connected.get() {
646
+ None => Some((
647
+ self.started + bound(self.timeouts.connect),
648
+ Expiry::Fail(HttpError::ConnectionTimeout),
649
+ )),
650
+ Some(at) => Some((
651
+ *at + bound(self.timeouts.first_byte),
652
+ Expiry::Fail(HttpError::ConnectionReadTimeout),
653
+ )),
654
+ },
655
+ Phase::Answered(_) if state.is_awaiting_body() => {
656
+ let after = bound(self.timeouts.between_bytes);
657
+ state.last_part.map(|at| (at + after, Expiry::Body))
658
+ }
659
+ Phase::Answered(_) | Phase::Failed(_) => None,
660
+ }
661
+ }
662
+
663
+ /// End the wait under way once its timeout passed, answering whether it
664
+ /// did, so the task sending the request can be let go.
665
+ fn expire(&self, state: &mut State) -> bool {
666
+ let Some((at, expiry)) = self.deadline_of(state) else {
667
+ return false;
668
+ };
669
+ if Instant::now() < at {
670
+ return false;
671
+ }
672
+ match expiry {
673
+ Expiry::Fail(err) => state.phase = Phase::Failed(err),
674
+ Expiry::Body => state.body_timed_out = true,
675
+ }
676
+ true
677
+ }
678
+
679
+ /// The response's head, or why it has none; `None` while the request is
680
+ /// under way.
681
+ pub fn response(&self) -> Option<Result<ResponseHead, HttpError>> {
682
+ match &self.state().phase {
683
+ Phase::Sending => None,
684
+ Phase::Answered(head) => Some(Ok(head.clone())),
685
+ Phase::Failed(err) => Some(Err(*err)),
686
+ }
687
+ }
688
+
689
+ /// Allocate the file a poll finds readable once the response's head
690
+ /// arrives or the request fails, as `anon_inode_getfile` does.
691
+ pub fn alloc_response_file(self: &Arc<Self>, cred: &Arc<Cred>) -> File {
692
+ let how = OpenHow {
693
+ read: true,
694
+ nonblock: true,
695
+ ..OpenHow::default()
696
+ };
697
+ anon_inode_getfile(Box::new(ResponseFile(self.clone())), &how, cred)
698
+ }
699
+
700
+ /// Allocate the file the response's body reads from, as
701
+ /// `anon_inode_getfile` does.
702
+ pub fn alloc_body_file(self: &Arc<Self>, cred: &Arc<Cred>) -> File {
703
+ let how = OpenHow {
704
+ read: true,
705
+ nonblock: true,
706
+ ..OpenHow::default()
707
+ };
708
+ anon_inode_getfile(Box::new(BodyFile(self.clone())), &how, cred)
709
+ }
710
+
711
+ /// Read what the body holds into `buf`: zero at its end, `WouldBlock`
712
+ /// while it holds nothing yet, `ConnectionReset` for a body cut short, and
713
+ /// `TimedOut` once its next part waited past its timeout.
714
+ fn read(&self, buf: &mut [u8]) -> Result<usize, Errno> {
715
+ let mut state = self.state();
716
+ if state.body.is_empty() {
717
+ return match (state.body_ended, state.body_failed, state.body_timed_out) {
718
+ (_, true, _) => Err(Errno::ConnectionReset),
719
+ (_, _, true) => Err(Errno::TimedOut),
720
+ (true, _, _) => Ok(0),
721
+ _ => Err(Errno::WouldBlock),
722
+ };
723
+ }
724
+ let n = buf.len().min(state.body.len());
725
+ for (slot, byte) in buf.iter_mut().zip(state.body.drain(..n)) {
726
+ *slot = byte;
727
+ }
728
+ let filler = state.filler.take();
729
+ drop(state);
730
+ if let Some(filler) = filler {
731
+ filler.wake();
732
+ }
733
+ Ok(n)
734
+ }
735
+
736
+ /// Let the exchange go, dropping its task with the connection it holds
737
+ /// and the body's buffer. The head stays readable, and held against the
738
+ /// Invocation's memory, while the guest keeps the exchange.
739
+ pub fn release(&self) {
740
+ self.cancel_task();
741
+ lock(&self.state).body = VecDeque::new();
742
+ *self.place.lock().unwrap_or_else(PoisonError::into_inner) = None;
743
+ *self.memory.lock().unwrap_or_else(PoisonError::into_inner) = None;
744
+ }
745
+ }
746
+
747
+ /// The last hold on an exchange lets it go, as the last `fput` of a socket's
748
+ /// file releases it, so no exchange outlives what the guest holds of it.
749
+ impl Drop for Exchange {
750
+ fn drop(&mut self) {
751
+ self.release();
752
+ }
753
+ }
754
+
755
+ /// What answers for the file an exchange's response is awaited on.
756
+ struct ResponseFile(Arc<Exchange>);
757
+
758
+ impl FileOperations for ResponseFile {
759
+ fn poll(&self, _mode: FMode, table: Option<&mut PollTable>) -> PollMask {
760
+ poll_wait(&self.0.wait, table);
761
+ match self.0.state().phase {
762
+ Phase::Sending => PollMask::default(),
763
+ Phase::Answered(_) | Phase::Failed(_) => PollMask::IN,
764
+ }
765
+ }
766
+
767
+ fn is_stream(&self) -> bool {
768
+ true
769
+ }
770
+ }
771
+
772
+ /// What answers for the file an exchange's body is read from.
773
+ struct BodyFile(Arc<Exchange>);
774
+
775
+ impl FileOperations for BodyFile {
776
+ fn read_iter(&mut self, _iocb: &mut Kiocb, buf: &mut [u8]) -> Result<usize, Errno> {
777
+ self.0.read(buf)
778
+ }
779
+
780
+ fn poll(&self, _mode: FMode, table: Option<&mut PollTable>) -> PollMask {
781
+ poll_wait(&self.0.wait, table);
782
+ let state = self.0.state();
783
+ let settled = !matches!(state.phase, Phase::Sending);
784
+ match settled && !state.is_awaiting_body() {
785
+ true => PollMask::IN,
786
+ false => PollMask::default(),
787
+ }
788
+ }
789
+
790
+ fn is_stream(&self) -> bool {
791
+ true
792
+ }
793
+ }
794
+
795
+ fn lock(state: &Mutex<State>) -> MutexGuard<'_, State> {
796
+ state.lock().unwrap_or_else(PoisonError::into_inner)
797
+ }
798
+
799
+ /// Move `body` into the exchange's buffer as room frees, never past the
800
+ /// buffer, until the body ends or fails, or the exchange lets the task go;
801
+ /// what a frame holds past the room waits here.
802
+ async fn fill_body(state: &Mutex<State>, wait: &WaitQueueHead, mut body: ResponseBody) {
803
+ let mut pending = Bytes::new();
804
+ loop {
805
+ if pending.is_empty() {
806
+ match poll_fn(|cx| Pin::new(&mut body).poll_frame(cx)).await {
807
+ Some(Ok(frame)) => {
808
+ pending = frame.into_data().unwrap_or_default();
809
+ continue;
810
+ }
811
+ Some(Err(_)) => lock(state).body_failed = true,
812
+ None => lock(state).body_ended = true,
813
+ }
814
+ wait.wake_up();
815
+ return;
816
+ }
817
+ let room = poll_fn(|cx| {
818
+ let mut state = lock(state);
819
+ let room = MAX_SOCKET_BUFFER - state.body.len();
820
+ if room > 0 {
821
+ return Poll::Ready(room);
822
+ }
823
+ state.filler = Some(cx.waker().clone());
824
+ Poll::Pending
825
+ })
826
+ .await;
827
+ let moved = pending.split_to(room.min(pending.len()));
828
+ let mut filled = lock(state);
829
+ filled.body.extend(&moved[..]);
830
+ filled.last_part = Some(Instant::now());
831
+ drop(filled);
832
+ wait.wake_up();
833
+ }
834
+ }
835
+
836
+ /// The outbound seam: hyper's HTTP/1 client, with no redirect, proxy, or
837
+ /// decompression, every connection opened by kobako's connector and TLS ended
838
+ /// on the host. It holds its own TLS configuration, so its sessions end with
839
+ /// the Invocation that holds it.
840
+ pub struct HyperOutbound {
841
+ tls: Option<Arc<rustls::ClientConfig>>,
842
+ }
843
+
844
+ impl HyperOutbound {
845
+ pub fn new() -> Self {
846
+ Self {
847
+ tls: new_platform_tls(),
848
+ }
849
+ }
850
+ }
851
+
852
+ impl Default for HyperOutbound {
853
+ fn default() -> Self {
854
+ Self::new()
855
+ }
856
+ }
857
+
858
+ impl Outbound for HyperOutbound {
859
+ fn send_request(&self, request: OutgoingRequest, connector: Connector) -> ResponseFuture {
860
+ let tls = match request.scheme {
861
+ Scheme::Http => None,
862
+ Scheme::Https => match self.tls.clone() {
863
+ Some(config) => Some(config),
864
+ None => return Box::pin(async { Err(HttpError::TlsProtocol) }),
865
+ },
866
+ };
867
+ Box::pin(async move {
868
+ let http = to_hyper_request(request)?;
869
+ let connection = connector.dial().await?;
870
+ match tls {
871
+ None => send_over(Wire(connection), http).await,
872
+ Some(config) => {
873
+ let name = rustls::pki_types::ServerName::try_from(connector.authority().0)
874
+ .map_err(|_| HttpError::ProtocolError)?;
875
+ let session = tokio_rustls::TlsConnector::from(config)
876
+ .connect(name, connection)
877
+ .await
878
+ .map_err(|err| HttpError::from_tls(&err))?;
879
+ send_over(Wire(session), http).await
880
+ }
881
+ }
882
+ })
883
+ }
884
+ }
885
+
886
+ /// `request` as hyper sends it: the path with its query as the target, a
887
+ /// `host` header naming the authority, and each header with its credentials
888
+ /// swapped in.
889
+ fn to_hyper_request(request: OutgoingRequest) -> Result<hyper::Request<Full<Bytes>>, HttpError> {
890
+ let mut builder = hyper::Request::builder()
891
+ .method(request.method.as_str())
892
+ .uri(request.path_with_query.as_str())
893
+ .header("host", request.authority.as_str());
894
+ for (name, value) in &request.headers {
895
+ let value = request
896
+ .swaps
897
+ .iter()
898
+ .fold(value.clone(), |value, swap| swap.apply(name, &value));
899
+ builder = builder.header(name.as_str(), value);
900
+ }
901
+ builder
902
+ .body(Full::new(Bytes::from(request.body)))
903
+ .map_err(|_| HttpError::ProtocolError)
904
+ }
905
+
906
+ /// Send `request` over `wire` as HTTP/1, driving the connection beside it, and
907
+ /// answer its response, whose body drives the connection on until it ends.
908
+ async fn send_over<T>(
909
+ wire: Wire<T>,
910
+ request: hyper::Request<Full<Bytes>>,
911
+ ) -> Result<Response, HttpError>
912
+ where
913
+ T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
914
+ {
915
+ let (mut sender, connection) = hyper::client::conn::http1::Builder::new()
916
+ .max_buf_size(MAX_READ_BUFFER)
917
+ .handshake(wire)
918
+ .await
919
+ .map_err(|err| HttpError::from_hyper(&err))?;
920
+ let mut connection: ConnectionTask = Some(Box::pin(async move {
921
+ let _ = connection.await;
922
+ }));
923
+ let mut sending = Box::pin(sender.send_request(request));
924
+ let response = poll_fn(|cx| {
925
+ drive(&mut connection, cx);
926
+ sending.as_mut().poll(cx)
927
+ })
928
+ .await
929
+ .map_err(|err| HttpError::from_hyper(&err))?;
930
+ let head = ResponseHead {
931
+ status: response.status().as_u16(),
932
+ headers: response
933
+ .headers()
934
+ .iter()
935
+ .map(|(name, value)| (name.as_str().to_string(), value.as_bytes().to_vec()))
936
+ .collect(),
937
+ };
938
+ let body = DrivenBody {
939
+ body: response.into_body(),
940
+ connection,
941
+ };
942
+ Ok(Response {
943
+ head,
944
+ body: body.boxed_unsync(),
945
+ })
946
+ }
947
+
948
+ /// The connection's own future, until it ends.
949
+ type ConnectionTask = Option<Pin<Box<dyn Future<Output = ()> + Send>>>;
950
+
951
+ fn drive(connection: &mut ConnectionTask, cx: &mut Context<'_>) {
952
+ if let Some(running) = connection.as_mut() {
953
+ if running.as_mut().poll(cx).is_ready() {
954
+ *connection = None;
955
+ }
956
+ }
957
+ }
958
+
959
+ /// A response's body, driving its connection as it is read.
960
+ struct DrivenBody {
961
+ body: hyper::body::Incoming,
962
+ connection: ConnectionTask,
963
+ }
964
+
965
+ impl Body for DrivenBody {
966
+ type Data = Bytes;
967
+ type Error = HttpError;
968
+
969
+ fn poll_frame(
970
+ self: Pin<&mut Self>,
971
+ cx: &mut Context<'_>,
972
+ ) -> Poll<Option<Result<http_body::Frame<Bytes>, HttpError>>> {
973
+ let this = self.get_mut();
974
+ drive(&mut this.connection, cx);
975
+ Pin::new(&mut this.body)
976
+ .poll_frame(cx)
977
+ .map_err(|_| HttpError::ConnectionTerminated)
978
+ }
979
+ }
980
+
981
+ /// A connection, plain or under TLS, as hyper reads and writes it.
982
+ struct Wire<T>(T);
983
+
984
+ impl<T: AsyncRead + Unpin> hyper::rt::Read for Wire<T> {
985
+ fn poll_read(
986
+ self: Pin<&mut Self>,
987
+ cx: &mut Context<'_>,
988
+ mut buf: hyper::rt::ReadBufCursor<'_>,
989
+ ) -> Poll<io::Result<()>> {
990
+ // SAFETY: `ReadBuf` only writes initialized bytes into the cursor's
991
+ // spare capacity, and `advance` counts no more than it wrote.
992
+ let filled = unsafe {
993
+ let mut read = ReadBuf::uninit(buf.as_mut());
994
+ match Pin::new(&mut self.get_mut().0).poll_read(cx, &mut read) {
995
+ Poll::Ready(Ok(())) => read.filled().len(),
996
+ other => return other,
997
+ }
998
+ };
999
+ unsafe { buf.advance(filled) };
1000
+ Poll::Ready(Ok(()))
1001
+ }
1002
+ }
1003
+
1004
+ impl<T: AsyncWrite + Unpin> hyper::rt::Write for Wire<T> {
1005
+ fn poll_write(
1006
+ self: Pin<&mut Self>,
1007
+ cx: &mut Context<'_>,
1008
+ buf: &[u8],
1009
+ ) -> Poll<io::Result<usize>> {
1010
+ Pin::new(&mut self.get_mut().0).poll_write(cx, buf)
1011
+ }
1012
+
1013
+ fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
1014
+ Pin::new(&mut self.get_mut().0).poll_flush(cx)
1015
+ }
1016
+
1017
+ fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
1018
+ Pin::new(&mut self.get_mut().0).poll_shutdown(cx)
1019
+ }
1020
+ }
1021
+
1022
+ impl HttpError {
1023
+ /// The code a failed TLS handshake answers, as wasmtime-wasi-http's
1024
+ /// `p2_error_from_tls` names each: the rustls error it carries, a
1025
+ /// connection the server ended as `connection-terminated`, and any other
1026
+ /// as `TLS-protocol-error`; memory the socket could not reserve is the
1027
+ /// seam's own failure.
1028
+ pub(crate) fn from_tls(err: &io::Error) -> Self {
1029
+ match err
1030
+ .get_ref()
1031
+ .and_then(|inner| inner.downcast_ref::<rustls::Error>())
1032
+ {
1033
+ Some(rustls::Error::InvalidCertificate(_)) => return Self::TlsCertificate,
1034
+ Some(rustls::Error::AlertReceived(alert)) => {
1035
+ return Self::TlsAlertReceived(u8::from(*alert))
1036
+ }
1037
+ Some(_) => return Self::TlsProtocol,
1038
+ None => {}
1039
+ }
1040
+ if err.kind() == io::ErrorKind::UnexpectedEof {
1041
+ return Self::ConnectionTerminated;
1042
+ }
1043
+ Self::from_open_connection(&Errno::from_io_error(err)).unwrap_or(Self::TlsProtocol)
1044
+ }
1045
+
1046
+ /// The code an open connection failing with `err` answers: one the peer or
1047
+ /// the Invocation's end cut as `connection-terminated`, and memory the
1048
+ /// socket could not reserve as the seam's own failure; `None` for a
1049
+ /// failure the protocol over it names.
1050
+ fn from_open_connection(err: &Errno) -> Option<Self> {
1051
+ match err {
1052
+ Errno::ConnectionReset | Errno::ConnectionAborted | Errno::BrokenPipe => {
1053
+ Some(Self::ConnectionTerminated)
1054
+ }
1055
+ Errno::OutOfMemory => Some(Self::Internal),
1056
+ _ => None,
1057
+ }
1058
+ }
1059
+
1060
+ /// The code a connection failing with `err` answers, as wasmtime-wasi-http
1061
+ /// names each; memory the socket could not reserve is the seam's own
1062
+ /// failure.
1063
+ pub(crate) fn from_errno(err: &Errno) -> Self {
1064
+ match err {
1065
+ Errno::OutOfMemory => Self::Internal,
1066
+ Errno::TimedOut => Self::ConnectionTimeout,
1067
+ Errno::HostUnreachable
1068
+ | Errno::NetworkUnreachable
1069
+ | Errno::NetworkDown
1070
+ | Errno::AddressFamilyNotSupported => Self::DestinationUnavailable,
1071
+ Errno::ConnectionReset | Errno::ConnectionAborted => Self::ConnectionTerminated,
1072
+ Errno::AddressNotAvailable => Self::DnsError,
1073
+ _ => Self::ConnectionRefused,
1074
+ }
1075
+ }
1076
+
1077
+ /// The code a failed exchange answers. Its connection failing under it
1078
+ /// answers as an open connection's failure does, as the TLS handshake's
1079
+ /// does, so a failure answers one code wherever in the exchange it lands.
1080
+ fn from_hyper(err: &hyper::Error) -> Self {
1081
+ let failed = std::error::Error::source(err)
1082
+ .and_then(|source| source.downcast_ref::<io::Error>())
1083
+ .and_then(io::Error::get_ref)
1084
+ .and_then(|inner| inner.downcast_ref::<CarriedErrno>());
1085
+ if let Some(code) = failed.and_then(|CarriedErrno(errno)| Self::from_open_connection(errno))
1086
+ {
1087
+ return code;
1088
+ }
1089
+ if err.is_parse() || err.is_parse_status() {
1090
+ Self::ProtocolError
1091
+ } else if err.is_timeout() {
1092
+ Self::ConnectionTimeout
1093
+ } else if err.is_incomplete_message() || err.is_closed() || err.is_canceled() {
1094
+ Self::ConnectionTerminated
1095
+ } else {
1096
+ Self::Internal
1097
+ }
1098
+ }
1099
+ }
1100
+
1101
+ #[cfg(test)]
1102
+ mod tests {
1103
+ use std::io::{BufRead, BufReader, Read, Write};
1104
+ use std::net::TcpListener;
1105
+ use std::thread;
1106
+ use std::time::Duration;
1107
+
1108
+ use std::net::SocketAddr;
1109
+
1110
+ use super::super::family::testing::{CountsDials, StallsLookups};
1111
+ use super::*;
1112
+ use crate::net::{HostInet, Namespace, OpenConnections, ProtocolFamily};
1113
+ use crate::network::Protocol;
1114
+ use crate::network::{Builder, Endpoint, Network};
1115
+ use crate::security::prepare_creds;
1116
+
1117
+ // @behavior NW-225
1118
+ #[test]
1119
+ fn a_failed_dial_answers_the_code_its_host_error_names() {
1120
+ let dials = [
1121
+ io::ErrorKind::TimedOut,
1122
+ io::ErrorKind::ConnectionRefused,
1123
+ io::ErrorKind::ConnectionReset,
1124
+ io::ErrorKind::ConnectionAborted,
1125
+ io::ErrorKind::HostUnreachable,
1126
+ io::ErrorKind::NetworkUnreachable,
1127
+ io::ErrorKind::NetworkDown,
1128
+ io::ErrorKind::AddrNotAvailable,
1129
+ ]
1130
+ .map(|kind| HttpError::from_errno(&Errno::from_io_error(&io::Error::from(kind))));
1131
+ assert_eq!(
1132
+ dials,
1133
+ [
1134
+ HttpError::ConnectionTimeout,
1135
+ HttpError::ConnectionRefused,
1136
+ HttpError::ConnectionTerminated,
1137
+ HttpError::ConnectionTerminated,
1138
+ HttpError::DestinationUnavailable,
1139
+ HttpError::DestinationUnavailable,
1140
+ HttpError::DestinationUnavailable,
1141
+ HttpError::DnsError,
1142
+ ],
1143
+ "a dial that timed out, was refused, reset, aborted, found no route to its host or \
1144
+ network, or had no local address must answer connection-timeout, \
1145
+ connection-refused, connection-terminated twice, destination-unavailable three \
1146
+ times, and DNS-error, as wasmtime-wasi-http names each"
1147
+ );
1148
+ }
1149
+
1150
+ // @behavior NW-225
1151
+ #[test]
1152
+ fn a_dial_to_a_host_down_answers_connection_refused() {
1153
+ use rustix::io::Errno as HostErrno;
1154
+ #[cfg(target_os = "linux")]
1155
+ let downs = [
1156
+ ("EHOSTDOWN", HostErrno::HOSTDOWN),
1157
+ ("ENONET", HostErrno::NONET),
1158
+ ];
1159
+ #[cfg(not(target_os = "linux"))]
1160
+ let downs = [("EHOSTDOWN", HostErrno::HOSTDOWN)];
1161
+ for (name, errno) in downs {
1162
+ let err = io::Error::from_raw_os_error(errno.raw_os_error());
1163
+ assert_eq!(
1164
+ HttpError::from_errno(&Errno::from_io_error(&err)),
1165
+ HttpError::ConnectionRefused,
1166
+ "a dial failing with {name} must answer connection-refused, as \
1167
+ wasmtime-wasi-http answers an errno std names no kind for"
1168
+ );
1169
+ }
1170
+ }
1171
+
1172
+ // @behavior NW-225
1173
+ #[test]
1174
+ fn a_failed_tls_handshake_answers_the_code_wasmtime_wasi_http_gives_it() {
1175
+ let alert = io::Error::new(
1176
+ io::ErrorKind::InvalidData,
1177
+ rustls::Error::AlertReceived(rustls::AlertDescription::HandshakeFailure),
1178
+ );
1179
+ let handshakes = [
1180
+ io::ErrorKind::UnexpectedEof,
1181
+ io::ErrorKind::ConnectionReset,
1182
+ io::ErrorKind::BrokenPipe,
1183
+ io::ErrorKind::InvalidData,
1184
+ io::ErrorKind::TimedOut,
1185
+ ]
1186
+ .map(|kind| HttpError::from_tls(&io::Error::from(kind)));
1187
+ assert_eq!(
1188
+ (handshakes, HttpError::from_tls(&alert)),
1189
+ (
1190
+ [
1191
+ HttpError::ConnectionTerminated,
1192
+ HttpError::ConnectionTerminated,
1193
+ HttpError::ConnectionTerminated,
1194
+ HttpError::TlsProtocol,
1195
+ HttpError::TlsProtocol,
1196
+ ],
1197
+ HttpError::TlsAlertReceived(40)
1198
+ ),
1199
+ "a TLS handshake the server ended by EOF, reset, or a broken pipe must answer \
1200
+ connection-terminated, bytes TLS could not read or any other failure \
1201
+ TLS-protocol-error, and a handshake_failure alert TLS-alert-received with its id, as \
1202
+ wasmtime-wasi-http's p2_error_from_tls names each"
1203
+ );
1204
+ }
1205
+
1206
+ /// A loopback server answering one request with `hello`, handing back the
1207
+ /// request line and headers it read.
1208
+ fn serve_once() -> (u16, thread::JoinHandle<Vec<String>>) {
1209
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
1210
+ let port = listener.local_addr().unwrap().port();
1211
+ let server = thread::spawn(move || {
1212
+ let (stream, _) = listener.accept().unwrap();
1213
+ let mut reader = BufReader::new(stream.try_clone().unwrap());
1214
+ let mut head = Vec::new();
1215
+ loop {
1216
+ let mut line = String::new();
1217
+ reader.read_line(&mut line).unwrap();
1218
+ if line.trim_end().is_empty() {
1219
+ break;
1220
+ }
1221
+ head.push(line.trim_end().to_string());
1222
+ }
1223
+ (&stream)
1224
+ .write_all(b"HTTP/1.1 200 OK\r\ncontent-length: 5\r\nx-kind: test\r\n\r\nhello")
1225
+ .unwrap();
1226
+ head
1227
+ });
1228
+ (port, server)
1229
+ }
1230
+
1231
+ /// A namespace whose one entry holds `endpoints`.
1232
+ fn namespace(endpoints: Vec<Endpoint>) -> Namespace {
1233
+ let mut entry = Builder::default();
1234
+ endpoints.into_iter().for_each(|endpoint| {
1235
+ entry.endpoint(endpoint);
1236
+ });
1237
+ let network = Network::new("web", Arc::new(entry)).unwrap();
1238
+ Namespace::for_network(&network, Arc::new(HostInet), Arc::new(HyperOutbound::new()))
1239
+ }
1240
+
1241
+ fn greeting_request(authority: &str) -> OutgoingRequest {
1242
+ let mut request = OutgoingRequest::new("GET", Scheme::Http, authority);
1243
+ request.path_with_query = "/greeting?lang=en".to_string();
1244
+ request
1245
+ }
1246
+
1247
+ /// The exchange's head, then its whole body.
1248
+ fn read_response(exchange: &Exchange) -> Result<(ResponseHead, Vec<u8>), HttpError> {
1249
+ let head = loop {
1250
+ match exchange.response() {
1251
+ Some(answer) => break answer?,
1252
+ None => thread::sleep(Duration::from_millis(1)),
1253
+ }
1254
+ };
1255
+ let mut body = Vec::new();
1256
+ let mut buf = [0; 64];
1257
+ loop {
1258
+ match exchange.read(&mut buf) {
1259
+ Ok(0) => return Ok((head, body)),
1260
+ Ok(n) => body.extend_from_slice(&buf[..n]),
1261
+ Err(Errno::WouldBlock) => thread::sleep(Duration::from_millis(1)),
1262
+ Err(_) => return Err(HttpError::ConnectionTerminated),
1263
+ }
1264
+ }
1265
+ }
1266
+
1267
+ // @behavior NW-225
1268
+ #[test]
1269
+ fn a_request_to_a_name_with_no_address_answers_dns_error_undialed() {
1270
+ let mut entry = Builder::default();
1271
+ entry.endpoint(Endpoint::new("missing.test", 80).unwrap());
1272
+ let network = Network::new("web", Arc::new(entry)).unwrap();
1273
+ // A Mock answers a name it holds no route for with no address.
1274
+ let mock = Arc::new(crate::net::Mock::default());
1275
+ let namespace = Namespace::for_network(
1276
+ &network,
1277
+ mock.clone(),
1278
+ Arc::new(HyperOutbound { tls: None }),
1279
+ );
1280
+ let exchange = crate::net::send(
1281
+ &namespace,
1282
+ &prepare_creds(&[], None),
1283
+ greeting_request("missing.test"),
1284
+ )
1285
+ .expect("the endpoint admits it");
1286
+ assert_eq!(
1287
+ (read_response(&exchange).err(), mock.dialed_addresses()),
1288
+ (Some(HttpError::DnsError), Vec::new()),
1289
+ "a request to a name its resolver finds no address for must answer DNS-error, as \
1290
+ wasmtime-wasi-http answers a failed lookup, and dial nothing"
1291
+ );
1292
+ }
1293
+
1294
+ #[test]
1295
+ fn an_https_request_without_trust_roots_dials_nothing() {
1296
+ let mut entry = Builder::default();
1297
+ entry.endpoint(Endpoint::new("127.0.0.1", 443).unwrap());
1298
+ let network = Network::new("web", Arc::new(entry)).unwrap();
1299
+ let family = Arc::new(CountsDials::default());
1300
+ let namespace = Namespace::for_network(
1301
+ &network,
1302
+ family.clone(),
1303
+ Arc::new(HyperOutbound { tls: None }),
1304
+ );
1305
+ let cred = prepare_creds(&[], None);
1306
+ let mut request = OutgoingRequest::new("GET", Scheme::Https, "127.0.0.1:443");
1307
+ request.path_with_query = "/".to_string();
1308
+ let exchange =
1309
+ crate::net::send(&namespace, &cred, request).expect("the endpoint admits it");
1310
+ let answer = loop {
1311
+ match exchange.response() {
1312
+ Some(answer) => break answer,
1313
+ None => thread::sleep(Duration::from_millis(1)),
1314
+ }
1315
+ };
1316
+ assert_eq!(
1317
+ (answer.err(), family.dials()),
1318
+ (Some(HttpError::TlsProtocol), 0),
1319
+ "an https request through a seam with no trust roots must answer TlsProtocol before \
1320
+ dialing the host"
1321
+ );
1322
+ }
1323
+
1324
+ // @behavior NW-022
1325
+ #[test]
1326
+ fn a_released_requests_upstream_still_sending_holds_its_place() {
1327
+ // A released connection's queue only shrinks, so one still holding
1328
+ // bytes after the second request was sent held them while it was
1329
+ // sent. A try whose queue drained first proves nothing and goes again.
1330
+ let attempt = || {
1331
+ // TCP fixes a connection's window scale in its handshake and never
1332
+ // shrinks a window it offered, so only a receive buffer set before
1333
+ // listening keeps what the peer takes to a few kilobytes.
1334
+ let listener = socket2::Socket::new(socket2::Domain::IPV4, socket2::Type::STREAM, None)
1335
+ .expect("a loopback socket");
1336
+ listener.set_recv_buffer_size(4096).unwrap();
1337
+ listener
1338
+ .bind(&SocketAddr::from(([127, 0, 0, 1], 0)).into())
1339
+ .expect("a loopback listener");
1340
+ listener.listen(1).unwrap();
1341
+ let listener = std::net::TcpListener::from(listener);
1342
+ let port = listener.local_addr().unwrap().port();
1343
+ let mut entry = Builder::default();
1344
+ entry.endpoint(Endpoint::new("127.0.0.1", port).unwrap());
1345
+ let network = Network::new("web", Arc::new(entry)).unwrap();
1346
+ let namespace = Namespace::new(
1347
+ [&network],
1348
+ &crate::net::Backends::new(Arc::new(HostInet), Arc::new(HostInet)),
1349
+ OpenConnections::new(Some(1)),
1350
+ None,
1351
+ )
1352
+ .expect("the endpoints compile")
1353
+ .with_outbound(Arc::new(HyperOutbound::new()));
1354
+ let cred = prepare_creds(&[], None);
1355
+ let authority = format!("127.0.0.1:{port}");
1356
+ let mut request = OutgoingRequest::new("POST", Scheme::Http, &authority);
1357
+ request.path_with_query = "/".to_string();
1358
+ // Linux sizes a send buffer as the connection opens, from its
1359
+ // MSS, up to `tcp_wmem`'s default most of 4 MiB, so a body past
1360
+ // that always leaves bytes in the upstream's queue.
1361
+ request.body = vec![b'b'; 8 * 1024 * 1024];
1362
+ let exchange =
1363
+ crate::net::send(&namespace, &cred, request).expect("the endpoint admits it");
1364
+ // The peer accepts and never reads, so the request's bytes stay queued.
1365
+ let (peer, _) = listener.accept().expect("the listener accepts");
1366
+ // A send buffer set by hand stops growing, so once the peer's
1367
+ // window is full the host takes no more of the body.
1368
+ let upstreams = namespace.tcp_sockets().held();
1369
+ for upstream in &upstreams {
1370
+ upstream.lock_host_send_buffer(4096);
1371
+ }
1372
+ let give_up = Instant::now() + Duration::from_secs(5);
1373
+ while upstreams.iter().all(|upstream| upstream.is_drained()) {
1374
+ assert!(
1375
+ Instant::now() < give_up,
1376
+ "the request's body never queued on its upstream in five seconds"
1377
+ );
1378
+ thread::sleep(Duration::from_millis(1));
1379
+ }
1380
+ exchange.release();
1381
+ let while_sending =
1382
+ crate::net::send(&namespace, &cred, greeting_request(&authority)).err();
1383
+ let still_sending = upstreams.iter().any(|upstream| !upstream.is_drained());
1384
+ drop(peer);
1385
+ let deadline = Instant::now() + Duration::from_secs(5);
1386
+ let once_closed = loop {
1387
+ match crate::net::send(&namespace, &cred, greeting_request(&authority)) {
1388
+ Ok(_) => break true,
1389
+ Err(_) if Instant::now() < deadline => thread::sleep(Duration::from_millis(1)),
1390
+ Err(_) => break false,
1391
+ }
1392
+ };
1393
+ still_sending.then_some((while_sending, once_closed))
1394
+ };
1395
+ assert_eq!(
1396
+ (0..5).find_map(|_| attempt()),
1397
+ Some((Some(HttpError::ConnectionLimitReached), true)),
1398
+ "a request past an open_connection_limit of 1 must answer ConnectionLimitReached \
1399
+ while a released request's connection still sends its queued body, and send once \
1400
+ that connection closed"
1401
+ );
1402
+ }
1403
+
1404
+ /// A host refusing every dial, whose resolver records the thread each
1405
+ /// query starts on and never answers.
1406
+ #[derive(Default)]
1407
+ struct RecordsResolveThread(Mutex<Vec<thread::ThreadId>>);
1408
+
1409
+ impl ProtocolFamily for RecordsResolveThread {
1410
+ fn connect(&self, _addr: SocketAddr) -> io::Result<std::net::TcpStream> {
1411
+ Err(io::ErrorKind::ConnectionRefused.into())
1412
+ }
1413
+ }
1414
+
1415
+ impl crate::net::Resolver for RecordsResolveThread {
1416
+ fn resolve(&self, _name: &str) -> crate::net::Resolving {
1417
+ self.0.lock().unwrap().push(thread::current().id());
1418
+ Box::pin(std::future::pending())
1419
+ }
1420
+ }
1421
+
1422
+ // The reactor never makes the call a Resolver may block on: a request's
1423
+ // lookup starts on the thread that sends it.
1424
+ #[test]
1425
+ fn a_requests_lookup_starts_on_the_thread_that_sends_it() {
1426
+ let mut entry = Builder::default();
1427
+ entry.endpoint(Endpoint::new("api.example.com", 80).unwrap());
1428
+ let network = Network::new("web", Arc::new(entry)).unwrap();
1429
+ let family = Arc::new(RecordsResolveThread::default());
1430
+ let namespace = Namespace::new(
1431
+ [&network],
1432
+ &crate::net::Backends::new(family.clone(), family.clone()),
1433
+ OpenConnections::new(None),
1434
+ None,
1435
+ )
1436
+ .expect("the endpoints compile");
1437
+ let cred = prepare_creds(&[], None);
1438
+ let exchange = crate::net::send(&namespace, &cred, greeting_request("api.example.com"))
1439
+ .expect("the endpoint admits it");
1440
+ thread::sleep(Duration::from_millis(50));
1441
+ let started_on = family.0.lock().unwrap().clone();
1442
+ exchange.release();
1443
+ assert_eq!(
1444
+ started_on,
1445
+ vec![thread::current().id()],
1446
+ "a request's lookup must start once, on the thread that sends the request, never \
1447
+ on the reactor"
1448
+ );
1449
+ }
1450
+
1451
+ // @behavior NW-085
1452
+ #[test]
1453
+ fn a_released_requests_lookup_is_cancelled_and_gives_its_place_back_at_once() {
1454
+ let (started, resolving) = std::sync::mpsc::channel();
1455
+ let mut entry = Builder::default();
1456
+ entry.endpoint(Endpoint::new("api.example.com", 80).unwrap());
1457
+ let network = Network::new("web", Arc::new(entry)).unwrap();
1458
+ let family = Arc::new(StallsLookups::new(started));
1459
+ let namespace = Namespace::new(
1460
+ [&network],
1461
+ &crate::net::Backends::new(family.clone(), family.clone()),
1462
+ OpenConnections::new(Some(1)),
1463
+ None,
1464
+ )
1465
+ .expect("the endpoints compile");
1466
+ let cred = prepare_creds(&[], None);
1467
+ let first = crate::net::send(&namespace, &cred, greeting_request("api.example.com"))
1468
+ .expect("the first request has a place");
1469
+ resolving
1470
+ .recv_timeout(Duration::from_secs(5))
1471
+ .expect("the request's lookup starts");
1472
+ first.release();
1473
+ let next = crate::net::send(&namespace, &cred, greeting_request("api.example.com"));
1474
+ assert_eq!(
1475
+ (next.is_ok(), family.cancelled()),
1476
+ (true, 1),
1477
+ "with an open_connection_limit of 1, a request sent after one released while its \
1478
+ authority still resolves must take the place at once, the released request's query \
1479
+ cancelled though the resolver never answered"
1480
+ );
1481
+ }
1482
+
1483
+ // @behavior NW-022
1484
+ #[test]
1485
+ fn a_lookup_or_a_request_holding_the_last_place_makes_a_connect_answer_eagain() {
1486
+ let (started, resolving) = std::sync::mpsc::channel();
1487
+ let mut entry = Builder::default();
1488
+ entry.endpoint(Endpoint::new("api.example.com", 80).unwrap());
1489
+ entry.endpoint(
1490
+ Endpoint::new("127.0.0.1", 4567)
1491
+ .unwrap()
1492
+ .with_protocol(Protocol::Tcp),
1493
+ );
1494
+ let network = Network::new("web", Arc::new(entry)).unwrap();
1495
+ let family = Arc::new(StallsLookups::new(started));
1496
+ let namespace = Namespace::new(
1497
+ [&network],
1498
+ &crate::net::Backends::new(family.clone(), family),
1499
+ OpenConnections::new(Some(1)),
1500
+ None,
1501
+ )
1502
+ .expect("the endpoints compile");
1503
+ let cred = prepare_creds(&[], None);
1504
+ let connect = || {
1505
+ let sock =
1506
+ crate::net::TcpSock::create(&namespace, &cred, crate::net::AddressFamily::Inet)?;
1507
+ let addr = "127.0.0.1:4567".parse().ok();
1508
+ crate::net::sys_connect(&namespace, &cred, sock.as_ref(), addr)
1509
+ };
1510
+ let lookup = crate::net::resolve(&namespace, "api.example.com").expect("a lookup starts");
1511
+ resolving
1512
+ .recv_timeout(Duration::from_secs(5))
1513
+ .expect("the guest's lookup reaches the resolver");
1514
+ let past_lookup = connect().err();
1515
+ drop(lookup);
1516
+ let deadline = std::time::Instant::now() + Duration::from_secs(5);
1517
+ let request = loop {
1518
+ match crate::net::send(&namespace, &cred, greeting_request("api.example.com")) {
1519
+ Ok(request) => break request,
1520
+ Err(_) if std::time::Instant::now() < deadline => {
1521
+ thread::sleep(Duration::from_millis(1))
1522
+ }
1523
+ Err(err) => panic!("the lookup's place never came back: {err:?}"),
1524
+ }
1525
+ };
1526
+ resolving
1527
+ .recv_timeout(Duration::from_secs(5))
1528
+ .expect("the request's lookup reaches the resolver");
1529
+ let past_request = connect().err();
1530
+ drop(request);
1531
+ assert_eq!(
1532
+ (past_lookup, past_request),
1533
+ (Some(Errno::WouldBlock), Some(Errno::WouldBlock)),
1534
+ "a socket's connect must answer EAGAIN while a guest's lookup, or a request's, holds \
1535
+ the last place of an open_connection_limit of 1"
1536
+ );
1537
+ }
1538
+
1539
+ // @behavior NW-196
1540
+ #[test]
1541
+ fn a_request_dials_through_a_socket_of_the_invocations_own() {
1542
+ let (port, server) = serve_once();
1543
+ let seen = Arc::new(Mutex::new(Vec::new()));
1544
+ let mut entry = Builder::default();
1545
+ entry.endpoint(
1546
+ Endpoint::new("localhost", port)
1547
+ .unwrap()
1548
+ .with_addresses(vec![crate::network::Addresses::Local]),
1549
+ );
1550
+ let recorded = seen.clone();
1551
+ entry.connect_policy(move |connection: &crate::network::Connection<'_>| {
1552
+ recorded.lock().unwrap().push(connection.host.to_string());
1553
+ true
1554
+ });
1555
+ let network = Network::new("web", Arc::new(entry)).unwrap();
1556
+ let namespace =
1557
+ Namespace::for_network(&network, Arc::new(HostInet), Arc::new(HyperOutbound::new()));
1558
+ let cred_within = |limit| {
1559
+ crate::security::restrict_self(
1560
+ &prepare_creds(&[], limit),
1561
+ [&crate::grant::Grant::Network(network.clone())],
1562
+ )
1563
+ .expect("every Mount stands")
1564
+ };
1565
+ let request = || greeting_request(&format!("localhost:{port}"));
1566
+ let cramped = crate::net::send(
1567
+ &namespace,
1568
+ &cred_within(Some(MAX_READ_BUFFER + 2 * MAX_SOCKET_BUFFER)),
1569
+ request(),
1570
+ )
1571
+ .map(|exchange| read_response(&exchange).map(|(head, _)| head.status));
1572
+ let roomy = crate::net::send(&namespace, &cred_within(None), request())
1573
+ .map(|exchange| read_response(&exchange).map(|(head, _)| head.status));
1574
+ assert_eq!(
1575
+ (cramped, roomy, seen.lock().unwrap().clone()),
1576
+ (
1577
+ Ok(Err(HttpError::Internal)),
1578
+ Ok(Ok(200)),
1579
+ vec!["localhost".to_string(), "localhost".to_string()]
1580
+ ),
1581
+ "a guest's request must fail before it reaches the server when the Invocation's \
1582
+ memory cannot hold its upstream socket's buffers, reach it with room, and show the \
1583
+ connect policy only the host the guest named, never an address it resolved to"
1584
+ );
1585
+ server.join().unwrap();
1586
+ }
1587
+
1588
+ // @behavior NW-037
1589
+ #[test]
1590
+ fn a_request_to_an_http_endpoint_reads_back_the_response() {
1591
+ let (port, server) = serve_once();
1592
+ let namespace = namespace(vec![Endpoint::new("localhost", port)
1593
+ .unwrap()
1594
+ .with_addresses(vec![crate::network::Addresses::Local])]);
1595
+ let cred = prepare_creds(&[], None);
1596
+ let exchange = crate::net::send(
1597
+ &namespace,
1598
+ &cred,
1599
+ greeting_request(&format!("LocalHost:{port}")),
1600
+ )
1601
+ .expect("the endpoint names the authority");
1602
+ let (head, body) = read_response(&exchange).expect("the server answers");
1603
+ let seen = server.join().unwrap();
1604
+ assert!(
1605
+ head.status == 200
1606
+ && head
1607
+ .headers
1608
+ .contains(&("x-kind".to_string(), b"test".to_vec()))
1609
+ && body == b"hello"
1610
+ && seen[0] == "GET /greeting?lang=en HTTP/1.1",
1611
+ "a guest's request to an http endpoint must send the request line and read back the \
1612
+ status, headers, and body, got {head:?} {body:?} after {seen:?}"
1613
+ );
1614
+ }
1615
+
1616
+ /// A seam recording the authority each request it is handed names, and
1617
+ /// answering none.
1618
+ #[derive(Default)]
1619
+ struct RecordsAuthority(Mutex<Vec<String>>);
1620
+
1621
+ impl Outbound for RecordsAuthority {
1622
+ fn send_request(&self, request: OutgoingRequest, _connector: Connector) -> ResponseFuture {
1623
+ self.0.lock().unwrap().push(request.authority);
1624
+ Box::pin(async { Err(HttpError::Internal) })
1625
+ }
1626
+ }
1627
+
1628
+ // @behavior NW-199
1629
+ #[test]
1630
+ fn a_request_names_its_server_as_the_endpoint_judged_it() {
1631
+ let (port, server) = serve_once();
1632
+ let namespace = namespace(vec![Endpoint::new("localhost", port)
1633
+ .unwrap()
1634
+ .with_addresses(vec![crate::network::Addresses::Local])]);
1635
+ let cred = prepare_creds(&[], None);
1636
+ let exchange = crate::net::send(
1637
+ &namespace,
1638
+ &cred,
1639
+ greeting_request(&format!("LocalHost.:{port}")),
1640
+ )
1641
+ .expect("the endpoint names the authority");
1642
+ read_response(&exchange).expect("the server answers");
1643
+ let seen = server.join().unwrap();
1644
+ let mut entry = Builder::default();
1645
+ entry.endpoint(
1646
+ Endpoint::new("localhost", 80)
1647
+ .unwrap()
1648
+ .with_addresses(vec![crate::network::Addresses::Local]),
1649
+ );
1650
+ let network = Network::new("web", Arc::new(entry)).unwrap();
1651
+ let recorded = Arc::new(RecordsAuthority::default());
1652
+ let at_default = Namespace::for_network(&network, Arc::new(HostInet), recorded.clone());
1653
+ let _ = crate::net::send(&at_default, &cred, greeting_request("LOCALHOST:80"));
1654
+ assert_eq!(
1655
+ (
1656
+ seen.iter()
1657
+ .find(|line| line.to_ascii_lowercase().starts_with("host:"))
1658
+ .cloned(),
1659
+ recorded.0.lock().unwrap().clone()
1660
+ ),
1661
+ (
1662
+ Some(format!("host: localhost:{port}")),
1663
+ vec!["localhost".to_string()]
1664
+ ),
1665
+ "a guest's request to an authority spelled in another case, with a trailing dot, or \
1666
+ with its scheme's default port must name its server as the endpoint judged it: \
1667
+ lowercase, no trailing dot, and no default port"
1668
+ );
1669
+ }
1670
+
1671
+ // @behavior NW-204
1672
+ #[test]
1673
+ fn a_response_head_past_the_read_buffer_answers_a_protocol_error() {
1674
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
1675
+ let port = listener.local_addr().unwrap().port();
1676
+ let server = thread::spawn(move || {
1677
+ let (stream, _) = listener.accept().unwrap();
1678
+ let mut reader = BufReader::new(stream.try_clone().unwrap());
1679
+ let mut line = String::new();
1680
+ while reader.read_line(&mut line).unwrap() > 2 {
1681
+ line.clear();
1682
+ }
1683
+ let long = "v".repeat(MAX_FIELD_SIZE * 2);
1684
+ let _ =
1685
+ (&stream).write_all(format!("HTTP/1.1 200 OK\r\nx-long: {long}\r\n").as_bytes());
1686
+ });
1687
+ let namespace = namespace(vec![Endpoint::new("127.0.0.1", port)
1688
+ .unwrap()
1689
+ .with_addresses(vec![crate::network::Addresses::Local])]);
1690
+ let cred = prepare_creds(&[], None);
1691
+ let answer = crate::net::send(
1692
+ &namespace,
1693
+ &cred,
1694
+ greeting_request(&format!("127.0.0.1:{port}")),
1695
+ )
1696
+ .expect("the endpoint names the authority");
1697
+ let tight = prepare_creds(&[], Some(MAX_READ_BUFFER + MAX_SOCKET_BUFFER));
1698
+ let unheld = crate::net::send(
1699
+ &namespace,
1700
+ &tight,
1701
+ greeting_request(&format!("127.0.0.1:{port}")),
1702
+ )
1703
+ .err();
1704
+ assert_eq!(
1705
+ (read_response(&answer).err(), unheld),
1706
+ (Some(HttpError::ProtocolError), Some(HttpError::Internal)),
1707
+ "a response whose head runs past 128 KiB must answer a protocol error, and a request \
1708
+ whose Invocation cannot hold the seam's read buffer beside the request must fail \
1709
+ before it is sent"
1710
+ );
1711
+ server.join().unwrap();
1712
+ }
1713
+
1714
+ // @behavior NW-203
1715
+ #[test]
1716
+ fn a_requests_method_is_judged_as_it_is_sent() {
1717
+ use crate::network::{Action, HttpRule};
1718
+ let seen = Arc::new(Mutex::new(Vec::new()));
1719
+ let recorded = seen.clone();
1720
+ let mut entry = Builder::default();
1721
+ entry
1722
+ .endpoint(
1723
+ Endpoint::new("127.0.0.1", 80)
1724
+ .unwrap()
1725
+ .with_addresses(vec![crate::network::Addresses::Local])
1726
+ .with_rules(vec![
1727
+ HttpRule::new(Action::Allow, Some("GET"), "/greeting"),
1728
+ HttpRule::new(Action::Deny, Some("DELETE"), "/greeting"),
1729
+ HttpRule::new(Action::Allow, Some("delete"), "/greeting"),
1730
+ ]),
1731
+ )
1732
+ .endpoint(
1733
+ Endpoint::new("127.0.0.1", 81)
1734
+ .unwrap()
1735
+ .with_addresses(vec![crate::network::Addresses::Local]),
1736
+ )
1737
+ .request_policy(move |sent: &crate::network::Request<'_>| {
1738
+ recorded.lock().unwrap().push(sent.method.to_string());
1739
+ true
1740
+ });
1741
+ let network = Network::new("web", Arc::new(entry)).unwrap();
1742
+ let namespace = Namespace::for_network(
1743
+ &network,
1744
+ Arc::new(HostInet),
1745
+ Arc::new(RecordsAuthority::default()),
1746
+ );
1747
+ let cred = prepare_creds(&[], None);
1748
+ let send = |method: &str, authority: &str| {
1749
+ let mut request = greeting_request(authority);
1750
+ request.method = method.to_string();
1751
+ crate::net::send(&namespace, &cred, request).is_ok()
1752
+ };
1753
+ let sent = [
1754
+ send("GET", "127.0.0.1"),
1755
+ send("get", "127.0.0.1"),
1756
+ send("delete", "127.0.0.1"),
1757
+ send("get", "127.0.0.1:81"),
1758
+ ];
1759
+ assert_eq!(
1760
+ (sent, seen.lock().unwrap().clone()),
1761
+ (
1762
+ [true, false, false, true],
1763
+ vec!["GET".to_string(), "get".to_string()]
1764
+ ),
1765
+ "a request by get must match no rule allowing GET, a request by delete must stay \
1766
+ refused by a rule denying DELETE, and the request policy must see get as sent"
1767
+ );
1768
+ }
1769
+
1770
+ // @behavior NW-201
1771
+ #[test]
1772
+ fn an_authority_spelling_an_address_short_is_judged_and_named_as_that_address() {
1773
+ let mut entry = Builder::default();
1774
+ entry.endpoint(
1775
+ Endpoint::new("127.0.0.1", 80)
1776
+ .unwrap()
1777
+ .with_addresses(vec![crate::network::Addresses::Local]),
1778
+ );
1779
+ let network = Network::new("web", Arc::new(entry)).unwrap();
1780
+ let recorded = Arc::new(RecordsAuthority::default());
1781
+ let namespace = Namespace::for_network(&network, Arc::new(HostInet), recorded.clone());
1782
+ let cred = prepare_creds(&[], None);
1783
+ let sent = crate::net::send(&namespace, &cred, greeting_request("127.1")).is_ok();
1784
+ assert_eq!(
1785
+ (sent, recorded.0.lock().unwrap().clone()),
1786
+ (true, vec!["127.0.0.1".to_string()]),
1787
+ "a guest's request to the authority 127.1 must pass an endpoint naming 127.0.0.1, \
1788
+ and name its server as that address"
1789
+ );
1790
+ }
1791
+
1792
+ // @behavior NW-122
1793
+ #[test]
1794
+ fn an_http_connection_sends_without_nagles_delay() {
1795
+ /// The host's network, keeping a handle on each stream it dials.
1796
+ #[derive(Default)]
1797
+ struct KeepsStreams(Mutex<Vec<std::net::TcpStream>>);
1798
+ impl ProtocolFamily for KeepsStreams {
1799
+ fn connect(&self, addr: SocketAddr) -> io::Result<std::net::TcpStream> {
1800
+ let stream = HostInet.connect(addr)?;
1801
+ self.0.lock().unwrap().push(stream.try_clone()?);
1802
+ Ok(stream)
1803
+ }
1804
+ }
1805
+ impl crate::net::Resolver for KeepsStreams {
1806
+ fn resolve(&self, name: &str) -> crate::net::Resolving {
1807
+ crate::net::Resolver::resolve(&HostInet, name)
1808
+ }
1809
+ }
1810
+ let (port, server) = serve_once();
1811
+ let mut entry = Builder::default();
1812
+ entry.endpoint(
1813
+ Endpoint::new("127.0.0.1", port)
1814
+ .unwrap()
1815
+ .with_addresses(vec![crate::network::Addresses::Local]),
1816
+ );
1817
+ let network = Network::new("web", Arc::new(entry)).unwrap();
1818
+ let family = Arc::new(KeepsStreams::default());
1819
+ let namespace =
1820
+ Namespace::for_network(&network, family.clone(), Arc::new(HyperOutbound::new()));
1821
+ let cred = prepare_creds(&[], None);
1822
+ let exchange = crate::net::send(
1823
+ &namespace,
1824
+ &cred,
1825
+ greeting_request(&format!("127.0.0.1:{port}")),
1826
+ )
1827
+ .expect("the endpoint names the authority");
1828
+ read_response(&exchange).expect("the server answers");
1829
+ server.join().unwrap();
1830
+ let nodelay = family
1831
+ .0
1832
+ .lock()
1833
+ .unwrap()
1834
+ .iter()
1835
+ .map(|stream| stream.nodelay().unwrap())
1836
+ .collect::<Vec<_>>();
1837
+ assert_eq!(
1838
+ nodelay,
1839
+ [true],
1840
+ "an HTTP request sent through the Protocol Family must turn TCP_NODELAY on for the \
1841
+ stream it dialed, as an HTTP client writes each request whole"
1842
+ );
1843
+ }
1844
+
1845
+ // @behavior NW-058
1846
+ #[test]
1847
+ fn a_body_past_the_buffer_streams_as_it_is_read() {
1848
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
1849
+ let port = listener.local_addr().unwrap().port();
1850
+ let size = MAX_SOCKET_BUFFER * 4;
1851
+ let server = thread::spawn(move || {
1852
+ let (mut stream, _) = listener.accept().unwrap();
1853
+ let mut head = [0; 1024];
1854
+ let _ = stream.read(&mut head);
1855
+ let header = format!("HTTP/1.1 200 OK\r\ncontent-length: {size}\r\n\r\n");
1856
+ stream.write_all(header.as_bytes()).unwrap();
1857
+ let body = (0..size).map(|i| (i % 251) as u8).collect::<Vec<_>>();
1858
+ stream.write_all(&body).unwrap();
1859
+ });
1860
+ let namespace = namespace(vec![Endpoint::new("127.0.0.1", port).unwrap()]);
1861
+ let cred = prepare_creds(&[], None);
1862
+ let exchange = crate::net::send(
1863
+ &namespace,
1864
+ &cred,
1865
+ greeting_request(&format!("127.0.0.1:{port}")),
1866
+ )
1867
+ .expect("an endpoint names the authority");
1868
+ let mut held = 0;
1869
+ let mut body = Vec::new();
1870
+ let mut buf = [0; 4096];
1871
+ let started = std::time::Instant::now();
1872
+ while body.len() < size && started.elapsed() < Duration::from_secs(10) {
1873
+ held = held.max(exchange.state().body.len());
1874
+ match exchange.read(&mut buf) {
1875
+ Ok(0) => break,
1876
+ Ok(n) => body.extend_from_slice(&buf[..n]),
1877
+ Err(_) => thread::sleep(Duration::from_millis(1)),
1878
+ }
1879
+ }
1880
+ server.join().unwrap();
1881
+ let expected = (0..size).map(|i| (i % 251) as u8).collect::<Vec<_>>();
1882
+ assert!(
1883
+ body == expected && held <= MAX_SOCKET_BUFFER,
1884
+ "a response body four buffers long must reach the guest whole and in order, the body \
1885
+ waiting for it never past one buffer, got {} bytes, holding up to {held}",
1886
+ body.len()
1887
+ );
1888
+ }
1889
+
1890
+ // @behavior NW-038
1891
+ #[test]
1892
+ fn a_request_no_http_endpoint_names_is_denied_unsent() {
1893
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
1894
+ listener.set_nonblocking(true).unwrap();
1895
+ let port = listener.local_addr().unwrap().port();
1896
+ let raw = Endpoint::new("127.0.0.1", port)
1897
+ .unwrap()
1898
+ .with_protocol(Protocol::Tcp);
1899
+ let namespace = namespace(vec![raw]);
1900
+ let cred = prepare_creds(&[], None);
1901
+ let answers = [
1902
+ format!("127.0.0.1:{port}"),
1903
+ format!("elsewhere.example:{port}"),
1904
+ format!("198.18.0.0:{port}"),
1905
+ ]
1906
+ .map(|authority| crate::net::send(&namespace, &cred, greeting_request(&authority)).err());
1907
+ thread::sleep(Duration::from_millis(20));
1908
+ assert!(
1909
+ answers
1910
+ .iter()
1911
+ .all(|answer| *answer == Some(HttpError::Denied))
1912
+ && listener.accept().is_err(),
1913
+ "a guest's request to an authority only a tcp endpoint names, one no endpoint names, \
1914
+ or a virtual address must answer Denied, sending nothing, got {answers:?}"
1915
+ );
1916
+ }
1917
+
1918
+ // @behavior NW-041
1919
+ #[test]
1920
+ fn a_request_carrying_a_header_the_seam_frames_is_refused_unsent() {
1921
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
1922
+ listener.set_nonblocking(true).unwrap();
1923
+ let port = listener.local_addr().unwrap().port();
1924
+ let namespace = namespace(vec![Endpoint::new("127.0.0.1", port).unwrap()]);
1925
+ let cred = prepare_creds(&[], None);
1926
+ let answers = ["Host", "content-length", "bad name"].map(|name| {
1927
+ let mut request = greeting_request(&format!("127.0.0.1:{port}"));
1928
+ request.headers.push((name.to_string(), b"x".to_vec()));
1929
+ crate::net::send(&namespace, &cred, request).err()
1930
+ });
1931
+ thread::sleep(Duration::from_millis(20));
1932
+ assert!(
1933
+ answers
1934
+ .iter()
1935
+ .all(|answer| *answer == Some(HttpError::Denied))
1936
+ && listener.accept().is_err(),
1937
+ "a guest's request carrying host, content-length, or a name that is no \
1938
+ token must answer Denied, sending nothing, got {answers:?}"
1939
+ );
1940
+ }
1941
+
1942
+ // @behavior NW-184
1943
+ #[test]
1944
+ fn a_request_whose_target_is_not_in_origin_form_is_refused_unsent() {
1945
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
1946
+ listener.set_nonblocking(true).unwrap();
1947
+ let port = listener.local_addr().unwrap().port();
1948
+ let namespace = namespace(vec![Endpoint::new("127.0.0.1", port).unwrap()]);
1949
+ let cred = prepare_creds(&[], None);
1950
+ let answers = ["http://other.example/x", "*", "", "/a#b"].map(|target| {
1951
+ let mut request = greeting_request(&format!("127.0.0.1:{port}"));
1952
+ request.path_with_query = target.to_string();
1953
+ crate::net::send(&namespace, &cred, request).err()
1954
+ });
1955
+ thread::sleep(Duration::from_millis(20));
1956
+ assert!(
1957
+ answers
1958
+ .iter()
1959
+ .all(|answer| *answer == Some(HttpError::Denied))
1960
+ && listener.accept().is_err(),
1961
+ "a guest's request whose target is absolute-form, asterisk-form, empty, or \
1962
+ carries a fragment must answer Denied, sending nothing, got {answers:?}"
1963
+ );
1964
+ }
1965
+
1966
+ // @behavior NW-219
1967
+ #[test]
1968
+ fn a_request_whose_method_is_not_a_token_is_refused_unsent() {
1969
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
1970
+ listener.set_nonblocking(true).unwrap();
1971
+ let port = listener.local_addr().unwrap().port();
1972
+ let namespace = namespace(vec![Endpoint::new("127.0.0.1", port).unwrap()]);
1973
+ let cred = prepare_creds(&[], None);
1974
+ let answers = ["", "GET X"].map(|method| {
1975
+ let mut request = greeting_request(&format!("127.0.0.1:{port}"));
1976
+ request.method = method.to_string();
1977
+ crate::net::send(&namespace, &cred, request).err()
1978
+ });
1979
+ thread::sleep(Duration::from_millis(20));
1980
+ assert!(
1981
+ answers
1982
+ .iter()
1983
+ .all(|answer| *answer == Some(HttpError::Denied))
1984
+ && listener.accept().is_err(),
1985
+ "a guest's request whose method is empty or holds a space must answer Denied, \
1986
+ sending nothing, got {answers:?}"
1987
+ );
1988
+ }
1989
+
1990
+ // @behavior NW-048
1991
+ #[test]
1992
+ fn an_https_request_opens_tls_before_any_byte_of_http() {
1993
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
1994
+ let port = listener.local_addr().unwrap().port();
1995
+ // The server reads the whole first record before it answers in clear,
1996
+ // and drains the client before it closes: a close with bytes unread
1997
+ // resets the connection on Windows before the client parses a byte.
1998
+ let server = thread::spawn(move || {
1999
+ let (mut stream, _) = listener.accept().unwrap();
2000
+ let mut header = [0; 5];
2001
+ stream.read_exact(&mut header).unwrap();
2002
+ let length = u16::from_be_bytes([header[3], header[4]]);
2003
+ stream
2004
+ .read_exact(&mut vec![0; usize::from(length)])
2005
+ .unwrap();
2006
+ stream.write_all(b"HTTP/1.1 200 OK\r\n\r\n").unwrap();
2007
+ stream.shutdown(std::net::Shutdown::Write).unwrap();
2008
+ let _ = stream.read_to_end(&mut Vec::new());
2009
+ header[0]
2010
+ });
2011
+ let namespace = namespace(vec![Endpoint::new("127.0.0.1", port).unwrap()]);
2012
+ let cred = prepare_creds(&[], None);
2013
+ let mut request = greeting_request(&format!("127.0.0.1:{port}"));
2014
+ request.scheme = Scheme::Https;
2015
+ let exchange =
2016
+ crate::net::send(&namespace, &cred, request).expect("an endpoint names the authority");
2017
+ let answer = read_response(&exchange).map(|(head, _)| head.status);
2018
+ assert_eq!(
2019
+ (server.join().unwrap(), answer),
2020
+ (0x16, Err(HttpError::TlsProtocol)),
2021
+ "an https request must open TLS with a handshake record before any HTTP, and a server \
2022
+ answering in clear must fail it as a TLS protocol error"
2023
+ );
2024
+ }
2025
+
2026
+ // @behavior NW-045
2027
+ #[test]
2028
+ fn a_request_policy_sees_the_request_and_refuses_it_unsent() {
2029
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
2030
+ listener.set_nonblocking(true).unwrap();
2031
+ let port = listener.local_addr().unwrap().port();
2032
+ let seen = Arc::new(Mutex::new(Vec::new()));
2033
+ let recorded = seen.clone();
2034
+ let mut entry = Builder::default();
2035
+ entry
2036
+ .endpoint(Endpoint::new("127.0.0.1", port).unwrap())
2037
+ .request_policy(move |sent: &crate::network::Request<'_>| {
2038
+ recorded.lock().unwrap().push((
2039
+ sent.authority.0.to_string(),
2040
+ sent.method.to_string(),
2041
+ sent.scheme.to_string(),
2042
+ sent.path.to_string(),
2043
+ sent.header_names
2044
+ .iter()
2045
+ .map(|n| n.to_string())
2046
+ .collect::<Vec<_>>(),
2047
+ ));
2048
+ false
2049
+ });
2050
+ let network = Network::new("web", Arc::new(entry)).unwrap();
2051
+ let namespace =
2052
+ Namespace::for_network(&network, Arc::new(HostInet), Arc::new(HyperOutbound::new()));
2053
+ let mut request = greeting_request(&format!("127.0.0.1:{port}"));
2054
+ request
2055
+ .headers
2056
+ .push(("X-Probe".to_string(), b"secret".to_vec()));
2057
+ let cred = prepare_creds(&[], None);
2058
+ let answer = crate::net::send(&namespace, &cred, request).err();
2059
+ thread::sleep(Duration::from_millis(20));
2060
+ assert_eq!(
2061
+ (
2062
+ answer,
2063
+ seen.lock().unwrap().clone(),
2064
+ listener.accept().is_err()
2065
+ ),
2066
+ (
2067
+ Some(HttpError::Denied),
2068
+ vec![(
2069
+ "127.0.0.1".to_string(),
2070
+ "GET".to_string(),
2071
+ "http".to_string(),
2072
+ "/greeting".to_string(),
2073
+ vec!["x-probe".to_string()]
2074
+ )],
2075
+ true
2076
+ ),
2077
+ "a request policy must see the authority, method, scheme, path with no query, and \
2078
+ header names in lower case however the request spelled them, and its refusal must \
2079
+ answer Denied, sending nothing"
2080
+ );
2081
+ }
2082
+
2083
+ /// A seam recording the credentials each request it is handed carries, and
2084
+ /// answering none.
2085
+ #[derive(Default)]
2086
+ struct RecordsSwaps(Mutex<Vec<Vec<Swap>>>);
2087
+
2088
+ impl Outbound for RecordsSwaps {
2089
+ fn send_request(&self, request: OutgoingRequest, _connector: Connector) -> ResponseFuture {
2090
+ self.0.lock().unwrap().push(request.swaps);
2091
+ Box::pin(async { Err(HttpError::Internal) })
2092
+ }
2093
+ }
2094
+
2095
+ /// The swaps a request with `header: value` carries once its entry binds a
2096
+ /// credential to `exact.example.com` and `*.example.com` on 443, allowing
2097
+ /// GET on `/api/**`.
2098
+ fn swaps_of(scheme: Scheme, host: &str, path: &str, header: &str, value: &[u8]) -> Vec<Swap> {
2099
+ send_bound(scheme, host, path, &[(header, value)])
2100
+ .expect("an endpoint names the authority")
2101
+ .unwrap_or_default()
2102
+ }
2103
+
2104
+ /// What `send` answers a request with `headers` under the entry `swaps_of`
2105
+ /// describes: the swaps the seam was handed, `None` when it was handed no
2106
+ /// request.
2107
+ fn send_bound(
2108
+ scheme: Scheme,
2109
+ host: &str,
2110
+ path: &str,
2111
+ headers: &[(&str, &[u8])],
2112
+ ) -> Result<Option<Vec<Swap>>, HttpError> {
2113
+ let mut entry = Builder::default();
2114
+ entry
2115
+ .endpoint(
2116
+ Endpoint::new("exact.example.com", 443)
2117
+ .unwrap()
2118
+ .with_rules(vec![crate::network::HttpRule::new(
2119
+ crate::network::Action::Allow,
2120
+ Some("GET"),
2121
+ "/api/**",
2122
+ )]),
2123
+ )
2124
+ .endpoint(Endpoint::new("*.example.com", 443).unwrap())
2125
+ .credential(crate::network::Credential::new(
2126
+ crate::network::Placement::Bearer,
2127
+ "kobako-token",
2128
+ Arc::new(|_: &crate::network::Request<'_>| {
2129
+ Ok(Some(crate::network::Secret::new("s3cret")))
2130
+ }),
2131
+ ));
2132
+ let network = Network::new("api", Arc::new(entry)).unwrap();
2133
+ let seam = Arc::new(RecordsSwaps::default());
2134
+ let family = Arc::new(CountsDials::default());
2135
+ let namespace = Namespace::for_network(&network, family, seam.clone());
2136
+ let mut request = OutgoingRequest::new("GET", scheme, format!("{host}:443"));
2137
+ request.path_with_query = path.to_string();
2138
+ request.headers = headers
2139
+ .iter()
2140
+ .map(|(name, value)| (name.to_string(), value.to_vec()))
2141
+ .collect();
2142
+ let sent = crate::net::send(&namespace, &prepare_creds(&[], None), request);
2143
+ if let Ok(exchange) = &sent {
2144
+ let _ = read_response(exchange);
2145
+ }
2146
+ let recorded = seam.0.lock().unwrap().pop();
2147
+ sent.map(|_| recorded)
2148
+ }
2149
+
2150
+ // @behavior NW-220
2151
+ #[test]
2152
+ fn a_request_carrying_a_credentials_placeholder_more_than_once_is_refused_unsent() {
2153
+ let answer = |scheme, headers: &[(&str, &[u8])]| {
2154
+ send_bound(scheme, "exact.example.com", "/api/repos", headers)
2155
+ .map(|handed| handed.map(|swaps| swaps.len()))
2156
+ };
2157
+ assert_eq!(
2158
+ [
2159
+ answer(
2160
+ Scheme::Https,
2161
+ &[("authorization", b"Bearer kobako-token kobako-token")]
2162
+ ),
2163
+ answer(
2164
+ Scheme::Https,
2165
+ &[
2166
+ ("authorization", b"Bearer kobako-token"),
2167
+ ("Authorization", b"Bearer kobako-token"),
2168
+ ]
2169
+ ),
2170
+ // `a:kobako-tokenkobako-token`, encoded.
2171
+ answer(
2172
+ Scheme::Https,
2173
+ &[(
2174
+ "authorization",
2175
+ b"Basic YTprb2Jha28tdG9rZW5rb2Jha28tdG9rZW4="
2176
+ )]
2177
+ ),
2178
+ answer(
2179
+ Scheme::Https,
2180
+ &[
2181
+ ("authorization", b"Bearer kobako-token"),
2182
+ ("x-echo", b"kobako-token kobako-token"),
2183
+ ]
2184
+ ),
2185
+ ],
2186
+ [
2187
+ Err(HttpError::Denied),
2188
+ Err(HttpError::Denied),
2189
+ Err(HttpError::Denied),
2190
+ Ok(Some(1)),
2191
+ ],
2192
+ "a request whose credential header carries the placeholder more than once, in one \
2193
+ value, across two, or inside a Basic pair, must answer Denied with nothing sent, \
2194
+ and one carrying it once must go with its one swap whatever another header holds"
2195
+ );
2196
+ }
2197
+
2198
+ // @behavior NW-050
2199
+ #[test]
2200
+ fn a_credential_binds_only_to_https_its_exact_host_and_an_allowed_path() {
2201
+ let bound = |scheme, host, path, header| {
2202
+ !swaps_of(scheme, host, path, header, b"Bearer kobako-token").is_empty()
2203
+ };
2204
+ assert_eq!(
2205
+ [
2206
+ bound(
2207
+ Scheme::Https,
2208
+ "exact.example.com",
2209
+ "/api/repos",
2210
+ "authorization"
2211
+ ),
2212
+ bound(
2213
+ Scheme::Http,
2214
+ "exact.example.com",
2215
+ "/api/repos",
2216
+ "authorization"
2217
+ ),
2218
+ bound(
2219
+ Scheme::Https,
2220
+ "other.example.com",
2221
+ "/api/repos",
2222
+ "authorization"
2223
+ ),
2224
+ bound(
2225
+ Scheme::Https,
2226
+ "exact.example.com",
2227
+ "/admin",
2228
+ "authorization"
2229
+ ),
2230
+ bound(
2231
+ Scheme::Https,
2232
+ "exact.example.com",
2233
+ "/api/a;b",
2234
+ "authorization"
2235
+ ),
2236
+ bound(Scheme::Https, "exact.example.com", "/api/repos", "x-other"),
2237
+ ],
2238
+ [true, false, false, false, false, false],
2239
+ "a credential must bind only to https, to its entry's exact host, on a path the \
2240
+ endpoint allows and every server reads alike, and only where its header carries the \
2241
+ placeholder"
2242
+ );
2243
+ }
2244
+
2245
+ // @behavior NW-051
2246
+ #[test]
2247
+ fn a_swap_replaces_the_placeholder_in_its_header_only() {
2248
+ let swap = Swap {
2249
+ header: "authorization".to_string(),
2250
+ placeholder: b"kobako-token".to_vec(),
2251
+ secret: crate::network::Secret::new("s3cret"),
2252
+ };
2253
+ let masked = format!("{:?}", swap.secret);
2254
+ let mut request = OutgoingRequest::new("GET", Scheme::Https, "api.example.com:443");
2255
+ request.path_with_query = "/".to_string();
2256
+ request.headers = vec![
2257
+ ("Authorization".to_string(), b"Bearer kobako-token".to_vec()),
2258
+ ("x-echo".to_string(), b"kobako-token".to_vec()),
2259
+ ];
2260
+ request.swaps = vec![swap];
2261
+ let sent = to_hyper_request(request).expect("the request's headers are valid");
2262
+ let header = |name| {
2263
+ sent.headers()
2264
+ .get(name)
2265
+ .map(|value| value.as_bytes().to_vec())
2266
+ };
2267
+ assert_eq!(
2268
+ (
2269
+ header("authorization"),
2270
+ header("x-echo"),
2271
+ sent.headers().len(),
2272
+ masked
2273
+ ),
2274
+ (
2275
+ Some(b"Bearer s3cret".to_vec()),
2276
+ Some(b"kobako-token".to_vec()),
2277
+ 3,
2278
+ "Secret(**)".to_string()
2279
+ ),
2280
+ "the outbound seam must send a request with its credential's placeholder \
2281
+ replaced in the header the credential names only, every other header as the guest \
2282
+ set it, and the secret must print as a mask"
2283
+ );
2284
+ }
2285
+
2286
+ // @behavior NW-076
2287
+ #[test]
2288
+ fn a_request_sends_its_secret_inside_a_basic_pair_carrying_the_placeholder() {
2289
+ let sent = |value: &[u8]| {
2290
+ swaps_of(
2291
+ Scheme::Https,
2292
+ "exact.example.com",
2293
+ "/api/repos",
2294
+ "authorization",
2295
+ value,
2296
+ )
2297
+ .iter()
2298
+ .fold(value.to_vec(), |value, swap| {
2299
+ swap.apply("authorization", &value)
2300
+ })
2301
+ };
2302
+ assert_eq!(
2303
+ [sent(b"Basic dXNlcjprb2Jha28tdG9rZW4="), sent(b"Basic YQ=="),],
2304
+ [b"Basic dXNlcjpzM2NyZXQ=".to_vec(), b"Basic YQ==".to_vec()],
2305
+ "a request whose Basic pair encodes the placeholder must go with the secret in the \
2306
+ pair, encoded again, as OpenShell's does, and a pair without it must go as sent"
2307
+ );
2308
+ }
2309
+
2310
+ // @behavior NW-116
2311
+ #[test]
2312
+ fn a_socket_and_an_exchange_hold_their_buffers_against_the_invocations_memory() {
2313
+ let mut entry = Builder::default();
2314
+ entry.endpoint(Endpoint::new("api.example.com", 80).unwrap());
2315
+ let network = Network::new("api", Arc::new(entry)).unwrap();
2316
+ let namespace = Namespace::for_network(
2317
+ &network,
2318
+ Arc::new(CountsDials::default()),
2319
+ Arc::new(RecordsSwaps::default()),
2320
+ );
2321
+ let cred = prepare_creds(&[], Some(2 * MAX_SOCKET_BUFFER));
2322
+ let tcp = crate::net::TcpSock::create(&namespace, &cred, crate::net::AddressFamily::Inet);
2323
+ let udp =
2324
+ crate::net::UdpSock::create(&namespace, &cred, crate::net::AddressFamily::Inet).err();
2325
+ let sent = crate::net::send(
2326
+ &namespace,
2327
+ &cred,
2328
+ OutgoingRequest::new("GET", Scheme::Http, "api.example.com:80"),
2329
+ )
2330
+ .err();
2331
+ let made = tcp.is_ok();
2332
+ drop(tcp);
2333
+ let again =
2334
+ crate::net::UdpSock::create(&namespace, &cred, crate::net::AddressFamily::Inet).is_ok();
2335
+ assert_eq!(
2336
+ (made, udp, sent, again),
2337
+ (
2338
+ true,
2339
+ Some(Errno::OutOfMemory),
2340
+ Some(HttpError::Internal),
2341
+ true
2342
+ ),
2343
+ "with a memory_limit holding one TCP socket's buffers, a UDP socket must answer \
2344
+ ENOMEM and a request fail before it is sent, and a UDP socket must be made once the \
2345
+ TCP socket is gone"
2346
+ );
2347
+ }
2348
+
2349
+ /// A seam answering each request at once with one header of `.0` bytes and
2350
+ /// no body.
2351
+ struct AnswersHeader(usize);
2352
+
2353
+ impl Outbound for AnswersHeader {
2354
+ fn send_request(&self, _: OutgoingRequest, _: Connector) -> ResponseFuture {
2355
+ let size = self.0;
2356
+ Box::pin(async move {
2357
+ Ok(Response {
2358
+ head: ResponseHead {
2359
+ status: 200,
2360
+ headers: vec![("x-large".to_string(), vec![b'v'; size])],
2361
+ },
2362
+ body: http_body_util::Empty::<Bytes>::new()
2363
+ .map_err(|never| match never {})
2364
+ .boxed_unsync(),
2365
+ })
2366
+ })
2367
+ }
2368
+ }
2369
+
2370
+ // @behavior NW-156
2371
+ #[test]
2372
+ fn a_response_head_past_the_invocations_memory_answers_internal_error() {
2373
+ let status = |size: usize| {
2374
+ let mut entry = Builder::default();
2375
+ entry.endpoint(Endpoint::new("api.example.com", 80).unwrap());
2376
+ let network = Network::new("api", Arc::new(entry)).unwrap();
2377
+ let namespace = Namespace::for_network(
2378
+ &network,
2379
+ Arc::new(CountsDials::default()),
2380
+ Arc::new(AnswersHeader(size)),
2381
+ );
2382
+ let cred = prepare_creds(&[], Some(MAX_READ_BUFFER + MAX_SOCKET_BUFFER + 16 * 1024));
2383
+ let exchange = crate::net::send(
2384
+ &namespace,
2385
+ &cred,
2386
+ OutgoingRequest::new("GET", Scheme::Http, "api.example.com:80"),
2387
+ )
2388
+ .expect("an endpoint names the authority");
2389
+ read_response(&exchange).map(|(head, _)| head.status)
2390
+ };
2391
+ assert_eq!(
2392
+ (status(1024), status(64 * 1024)),
2393
+ (Ok(200), Err(HttpError::Internal)),
2394
+ "a response whose head fits the Invocation's memory beside its buffer must be \
2395
+ answered, and one whose 64 KiB head does not must fail as internal"
2396
+ );
2397
+ }
2398
+
2399
+ // @behavior NW-156
2400
+ #[test]
2401
+ fn a_released_response_holds_its_head_until_the_exchange_is_dropped() {
2402
+ let mut entry = Builder::default();
2403
+ entry.endpoint(Endpoint::new("api.example.com", 80).unwrap());
2404
+ let network = Network::new("api", Arc::new(entry)).unwrap();
2405
+ let namespace = Namespace::for_network(
2406
+ &network,
2407
+ Arc::new(CountsDials::default()),
2408
+ Arc::new(AnswersHeader(24 * 1024)),
2409
+ );
2410
+ let cred = prepare_creds(&[], Some(MAX_READ_BUFFER + MAX_SOCKET_BUFFER + 40 * 1024));
2411
+ let exchange = crate::net::send(
2412
+ &namespace,
2413
+ &cred,
2414
+ OutgoingRequest::new("GET", Scheme::Http, "api.example.com:80"),
2415
+ )
2416
+ .expect("an endpoint names the authority");
2417
+ read_response(&exchange).expect("a 24 KiB head fits the memory");
2418
+ exchange.release();
2419
+ // Everything but the head: what the exchange gave back on release.
2420
+ let rest = MAX_READ_BUFFER + MAX_SOCKET_BUFFER + 30 * 1024;
2421
+ let while_held = MemoryCharge::new(cred.memory_budget(), rest).is_ok();
2422
+ drop(exchange);
2423
+ let after = MemoryCharge::new(cred.memory_budget(), rest).is_ok();
2424
+ assert_eq!(
2425
+ (while_held, after),
2426
+ (false, true),
2427
+ "a response whose 24 KiB head was read and its body finished must keep that head \
2428
+ against the Invocation's memory while the guest holds the response, so the \
2429
+ buffers and 30 KiB more are refused, and give it back once the response is dropped"
2430
+ );
2431
+ }
2432
+
2433
+ /// A seam that never answers, holding each request it is handed.
2434
+ struct NeverAnswers;
2435
+
2436
+ impl Outbound for NeverAnswers {
2437
+ fn send_request(&self, request: OutgoingRequest, _: Connector) -> ResponseFuture {
2438
+ Box::pin(async move {
2439
+ let _held = request;
2440
+ std::future::pending().await
2441
+ })
2442
+ }
2443
+ }
2444
+
2445
+ /// A seam that dials the request's authority, then never answers.
2446
+ struct DialsThenWaits;
2447
+
2448
+ impl Outbound for DialsThenWaits {
2449
+ fn send_request(&self, request: OutgoingRequest, connector: Connector) -> ResponseFuture {
2450
+ Box::pin(async move {
2451
+ let _held = request;
2452
+ let _connection = connector.dial().await?;
2453
+ std::future::pending().await
2454
+ })
2455
+ }
2456
+ }
2457
+
2458
+ /// The answer `exchange` settles on, once it settles. Ten seconds is far
2459
+ /// past any timeout these tests set, so an exchange still unsettled then
2460
+ /// is one no timeout was left to end.
2461
+ fn settled(exchange: &Exchange) -> Result<ResponseHead, HttpError> {
2462
+ let give_up = std::time::Instant::now() + Duration::from_secs(10);
2463
+ loop {
2464
+ match exchange.response() {
2465
+ Some(answer) => break answer,
2466
+ None if std::time::Instant::now() < give_up => {
2467
+ thread::sleep(Duration::from_millis(1))
2468
+ }
2469
+ None => panic!(
2470
+ "the exchange never settled in ten seconds; its dial {}, so a connected one \
2471
+ lost its first-byte timeout and an unconnected one its socket's wake",
2472
+ match exchange.connected.get() {
2473
+ Some(_) => "connected",
2474
+ None => "never connected",
2475
+ }
2476
+ ),
2477
+ }
2478
+ }
2479
+ }
2480
+
2481
+ // @behavior NW-205
2482
+ #[test]
2483
+ fn a_request_past_its_connect_or_first_byte_timeout_answers_that_timeout() {
2484
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
2485
+ let port = listener.local_addr().unwrap().port();
2486
+ // Each request's namespace lives as long as the test: the Invocation's
2487
+ // end aborts what it still holds.
2488
+ let namespaces = std::cell::RefCell::new(Vec::new());
2489
+ let timed = |seam: Arc<dyn Outbound>, timeouts: Timeouts| {
2490
+ let mut entry = Builder::default();
2491
+ entry.endpoint(
2492
+ Endpoint::new("127.0.0.1", port)
2493
+ .unwrap()
2494
+ .with_addresses(vec![crate::network::Addresses::Local]),
2495
+ );
2496
+ let network = Network::new("web", Arc::new(entry)).unwrap();
2497
+ let namespace = Namespace::for_network(&network, Arc::new(HostInet), seam);
2498
+ let mut request = greeting_request(&format!("127.0.0.1:{port}"));
2499
+ request.timeouts = timeouts;
2500
+ let exchange = crate::net::send(&namespace, &prepare_creds(&[], None), request)
2501
+ .expect("the endpoint names the authority");
2502
+ namespaces.borrow_mut().push(namespace);
2503
+ exchange
2504
+ };
2505
+ let at_once = Some(Duration::ZERO);
2506
+ let unconnected = timed(
2507
+ Arc::new(NeverAnswers),
2508
+ Timeouts {
2509
+ connect: at_once,
2510
+ ..Timeouts::default()
2511
+ },
2512
+ );
2513
+ let unanswered = timed(
2514
+ Arc::new(DialsThenWaits),
2515
+ Timeouts {
2516
+ first_byte: at_once,
2517
+ ..Timeouts::default()
2518
+ },
2519
+ );
2520
+ let sent = Instant::now();
2521
+ let unset = timed(Arc::new(NeverAnswers), Timeouts::default());
2522
+ let unset_bound = unset
2523
+ .deadline()
2524
+ .map(|at| at.saturating_duration_since(sent).as_secs());
2525
+ let unset_head = timed(Arc::new(DialsThenWaits), Timeouts::default());
2526
+ let give_up = Instant::now() + Duration::from_secs(10);
2527
+ let connected = loop {
2528
+ match unset_head.connected.get() {
2529
+ Some(at) => break *at,
2530
+ None if Instant::now() < give_up => thread::sleep(Duration::from_millis(1)),
2531
+ None => panic!(
2532
+ "the request never connected to the loopback listener in ten seconds and \
2533
+ answered {:?}: an answer names why its dial failed, none that its socket \
2534
+ was never woken",
2535
+ unset_head.response()
2536
+ ),
2537
+ }
2538
+ };
2539
+ let unset_head_bound = unset_head
2540
+ .deadline()
2541
+ .map(|at| at.saturating_duration_since(connected).as_secs());
2542
+ assert_eq!(
2543
+ (
2544
+ settled(&unconnected).err(),
2545
+ settled(&unanswered).err(),
2546
+ unset_bound,
2547
+ unset.response(),
2548
+ unset_head_bound,
2549
+ unset_head.response()
2550
+ ),
2551
+ (
2552
+ Some(HttpError::ConnectionTimeout),
2553
+ Some(HttpError::ConnectionReadTimeout),
2554
+ Some(600),
2555
+ None,
2556
+ Some(600),
2557
+ None
2558
+ ),
2559
+ "a request whose connect outlasts its connect timeout must answer connection-timeout, \
2560
+ one connected whose head outlasts its first-byte timeout connection-read-timeout, \
2561
+ and one setting no timeout must wait on, its connect bounded 600 seconds after it \
2562
+ was sent and its head 600 seconds after it connected"
2563
+ );
2564
+ }
2565
+
2566
+ // @behavior NW-205
2567
+ #[test]
2568
+ fn a_body_whose_next_part_outlasts_its_timeout_fails_its_read() {
2569
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
2570
+ let port = listener.local_addr().unwrap().port();
2571
+ let server = thread::spawn(move || {
2572
+ for _ in 0..2 {
2573
+ let (stream, _) = listener.accept().unwrap();
2574
+ let mut reader = BufReader::new(stream.try_clone().unwrap());
2575
+ let mut line = String::new();
2576
+ while reader.read_line(&mut line).unwrap() > 2 {
2577
+ line.clear();
2578
+ }
2579
+ (&stream)
2580
+ .write_all(b"HTTP/1.1 200 OK\r\ncontent-length: 10\r\n\r\n")
2581
+ .unwrap();
2582
+ // Hold the connection open with the body unsent until the
2583
+ // client lets it go.
2584
+ let _ = reader.read_line(&mut line);
2585
+ }
2586
+ });
2587
+ let namespace = namespace(vec![Endpoint::new("127.0.0.1", port)
2588
+ .unwrap()
2589
+ .with_addresses(vec![crate::network::Addresses::Local])]);
2590
+ let mut request = greeting_request(&format!("127.0.0.1:{port}"));
2591
+ request.timeouts.between_bytes = Some(Duration::ZERO);
2592
+ let cred = prepare_creds(&[], None);
2593
+ let exchange =
2594
+ crate::net::send(&namespace, &cred, request).expect("the endpoint names the authority");
2595
+ let head = settled(&exchange).map(|head| head.status);
2596
+ let mut buf = [0; 8];
2597
+ let read = exchange.read(&mut buf);
2598
+ let readable = exchange.alloc_body_file(&cred).poll(None) == PollMask::IN;
2599
+ exchange.release();
2600
+ let sent = Instant::now();
2601
+ let unset = crate::net::send(
2602
+ &namespace,
2603
+ &cred,
2604
+ greeting_request(&format!("127.0.0.1:{port}")),
2605
+ )
2606
+ .expect("the endpoint names the authority");
2607
+ let unset_head = settled(&unset).map(|head| head.status);
2608
+ let unset_bound = unset
2609
+ .deadline()
2610
+ .map(|at| at.saturating_duration_since(sent).as_secs());
2611
+ unset.release();
2612
+ server.join().unwrap();
2613
+ assert_eq!(
2614
+ (head, read, readable, unset_head, unset_bound),
2615
+ (Ok(200), Err(Errno::TimedOut), true, Ok(200), Some(600)),
2616
+ "a body whose next part outlasts its between-bytes timeout must fail its read as \
2617
+ ETIMEDOUT once its head arrived, and its file must poll readable so a waiting \
2618
+ reader meets that failure; a body setting no timeout must bound its wait 600 \
2619
+ seconds after its head"
2620
+ );
2621
+ }
2622
+
2623
+ // @behavior NW-156
2624
+ #[test]
2625
+ fn a_request_in_flight_holds_its_parts_against_the_invocations_memory() {
2626
+ let mut entry = Builder::default();
2627
+ entry.endpoint(Endpoint::new("api.example.com", 80).unwrap());
2628
+ let network = Network::new("api", Arc::new(entry)).unwrap();
2629
+ let namespace = Namespace::for_network(
2630
+ &network,
2631
+ Arc::new(CountsDials::default()),
2632
+ Arc::new(NeverAnswers),
2633
+ );
2634
+ let cred = prepare_creds(&[], Some(MAX_READ_BUFFER + MAX_SOCKET_BUFFER + 40 * 1024));
2635
+ let mut request = OutgoingRequest::new("GET", Scheme::Http, "api.example.com:80");
2636
+ request.path_with_query = format!("/{}", "p".repeat(30 * 1024));
2637
+ let exchange =
2638
+ crate::net::send(&namespace, &cred, request).expect("an endpoint names the authority");
2639
+ let while_sent = MemoryCharge::new(cred.memory_budget(), 20 * 1024).is_ok();
2640
+ exchange.release();
2641
+ let after = MemoryCharge::new(cred.memory_budget(), 20 * 1024).is_ok();
2642
+ assert_eq!(
2643
+ (while_sent, after),
2644
+ (false, true),
2645
+ "a request whose 30 KiB path is under way must hold it against a memory_limit of \
2646
+ its buffer and 40 KiB, so 20 KiB more is refused, and give it back once released"
2647
+ );
2648
+ }
2649
+
2650
+ mod stress {
2651
+ use std::task::Context;
2652
+ use std::time::Instant;
2653
+
2654
+ use super::*;
2655
+
2656
+ /// A body that never ends, a frame ready at every poll.
2657
+ struct EndlessBody;
2658
+
2659
+ impl Body for EndlessBody {
2660
+ type Data = Bytes;
2661
+ type Error = HttpError;
2662
+
2663
+ fn poll_frame(
2664
+ self: Pin<&mut Self>,
2665
+ _cx: &mut Context<'_>,
2666
+ ) -> Poll<Option<Result<http_body::Frame<Bytes>, HttpError>>> {
2667
+ Poll::Ready(Some(Ok(http_body::Frame::data(Bytes::from_static(
2668
+ &[0; 1024],
2669
+ )))))
2670
+ }
2671
+ }
2672
+
2673
+ /// A seam answering each request at once with an endless body.
2674
+ struct StreamsEndlessly;
2675
+
2676
+ impl Outbound for StreamsEndlessly {
2677
+ fn send_request(&self, _: OutgoingRequest, _: Connector) -> ResponseFuture {
2678
+ Box::pin(async {
2679
+ Ok(Response {
2680
+ head: ResponseHead {
2681
+ status: 200,
2682
+ headers: Vec::new(),
2683
+ },
2684
+ body: EndlessBody.boxed_unsync(),
2685
+ })
2686
+ })
2687
+ }
2688
+ }
2689
+
2690
+ // The reactor fills a body while the guest lets the exchange go, so the
2691
+ // last hold may fall in the middle of a fill. The reactor must answer a
2692
+ // request sent after every one of them.
2693
+ // @behavior NW-082
2694
+ #[test]
2695
+ #[ignore = "a stress witness; run by rake crates:stress"]
2696
+ fn an_exchange_let_go_while_its_body_fills_leaves_the_reactor_running() {
2697
+ let mut entry = Builder::default();
2698
+ entry.endpoint(Endpoint::new("api.example.com", 80).unwrap());
2699
+ let network = Network::new("api", Arc::new(entry)).unwrap();
2700
+ let namespace = Namespace::for_network(
2701
+ &network,
2702
+ Arc::new(CountsDials::default()),
2703
+ Arc::new(StreamsEndlessly),
2704
+ );
2705
+ let cred = prepare_creds(&[], None);
2706
+ let send = || {
2707
+ crate::net::send(
2708
+ &namespace,
2709
+ &cred,
2710
+ OutgoingRequest::new("GET", Scheme::Http, "api.example.com:80"),
2711
+ )
2712
+ .expect("an endpoint names the authority")
2713
+ };
2714
+ for _ in 0..20_000 {
2715
+ let exchange = send();
2716
+ while exchange.response().is_none() {
2717
+ std::hint::spin_loop();
2718
+ }
2719
+ let _ = exchange.read(&mut [0; 1]);
2720
+ }
2721
+ let last = send();
2722
+ let deadline = Instant::now() + Duration::from_secs(5);
2723
+ while last.response().is_none() && Instant::now() < deadline {
2724
+ thread::sleep(Duration::from_millis(1));
2725
+ }
2726
+ assert!(
2727
+ last.response().is_some(),
2728
+ "a request sent after 20,000 exchanges let go while their bodies fill must still \
2729
+ be answered, so no exchange's release ever stops the reactor"
2730
+ );
2731
+ }
2732
+ }
2733
+ }