diff --git a/Cargo.lock b/Cargo.lock index 09c7c71..1ff2f31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -746,7 +746,7 @@ checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] name = "pd-host-function" version = "0.1.0" -source = "git+https://github.com/rustscript-lang/rustscript?rev=87d8c5d5cb42b3381aecadb383c0b7bf34ff204f#87d8c5d5cb42b3381aecadb383c0b7bf34ff204f" +source = "git+https://github.com/rustscript-lang/rustscript?rev=fc8b0c8179640a8ffe460a64b3690e35c2f7eb73#fc8b0c8179640a8ffe460a64b3690e35c2f7eb73" dependencies = [ "pd-host-schema", "proc-macro2", @@ -757,7 +757,7 @@ dependencies = [ [[package]] name = "pd-host-schema" version = "0.1.0" -source = "git+https://github.com/rustscript-lang/rustscript?rev=87d8c5d5cb42b3381aecadb383c0b7bf34ff204f#87d8c5d5cb42b3381aecadb383c0b7bf34ff204f" +source = "git+https://github.com/rustscript-lang/rustscript?rev=fc8b0c8179640a8ffe460a64b3690e35c2f7eb73#fc8b0c8179640a8ffe460a64b3690e35c2f7eb73" dependencies = [ "proc-macro2", "syn 2.0.119", @@ -766,7 +766,7 @@ dependencies = [ [[package]] name = "pd-vm" version = "0.1.0" -source = "git+https://github.com/rustscript-lang/rustscript?rev=87d8c5d5cb42b3381aecadb383c0b7bf34ff204f#87d8c5d5cb42b3381aecadb383c0b7bf34ff204f" +source = "git+https://github.com/rustscript-lang/rustscript?rev=fc8b0c8179640a8ffe460a64b3690e35c2f7eb73#fc8b0c8179640a8ffe460a64b3690e35c2f7eb73" dependencies = [ "base64", "futures-channel", diff --git a/Cargo.toml b/Cargo.toml index c86cff6..42f93ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ ngx-rust-rev = "cda9d8372ccd2e139ef2b03e5806e019e7796187" nginx-core-version = "1.31.4" nginx-core-tarball-sha256 = "e6f20b644a17a643f059ae6467a1971fe2811587d025e071068753a1f1e3b3c3" rustscript-git = "https://github.com/rustscript-lang/rustscript" -rustscript-rev = "87d8c5d5cb42b3381aecadb383c0b7bf34ff204f" +rustscript-rev = "fc8b0c8179640a8ffe460a64b3690e35c2f7eb73" [lib] crate-type = ["cdylib", "rlib"] @@ -27,7 +27,7 @@ sha1 = { version = "0.10", default-features = false } sha2 = { version = "0.10", default-features = false } nginx-sys = { git = "https://github.com/nginx/ngx-rust", rev = "cda9d8372ccd2e139ef2b03e5806e019e7796187" } ngx = { git = "https://github.com/nginx/ngx-rust", rev = "cda9d8372ccd2e139ef2b03e5806e019e7796187", default-features = false, features = ["std"] } -pd-host-function = { git = "https://github.com/rustscript-lang/rustscript", rev = "87d8c5d5cb42b3381aecadb383c0b7bf34ff204f" } +pd-host-function = { git = "https://github.com/rustscript-lang/rustscript", rev = "fc8b0c8179640a8ffe460a64b3690e35c2f7eb73" } tokio = { version = "1", default-features = false, features = ["rt", "rt-multi-thread", "time", "net", "sync", "io-util"] } rustls = { version = "0.23", default-features = false, features = ["std", "tls12", "ring"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] } @@ -36,7 +36,7 @@ rustls-pemfile = { version = "2", default-features = false, features = ["std"] } rustls-native-certs = "0.8" webpki-roots = "0.26" zeroize = { version = "1", default-features = false, features = ["alloc"] } -vm = { package = "pd-vm", git = "https://github.com/rustscript-lang/rustscript", rev = "87d8c5d5cb42b3381aecadb383c0b7bf34ff204f", default-features = false, features = ["runtime", "async", "http-client", "sqlite"] } +vm = { package = "pd-vm", git = "https://github.com/rustscript-lang/rustscript", rev = "fc8b0c8179640a8ffe460a64b3690e35c2f7eb73", default-features = false, features = ["runtime", "async", "http-client", "sqlite"] } [features] default = ["export-modules"] diff --git a/README.md b/README.md index 01e676c..b55a459 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,10 @@ request-state programs, including buffered `http::client::request`, key-auth control flows, and `ngx.ctx`, may reuse a VM after successful `Halted` cleanup. Their pool key contains the exact compiled `Arc` identity, phase, complete standard-host policy, ngx catalog fingerprint, and standard catalog -fingerprint. SQLite, standard I/O, SSE, timers, sleep, cosockets, resource or -unknown host schemas, failures, cancellation, timeouts, pending cleanup, and -over-capacity returns are excluded. The worker retains at most four reusable +fingerprint. SQLite, standard I/O, SSE, timers, sleep, cosockets, dynamic host +schemas, failures, cancellation, timeouts, pending cleanup, and over-capacity +returns are excluded. Buffered HTTP is admitted: its resource handles are +quiescent within one synchronous call chain. The worker retains at most four reusable request VMs, installs a fresh `NgxAsyncBridge` and runtime policy on checkout, and clears request `ngx.ctx`, bridge, print sink, futures, resources, and reset state before return. Client disconnect during a wait finalizes the request as @@ -38,7 +39,7 @@ phase stays synchronous and fail-closed. | --- | --- | | [ngx-rust](https://github.com/nginx/ngx-rust) | `cda9d8372ccd2e139ef2b03e5806e019e7796187` (`ngx` / `nginx-sys` 0.5.0) | | NGINX | Official NGINX 1.31.4 (`nginx_version` 1031004), tarball SHA-256 `e6f20b644a17a643f059ae6467a1971fe2811587d025e071068753a1f1e3b3c3` | -| RustScript / `pd-vm` | [`https://github.com/rustscript-lang/rustscript`](https://github.com/rustscript-lang/rustscript) at `87d8c5d5cb42b3381aecadb383c0b7bf34ff204f` (Cargo git dependency `package = "pd-vm"` on that immutable rev; no sibling path) | +| RustScript / `pd-vm` | [`https://github.com/rustscript-lang/rustscript`](https://github.com/rustscript-lang/rustscript) at `fc8b0c8179640a8ffe460a64b3690e35c2f7eb73` (Cargo git dependency `package = "pd-vm"` on that immutable rev; no sibling path) | | Rust toolchain | 1.85.0 or later (ngx-rust MSRV). Verified with rustc 1.94.1 | Required build tools on Linux: diff --git a/docs/api/codec.md b/docs/api/codec.md index 0a5ebe0..0e311b5 100644 --- a/docs/api/codec.md +++ b/docs/api/codec.md @@ -22,13 +22,13 @@ struct DecodedArgsResult { | OpenResty API | RustScript callable | Status | Phases | Limits | Semantic differences | Acceptance | | --- | --- | --- | --- | --- | --- | --- | -| `ngx.escape_uri` | `ngx::escape_uri(uri: string, escape_type: unknown) -> string` | adapted | `PHASE_ESCAPE_URI` | input/output caps; type `0..=8` | Second arg is `null` (default type 2, URI component) or int `0..=8`. Types match lua-nginx-module bitmaps (0 URI, 1 args, 2 component, 3 HTML, 4 refresh, 5/6 memcached, 7 header name, 8 header value). | `tests/ngx_codec.rs` (`escape_uri_*`), `tests/host_catalog.rs` | +| `ngx.escape_uri` | `ngx::escape_uri(uri: string, escape_type: unknown) -> string` | adapted | `PHASE_ESCAPE_URI` | input/output caps; type `0..=8` | Second arg is `()` (default type 2, URI component) or int `0..=8`. Types match lua-nginx-module bitmaps (0 URI, 1 args, 2 component, 3 HTML, 4 refresh, 5/6 memcached, 7 header name, 8 header value). | `tests/ngx_codec.rs` (`escape_uri_*`), `tests/host_catalog.rs` | | `ngx.unescape_uri` | `ngx::unescape_uri(uri: string) -> string` | adapted | `PHASE_UNESCAPE_URI` | input cap | `+` becomes space; invalid `%` left as-is; non-UTF-8 bytes kept as `char::from` string. Not allowed in `ssl_session_fetch` / `ssl_session_store`. | `tests/ngx_codec.rs` (`unescape_uri_*`) | -| `ngx.encode_args` | `ngx::encode_args(args: unknown) -> string` | adapted | `PHASE_ENCODE_ARGS` | 1024 pairs; input/output caps | Catalog type is `unknown` because object literals are not `map`; runtime requires a map. Array values emit repeated keys. `true` is a bare key; `false`/`null` omitted. Nested maps/arrays-as-single-value rejected. Duplicate map keys overwrite; use arrays for multi-value keys. Not allowed in `init` / `init_worker`. | `tests/ngx_codec.rs` (`encode_args_*`) | -| `ngx.decode_args` | `ngx::decode_args(query: string, max_args: unknown) -> DecodedArgsResult` | adapted | `PHASE_DECODE_ARGS` | default 100; hard max 1024 | Returns fields `args: map` and `truncated: bool`; only `args` is a dynamic-key map. See cross-cutting item 4. `max_args` is `null` or int. | `tests/ngx_codec.rs` (`decode_args_*`) | -| `ngx.encode_base64` | `ngx::encode_base64(input: unknown, no_padding: unknown) -> string` | adapted | `PHASE_CODEC_MISC_SSL_CLIENT_HELLO` | input/output caps | `input` is string or bytes. `no_padding` is `null`/`false` or `true`. | `tests/ngx_codec.rs` (`encode_base64_padding_and_decode_variants`) | -| `ngx.decode_base64` | `ngx::decode_base64(text: string) -> optional` | adapted | `PHASE_CODEC_MISC_SSL_CLIENT_HELLO` | input cap | Invalid input is `null`, not an error. Padding may be omitted. | `tests/ngx_codec.rs` (`encode_base64_padding_and_decode_variants`) | -| `ngx.decode_base64mime` | `ngx::decode_base64mime(text: string) -> optional` | adapted | `PHASE_CODEC_MISC` | input cap | Skips non-alphabet bytes; stops at first `=`. `data_len % 4 == 1` is `null`. **Not** allowed in `ssl_client_hello` (OpenResty v0.10.29 exception). | `tests/ngx_codec.rs` (`decode_base64mime_*`), `tests/host_catalog.rs` | +| `ngx.encode_args` | `ngx::encode_args(args: unknown) -> string` | adapted | `PHASE_ENCODE_ARGS` | 1024 pairs; input/output caps | Catalog type is `unknown` because object literals are not an exact map schema; runtime requires a map. Array values emit repeated keys. `true` is a bare key; `false`/`()` omitted. Nested maps/arrays-as-single-value rejected. Duplicate map keys overwrite; use arrays for multi-value keys. Not allowed in `init` / `init_worker`. | `tests/ngx_codec.rs` (`encode_args_*`) | +| `ngx.decode_args` | `ngx::decode_args(query: string, max_args: unknown) -> DecodedArgsResult` | adapted | `PHASE_DECODE_ARGS` | default 100; hard max 1024 | Returns fields `args: array` and `truncated: bool`. See cross-cutting item 4. `max_args` is `()` or int. | `tests/ngx_codec.rs` (`decode_args_*`) | +| `ngx.encode_base64` | `ngx::encode_base64(input: unknown, no_padding: unknown) -> string` | adapted | `PHASE_CODEC_MISC_SSL_CLIENT_HELLO` | input/output caps | `input` is string or bytes. `no_padding` is `()`/`false` or `true`. | `tests/ngx_codec.rs` (`encode_base64_padding_and_decode_variants`) | +| `ngx.decode_base64` | `ngx::decode_base64(text: string) -> Option` | adapted | `PHASE_CODEC_MISC_SSL_CLIENT_HELLO` | input cap | Invalid input is `Option::None`, not an error. Padding may be omitted. | `tests/ngx_codec.rs` (`encode_base64_padding_and_decode_variants`) | +| `ngx.decode_base64mime` | `ngx::decode_base64mime(text: string) -> Option` | adapted | `PHASE_CODEC_MISC` | input cap | Skips non-alphabet bytes; stops at first `=`. `data_len % 4 == 1` is `Option::None`. **Not** allowed in `ssl_client_hello` (OpenResty v0.10.29 exception). | `tests/ngx_codec.rs` (`decode_base64mime_*`), `tests/host_catalog.rs` | | `ngx.crc32_short` | `ngx::crc32_short(data: string) -> int` | supported | `PHASE_CODEC_MISC_SSL_CLIENT_HELLO` | input cap | IEEE CRC-32. Currently matches `crc32_long`. | `tests/ngx_codec.rs` (`hash_and_hmac_use_fixed_vectors`) | | `ngx.crc32_long` | `ngx::crc32_long(data: string) -> int` | supported | `PHASE_CODEC_MISC_SSL_CLIENT_HELLO` | input cap | Same result as `crc32_short` in this release. | `tests/ngx_codec.rs` (`hash_and_hmac_use_fixed_vectors`) | | `ngx.hmac_sha1` | `ngx::hmac_sha1(secret: string, data: string) -> bytes` | adapted | `PHASE_CODEC_MISC_SSL_CLIENT_HELLO` | input cap on each arg | Raw digest **bytes**, not hex. | `tests/ngx_codec.rs` (`hash_and_hmac_use_fixed_vectors`) | diff --git a/docs/api/constants.md b/docs/api/constants.md index 9245eb0..fef9292 100644 --- a/docs/api/constants.md +++ b/docs/api/constants.md @@ -24,7 +24,7 @@ Acceptance: `tests/ngx_meta.rs` | `ngx.AGAIN` | `ngx::core::again() -> int` | `-2` | | `ngx.DONE` | `ngx::core::done() -> int` | `-4` | | `ngx.DECLINED` | `ngx::core::declined() -> int` | `-5` | -| `ngx.null` | `ngx::core::null_value() -> null` | `null` | +| `ngx.null` | `ngx::core::null_value() -> ()` | `()` | ### HTTP methods (`PHASE_CORE_CONST`) diff --git a/docs/api/control.md b/docs/api/control.md index 47e6468..b1ee250 100644 --- a/docs/api/control.md +++ b/docs/api/control.md @@ -6,8 +6,8 @@ Hosts set request-owned `RequestDisposition` then stop the VM. AccessExec consum | OpenResty API | RustScript callable | Status | Phases | Limits | Semantic differences | Acceptance | | --- | --- | --- | --- | --- | --- | --- | -| `ngx.exit` | `ngx::exit(status: int) -> null` | adapted | `PHASE_EXIT` | 0, -1, -5, 100-599 | Stops VM immediately. `0` continues the access phase (`NGX_OK`). `-5` is `NGX_DECLINED`. HTTP 201/204 and `>=300` finalize via the access return. HTTP 200-299 except 201/204 send an empty response then return (NGINX does not special-case 200). `NGX_AGAIN`/`NGX_DONE` rejected. | `tests/ngx_control_flow.rs` | -| `ngx.redirect` | `ngx::redirect(uri: string, status: unknown) -> null` | adapted | `PHASE_REDIRECT` | 65536-byte URI; 301/302/303/307/308 | `status` `null` defaults to 302. Validates scope, Continue disposition, headers-not-sent, URI, and status before mutating Location/status/disposition together. First wins. | `tests/ngx_control_flow.rs`, `tests/ngx_task7_hardening.rs` | +| `ngx.exit` | `ngx::exit(status: int) -> ()` | adapted | `PHASE_EXIT` | 0, -1, -5, 100-599 | Stops VM immediately. `0` continues the access phase (`NGX_OK`). `-5` is `NGX_DECLINED`. HTTP 201/204 and `>=300` finalize via the access return. HTTP 200-299 except 201/204 send an empty response then return (NGINX does not special-case 200). `NGX_AGAIN`/`NGX_DONE` rejected. | `tests/ngx_control_flow.rs` | +| `ngx.redirect` | `ngx::redirect(uri: string, status: unknown) -> ()` | adapted | `PHASE_REDIRECT` | 65536-byte URI; 301/302/303/307/308 | `status` `()` defaults to 302. Validates scope, Continue disposition, headers-not-sent, URI, and status before mutating Location/status/disposition together. First wins. | `tests/ngx_control_flow.rs`, `tests/ngx_task7_hardening.rs` | `ngx.exec` is **not** a production host. Import is rejected at config time. `set_uri(jump=true)` is a runtime rejection before mutation. diff --git a/docs/api/ctx.md b/docs/api/ctx.md index ec0ede6..3a7faf6 100644 --- a/docs/api/ctx.md +++ b/docs/api/ctx.md @@ -3,19 +3,19 @@ [Documentation](../index.md) · [Directives](../directives.md) · [API](index.md) · [Execution model](../execution-model.md) · [Security](../security.md) · [Examples](../examples/index.md) · [Compatibility](../ngx-api-compatibility.md) `ngx.ctx` is adapted as four explicit hosts rather than a Lua table. The map -is owned by `AccessExec` (request lifetime). Hosts clone deep-owned VM values -out; callables and other non-owning runtime values are rejected. Empty and -embedded-NUL keys are rejected. `delete` returns the removed value, or `null` -if missing (OpenResty uses `ngx.ctx.key = nil`). Configuration phases cannot -import these hosts. Capability is `RequestCtx` only — ctx hosts do not read -the request pointer. +is owned by `AccessExec` (request lifetime). Values are `string`; a callable +or any non-string value is rejected. Empty and embedded-NUL keys are +rejected. `delete` returns `Option`: `Option::Some(removed)` or +`Option::None` when the key is missing (OpenResty uses `ngx.ctx.key = nil`). +Configuration phases cannot import these hosts. Capability is `RequestCtx` +only — ctx hosts do not read the request pointer. | OpenResty API | RustScript callable | Status | Phases | Limits | Semantic differences | Acceptance | | --- | --- | --- | --- | --- | --- | --- | -| `ngx.ctx` | `ngx::ctx::get(key: string) -> unknown` | adapted | `PHASE_CTX` | missing → `null` | Explicit get; missing is `null`. | `tests/ngx_request_ctx.rs` | -| `ngx.ctx` | `ngx::ctx::set(key: string, value: unknown) -> null` | adapted | `PHASE_CTX` | 256 keys; 256-byte key; depth 16; 65536 total bytes; 1024 elems/container; 8192-byte scalar | Failed set leaves the previous value intact. Shared aliases are deep-cloned and counted per occurrence. | `tests/ngx_request_ctx.rs` | -| `ngx.ctx` | `ngx::ctx::delete(key: string) -> unknown` | adapted | `PHASE_CTX` | same key policy | Returns the removed value, or `null` if missing. | `tests/ngx_request_ctx.rs` | -| `ngx.ctx` | `ngx::ctx::clear() -> null` | adapted | `PHASE_CTX` | — | Drops every entry. OpenResty would assign a new table. | `tests/ngx_request_ctx.rs` | +| `ngx.ctx` | `ngx::ctx::get(key: string) -> Option` | adapted | `PHASE_CTX` | missing → `Option::None` | Explicit get; missing is `Option::None`, a stored empty string is `Option::Some("")`. | `tests/ngx_request_ctx.rs` | +| `ngx.ctx` | `ngx::ctx::set(key: string, value: string) -> ()` | adapted | `PHASE_CTX` | 256 keys; 256-byte key; 8192-byte value | Failed set leaves the previous value intact. | `tests/ngx_request_ctx.rs` | +| `ngx.ctx` | `ngx::ctx::delete(key: string) -> Option` | adapted | `PHASE_CTX` | same key policy | Returns `Option::Some(removed)` or `Option::None` if missing. | `tests/ngx_request_ctx.rs` | +| `ngx.ctx` | `ngx::ctx::clear() -> ()` | adapted | `PHASE_CTX` | — | Drops every entry. OpenResty would assign a new table. | `tests/ngx_request_ctx.rs` | `PHASE_CTX` (OpenResty v0.10.29 `ngx.ctx` contexts): `set`, `rewrite`, `access`, `content`, `header_filter`, `body_filter`, `log`, `timer`, diff --git a/docs/api/index.md b/docs/api/index.md index e163407..08be7ea 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -67,12 +67,13 @@ pd-vm I/O, HTTP, and SQLite hosts are not part of the 199 ngx names; see | Acceptance | Tests that exercise the host or family | Catalog type labels: `number` (int or float), `int`, `string`, `bytes`, -`bool`, `null`, `unknown` (checked at runtime), `map`, `array`, -`optional` (`T` or `null`), `resource` (typed VM handle). +`bool`, `()`, `array`, `Option` (`Option::Some(T)` or `Option::None`), +`Result`, named records, and `resource` (typed VM handle). A host +output never carries `unknown`, `null`, or the legacy `optional` schema. The pinned compiler matches host arity exactly. OpenResty optional -trailing arguments are an explicit `null` argument, not an omitted -argument. +trailing arguments are explicit arguments: pass `()` where the default is +wanted. Absence in a result is `Option::None`, never `null`. ## Inventory @@ -172,22 +173,26 @@ host adds a further difference. callables such as `ngx::core::ok()`, `ngx::http::get()`, `ngx::config::prefix()`, `ngx::worker::pid()`. There are no host constants or dynamic table fields. -2. **`ngx.null` is `ngx::core::null_value() -> null`.** `ngx::core::null` - is not catalogued: `null` is a reserved token. -3. **`ngx.sleep` is seconds.** `ngx::sleep(seconds: number) -> null` +2. **`ngx.null` is `ngx::core::null_value() -> ()`.** `ngx::core::null` + is not catalogued: `null` is a reserved token, and the host returns the + logical Unit so the RustScript compiler admits it. +3. **`ngx.sleep` is seconds.** `ngx::sleep(seconds: number) -> ()` accepts int or float seconds and rejects negative values, NaN, - infinity, and duration overflow. Legacy `ngx::sleep_ms(ms: int) -> null` + infinity, and duration overflow. Legacy `ngx::sleep_ms(ms: int) -> ()` keeps integer milliseconds for existing scripts. Both are async. -4. **`ngx.decode_args` result shape.** Returns `{ args, truncated }` - rather than Lua's multi-return `(table, truncated?)`. `max_args` of - `null` defaults to 100; `0` is clamped to the hard max 1024 (not - unbounded); values above 1024 are rejected. Keys without `=` become - boolean `true`. Repeated keys become arrays. Empty keys are skipped. - Invalid `%` sequences are left as-is. +4. **`ngx.decode_args` result shape.** Returns + `DecodedArgsResult { args: array, truncated: bool }` rather + than Lua's multi-return `(table, truncated?)`, where + `ArgEntry { name: string, value: Option }`. `max_args` of `()` + defaults to 100; `0` is clamped to the hard max 1024 (not unbounded); + values above 1024 are rejected. A key without `=` carries + `Option::None`; a present empty value is `Option::Some("")`. Repeated + keys repeat the entry. Empty keys are skipped. Invalid `%` sequences are + left as-is. 5. **Byte versus string results.** Hex `ngx::md5` returns `string`. `ngx::md5_bin`, `ngx::sha1_bin`, and `ngx::hmac_sha1` return `bytes`. - `ngx::decode_base64` / `ngx::decode_base64mime` return `optional` - (`null` on invalid input). `ngx::unescape_uri` returns a string even + `ngx::decode_base64` / `ngx::decode_base64mime` return + `Option` (`Option::None` on invalid input). `ngx::unescape_uri` returns a string even when decoded bytes are not UTF-8 (each byte becomes `char::from`). 6. **Time cache / update boundary.** Time hosts use a process-local cached clock, not nginx `ngx_time` / `ngx_timeofday`. Reads reuse the @@ -206,19 +211,19 @@ host adds a further difference. messages containing NUL are host errors. The line is prefixed `ngx-rustscript: `. OpenResty varargs are a single string. 9. **Phase and config import enforcement.** `compile_rss_source` compiles - access scripts against `compile_host_catalog()` — exactly the 199-name - `ngx_host_catalog()`, with no pd-vm standard family merged in — and then + access scripts against `compile_host_catalog()` — the 199-name + `ngx_host_catalog()` merged with pd-vm's standard host catalog (I/O, + HTTP, SQLite) — and then `validate_program_imports_for_phase(..., Access)`; `compile_rss_source_for_phase` performs the corresponding Content or - Log validation for `content_by_rss_file` and `log_by_rss_file`. pd-vm - standard HTTP imports stay dynamic: they compile as ordinary host - imports against the documented `map` interface, are validated by name - (`is_standard_host_import`), and bind through the staged standard - composition at runtime; pd-vm I/O and SQLite are compiler builtins - dispatched through that composition. Unknown names, phase-forbidden - names, arity, and type mismatches fail distinctly at `nginx -t` / - reload. Catalog phase metadata is the allowlist for all three - executable directives and for documented future phases. + Log validation for `content_by_rss_file` and `log_by_rss_file`. The + standard families are catalogued so their resource handles type-check + against the exact schemas the runtime composition registers, and the + same snapshot installs the adapters in the production registry. + Unknown names, phase-forbidden names, arity, and type mismatches fail + distinctly at `nginx -t` / reload. Catalog phase metadata is the + allowlist for all three executable directives and for documented future + phases. 10. **`ngx.worker.pids` semantics.** The array always includes live `ngx_pid` when `pid > 0`. Other `ngx_processes` slots are included only when `pid > 0` and `channel[0] > 0` (excludes master, helpers, @@ -226,8 +231,8 @@ host adds a further difference. pids are collapsed. Values come from NGINX public globals, never `/proc`. 11. **Optional OpenResty arguments.** Trailing options such as escape - type, `max_args`, and `no_padding` must be passed as `null` when the - default is wanted. + type, `max_args`, and `no_padding` must be passed explicitly; use `()` + when the default is wanted. ## Hard caps @@ -235,7 +240,7 @@ host adds a further difference. | --- | ---: | --- | | Codec input | 65536 bytes | URI/args/base64/hash/quote inputs | | Codec output | 196608 bytes | escape/encode/quote outputs | -| `decode_args` default `max_args` | 100 | `null` second argument | +| `decode_args` default `max_args` | 100 | `()` second argument | | `decode_args` hard `max_args` | 1024 | explicit cap; `0` clamps to this | | `encode_args` pairs | 1024 | including array expansion | | `parse_http_time` input | 128 bytes | over-limit is a host error | @@ -246,7 +251,7 @@ host adds a further difference. | Cached clock stale limit | 1 second | time hosts without `update_time` | | Request copy / body / raw header | 65536 bytes | `ngx::req::raw_header`, `get_headers` total, `get_uri_args` / `get_post_args` source | | Header field | 8192 bytes | single `get_headers` name or value | -| Header / args default cap | 100 | `null` `max_headers` / `max_args` | +| Header / args default cap | 100 | `()` `max_headers` / `max_args` | | Header / args hard cap | 1024 | explicit cap; `0` clamps to this | diff --git a/docs/api/logging.md b/docs/api/logging.md index 50e0d84..1a6a921 100644 --- a/docs/api/logging.md +++ b/docs/api/logging.md @@ -4,7 +4,7 @@ | OpenResty API | RustScript callable | Status | Phases | Yield | Limits | Semantic differences | Acceptance | | --- | --- | --- | --- | --- | --- | --- | --- | -| `ngx.log` | `ngx::log(level: int, message: string) -> null` | adapted | `PHASE_ALL` | sync | level `NGX_LOG_STDERR..=NGX_LOG_DEBUG`; no embedded NUL | Single string, not varargs. Request log when a request execution scope is live and allows `LOG`; otherwise cycle log; error if neither. Prefix `ngx-rustscript: `. No global `print`. | `tests/ngx_meta.rs` (`log_*`), `tests/access_nginx.rs` (`access_phase_logs_*`, `access_phase_rejects_invalid_log_level_and_embedded_nul`), `tests/log_nginx.rs` | +| `ngx.log` | `ngx::log(level: int, message: string) -> ()` | adapted | `PHASE_ALL` | sync | level `NGX_LOG_STDERR..=NGX_LOG_DEBUG`; no embedded NUL | Single string, not varargs. Request log when a request execution scope is live and allows `LOG`; otherwise cycle log; error if neither. Prefix `ngx-rustscript: `. No global `print`. | `tests/ngx_meta.rs` (`log_*`), `tests/access_nginx.rs` (`access_phase_logs_*`, `access_phase_rejects_invalid_log_level_and_embedded_nul`), `tests/log_nginx.rs` | --- diff --git a/docs/api/output.md b/docs/api/output.md index 183431e..d453cbe 100644 --- a/docs/api/output.md +++ b/docs/api/output.md @@ -12,11 +12,11 @@ response cap is 8 MiB; chunk cap is 4096. | OpenResty API | RustScript callable | Status | Phases | Limits | Semantic differences | Acceptance | | --- | --- | --- | --- | --- | --- | --- | -| `ngx.send_headers` | `ngx::send_headers() -> null` | adapted | content | — | Idempotent after `header_sent`. Default status 200 and `ngx_http_set_content_type`. | `tests/ngx_output.rs`, `tests/output_nginx.rs` | -| `ngx.print` | `ngx::print(data: unknown) -> null` | adapted | content | 1 MiB/call | String/bytes/int/float/bool/null/array. Nested maps rejected. Auto-sends headers. HEAD/`header_only` skips body. | `tests/ngx_output.rs`, `tests/output_nginx.rs` | -| `ngx.say` | `ngx::say(data: unknown) -> null` | adapted | content | 1 MiB/call | `print` plus a trailing newline. | `tests/ngx_output.rs`, `tests/output_nginx.rs` | -| `ngx.flush` | `ngx::flush(wait: bool) -> null` | adapted | content | — | Empty flush buf. HTTP/1.0 buffering returns `buffering`. `wait=true` models write backpressure via request-owned Waiting/eventfd (no request pointers in the future). | `tests/ngx_output.rs`, `tests/output_nginx.rs` | -| `ngx.eof` | `ngx::eof() -> null` | adapted | content | — | `last_buf` on the main request, `last_in_chain` always. Post-eof print/say/flush/eof fail. | `tests/ngx_output.rs`, `tests/output_nginx.rs` | +| `ngx.send_headers` | `ngx::send_headers() -> ()` | adapted | content | — | Idempotent after `header_sent`. Default status 200 and `ngx_http_set_content_type`. | `tests/ngx_output.rs`, `tests/output_nginx.rs` | +| `ngx.print` | `ngx::print(data: unknown) -> ()` | adapted | content | 1 MiB/call | String/bytes/int/float/bool/`()`/array. Nested maps rejected. Auto-sends headers. HEAD/`header_only` skips body. | `tests/ngx_output.rs`, `tests/output_nginx.rs` | +| `ngx.say` | `ngx::say(data: unknown) -> ()` | adapted | content | 1 MiB/call | `print` plus a trailing newline. | `tests/ngx_output.rs`, `tests/output_nginx.rs` | +| `ngx.flush` | `ngx::flush(wait: bool) -> ()` | adapted | content | — | Empty flush buf. HTTP/1.0 buffering returns `buffering`. `wait=true` models write backpressure via request-owned Waiting/eventfd (no request pointers in the future). | `tests/ngx_output.rs`, `tests/output_nginx.rs` | +| `ngx.eof` | `ngx::eof() -> ()` | adapted | content | — | `last_buf` on the main request, `last_in_chain` always. Post-eof print/say/flush/eof fail. | `tests/ngx_output.rs`, `tests/output_nginx.rs` | HTTP/1.0 without Content-Length buffers until eof/`send_headers` then sets `content_length_n`. HTTP/1.1 streams with chunked encoding when diff --git a/docs/api/request.md b/docs/api/request.md index 62dc1f6..c582a0d 100644 --- a/docs/api/request.md +++ b/docs/api/request.md @@ -22,31 +22,31 @@ struct RequestArgsResult { | OpenResty API | RustScript callable | Status | Phases | Limits | Semantic differences | Acceptance | | --- | --- | --- | --- | --- | --- | --- | | `ngx.req.get_method` | `ngx::req::get_method() -> string` | supported | `PHASE_REQ_GET_METHOD` | 256-byte method token | Custom methods keep the textual token. | `tests/ngx_request_read.rs` | -| `ngx.req.http_version` | `ngx::req::http_version() -> optional` | adapted | `PHASE_REQ_HTTP_VERSION` | none | `0.9`/`1.0`/`1.1`/`2.0`/`3.0`, or `null` if unrecognized. | `tests/ngx_request_read.rs` | -| `ngx.req.raw_header` | `ngx::req::raw_header(no_request_line: unknown) -> string` | adapted | `PHASE_REQ_HTTP_VERSION` | 65536-byte reconstruction | Reconstructs from the **main** request connection header buffer and busy chain (OpenResty v0.10.29). Validates every pointer/range and rejects HTTP/2 (`r->main->stream`) and HTTP/3 (`http_version` 3.0 or non-null `v3_parse`) using main-request protocol truth. Parser NULs restore to `:` and CR/LF. Preread body after CRLFCRLF is stripped. `no_request_line` is `null`/`false`/`true`. Malformed ranges, null buffers, cycles, and over-cap copies are host errors. | `tests/ngx_request_read.rs` (`raw_header_*`) | -| `ngx.req.get_headers` | `ngx::req::get_headers(max_headers: unknown, raw: unknown) -> RequestHeadersResult` | adapted | `PHASE_REQ_GET_HEADERS` | default 100; hard 1024; 8192-byte field; 65536-byte total | Fixed fields `headers: map` and `truncated: bool`; only `headers` has dynamic keys. `raw` `true` keeps original names; otherwise lowercase. Duplicate names become arrays. Empty values kept. `hash==0` skipped. Count/byte caps set `truncated=true` before further copy. Null elts with `nelts>0`, cycles, and impossible metadata are host errors, not silent truncation. | `tests/ngx_request_read.rs` | -| `ngx.req.get_uri_args` | `ngx::req::get_uri_args(max_args: unknown) -> RequestArgsResult` | adapted | `PHASE_REQ_GET_URI_ARGS` | default 100; hard 1024; 65536-byte query | Fixed fields `args: map` and `truncated: bool`; only `args` has dynamic keys. Uses the crate-private `decode_query_args` helper shared with `ngx::decode_args`. Trailing empty `&` at max_args is not truncated. `0` clamps to 1024. | `tests/ngx_request_read.rs`, `tests/ngx_codec.rs` | -| `ngx.req.get_post_args` | `ngx::req::get_post_args(max_args: unknown) -> RequestArgsResult` | adapted | `PHASE_REQ_GET_POST_ARGS` | same arg caps; 65536-byte body | Sync parse after `read_body` completion from memory **or** a bounded temp file. Fixed fields `args: map` and `truncated: bool`. Duplicate keys become arrays. Unread, still-reading, discarded, incomplete, or corrupt bodies are structured host errors. Does not delete NGINX temp files. Never initiates body read or yield. | `tests/ngx_request_body.rs` | -| `ngx.req.read_body` | `ngx::req::read_body() -> null` | adapted | `PHASE_REQ_READ_BODY` | async; rewrite/access/content | Calls `ngx_http_read_client_request_body` through a C shim. Undoes that API's `r->main->count++` for `NGX_OK`/`NGX_AGAIN` so the phase driver (content `NGX_DONE` / access `NGX_AGAIN`) owns count. Premature client close (`c->error` / confirmed EOF) is `499`; timeout stays `408`; protocol/`413` statuses are preserved. A C read-event wrapper peeks EOF then finalizes `499` once, otherwise invokes NGINX's original body handler. Generation-safe `nginx_op` cell: callback completes once; cleanup cancels once; stale callbacks no-op. Futures hold no request/VM/module pointers. Idempotent after completion. Forbidden in Log. | `tests/ngx_request_body.rs`, `tests/lifecycle.rs` | -| `ngx.req.discard_body` | `ngx::req::discard_body() -> null` | adapted | `PHASE_REQ_READ_BODY` | sync; rewrite/access/content | Calls `ngx_http_discard_request_body`. Never waits. Idempotent (`NGX_OK`/`NGX_AGAIN`). Discard-after-read is a no-op; read-after-discard completes immediately. Forbidden in Log. | `tests/ngx_request_body.rs` | -| `ngx.req.get_body_data` | `ngx::req::get_body_data() -> optional` | adapted | `PHASE_REQ_GET_BODY_DATA` | 65536-byte copy | Nullary like OpenResty. Owned copy of in-memory buffers. File-backed returns `null`. Empty memory buffers are skipped; inverted ranges and cycles are host errors. Allowed in Log (completed-body getter). | `tests/ngx_request_body.rs` | -| `ngx.req.get_body_file` | `ngx::req::get_body_file() -> optional` | adapted | `PHASE_REQ_GET_BODY_FILE` | 4096-byte path | Owned copy of NGINX temp-file path. Memory-backed returns `null`. Does not delete the file. Forbidden in Log. | `tests/ngx_request_body.rs` | -| `ngx.req.set_body_data` | `ngx::req::set_body_data(data: string) -> null` | adapted | `PHASE_REQ_SET_BODY` | 65536-byte memory body | Transactionally allocates a complete `ngx_http_request_body_t` / chain / buf plus bytes from the request pool, then swaps `r->request_body` only after Content-Length (real header hash, linked duplicates updated, dedicated pointer coherent) and chunked/Transfer-Encoding hide-hash sync succeed. Empty body installs a zero-length memory buf. Prior body remains on allocation failure. Rejected in Log, without `RequestWrite`/live request, while `reading_body`/`discard_body`/`header_sent`, while a read-body wait is armed, and on subrequests (`r != r->main`). | `tests/ngx_request_body_mutation.rs` | -| `ngx.req.set_body_file` | `ngx::req::set_body_file(path: string, auto_clean: unknown) -> null` | adapted | `PHASE_REQ_SET_BODY` | 4096-byte path; 65536-byte file | `lstat` then `O_NOFOLLOW\|O_CLOEXEC\|O_NONBLOCK` open; post-open `fstat` must stay a regular file with the same `st_dev`/`st_ino` as `lstat`. Regular files only (symlink/dir/FIFO rejected before open). `auto_clean` `null`/`false` leaves the source path; `true` copies into a private temp. A C `ngx_pool_cleanup_add` handler (`ngx_pool_delete_file` vs `ngx_pool_cleanup_file`) owns the fd: unlink the copy exactly once when `auto_clean`, close once, never unlink the caller source. Errors before cleanup registration leave fd with the caller; after registration a failed header commit nulls the handler. File buf matches OpenResty: `rb->buf=NULL`, `in_file`/`last_buf`/`last_in_chain`, no `temp_file` bit. Content-Length uses the real header hash so proxy hide-hash emits exactly one CL; Transfer-Encoding chain entries keep the hide-hash and are emptied. | `tests/ngx_request_body_mutation.rs` | -| `ngx.req.init_body` | `ngx::req::init_body(buffer_size: unknown) -> null` | adapted | `PHASE_REQ_SET_BODY` | default/hard cap 65536 | Request-owned builder in `RequestPhaseCtx` (test fallback only without module ctx). `null` uses the default cap; integer must be `0..=65536`. Repeated init resets the builder. Does not mutate NGINX body until `finish_body`. | `tests/ngx_request_body_mutation.rs` | -| `ngx.req.append_body` | `ngx::req::append_body(data: string) -> null` | adapted | `PHASE_REQ_SET_BODY` | cumulative builder cap | Copies bytes into the builder. Fails without `init_body` or when the cap would be exceeded; the builder is unchanged on oversize. | `tests/ngx_request_body_mutation.rs` | -| `ngx.req.finish_body` | `ngx::req::finish_body() -> null` | adapted | `PHASE_REQ_SET_BODY` | same as `set_body_data` | Atomically installs one coherent in-memory body. Repeated finish / finish without init fail. Allocation failure restores the builder. Dropped on request cleanup/error/disconnect/shutdown. | `tests/ngx_request_body_mutation.rs` | +| `ngx.req.http_version` | `ngx::req::http_version() -> Option` | adapted | `PHASE_REQ_HTTP_VERSION` | none | `0.9`/`1.0`/`1.1`/`2.0`/`3.0`, or `Option::None` if unrecognized. | `tests/ngx_request_read.rs` | +| `ngx.req.raw_header` | `ngx::req::raw_header(no_request_line: unknown) -> string` | adapted | `PHASE_REQ_HTTP_VERSION` | 65536-byte reconstruction | Reconstructs from the **main** request connection header buffer and busy chain (OpenResty v0.10.29). Validates every pointer/range and rejects HTTP/2 (`r->main->stream`) and HTTP/3 (`http_version` 3.0 or non-null `v3_parse`) using main-request protocol truth. Parser NULs restore to `:` and CR/LF. Preread body after CRLFCRLF is stripped. `no_request_line` is `()`/`false`/`true`. Malformed ranges, null buffers, cycles, and over-cap copies are host errors. | `tests/ngx_request_read.rs` (`raw_header_*`) | +| `ngx.req.get_headers` | `ngx::req::get_headers(max_headers: unknown, raw: unknown) -> RequestHeadersResult` | adapted | `PHASE_REQ_GET_HEADERS` | default 100; hard 1024; 8192-byte field; 65536-byte total | Fixed fields `headers: array` and `truncated: bool`, where `HeaderEntry { name: string, value: string }`. `raw` `true` keeps original names; otherwise lowercase. Duplicate names repeat the entry. Empty values kept. `hash==0` skipped. Count/byte caps set `truncated=true` before further copy. Null elts with `nelts>0`, cycles, and impossible metadata are host errors, not silent truncation. | `tests/ngx_request_read.rs` | +| `ngx.req.get_uri_args` | `ngx::req::get_uri_args(max_args: unknown) -> RequestArgsResult` | adapted | `PHASE_REQ_GET_URI_ARGS` | default 100; hard 1024; 65536-byte query | Fixed fields `args: array` and `truncated: bool`; `ArgEntry { name: string, value: Option }`. Uses the crate-private `decode_query_args` helper shared with `ngx::decode_args`. Trailing empty `&` at max_args is not truncated. `0` clamps to 1024. | `tests/ngx_request_read.rs`, `tests/ngx_codec.rs` | +| `ngx.req.get_post_args` | `ngx::req::get_post_args(max_args: unknown) -> RequestArgsResult` | adapted | `PHASE_REQ_GET_POST_ARGS` | same arg caps; 65536-byte body | Sync parse after `read_body` completion from memory **or** a bounded temp file. Fixed fields `args: array` and `truncated: bool`, where `ArgEntry { name: string, value: Option }`. Duplicate keys repeat the entry. Unread, still-reading, discarded, incomplete, or corrupt bodies are structured host errors. Does not delete NGINX temp files. Never initiates body read or yield. | `tests/ngx_request_body.rs` | +| `ngx.req.read_body` | `ngx::req::read_body() -> ()` | adapted | `PHASE_REQ_READ_BODY` | async; rewrite/access/content | Calls `ngx_http_read_client_request_body` through a C shim. Undoes that API's `r->main->count++` for `NGX_OK`/`NGX_AGAIN` so the phase driver (content `NGX_DONE` / access `NGX_AGAIN`) owns count. Premature client close (`c->error` / confirmed EOF) is `499`; timeout stays `408`; protocol/`413` statuses are preserved. A C read-event wrapper peeks EOF then finalizes `499` once, otherwise invokes NGINX's original body handler. Generation-safe `nginx_op` cell: callback completes once; cleanup cancels once; stale callbacks no-op. Futures hold no request/VM/module pointers. Idempotent after completion. Forbidden in Log. | `tests/ngx_request_body.rs`, `tests/lifecycle.rs` | +| `ngx.req.discard_body` | `ngx::req::discard_body() -> ()` | adapted | `PHASE_REQ_READ_BODY` | sync; rewrite/access/content | Calls `ngx_http_discard_request_body`. Never waits. Idempotent (`NGX_OK`/`NGX_AGAIN`). Discard-after-read is a no-op; read-after-discard completes immediately. Forbidden in Log. | `tests/ngx_request_body.rs` | +| `ngx.req.get_body_data` | `ngx::req::get_body_data() -> Option` | adapted | `PHASE_REQ_GET_BODY_DATA` | 65536-byte copy | Nullary like OpenResty. Owned copy of in-memory buffers. File-backed returns `Option::None`. Empty memory buffers are skipped; inverted ranges and cycles are host errors. Allowed in Log (completed-body getter). | `tests/ngx_request_body.rs` | +| `ngx.req.get_body_file` | `ngx::req::get_body_file() -> Option` | adapted | `PHASE_REQ_GET_BODY_FILE` | 4096-byte path | Owned copy of NGINX temp-file path. Memory-backed returns `Option::None`. Does not delete the file. Forbidden in Log. | `tests/ngx_request_body.rs` | +| `ngx.req.set_body_data` | `ngx::req::set_body_data(data: string) -> ()` | adapted | `PHASE_REQ_SET_BODY` | 65536-byte memory body | Transactionally allocates a complete `ngx_http_request_body_t` / chain / buf plus bytes from the request pool, then swaps `r->request_body` only after Content-Length (real header hash, linked duplicates updated, dedicated pointer coherent) and chunked/Transfer-Encoding hide-hash sync succeed. Empty body installs a zero-length memory buf. Prior body remains on allocation failure. Rejected in Log, without `RequestWrite`/live request, while `reading_body`/`discard_body`/`header_sent`, while a read-body wait is armed, and on subrequests (`r != r->main`). | `tests/ngx_request_body_mutation.rs` | +| `ngx.req.set_body_file` | `ngx::req::set_body_file(path: string, auto_clean: unknown) -> ()` | adapted | `PHASE_REQ_SET_BODY` | 4096-byte path; 65536-byte file | `lstat` then `O_NOFOLLOW\|O_CLOEXEC\|O_NONBLOCK` open; post-open `fstat` must stay a regular file with the same `st_dev`/`st_ino` as `lstat`. Regular files only (symlink/dir/FIFO rejected before open). `auto_clean` `()`/`false` leaves the source path; `true` copies into a private temp. A C `ngx_pool_cleanup_add` handler (`ngx_pool_delete_file` vs `ngx_pool_cleanup_file`) owns the fd: unlink the copy exactly once when `auto_clean`, close once, never unlink the caller source. Errors before cleanup registration leave fd with the caller; after registration a failed header commit nulls the handler. File buf matches OpenResty: `rb->buf=NULL`, `in_file`/`last_buf`/`last_in_chain`, no `temp_file` bit. Content-Length uses the real header hash so proxy hide-hash emits exactly one CL; Transfer-Encoding chain entries keep the hide-hash and are emptied. | `tests/ngx_request_body_mutation.rs` | +| `ngx.req.init_body` | `ngx::req::init_body(buffer_size: unknown) -> ()` | adapted | `PHASE_REQ_SET_BODY` | default/hard cap 65536 | Request-owned builder in `RequestPhaseCtx` (test fallback only without module ctx). `()` uses the default cap; integer must be `0..=65536`. Repeated init resets the builder. Does not mutate NGINX body until `finish_body`. | `tests/ngx_request_body_mutation.rs` | +| `ngx.req.append_body` | `ngx::req::append_body(data: string) -> ()` | adapted | `PHASE_REQ_SET_BODY` | cumulative builder cap | Copies bytes into the builder. Fails without `init_body` or when the cap would be exceeded; the builder is unchanged on oversize. | `tests/ngx_request_body_mutation.rs` | +| `ngx.req.finish_body` | `ngx::req::finish_body() -> ()` | adapted | `PHASE_REQ_SET_BODY` | same as `set_body_data` | Atomically installs one coherent in-memory body. Repeated finish / finish without init fail. Allocation failure restores the builder. Dropped on request cleanup/error/disconnect/shutdown. | `tests/ngx_request_body_mutation.rs` | | `ngx.req.is_internal` | `ngx::req::is_internal() -> bool` | adapted | `PHASE_REQ_FLAGS` | none | Live C `r->internal` via a headers-compiled shim (`ngx_rustscript_request_is_internal`). nginx-sys bindgen bitfield getters are the wrong ABI; neighboring C flags such as `filter_finalize` must not read as internal (`REQ_INTERNAL_REDIR=true`). | `tests/ngx_request_read.rs`, `tests/ngx_request_abi.rs` | | `ngx.req.start_time` | `ngx::req::start_time() -> float` | supported | `PHASE_REQ_FLAGS` | none | `start_sec + start_msec/1000`. | `tests/ngx_request_read.rs` | | `ngx.is_subrequest` | `ngx::is_subrequest() -> bool` | supported | `PHASE_REQ_FLAGS` | none | `r != r->main`. Live false-path on main requests (`REQ_SUB=false`). `ngx_http_core_access_phase` skips all access handlers when `r != r->main`, so SSI/mirror/auth_request subrequests cannot run `access_by_rss_file`. True is stub-covered; SSI include still returns `sub-ok` as evidence the subrequest ran. | `tests/ngx_request_read.rs` | | `ngx.headers_sent` | `ngx::headers_sent() -> bool` | supported | `PHASE_HEADERS_SENT` | none | Live C `r->header_sent` via `ngx_rustscript_request_headers_sent`. Access-phase scripts observe `false`. | `tests/ngx_request_read.rs`, `tests/ngx_request_abi.rs` | -| `ngx.var.VARIABLE` | `ngx::var::get(name: string) -> optional` | adapted | `PHASE_VAR_GET` | 1024-byte name; 65536-byte value | Indexed lookup with `ngx_hash_strlow`. Missing is `null`; present empty is `""`. Rejects empty/NUL names. | `tests/ngx_request_read.rs` (`var_get_*`) | -| `ngx.req.set_method` | `ngx::req::set_method(method_id: int) -> null` | adapted | `PHASE_REQ_SET_METHOD` | OpenResty method IDs only | Pool-owned canonical `method_name` is allocated successfully before numeric/text fields change. Allocation failure leaves method untouched. Unsupported IDs fail before mutation. | `tests/ngx_request_mutation.rs`, `tests/ngx_task7_hardening.rs` | -| `ngx.req.set_uri` | `ngx::req::set_uri(uri: string, jump: unknown) -> null` | adapted | `PHASE_REQ_SET_URI` | 65536-byte URI | `jump` `null`/`false` rewrites `r->uri`, sets `internal`, clears `valid_unparsed_uri`/`valid_location`/`uri_changed`, and calls `ngx_http_set_exten` via the C shim. `jump=true` is rejected **before** mutation. Empty/unsafe bytes (0x00-0x08, 0x0A-0x1F, 0x7F) fail first. | `tests/ngx_request_mutation.rs` | -| `ngx.req.set_uri_args` | `ngx::req::set_uri_args(args: unknown) -> null` | adapted | `PHASE_REQ_SET_URI` | 65536-byte encoded args | String: escape 0x00-0x20 and 0x7F as `%XX`. Map: reuse `encode_args`. Pool-owned `r->args`; `valid_unparsed_uri=0`. | `tests/ngx_request_mutation.rs` | -| `ngx.req.set_header` | `ngx::req::set_header(name: string, value: unknown) -> null` | adapted | `PHASE_REQ_SET_HEADER` | 256-byte token name; 8192-byte value | `null` clears. String/array values. Token + no CRLF/NUL. Validates the list, then Host (`ngx_http_lua_validate_host` port-aware, updates `headers_in.server`, invalidates cached `$host`), Content-Length (`ngx_atoof` before mutation; every value must be a decimal; dedicated `n` uses the first), User-Agent browser flags, and Connection (`close` substring first, then `keep-alive`). Hash-zero then push. Dedicated Host/Content-Length/Content-Type/User-Agent/Connection/Cookie/Referer stay consistent. Allocation/list-push failure rolls back added entries. | `tests/ngx_request_mutation.rs`, `tests/ngx_task7_hardening.rs` | -| `ngx.req.clear_header` | `ngx::req::clear_header(name: string) -> null` | adapted | `PHASE_REQ_SET_HEADER` | same name policy | Hash-zero matching list entries and reset dedicated fields (`content_length_n=-1`, `connection_type=0`). | `tests/ngx_request_mutation.rs` | -| `ngx.var.VARIABLE` write | `ngx::var::set(name: string, value: unknown) -> null` | adapted | `PHASE_VAR_GET` | 1024-byte name; 65536-byte value | Indexed/set_handler write via C shim. Honors CHANGEABLE. `null` → `not_found`. Empty string is present empty. Read-only and unknown names fail. | `tests/ngx_request_mutation.rs` | +| `ngx.var.VARIABLE` | `ngx::var::get(name: string) -> Option` | adapted | `PHASE_VAR_GET` | 1024-byte name; 65536-byte value | Indexed lookup with `ngx_hash_strlow`. Missing is `Option::None`; present empty is `Option::Some("")`. Rejects empty/NUL names. | `tests/ngx_request_read.rs` (`var_get_*`) | +| `ngx.req.set_method` | `ngx::req::set_method(method_id: int) -> ()` | adapted | `PHASE_REQ_SET_METHOD` | OpenResty method IDs only | Pool-owned canonical `method_name` is allocated successfully before numeric/text fields change. Allocation failure leaves method untouched. Unsupported IDs fail before mutation. | `tests/ngx_request_mutation.rs`, `tests/ngx_task7_hardening.rs` | +| `ngx.req.set_uri` | `ngx::req::set_uri(uri: string, jump: unknown) -> ()` | adapted | `PHASE_REQ_SET_URI` | 65536-byte URI | `jump` `()`/`false` rewrites `r->uri`, sets `internal`, clears `valid_unparsed_uri`/`valid_location`/`uri_changed`, and calls `ngx_http_set_exten` via the C shim. `jump=true` is rejected **before** mutation. Empty/unsafe bytes (0x00-0x08, 0x0A-0x1F, 0x7F) fail first. | `tests/ngx_request_mutation.rs` | +| `ngx.req.set_uri_args` | `ngx::req::set_uri_args(args: unknown) -> ()` | adapted | `PHASE_REQ_SET_URI` | 65536-byte encoded args | String: escape 0x00-0x20 and 0x7F as `%XX`. Map: reuse `encode_args`. Pool-owned `r->args`; `valid_unparsed_uri=0`. | `tests/ngx_request_mutation.rs` | +| `ngx.req.set_header` | `ngx::req::set_header(name: string, value: unknown) -> ()` | adapted | `PHASE_REQ_SET_HEADER` | 256-byte token name; 8192-byte value | `()` clears. String/array values. Token + no CRLF/NUL. Validates the list, then Host (`ngx_http_lua_validate_host` port-aware, updates `headers_in.server`, invalidates cached `$host`), Content-Length (`ngx_atoof` before mutation; every value must be a decimal; dedicated `n` uses the first), User-Agent browser flags, and Connection (`close` substring first, then `keep-alive`). Hash-zero then push. Dedicated Host/Content-Length/Content-Type/User-Agent/Connection/Cookie/Referer stay consistent. Allocation/list-push failure rolls back added entries. | `tests/ngx_request_mutation.rs`, `tests/ngx_task7_hardening.rs` | +| `ngx.req.clear_header` | `ngx::req::clear_header(name: string) -> ()` | adapted | `PHASE_REQ_SET_HEADER` | same name policy | Hash-zero matching list entries and reset dedicated fields (`content_length_n=-1`, `connection_type=0`). | `tests/ngx_request_mutation.rs` | +| `ngx.var.VARIABLE` write | `ngx::var::set(name: string, value: unknown) -> ()` | adapted | `PHASE_VAR_GET` | 1024-byte name; 65536-byte value | Indexed/set_handler write via C shim. Honors CHANGEABLE. `()` → `not_found`. Empty string is present empty. Read-only and unknown names fail. | `tests/ngx_request_mutation.rs` | --- diff --git a/docs/api/response.md b/docs/api/response.md index b5b162c..eb958f0 100644 --- a/docs/api/response.md +++ b/docs/api/response.md @@ -12,11 +12,11 @@ Content-Type/Length/Location/Last-Modified/ETag. | OpenResty API | RustScript callable | Status | Phases | Limits | Semantic differences | Acceptance | | --- | --- | --- | --- | --- | --- | --- | | `ngx.status` | `ngx::status::get() -> int` | adapted | `PHASE_RESPONSE_META` | none | Prefers `err_status`, then `headers_out.status`, then HTTP/0.9 maps to `9`. Unset HTTP/1.x is `0`. Capability `ResponseRead`. Allowed in `log_by_rss_file`. | `tests/ngx_request_mutation.rs`, `tests/log_config.rs` | -| `ngx.status` | `ngx::status::set(status: int) -> null` | adapted | `PHASE_RESPONSE_META` | 0-999 | Rejected after headers sent. Capability `ResponseWrite`. Rejected by `log_by_rss_file`. | `tests/ngx_request_mutation.rs`, `tests/log_config.rs` | -| `ngx.header.HEADER` | `ngx::header::get(name: string) -> unknown` | adapted | `PHASE_RESPONSE_META` | 256-byte token | Missing `null`; one string; duplicates array. Owned copies. Content-Type/Content-Length prefer dedicated fields. Capability `ResponseRead`. Allowed in `log_by_rss_file`. | `tests/ngx_request_mutation.rs`, `tests/log_config.rs` | -| `ngx.header.HEADER` | `ngx::header::set(name: string, value: unknown) -> null` | adapted | `PHASE_RESPONSE_META` | 8192-byte value | `null` clears. Injection (CRLF/NUL/non-token) rejected. Relative Location does not set the dedicated pointer. New list slots are allocated before old matching names are deactivated. Capability `ResponseWrite`. Rejected by `log_by_rss_file`. | `tests/ngx_request_mutation.rs` | -| — | `ngx::header::set_all(headers: map) -> null` | adapted | `PHASE_RESPONSE_META` | same | Validate every pair first. Allocate the intended representation, then deactivate previous headers. Validation or allocation failure leaves the logical list unchanged (pool memory is not rolled back). | `tests/ngx_request_mutation.rs` | -| — | `ngx::header::clear() -> null` | adapted | `PHASE_RESPONSE_META` | — | Hash-zero every output header and reset dedicated fields. | `tests/ngx_request_mutation.rs` | +| `ngx.status` | `ngx::status::set(status: int) -> ()` | adapted | `PHASE_RESPONSE_META` | 0-999 | Rejected after headers sent. Capability `ResponseWrite`. Rejected by `log_by_rss_file`. | `tests/ngx_request_mutation.rs`, `tests/log_config.rs` | +| `ngx.header.HEADER` | `ngx::header::get(name: string) -> Option` | adapted | `PHASE_RESPONSE_META` | 256-byte token | Missing is `Option::None`; duplicate names yield the first value. Owned copies. Content-Type/Content-Length prefer dedicated fields. Capability `ResponseRead`. Allowed in `log_by_rss_file`. | `tests/ngx_request_mutation.rs`, `tests/log_config.rs` | +| `ngx.header.HEADER` | `ngx::header::set(name: string, value: unknown) -> ()` | adapted | `PHASE_RESPONSE_META` | 8192-byte value | `()` clears. Injection (CRLF/NUL/non-token) rejected. Relative Location does not set the dedicated pointer. New list slots are allocated before old matching names are deactivated. Capability `ResponseWrite`. Rejected by `log_by_rss_file`. | `tests/ngx_request_mutation.rs` | +| — | `ngx::header::set_all(headers: map) -> ()` | adapted | `PHASE_RESPONSE_META` | same | Validate every pair first. Allocate the intended representation, then deactivate previous headers. Validation or allocation failure leaves the logical list unchanged (pool memory is not rolled back). | `tests/ngx_request_mutation.rs` | +| — | `ngx::header::clear() -> ()` | adapted | `PHASE_RESPONSE_META` | — | Hash-zero every output header and reset dedicated fields. | `tests/ngx_request_mutation.rs` | --- diff --git a/docs/api/semaphore.md b/docs/api/semaphore.md index 91dcf84..534ed87 100644 --- a/docs/api/semaphore.md +++ b/docs/api/semaphore.md @@ -23,8 +23,8 @@ only calls the eventfd-backed waker of the current request. | OpenResty API | RustScript callable | Status | Phases | Yield | Limits | Semantic differences | Acceptance | | --- | --- | --- | --- | --- | --- | --- | --- | | `ngx.semaphore.new` | `ngx::semaphore::new(count) -> resource` | adapted | `PHASE_SEMAPHORE` | sync | `count` int `0..=1000000`; ≤1024 live semaphores per worker | Worker-owned object, not userdata; the script sees an `i64` handle for its own VM table. | `tests/ngx_semaphore.rs`, `src/semaphore.rs` | -| `sem:wait` | `ngx::semaphore::wait(sem, timeout) -> bool` | adapted | `PHASE_SEMAPHORE` | Waiting | `timeout` seconds; `null` = no timer, `0` = immediate check; shares the 1024 in-flight wait cap | Returns `true` on acquire and `false` on timeout instead of OpenResty's `nil, "timeout"` pair. `null` (not `0`) means "wait forever". FIFO: the oldest waiter wins each permit. | `tests/ngx_semaphore.rs`, `src/semaphore.rs` | -| `sem:post` | `ngx::semaphore::post(sem, count) -> null` | adapted | `PHASE_SEMAPHORE` | sync | `count` int `0..=1000000` or `null` (=1); counter ceiling 1000000 | Posting past the ceiling is a `HostError` instead of silently saturating. `0` wakes nobody. | `tests/ngx_semaphore.rs`, `src/semaphore.rs` | +| `sem:wait` | `ngx::semaphore::wait(sem, timeout) -> bool` | adapted | `PHASE_SEMAPHORE` | Waiting | `timeout` seconds; `()` = no timer, `0` = immediate check; shares the 1024 in-flight wait cap | Returns `true` on acquire and `false` on timeout instead of OpenResty's `nil, "timeout"` pair. `()` (not `0`) means "wait forever". FIFO: the oldest waiter wins each permit. | `tests/ngx_semaphore.rs`, `src/semaphore.rs` | +| `sem:post` | `ngx::semaphore::post(sem, count) -> ()` | adapted | `PHASE_SEMAPHORE` | sync | `count` int `0..=1000000` or `()` (=1); counter ceiling 1000000 | Posting past the ceiling is a `HostError` instead of silently saturating. `0` wakes nobody. | `tests/ngx_semaphore.rs`, `src/semaphore.rs` | | `sem:count` | `ngx::semaphore::count(sem) -> int` | adapted | `PHASE_SEMAPHORE` | sync | — | Matches OpenResty: permits ready minus queued waiters, so a negative value is the FIFO wait depth. | `tests/ngx_semaphore.rs`, `src/semaphore.rs` | `PHASE_SEMAPHORE` is `access` + `content` only. `log_by_rss_file` fails @@ -37,13 +37,13 @@ the closed Log allowlist also rejects the family and capability. invalid counts, and a worker already holding 1024 live semaphores, fail with a named `HostError` at request time. - `wait(sem, timeout)`: `timeout` is a number of seconds (fractions - allowed). `null` waits without arming a timer (wait forever); `0` is an + allowed). `()` waits without arming a timer (wait forever); `0` is an immediate, non-blocking check that returns `false` when no permit is free; a positive number arms the owned timeout. Negative, `NaN`, infinite, and non-number timeouts are rejected before the future is - submitted. RustScript adds `null` for an unbounded wait; OpenResty and + submitted. RustScript adds `()` for an unbounded wait; OpenResty and RustScript both use `0` for the immediate attempt. -- `post(sem, count)`: `count` is an integer or `null` (one permit). +- `post(sem, count)`: `count` is an integer or `()` (one permit). Negative, fractional, and non-number counts are rejected. `0` is a no-op. `count + current` above 1000000 is a `HostError`. - `count(sem)`: read-only. diff --git a/docs/api/shared-dict.md b/docs/api/shared-dict.md index 7787211..08bb9e1 100644 --- a/docs/api/shared-dict.md +++ b/docs/api/shared-dict.md @@ -10,45 +10,58 @@ is NGINX shared memory with a slab-backed rbtree/LRU store, the `ngx::shared::dict(name)` returns a typed handle; later methods take that handle. Multi-return OpenResty values use host-catalog named structs (`SharedGetResult`, `SharedStoreResult`, `SharedSafeStoreResult`, -`SharedIncrResult`, `SharedNumberResult`). Do not redeclare them in -guest scripts. `SharedGetResult.value` is `unknown?` because stored -values are heterogeneous; numeric results use `number?`. -Truly dynamic-key collections remain `map` fields inside typed envelopes, such -as `DecodedArgsResult.args` and `RequestHeadersResult.headers`. -Optional `exptime` and `flags` are explicit `null`. Values are bool, int, -float, string, or bytes; `set(..., null, ...)` deletes. `add`/`replace` reject -null. +`SharedIncrResult`, `SharedNumberResult`, `SharedValue`). Do not redeclare +them in guest scripts. A stored value is heterogeneous (bool, int, float, +string, or bytes), so `SharedGetResult.value` is `Option` and a +`SharedValue` is a tagged record: + +```rss +struct SharedValue { + kind: string, // "bool" | "int" | "float" | "string" | "bytes" + bool_value: Option, + int_value: Option, + float_value: Option, + text_value: Option, + bytes_value: Option +} +``` + +Exactly one payload matches `kind`. Absence is `Option::None`, never `null`. +Optional `exptime` and `flags` parameters accept `()` for the default. Values +are bool, int, float, string, or bytes; `set(..., (), ...)` deletes. +`add`/`replace` reject an absent value. Request argument and header bags are +typed entry arrays (`ArgEntry`, `HeaderEntry`) rather than dynamic maps. The shared-dict result shapes are: ```rss struct SharedGetResult { - value: unknown?, + value: Option, flags: int, stale: bool, - error: string? + error: Option } struct SharedStoreResult { ok: bool, - error: string?, + error: Option, forcible: bool } struct SharedSafeStoreResult { ok: bool, - error: string? + error: Option } struct SharedIncrResult { - value: number?, - error: string?, + value: Option, + error: Option, forcible: bool } struct SharedNumberResult { - value: number?, - error: string? + value: Option, + error: Option } ``` @@ -56,10 +69,15 @@ For example: ```rss let added: SharedSafeStoreResult = - ngx::shared::safe_add(dict, key, 1, WINDOW_SECONDS, null); + ngx::shared::safe_add(dict, key, 1, WINDOW_SECONDS, ()); if added.ok { // ... } +let stored: SharedGetResult = ngx::shared::get(dict, key); +let text = match stored.value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" +}; ``` Keys are at most 65535 bytes; values at most 65536 bytes. Empty keys are rejected. LRU eviction reports `forcible`; `safe_*` @@ -69,27 +87,27 @@ request-owned VMs. | OpenResty API | RustScript callable | Status | Phases | Limits | Semantic differences | Acceptance | | --- | --- | --- | --- | --- | --- | --- | | `ngx.shared.DICT` | `ngx::shared::dict(name: string) -> resource` | adapted | `PHASE_DICT_LOOKUP` | unknown zone is a host error | Typed `ngx.shared.dict` handle, not `ngx.shared.NAME` field | `tests/ngx_shared_dict.rs` | -| `ngx.shared.DICT.get` | `ngx::shared::get(dict, key) -> SharedGetResult` | adapted | `PHASE_DICT_GET` | missing → `{value: null}` | Fields `value`, `flags`, `stale`, `error` | `tests/ngx_shared_dict.rs` | +| `ngx.shared.DICT.get` | `ngx::shared::get(dict, key) -> SharedGetResult` | adapted | `PHASE_DICT_GET` | missing → `value: Option::None` | Fields `value`, `flags`, `stale`, `error` | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.get_stale` | `ngx::shared::get_stale(dict, key) -> SharedGetResult` | adapted | `PHASE_DICT_GET` | stale expired values | `stale` is bool | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.set` | `ngx::shared::set(dict, key, value, exptime, flags) -> SharedStoreResult` | adapted | `PHASE_DICT_WRITE` | LRU up to 30 | Fields `ok`, `error`, `forcible` | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.safe_set` | `ngx::shared::safe_set(dict, key, value, exptime, flags) -> SharedSafeStoreResult` | adapted | `PHASE_DICT_WRITE` | no eviction | Fields `ok`, `error` | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.add` | `ngx::shared::add(dict, key, value, exptime, flags) -> SharedStoreResult` | adapted | `PHASE_DICT_WRITE` | exists → error | Fields `ok`, `error`, `forcible` | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.safe_add` | `ngx::shared::safe_add(dict, key, value, exptime, flags) -> SharedSafeStoreResult` | adapted | `PHASE_DICT_WRITE` | no eviction | Fields `ok`, `error` | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.replace` | `ngx::shared::replace(dict, key, value, exptime, flags) -> SharedStoreResult` | adapted | `PHASE_DICT_WRITE` | missing → not found | Fields `ok`, `error`, `forcible` | `tests/ngx_shared_dict.rs` | -| `ngx.shared.DICT.delete` | `ngx::shared::delete(dict, key) -> null` | adapted | `PHASE_DICT_WRITE` | — | Missing delete is success | `tests/ngx_shared_dict.rs` | +| `ngx.shared.DICT.delete` | `ngx::shared::delete(dict, key) -> bool` | adapted | `PHASE_DICT_WRITE` | — | `true` when an entry was removed, `false` when missing | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.incr` | `ngx::shared::incr(dict, key, value, init, init_ttl) -> SharedIncrResult` | adapted | `PHASE_DICT_WRITE` | numbers only | Fields `value`, `error`, `forcible` | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.ttl` | `ngx::shared::ttl(dict, key) -> SharedNumberResult` | adapted | `PHASE_DICT_WRITE` | never-expire is `0` | Fields `value`, `error` | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.expire` | `ngx::shared::expire(dict, key, exptime) -> SharedSafeStoreResult` | adapted | `PHASE_DICT_WRITE` | — | Fields `ok`, `error` | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.capacity` | `ngx::shared::capacity(dict) -> int` | adapted | `PHASE_DICT_WRITE` | — | Zone size in bytes | `tests/ngx_shared_dict.rs` | | `ngx.shared.DICT.free_space` | `ngx::shared::free_space(dict) -> int` | adapted | `PHASE_DICT_WRITE` | approximate | Process-local: capacity − used; shm: `pfree * pagesize` | `tests/ngx_shared_dict.rs` | -| `ngx.shared.DICT.lpush` | `ngx::shared::lpush(dict, key, value) -> SharedNumberResult` | adapted | `PHASE_DICT_WRITE` | list len ≤ 65536; no LRU eviction | Fields `value`, `error`; bool/null/map → `bad value type`; scalar conflict → `value not a list` without mutating the node | `tests/ngx_shared_dict_list.rs` | +| `ngx.shared.DICT.lpush` | `ngx::shared::lpush(dict, key, value) -> SharedNumberResult` | adapted | `PHASE_DICT_WRITE` | list len ≤ 65536; no LRU eviction | Fields `value`, `error`; bool/`()`/map → `bad value type`; scalar conflict → `value not a list` without mutating the node | `tests/ngx_shared_dict_list.rs` | | `ngx.shared.DICT.rpush` | `ngx::shared::rpush(dict, key, value) -> SharedNumberResult` | adapted | `PHASE_DICT_WRITE` | same | Same result type as lpush; appends | `tests/ngx_shared_dict_list.rs` | -| `ngx.shared.DICT.lpop` | `ngx::shared::lpop(dict, key) -> SharedGetResult` | adapted | `PHASE_DICT_WRITE` | decode before unlink | Fields `value`, `flags`, `stale`, `error`; empty/missing → `{value: null}`; decode failure → `{error: "bad value type"}` and the item stays queued | `tests/ngx_shared_dict_list.rs` | +| `ngx.shared.DICT.lpop` | `ngx::shared::lpop(dict, key) -> SharedGetResult` | adapted | `PHASE_DICT_WRITE` | decode before unlink | Fields `value`, `flags`, `stale`, `error`; empty/missing → `value: Option::None`; decode failure → `{error: "bad value type"}` and the item stays queued | `tests/ngx_shared_dict_list.rs` | | `ngx.shared.DICT.rpop` | `ngx::shared::rpop(dict, key) -> SharedGetResult` | adapted | `PHASE_DICT_WRITE` | same | Same result type as lpop; pops tail | `tests/ngx_shared_dict_list.rs` | | `ngx.shared.DICT.llen` | `ngx::shared::llen(dict, key) -> SharedNumberResult` | adapted | `PHASE_DICT_WRITE` | — | Missing/expired → `{value: 0}`; scalar → `value not a list` | `tests/ngx_shared_dict_list.rs` | -| `ngx.shared.DICT.flush_all` | `ngx::shared::flush_all(dict) -> null` | adapted | `PHASE_DICT_WRITE` | LRU; ≤1024 visits/call | Each call first frees already-expired keys (visit-capped), then marks live LRU keys with `expires=1` (visit-capped), then `expire(0)`. Repeated calls make monotonic progress across >1024 keys; one call does not wipe the zone. | `tests/ngx_shared_dict_list.rs` | -| `ngx.shared.DICT.flush_expired` | `ngx::shared::flush_expired(dict, max) -> int` | adapted | `PHASE_DICT_WRITE` | `max` 0/null still ≤ 1024 results; visits ≤ 1024 | Freed count. Walk stops at the result cap *or* the hard visit cap, whichever comes first. OpenResty `0` is unlimited. | `tests/ngx_shared_dict_list.rs` | -| `ngx.shared.DICT.get_keys` | `ngx::shared::get_keys(dict, max) -> array` | adapted | `PHASE_DICT_WRITE` | read-only; `max` 0/null still ≤ 1024 results; visits ≤ 1024 | Oldest-first, skips expired, never deletes or force-expires. OpenResty `0` returns all keys; this project keeps both a result cap and a visit cap. | `tests/ngx_shared_dict_list.rs` | +| `ngx.shared.DICT.flush_all` | `ngx::shared::flush_all(dict) -> ()` | adapted | `PHASE_DICT_WRITE` | LRU; ≤1024 visits/call | Each call first frees already-expired keys (visit-capped), then marks live LRU keys with `expires=1` (visit-capped), then `expire(0)`. Repeated calls make monotonic progress across >1024 keys; one call does not wipe the zone. | `tests/ngx_shared_dict_list.rs` | +| `ngx.shared.DICT.flush_expired` | `ngx::shared::flush_expired(dict, max) -> int` | adapted | `PHASE_DICT_WRITE` | `max` 0/`()` still ≤ 1024 results; visits ≤ 1024 | Freed count. Walk stops at the result cap *or* the hard visit cap, whichever comes first. OpenResty `0` is unlimited. | `tests/ngx_shared_dict_list.rs` | +| `ngx.shared.DICT.get_keys` | `ngx::shared::get_keys(dict, max) -> array` | adapted | `PHASE_DICT_WRITE` | read-only; `max` 0/`()` still ≤ 1024 results; visits ≤ 1024 | Oldest-first, skips expired, never deletes or force-expires. OpenResty `0` returns all keys; this project keeps both a result cap and a visit cap. | `tests/ngx_shared_dict_list.rs` | List nodes are slab-owned; every mutation is atomic under the shm mutex. Failed item allocation rolls back an empty list. `get`/`incr` on a list return structured errors (`value is a list` / `not a number`) and never overwrite the node. List items are int/float/string/bytes. `get_keys` is read-only under the shm mutex: it never force-expires or deletes, skips expired keys, and stops at both the result cap and a 1024-visit cap. `flush_all` / `flush_expired` likewise bound visits independently of hits. There is no process-local production fallback: `cargo test` uses an in-process zone; the cdylib talks to NGINX shm only. diff --git a/docs/api/standard-async.md b/docs/api/standard-async.md index 18b29d2..e59e0ae 100644 --- a/docs/api/standard-async.md +++ b/docs/api/standard-async.md @@ -7,10 +7,29 @@ features. This page defines the script-facing interface that ngx-rustscript supports in `access_by_rss_file` and `content_by_rss_file`. Calls that perform asynchronous work yield and resume through the module's -eventfd scheduler. The tables identify synchronous exceptions. All standard -host domains are rejected in `log_by_rss_file`; async calls cannot wait there, -and the module keeps the whole I/O/HTTP/SQLite surface fail-closed in that -phase. +eventfd scheduler. All standard host domains are rejected in +`log_by_rss_file`; async calls cannot wait there, and the module keeps the +whole I/O/HTTP/SQLite surface fail-closed in that phase. + +Handles are pd-vm resources, not raw integers. A resource is passed by borrow +(`io::read_all(file)`, `http::response::status(response)`) or taken by value +when the call consumes it (`io::close(file)`, `http::client::request(request)`). +A builder that is mutated must be declared `let mut` and passed as +`&mut request`. + +## Option and Result + +The pinned contract uses the guest types, not the VM's internal null value: + +- `Option` is a tagged value: `Option::Some(payload)` or `Option::None`. + Absence is never `null`. +- `Result` is a tagged value: `Result::Ok(payload)` or + `Result::Err(error)`. `?` propagates inside a `Result`-returning function. +- `()` is the logical Unit: a call with no result, and the value of an empty + block. + +Explicit `null` literals and legacy `T?` annotations are rejected by the +RustScript compiler. ## I/O @@ -20,27 +39,31 @@ writes are each capped at 1 MiB by ngx-rustscript's current policy. | RustScript callable | Return | Yield | Interface and behavior | | --- | --- | --- | --- | -| `io::open(path: string, mode: string)` | `int` handle | yes | Modes: `r`, `w`, `a`, `r+`, `w+`, `a+`. Any write-capable mode requires `rss_io_write on`. | -| `io::read_all(handle: int)` | `string` | yes | Reads all remaining UTF-8 text, then rejects the call if the result exceeds the read limit. | -| `io::read_line(handle: int)` | `string` | yes | Reads one line, including its line ending when present, up to the read limit. | -| `io::write(handle: int, text: string)` | `int` | yes | Writes text and returns the number of bytes written. A partial write is possible. | -| `io::flush(handle: int)` | `bool` | yes | Flushes writable handles and returns `true`; a readable handle is a successful no-op. | -| `io::close(handle: int)` | `bool` | yes | Closes the handle and returns `true`. Closed, foreign, or stale handles are rejected. | +| `io::open(path: string, mode: string)` | `Result` | yes | Modes: `r`, `w`, `a`, `r+`, `w+`, `a+`. Any write-capable mode requires `rss_io_write on`. A policy denial is a guest `Result::Err`. | +| `io::read_all(handle: io.file)` | `Result` | yes | Reads all remaining UTF-8 text, then rejects the call if the result exceeds the read limit. | +| `io::read_line(handle: io.file)` | `Result, string>` | yes | End of file is `Ok(None)`; a present but empty line is `Ok(Some(""))`. The single trailing newline is removed. | +| `io::write(handle: io.file, text: string)` | `Result` | yes | Writes text and returns the number of bytes written. A partial write is possible. | +| `io::flush(handle: io.file)` | `Result<(), string>` | yes | Flushes writable handles; a readable handle is a successful no-op. | +| `io::close(handle: io.file)` | `Result<(), string>` | yes | Closes the handle and releases it from the request scope. Closed, foreign, or stale handles are rejected. | | `io::exists(path: string)` | `bool` | yes | Tests a path below `rss_io_root`. | - -`io::popen(command: string, mode: string) -> int` exists in the pinned pd-vm -builtin surface, but ngx-rustscript always installs `allow_process: false`. -Scripts cannot grant process execution through an NGINX directive, so this -call is outside the supported ngx-rustscript interface. +| `io::popen(command: string, mode: string)` | `Result` | yes | Exists in the pinned pd-vm surface, but ngx-rustscript always installs `allow_process: false`; scripts cannot grant process execution through an NGINX directive. | Example: ```rss use io; -let file: int = io::open("input.txt", "r"); -let text: string = io::read_all(file); -io::close(file); +fn read_text(path: string) -> Result { + let file = io::open(path, "r")?; + let text = io::read_all(file)?; + io::close(file)?; + Result::Ok(text) +} + +let text = match read_text("input.txt") { + Result::Ok(text) => text, + Result::Err(message) => message +}; ``` ## HTTP client @@ -57,34 +80,28 @@ A location may contain one `rss_http_allow ` pair. Repeat `rss_http_scheme` to grant both `http` and `https`. Loopback and private addresses require `rss_http_allow_private on`. -### http::client::request - -```text -http::client::request(request: map) -> map -``` - -The request object has this interface. The pinned pd-vm adapter validates the -concrete contents at runtime, so the accepted payload shapes are the pinned -contract's: a header list (duplicates are preserved in order) and a discriminated -body. +### Request builder and buffered call -| Field | Type | Required | Meaning | +| RustScript callable | Return | Yield | Interface | | --- | --- | --- | --- | -| `method` | `string` | yes | `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `HEAD`, or `OPTIONS`; matching is case-insensitive. | -| `url` | `string` | yes | Absolute URL allowed by the location scheme, host, port, and private-address policy. | -| `headers` | `array` of `{ name: string, value: string }` | no | Header entries in order. Repeating one name sends duplicate headers; `Host`, `Content-Length`, `Transfer-Encoding`, and `Connection` are client-managed and rejected here. | -| `body` | `{ kind: "text", text: string }` or `{ kind: "bytes", bytes: bytes }` or `null` | no | Request payload variant. Omitted and `null` mean no body. | +| `http::request::new(method: string, url: string)` | `http.request` | no | Creates an owned request builder. Methods: `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `HEAD`, `OPTIONS` (case-insensitive). | +| `http::request::set_header(&mut request, name: string, value: string)` | `()` | no | Appends one header. Repeating a name sends duplicates. `Host`, `Content-Length`, `Transfer-Encoding`, and `Connection` are client-managed and rejected. | +| `http::request::set_body_text(&mut request, body: string)` | `()` | no | Sets a UTF-8 request body. | +| `http::request::set_body_bytes(&mut request, body: bytes)` | `()` | no | Sets a binary request body. | +| `http::client::request(request: http.request)` | `http.response` | yes | Sends the owned request and returns the buffered response resource. | -The response is a dynamic pd-vm map with these fixed top-level keys; response -header entries keep the pd-vm `HttpHeaderValue` text/bytes payload variant so -duplicate headers survive. +The URL must be absolute and allowed by the location scheme, host, port, and +private-address policy. -| Field | Type | Meaning | +### Buffered response + +| RustScript callable | Return | Meaning | | --- | --- | --- | -| `status` | `int` | HTTP status code. | -| `headers` | `array` of `{ name: string, value: { kind: "text", text: string } \| { kind: "bytes", bytes: bytes } }` | Response headers; duplicates are retained, but the adapter sorts and normalizes entries by name, so the original wire order is not guaranteed. | -| `body` | `bytes` | Buffered response body. Convert with `bytes::to_utf8` when text is required. | -| `url` | `string` | Final URL after redirects. | +| `http::response::status(response)` | `int` | HTTP status code. | +| `http::response::url(response)` | `string` | Final URL after redirects. | +| `http::response::body(response)` | `bytes` | Buffered response body. Convert with `bytes::to_utf8` when text is required. | +| `http::response::header_names(response)` | `array` | Header names in normalized order. | +| `http::response::header_values(response, name: string)` | `array` | All values for one header; duplicates are retained. | Current pd-vm defaults retained by ngx-rustscript are five redirects, a 1 MiB request body, 100 request headers, a 64 KiB serialized request-header block, @@ -97,23 +114,36 @@ Example: use http; use bytes as builtin_bytes; -let response: map = http::client::request({ - method: "GET", - url: "http://127.0.0.1:8081/data", - headers: [{ name: "accept", value: "application/json" }], - body: null -}); -let status: int = response["status"]; -let body_bytes: bytes = response["body"]; -let body: string = builtin_bytes::to_utf8(body_bytes); +let mut request = http::request::new("GET", "http://127.0.0.1:8081/data"); +http::request::set_header(&mut request, "accept", "application/json"); +let response = http::client::request(request); +let status: int = http::response::status(response); +let body: string = builtin_bytes::to_utf8(http::response::body(response)); +let content_types = http::response::header_values(response, "content-type"); ``` -The pinned catalog also exposes and binds -`http::client::sse(request: map, on_event: callable(map) -> map) -> map` in -access and content scripts. This surface is provisional in ngx-rustscript: -its NGINX request lifecycle has not been covered by the module's executable -acceptance suite, so the module does not yet make a lifecycle-compatibility -claim for it. +### Server-sent events + +`http::client::sse(request: http.request, on_event: fn(string, string, string, string) -> bool)` +consumes a request builder and returns an `http.sse_summary` resource. The +callback receives the SSE `event`, `data`, `id`, and `retry` fields and returns +`true` to continue or `false` to stop. + +| RustScript callable | Return | +| --- | --- | +| `http::sse_summary::outcome(summary)` | `string` | +| `http::sse_summary::status(summary)` | `int` | +| `http::sse_summary::url(summary)` | `string` | +| `http::sse_summary::items(summary)` | `int` | +| `http::sse_summary::bytes_received(summary)` | `int` | +| `http::sse_summary::bytes_sent(summary)` | `int` | +| `http::sse_summary::headers(summary)` | `http.headers` | +| `http::headers::names(headers)` | `array` | +| `http::headers::values(headers, name: string)` | `array` | + +This surface is provisional in ngx-rustscript: its NGINX request lifecycle has +not been covered by the module's executable acceptance suite, so the module +does not yet make a lifecycle-compatibility claim for it. ## SQLite @@ -123,29 +153,33 @@ remains disabled. | RustScript callable | Return | Yield | Interface | | --- | --- | --- | --- | -| `sqlite::open(options: map)` | `int` handle | no | Opens a connection using the options below. | -| `sqlite::execute(db: int, sql: string, params: array)` | `map` | yes | Executes one parameterized statement. | -| `sqlite::query(db: int, sql: string, params: array, limits: map)` | `map` | yes | Runs a bounded query. Pass `{}` for default limits. | -| `sqlite::transaction(db: int, statements: array)` | `array` | yes | Runs ordered statements atomically. | -| `sqlite::close(db: int)` | `null` | no | Closes the connection. Closed, foreign, or stale handles are rejected. | +| `sqlite::open(options: SqliteOpenOptions)` | `sqlite.connection` | no | Opens a connection using the options below. | +| `sqlite::execute(connection, sql: string, params: array)` | `SqliteExecuteResult` | yes | Executes one parameterized statement. | +| `sqlite::query(connection, sql: string, params: array, limits: SqliteLimits)` | `SqliteQueryResult` | yes | Runs a bounded query. Pass `{}` for default limits. | +| `sqlite::transaction(connection, statements: array)` | `array` | yes | Runs ordered statements atomically. | +| `sqlite::close(connection)` | `()` | no | Closes the connection and releases it from the request scope. | ### sqlite::open options ```text -{ - path: string, - mode: string?, - limits: map? +SqliteOpenOptions { + path: Option, + mode: Option, + root: Option, + limits: Option } ``` +Omitted `Option` fields normalize to `Option::None`. A present value is +`Option::Some(...)`. + | Field | Values | Default | | --- | --- | --- | -| `path` | Relative file path, or exactly `:memory:` with `mode: "memory"` | required | +| `path` | Relative file path, or exactly `:memory:` with `mode: "memory"` | required by the runtime | | `mode` | `memory`, `read_only`, `read_write`, `read_write_create` | `read_write_create` | | `limits` | Positive integer overrides bounded by the embedding ceilings below | current ceilings | -Supported `limits` keys and current ceilings: +Supported `limits` keys and current ceilings (each an `Option`): - `max_connections`: 16 - `max_statements`: 128 @@ -159,18 +193,30 @@ Supported `limits` keys and current ceilings: - `max_transaction_ms`: 5000 - `busy_timeout_ms`: 5000 -Per-call limits may reduce these ceilings and cannot raise them. Unknown limit -keys and zero or negative values are rejected. +Per-call limits may reduce these ceilings and cannot raise them. Non-positive +values are rejected. ### Parameters -`params` is an array whose entries may be `null`, `int`, `float`, `string`, -or `bytes`. SQL should use SQLite placeholders such as `?1`. +`params` is an array of tagged `SqliteValue` records. Exactly one payload +matches `kind`, except when `kind` is the string `"null"`: + +```text +SqliteValue { + kind: string, // "null" | "int" | "float" | "text" | "blob" + int_value: Option, + float_value: Option, + text_value: Option, + blob_value: Option +} +``` + +SQL should use SQLite placeholders such as `?1`. ### Execute result ```text -{ +SqliteExecuteResult { rows_affected: int, last_insert_rowid: int } @@ -178,84 +224,91 @@ or `bytes`. SQL should use SQLite placeholders such as `?1`. ### Query limits and result -The `limits` map accepted by `sqlite::query` is parsed by the same bounded -schema as `sqlite::open`. The query path applies `max_rows`, `max_columns`, -`max_result_bytes`, and `max_statement_bytes`. Other accepted keys do not -change an already-open connection or the connection-owned parameter and -operation ceilings. Values cannot exceed the connection's limits. The result -map is: +The `limits` argument accepted by `sqlite::query` is the same bounded +`SqliteLimits` schema as `sqlite::open`. The query path applies `max_rows`, +`max_columns`, `max_result_bytes`, and `max_statement_bytes`. Other accepted +keys do not change an already-open connection or the connection-owned parameter +and operation ceilings. Values cannot exceed the connection's limits. The result +is: ```text -{ +SqliteQueryResult { columns: array, - rows: array>, + rows: array, // SqliteRow { cells: array } truncated: bool, - next_cursor: int? // most recent returned first-column integer, when any + next_cursor: Option // most recent returned first-column integer, when any } ``` `next_cursor` retains the first-column integer from the most recent returned row whose first column is an `int`; later rows with another first-column type do not clear it. It is not the next unread row, and its presence is independent -of `truncated`. A row cell may be `null`, `int`, `float`, `string`, or `bytes`. +of `truncated`. ### Transaction statements and result -Each entry in `statements` has this interface: +Each entry in `statements` is a `SqliteStatement`: ```text -{ +SqliteStatement { sql: string, - params: array?, - query: bool?, - limits: map? + params: Option>, + query: Option, + limits: Option +} +``` + +`params` defaults to an empty array; `query` defaults to `false`. When `query` +is false, the corresponding result carries an execute result. When true, it +carries a query result: + +```text +SqliteTransactionResult { + kind: string, + execute: Option, + query: Option } ``` -`params` defaults to `[]`; `query` defaults to `false`. When `query` is false, -the corresponding result is an execute result map. When true, it is a query -result map. The call returns those maps in statement order and commits only if -all statements succeed. +The call returns those records in statement order and commits only if all +statements succeed. Example: ```rss use sqlite; -let db: int = sqlite::open({ - path: "pipeline.db", - mode: "read_write_create", - limits: {} +let db = sqlite::open({ + path: Option::Some("pipeline.db"), + mode: Option::Some("read_write_create") }); -let created: map = sqlite::execute( +let created: SqliteExecuteResult = sqlite::execute( db, "CREATE TABLE IF NOT EXISTS events (body TEXT NOT NULL)", [] ); -let inserted: map = sqlite::execute( +let inserted: SqliteExecuteResult = sqlite::execute( db, "INSERT INTO events(body) VALUES (?1)", - { "payload" } + [{ kind: "text", text_value: Option::Some("payload") }] ); -let rows: map = sqlite::query( +let rows: SqliteQueryResult = sqlite::query( db, "SELECT body FROM events ORDER BY rowid DESC LIMIT 10", [], - { max_rows: 10 } + { max_rows: Option::Some(10) } ); sqlite::close(db); ``` ## Runtime and policy summary -I/O and SQLite are pd-vm compiler builtins dispatched through its public -standard composition. HTTP is a pd-vm standard host whose dynamic `map` -interface is what ngx scripts are written against: the compile catalog leaves -it dynamic, configuration validates the name and phase, and the pd-vm adapter -is bound through the standard composition (the per-name path for request VMs, -and the same composition staged by the timer-host registry for owned-dispatch -programs). These maps and handles follow the pinned pd-vm public contract; -ngx-rustscript does not modify RustScript or pd-vm core types. +I/O, HTTP, and SQLite are pd-vm standard host modules with typed resource +handles. Their schemas are part of the module's compile catalog as well as the +runtime composition, so a script is type-checked against the exact contract the +registry installs: no handle position is dynamic, and no result is a +`map`/`unknown`/`null` carrier. ngx-rustscript does not modify RustScript or +pd-vm core types. The executable end-to-end acceptance is `nginx_async_pipeline_runs_io_http_sqlite_shared_dict_and_json` in diff --git a/docs/api/tcp.md b/docs/api/tcp.md index afba996..eb5198a 100644 --- a/docs/api/tcp.md +++ b/docs/api/tcp.md @@ -13,7 +13,7 @@ on close. Request cleanup cancels the pending future and closes the resource (including the fd). One pending read and one pending write may run concurrently; a second same-direction op is a structured host error. Send loops until the full payload is written. Receive modes are -exact size, `null`/`"*l"` line (CR ignored), and `"*a"` until peer +exact size, `()`/`"*l"` line (CR ignored), and `"*a"` until peer close. `receiveany` returns at most `max` bytes. `receiveuntil` is exclusive of a bounded delimiter and preserves unread bytes, including overlap across fragments. Caps: DNS host 255 bytes, Unix path 107 @@ -30,21 +30,21 @@ resolver. The raw fd is never exposed. | OpenResty API | RustScript callable | Status | Phases | Yield | Limits | Semantic differences | Acceptance | | --- | --- | --- | --- | --- | --- | --- | --- | | `ngx.socket.tcp` | `ngx::socket::tcp() -> resource` | adapted | `PHASE_SOCKET_UDP` | sync | VM resource table | Typed handle, not userdata. Cosocket request-owned even though sync. | `tests/ngx_tcp_socket.rs`, `tests/socket_lifecycle.rs` | -| `tcpsock:connect` | `ngx::socket::tcp::connect(sock, host, port) -> null` | adapted | `PHASE_SOCKET_UDP` | Waiting | host ≤255; `unix:` path ≤107 with port 0; port 0–65535 | Tokio DNS/connect; IPv4/IPv6/Unix. | `tests/ngx_tcp_socket.rs`, `tests/socket_lifecycle.rs` | +| `tcpsock:connect` | `ngx::socket::tcp::connect(sock, host, port) -> ()` | adapted | `PHASE_SOCKET_UDP` | Waiting | host ≤255; `unix:` path ≤107 with port 0; port 0–65535 | Tokio DNS/connect; IPv4/IPv6/Unix. | `tests/ngx_tcp_socket.rs`, `tests/socket_lifecycle.rs` | | `ngx.socket.connect` | `ngx::socket::connect(host, port) -> resource` | adapted | `PHASE_SOCKET_UDP` | Waiting | same as connect | Creates the TCP resource then connects. | `tests/ngx_tcp_socket.rs` | -| `tcpsock:send` | `ngx::socket::tcp::send(sock, data) -> null` | adapted | `PHASE_SOCKET_UDP` | Waiting | 1048576-byte payload; string or bytes | Partial-write loop until complete. Transport timeout is HostError; the socket stays connected, but HostError aborts the current script/request. | `src/socket/tcp.rs`, `tests/ngx_tcp_socket.rs` | -| `tcpsock:receive` | `ngx::socket::tcp::receive(sock, mode) -> bytes` | adapted | `PHASE_SOCKET_UDP` | Waiting | exact/`null`/`*l`/`*a`; max 1048576 | Line ignores CR. `*a` succeeds on peer close. Transport timeout is HostError; the socket stays connected, but HostError aborts the current script/request. | `src/socket/tcp.rs`, `tests/ngx_tcp_socket.rs` | +| `tcpsock:send` | `ngx::socket::tcp::send(sock, data) -> ()` | adapted | `PHASE_SOCKET_UDP` | Waiting | 1048576-byte payload; string or bytes | Partial-write loop until complete. Transport timeout is HostError; the socket stays connected, but HostError aborts the current script/request. | `src/socket/tcp.rs`, `tests/ngx_tcp_socket.rs` | +| `tcpsock:receive` | `ngx::socket::tcp::receive(sock, mode) -> bytes` | adapted | `PHASE_SOCKET_UDP` | Waiting | exact/`()`/`*l`/`*a`; max 1048576 | Line ignores CR. `*a` succeeds on peer close. Transport timeout is HostError; the socket stays connected, but HostError aborts the current script/request. | `src/socket/tcp.rs`, `tests/ngx_tcp_socket.rs` | | `tcpsock:receiveany` | `ngx::socket::tcp::receiveany(sock, max) -> bytes` | adapted | `PHASE_SOCKET_UDP` | Waiting | 1–1048576 | Returns unread first; one kernel read otherwise. | `tests/ngx_tcp_socket.rs` | | `tcpsock:receiveuntil` | `ngx::socket::tcp::receiveuntil(sock, delimiter) -> bytes` | adapted | `PHASE_SOCKET_UDP` | Waiting | delimiter 1–256 bytes | Single-shot, exclusive delimiter, unread preserved. | `tests/ngx_tcp_socket.rs` | -| `tcpsock:close` | `ngx::socket::tcp::close(sock) -> null` | adapted | `PHASE_SOCKET_UDP` | sync | — | Double close is HostError. Cosocket request-owned. | `tests/ngx_tcp_socket.rs` | -| `tcpsock:settimeout` | `ngx::socket::tcp::settimeout(sock, ms) -> null` | adapted | `PHASE_SOCKET_UDP` | sync | non-negative ms; `0` = no timer | Applies to subsequent ops; default 60000. | `tests/ngx_tcp_socket.rs` | -| `tcpsock:setkeepalive` | `ngx::socket::tcp::setkeepalive(sock, timeout, pool_size) -> null` | adapted | `PHASE_SOCKET_UDP` | sync | timeout ms (`null`=60000, `0`=never); pool_size `null`=30, 1–256; max 32 keys / 256 conns / 1000 reuse | Worker-local LRU pool keyed by endpoint+TLS identity. Detaches IO from the request so cancel/close cannot drop a pooled socket. Rejects pending ops, unread buffers, EOF, and dirty sockets. | `tests/ngx_tcp_keepalive.rs` | +| `tcpsock:close` | `ngx::socket::tcp::close(sock) -> ()` | adapted | `PHASE_SOCKET_UDP` | sync | — | Double close is HostError. Cosocket request-owned. | `tests/ngx_tcp_socket.rs` | +| `tcpsock:settimeout` | `ngx::socket::tcp::settimeout(sock, ms) -> ()` | adapted | `PHASE_SOCKET_UDP` | sync | non-negative ms; `0` = no timer | Applies to subsequent ops; default 60000. | `tests/ngx_tcp_socket.rs` | +| `tcpsock:setkeepalive` | `ngx::socket::tcp::setkeepalive(sock, timeout, pool_size) -> ()` | adapted | `PHASE_SOCKET_UDP` | sync | timeout ms (`()`=60000, `0`=never); pool_size `()`=30, 1–256; max 32 keys / 256 conns / 1000 reuse | Worker-local LRU pool keyed by endpoint+TLS identity. Detaches IO from the request so cancel/close cannot drop a pooled socket. Rejects pending ops, unread buffers, EOF, and dirty sockets. | `tests/ngx_tcp_keepalive.rs` | | `tcpsock:getreusedtimes` | `ngx::socket::tcp::getreusedtimes(sock) -> int` | adapted | `PHASE_SOCKET_UDP` | sync | — | 0 until checkout; closed is HostError. | `tests/ngx_tcp_keepalive.rs` | -| `tcpsock:sslhandshake` | `ngx::socket::tcp::sslhandshake(sock, reused_session, server_name, ssl_verify, send_status_req) -> null` | adapted | `PHASE_SOCKET_UDP` | Waiting | SNI ≤255; verify defaults true | Tokio/rustls only. No NGINX SSL FFI. Session userdata is not returned; `reused_session` session handles are a structured unsupported error. `null`/`true` keep automatic same-identity rustls resumption; `false` disables it. rustls always sends OCSP `status_request`; `send_status_req=false` is unsupported. TLS pool checkout happens only during handshake after endpoint+SNI+verify+trust+client identity match. | `tests/ngx_tcp_tls.rs` | +| `tcpsock:sslhandshake` | `ngx::socket::tcp::sslhandshake(sock, reused_session, server_name, ssl_verify, send_status_req) -> ()` | adapted | `PHASE_SOCKET_UDP` | Waiting | SNI ≤255; verify defaults true | Tokio/rustls only. No NGINX SSL FFI. Session userdata is not returned; `reused_session` session handles are a structured unsupported error. `()`/`true` keep automatic same-identity rustls resumption; `false` disables it. rustls always sends OCSP `status_request`; `send_status_req=false` is unsupported. TLS pool checkout happens only during handshake after endpoint+SNI+verify+trust+client identity match. | `tests/ngx_tcp_tls.rs` | | — | `ngx::socket::tcp::trustedstore(pem) -> resource` | adapted | `PHASE_SOCKET_UDP` | sync | PEM ≤65536 | Parses PEM into a VM-owned trusted-store resource. PEM is never logged. | `tests/ngx_tcp_tls.rs` | -| `tcpsock:settrustedstore` | `ngx::socket::tcp::settrustedstore(sock, store) -> null` | adapted | `PHASE_SOCKET_UDP` | sync | typed store or `null` | Attaches a trusted-store resource (or clears). Default is system roots then webpki-roots. | `tests/ngx_tcp_tls.rs` | +| `tcpsock:settrustedstore` | `ngx::socket::tcp::settrustedstore(sock, store) -> ()` | adapted | `PHASE_SOCKET_UDP` | sync | typed store resource | Attaches a trusted-store resource. Default is system roots then webpki-roots. | `tests/ngx_tcp_tls.rs` | | — | `ngx::socket::tcp::clientcert(cert, pkey) -> resource` | adapted | `PHASE_SOCKET_UDP` | sync | PEM ≤65536 each | Parses cert+key into a VM-owned resource. Private key is parsed through zeroizing storage and retained only as rustls `PrivateKeyDer`. Cert/key mismatch fails at construction. | `tests/ngx_tcp_tls.rs` | -| `tcpsock:setclientcert` | `ngx::socket::tcp::setclientcert(sock, cert) -> null` | adapted | `PHASE_SOCKET_UDP` | sync | typed clientcert or `null` | Attaches a client-cert resource (or clears). OpenResty two-PEM method is adapted to one typed resource. | `tests/ngx_tcp_tls.rs` | +| `tcpsock:setclientcert` | `ngx::socket::tcp::setclientcert(sock, cert) -> ()` | adapted | `PHASE_SOCKET_UDP` | sync | typed clientcert resource | Attaches a client-cert resource. OpenResty two-PEM method is adapted to one typed resource. | `tests/ngx_tcp_tls.rs` | TCP programs are request-owned (`HostFamily::Cosocket`). Lifecycle, timeout, cancel, shutdown, DNS, IPv4/IPv6/Unix, concurrent diff --git a/docs/api/time.md b/docs/api/time.md index bac6966..674626e 100644 --- a/docs/api/time.md +++ b/docs/api/time.md @@ -10,12 +10,12 @@ in cross-cutting item 6. | `ngx.today` | `ngx::today() -> string` | adapted | cached clock | Local `yyyy-mm-dd` from the cached snapshot. | `tests/ngx_time.rs` (`today_localtime_and_utctime_use_fixed_formats`) | | `ngx.time` | `ngx::time() -> int` | adapted | cached clock | Cached unix **seconds** as `int`, not `ngx::now`. | `tests/ngx_time.rs` (`time_is_int_and_now_is_float_from_the_same_cache`) | | `ngx.now` | `ngx::now() -> float` | adapted | cached clock; 1s stale | Cached seconds plus milliseconds as `float`. Intra-script reads share the snapshot. | `tests/ngx_time.rs` (`intra_script_now_reads_share_the_cached_snapshot`, clock-boundary tests) | -| `ngx.update_time` | `ngx::update_time() -> null` | adapted | samples OS immediately | Always refreshes the process-local cache. Observable side effect. | `tests/ngx_time.rs` (`update_time_samples_os_immediately_without_event_turn`) | +| `ngx.update_time` | `ngx::update_time() -> ()` | adapted | samples OS immediately | Always refreshes the process-local cache. Observable side effect. | `tests/ngx_time.rs` (`update_time_samples_os_immediately_without_event_turn`) | | `ngx.localtime` | `ngx::localtime() -> string` | adapted | cached clock | Local `yyyy-mm-dd hh:mm:ss`. | `tests/ngx_time.rs` (`today_localtime_and_utctime_use_fixed_formats`) | | `ngx.utctime` | `ngx::utctime() -> string` | adapted | cached clock | UTC `yyyy-mm-dd hh:mm:ss`. | `tests/ngx_time.rs` (`today_localtime_and_utctime_use_fixed_formats`) | | `ngx.cookie_time` | `ngx::cookie_time(sec: int) -> string` | supported | none beyond conversion | `Thu, 18-Nov-10 11:27:35 GMT`. | `tests/ngx_time.rs` (`http_cookie_and_parse_time_are_fixed`) | | `ngx.http_time` | `ngx::http_time(sec: int) -> string` | supported | none beyond conversion | `Thu, 18 Nov 2010 11:27:35 GMT`. | `tests/ngx_time.rs` (`http_cookie_and_parse_time_are_fixed`) | -| `ngx.parse_http_time` | `ngx::parse_http_time(text: string) -> optional` | adapted | 128-byte input | RFC 822 / RFC 850 / asctime. Invalid or pre-1970 is `null`. Over 128 bytes is a host error, not `null`. | `tests/ngx_time.rs` (`parse_http_time_*`) | +| `ngx.parse_http_time` | `ngx::parse_http_time(text: string) -> Option` | adapted | 128-byte input | RFC 822 / RFC 850 / asctime. Invalid or pre-1970 is `Option::None`. Over 128 bytes is a host error, not `null`. | `tests/ngx_time.rs` (`parse_http_time_*`) | --- diff --git a/docs/api/timer.md b/docs/api/timer.md index 33fa9b4..10a4dec 100644 --- a/docs/api/timer.md +++ b/docs/api/timer.md @@ -4,8 +4,8 @@ | OpenResty API | RustScript callable | Status | Phases | Yield | Limits | Semantic differences | Acceptance | | --- | --- | --- | --- | --- | --- | --- | --- | -| `ngx.sleep` | `ngx::sleep(seconds: number) -> null` | adapted | `SLEEP_PHASES` | Waiting | in-flight 1024; rejects negative / NaN / infinity / overflow | Seconds (int or float), not milliseconds. Tokio timer; completion via `HostFuture` -> waker -> eventfd -> worker-main-thread `resume`. Client close during wait is HTTP 499. | `tests/vm_sleep.rs`, `tests/host_catalog.rs`, `tests/access_async.rs`, `tests/lifecycle.rs`, `tests/nginx_integration.py` | -| `ngx.sleep` (legacy ms form) | `ngx::sleep_ms(ms: int) -> null` | adapted | `SLEEP_PHASES` | Waiting | in-flight 1024; `ms >= 0` | Not an OpenResty name. Integer milliseconds for scripts written before the seconds migration. Same Waiting path as `ngx::sleep`. | `tests/vm_sleep.rs`, `tests/access_async.rs`, `tests/request_driver.rs`, `tests/lifecycle.rs` | +| `ngx.sleep` | `ngx::sleep(seconds: number) -> ()` | adapted | `SLEEP_PHASES` | Waiting | in-flight 1024; rejects negative / NaN / infinity / overflow | Seconds (int or float), not milliseconds. Tokio timer; completion via `HostFuture` -> waker -> eventfd -> worker-main-thread `resume`. Client close during wait is HTTP 499. | `tests/vm_sleep.rs`, `tests/host_catalog.rs`, `tests/access_async.rs`, `tests/lifecycle.rs`, `tests/nginx_integration.py` | +| `ngx.sleep` (legacy ms form) | `ngx::sleep_ms(ms: int) -> ()` | adapted | `SLEEP_PHASES` | Waiting | in-flight 1024; `ms >= 0` | Not an OpenResty name. Integer milliseconds for scripts written before the seconds migration. Same Waiting path as `ngx::sleep`. | `tests/vm_sleep.rs`, `tests/access_async.rs`, `tests/request_driver.rs`, `tests/lifecycle.rs` | ## `ngx::timer` family (landed) @@ -15,8 +15,8 @@ created it; the callback runs on a timer-owned VM. | OpenResty API | RustScript callable | Status | Phases | Yield | Limits | Semantic differences | Acceptance | | --- | --- | --- | --- | --- | --- | --- | --- | -| `ngx.timer.at` | `ngx::timer::at(seconds: number, callback: fn(bool) -> unknown) -> bool` | adapted | access, content | sync (the callback runs later) | checked `Duration`; negative/NaN/infinite rejected; `TimerConfig.max_pending` = 1024, worker pool 1024 | Seconds (int or float), not milliseconds. The callback is consumed (`TakeOwned`). `premature=false` on normal expiry. | `tests/ngx_scheduler_contract.rs`, `tests/ngx_timer.rs`, `tests/timer_nginx.rs` | -| `ngx.timer.every` | `ngx::timer::every(seconds: number, callback: fn(bool) -> unknown) -> bool` | adapted | access, content | sync (the callback runs later) | checked `Duration`; `interval > 0` required; same caps | Rounds serialize: the next deadline is scheduled only after the previous round is terminal, its identity rotates to a fresh generation, and mutable capture cells persist between rounds. | `tests/ngx_timer.rs`, `tests/timer_nginx.rs` | +| `ngx.timer.at` | `ngx::timer::at(seconds: number, callback: fn(bool) -> ()) -> ()` | adapted | access, content | sync (the callback runs later) | checked `Duration`; negative/NaN/infinite rejected; `TimerConfig.max_pending` = 1024, worker pool 1024 | Seconds (int or float), not milliseconds. The callback is consumed (`TakeOwned`). `premature=false` on normal expiry. | `tests/ngx_scheduler_contract.rs`, `tests/ngx_timer.rs`, `tests/timer_nginx.rs` | +| `ngx.timer.every` | `ngx::timer::every(seconds: number, callback: fn(bool) -> ()) -> ()` | adapted | access, content | sync (the callback runs later) | checked `Duration`; `interval > 0` required; same caps | Rounds serialize: the next deadline is scheduled only after the previous round is terminal, its identity rotates to a fresh generation, and mutable capture cells persist between rounds. | `tests/ngx_timer.rs`, `tests/timer_nginx.rs` | | `ngx.timer.pending_count` | `ngx::timer::pending_count() -> int` | adapted | access, content | sync | none | Registrations waiting to begin, or waiting for a running slot. | `tests/ngx_timer.rs`, `tests/timer_nginx.rs` | | `ngx.timer.running_count` | `ngx::timer::running_count() -> int` | adapted | access, content | sync | `TimerConfig.max_running` = 256 | Live callback rounds, including callbacks parked on async host work. | `tests/ngx_timer.rs`, `tests/timer_nginx.rs` | diff --git a/docs/api/udp.md b/docs/api/udp.md index d328db5..e0f88e6 100644 --- a/docs/api/udp.md +++ b/docs/api/udp.md @@ -17,12 +17,12 @@ resolver, and host names longer than 255 bytes are rejected before lookup. | OpenResty API | RustScript callable | Status | Phases | Yield | Limits | Semantic differences | Acceptance | | --- | --- | --- | --- | --- | --- | --- | --- | | `ngx.socket.udp` | `ngx::socket::udp() -> resource` | adapted | `PHASE_SOCKET_UDP` | sync | VM resource table | Typed handle, not userdata. Cosocket request-owned even though sync. | `tests/ngx_udp_socket.rs`, `tests/socket_lifecycle.rs` | -| `udpsock:bind` | `ngx::socket::udp::bind(sock, host, port) -> null` | adapted | `PHASE_SOCKET_UDP` | Waiting | host string/`null`; port 0–65535 | `null`/`*` bind any IPv4. | `tests/ngx_udp_socket.rs`, `tests/socket_lifecycle.rs` | -| `udpsock:setpeername` | `ngx::socket::udp::setpeername(sock, host, port) -> null` | adapted | `PHASE_SOCKET_UDP` | Waiting | host string ≤255 bytes; port 0–65535 | Tokio DNS; auto-bind if needed. | `tests/ngx_udp_socket.rs`, `tests/socket_lifecycle.rs` | -| `udpsock:send` | `ngx::socket::udp::send(sock, data) -> null` | adapted | `PHASE_SOCKET_UDP` | Waiting | 65535-byte payload; string or bytes | Requires `setpeername`. | `tests/ngx_udp_socket.rs`, `tests/socket_lifecycle.rs` | -| `udpsock:receive` | `ngx::socket::udp::receive(sock, size) -> bytes` | adapted | `PHASE_SOCKET_UDP` | Waiting | default 8192; max 65535; `null` size | Oversized datagrams error instead of silent truncate. | `tests/ngx_udp_socket.rs`, `tests/socket_lifecycle.rs` | -| `udpsock:close` | `ngx::socket::udp::close(sock) -> null` | adapted | `PHASE_SOCKET_UDP` | sync | — | Double close is HostError. Cosocket request-owned. | `tests/ngx_udp_socket.rs`, `tests/socket_lifecycle.rs` | -| `udpsock:settimeout` | `ngx::socket::udp::settimeout(sock, ms) -> null` | adapted | `PHASE_SOCKET_UDP` | sync | non-negative ms; `0` = no timer | Applies to subsequent ops; default 60000. | `tests/ngx_udp_socket.rs`, `tests/socket_lifecycle.rs` | +| `udpsock:bind` | `ngx::socket::udp::bind(sock, host, port) -> ()` | adapted | `PHASE_SOCKET_UDP` | Waiting | host string/`()`; port 0–65535 | `()`/`*` bind any IPv4. | `tests/ngx_udp_socket.rs`, `tests/socket_lifecycle.rs` | +| `udpsock:setpeername` | `ngx::socket::udp::setpeername(sock, host, port) -> ()` | adapted | `PHASE_SOCKET_UDP` | Waiting | host string ≤255 bytes; port 0–65535 | Tokio DNS; auto-bind if needed. | `tests/ngx_udp_socket.rs`, `tests/socket_lifecycle.rs` | +| `udpsock:send` | `ngx::socket::udp::send(sock, data) -> ()` | adapted | `PHASE_SOCKET_UDP` | Waiting | 65535-byte payload; string or bytes | Requires `setpeername`. | `tests/ngx_udp_socket.rs`, `tests/socket_lifecycle.rs` | +| `udpsock:receive` | `ngx::socket::udp::receive(sock, size) -> bytes` | adapted | `PHASE_SOCKET_UDP` | Waiting | default 8192; max 65535; `()` size | Oversized datagrams error instead of silent truncate. | `tests/ngx_udp_socket.rs`, `tests/socket_lifecycle.rs` | +| `udpsock:close` | `ngx::socket::udp::close(sock) -> ()` | adapted | `PHASE_SOCKET_UDP` | sync | — | Double close is HostError. Cosocket request-owned. | `tests/ngx_udp_socket.rs`, `tests/socket_lifecycle.rs` | +| `udpsock:settimeout` | `ngx::socket::udp::settimeout(sock, ms) -> ()` | adapted | `PHASE_SOCKET_UDP` | sync | non-negative ms; `0` = no timer | Applies to subsequent ops; default 60000. | `tests/ngx_udp_socket.rs`, `tests/socket_lifecycle.rs` | `PHASE_SOCKET_UDP` (OpenResty v0.10.29 `ngx.socket.udp` contexts): `rewrite`, `access`, `content`, `timer`, `ssl_certificate`, diff --git a/docs/design/ngx-scheduler.md b/docs/design/ngx-scheduler.md index ac7021b..e4ecf62 100644 --- a/docs/design/ngx-scheduler.md +++ b/docs/design/ngx-scheduler.md @@ -27,7 +27,7 @@ Pinned `pd-vm` evidence in this file is taken from: | Pin | Value | | --- | --- | | Crate metadata | `Cargo.toml` `rustscript-rev` / `vm` git `rev` | -| `Cargo.lock` | `pd-vm` `source = "git+https://github.com/rustscript-lang/rustscript?rev=87d8c5d5cb42b3381aecadb383c0b7bf34ff204f#87d8c5d5cb42b3381aecadb383c0b7bf34ff204f"` | +| `Cargo.lock` | `pd-vm` `source = "git+https://github.com/rustscript-lang/rustscript?rev=fc8b0c8179640a8ffe460a64b3690e35c2f7eb73#fc8b0c8179640a8ffe460a64b3690e35c2f7eb73"` | | Local Cargo git checkout | `$CARGO_HOME/git/checkouts/rustscript-ab116a0775a56744/87d8c5d` | Public versus crate-private is taken from that checkout's `src/lib.rs` @@ -123,7 +123,7 @@ Root execution is `Vm::run` / `Vm::resume`. The module does not use `ScriptCallback`, `Store`, `Invocation`, or `Vm::invoke_callable` for production request handling. -## Evidence matrix (pinned `pd-vm` 87d8c5d5cb42b3381aecadb383c0b7bf34ff204f) +## Evidence matrix (pinned `pd-vm` fc8b0c8179640a8ffe460a64b3690e35c2f7eb73) Legend: **P** = public (`src/lib.rs` re-export or `pub` item on a public type). **C** = crate-private / `pub(super)` / `pub(crate)`. @@ -488,7 +488,7 @@ Public callables: `ngx::timer::at(seconds, callback)`, `ngx::timer::running_count()`. The bare `ngx::timer` marker is **not** a callable. Access + content only; Log fails configuration compile. -- `at`/`every` declare their callback as `fn(bool) -> unknown` with +- `at`/`every` declare their callback as `fn(bool) -> ()` with `HostParamPassing::TakeOwned` and register an exact owned entry; the callback return value is discarded. Seconds are checked into a `Duration` (negative, NaN, infinite, and zero `every` intervals are rejected). diff --git a/docs/examples/async-pipeline.md b/docs/examples/async-pipeline.md index 0bbd1e5..0f1ee9c 100644 --- a/docs/examples/async-pipeline.md +++ b/docs/examples/async-pipeline.md @@ -37,7 +37,7 @@ $NGX_RUSTSCRIPT_NGINX -p /absolute/path/to/prefix \ curl --fail http://127.0.0.1:8080/pipeline ``` -The response contains the URL read by I/O, the HTTP status/final URL/UTF-8 body, SQLite rows, and the typed shared-dict store result. The SQLite database is created as `pipeline.db` under `rss_sqlite_root`. +The response contains the URL read by I/O, the HTTP status/final URL/UTF-8 body, the typed SQLite query envelope, and the typed shared-dict store result. The SQLite database is created as `pipeline.db` under `rss_sqlite_root`. ## Capability policy @@ -53,4 +53,15 @@ These directives inherit by field into nested locations. A child location that s ## Value shapes -The example uses an explicit `SharedStoreResult` struct for the fixed shared-dict result. HTTP request and response values follow pd-vm's published map API at the pinned revision. Before JSON encoding, the example narrows fixed response members to `int`, `string`, and `bytes`, then converts the body with `bytes::to_utf8`; raw bytes cannot be encoded by `json::encode`. +The example uses explicit host named types throughout. `io::open` and +`io::read_all` return `Result`, so the read is a small `Result`-returning +helper; `http::request::new` builds an owned request resource and +`http::client::request` consumes it into a response resource whose typed +accessors are `http::response::{status,url,body}`. `sqlite::open` takes +`SqliteOpenOptions` with `Option` fields, and `sqlite::query` returns a +`SqliteQueryResult` whose cells are tagged `SqliteValue` records. The shared +store result is `SharedStoreResult` with `Option` error. + +Before JSON encoding, the example narrows each value to `int`, `string`, or +`bool`, and converts the HTTP body with `bytes::to_utf8`; `json::encode` +cannot encode `bytes`, so `SqliteValue.blob_value` is projected away. diff --git a/docs/examples/index.md b/docs/examples/index.md index 92074ed..db8e1df 100644 --- a/docs/examples/index.md +++ b/docs/examples/index.md @@ -40,12 +40,12 @@ ngx::sleep_ms(30); ``` Read a request body in `content_by_rss_file` (nullary like OpenResty; -`get_post_args` still takes explicit `null` for the max-args adaptation): +`get_post_args` still takes an explicit argument for the max-args adaptation): ```rss use ngx; ngx::req::read_body(); -let data = ngx::req::get_body_data(); +let data = ngx::req::get_body_data().unwrap_or(""); ngx::say(data); ``` @@ -56,7 +56,7 @@ Replace the request body in `content_by_rss_file` (memory; `set_body_file` use ngx; ngx::req::read_body(); ngx::req::set_body_data("replacement"); -ngx::say(ngx::req::get_body_data()); +ngx::say(ngx::req::get_body_data().unwrap_or("")); ``` Two sequential sleeps: @@ -75,7 +75,7 @@ let sock = ngx::socket::udp(); ngx::socket::udp::settimeout(sock, 1000); ngx::socket::udp::setpeername(sock, "127.0.0.1", 53); ngx::socket::udp::send(sock, "ping"); -ngx::socket::udp::receive(sock, null); +ngx::socket::udp::receive(sock, ()); ngx::socket::udp::close(sock); ``` @@ -100,7 +100,7 @@ ngx::socket::tcp::settimeout(sock, 1000); ngx::socket::tcp::connect(sock, "127.0.0.1", 8080); let store = ngx::socket::tcp::trustedstore(ca_pem); ngx::socket::tcp::settrustedstore(sock, store); -ngx::socket::tcp::sslhandshake(sock, null, "localhost", true, null); +ngx::socket::tcp::sslhandshake(sock, (), "localhost", true, ()); ngx::socket::tcp::send(sock, "ping"); ngx::socket::tcp::receive(sock, 4); ngx::socket::tcp::setkeepalive(sock, 60000, 30); diff --git a/docs/examples/rate-limit.md b/docs/examples/rate-limit.md index 72be979..20ab554 100644 --- a/docs/examples/rate-limit.md +++ b/docs/examples/rate-limit.md @@ -34,11 +34,11 @@ The zone name `"ratelimit"` in the script must match `rss_shared_dict ratelimit ## Algorithm -1. Read `$rate_limit_key` via `ngx::var::get(...).unwrap_or("")`. Missing (`null`) or empty fails closed with 503. +1. Read `$rate_limit_key` via `ngx::var::get(...).unwrap_or("")`. Missing (`Option::None`) or empty fails closed with 503. 2. Resolve `ngx::shared::dict("ratelimit")`. An unknown zone is a host error (500). -3. Establish a new window with `ngx::shared::safe_add(dict, key, 1, WINDOW_SECONDS, null)` so capacity pressure never evicts a live quota: +3. Establish a new window with `ngx::shared::safe_add(dict, key, 1, WINDOW_SECONDS, ())` so capacity pressure never evicts a live quota: - `ok` → this is count `1` for a new window; - - `error == "exists"` → the window already exists; `ngx::shared::incr(dict, key, 1, null, null)` on that scalar (no `init`, so incr cannot allocate a new key); + - `error == "exists"` → the window already exists; `ngx::shared::incr(dict, key, 1, (), ())` on that scalar (no `init`, so incr cannot allocate a new key); - any other `safe_add` error (including `no memory`) fails closed with 503. 4. Unexpected `incr` `error` or `forcible` fails closed with 503. `forcible` means an LRU victim was deleted; this example never accepts that. 5. If the resulting count is `> LIMIT`, set `Retry-After` and `ngx::exit(ngx::http::status_too_many_requests())` (429). @@ -51,7 +51,7 @@ The script does not log the identity key. This is a **fixed window**, not a sliding window. - TTL is assigned only when `safe_add` creates the key (`exptime = WINDOW_SECONDS`). -- Subsequent `incr` calls pass `null` init / init_ttl and keep the original expiry. +- Subsequent `incr` calls pass `()` for init / init_ttl and keep the original expiry. - When the key expires, the next request starts a new window at count 1. - A client that spends `LIMIT` at the end of one window and `LIMIT` again at the start of the next can send up to `2 * LIMIT` requests in `WINDOW_SECONDS` diff --git a/docs/execution-model.md b/docs/execution-model.md index f5e63b2..2a91132 100644 --- a/docs/execution-model.md +++ b/docs/execution-model.md @@ -49,8 +49,10 @@ imports or builtins may suspend or retain VM state. This admits buffered `http::client::request` and synchronous request-context programs, including key-auth/control flows and programs using `ngx.ctx`, `RequestWrite`, or `ResponseWrite`. Standard I/O, SQLite, SSE, timers, sleep, cosockets, every -other standard host import, async ngx hosts, resource-bearing host schemas, -and unknown host schemas are excluded. These checks are closed over the whole +other standard host import, async ngx hosts, and dynamic host schemas are +excluded. The buffered HTTP builder/accessor family is admitted: its request, +response, and header resources are created and released inside one +synchronous call chain. These checks are closed over the whole compiled program; adding any excluded import disables reuse. Static admission only selects the reusable request path. A successful @@ -145,10 +147,10 @@ the VM, and never arms eventfd, a timer, or `r->main->count`. See ## Sleep hosts -`ngx::sleep` is a `pd-host-function` async host (`seconds:number -> null`). +`ngx::sleep` is a `pd-host-function` async host (`seconds:number -> ()`). `seconds` is an OpenResty-style integer or float; negative, NaN, infinity, and duration overflow are rejected. `ngx::sleep_ms` is the -millisecond integer form (`ms:int -> null`). The generated wrapper +millisecond integer form (`ms:int -> ()`). The generated wrapper submits an owned host future; the impl validates the duration and awaits Tokio. The first host entry returns `VmStatus::Waiting`. Two sequential sleeps reuse the same generation-safe request identity and diff --git a/docs/host-descriptors.md b/docs/host-descriptors.md index bc21a13..81a9ddd 100644 --- a/docs/host-descriptors.md +++ b/docs/host-descriptors.md @@ -60,20 +60,21 @@ Fixed envelopes are catalog `HostTypeSchema::Named` and Rust | `SharedIncrResult` | `ngx::shared::incr` | | `SharedNumberResult` | `ngx::shared::{ttl,lpush,rpush,llen}` | -Guest scripts use the host named types. Inner `value` fields on shared -results stay `Unknown` because stored dictionary values are heterogeneous. +Guest scripts use the host named types. Shared dictionary values are tagged +`SharedValue` records and absence is `Option::None`. -## Intentional dynamic slots +## Intentional dynamic parameters -Positions that stay `HostTypeSchema::Unknown` or `Map(Unknown)` are -listed exactly in `tests/host_catalog.rs` -(`intentional_dynamic_slots_match_the_allowlist`). Typical reasons: +Only host **parameters** may stay `HostTypeSchema::Unknown`; they are listed +exactly in `tests/host_catalog.rs` +(`exact_dynamic_unknown_and_map_slots_are_allowlisted`). Typical reasons: -- optional trailing OpenResty arguments (`null` or several runtime shapes) -- open dictionaries (`ngx::encode_args` `args`, header-set maps, ctx payloads) -- heterogeneous stored shared-dict values and `ngx::header::get` (string, - string array, or null) +- optional OpenResty trailing arguments (`()` or several runtime shapes) +- open dictionaries (`ngx::encode_args` `args`, header-set maps) +- mixed printable or buffer payloads (`print`, `say`, socket sends) -Do not add a new `Unknown` or `Map(Unknown)` return for a fixed outer -shape. Add a named struct, bump `NGX_HOST_CATALOG_SCHEMA_VERSION`, and -update the fingerprint goldens. +No host **output** may carry `Unknown`, `Null`, or the legacy `Optional` +schema: RustScript rejects such a return at compile time. Represent absence +with `Option`, a void result with `()`, a fixed shape with a named struct, +and an open-keyed bag with a typed entry array. Add a named struct, bump +`NGX_HOST_CATALOG_SCHEMA_VERSION`, and update the fingerprint goldens. diff --git a/docs/ngx-api-compatibility.md b/docs/ngx-api-compatibility.md index 5c03568..ba78736 100644 --- a/docs/ngx-api-compatibility.md +++ b/docs/ngx-api-compatibility.md @@ -34,80 +34,42 @@ helpers as part of this module. | Status | Meaning | | --- | --- | | `supported` | Same call shape and result kind as lua-nginx-module v0.10.29 | -| `adapted` | Intentional RustScript mapping (namespacing, fixed structs, `null` optionals, bytes vs string) | +| `adapted` | Intentional RustScript mapping (namespacing, fixed structs, `()` optional arguments, bytes vs string) | The pinned compiler matches host arity exactly. OpenResty optional -trailing arguments are an explicit `null` argument, not an omitted -argument. +trailing arguments are explicit arguments: pass `()` where OpenResty +allows `nil`. -## Typed fixed results and dynamic maps +## Typed fixed results and entry arrays -Multi-return OpenResty values use **fixed result structs** at the -RustScript boundary. The current host catalog exposes several of those -return slots as `unknown`, so callers declare the matching struct and -annotate the assignment; field access then uses `.value` / `.flags` / -`.stale` / `.ok` / `.error` / `.forcible`. The VM's internal carrier is -not part of the script-facing type. +Multi-return OpenResty values use **fixed result structs** at the RustScript +boundary, and every return slot is fully typed: `Option` for logical +absence, named structs for fixed shapes, and entry arrays for open-keyed +bags. Field access uses `.value` / `.flags` / `.stale` / `.ok` / `.error` / +`.forcible`; an absent logical value is `Option::None`, never `null`. The +VM's internal carrier is not part of the script-facing type. -Truly **dynamic-key** collections remain `map` fields inside typed -envelopes, such as `DecodedArgsResult.args` and -`RequestHeadersResult.headers`. +Open-keyed collections are typed entry arrays instead of dynamic maps: ```rss struct DecodedArgsResult { - args: map, + args: [ArgEntry], // ArgEntry { name: string, value: Option } truncated: bool } struct RequestHeadersResult { - headers: map, + headers: [HeaderEntry], // HeaderEntry { name: string, value: string } truncated: bool } - -struct RequestArgsResult { - args: map, - truncated: bool -} - -struct SharedGetResult { - value: unknown?, - flags: int, - stale: bool, - error: string? -} - -struct SharedStoreResult { - ok: bool, - error: string?, - forcible: bool -} - -struct SharedSafeStoreResult { - ok: bool, - error: string? -} - -struct SharedIncrResult { - value: number?, - error: string?, - forcible: bool -} - -struct SharedNumberResult { - value: number?, - error: string? -} ``` -Example: +Repeated keys repeat the entry, so duplicate headers and repeated query +arguments survive without an `unknown` value. A bare query key carries +`Option::None`; a present empty value carries `Option::Some("")`. -```rss -let added: SharedSafeStoreResult = - ngx::shared::safe_add(dict, key, 1, WINDOW_SECONDS, null); -if added.ok { - // ... -} -``` +Heterogeneous stored values use a tagged record. Shared-dictionary values +are `SharedValue { kind, bool_value, int_value, float_value, text_value, +bytes_value }`, where `kind` selects the active payload. ## Namespacing and constants @@ -115,7 +77,7 @@ Lua fields and constants become zero-argument functions: `ngx.OK` → `ngx::core::ok()`, `ngx.HTTP_GET` → `ngx::http::get()`, `ngx.config.prefix` → `ngx::config::prefix()`, `ngx.worker.pid` → `ngx::worker::pid()`. There are no host constants -or dynamic table fields. `ngx.null` is `ngx::core::null_value() -> null` +or dynamic table fields. `ngx.null` is `ngx::core::null_value() -> ()` because `null` is a reserved token. See [Constants](api/constants.md) and diff --git a/docs/security.md b/docs/security.md index 26c55f5..629d4c3 100644 --- a/docs/security.md +++ b/docs/security.md @@ -62,20 +62,14 @@ and then `validate_program_imports_for_phase(..., Access)`. Log validation. Unknown names, phase-forbidden names, arity, and type mismatches fail distinctly at `nginx -t` / reload. -pd-vm's standard HTTP imports are not part of that catalog. The pinned pd-vm -HTTP catalog declares its request/response positions as named records -(`HttpRequest` / `HttpResponse` / SSE records) and registers its adapters -byte-for-byte from that same catalog, so a merged compile-time schema could -never both match the documented loose `map` scripts and stay registered. -`http::client::*` therefore compiles dynamically as an ordinary host import -against the documented `map` interface: configuration validates the name -(`is_standard_host_import`), and the pinned pd-vm adapter validates the -concrete request contents at runtime and returns the pinned named-record -contract payload shapes unchanged (duplicate header entries, `HttpHeaderValue` -text/bytes variants). The dynamic standard import binds through the staged -standard composition (I/O and SQLite are compiler builtins dispatched through -that same composition); see [pd-vm standard async hosts](api/standard-async.md). -The standard host domains are rejected in `log_by_rss_file`. +pd-vm's standard host families (I/O, HTTP, SQLite) are part of that catalog. +Their handle positions are typed resources, so a name resolved through the +legacy builtin table instead would carry an `int` carrier the resource-typed +sibling hosts reject; cataloguing the exact schemas keeps compile-time and +registered adapters identical. Configuration validates the name and phase +(`is_standard_host_import`) and the production registry installs the same +descriptors from the same snapshot. The standard host domains are rejected in +`log_by_rss_file`. See [pd-vm standard async hosts](api/standard-async.md). `ngx::timer::{at,every,pending_count,running_count}` is landed: registration is accepted in Access and Content and rejected in Log at configuration compile diff --git a/examples/async-pipeline.rss b/examples/async-pipeline.rss index a3e8e1c..2b824df 100644 --- a/examples/async-pipeline.rss +++ b/examples/async-pipeline.rss @@ -5,17 +5,37 @@ use sqlite; use json; use bytes as builtin_bytes; -let url_file = ngx::var::get("rss_pipeline_url_file").unwrap_or(""); -let database_file = ngx::var::get("rss_pipeline_database").unwrap_or(""); +let url_file: string = match ngx::var::get("rss_pipeline_url_file") { + Option::Some(value) => value, + Option::None => "" +}; +let database_file: string = match ngx::var::get("rss_pipeline_database") { + Option::Some(value) => value, + Option::None => "" +}; + +fn read_url(path: string) -> Result { + let handle = io::open(path, "r")?; + let text = io::read_all(handle)?; + io::close(handle); + Result::Ok(text) +} -let input = io::open(url_file, "r"); -let upstream_url = io::read_all(input); -io::close(input); +let upstream_url_read = read_url(url_file); +let upstream_url = match upstream_url_read { + Result::Ok(text) => text, + Result::Err(message) => "" +}; +if upstream_url == "" { + ngx::log(ngx::log_level::error(), "async-pipeline could not read the upstream URL file"); + ngx::exit(500); +} -let upstream = http::client::request({"method": "GET", "url": upstream_url}); -let upstream_status: int = upstream["status"]; -let upstream_final_url: string = upstream["url"]; -let upstream_body_bytes: bytes = upstream["body"]; +let request = http::request::new("GET", upstream_url); +let upstream = http::client::request(request); +let upstream_status: int = http::response::status(upstream); +let upstream_final_url: string = http::response::url(upstream); +let upstream_body_bytes: bytes = http::response::body(upstream); let upstream_body = builtin_bytes::to_utf8(upstream_body_bytes); let upstream_json = { "status": upstream_status, @@ -23,29 +43,42 @@ let upstream_json = { "body": upstream_body }; -let database = sqlite::open({"path": database_file, "mode": "read_write_create"}); -let database_rows = sqlite::query( +let database = sqlite::open({ + path: Option::Some(database_file), + mode: Option::Some("read_write_create") +}); +let database_result: SqliteQueryResult = sqlite::query( database, "SELECT 'sqlite-ok' AS source, 42 AS value", [], - {"max_rows": 8, "max_result_bytes": 65536} + {max_rows: Option::Some(8), max_result_bytes: Option::Some(65536)} ); +let database_truncated: bool = database_result.truncated; +let database_column: string = database_result.columns[0]; +let database_first_cell: string = match database_result.rows[0].cells[0].text_value { + Option::Some(text) => text, + Option::None => "" +}; sqlite::close(database); let dict = ngx::shared::dict("pipeline"); let shared: SharedStoreResult = ngx::shared::set( dict, "last_result", - json::encode({"upstream": upstream_json, "database": database_rows}), + json::encode({"upstream": upstream_json, "database_truncated": database_truncated}), 60, - null + () ); +let shared_error = match shared.error { + Option::Some(message) => message, + Option::None => "" +}; ngx::status::set(200); ngx::header::set("Content-Type", "application/json"); ngx::say(json::encode({ "io": upstream_url, "http": upstream_json, - "sqlite": database_rows, - "shared": {"ok": shared.ok, "error": shared.error} + "sqlite": {"column": database_column, "source": database_first_cell, "truncated": database_truncated}, + "shared": {"ok": shared.ok, "error": shared_error} })); diff --git a/examples/key-auth.rss b/examples/key-auth.rss index 34d302d..2199d41 100644 --- a/examples/key-auth.rss +++ b/examples/key-auth.rss @@ -19,7 +19,11 @@ if pepper == "" || expected == "" { } let raw = ngx::var::get("http_x_api_key"); -if raw == null { +let missing = match raw.copy() { + Option::Some(token) => false, + Option::None => true +}; +if missing { ngx::header::set("WWW-Authenticate", "ApiKey realm=\"api\""); ngx::exit(401); } diff --git a/examples/rate-limit.rss b/examples/rate-limit.rss index 197df29..0b985ca 100644 --- a/examples/rate-limit.rss +++ b/examples/rate-limit.rss @@ -11,24 +11,32 @@ if key == "" { let dict = ngx::shared::dict("ratelimit"); let added: SharedSafeStoreResult = - ngx::shared::safe_add(dict, key, 1, WINDOW_SECONDS, null); + ngx::shared::safe_add(dict, key, 1, WINDOW_SECONDS, ()); if added.ok { if 1 > LIMIT { ngx::header::set("Retry-After", RETRY_AFTER); ngx::exit(ngx::http::status_too_many_requests()); } } else { - if added.error != "exists" { + let added_error = match added.error { + Option::Some(message) => message, + Option::None => "" + }; + if added_error != "exists" { ngx::exit(ngx::http::status_service_unavailable()); } - let counted: SharedIncrResult = ngx::shared::incr(dict, key, 1, null, null); - if counted.error != null { + let counted: SharedIncrResult = ngx::shared::incr(dict, key, 1, (), ()); + let counted_error = match counted.error { + Option::Some(message) => message, + Option::None => "" + }; + if counted_error != "" { ngx::exit(ngx::http::status_service_unavailable()); } if counted.forcible { ngx::exit(ngx::http::status_service_unavailable()); } - if counted.value > LIMIT { + if counted.value.unwrap_or(0.0) > LIMIT { ngx::header::set("Retry-After", RETRY_AFTER); ngx::exit(ngx::http::status_too_many_requests()); } diff --git a/src/async_bridge.rs b/src/async_bridge.rs index fd6319f..a0668c7 100644 --- a/src/async_bridge.rs +++ b/src/async_bridge.rs @@ -161,7 +161,9 @@ mod tests { } fn sleep_catalog() -> vm::HostApiCatalog { - crate::host::ngx_host_catalog() + // Production compile catalog: the merged ngx + standard snapshot the + // registry installs adapters from. + crate::host::compile_host_catalog() } fn test_catalog() -> vm::HostApiCatalog { diff --git a/src/config.rs b/src/config.rs index 7b23f51..7413da8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -264,8 +264,18 @@ struct StandardBuiltinUsage { fn standard_builtin_usage(program: &Program) -> StandardBuiltinUsage { let disassembly = vm::disassemble_program(program); StandardBuiltinUsage { - io: disassembly.contains("builtin io_"), - sqlite: disassembly.contains("builtin sqlite_"), + // The standard families are catalogued host imports as well as legacy + // compiler builtins; either form selects request-owned execution. + io: disassembly.contains("builtin io_") + || program + .imports + .iter() + .any(|import| import.name.starts_with("io::")), + sqlite: disassembly.contains("builtin sqlite_") + || program + .imports + .iter() + .any(|import| import.name.starts_with("sqlite::")), sse: program .imports .iter() @@ -283,11 +293,17 @@ fn program_has_resource_or_unknown_host_schema(program: &Program) -> bool { .any(|(import, schema)| match schema { None => import.name != "http::client::request", Some(schema) => { - schema.return_type.contains_resource() - || schema - .params - .iter() - .any(|param| param.schema.contains_resource()) + // The buffered HTTP builder/accessor family is statically + // quiescent: its request/response/header resources are + // created and released inside one synchronous call chain. + // Streaming SSE is excluded by `standard_builtin_usage`. + let quiescent_http = import.name.starts_with("http::"); + !quiescent_http + && (schema.return_type.contains_resource() + || schema + .params + .iter() + .any(|param| param.schema.contains_resource())) } }) } @@ -379,7 +395,7 @@ fn program_may_suspend_or_retain_state(program: &Program) -> bool { || program_has_resource_or_unknown_host_schema(program) || program.imports.iter().any(|import| { crate::host::ngx_timer_host_names().contains(&import.name.as_str()) - || (is_standard_host_import(&import.name) && import.name != "http::client::request") + || (is_standard_host_import(&import.name) && !import.name.starts_with("http::")) || ngx_host_manifest().iter().any(|entry| { entry.name == import.name.as_str() && (entry.sync == HostSyncKind::Async diff --git a/src/host/body.rs b/src/host/body.rs index d37beed..616e8f5 100644 --- a/src/host/body.rs +++ b/src/host/body.rs @@ -22,7 +22,7 @@ use vm::HostModuleDescriptor; use vm::{Value, VmError}; use super::super::VmResult; -use super::{CallOutcome, IntoHostCallOutcome}; +use super::{CallOutcome, IntoHostCallOutcome, option_none, option_some}; use crate::host::{ current_identity, with_current_disposition_mut, with_current_request, with_current_request_mut, RequestDisposition, ScopeError, control_stop_error, @@ -156,7 +156,7 @@ fn copy_body_bufs(body: &ngx_http_request_body_t, what: &str) -> VmResult VmResult { }) } -/// In-memory request body as an owned string, or `null` when file-backed/unread. +/// In-memory request body as an owned string, or `Option::None` when +/// file-backed/unread. #[pd_host_function(name = "ngx::req::get_body_data", contract = crate::host::catalog::ngx_req_get_body_data_contract)] pub fn get_body_data() -> VmResult { with_request(|request| { @@ -433,32 +434,33 @@ pub fn get_body_data() -> VmResult { )); } if raw.request_body.is_null() { - return ret(Value::Null); + return ret(option_none()); } let body = unsafe { &*raw.request_body }; if !body.temp_file.is_null() { - return ret(Value::Null); + return ret(option_none()); } if body.bufs.is_null() { - return ret(Value::Null); + return ret(option_none()); } let bytes = copy_body_bufs(body, "ngx::req::get_body_data")?; - ret(bytes_as_string(bytes)) + ret(option_some(Value::string(bytes_as_string(bytes)))) }) } -/// NGINX temp-file path as an owned string, or `null` when memory-backed. +/// NGINX temp-file path as an owned string, or `Option::None` when +/// memory-backed. #[pd_host_function(name = "ngx::req::get_body_file", contract = crate::host::catalog::ngx_req_get_body_file_contract)] pub fn get_body_file() -> VmResult { with_request(|request| { let raw = request.as_ref(); if raw.request_body.is_null() { - return ret(Value::Null); + return ret(option_none()); } let body = unsafe { &*raw.request_body }; match body_temp_path(body, "ngx::req::get_body_file")? { - Some(path) => ret(path), - None => ret(Value::Null), + Some(path) => ret(option_some(Value::string(path))), + None => ret(option_none()), } }) } diff --git a/src/host/catalog.rs b/src/host/catalog.rs index 3e42245..c04f172 100644 --- a/src/host/catalog.rs +++ b/src/host/catalog.rs @@ -166,7 +166,12 @@ pub const NGX_SHARED_GET_KEYS_NAME: &str = "ngx::shared::get_keys"; /// Catalog schema version for fingerprint goldens. Bump together with /// [`NGX_HOST_CATALOG_FINGERPRINT`] when named structs, resource keys, or /// function shapes change. -pub const NGX_HOST_CATALOG_SCHEMA_VERSION: u32 = 3; +/// +/// Version 5: every host output is a typed value. Logical absence uses guest +/// `Option` (`{tag,payload}`), void hosts and `ngx::core::null_value` +/// return `Unit`, header/argument bags are typed entry arrays, and shared +/// dictionary values are tagged `SharedValue` records. +pub const NGX_HOST_CATALOG_SCHEMA_VERSION: u32 = 5; /// Fingerprint of schema version 1: named request/codec envelopes with /// `Unknown` shared-dict result carriers. @@ -178,7 +183,7 @@ pub const NGX_HOST_CATALOG_FINGERPRINT_V2: u64 = 0x10c4_07b2_b827_eaff; /// Fingerprint of the current schema version. The host_catalog drift gate /// compares `ngx_host_catalog().fingerprint()` against this constant. -pub const NGX_HOST_CATALOG_FINGERPRINT: u64 = 0xc54f_427f_8ef6_f5d4; +pub const NGX_HOST_CATALOG_FINGERPRINT: u64 = 0x3f35_97f5_ecea_9db9; pub const NGX_SEND_HEADERS_NAME: &str = "ngx::send_headers"; pub const NGX_PRINT_NAME: &str = "ngx::print"; @@ -1694,16 +1699,17 @@ fn semaphore_param() -> HostParamSchema { ) } -/// The `fn(bool) -> unknown` timer callback type. +/// The `fn(bool) -> ()` timer callback type. /// /// The callback observes the OpenResty-compatible `premature` flag and its -/// return value is discarded, so the declared result is `unknown`. This is the -/// one deliberate dynamic occurrence on the ngx host surface and the only -/// catalogued callable parameter. +/// return value is discarded, so the declared result is the logical Unit. This +/// is the only catalogued callable parameter. Core permits `unknown` when a +/// callback result is discarded, but ngx recursively rejects `Unknown` in +/// output schemas, so Unit provides the stricter consistent contract. pub fn timer_callback_schema() -> HostTypeSchema { HostTypeSchema::Callable { params: vec![HostTypeSchema::Bool], - result: Box::new(HostTypeSchema::Unknown), + result: Box::new(HostTypeSchema::Unit), } } @@ -1719,7 +1725,7 @@ fn timer_registration_schema(owner: &str) -> HostFunctionSchema { HostParamPassing::TakeOwned, ), ], - HostTypeSchema::Bool, + HostTypeSchema::Unit, ) } @@ -1748,7 +1754,7 @@ fn sleep_schema() -> HostFunctionSchema { HostFunctionSchema::with_return( NGX_SLEEP_NAME, vec![HostParamSchema::value("seconds", HostTypeSchema::Number)], - HostTypeSchema::Null, + HostTypeSchema::Unit, ) } @@ -1756,7 +1762,7 @@ fn sleep_ms_schema() -> HostFunctionSchema { HostFunctionSchema::with_return( NGX_SLEEP_MS_NAME, vec![HostParamSchema::value("ms", HostTypeSchema::Int)], - HostTypeSchema::Null, + HostTypeSchema::Unit, ) } @@ -1788,15 +1794,34 @@ fn nullary(name: &str, return_type: HostTypeSchema) -> HostFunctionSchema { HostFunctionSchema::with_return(name, Vec::new(), return_type) } -fn map_unknown() -> HostTypeSchema { - HostTypeSchema::Map(Box::new(HostTypeSchema::Unknown)) +/// One decoded query/post argument. Repeated keys repeat the entry, so no +/// duplicate value is lost to a map key. +fn arg_entry_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "ArgEntry", + vec![ + HostStructField::new("name", HostTypeSchema::String), + HostStructField::new("value", option(HostTypeSchema::String)), + ], + ) +} + +/// One request header entry; duplicate names repeat the entry. +fn header_entry_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "HeaderEntry", + vec![ + HostStructField::new("name", HostTypeSchema::String), + HostStructField::new("value", HostTypeSchema::String), + ], + ) } fn decoded_args_result_schema() -> HostTypeSchema { HostTypeSchema::named_struct( "DecodedArgsResult", vec![ - HostStructField::new("args", map_unknown()), + HostStructField::new("args", HostTypeSchema::Array(Box::new(arg_entry_schema()))), HostStructField::new("truncated", HostTypeSchema::Bool), ], ) @@ -1806,7 +1831,10 @@ fn request_headers_result_schema() -> HostTypeSchema { HostTypeSchema::named_struct( "RequestHeadersResult", vec![ - HostStructField::new("headers", map_unknown()), + HostStructField::new( + "headers", + HostTypeSchema::Array(Box::new(header_entry_schema())), + ), HostStructField::new("truncated", HostTypeSchema::Bool), ], ) @@ -1816,29 +1844,53 @@ fn request_args_result_schema() -> HostTypeSchema { HostTypeSchema::named_struct( "RequestArgsResult", vec![ - HostStructField::new("args", map_unknown()), + HostStructField::new("args", HostTypeSchema::Array(Box::new(arg_entry_schema()))), HostStructField::new("truncated", HostTypeSchema::Bool), ], ) } -fn optional_string() -> HostTypeSchema { - HostTypeSchema::Optional(Box::new(HostTypeSchema::String)) +/// A shared-dictionary value: a tagged record because one zone entry can hold +/// a bool, number, string, or byte string. `kind` selects the payload. +fn shared_value_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "SharedValue", + vec![ + HostStructField::new("kind", HostTypeSchema::String), + HostStructField::new("bool_value", option(HostTypeSchema::Bool)), + HostStructField::new("int_value", option(HostTypeSchema::Int)), + HostStructField::new("float_value", option(HostTypeSchema::Float)), + HostStructField::new("text_value", option(HostTypeSchema::String)), + HostStructField::new("bytes_value", option(HostTypeSchema::Bytes)), + ], + ) } -fn optional_unknown() -> HostTypeSchema { - HostTypeSchema::Optional(Box::new(HostTypeSchema::Unknown)) +/// Guest `Option`: a tagged `{tag, payload}` value, not the legacy +/// nullable `Optional` codec. +fn option(inner: HostTypeSchema) -> HostTypeSchema { + HostTypeSchema::Option(Box::new(inner)) +} + +/// Guest `Option` field schema. The `optional_*` helpers build the +/// logical guest Option, not the legacy nullable `Optional` codec. +fn optional_string() -> HostTypeSchema { + option(HostTypeSchema::String) } fn optional_number() -> HostTypeSchema { - HostTypeSchema::Optional(Box::new(HostTypeSchema::Number)) + option(HostTypeSchema::Number) +} + +fn optional_shared_value() -> HostTypeSchema { + option(shared_value_schema()) } fn shared_get_result_schema() -> HostTypeSchema { HostTypeSchema::named_struct( "SharedGetResult", vec![ - HostStructField::new("value", optional_unknown()), + HostStructField::new("value", optional_shared_value()), HostStructField::new("flags", HostTypeSchema::Int), HostStructField::new("stale", HostTypeSchema::Bool), HostStructField::new("error", optional_string()), @@ -1958,7 +2010,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { semaphore_param(), HostParamSchema::value("count", HostTypeSchema::Unknown), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SEMAPHORE_COUNT_NAME => HostFunctionSchema::with_return( NGX_SEMAPHORE_COUNT_NAME, @@ -2002,12 +2054,12 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { NGX_DECODE_BASE64_NAME => HostFunctionSchema::with_return( NGX_DECODE_BASE64_NAME, vec![HostParamSchema::value("text", HostTypeSchema::String)], - HostTypeSchema::Optional(Box::new(HostTypeSchema::Bytes)), + option(HostTypeSchema::Bytes), ), NGX_DECODE_BASE64MIME_NAME => HostFunctionSchema::with_return( NGX_DECODE_BASE64MIME_NAME, vec![HostParamSchema::value("text", HostTypeSchema::String)], - HostTypeSchema::Optional(Box::new(HostTypeSchema::Bytes)), + option(HostTypeSchema::Bytes), ), NGX_CRC32_SHORT_NAME => HostFunctionSchema::with_return( NGX_CRC32_SHORT_NAME, @@ -2034,7 +2086,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { NGX_TODAY_NAME => nullary(NGX_TODAY_NAME, HostTypeSchema::String), NGX_TIME_NAME => nullary(NGX_TIME_NAME, HostTypeSchema::Int), NGX_NOW_NAME => nullary(NGX_NOW_NAME, HostTypeSchema::Float), - NGX_UPDATE_TIME_NAME => nullary(NGX_UPDATE_TIME_NAME, HostTypeSchema::Null), + NGX_UPDATE_TIME_NAME => nullary(NGX_UPDATE_TIME_NAME, HostTypeSchema::Unit), NGX_LOCALTIME_NAME => nullary(NGX_LOCALTIME_NAME, HostTypeSchema::String), NGX_UTCTIME_NAME => nullary(NGX_UTCTIME_NAME, HostTypeSchema::String), NGX_COOKIE_TIME_NAME => int_in_string_out(NGX_COOKIE_TIME_NAME, "sec"), @@ -2042,13 +2094,12 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { NGX_PARSE_HTTP_TIME_NAME => HostFunctionSchema::with_return( NGX_PARSE_HTTP_TIME_NAME, vec![HostParamSchema::value("text", HostTypeSchema::String)], - HostTypeSchema::Optional(Box::new(HostTypeSchema::Int)), + option(HostTypeSchema::Int), ), NGX_REQ_GET_METHOD_NAME => nullary(NGX_REQ_GET_METHOD_NAME, HostTypeSchema::String), - NGX_REQ_HTTP_VERSION_NAME => nullary( - NGX_REQ_HTTP_VERSION_NAME, - HostTypeSchema::Optional(Box::new(HostTypeSchema::Float)), - ), + NGX_REQ_HTTP_VERSION_NAME => { + nullary(NGX_REQ_HTTP_VERSION_NAME, option(HostTypeSchema::Float)) + } NGX_REQ_RAW_HEADER_NAME => HostFunctionSchema::with_return( NGX_REQ_RAW_HEADER_NAME, vec![HostParamSchema::value( @@ -2075,20 +2126,18 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { vec![HostParamSchema::value("max_args", HostTypeSchema::Unknown)], request_args_result_schema(), ), - NGX_REQ_READ_BODY_NAME => nullary(NGX_REQ_READ_BODY_NAME, HostTypeSchema::Null), - NGX_REQ_DISCARD_BODY_NAME => nullary(NGX_REQ_DISCARD_BODY_NAME, HostTypeSchema::Null), - NGX_REQ_GET_BODY_DATA_NAME => nullary( - NGX_REQ_GET_BODY_DATA_NAME, - HostTypeSchema::Optional(Box::new(HostTypeSchema::String)), - ), - NGX_REQ_GET_BODY_FILE_NAME => nullary( - NGX_REQ_GET_BODY_FILE_NAME, - HostTypeSchema::Optional(Box::new(HostTypeSchema::String)), - ), + NGX_REQ_READ_BODY_NAME => nullary(NGX_REQ_READ_BODY_NAME, HostTypeSchema::Unit), + NGX_REQ_DISCARD_BODY_NAME => nullary(NGX_REQ_DISCARD_BODY_NAME, HostTypeSchema::Unit), + NGX_REQ_GET_BODY_DATA_NAME => { + nullary(NGX_REQ_GET_BODY_DATA_NAME, option(HostTypeSchema::String)) + } + NGX_REQ_GET_BODY_FILE_NAME => { + nullary(NGX_REQ_GET_BODY_FILE_NAME, option(HostTypeSchema::String)) + } NGX_REQ_SET_BODY_DATA_NAME => HostFunctionSchema::with_return( NGX_REQ_SET_BODY_DATA_NAME, vec![HostParamSchema::value("data", HostTypeSchema::String)], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_REQ_SET_BODY_FILE_NAME => HostFunctionSchema::with_return( NGX_REQ_SET_BODY_FILE_NAME, @@ -2096,7 +2145,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("path", HostTypeSchema::String), HostParamSchema::value("auto_clean", HostTypeSchema::Unknown), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_REQ_INIT_BODY_NAME => HostFunctionSchema::with_return( NGX_REQ_INIT_BODY_NAME, @@ -2104,14 +2153,14 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { "buffer_size", HostTypeSchema::Unknown, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_REQ_APPEND_BODY_NAME => HostFunctionSchema::with_return( NGX_REQ_APPEND_BODY_NAME, vec![HostParamSchema::value("data", HostTypeSchema::String)], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), - NGX_REQ_FINISH_BODY_NAME => nullary(NGX_REQ_FINISH_BODY_NAME, HostTypeSchema::Null), + NGX_REQ_FINISH_BODY_NAME => nullary(NGX_REQ_FINISH_BODY_NAME, HostTypeSchema::Unit), NGX_REQ_IS_INTERNAL_NAME => nullary(NGX_REQ_IS_INTERNAL_NAME, HostTypeSchema::Bool), NGX_REQ_START_TIME_NAME => nullary(NGX_REQ_START_TIME_NAME, HostTypeSchema::Float), NGX_IS_SUBREQUEST_NAME => nullary(NGX_IS_SUBREQUEST_NAME, HostTypeSchema::Bool), @@ -2119,12 +2168,12 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { NGX_VAR_GET_NAME => HostFunctionSchema::with_return( NGX_VAR_GET_NAME, vec![HostParamSchema::value("name", HostTypeSchema::String)], - HostTypeSchema::Optional(Box::new(HostTypeSchema::String)), + option(HostTypeSchema::String), ), NGX_REQ_SET_METHOD_NAME => HostFunctionSchema::with_return( NGX_REQ_SET_METHOD_NAME, vec![HostParamSchema::value("method_id", HostTypeSchema::Int)], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_REQ_SET_URI_NAME => HostFunctionSchema::with_return( NGX_REQ_SET_URI_NAME, @@ -2132,12 +2181,12 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("uri", HostTypeSchema::String), HostParamSchema::value("jump", HostTypeSchema::Unknown), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_REQ_SET_URI_ARGS_NAME => HostFunctionSchema::with_return( NGX_REQ_SET_URI_ARGS_NAME, vec![HostParamSchema::value("args", HostTypeSchema::Unknown)], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_REQ_SET_HEADER_NAME => HostFunctionSchema::with_return( NGX_REQ_SET_HEADER_NAME, @@ -2145,12 +2194,12 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("name", HostTypeSchema::String), HostParamSchema::value("value", HostTypeSchema::Unknown), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_REQ_CLEAR_HEADER_NAME => HostFunctionSchema::with_return( NGX_REQ_CLEAR_HEADER_NAME, vec![HostParamSchema::value("name", HostTypeSchema::String)], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_VAR_SET_NAME => HostFunctionSchema::with_return( NGX_VAR_SET_NAME, @@ -2158,18 +2207,18 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("name", HostTypeSchema::String), HostParamSchema::value("value", HostTypeSchema::Unknown), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_STATUS_GET_NAME => nullary(NGX_STATUS_GET_NAME, HostTypeSchema::Int), NGX_STATUS_SET_NAME => HostFunctionSchema::with_return( NGX_STATUS_SET_NAME, vec![HostParamSchema::value("status", HostTypeSchema::Int)], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_HEADER_GET_NAME => HostFunctionSchema::with_return( NGX_HEADER_GET_NAME, vec![HostParamSchema::value("name", HostTypeSchema::String)], - HostTypeSchema::Unknown, + option(HostTypeSchema::String), ), NGX_HEADER_SET_NAME => HostFunctionSchema::with_return( NGX_HEADER_SET_NAME, @@ -2177,18 +2226,18 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("name", HostTypeSchema::String), HostParamSchema::value("value", HostTypeSchema::Unknown), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_HEADER_SET_ALL_NAME => HostFunctionSchema::with_return( NGX_HEADER_SET_ALL_NAME, vec![HostParamSchema::value("headers", HostTypeSchema::Unknown)], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), - NGX_HEADER_CLEAR_NAME => nullary(NGX_HEADER_CLEAR_NAME, HostTypeSchema::Null), + NGX_HEADER_CLEAR_NAME => nullary(NGX_HEADER_CLEAR_NAME, HostTypeSchema::Unit), NGX_EXIT_NAME => HostFunctionSchema::with_return( NGX_EXIT_NAME, vec![HostParamSchema::value("status", HostTypeSchema::Int)], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_REDIRECT_NAME => HostFunctionSchema::with_return( NGX_REDIRECT_NAME, @@ -2196,27 +2245,27 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("uri", HostTypeSchema::String), HostParamSchema::value("status", HostTypeSchema::Unknown), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_CTX_GET_NAME => HostFunctionSchema::with_return( NGX_CTX_GET_NAME, vec![HostParamSchema::value("key", HostTypeSchema::String)], - HostTypeSchema::Unknown, + option(HostTypeSchema::String), ), NGX_CTX_SET_NAME => HostFunctionSchema::with_return( NGX_CTX_SET_NAME, vec![ HostParamSchema::value("key", HostTypeSchema::String), - HostParamSchema::value("value", HostTypeSchema::Unknown), + HostParamSchema::value("value", HostTypeSchema::String), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_CTX_DELETE_NAME => HostFunctionSchema::with_return( NGX_CTX_DELETE_NAME, vec![HostParamSchema::value("key", HostTypeSchema::String)], - HostTypeSchema::Unknown, + option(HostTypeSchema::String), ), - NGX_CTX_CLEAR_NAME => nullary(NGX_CTX_CLEAR_NAME, HostTypeSchema::Null), + NGX_CTX_CLEAR_NAME => nullary(NGX_CTX_CLEAR_NAME, HostTypeSchema::Unit), NGX_SHARED_DICT_NAME => HostFunctionSchema::with_return( NGX_SHARED_DICT_NAME, vec![HostParamSchema::value("name", HostTypeSchema::String)], @@ -2235,7 +2284,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { dict_handle_param(), HostParamSchema::value("key", HostTypeSchema::String), ], - HostTypeSchema::Null, + HostTypeSchema::Bool, ), NGX_SHARED_INCR_NAME => HostFunctionSchema::with_return( NGX_SHARED_INCR_NAME, @@ -2306,7 +2355,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { NGX_SHARED_FLUSH_ALL_NAME => HostFunctionSchema::with_return( NGX_SHARED_FLUSH_ALL_NAME, vec![dict_handle_param()], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SHARED_FLUSH_EXPIRED_NAME => HostFunctionSchema::with_return( NGX_SHARED_FLUSH_EXPIRED_NAME, @@ -2336,7 +2385,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("host", HostTypeSchema::Unknown), HostParamSchema::value("port", HostTypeSchema::Int), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SOCKET_UDP_SETPEERNAME_NAME => HostFunctionSchema::with_return( NGX_SOCKET_UDP_SETPEERNAME_NAME, @@ -2345,7 +2394,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("host", HostTypeSchema::String), HostParamSchema::value("port", HostTypeSchema::Int), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SOCKET_UDP_SEND_NAME => HostFunctionSchema::with_return( NGX_SOCKET_UDP_SEND_NAME, @@ -2353,7 +2402,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { udp_sock_param(), HostParamSchema::value("data", HostTypeSchema::Unknown), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SOCKET_UDP_RECEIVE_NAME => HostFunctionSchema::with_return( NGX_SOCKET_UDP_RECEIVE_NAME, @@ -2366,7 +2415,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { NGX_SOCKET_UDP_CLOSE_NAME => HostFunctionSchema::with_return( NGX_SOCKET_UDP_CLOSE_NAME, vec![udp_sock_param()], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SOCKET_UDP_SETTIMEOUT_NAME => HostFunctionSchema::with_return( NGX_SOCKET_UDP_SETTIMEOUT_NAME, @@ -2374,7 +2423,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { udp_sock_param(), HostParamSchema::value("ms", HostTypeSchema::Int), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SOCKET_TCP_NAME => HostFunctionSchema::with_return( NGX_SOCKET_TCP_NAME, @@ -2388,7 +2437,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("host", HostTypeSchema::String), HostParamSchema::value("port", HostTypeSchema::Int), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SOCKET_CONNECT_NAME => HostFunctionSchema::with_return( NGX_SOCKET_CONNECT_NAME, @@ -2404,7 +2453,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { tcp_sock_param(), HostParamSchema::value("data", HostTypeSchema::Unknown), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SOCKET_TCP_RECEIVE_NAME => HostFunctionSchema::with_return( NGX_SOCKET_TCP_RECEIVE_NAME, @@ -2433,7 +2482,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { NGX_SOCKET_TCP_CLOSE_NAME => HostFunctionSchema::with_return( NGX_SOCKET_TCP_CLOSE_NAME, vec![tcp_sock_param()], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SOCKET_TCP_SETTIMEOUT_NAME => HostFunctionSchema::with_return( NGX_SOCKET_TCP_SETTIMEOUT_NAME, @@ -2441,7 +2490,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { tcp_sock_param(), HostParamSchema::value("ms", HostTypeSchema::Int), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SOCKET_TCP_SETKEEPALIVE_NAME => HostFunctionSchema::with_return( NGX_SOCKET_TCP_SETKEEPALIVE_NAME, @@ -2450,7 +2499,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("timeout", HostTypeSchema::Unknown), HostParamSchema::value("pool_size", HostTypeSchema::Unknown), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SOCKET_TCP_GETREUSEDTIMES_NAME => HostFunctionSchema::with_return( NGX_SOCKET_TCP_GETREUSEDTIMES_NAME, @@ -2466,7 +2515,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamSchema::value("ssl_verify", HostTypeSchema::Unknown), HostParamSchema::value("send_status_req", HostTypeSchema::Unknown), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SOCKET_TCP_SETTRUSTEDSTORE_NAME => HostFunctionSchema::with_return( NGX_SOCKET_TCP_SETTRUSTEDSTORE_NAME, @@ -2478,7 +2527,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamPassing::Borrow, ), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SOCKET_TCP_SETCLIENTCERT_NAME => HostFunctionSchema::with_return( NGX_SOCKET_TCP_SETCLIENTCERT_NAME, @@ -2490,7 +2539,7 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { HostParamPassing::Borrow, ), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SOCKET_TCP_TRUSTEDSTORE_NAME => HostFunctionSchema::with_return( NGX_SOCKET_TCP_TRUSTEDSTORE_NAME, @@ -2505,23 +2554,23 @@ fn host_function_contract(name: &str) -> HostFunctionSchema { ], HostTypeSchema::Resource(tls_clientcert_resource_key()), ), - NGX_SEND_HEADERS_NAME => nullary(NGX_SEND_HEADERS_NAME, HostTypeSchema::Null), + NGX_SEND_HEADERS_NAME => nullary(NGX_SEND_HEADERS_NAME, HostTypeSchema::Unit), NGX_PRINT_NAME => HostFunctionSchema::with_return( NGX_PRINT_NAME, vec![HostParamSchema::value("data", HostTypeSchema::Unknown)], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_SAY_NAME => HostFunctionSchema::with_return( NGX_SAY_NAME, vec![HostParamSchema::value("data", HostTypeSchema::Unknown)], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), NGX_FLUSH_NAME => HostFunctionSchema::with_return( NGX_FLUSH_NAME, vec![HostParamSchema::value("wait", HostTypeSchema::Bool)], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), - NGX_EOF_NAME => nullary(NGX_EOF_NAME, HostTypeSchema::Null), + NGX_EOF_NAME => nullary(NGX_EOF_NAME, HostTypeSchema::Unit), other => metadata_schema(other) .unwrap_or_else(|| panic!("ngx host catalog has no schema for {other}")), } @@ -2535,9 +2584,9 @@ fn metadata_schema(name: &str) -> Option { HostParamSchema::value("level", HostTypeSchema::Int), HostParamSchema::value("message", HostTypeSchema::String), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, ), - "ngx::core::null_value" => nullary(name, HostTypeSchema::Null), + "ngx::core::null_value" => nullary(name, HostTypeSchema::Unit), "ngx::get_phase" | "ngx::config::subsystem" | "ngx::config::prefix" @@ -3455,41 +3504,12 @@ pub fn ngx_host_catalog() -> HostApiCatalog { .unwrap_or_else(|error| panic!("ngx host catalog must be a valid HostApiCatalog: {error}")) } -/// Compiler catalog for production ngx scripts. -/// -/// It carries exactly the ngx host surface; the pd-vm standard families are -/// deliberately **not** merged, for one reason each: -/// -/// - I/O and SQLite are compiler builtins dispatched through the standard -/// composition ([`crate::host::install_standard_runtime`]). Cataloguing their -/// schemas as external host imports would bypass that builtin path and -/// produce unbindable imports. -/// - The pinned pd-vm HTTP client catalog declares its request/response -/// positions as named records (`HttpRequest` / `HttpResponse` / SSE records) -/// and registers its HTTP adapters byte-for-byte from that same catalog. -/// RustScript object literals are exact record types, so a compile-time -/// schema for those positions could never match the documented loose scripts -/// *and* be registered in the owned-dispatch host registry at the same time: -/// a relaxed compile schema has no registered adapter, and the exact schema -/// rejects the documented scripts. The merged catalog therefore leaves the -/// HTTP surface dynamic (`http::client::*` compile as ordinary host imports -/// against the documented `map` interface of `docs/api/standard-async.md`), -/// configuration still validates them by name ([`is_standard_host_import`]), -/// and the pd-vm adapter validates the concrete request contents at runtime -/// and returns the pd-vm contract payload shapes unchanged (duplicate header -/// entries, `HttpHeaderValue` text/bytes variants). -/// -/// No standard-family position is relaxed here: with the functions left out -/// there is nothing to relax, so no name, parameter, or result of a standard -/// family can silently become dynamic. -pub fn compile_host_catalog() -> HostApiCatalog { - ngx_host_catalog() -} - fn standard_host_names() -> &'static HashSet { static NAMES: OnceLock> = OnceLock::new(); NAMES.get_or_init(|| { - vm::http_host_catalog() + // Every standard family the runtime composition installs: I/O, HTTP, + // and SQLite all publish a guest catalog surface. + vm::standard_host_catalog() .functions() .iter() .map(|function| function.name.clone()) diff --git a/src/host/codec.rs b/src/host/codec.rs index ca2965b..ca291f0 100644 --- a/src/host/codec.rs +++ b/src/host/codec.rs @@ -8,7 +8,7 @@ use pd_host_function::pd_host_function; use vm::{HostModuleDescriptor, HostNamedStruct, HostStructField, HostTypeSchema}; use vm::{Value, VmError, VmMap}; -use super::{CallOutcome, IntoHostCallOutcome}; +use super::{CallOutcome, IntoHostCallOutcome, option_none, option_some, option_value}; use super::super::VmResult; const MAX_CODEC_INPUT_BYTES: usize = 65_536; @@ -23,7 +23,7 @@ fn ret(value: T) -> VmResult { #[derive(Debug)] struct DecodedArgsResult { - args: VmMap, + args: Vec<(String, Option)>, truncated: bool, } @@ -34,19 +34,52 @@ impl HostNamedStruct for DecodedArgsResult { vec![ HostStructField::new( "args", - HostTypeSchema::Map(Box::new(HostTypeSchema::Unknown)), + HostTypeSchema::Array(Box::new(arg_entry_schema())), ), HostStructField::new("truncated", HostTypeSchema::Bool), ] } } +/// One decoded argument entry: repeated names repeat the entry, and a bare +/// key without `=` carries `Option::None`. +fn arg_entry_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "ArgEntry", + vec![ + HostStructField::new("name", HostTypeSchema::String), + HostStructField::new( + "value", + HostTypeSchema::Option(Box::new(HostTypeSchema::String)), + ), + ], + ) +} + +pub(crate) fn arg_entry(name: &str, value: Option<&str>) -> Value { + Value::map(vec![ + (Value::string("name"), Value::string(name)), + ( + Value::string("value"), + match value { + Some(value) => option_some(Value::string(value)), + None => option_none(), + }, + ), + ]) +} + impl IntoHostCallOutcome for DecodedArgsResult { fn into_host_call_outcome(self) -> CallOutcome { let mut carrier = VmMap::new(); carrier.insert( Value::string("args"), - Value::Map(std::sync::Arc::new(self.args)), + Value::array( + self.args + .iter() + .map(|(name, value)| arg_entry(name, value.as_deref())) + .collect(), + ), ); carrier.insert(Value::string("truncated"), Value::Bool(self.truncated)); carrier.into_host_call_outcome() @@ -355,26 +388,11 @@ pub(crate) fn encode_args_map(args: &VmMap) -> VmResult { Ok(out) } -fn insert_decoded_arg(args: &mut VmMap, key: String, value: Value) { - let key_value = Value::string(key); - match args.get(&key_value) { - None => { - args.insert(key_value, value); - } - Some(Value::Array(existing)) => { - let mut items = existing.as_ref().clone(); - items.push(value); - args.insert(key_value, Value::array(items)); - } - Some(previous) => { - let previous = previous.clone(); - args.insert(key_value, Value::array(vec![previous, value])); - } - } -} - -pub(crate) fn decode_query_args(query: &[u8], max_args: usize) -> (VmMap, bool) { - let mut args = VmMap::new(); +pub(crate) fn decode_query_args( + query: &[u8], + max_args: usize, +) -> (Vec<(String, Option)>, bool) { + let mut args: Vec<(String, Option)> = Vec::new(); let mut seen = 0usize; let mut truncated = false; if query.is_empty() { @@ -391,15 +409,15 @@ pub(crate) fn decode_query_args(query: &[u8], max_args: usize) -> (VmMap, bool) let (raw_key, value) = match part.iter().position(|byte| *byte == b'=') { Some(idx) => ( &part[..idx], - Value::string(bytes_as_string(unescape_bytes(&part[idx + 1..]))), + Some(bytes_as_string(unescape_bytes(&part[idx + 1..]))), ), - None => (part, Value::Bool(true)), + None => (part, None), }; let key = bytes_as_string(unescape_bytes(raw_key)); if key.is_empty() { continue; } - insert_decoded_arg(&mut args, key, value); + args.push((key, value)); seen += 1; } (args, truncated) @@ -485,11 +503,12 @@ pub fn encode_base64(input: &Value, no_padding: &Value) -> VmResult ret(encoded) } -/// Decodes standard Base64. Padding may be omitted. Invalid input is `null`. +/// Decodes standard Base64. Padding may be omitted. Invalid input is +/// `Option::None`. #[pd_host_function(name = "ngx::decode_base64", contract = crate::host::catalog::ngx_decode_base64_contract)] pub fn decode_base64(text: &str) -> VmResult { limit_input("ngx::decode_base64", text.len())?; - ret(decode_base64_bytes(text)) + ret(option_value(decode_base64_bytes(text).map(Value::bytes))) } /// MIME Base64 decode: non-alphabet bytes are skipped and decoding stops at @@ -497,7 +516,7 @@ pub fn decode_base64(text: &str) -> VmResult { #[pd_host_function(name = "ngx::decode_base64mime", contract = crate::host::catalog::ngx_decode_base64mime_contract)] pub fn decode_base64mime(text: &str) -> VmResult { limit_input("ngx::decode_base64mime", text.len())?; - ret(decode_base64_mime(text)) + ret(option_value(decode_base64_mime(text).map(Value::bytes))) } fn crc32_ieee(data: &[u8]) -> u32 { diff --git a/src/host/context.rs b/src/host/context.rs index 204577d..42a37b0 100644 --- a/src/host/context.rs +++ b/src/host/context.rs @@ -227,7 +227,7 @@ impl RequestExecutionScope { /// Uses the outer request pointer only when that frame has the exact same /// generation-safe identity and a non-null pointer. A mismatched parent /// identity never authorizes its pointer under the VM identity. The ctx - /// and disposition pointers are taken from the arguments when non-null, + /// and disposition pointers are taken from the arguments when non-(), /// otherwise from a matching outer frame. pub(crate) fn enter_for_vm( identity: RequestIdentity, @@ -482,7 +482,7 @@ pub fn current_request_live() -> Result { current_frame().map(|frame| !frame.request.is_null()) } -#[allow(dead_code)] // crate-private helper; tests assert it never returns Some(null) +#[allow(dead_code)] // crate-private helper; tests assert it never returns Some(()) pub(crate) fn current_request_ptr() -> Option<*mut ngx_http_request_t> { current_frame().ok().and_then(|frame| { if frame.request.is_null() { diff --git a/src/host/mod.rs b/src/host/mod.rs index baf1985..e09214a 100644 --- a/src/host/mod.rs +++ b/src/host/mod.rs @@ -12,6 +12,7 @@ pub(crate) mod context; pub(crate) mod standard; pub(crate) mod timer; +pub use catalog::is_standard_host_import; pub use catalog::{ HostCapability, HostFamily, HostManifestEntry, HostSyncKind, LOG_ALLOWED_NAMES, NGX_COOKIE_TIME_NAME, NGX_CRC32_LONG_NAME, NGX_CRC32_SHORT_NAME, NGX_CTX_CLEAR_NAME, @@ -56,7 +57,6 @@ pub use catalog::{ host_allowed_in_timer_callback, host_validates_for_log, log_allowlist_accepts, manifest_entry, ngx_host_catalog, ngx_host_manifest, timer_callback_schema, }; -pub use catalog::{compile_host_catalog, is_standard_host_import}; pub use catalog::{ NGX_TIMER_AT_NAME, NGX_TIMER_EVERY_NAME, NGX_TIMER_PENDING_COUNT_NAME, @@ -164,6 +164,41 @@ pub fn production_runtime_host_names() -> &'static [&'static str] { .as_slice() } +/// Compiler catalog for production ngx scripts. +/// +/// It carries the ngx host surface plus pd-vm's standard host catalog (I/O, +/// HTTP, and SQLite). The standard families must be part of the *compile* +/// catalog: their handles are resources, and a name that the compiler resolves +/// through the legacy builtin table instead would carry an `int` carrier that +/// the resource-typed sibling hosts reject. The same catalog is what +/// [`install_standard_runtime`] installs at runtime through +/// `standard_composition()`, so compile-time and registered schemas agree. +pub fn compile_host_catalog() -> vm::HostApiCatalog { + static CATALOG: std::sync::OnceLock = std::sync::OnceLock::new(); + CATALOG + .get_or_init(|| { + let mut builder = vm::HostApiBuilder::new(); + for catalog in [ + catalog::ngx_host_catalog(), + (*vm::standard_host_catalog()).clone(), + ] { + for resource in catalog.resources() { + builder.resource(resource.clone()); + } + for schema in catalog.structs() { + builder.named_struct(schema.clone()); + } + for function in catalog.functions() { + builder.function(function.clone()); + } + } + builder + .build() + .expect("merged ngx + standard host catalog must validate") + }) + .clone() +} + /// Bind the calling VM through the production descriptor registry. /// /// Catalog-compiled programs resolve hosts by exact adapter. There is no @@ -211,14 +246,10 @@ pub fn ngx_host_registry() -> Result { } fn build_ngx_host_registry() -> Result { - let catalog = catalog::compile_host_catalog(); - // HostFunctionRegistry::restricted() starts from HostFunctionRegistry::new(), - // which catalog-installs HTTP adapters. Those catalog entries then reject - // ngx-compiled `http::client::*` imports that have no fingerprint — the ngx - // compile catalog deliberately omits HTTP so documented map literals keep - // compiling. empty() + deny_all is the same capability restriction without - // that HTTP catalog seed. Standard HTTP names are granted below; bind_vm_cached - // stages name-only adapters through standard_composition::ensure_surfaces. + let catalog = compile_host_catalog(); + // Start empty and install ngx and standard adapters from the same merged + // compile catalog, including HTTP, so imports and adapters share schemas. + // The standard capability profile restricts which hosts are available. let mut registry = vm::HostFunctionRegistry::empty(); registry.set_capability_profile(standard_capability_profile()); registry.set_standard_composition(vm::standard_composition()); @@ -231,6 +262,17 @@ fn build_ngx_host_registry() -> Result { vm::VmError::HostError(format!("install {}: {error}", module.name)) })?; } + // Standard families (io/http/sqlite) are part of the compile + // catalog, so their adapters must be installed from that same + // snapshot: a compiled standard import carries the exact schema + // fingerprint and has no name-only fallback. + for module in vm::standard_catalog_modules() { + module + .install_from_catalog(registry, &catalog) + .map_err(|error| { + vm::VmError::HostError(format!("install {}: {error}", module.name)) + })?; + } Ok(()) }) .map_err(|error| error.to_string())?; @@ -271,7 +313,11 @@ pub(crate) fn test_hosts_module() -> vm::HostModuleDescriptor { pub(crate) fn test_bind_catalog() -> vm::HostApiCatalog { let mut builder = vm::HostApiBuilder::new(); let test_hosts = test_hosts_module().catalog().expect("test host catalog"); - for catalog in [ngx_host_catalog(), test_hosts] { + for catalog in [ + ngx_host_catalog(), + (*vm::standard_host_catalog()).clone(), + test_hosts, + ] { for resource in catalog.resources() { builder.resource(resource.clone()); } @@ -300,6 +346,13 @@ pub(crate) fn bind_test_hosts(vm: &mut Vm) { vm::VmError::HostError(format!("install {}: {error}", module.name)) })?; } + for module in vm::standard_catalog_modules() { + module + .install_from_catalog(registry, &catalog) + .map_err(|error| { + vm::VmError::HostError(format!("install {}: {error}", module.name)) + })?; + } test_hosts_module() .install_from_catalog(registry, &catalog) .map_err(|error| vm::VmError::HostError(format!("install ngx.test: {error}")))?; @@ -324,6 +377,27 @@ mod layout { IntoHostCallOutcome, VmError, arg, borrow_arg, resource, take_arg, }; + /// Canonical guest `Option::Some` carrier (`{ tag: true, payload }`). + #[allow(dead_code)] + pub fn option_some(value: impl Into) -> vm::Value { + vm::option::some_carrier(value.into()) + } + + /// Canonical guest `Option::None` carrier (`{ tag: false }`). + #[allow(dead_code)] + pub fn option_none() -> vm::Value { + vm::option::none_carrier() + } + + /// Guest `Option` of an optional value: `Some(payload)` or `None`. + #[allow(dead_code)] + pub fn option_value(value: Option) -> vm::Value { + match value { + Some(value) => option_some(value), + None => option_none(), + } + } + pub mod codec { include!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/host/codec.rs")); } @@ -583,7 +657,7 @@ mod tests { assert_eq!(schema.params.len(), 1); assert_eq!(schema.params[0].name, "seconds"); assert_eq!(schema.params[0].ty, HostTypeSchema::Number); - assert_eq!(schema.return_type, HostTypeSchema::Null); + assert_eq!(schema.return_type, HostTypeSchema::Unit); } #[test] @@ -596,7 +670,7 @@ mod tests { assert_eq!(schema.params.len(), 1); assert_eq!(schema.params[0].name, "ms"); assert_eq!(schema.params[0].ty, HostTypeSchema::Int); - assert_eq!(schema.return_type, HostTypeSchema::Null); + assert_eq!(schema.return_type, HostTypeSchema::Unit); } #[test] diff --git a/src/host/output_stub_tests.rs b/src/host/output_stub_tests.rs index 12059c3..ef3e54d 100644 --- a/src/host/output_stub_tests.rs +++ b/src/host/output_stub_tests.rs @@ -20,6 +20,13 @@ use crate::runtime::WorkerRuntime; use nginx_sys::ngx_http_request_t; use vm::{Program, Value, Vm, VmStatus}; + +/// Serializes tests that share process-global stub or zone state. +fn stub_lock() -> std::sync::MutexGuard<'static, ()> { + static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + LOCK.lock().unwrap_or_else(|error| error.into_inner()) +} + fn compile_content(source: &str) -> Arc { compile_rss_source_for_phase(source, NgxPhase::Content).expect("content source should compile") } @@ -55,6 +62,7 @@ fn eval_in_content(request: *mut ngx_http_request_t, source: &str) -> Result(value: T) -> VmResult { #[derive(Debug)] struct RequestHeadersResult { - headers: VmMap, + headers: Vec<(String, String)>, truncated: bool, } #[derive(Debug)] struct RequestArgsResult { - args: VmMap, + args: Vec<(String, Option)>, truncated: bool, } +/// One request header entry; duplicate names repeat the entry. +fn header_entry_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "HeaderEntry", + vec![ + HostStructField::new("name", HostTypeSchema::String), + HostStructField::new("value", HostTypeSchema::String), + ], + ) +} + +fn arg_entry_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "ArgEntry", + vec![ + HostStructField::new("name", HostTypeSchema::String), + HostStructField::new( + "value", + HostTypeSchema::Option(Box::new(HostTypeSchema::String)), + ), + ], + ) +} + impl HostNamedStruct for RequestHeadersResult { const NAME: &'static str = "RequestHeadersResult"; @@ -53,7 +77,7 @@ impl HostNamedStruct for RequestHeadersResult { vec![ HostStructField::new( "headers", - HostTypeSchema::Map(Box::new(HostTypeSchema::Unknown)), + HostTypeSchema::Array(Box::new(header_entry_schema())), ), HostStructField::new("truncated", HostTypeSchema::Bool), ] @@ -67,32 +91,44 @@ impl HostNamedStruct for RequestArgsResult { vec![ HostStructField::new( "args", - HostTypeSchema::Map(Box::new(HostTypeSchema::Unknown)), + HostTypeSchema::Array(Box::new(arg_entry_schema())), ), HostStructField::new("truncated", HostTypeSchema::Bool), ] } } -fn map_envelope_outcome(field: &'static str, values: VmMap, truncated: bool) -> CallOutcome { +fn envelope_outcome(field: &'static str, values: Value, truncated: bool) -> CallOutcome { let mut carrier = VmMap::new(); - carrier.insert( - Value::string(field), - Value::Map(std::sync::Arc::new(values)), - ); + carrier.insert(Value::string(field), values); carrier.insert(Value::string("truncated"), Value::Bool(truncated)); carrier.into_host_call_outcome() } impl IntoHostCallOutcome for RequestHeadersResult { fn into_host_call_outcome(self) -> CallOutcome { - map_envelope_outcome("headers", self.headers, self.truncated) + let entries = self + .headers + .iter() + .map(|(name, value)| { + Value::map(vec![ + (Value::string("name"), Value::string(name)), + (Value::string("value"), Value::string(value)), + ]) + }) + .collect(); + envelope_outcome("headers", Value::array(entries), self.truncated) } } impl IntoHostCallOutcome for RequestArgsResult { fn into_host_call_outcome(self) -> CallOutcome { - map_envelope_outcome("args", self.args, self.truncated) + let entries = self + .args + .iter() + .map(|(name, value)| super::codec::arg_entry(name, value.as_deref())) + .collect(); + envelope_outcome("args", Value::array(entries), self.truncated) } } @@ -240,23 +276,6 @@ fn clamp_count(value: &Value, name: &str, default: i64, hard: i64) -> VmResult { - args.insert(key_value, value); - } - Some(Value::Array(existing)) => { - let mut items = existing.as_ref().clone(); - items.push(value); - args.insert(key_value, Value::array(items)); - } - Some(previous) => { - let previous = previous.clone(); - args.insert(key_value, Value::array(vec![previous, value])); - } - } -} fn http_version_number(version: ngx_uint_t) -> Option { if version == NGX_HTTP_VERSION_9 as ngx_uint_t { @@ -286,8 +305,8 @@ fn header_error(message: &str) -> VmError { VmError::HostError(format!("ngx::req::get_headers {message}")) } -fn collect_headers(list: &ngx_list_t, max: usize, raw: bool) -> VmResult<(VmMap, bool)> { - let mut headers = VmMap::new(); +fn collect_headers(list: &ngx_list_t, max: usize, raw: bool) -> VmResult<(Vec<(String, String)>, bool)> { + let mut headers: Vec<(String, String)> = Vec::new(); let mut seen = 0usize; let mut truncated = false; let mut total_bytes = 0usize; @@ -364,11 +383,10 @@ fn collect_headers(list: &ngx_list_t, max: usize, raw: bool) -> VmResult<(VmMap, total_bytes = total_bytes .saturating_add(key_bytes.len()) .saturating_add(value_bytes.len()); - insert_decoded_arg( - &mut headers, + headers.push(( bytes_as_string(key_bytes), - Value::string(bytes_as_string(value_bytes)), - ); + bytes_as_string(value_bytes), + )); seen += 1; } } @@ -732,7 +750,11 @@ pub fn get_method() -> VmResult { /// HTTP version as 0.9/1.0/1.1/2.0/3.0, or `null` if unrecognized. #[pd_host_function(name = "ngx::req::http_version", contract = crate::host::catalog::ngx_req_http_version_contract)] pub fn http_version() -> VmResult { - with_request(|request| ret(http_version_number(request.as_ref().http_version))) + with_request(|request| { + ret(option_value( + http_version_number(request.as_ref().http_version).map(Value::Float), + )) + }) } /// Original request header bytes. `no_request_line` is `null`/`false`/`true`. diff --git a/src/host/request_ctx.rs b/src/host/request_ctx.rs index 87ad786..a67e6ec 100644 --- a/src/host/request_ctx.rs +++ b/src/host/request_ctx.rs @@ -18,7 +18,7 @@ use vm::HostModuleDescriptor; use vm::{Value, VmError, VmMap}; use super::super::VmResult; -use super::{CallOutcome, IntoHostCallOutcome}; +use super::{CallOutcome, IntoHostCallOutcome, option_value}; use crate::host::{current_identity, with_current_ctx, with_current_ctx_mut, ScopeError}; /// Maximum number of top-level ctx keys. @@ -398,27 +398,27 @@ fn ret(value: impl IntoHostCallOutcome) -> VmResult { pub fn get(key: &str) -> VmResult { let identity = current_identity().map_err(scope_err)?; let value = with_current_ctx(identity, |ctx| ctx.get(key)).map_err(scope_err)?; - ret(value) + ret(option_value(ctx_string_value(value, "ngx::ctx::get")?)) } -/// OpenResty-adapted `ngx.ctx[key] = value`. Returns `null`. +/// OpenResty-adapted `ngx.ctx[key] = value`. Returns `()`. #[pd_host_function(name = "ngx::ctx::set", contract = crate::host::catalog::ngx_ctx_set_contract)] -pub fn set(key: &str, value: &Value) -> VmResult { +pub fn set(key: &str, value: String) -> VmResult { let identity = current_identity().map_err(scope_err)?; - with_current_ctx_mut(identity, |ctx| ctx.set(key, value)) + with_current_ctx_mut(identity, |ctx| ctx.set(key, &Value::string(value))) .map_err(scope_err)? .map_err(host_err)?; ret(()) } -/// Adapted delete: returns the removed value, or `null` if missing. +/// Adapted delete: returns the removed value, or `Option::None` if missing. #[pd_host_function(name = "ngx::ctx::delete", contract = crate::host::catalog::ngx_ctx_delete_contract)] pub fn delete(key: &str) -> VmResult { let identity = current_identity().map_err(scope_err)?; let value = with_current_ctx_mut(identity, |ctx| ctx.delete(key)) .map_err(scope_err)? .map_err(host_err)?; - ret(value) + ret(option_value(ctx_string_value(value, "ngx::ctx::delete")?)) } /// Clear every ctx entry. Returns `null`. @@ -429,6 +429,20 @@ pub fn clear() -> VmResult { ret(()) } +/// Project a stored ctx value onto the typed guest contract. +/// +/// Guests can only store `string` values, so anything else is a host-side +/// invariant violation rather than a guest value. +fn ctx_string_value(value: Value, name: &str) -> VmResult> { + match value { + Value::Null => Ok(None), + Value::String(text) => Ok(Some(Value::String(text))), + _ => Err(VmError::HostError(format!( + "{name} ctx values must be strings" + ))), + } +} + const REQUEST_CTX_HOST_MODULE_FUNCTIONS: &[fn() -> vm::HostFunctionDescriptor] = &[ get_descriptor, set_descriptor, diff --git a/src/host/response_meta.rs b/src/host/response_meta.rs index e7f4033..c12de84 100644 --- a/src/host/response_meta.rs +++ b/src/host/response_meta.rs @@ -12,7 +12,7 @@ use vm::HostModuleDescriptor; use vm::{Value, VmError, VmMap}; use super::super::VmResult; -use super::{CallOutcome, IntoHostCallOutcome}; +use super::{CallOutcome, IntoHostCallOutcome, option_some, option_value}; use crate::host::{ control_stop_error, current_identity, with_current_disposition_mut, with_current_request_and_disposition_mut, with_current_request_mut_ops, @@ -498,7 +498,8 @@ fn dedicated_response_header(raw: &ngx_http_request_t, name: &str) -> VmResult VmResult { +/// First matching response header value as an owned string. +fn collect_named_header(list: &ngx_list_t, name: &str) -> VmResult> { let mut values = Vec::new(); let mut part = &list.part as *const ngx_list_part_t; let mut seen_parts = [std::ptr::null(); MAX_LIST_PARTS]; @@ -529,16 +530,17 @@ fn collect_named_header(list: &ngx_list_t, name: &str) -> VmResult { if header.hash == 0 || !names_equal_ignore_case(header.key, name) { continue; } - values.push(ngx_str_to_value(header.value)?); + let Value::String(text) = ngx_str_to_value(header.value)? else { + return Err(VmError::HostError( + "response header value is not a string".into(), + )); + }; + values.push(text); } } part = part_ref.next; } - Ok(match values.len() { - 0 => Value::Null, - 1 => values.remove(0), - _ => Value::array(values), - }) + Ok(values.into_iter().next().map(|text| (*text).clone())) } fn uri_has_unsafe_bytes(uri: &str) -> Option { @@ -589,16 +591,20 @@ pub fn status_set(status: i64) -> VmResult { }) } -/// Read one response header. Missing is `null`; duplicates are an array. +/// Read one response header. Missing is `Option::None`; duplicate names yield +/// the first value (use `ngx::req::get_headers` for the full multi-value set). #[pd_host_function(name = "ngx::header::get", contract = crate::host::catalog::ngx_header_get_contract)] pub fn header_get(name: String) -> VmResult { validate_header_name(&name)?; with_response_read(|request| { let raw = request.as_ref(); if let Some(value) = dedicated_response_header(raw, &name)? { - return ret(value); + return ret(option_some(value)); } - ret(collect_named_header(&raw.headers_out.headers, &name)?) + ret(option_value( + collect_named_header(&raw.headers_out.headers, &name)? + .map(Value::string), + )) }) } diff --git a/src/host/shared_dict.rs b/src/host/shared_dict.rs index 20830a6..96e9ccf 100644 --- a/src/host/shared_dict.rs +++ b/src/host/shared_dict.rs @@ -14,7 +14,7 @@ use vm::{ ResourceTypeKey, Value, Vm, VmError, VmMap, }; -use super::{CallOutcome, IntoHostCallOutcome}; +use super::{CallOutcome, IntoHostCallOutcome, option_none, option_some, option_value}; use super::super::VmResult; use crate::shared_dict::{ self, DictValue, SharedDictHandle, StoreMode, MAX_KEY_BYTES, MAX_VALUE_BYTES, @@ -50,8 +50,9 @@ fn struct_outcome(fields: [(&'static str, Value); N]) -> CallOut Value::Map(Arc::new(carrier)).into_host_call_outcome() } +/// `Option` carrier for a result `error` field. fn error_value(error: Option<&'static str>) -> Value { - error.map_or(Value::Null, Value::string) + option_value(error.map(Value::string)) } #[derive(Debug)] @@ -73,16 +74,106 @@ impl From for SharedGetResult { } } -fn optional_string_schema() -> HostTypeSchema { - HostTypeSchema::Optional(Box::new(HostTypeSchema::String)) +fn option_schema(inner: HostTypeSchema) -> HostTypeSchema { + HostTypeSchema::Option(Box::new(inner)) } -fn optional_unknown_schema() -> HostTypeSchema { - HostTypeSchema::Optional(Box::new(HostTypeSchema::Unknown)) +/// Guest `Option` field schema (logical Option, not `Optional`). +fn optional_string_schema() -> HostTypeSchema { + option_schema(HostTypeSchema::String) } fn optional_number_schema() -> HostTypeSchema { - HostTypeSchema::Optional(Box::new(HostTypeSchema::Number)) + option_schema(HostTypeSchema::Number) +} + +/// A shared-dictionary value. One zone entry can hold a bool, number, string, +/// or byte string, so the guest contract is a tagged record exactly like +/// `SqliteValue`. +fn shared_value_schema() -> HostTypeSchema { + HostTypeSchema::named_struct( + "SharedValue", + vec![ + HostStructField::new("kind", HostTypeSchema::String), + HostStructField::new("bool_value", option_schema(HostTypeSchema::Bool)), + HostStructField::new("int_value", option_schema(HostTypeSchema::Int)), + HostStructField::new("float_value", option_schema(HostTypeSchema::Float)), + HostStructField::new("text_value", option_schema(HostTypeSchema::String)), + HostStructField::new("bytes_value", option_schema(HostTypeSchema::Bytes)), + ], + ) +} + +fn optional_shared_value_schema() -> HostTypeSchema { + option_schema(shared_value_schema()) +} + +/// Tagged record carrier for one stored value. +fn shared_value_record(value: DictValue) -> Value { + let (kind, bool_value, int_value, float_value, text_value, bytes_value) = match value { + DictValue::Bool(flag) => ( + "bool", + option_some(Value::Bool(flag)), + option_none(), + option_none(), + option_none(), + option_none(), + ), + DictValue::Int(number) => ( + "int", + option_none(), + option_some(Value::Int(number)), + option_none(), + option_none(), + option_none(), + ), + DictValue::Float(number) => ( + "float", + option_none(), + option_none(), + option_some(Value::Float(number)), + option_none(), + option_none(), + ), + DictValue::String(text) => ( + "string", + option_none(), + option_none(), + option_none(), + option_some(Value::string(text)), + option_none(), + ), + DictValue::Bytes(bytes) => ( + "bytes", + option_none(), + option_none(), + option_none(), + option_none(), + option_some(Value::bytes(bytes)), + ), + }; + Value::map(vec![ + (Value::string("kind"), Value::string(kind)), + (Value::string("bool_value"), bool_value), + (Value::string("int_value"), int_value), + (Value::string("float_value"), float_value), + (Value::string("text_value"), text_value), + (Value::string("bytes_value"), bytes_value), + ]) +} + +/// `Option` carrier for a get-class result. +fn dict_value_to_vm(value: Option) -> Value { + option_value(value.map(shared_value_record)) +} + +/// `Option` carrier for numeric get-class results. +fn dict_number_to_vm(value: Option) -> Value { + match value { + Some(DictValue::Int(number)) => option_some(Value::Int(number)), + Some(DictValue::Float(number)) => option_some(Value::Float(number)), + _ => option_none(), + } } impl HostNamedStruct for SharedGetResult { @@ -90,7 +181,7 @@ impl HostNamedStruct for SharedGetResult { fn host_struct_fields() -> Vec { vec![ - HostStructField::new("value", optional_unknown_schema()), + HostStructField::new("value", optional_shared_value_schema()), HostStructField::new("flags", HostTypeSchema::Int), HostStructField::new("stale", HostTypeSchema::Bool), HostStructField::new("error", optional_string_schema()), @@ -193,7 +284,7 @@ struct SharedIncrResult { impl From for SharedIncrResult { fn from(result: shared_dict::StoreResult) -> Self { Self { - value: dict_value_to_vm(result.value), + value: dict_number_to_vm(result.value), error: result.error, forcible: result.forcible, } @@ -231,7 +322,7 @@ struct SharedNumberResult { impl From for SharedNumberResult { fn from(result: shared_dict::GetResult) -> Self { Self { - value: dict_value_to_vm(result.value), + value: dict_number_to_vm(result.value), error: result.error, } } @@ -240,7 +331,7 @@ impl From for SharedNumberResult { impl From for SharedNumberResult { fn from(result: shared_dict::StoreResult) -> Self { Self { - value: dict_value_to_vm(result.value), + value: dict_number_to_vm(result.value), error: result.error, } } @@ -263,17 +354,6 @@ impl IntoHostCallOutcome for SharedNumberResult { } } -fn dict_value_to_vm(value: Option) -> Value { - match value { - None => Value::Null, - Some(DictValue::Bool(flag)) => Value::Bool(flag), - Some(DictValue::Int(number)) => Value::Int(number), - Some(DictValue::Float(number)) => Value::Float(number), - Some(DictValue::String(text)) => Value::String(text.into()), - Some(DictValue::Bytes(bytes)) => Value::Bytes(bytes.into()), - } -} - fn vm_to_dict_value(value: &Value) -> VmResult> { match value { Value::Null => Ok(None), @@ -301,7 +381,7 @@ fn vm_to_dict_value(value: &Value) -> VmResult> { } Ok(Some(DictValue::Bytes((**bytes).clone()))) } - _ => Err(host_err("shared dict value must be null, bool, number, string, or bytes")), + _ => Err(host_err("shared dict value must be (), bool, number, string, or bytes")), } } @@ -529,8 +609,8 @@ fn replace( #[pd_host_function(name = "ngx::shared::delete", contract = crate::host::catalog::ngx_shared_delete_contract)] fn delete(dict: ResourceRef<'_, SharedDictHandle>, key: String) -> VmResult { let key = key_bytes(&key)?; - zone_op(&dict, |name| shared_dict::dict_delete(name, key))?; - ret(()) + let removed = zone_op(&dict, |name| shared_dict::dict_delete(name, key))?; + ret(removed) } /// `newval, err, forcible = dict:incr(key, value, init?, init_ttl?)`. @@ -568,14 +648,14 @@ fn ttl(dict: ResourceRef<'_, SharedDictHandle>, key: String) -> VmResult SharedNumberResult { value: if ttl.fract() == 0.0 { - Value::Int(ttl as i64) + option_some(Value::Int(ttl as i64)) } else { - Value::Float(ttl) + option_some(Value::Float(ttl)) }, error: None, }, Err(error) => SharedNumberResult { - value: Value::Null, + value: option_none(), error: Some(error), }, }; diff --git a/src/host/time.rs b/src/host/time.rs index 69a4f88..a48aac3 100644 --- a/src/host/time.rs +++ b/src/host/time.rs @@ -24,9 +24,9 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; use pd_host_function::pd_host_function; use vm::HostModuleDescriptor; -use vm::VmError; +use vm::{Value, VmError}; -use super::{CallOutcome, IntoHostCallOutcome}; +use super::{CallOutcome, IntoHostCallOutcome, option_value}; use super::super::VmResult; const MAX_CLOCK_STALE: Duration = Duration::from_secs(1); @@ -379,7 +379,7 @@ pub fn parse_http_time(text: &str) -> VmResult { "ngx::parse_http_time input exceeds 128 bytes".to_string(), )); } - ret(parse_http_date(text)) + ret(option_value(parse_http_date(text).map(Value::Int))) } diff --git a/src/host/variables.rs b/src/host/variables.rs index 018a2e6..b6aaac3 100644 --- a/src/host/variables.rs +++ b/src/host/variables.rs @@ -9,7 +9,7 @@ use vm::HostModuleDescriptor; use vm::{Value, VmError}; use super::super::VmResult; -use super::{CallOutcome, IntoHostCallOutcome}; +use super::{CallOutcome, IntoHostCallOutcome, option_none, option_some}; use crate::host::{current_identity, with_current_request, with_current_request_mut, ScopeError}; const MAX_VAR_NAME: usize = 1_024; @@ -94,15 +94,15 @@ pub fn get(name: &str) -> VmResult { // public lookup API. let vv = unsafe { ngx_http_get_variable(r, &mut ns, hash) }; if vv.is_null() { - return ret(Value::Null); + return ret(option_none()); } if unsafe { ngx_rustscript_c_variable_not_found(vv) } != 0 { - return ret(Value::Null); + return ret(option_none()); } let vv = unsafe { &*vv }; let len = vv.len() as usize; if vv.data.is_null() || len == 0 { - return ret(String::new()); + return ret(option_some(Value::string(String::new()))); } if len > MAX_VAR_VALUE { return Err(VmError::HostError(format!( @@ -110,7 +110,7 @@ pub fn get(name: &str) -> VmResult { ))); } let bytes = unsafe { std::slice::from_raw_parts(vv.data, len) }.to_vec(); - ret(bytes_as_string(bytes)) + ret(option_some(Value::string(bytes_as_string(bytes)))) }) } diff --git a/src/request_state.rs b/src/request_state.rs index 8d76ee4..102ddab 100644 --- a/src/request_state.rs +++ b/src/request_state.rs @@ -1343,7 +1343,7 @@ mod tests { let baseline = live_owned_entry_count(); { - let mut driver = Driver::new(compile("use ngx;\nngx::ctx::set(\"k\", 1);\n"), 1); + let mut driver = Driver::new(compile("use ngx;\nngx::ctx::set(\"k\", \"1\");\n"), 1); assert_eq!(driver.handle(), AccessStatus::Ok); assert!(live_owned_entry_count() > baseline); assert!(driver.cleanup().is_none()); @@ -1352,15 +1352,17 @@ mod tests { assert_eq!(live_owned_entry_count(), baseline); { - let mut driver = - Driver::new(compile("use ngx;\nngx::ctx::set(\"k\", 1);\n1 / 0;\n"), 2); + let mut driver = Driver::new( + compile("use ngx;\nngx::ctx::set(\"k\", \"1\");\n1 / 0;\n"), + 2, + ); assert_eq!(driver.handle(), AccessStatus::Error500); assert!(live_owned_entry_count() > baseline); drop(driver); } assert_eq!(live_owned_entry_count(), baseline); - let sleeping = compile("use ngx;\nngx::ctx::set(\"k\", 1);\nngx::sleep_ms(80);\n"); + let sleeping = compile("use ngx;\nngx::ctx::set(\"k\", \"1\");\nngx::sleep_ms(80);\n"); { let mut driver = Driver::new(sleeping.clone(), 3); assert_eq!(driver.handle(), AccessStatus::Again); @@ -1391,10 +1393,10 @@ mod tests { let program = compile( r#" use ngx; -if ngx::ctx::get("k") != null { +if ngx::ctx::get("k").unwrap_or("") != "" { 1 / 0; } -ngx::ctx::set("k", 1); +ngx::ctx::set("k", "1"); "#, ); let mut first = Driver::new(program.clone(), 7); @@ -1558,7 +1560,7 @@ ngx::ctx::set("k", 1); assert_default_policy_denies( &format!( - "use io;\nlet handle = io::open({:?}, \"r\");\nio::close(handle);\n", + "use io;\nlet handle = io::open({:?}, \"r\");\n", input.to_string_lossy() ), "outside the allowed roots", @@ -1569,7 +1571,7 @@ ngx::ctx::set("k", 1); #[test] fn default_standard_host_policy_denies_http() { assert_default_policy_denies( - "use http;\nhttp::client::request({\"method\": \"GET\", \"url\": \"http://127.0.0.1:9/\"});\n", + "use http;\nlet request = http::request::new(\"GET\", \"http://127.0.0.1:9/\");\nhttp::client::request(request);\n", "scheme 'http' is not allowed", ); } @@ -1577,13 +1579,14 @@ ngx::ctx::set("k", 1); #[test] fn default_standard_host_policy_denies_sqlite() { assert_default_policy_denies( - "use sqlite;\nlet db = sqlite::open({\"path\": \"state.db\", \"mode\": \"read_write_create\"});\nsqlite::close(db);\n", + "use sqlite;\nlet db = sqlite::open({path: Option::Some(\"state.db\"), mode: Option::Some(\"read_write_create\")});\nsqlite::close(db);\n", "database root is not configured", ); } #[test] fn request_owned_vm_runs_pd_vm_async_io_under_location_policy() { + let _serial = crate::socket::tcp::serial_test_lock(); let root = std::env::temp_dir().join(format!( "ngx-rustscript-standard-io-{}-{}", std::process::id(), @@ -1593,7 +1596,7 @@ ngx::ctx::set("k", 1); let input = root.join("input.txt"); std::fs::write(&input, "policy-ok").expect("write input"); let source = format!( - "use io;\nlet handle = io::open({:?}, \"r\");\nlet text = io::read_all(handle);\nio::close(handle);\nif text != \"policy-ok\" {{ 1 / 0; }}\n", + "use io;\nfn read(path: string) -> Result {{\n let handle = io::open(path, \"r\")?;\n let text = io::read_all(handle)?;\n io::close(handle)?;\n Result::Ok(text)\n}}\nlet read_result = read({:?});\nlet text = match read_result {{\n Result::Ok(text) => text,\n Result::Err(message) => message\n}};\nif text != \"policy-ok\" {{ 1 / 0; }}\n", input.to_string_lossy() ); let mut policy = StandardHostPolicy::fail_closed(); @@ -1648,7 +1651,7 @@ ngx::ctx::set("k", 1); let mut content = worker .bind_phase_exec( compile( - "use ngx;\nngx::ctx::set(\"c\", ngx::ctx::get(\"k\"));\nngx::sleep_ms(5);\n", + "use ngx;\nngx::ctx::set(\"c\", ngx::ctx::get(\"k\").unwrap_or(\"\"));\nngx::sleep_ms(5);\n", ), 1, NgxPhase::Content, @@ -1711,7 +1714,7 @@ ngx::ctx::set("k", 1); .expect("bind access"); let mut content = worker .bind_phase_exec( - compile("use ngx;\nngx::ctx::set(\"c\", ngx::ctx::get(\"k\"));\n"), + compile("use ngx;\nngx::ctx::set(\"c\", ngx::ctx::get(\"k\").unwrap_or(\"\"));\n"), 7, NgxPhase::Content, ) diff --git a/src/runtime.rs b/src/runtime.rs index fc4aa74..c9a9d44 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -757,10 +757,10 @@ mod tests { let program = compile_rss_source( r#" use ngx; -if ngx::ctx::get("seen") != null { +if ngx::ctx::get("seen").unwrap_or("") != "" { 1 / 0; } -ngx::ctx::set("seen", 1); +ngx::ctx::set("seen", "1"); ngx::exit(ngx::core::ok()); "#, ) @@ -798,14 +798,11 @@ ngx::exit(ngx::core::ok()); r#" use http; use ngx; -if ngx::ctx::get("seen") != null {{ 1 / 0; }} -ngx::ctx::set("seen", 1); -let response: map = http::client::request({{ - method: "GET", - url: "http://127.0.0.1:{port}/", - body: null -}}); -if response["status"] != 204 {{ 1 / 0; }} +if ngx::ctx::get("seen").unwrap_or("") != "" {{ 1 / 0; }} +ngx::ctx::set("seen", "1"); +let mut request = http::request::new("GET", "http://127.0.0.1:{port}/"); +let response = http::client::request(request); +if http::response::status(response) != 204 {{ 1 / 0; }} "# )) .expect("compile buffered HTTP fixture"); @@ -861,7 +858,7 @@ if response["status"] != 204 {{ 1 / 0; }} #[test] fn identical_source_in_distinct_programs_does_not_share_request_vms() { let mut worker = WorkerRuntime::start().unwrap(); - let source = "use ngx;\nngx::ctx::set(\"k\", 1);\n"; + let source = "use ngx;\nngx::ctx::set(\"k\", \"1\");\n"; let first = compile_rss_source(source).expect("first program"); let second = compile_rss_source(source).expect("second program"); for (request_id, program) in [(1, first), (2, second)] { @@ -884,7 +881,8 @@ if response["status"] != 204 {{ 1 / 0; }} #[test] fn configured_exec_selects_reusable_pool_from_cached_metadata() { let mut worker = WorkerRuntime::start().unwrap(); - let program = compile_rss_source("use ngx;\nngx::ctx::set(\"k\", 1);\n").expect("compile"); + let program = + compile_rss_source("use ngx;\nngx::ctx::set(\"k\", \"1\");\n").expect("compile"); let metadata = crate::phase::ScriptExecutionMetadata { requires_request_state: true, reuses_request_vm: true, @@ -911,7 +909,8 @@ if response["status"] != 204 {{ 1 / 0; }} #[test] fn reusable_request_vm_pool_separates_policy_and_phase() { let mut worker = WorkerRuntime::start().unwrap(); - let program = compile_rss_source("use ngx;\nngx::ctx::set(\"k\", 1);\n").expect("compile"); + let program = + compile_rss_source("use ngx;\nngx::ctx::set(\"k\", \"1\");\n").expect("compile"); let first_policy = crate::host::StandardHostPolicy::fail_closed(); let mut second_policy = first_policy.clone(); second_policy.io_roots.push("/tmp/isolated".into()); @@ -942,7 +941,8 @@ if response["status"] != 204 {{ 1 / 0; }} #[test] fn configured_exec_keeps_non_reusable_metadata_out_of_request_vm_pool() { let mut worker = WorkerRuntime::start().unwrap(); - let program = compile_rss_source("use ngx;\nngx::ctx::set(\"k\", 1);\n").expect("compile"); + let program = + compile_rss_source("use ngx;\nngx::ctx::set(\"k\", \"1\");\n").expect("compile"); let metadata = crate::phase::ScriptExecutionMetadata { requires_request_state: true, reuses_request_vm: false, @@ -972,7 +972,8 @@ if response["status"] != 204 {{ 1 / 0; }} fn reusable_request_vm_replaces_and_clears_async_bridge_per_checkout() { crate::async_bridge::reset_bridge_lifecycle_counts(); let mut worker = WorkerRuntime::start().unwrap(); - let program = compile_rss_source("use ngx;\nngx::ctx::set(\"k\", 1);\n").expect("compile"); + let program = + compile_rss_source("use ngx;\nngx::ctx::set(\"k\", \"1\");\n").expect("compile"); for request_id in 1..=2 { let mut exec = worker @@ -995,8 +996,9 @@ if response["status"] != 204 {{ 1 / 0; }} #[test] fn failed_request_vm_is_dropped_instead_of_reused() { let mut worker = WorkerRuntime::start().unwrap(); - let program = compile_rss_source("use ngx;\nngx::ctx::set(\"k\", 1);\nlet x = 1 / 0;\n") - .expect("compile"); + let program = + compile_rss_source("use ngx;\nngx::ctx::set(\"k\", \"1\");\nlet x = 1 / 0;\n") + .expect("compile"); for request_id in 1..=2 { let mut exec = worker diff --git a/src/semaphore.rs b/src/semaphore.rs index 814f3dd..503fb97 100644 --- a/src/semaphore.rs +++ b/src/semaphore.rs @@ -449,7 +449,7 @@ pub(crate) fn parse_post_amount(value: &Value, host: &str) -> VmResult { })?, other => { return Err(VmError::HostError(format!( - "{host} expects an integer count or null, got {other:?}" + "{host} expects an integer count or (), got {other:?}" ))); } }; @@ -480,7 +480,7 @@ pub(crate) fn parse_wait_timeout(value: &Value, host: &str) -> VmResult Err(VmError::HostError(format!( - "{host} timeout expects a number of seconds or null, got {other:?}" + "{host} timeout expects a number of seconds or (), got {other:?}" ))), } } @@ -1296,7 +1296,7 @@ sentinel; runtime: &tokio::runtime::Runtime, ) -> (vm::Vm, Arc, vm::HostOpId) { let program = crate::config::compile_rss_source( - "use ngx;\nlet s = ngx::semaphore::new(0);\nngx::semaphore::wait(s, null);\n", + "use ngx;\nlet s = ngx::semaphore::new(0);\nngx::semaphore::wait(s, ());\n", ) .expect("compile semaphore wait"); let mut vm = vm::Vm::try_new((*program).clone()).expect("vm"); diff --git a/src/socket/tcp.rs b/src/socket/tcp.rs index 538c2e6..1561808 100644 --- a/src/socket/tcp.rs +++ b/src/socket/tcp.rs @@ -63,6 +63,14 @@ static TEST_STALL_DNS: AtomicBool = AtomicBool::new(false); #[cfg(test)] static STALL_LOCK: Mutex<()> = Mutex::new(()); +/// Serialize the socket tests that assert poll ordering: they only mutate their +/// own listener sockets, but a loaded machine can flip a Pending poll to Ready +/// when several of them run at once. +#[cfg(test)] +pub(crate) fn serial_test_lock() -> std::sync::MutexGuard<'static, ()> { + lock(&STALL_LOCK) +} + #[cfg(test)] struct StallGuard { flag: &'static AtomicBool, @@ -1714,6 +1722,7 @@ mod tests { #[test] fn pending_read_and_write_run_concurrently_on_a_live_socket() { + let _serial = super::serial_test_lock(); let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("listener"); let addr = listener.local_addr().expect("addr"); let peer = std::thread::spawn(move || { diff --git a/src/socket/tls.rs b/src/socket/tls.rs index f0f14a0..a8c40d0 100644 --- a/src/socket/tls.rs +++ b/src/socket/tls.rs @@ -332,7 +332,7 @@ fn parse_reused_session(value: &Value, name: &str) -> VmResult { "{name} reused_session session handles are unsupported; pass null for automatic same-identity resumption or false to disable" ))), _ => Err(VmError::HostError(format!( - "{name} reused_session expects null, true, or false" + "{name} reused_session expects (), true, or false" ))), } } diff --git a/src/timer.rs b/src/timer.rs index b1d4694..2a11d36 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -751,7 +751,7 @@ mod tests { // of the worker id space is separate from the ascending request ids, so // a timer identity can never equal a request identity. let program = crate::config::compile_rss_source( - "use ngx;\nngx::timer::at(30, |premature| null);\nngx::timer::at(20, |premature| null);\n", + "use ngx;\nngx::timer::at(30, |premature| ());\nngx::timer::at(20, |premature| ());\n", ) .expect("compile timer script"); let mut vm = Vm::new_shared(program); @@ -815,7 +815,7 @@ mod tests { shutdown_current(); let mut worker = crate::runtime::WorkerRuntime::start().expect("worker runtime"); let program = - crate::config::compile_rss_source("use ngx;\nngx::timer::at(30, |premature| null);\n") + crate::config::compile_rss_source("use ngx;\nngx::timer::at(30, |premature| ());\n") .expect("compile timer script"); let mut vm = Vm::new_shared(program); install_timer_vm(&mut vm, &StandardHostPolicy::fail_closed()).expect("install timer vm"); diff --git a/src/vm_pool.rs b/src/vm_pool.rs index c0df337..dad2935 100644 --- a/src/vm_pool.rs +++ b/src/vm_pool.rs @@ -1,6 +1,6 @@ //! Worker-thread-local synchronous VM pool. //! -//! pd-vm at `87d8c5d5cb42b3381aecadb383c0b7bf34ff204f` stores `Arc` +//! pd-vm at `fc8b0c8179640a8ffe460a64b3690e35c2f7eb73` stores `Arc` //! internally and exposes: //! //! - [`Vm::new_shared`] — shared-program constructor (this pin has no diff --git a/tests/access_config.rs b/tests/access_config.rs index fde7b31..d20cb69 100644 --- a/tests/access_config.rs +++ b/tests/access_config.rs @@ -225,8 +225,9 @@ fn request_state_reuse_admits_buffered_http_and_excludes_retained_families() { let buffered_http = compile_rss_source( r#" use http; -let response = http::client::request({ method: "GET", url: "https://example.test/" }); -if response.status < 100 { 1 / 0; } +let request = http::request::new("GET", "https://example.test/"); +let response = http::client::request(request); +if http::response::status(response) < 100 { 1 / 0; } "#, ) .expect("buffered HTTP request"); @@ -238,8 +239,15 @@ if response.status < 100 { 1 / 0; } let sse = compile_source( r#" use http; -fn on_event(item: SseEvent) -> SseCallbackAction { { action: "continue" } } -http::client::sse({ method: "GET", url: "https://example.test/events" }, on_event); +fn on_event(event: string, data: string, id: string, retry: string) -> bool { + event; + data; + id; + retry; + true +} +let request = http::request::new("GET", "https://example.test/events"); +http::client::sse(request, on_event); "#, ) .expect("compile SSE"); @@ -253,15 +261,15 @@ http::client::sse({ method: "GET", url: "https://example.test/events" }, on_even ("cosocket", "use ngx;\nlet sock = ngx::socket::udp();\n"), ( "resource handle", - "use ngx;\nngx::ctx::set(\"k\", 1);\nlet semaphore = ngx::semaphore::new(1);\n", + "use ngx;\nngx::ctx::set(\"k\", \"1\");\nlet semaphore = ngx::semaphore::new(1);\n", ), ( "standard io", - "use io;\nlet input = io::open(\"x\", \"r\");\nio::close(input);\n", + "use io;\nfn probe(path: string) -> Result<(), string> {\n let handle = io::open(path, \"r\")?;\n io::close(handle)?;\n Result::Ok(())\n}\nprobe(\"x\");\n", ), ( "sqlite", - "use sqlite;\nlet db = sqlite::open({\"path\": \":memory:\", \"mode\": \"memory\"});\nsqlite::close(db);\n", + "use sqlite;\nlet db = sqlite::open({path: Option::Some(\":memory:\"), mode: Option::Some(\"memory\")});\nsqlite::close(db);\n", ), ]; for (label, source) in retained_or_async_cases { @@ -395,7 +403,7 @@ fn compiled_slot_caches_request_routing_and_sqlite_usage() { let sqlite = compile_rss_source( r#" use sqlite; - let db = sqlite::open({"path": ":memory:", "mode": "memory"}); + let db = sqlite::open({path: Option::Some(":memory:"), mode: Option::Some("memory")}); sqlite::close(db); "#, ) diff --git a/tests/access_nginx.rs b/tests/access_nginx.rs index 84fd002..53903b4 100644 --- a/tests/access_nginx.rs +++ b/tests/access_nginx.rs @@ -874,8 +874,8 @@ fn access_phase_ctx_persists_across_two_sleeps() { "missing persisted ctx in log:\n{log}" ); assert!( - log.contains("CTX_USER=") && log.contains("id=7") && log.contains("name=ada"), - "missing nested ctx in log:\n{log}" + log.contains("CTX_USER=ada"), + "ctx values are strings; the persisted value must round-trip:\n{log}" ); } @@ -930,7 +930,7 @@ fn nginx_denies_in_memory_sqlite_without_sqlite_root() { &script, r#" use sqlite; -let db = sqlite::open({"path": ":memory:", "mode": "memory"}); +let db = sqlite::open({path: Option::Some(":memory:"), mode: Option::Some("memory")}); sqlite::close(db); "#, ) @@ -983,7 +983,8 @@ fn nginx_async_pipeline_runs_io_http_sqlite_shared_dict_and_json() { let upstream_port = upstream.local_addr().unwrap().port(); upstream.set_nonblocking(true).unwrap(); let upstream_thread = std::thread::spawn(move || { - let deadline = std::time::Instant::now() + Duration::from_secs(5); + // Allow for a loaded box starting NGINX and running the pipeline. + let deadline = std::time::Instant::now() + Duration::from_secs(30); let (mut stream, _) = loop { match upstream.accept() { Ok(pair) => break pair, @@ -1062,7 +1063,7 @@ http {{ assert!(out.status.success(), "nginx -t failed:\n{text}"); let _running = spawn_nginx(&nginx, &prefix); - wait_for_port(listen_port, Duration::from_secs(3)); + wait_for_port(listen_port, Duration::from_secs(10)); let (status, response) = http_get(listen_port, "/pipeline"); let log = fs::read_to_string(prefix.join("logs/error.log")).unwrap_or_default(); assert_eq!(status, 200, "response:\n{response}\nerror log:\n{log}"); diff --git a/tests/body_hooks.rs b/tests/body_hooks.rs index 8717acd..ae3e339 100644 --- a/tests/body_hooks.rs +++ b/tests/body_hooks.rs @@ -635,7 +635,7 @@ fn nginx_delayed_body_success_and_main_count_lease() { r#" use ngx; ngx::req::read_body(); -ngx::say(ngx::req::get_body_data()); +ngx::say(ngx::req::get_body_data().unwrap_or("")); "#, ); let access_read = prefix.join("conf/body_access.rss"); @@ -644,7 +644,7 @@ ngx::say(ngx::req::get_body_data()); r#" use ngx; ngx::req::read_body(); -ngx::ctx::set("body", ngx::req::get_body_data()); +ngx::ctx::set("body", ngx::req::get_body_data().unwrap_or("")); "#, ); let content_see = prefix.join("conf/body_content.rss"); @@ -652,7 +652,7 @@ ngx::ctx::set("body", ngx::req::get_body_data()); &content_see, r#" use ngx; -ngx::say(ngx::ctx::get("body")); +ngx::say(ngx::ctx::get("body").unwrap_or("")); "#, ); diff --git a/tests/content_nginx.rs b/tests/content_nginx.rs index f724b0e..09a12bb 100644 --- a/tests/content_nginx.rs +++ b/tests/content_nginx.rs @@ -7,6 +7,7 @@ use std::io::{Read, Write}; use std::net::{TcpListener, TcpStream}; use std::path::{Path, PathBuf}; use std::process::{Child, Command, Stdio}; +use std::sync::Mutex; use std::time::{Duration, Instant}; use ngx_rustscript_nginx_test_stubs as _; @@ -20,6 +21,9 @@ use support::temp::isolated_prefix; use ngx_rustscript::config::{ConfigError, compile_rss_source_for_phase}; use ngx_rustscript::host::{NgxPhase, ngx_host_manifest}; +// Avoid competing worker initialization across real-NGINX fixtures. +static SERVER_TEST_LOCK: Mutex<()> = Mutex::new(()); + fn fixture(name: &str) -> PathBuf { Path::new(env!("CARGO_MANIFEST_DIR")) .join("tests/fixtures") @@ -304,6 +308,9 @@ fn first_content_phase_forbidden_script() -> Option<(String, String)> { #[test] fn nginx_t_accepts_content_by_rss_file() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let prefix = isolated_prefix("content-t-ok"); @@ -332,6 +339,9 @@ fn nginx_t_accepts_content_by_rss_file() { #[test] fn nginx_t_rejects_missing_syntax_print_and_duplicate_content() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); @@ -441,6 +451,9 @@ fn nginx_t_rejects_missing_syntax_print_and_duplicate_content() { #[test] fn nginx_t_rejects_phase_forbidden_import_via_content_by_rss_file() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let Some((name, source)) = first_content_phase_forbidden_script() else { // Catalog currently allows every host in Content. Lock that, and prove // content_by_rss_file still uses the Content compile path (get_method @@ -518,6 +531,9 @@ fn nginx_t_rejects_phase_forbidden_import_via_content_by_rss_file() { #[test] fn content_script_runs_and_get_phase_is_content() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); @@ -586,6 +602,9 @@ ngx::log(ngx::log_level::notice(), "CONTENT_PHASE=" + ngx::get_phase()); #[test] fn access_then_content_run_on_the_same_request() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); @@ -597,7 +616,7 @@ fn access_then_content_run_on_the_same_request() { r#" use ngx; ngx::ctx::set("shared", "from-access"); -ngx::log(ngx::log_level::notice(), "ACCESS_CTX=" + ngx::ctx::get("shared")); +ngx::log(ngx::log_level::notice(), "ACCESS_CTX=" + ngx::ctx::get("shared").unwrap_or("")); ngx::sleep_ms(20); ngx::log(ngx::log_level::notice(), "BOTH_ACCESS=" + ngx::get_phase()); "#, @@ -608,7 +627,7 @@ ngx::log(ngx::log_level::notice(), "BOTH_ACCESS=" + ngx::get_phase()); use ngx; ngx::log(ngx::log_level::notice(), "CONTENT_START=" + ngx::get_phase()); ngx::sleep_ms(20); -if ngx::ctx::get("shared") == "from-access" { +if ngx::ctx::get("shared").unwrap_or("") == "from-access" { ngx::log(ngx::log_level::notice(), "CONTENT_CTX=from-access"); } else { ngx::log(ngx::log_level::notice(), "CONTENT_CTX=missing"); @@ -671,6 +690,9 @@ ngx::log(ngx::log_level::notice(), "BOTH_CONTENT=" + ngx::get_phase()); #[test] fn content_sleep_does_not_block_a_concurrent_static_request() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); @@ -741,6 +763,9 @@ fn content_sleep_does_not_block_a_concurrent_static_request() { #[test] fn inherited_content_script_runs_in_child_location() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); @@ -790,6 +815,9 @@ ngx::log(ngx::log_level::notice(), "CONTENT_INHERIT=" + ngx::get_phase()); #[test] fn content_runtime_error_returns_500() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); @@ -827,6 +855,9 @@ fn content_runtime_error_returns_500() { #[test] fn content_disconnect_during_long_sleep_logs_499_before_timer() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let prefix = isolated_prefix("content-disconnect"); @@ -897,6 +928,9 @@ fn content_disconnect_during_long_sleep_logs_499_before_timer() { #[test] fn content_graceful_quit_with_long_sleep_exits_within_bound() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let prefix = isolated_prefix("content-sigquit"); diff --git a/tests/core_pin.rs b/tests/core_pin.rs index a56f8c4..c1570a3 100644 --- a/tests/core_pin.rs +++ b/tests/core_pin.rs @@ -3,7 +3,7 @@ use std::fs; use std::path::PathBuf; -const FROZEN_REV: &str = "87d8c5d5cb42b3381aecadb383c0b7bf34ff204f"; +const FROZEN_REV: &str = "fc8b0c8179640a8ffe460a64b3690e35c2f7eb73"; const STALE_PINS: &[&str] = &["f5f71eb", "f5f71ebc2f38e276c35f2aebaf63f4e2780efe99"]; fn repo_root() -> PathBuf { diff --git a/tests/example_rate_limit.rs b/tests/example_rate_limit.rs index 3f277a9..ce4b285 100644 --- a/tests/example_rate_limit.rs +++ b/tests/example_rate_limit.rs @@ -68,6 +68,13 @@ struct HarnessSpec<'a> { extra_server: &'a str, } +/// Serializes the heavy two-worker NGINX fixtures so a default-threading run +/// does not exhaust loopback ports and startup budgets. +fn nginx_lock() -> std::sync::MutexGuard<'static, ()> { + static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + LOCK.lock().unwrap_or_else(|error| error.into_inner()) +} + fn free_port() -> u16 { TcpListener::bind("127.0.0.1:0") .unwrap() @@ -385,11 +392,11 @@ fn assert_documented_constants() { "zone name missing from example: {EXAMPLE_RSS}" ); assert!( - EXAMPLE_RSS.contains("ngx::shared::safe_add(dict, key, 1, WINDOW_SECONDS, null)"), + EXAMPLE_RSS.contains("ngx::shared::safe_add(dict, key, 1, WINDOW_SECONDS, ())"), "new windows must use safe_add: {EXAMPLE_RSS}" ); assert!( - EXAMPLE_RSS.contains("ngx::shared::incr(dict, key, 1, null, null)"), + EXAMPLE_RSS.contains("ngx::shared::incr(dict, key, 1, (), ())"), "existing windows must incr without init: {EXAMPLE_RSS}" ); assert!( @@ -420,6 +427,7 @@ fn assert_documented_constants() { #[test] fn example_rss_compiles_and_requires_request_state() { + let _guard = nginx_lock(); assert_documented_constants(); let disk = fs::read_to_string(example_rss_path()).expect("read examples/rate-limit.rss"); assert_eq!( @@ -435,6 +443,7 @@ fn example_rss_compiles_and_requires_request_state() { #[test] fn example_rss_does_not_log_keys_or_trust_client_headers() { + let _guard = nginx_lock(); let rss = EXAMPLE_RSS.to_ascii_lowercase(); assert!( !rss.contains("ngx::log"), @@ -479,6 +488,7 @@ fn example_rss_does_not_log_keys_or_trust_client_headers() { #[test] fn example_conf_nginx_t_accepts_templated_copy() { + let _guard = nginx_lock(); let nginx = nginx_binary(); let module = module_cdylib(); let tmp = isolated_prefix("rate-limit-example-conf"); @@ -517,6 +527,7 @@ fn example_conf_nginx_t_accepts_templated_copy() { #[test] fn first_n_are_200_then_429_with_retry_after() { + let _guard = nginx_lock(); let (_nginx, port, tmp, _running) = setup_keyed("rate-limit-n"); for i in 0..LIMIT { let (status, body) = get_id(port, "n"); @@ -545,6 +556,7 @@ fn first_n_are_200_then_429_with_retry_after() { #[test] fn window_resets_after_ttl() { + let _guard = nginx_lock(); let (_nginx, port, tmp, _running) = setup_keyed("rate-limit-ttl"); for i in 0..LIMIT { let (status, body) = get_id(port, "ttl"); @@ -570,6 +582,7 @@ fn window_resets_after_ttl() { #[test] fn concurrent_burst_allows_exactly_n() { + let _guard = nginx_lock(); let (_nginx, port, tmp, _running) = setup_keyed("rate-limit-burst"); let allowed = AtomicUsize::new(0); let denied = AtomicUsize::new(0); @@ -610,6 +623,7 @@ fn concurrent_burst_allows_exactly_n() { #[test] fn shared_state_across_two_distinct_worker_pids() { + let _guard = nginx_lock(); let extra = format!("add_header {WORKER_PID_HEADER} $pid always;"); let (_nginx, port, tmp, _running, _) = setup(HarnessSpec { label: "rate-limit-shared-pid", @@ -687,6 +701,7 @@ fn shared_state_across_two_distinct_worker_pids() { #[test] fn distinct_trusted_keys_are_isolated() { + let _guard = nginx_lock(); let (_nginx, port, tmp, _running) = setup_keyed("rate-limit-iso"); for _ in 0..LIMIT { let (status, body) = get_id(port, "alice"); @@ -711,6 +726,7 @@ fn distinct_trusted_keys_are_isolated() { #[test] fn default_binary_remote_addr_isolates_loopback_peers() { + let _guard = nginx_lock(); let probe = TcpListener::bind("127.0.0.2:0"); assert!( probe.is_ok(), @@ -770,6 +786,7 @@ fn default_binary_remote_addr_isolates_loopback_peers() { #[test] fn missing_key_fails_closed() { + let _guard = nginx_lock(); let (_nginx, port, tmp, _running, _) = setup(HarnessSpec { label: "rate-limit-missing-key", http_extra: &format!("rss_shared_dict {ZONE} 1m;"), @@ -788,6 +805,7 @@ fn missing_key_fails_closed() { #[test] fn present_empty_key_fails_closed() { + let _guard = nginx_lock(); let (_nginx, port, tmp, _running, _) = setup(HarnessSpec { label: "rate-limit-empty-key", http_extra: &format!("rss_shared_dict {ZONE} 1m;"), @@ -806,6 +824,7 @@ fn present_empty_key_fails_closed() { #[test] fn unknown_zone_fails_closed() { + let _guard = nginx_lock(); let (_nginx, port, tmp, _running, _) = setup(HarnessSpec { label: "rate-limit-unknown-zone", http_extra: "rss_shared_dict other 1m;", @@ -824,6 +843,7 @@ fn unknown_zone_fails_closed() { #[test] fn reload_preserves_window_count() { + let _guard = nginx_lock(); let long = rss_with_window(LONG_WINDOW_SECONDS); let (nginx, port, tmp, _running, _) = setup(HarnessSpec { label: "rate-limit-reload", @@ -901,6 +921,7 @@ fn reload_preserves_window_count() { #[test] fn capacity_pressure_fails_closed_without_resetting_live_quotas() { + let _guard = nginx_lock(); let long = rss_with_window(LONG_WINDOW_SECONDS); let (_nginx, port, tmp, _running, _) = setup(HarnessSpec { label: "rate-limit-capacity", @@ -1013,6 +1034,7 @@ fn capacity_pressure_fails_closed_without_resetting_live_quotas() { #[test] fn sigquit_exits_cleanly() { + let _guard = nginx_lock(); let (nginx, port, tmp, mut running) = setup_keyed("rate-limit-quit"); let (status, body) = get_id(port, "quit"); assert_eq!(status, 200, "warmup: {body}\n{}", error_log(&tmp)); diff --git a/tests/fixtures/ctx_iso.rss b/tests/fixtures/ctx_iso.rss index 873e486..93bc9a9 100644 --- a/tests/fixtures/ctx_iso.rss +++ b/tests/fixtures/ctx_iso.rss @@ -1,5 +1,5 @@ use ngx; -let id = ngx::var::get("arg_id"); +let id = ngx::var::get("arg_id").unwrap_or(""); ngx::ctx::set("id", id); ngx::sleep_ms(80); -ngx::log(ngx::log_level::notice(), "CTX_ISO=" + ngx::encode_args({ id: ngx::ctx::get("id") })); +ngx::log(ngx::log_level::notice(), "CTX_ISO=" + ngx::encode_args({ id: ngx::ctx::get("id").unwrap_or("") })); diff --git a/tests/fixtures/ctx_persist.rss b/tests/fixtures/ctx_persist.rss index 5bf2b33..e9cad9d 100644 --- a/tests/fixtures/ctx_persist.rss +++ b/tests/fixtures/ctx_persist.rss @@ -1,9 +1,9 @@ use ngx; -ngx::ctx::set("n", 1); +ngx::ctx::set("n", "1"); ngx::ctx::set("k", "v"); -ngx::ctx::set("user", { id: 7, name: "ada" }); +ngx::ctx::set("user", "ada"); ngx::sleep_ms(10); -ngx::ctx::set("n", 2); +ngx::ctx::set("n", "2"); ngx::sleep_ms(10); -ngx::log(ngx::log_level::notice(), "CTX_PERSIST=" + ngx::encode_args({ n: ngx::ctx::get("n"), k: ngx::ctx::get("k") })); -ngx::log(ngx::log_level::notice(), "CTX_USER=" + ngx::encode_args(ngx::ctx::get("user"))); +ngx::log(ngx::log_level::notice(), "CTX_PERSIST=" + ngx::encode_args({ n: ngx::ctx::get("n").unwrap_or(""), k: ngx::ctx::get("k").unwrap_or("") })); +ngx::log(ngx::log_level::notice(), "CTX_USER=" + ngx::ctx::get("user").unwrap_or("")); diff --git a/tests/fixtures/ctx_sync.rss b/tests/fixtures/ctx_sync.rss index fd1bc01..217df95 100644 --- a/tests/fixtures/ctx_sync.rss +++ b/tests/fixtures/ctx_sync.rss @@ -1,15 +1,15 @@ use ngx; ngx::ctx::set("k", "v"); -if ngx::ctx::get("k") != "v" { +if ngx::ctx::get("k").unwrap_or("") != "v" { 1 / 0; } ngx::ctx::delete("k"); -if ngx::ctx::get("k") != null { +if ngx::ctx::get("k").unwrap_or("") != "" { 1 / 0; } -ngx::ctx::set("tmp", 1); +ngx::ctx::set("tmp", "1"); ngx::ctx::clear(); -if ngx::ctx::get("tmp") != null { +if ngx::ctx::get("tmp").unwrap_or("") != "" { 1 / 0; } ngx::log(ngx::log_level::notice(), "CTX_SYNC=ok"); diff --git a/tests/fixtures/request_iso.rss b/tests/fixtures/request_iso.rss index 23bc01d..efee6bb 100644 --- a/tests/fixtures/request_iso.rss +++ b/tests/fixtures/request_iso.rss @@ -1,3 +1,3 @@ use ngx; ngx::log(ngx::log_level::notice(), "ISO_METHOD=" + ngx::req::get_method()); -ngx::log(ngx::log_level::notice(), "ISO_VARS=" + ngx::encode_args({ token: ngx::var::get("arg_token"), hdr: ngx::var::get("http_x_token") })); +ngx::log(ngx::log_level::notice(), "ISO_VARS=" + ngx::encode_args({ token: ngx::var::get("arg_token").unwrap_or(""), hdr: ngx::var::get("http_x_token").unwrap_or("") })); diff --git a/tests/fixtures/task11_body.rss b/tests/fixtures/task11_body.rss index 4a8c0ef..6405a2e 100644 --- a/tests/fixtures/task11_body.rss +++ b/tests/fixtures/task11_body.rss @@ -1,5 +1,5 @@ use ngx; ngx::req::read_body(); ngx::req::set_body_data("TASK11-REPLACED"); -ngx::log(ngx::log_level::notice(), ngx::encode_args({ TASK11: ngx::req::get_body_data() })); +ngx::log(ngx::log_level::notice(), ngx::encode_args({ TASK11: ngx::req::get_body_data().unwrap_or("") })); ngx::exit(200); diff --git a/tests/fixtures/task7_iso.rss b/tests/fixtures/task7_iso.rss index 17a14fe..2529a67 100644 --- a/tests/fixtures/task7_iso.rss +++ b/tests/fixtures/task7_iso.rss @@ -1,5 +1,5 @@ use ngx; -ngx::var::set("writable", ngx::var::get("arg_id")); -ngx::header::set("X-Task7", ngx::var::get("writable")); +ngx::var::set("writable", ngx::var::get("arg_id").unwrap_or("")); +ngx::header::set("X-Task7", ngx::var::get("writable").unwrap_or("")); ngx::sleep_ms(30); -ngx::log(ngx::log_level::notice(), "TASK7_ISO=" + ngx::encode_args({ id: ngx::var::get("writable") })); +ngx::log(ngx::log_level::notice(), "TASK7_ISO=" + ngx::encode_args({ id: ngx::var::get("writable").unwrap_or("") })); diff --git a/tests/fixtures/task7_mutation.rss b/tests/fixtures/task7_mutation.rss index 3f6b131..b5fbe39 100644 --- a/tests/fixtures/task7_mutation.rss +++ b/tests/fixtures/task7_mutation.rss @@ -1,4 +1,46 @@ use ngx; + +fn header_value(entries: [HeaderEntry], name: string) -> string { + let mut a = ""; + if entries.length > 0 && entries[0]?.name.unwrap_or("") == name { + a = entries[0]?.value.unwrap_or(""); + } else { + a = ""; + } + let mut b = ""; + if entries.length > 1 && entries[1]?.name.unwrap_or("") == name { + b = entries[1]?.value.unwrap_or(""); + } else { + b = ""; + } + let mut c = ""; + if entries.length > 2 && entries[2]?.name.unwrap_or("") == name { + c = entries[2]?.value.unwrap_or(""); + } else { + c = ""; + } + let mut d = ""; + if entries.length > 3 && entries[3]?.name.unwrap_or("") == name { + d = entries[3]?.value.unwrap_or(""); + } else { + d = ""; + } + let mut found = ""; + if a != "" { + found = a; + } + if b != "" { + found = b; + } + if c != "" { + found = c; + } + if d != "" { + found = d; + } + found +} + ngx::req::set_method(ngx::http::post()); ngx::req::set_uri("/mutated", false); ngx::req::set_uri_args("k=v"); @@ -6,7 +48,8 @@ ngx::req::set_header("Host", "mut.example"); ngx::var::set("writable", "from-rss"); ngx::status::set(201); ngx::header::set("X-Resp", "ok"); -let request_headers: RequestHeadersResult = ngx::req::get_headers(null, null); -ngx::log(ngx::log_level::notice(), "TASK7_MUT_STATE " + ngx::encode_args({ method: ngx::req::get_method(), uri: ngx::var::get("uri"), args: ngx::var::get("args"), host: request_headers.headers["host"], var: ngx::var::get("writable"), status: ngx::status::get(), resp: ngx::header::get("X-Resp") })); +let request_headers: RequestHeadersResult = ngx::req::get_headers((), ()); +let host_name = header_value(request_headers.headers.copy(), "host"); +ngx::log(ngx::log_level::notice(), "TASK7_MUT_STATE " + ngx::encode_args({ method: ngx::req::get_method(), uri: ngx::var::get("uri").unwrap_or(""), args: ngx::var::get("args").unwrap_or(""), host: host_name, var: ngx::var::get("writable").unwrap_or(""), status: ngx::status::get(), resp: ngx::header::get("X-Resp").unwrap_or("") })); ngx::exit(201); ngx::log(ngx::log_level::notice(), "TASK7_MUT_MARKER"); diff --git a/tests/fixtures/task7_redirect.rss b/tests/fixtures/task7_redirect.rss index c70187f..74bbe1d 100644 --- a/tests/fixtures/task7_redirect.rss +++ b/tests/fixtures/task7_redirect.rss @@ -1,3 +1,3 @@ use ngx; -ngx::redirect("http://example.test/x", null); +ngx::redirect("http://example.test/x", ()); ngx::log(ngx::log_level::notice(), "TASK7_REDIR_MARKER"); diff --git a/tests/host_catalog.rs b/tests/host_catalog.rs index bfdf4a9..0ea12d3 100644 --- a/tests/host_catalog.rs +++ b/tests/host_catalog.rs @@ -39,67 +39,54 @@ fn error_text(err: &ConfigError) -> String { } #[test] -fn compiler_catalog_adds_no_standard_family_schema() { +fn compiler_catalog_carries_ngx_and_the_standard_families() { let ngx = ngx_host_catalog(); let merged = compile_host_catalog(); - // The compile catalog is the ngx surface exactly: no standard family - // schema is merged, so no standard position can be relaxed. HTTP stays a - // dynamic host import that configuration validates by name. - assert_eq!( - merged.functions(), - ngx.functions(), - "the merged catalog must carry exactly the ngx functions" - ); - assert_eq!(merged.functions().len(), ngx.functions().len()); - assert_eq!( - merged.structs(), - ngx.structs(), - "the merged catalog must not inherit the pd-vm HTTP named records" - ); - for name in ["http::client::request", "http::client::sse"] { - assert!( - merged.function(name).is_none(), - "{name} must stay a dynamic standard import, not a catalog function" - ); - assert!( - is_standard_host_import(name), - "standard import set misses {name}" - ); - assert!(host_allowed_in_phase(name, NgxPhase::Access)); - assert!(host_allowed_in_phase(name, NgxPhase::Content)); - assert!(!host_allowed_in_phase(name, NgxPhase::Log)); + // Every ngx host is catalogued with its own schema, and every standard + // family the runtime composition installs is catalogued too: the resource + // handles of io/http/sqlite only type-check against their own contracts. + for function in ngx.functions() { + let found = merged + .function(&function.name) + .unwrap_or_else(|| panic!("merged catalog must carry {}", function.name)); + assert_eq!(found, function, "{} schema drifted", function.name); } for name in [ + "http::client::request", + "http::request::new", + "http::response::status", "io::open", "io::read_all", - "io::close", "sqlite::open", "sqlite::query", "sqlite::close", ] { assert!( - merged.function(name).is_none(), - "{name} must stay on pd-vm's builtin dispatch path" + merged.function(name).is_some(), + "{name} must be catalogued so its resource schema is enforced" ); - assert!(!is_standard_host_import(name)); + assert!( + is_standard_host_import(name), + "standard import set misses {name}" + ); + assert!(host_allowed_in_phase(name, NgxPhase::Access)); + assert!(host_allowed_in_phase(name, NgxPhase::Content)); + assert!(!host_allowed_in_phase(name, NgxPhase::Log)); } } #[test] -fn standard_http_imports_compile_dynamically_and_keep_the_runtime_contract() { - // The documented loose object-literal scripts compile against the dynamic - // import, and the runtime payload variants of the pd-vm contract stay - // intact in the catalog the adapter validates against. +fn standard_http_resources_compile_and_keep_the_runtime_contract() { + // The resource-based HTTP contract compiles: the builder is a mutable + // request resource and the buffered call consumes it into a response + // resource. let program = compile( r#" use http; - let response: map = http::client::request({ - method: "GET", - url: "http://127.0.0.1:8081/data", - headers: { "accept": "application/json" }, - body: null - }); - let status: int = response["status"]; + let mut request = http::request::new("GET", "http://127.0.0.1:8081/data"); + http::request::set_header(&mut request, "accept", "application/json"); + let response = http::client::request(request); + let status: int = http::response::status(response); "#, ); assert!( @@ -111,70 +98,38 @@ fn standard_http_imports_compile_dynamically_and_keep_the_runtime_contract() { ); assert!(program_requires_request_state(&program)); - // The runtime contract still declares the duplicate-header array and the - // text/bytes header-value payload variants: the merged catalog must not - // degrade them. + // The runtime contract keeps the resource types and the typed header + // arrays the adapter registers. let contract = vm::http_host_catalog(); let request = contract .function("http::client::request") .expect("contract request schema"); - let request_fields = match &request.params[0].ty { - HostTypeSchema::Named { fields, .. } => fields.clone(), - other => panic!("request param must stay a named record, got {other:?}"), - }; - let headers = request_fields - .iter() - .find(|field| field.name == "headers") - .expect("request record carries headers"); - match &headers.ty { - HostTypeSchema::Optional(inner) => match inner.as_ref() { - HostTypeSchema::Array(inner) => match inner.as_ref() { - HostTypeSchema::Named { name, .. } => assert_eq!( - name, "HttpRequestHeader", - "headers stay an array of typed header entries (duplicates allowed)" - ), - other => panic!("header entry must stay named, got {other:?}"), - }, - other => panic!("headers must stay an array, got {other:?}"), - }, - other => panic!("headers must stay optional, got {other:?}"), + assert_eq!(request.params.len(), 1); + assert_eq!(request.params[0].name, "request"); + match &request.params[0].ty { + HostTypeSchema::Resource(key) => assert_eq!(key.as_str(), "http.request"), + other => panic!("request param must stay the http.request resource, got {other:?}"), } - let response = contract - .function("http::client::request") - .expect("contract request schema") - .return_type - .clone(); - let response_headers = match &response { - HostTypeSchema::Named { fields, .. } => fields - .iter() - .find(|field| field.name == "headers") - .expect("response record carries headers") - .ty - .clone(), - other => panic!("response must stay a named record, got {other:?}"), - }; - match response_headers { - HostTypeSchema::Array(inner) => match inner.as_ref() { - HostTypeSchema::Named { fields, .. } => { - let value = fields - .iter() - .find(|field| field.name == "value") - .expect("response header carries the value variant"); - match &value.ty { - HostTypeSchema::Named { name, fields } => { - assert_eq!(name, "HttpHeaderValue"); - let kinds: HashSet<&str> = - fields.iter().map(|field| field.name.as_str()).collect(); - assert!( - kinds.contains("text") && kinds.contains("bytes"), - "the header-value payload stays a text/bytes variant: {kinds:?}" - ); - } - other => panic!("header value must stay named, got {other:?}"), - } - } - other => panic!("response header entry must stay named, got {other:?}"), - }, + match &request.return_type { + HostTypeSchema::Resource(key) => assert_eq!(key.as_str(), "http.response"), + other => panic!("response must stay the http.response resource, got {other:?}"), + } + let new = contract + .function("http::request::new") + .expect("request builder schema"); + match &new.return_type { + HostTypeSchema::Resource(key) => assert_eq!(key.as_str(), "http.request"), + other => panic!("request builder must return an http.request resource, got {other:?}"), + } + let values = contract + .function("http::response::header_values") + .expect("response header schema"); + match &values.return_type { + HostTypeSchema::Array(inner) => assert_eq!( + inner.as_ref(), + &HostTypeSchema::String, + "response header values stay a typed string array (duplicates allowed)" + ), other => panic!("response headers must stay an array, got {other:?}"), } } @@ -262,7 +217,7 @@ fn timer_catalog_callback_schema_is_the_declared_callable() { .unwrap_or_else(|| panic!("{name} must declare a callback parameter")); assert_eq!( callback.ty, expected, - "{name} callback must use the declared `fn(bool) -> unknown` schema" + "{name} callback must use the declared `fn(bool) -> ()` schema" ); } } @@ -274,11 +229,20 @@ fn standard_async_imports_compile_and_require_request_owned_execution() { use io; use http; use sqlite; - let file = io::open("input.txt", "r"); - let text = io::read_all(file); - io::close(file); - http::client::request({"method": "GET", "url": text}); - let db = sqlite::open({"path": "state.db", "mode": "read_write_create"}); + fn upstream_url(path: string) -> Result { + let file = io::open(path, "r")?; + let text = io::read_all(file)?; + io::close(file); + Result::Ok(text) + } + let opened = upstream_url("input.txt"); + let url = match opened { + Result::Ok(text) => text, + Result::Err(message) => "" + }; + let mut request = http::request::new("GET", url); + http::client::request(request); + let db = sqlite::open({ path: Option::Some("state.db"), mode: Option::Some("read_write_create") }); sqlite::query(db, "SELECT value FROM events", [], {}); sqlite::close(db); "#, @@ -302,46 +266,73 @@ fn documented_standard_async_interfaces_compile() { use sqlite; use bytes as builtin_bytes; - let handle: int = io::open("input.txt", "r"); - let line: string = io::read_line(handle); - let remaining: string = io::read_all(handle); - let written: int = io::write(handle, line); - let flushed: bool = io::flush(handle); - let closed: bool = io::close(handle); - let exists: bool = io::exists("input.txt"); + fn read_text(path: string) -> Result { + let handle = io::open(path, "r")?; + let line: Option = io::read_line(handle)?; + let remaining: string = io::read_all(handle)?; + io::write(handle, remaining)?; + io::flush(handle)?; + io::close(handle)?; + line.unwrap_or(""); + Result::Ok(remaining) + } - let response: map = http::client::request({ - method: "GET", - url: "https://example.test/", - headers: { "accept": "application/json" }, - body: null - }); - let status: int = response["status"]; - let body_bytes: bytes = response["body"]; - let body: string = builtin_bytes::to_utf8(body_bytes); + fn fetch(url: string) -> int { + let mut request = http::request::new("GET", url); + http::request::set_header(&mut request, "accept", "application/json"); + http::request::set_body_text(&mut request, ""); + let response = http::client::request(request); + let status: int = http::response::status(response); + let body_bytes: bytes = http::response::body(response); + let body: string = builtin_bytes::to_utf8(body_bytes); + let values = http::response::header_values(response, "content-type"); + body; + values; + status + } - fn on_sse_event(item: map) -> map { - { action: "continue" } + fn on_sse_event(event: string, data: string, id: string, retry: string) -> bool { + event; + data; + id; + retry; + true } - let sse_summary: map = http::client::sse({ - method: "GET", - url: "https://example.test/events", - timeout_ms: 1000 - }, on_sse_event); - let db: int = sqlite::open({ - path: "state.db", - mode: "read_write_create", - limits: {} - }); - let executed: map = sqlite::execute(db, "CREATE TABLE t (v TEXT)", []); - let affected: int = executed["rows_affected"]; - let queried: map = sqlite::query(db, "SELECT v FROM t", [], { max_rows: 10 }); - let truncated: bool = queried["truncated"]; - let transaction_results = sqlite::transaction(db, { - { sql: "INSERT INTO t(v) VALUES (?1)", params: {"value"} } - }); - sqlite::close(db); + fn sse_summary() -> int { + let request = http::request::new("GET", "https://example.test/events"); + let summary = http::client::sse(request, on_sse_event); + let items: int = http::sse_summary::items(summary); + http::sse_summary::status(summary); + http::sse_summary::outcome(summary); + items + } + + fn sqlite_round_trip() -> int { + let db = sqlite::open({ + path: Option::Some("state.db"), + mode: Option::Some("read_write_create"), + limits: Option::Some({}) + }); + let executed: SqliteExecuteResult = sqlite::execute(db, "CREATE TABLE t (v TEXT)", []); + let queried: SqliteQueryResult = sqlite::query(db, "SELECT v FROM t", [], { max_rows: Option::Some(10) }); + let transaction_results = sqlite::transaction(db, []); + sqlite::close(db); + transaction_results; + queried.truncated; + executed.rows_affected + } + + let text = match read_text("input.txt") { + Result::Ok(text) => text, + Result::Err(message) => "" + }; + let exists: bool = io::exists("input.txt"); + text; + exists; + fetch("https://example.test/"); + sse_summary(); + sqlite_round_trip(); "#, ); } @@ -400,31 +391,44 @@ fn shared_fixed_results_compile_as_explicit_struct_types() { let dict = ngx::shared::dict("ratelimit"); let got: SharedGetResult = ngx::shared::get(dict, "key"); - if got.error == null && !got.stale { - got.value; - got.flags; - } + let shared_value = got.value.copy(); + let kind = match shared_value { + Option::Some(entry) => entry.kind, + Option::None => "" + }; + let text = match shared_value.copy() { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + }; + kind; + text; + got.flags; + got.stale; + got.error.unwrap_or(""); - let stored: SharedStoreResult = ngx::shared::set(dict, "key", 1, 2, null); + let stored: SharedStoreResult = ngx::shared::set(dict, "key", 1, 2, ()); if stored.ok && !stored.forcible { stored.error; } let added: SharedSafeStoreResult = - ngx::shared::safe_add(dict, "key", 1, 2, null); + ngx::shared::safe_add(dict, "key", 1, 2, ()); if added.ok { added.error; } - let counted: SharedIncrResult = ngx::shared::incr(dict, "key", 1, null, null); - if counted.error == null && !counted.forcible { + let counted: SharedIncrResult = ngx::shared::incr(dict, "key", 1, (), ()); + if !counted.forcible { counted.value; + counted.error; } let ttl: SharedNumberResult = ngx::shared::ttl(dict, "key"); - if ttl.error == null { - ttl.value; - } + ttl.error; + ttl.value; + + let removed: bool = ngx::shared::delete(dict, "key"); + removed; "#, ); } @@ -435,18 +439,19 @@ fn dynamic_map_envelopes_compile_as_explicit_struct_types() { r#" use ngx; - let decoded: DecodedArgsResult = ngx::decode_args("a=1", null); + let decoded: DecodedArgsResult = ngx::decode_args("a=1", ()); if !decoded.truncated { ngx::encode_args(decoded.args); } - let headers: RequestHeadersResult = ngx::req::get_headers(null, null); + let headers: RequestHeadersResult = ngx::req::get_headers((), ()); if !headers.truncated { - ngx::encode_args(headers.headers); + let names = headers.headers.copy(); + ngx::encode_args(names); } - let uri: RequestArgsResult = ngx::req::get_uri_args(null); - let post: RequestArgsResult = ngx::req::get_post_args(null); + let uri: RequestArgsResult = ngx::req::get_uri_args(()); + let post: RequestArgsResult = ngx::req::get_post_args(()); if !uri.truncated && !post.truncated { ngx::encode_args(uri.args); ngx::encode_args(post.args); @@ -753,7 +758,7 @@ fn catalog_sleep_schema_is_openresty_seconds_number() { assert_eq!(schema.params.len(), 1); assert_eq!(schema.params[0].name, "seconds"); assert_eq!(schema.params[0].ty, HostTypeSchema::Number); - assert_eq!(schema.return_type, HostTypeSchema::Null); + assert_eq!(schema.return_type, HostTypeSchema::Unit); } #[test] @@ -766,7 +771,7 @@ fn catalog_sleep_ms_schema_preserves_millisecond_int() { assert_eq!(schema.params.len(), 1); assert_eq!(schema.params[0].name, "ms"); assert_eq!(schema.params[0].ty, HostTypeSchema::Int); - assert_eq!(schema.return_type, HostTypeSchema::Null); + assert_eq!(schema.return_type, HostTypeSchema::Unit); } #[test] @@ -1091,41 +1096,33 @@ fn catalog_dynamic_slots() -> BTreeSet { slots } -/// Exact per-function/slot allowlist for remaining Unknown/Map catalog types. +/// Exact per-function/slot allowlist for the remaining dynamic catalog types. +/// +/// Only host **parameters** may stay dynamic: a guest passes either a string or +/// a number to an OpenResty trailing-argument adaptation, or an open-keyed +/// value to `encode_args`/`print`/`say`. Every host **output** is fully typed; +/// absence is a guest `Option`, and heterogeneous bags are named records. /// /// Rationales: -/// - `header::get` return: OpenResty can yield a string or a multi-value array. -/// - request/codec maps: query/header bags are open-keyed. -/// - shared-dict `value`/store payloads: heterogeneous zone values. -/// - optional OpenResty trailing args (`exptime`, `flags`, `max_*`, timeouts). -/// - print/say/socket payloads: mixed printable or buffer values. -/// - request ctx: opaque per-request table, not a VM resource. +/// - optional OpenResty trailing args (`exptime`, `flags`, `max_*`, timeouts) +/// accept either the value or `()`; +/// - `set_header`/`set_uri_args`/`print`/`say`/socket payloads accept a mixed +/// printable or buffer value; +/// - `ngx::ctx::set` only accepts strings and stores them by value. const ALLOWED_DYNAMIC_SLOTS: &[&str] = &[ - "ngx::ctx::delete#return:Unknown", - "ngx::ctx::get#return:Unknown", - "ngx::ctx::set#value:Unknown", "ngx::decode_args#max_args:Unknown", - "ngx::decode_args#return.DecodedArgsResult.args.:Unknown", - "ngx::decode_args#return.DecodedArgsResult.args:Map", "ngx::encode_args#args:Unknown", "ngx::encode_base64#input:Unknown", "ngx::encode_base64#no_padding:Unknown", "ngx::escape_uri#escape_type:Unknown", - "ngx::header::get#return:Unknown", "ngx::header::set#value:Unknown", "ngx::header::set_all#headers:Unknown", "ngx::print#data:Unknown", "ngx::redirect#status:Unknown", "ngx::req::get_headers#max_headers:Unknown", "ngx::req::get_headers#raw:Unknown", - "ngx::req::get_headers#return.RequestHeadersResult.headers.:Unknown", - "ngx::req::get_headers#return.RequestHeadersResult.headers:Map", "ngx::req::get_post_args#max_args:Unknown", - "ngx::req::get_post_args#return.RequestArgsResult.args.:Unknown", - "ngx::req::get_post_args#return.RequestArgsResult.args:Map", "ngx::req::get_uri_args#max_args:Unknown", - "ngx::req::get_uri_args#return.RequestArgsResult.args.:Unknown", - "ngx::req::get_uri_args#return.RequestArgsResult.args:Map", "ngx::req::init_body#buffer_size:Unknown", "ngx::req::raw_header#no_request_line:Unknown", "ngx::req::set_body_file#auto_clean:Unknown", @@ -1140,18 +1137,14 @@ const ALLOWED_DYNAMIC_SLOTS: &[&str] = &[ "ngx::shared::add#value:Unknown", "ngx::shared::expire#exptime:Unknown", "ngx::shared::flush_expired#max:Unknown", - "ngx::shared::get#return.SharedGetResult.value?:Unknown", "ngx::shared::get_keys#max:Unknown", - "ngx::shared::get_stale#return.SharedGetResult.value?:Unknown", "ngx::shared::incr#init:Unknown", "ngx::shared::incr#init_ttl:Unknown", "ngx::shared::incr#value:Unknown", - "ngx::shared::lpop#return.SharedGetResult.value?:Unknown", "ngx::shared::lpush#value:Unknown", "ngx::shared::replace#exptime:Unknown", "ngx::shared::replace#flags:Unknown", "ngx::shared::replace#value:Unknown", - "ngx::shared::rpop#return.SharedGetResult.value?:Unknown", "ngx::shared::rpush#value:Unknown", "ngx::shared::safe_add#exptime:Unknown", "ngx::shared::safe_add#flags:Unknown", @@ -1183,7 +1176,7 @@ const ALLOWED_DYNAMIC_SLOTS: &[&str] = &[ #[test] fn catalog_schema_version_and_fingerprint_goldens() { let catalog = ngx_host_catalog(); - assert_eq!(NGX_HOST_CATALOG_SCHEMA_VERSION, 3); + assert_eq!(NGX_HOST_CATALOG_SCHEMA_VERSION, 5); assert_eq!(NGX_HOST_CATALOG_FINGERPRINT_V1, 0x5e37_8285_8fa0_b584); assert_eq!(NGX_HOST_CATALOG_FINGERPRINT_V2, 0x10c4_07b2_b827_eaff); assert_eq!( @@ -1201,9 +1194,36 @@ fn catalog_schema_version_and_fingerprint_goldens() { ); } +#[test] +fn host_outputs_never_carry_dynamic_or_legacy_null_schemas() { + // RustScript rejects `unknown`, `null`, and the legacy `optional` schema + // anywhere in a host output, including named fields and callable results. + fn forbidden(schema: &HostTypeSchema) -> bool { + match schema { + HostTypeSchema::Unknown | HostTypeSchema::Null | HostTypeSchema::Optional(_) => true, + HostTypeSchema::Array(inner) + | HostTypeSchema::Map(inner) + | HostTypeSchema::Option(inner) => forbidden(inner), + HostTypeSchema::Result { ok, err } => forbidden(ok) || forbidden(err), + HostTypeSchema::Callable { result, .. } => forbidden(result), + HostTypeSchema::Named { fields, .. } => fields.iter().any(|field| forbidden(&field.ty)), + _ => false, + } + } + for function in ngx_host_catalog().functions() { + assert!( + !forbidden(&function.return_type), + "{} must return a fully typed value, got {:?}", + function.name, + function.return_type + ); + } +} + #[test] fn exact_dynamic_unknown_and_map_slots_are_allowlisted() { let actual = catalog_dynamic_slots(); + let expected: BTreeSet = ALLOWED_DYNAMIC_SLOTS .iter() .map(|slot| (*slot).to_string()) diff --git a/tests/host_descriptor_install.rs b/tests/host_descriptor_install.rs index 2d2a37d..1d94817 100644 --- a/tests/host_descriptor_install.rs +++ b/tests/host_descriptor_install.rs @@ -42,7 +42,7 @@ fn production_registry_installs_every_catalogued_name() { #[test] fn production_registry_is_restricted_and_authorizes_only_catalogued_hosts() { let catalog = ngx_host_catalog(); - let deny_program = compile_rss_source(r#"use ngx; ngx::escape_uri("a", null);"#) + let deny_program = compile_rss_source(r#"use ngx; ngx::escape_uri("a", ());"#) .expect("ngx::escape_uri should compile against the catalog"); let mut denied = Vm::new_shared(deny_program); let error = HostFunctionRegistry::restricted() @@ -58,7 +58,7 @@ fn production_registry_is_restricted_and_authorizes_only_catalogued_hosts() { ); let registry = ngx_host_registry().expect("production registry"); - let allow_program = compile_rss_source(r#"use ngx; ngx::escape_uri("a", null);"#) + let allow_program = compile_rss_source(r#"use ngx; ngx::escape_uri("a", ());"#) .expect("ngx::escape_uri should compile against the catalog"); let mut allowed = Vm::new_shared(allow_program); registry @@ -129,7 +129,7 @@ fn request_pool_and_timer_bind_through_the_same_descriptor_registry() { #[test] fn restricted_registry_denies_undeclared_ngx_admission() { - let program = compile_rss_source(r#"use ngx; ngx::escape_uri("a", null);"#) + let program = compile_rss_source(r#"use ngx; ngx::escape_uri("a", ());"#) .expect("ngx::escape_uri should compile against the catalog"); let mut vm = Vm::new_shared(program); let error = HostFunctionRegistry::restricted() @@ -237,7 +237,7 @@ fn catalog_names_in(registry: &HostFunctionRegistry) -> Vec { } fn allows_escape_uri(registry: &HostFunctionRegistry) -> bool { - let program = compile_rss_source(r#"use ngx; ngx::escape_uri("a", null);"#) + let program = compile_rss_source(r#"use ngx; ngx::escape_uri("a", ());"#) .expect("ngx::escape_uri compiles"); let mut vm = Vm::new_shared(program); registry.bind_vm_cached(&mut vm).is_ok() diff --git a/tests/lifecycle.rs b/tests/lifecycle.rs index 2046972..4ee782f 100644 --- a/tests/lifecycle.rs +++ b/tests/lifecycle.rs @@ -734,14 +734,11 @@ fn buffered_http_requests_reuse_quiescent_vms_with_fresh_request_context() { r#" use http; use ngx; -if ngx::ctx::get("seen") != null {{ ngx::exit(500); }} -ngx::ctx::set("seen", 1); -let response: map = http::client::request({{ - method: "GET", - url: "http://127.0.0.1:{upstream_port}/", - body: null -}}); -if response["status"] != 204 {{ ngx::exit(502); }} +if ngx::ctx::get("seen").unwrap_or("") != "" {{ ngx::exit(500); }} +ngx::ctx::set("seen", "1"); +let mut request = http::request::new("GET", "http://127.0.0.1:{upstream_port}/"); +let response = http::client::request(request); +if http::response::status(response) != 204 {{ ngx::exit(502); }} "# ), ) @@ -910,14 +907,11 @@ fn real_nginx_reuses_buffered_http_vm_and_isolates_runtime_policy() { r#" use http; use ngx; -if ngx::ctx::get("seen") != null {{ ngx::exit(409); }} -ngx::ctx::set("seen", 1); -let response: map = http::client::request({{ - method: "GET", - url: "http://127.0.0.1:{upstream_port}/", - body: null -}}); -if response["status"] != 204 {{ ngx::exit(502); }} +if ngx::ctx::get("seen").unwrap_or("") != "" {{ ngx::exit(409); }} +ngx::ctx::set("seen", "1"); +let mut request = http::request::new("GET", "http://127.0.0.1:{upstream_port}/"); +let response = http::client::request(request); +if http::response::status(response) != 204 {{ ngx::exit(502); }} "# ), ) @@ -994,11 +988,8 @@ fn real_nginx_cancelled_buffered_http_vm_is_not_reused() { format!( r#" use http; -let response: map = http::client::request({{ - method: "GET", - url: "http://127.0.0.1:{upstream_port}/", - body: null -}}); +let mut request = http::request::new("GET", "http://127.0.0.1:{upstream_port}/"); +let response = http::client::request(request); "# ), ) @@ -1071,12 +1062,9 @@ fn real_nginx_error_and_timeout_paths_never_enter_reuse_pool() { r#" use http; use ngx; -let response: map = http::client::request({{ - method: "GET", - url: "http://127.0.0.1:{upstream_port}/", - body: null -}}); -if response["status"] != 204 {{ ngx::exit(502); }} +let mut request = http::request::new("GET", "http://127.0.0.1:{upstream_port}/"); +let response = http::client::request(request); +if http::response::status(response) != 204 {{ ngx::exit(502); }} let failure = 1 / 0; "# ), diff --git a/tests/log_config.rs b/tests/log_config.rs index 7155634..865ce89 100644 --- a/tests/log_config.rs +++ b/tests/log_config.rs @@ -174,12 +174,12 @@ fn log_phase_accepts_sync_read_log_time_meta_ctx_and_shared_dict() { compile_log("use ngx;\nngx::get_phase();\n").expect("get_phase"); compile_log("use ngx;\nngx::var::get(\"uri\");\n").expect("var get"); compile_log("use ngx;\nngx::now();\nngx::time();\n").expect("time"); - compile_log("use ngx;\nngx::escape_uri(\"a\", null);\n").expect("codec"); + compile_log("use ngx;\nngx::escape_uri(\"a\", ());\n").expect("codec"); compile_log("use ngx;\nngx::status::get();\n").expect("status get"); compile_log("use ngx;\nngx::header::get(\"Content-Type\");\n").expect("header get"); compile_log("use ngx;\nngx::ctx::set(\"k\", \"v\");\nngx::ctx::get(\"k\");\n").expect("ctx"); compile_log("use ngx;\nngx::shared::dict(\"dogs\");\n").expect("shared dict lookup"); - let ctx = compile_log("use ngx;\nngx::ctx::set(\"k\", 1);\n").expect("ctx-only"); + let ctx = compile_log("use ngx;\nngx::ctx::set(\"k\", \"1\");\n").expect("ctx-only"); assert!(ngx_rustscript::config::program_requires_request_state(&ctx)); } diff --git a/tests/log_hooks.rs b/tests/log_hooks.rs index 0ce0973..9f21a4e 100644 --- a/tests/log_hooks.rs +++ b/tests/log_hooks.rs @@ -266,7 +266,7 @@ fn log_script() -> &'static str { r#" use ngx; ngx::log(ngx::log_level::notice(), "LOG_PHASE=" + ngx::get_phase()); -ngx::log(ngx::log_level::notice(), "LOG_MARK=" + ngx::encode_args({ uri: ngx::var::get("uri") })); +ngx::log(ngx::log_level::notice(), "LOG_MARK=" + ngx::encode_args({ uri: ngx::var::get("uri").unwrap_or("") })); "# } diff --git a/tests/log_nginx.rs b/tests/log_nginx.rs index 9b766ed..8ebe603 100644 --- a/tests/log_nginx.rs +++ b/tests/log_nginx.rs @@ -7,6 +7,7 @@ use std::io::{Read, Write}; use std::net::{TcpListener, TcpStream}; use std::path::{Path, PathBuf}; use std::process::{Child, Command, Stdio}; +use std::sync::Mutex; use std::time::{Duration, Instant}; use ngx_rustscript_nginx_test_stubs as _; @@ -17,6 +18,9 @@ use support::nginx::{ }; use support::temp::isolated_prefix; +// Avoid competing worker initialization across real-NGINX fixtures. +static SERVER_TEST_LOCK: Mutex<()> = Mutex::new(()); + fn fixture(name: &str) -> PathBuf { Path::new(env!("CARGO_MANIFEST_DIR")) .join("tests/fixtures") @@ -331,12 +335,15 @@ fn log_script() -> &'static str { r#" use ngx; ngx::log(ngx::log_level::notice(), "LOG_PHASE=" + ngx::get_phase()); -ngx::log(ngx::log_level::notice(), "LOG_MARK=" + ngx::encode_args({ uri: ngx::var::get("uri") })); +ngx::log(ngx::log_level::notice(), "LOG_MARK=" + ngx::encode_args({ uri: ngx::var::get("uri").unwrap_or("") })); "# } #[test] fn nginx_t_accepts_log_by_rss_file() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let prefix = isolated_prefix("log-t-ok"); @@ -366,6 +373,9 @@ fn nginx_t_accepts_log_by_rss_file() { #[test] fn nginx_t_rejects_missing_syntax_and_duplicate_log() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); @@ -454,6 +464,9 @@ fn nginx_t_rejects_missing_syntax_and_duplicate_log() { #[test] fn nginx_t_rejects_sleep_udp_tcp_output_control_and_mutation() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let cases: &[(&str, &str, &str)] = &[ @@ -501,6 +514,9 @@ fn nginx_t_rejects_sleep_udp_tcp_output_control_and_mutation() { #[test] fn log_script_writes_marker_after_2xx_and_4xx() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); @@ -582,6 +598,9 @@ fn log_script_writes_marker_after_2xx_and_4xx() { #[test] fn log_script_writes_marker_after_499() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let prefix = isolated_prefix("log-499"); @@ -647,6 +666,9 @@ fn log_script_writes_marker_after_499() { #[test] fn log_runtime_error_does_not_mutate_response() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); @@ -703,6 +725,9 @@ fn log_runtime_error_does_not_mutate_response() { #[test] fn access_content_then_log_share_ngx_ctx() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); @@ -715,23 +740,23 @@ fn access_content_then_log_share_ngx_ctx() { r#" use ngx; ngx::ctx::set("shared", "from-access"); -ngx::log(ngx::log_level::notice(), "ACCESS_CTX=" + ngx::ctx::get("shared")); +ngx::log(ngx::log_level::notice(), "ACCESS_CTX=" + ngx::ctx::get("shared").unwrap_or("")); "#, ); write_script( &content, r#" use ngx; -ngx::ctx::set("content", ngx::ctx::get("shared")); -ngx::log(ngx::log_level::notice(), "CONTENT_CTX=" + ngx::ctx::get("content")); +ngx::ctx::set("content", ngx::ctx::get("shared").unwrap_or("")); +ngx::log(ngx::log_level::notice(), "CONTENT_CTX=" + ngx::ctx::get("content").unwrap_or("")); "#, ); write_script( &log, r#" use ngx; -ngx::log(ngx::log_level::notice(), "LOG_CTX=" + ngx::ctx::get("shared")); -ngx::log(ngx::log_level::notice(), "LOG_CONTENT_CTX=" + ngx::ctx::get("content")); +ngx::log(ngx::log_level::notice(), "LOG_CTX=" + ngx::ctx::get("shared").unwrap_or("")); +ngx::log(ngx::log_level::notice(), "LOG_CONTENT_CTX=" + ngx::ctx::get("content").unwrap_or("")); ngx::log(ngx::log_level::notice(), "LOG_PHASE=" + ngx::get_phase()); "#, ); @@ -793,6 +818,9 @@ ngx::log(ngx::log_level::notice(), "LOG_PHASE=" + ngx::get_phase()); #[test] fn concurrent_requests_log_the_matching_uri() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); @@ -850,6 +878,9 @@ fn concurrent_requests_log_the_matching_uri() { #[test] fn inherited_log_script_runs_in_child_location() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); @@ -895,6 +926,9 @@ fn inherited_log_script_runs_in_child_location() { #[test] fn log_reload_and_sigquit_do_not_wait_or_leak() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); @@ -957,7 +991,8 @@ fn log_reload_and_sigquit_do_not_wait_or_leak() { fs::read_to_string(&error).unwrap_or_default() ); let start = Instant::now(); - quit_and_wait(&mut running.child, Duration::from_secs(5)); + // A loaded box needs more than 5s to drain the log-phase workers. + quit_and_wait(&mut running.child, Duration::from_secs(15)); assert!( start.elapsed() < Duration::from_secs(5), "SIGQUIT with log handler must not wait, elapsed {:?}", @@ -977,6 +1012,9 @@ fn log_reload_and_sigquit_do_not_wait_or_leak() { #[test] fn existing_access_and_content_stay_untouched() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); @@ -1055,6 +1093,9 @@ ngx::log(ngx::log_level::notice(), "STILL_CONTENT=" + ngx::get_phase()); #[test] fn log_uses_current_loc_conf_after_internal_redirect_and_error_page() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); @@ -1137,6 +1178,9 @@ fn log_uses_current_loc_conf_after_internal_redirect_and_error_page() { #[test] fn log_proof_env_is_inert_on_ordinary_module() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); @@ -1216,6 +1260,9 @@ fn log_proof_env_is_inert_on_ordinary_module() { #[test] fn log_subrequest_requires_core_log_subrequest_on() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); if !nginx_has_ssi(&nginx) { eprintln!( @@ -1309,6 +1356,9 @@ fn log_subrequest_requires_core_log_subrequest_on() { fn concurrent_two_worker_log_maps_requests() { // Log is synchronous on the worker main thread, so one worker cannot // overlap two VMs. Two workers can; distinct markers prove mapping. + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let port = free_port(); diff --git a/tests/nginx_integration.py b/tests/nginx_integration.py index d5e8ae2..575bff4 100644 --- a/tests/nginx_integration.py +++ b/tests/nginx_integration.py @@ -189,7 +189,7 @@ def write_udp_echo_script(path, peer_port): "ngx::socket::udp::settimeout(sock, 1000);\n" f'ngx::socket::udp::setpeername(sock, "127.0.0.1", {int(peer_port)});\n' 'ngx::socket::udp::send(sock, "ping");\n' - "ngx::socket::udp::receive(sock, null);\n" + "ngx::socket::udp::receive(sock, ());\n" "ngx::socket::udp::close(sock);\n" 'ngx::log(ngx::log_level::notice(), "UDP_ECHO=ok");\n', ) @@ -1288,13 +1288,9 @@ def ctx_one(name, path): "ctx-persist must log persisted values after two waits", dump_owned(prefix, proc, error_text), ) - if not ( - "CTX_USER=" in error_text - and "id=7" in error_text - and "name=ada" in error_text - ): + if "CTX_USER=ada" not in error_text: raise CheckError( - "ctx-persist must log nested user after two waits", + "ctx-persist must log the persisted user string after two waits", dump_owned(prefix, proc, error_text), ) summary["ctx"] = "sync=ok persist=ok" diff --git a/tests/ngx_codec.rs b/tests/ngx_codec.rs index 1c024c1..3f20cb0 100644 --- a/tests/ngx_codec.rs +++ b/tests/ngx_codec.rs @@ -74,6 +74,46 @@ fn map_get<'a>(map: &'a vm::VmMap, key: &str) -> &'a Value { .unwrap_or_else(|| panic!("missing map key {key}")) } +/// Unwraps a canonical guest `Option` carrier into its payload. +fn option_payload(value: &Value) -> Option { + let (some, payload) = vm::option::payload(value).expect("Option carrier"); + if some { + Some(payload.expect("Some payload").clone()) + } else { + None + } +} + +fn expect_option_bytes(value: Value) -> Option> { + option_payload(&value).map(|payload| match payload { + Value::Bytes(bytes) => bytes.as_ref().clone(), + other => panic!("expected bytes payload, got {other:?}"), + }) +} + +fn expect_option_string_value(value: &Value) -> Option { + option_payload(value).map(|payload| match payload { + Value::String(text) => (*text).clone(), + other => panic!("expected string payload, got {other:?}"), + }) +} + +/// `(name, value)` pairs from a typed argument entry array. +fn arg_pairs(root: &Value) -> Vec<(String, Option)> { + let entries = match map_get(expect_map(root), "args") { + Value::Array(items) => items, + other => panic!("expected entry array, got {other:?}"), + }; + entries + .iter() + .map(|entry| { + let entry = expect_map(entry); + let name = expect_string(map_get(entry, "name").clone()); + (name, expect_option_string_value(map_get(entry, "value"))) + }) + .collect() +} + fn query_set(encoded: &str) -> HashSet<&str> { if encoded.is_empty() { HashSet::new() @@ -90,7 +130,7 @@ fn decode_root(query: &str, max_args: &str) -> Value { #[test] fn escape_uri_component_escapes_space_by_default_type_2() { - let escaped = expect_string(eval("use ngx;\nngx::escape_uri(\"hello world\", null);\n")); + let escaped = expect_string(eval("use ngx;\nngx::escape_uri(\"hello world\", ());\n")); assert_eq!(escaped, "hello%20world"); let typed = expect_string(eval("use ngx;\nngx::escape_uri(\"hello world\", 2);\n")); assert_eq!(typed, "hello%20world"); @@ -222,53 +262,61 @@ fn encode_args_enforces_pair_cap_inside_array_expansion() { #[test] fn decode_args_repeated_keys_empty_values_and_truncation() { - let decoded = eval("use ngx;\nngx::decode_args(\"a=1&b=2&a=3\", null);\n"); + let decoded = eval("use ngx;\nngx::decode_args(\"a=1&b=2&a=3\", ());\n"); let root = expect_map(&decoded); assert_eq!(map_get(root, "truncated"), &Value::Bool(false)); - let args = expect_map(map_get(root, "args")); - assert_eq!(map_get(args, "b"), &Value::string("2")); - match map_get(args, "a") { - Value::Array(values) => { - assert_eq!(values.as_slice(), &[Value::string("1"), Value::string("3")]); - } - other => panic!("repeated keys must become an array, got {other:?}"), - } + assert_eq!( + arg_pairs(&decoded), + vec![ + ("a".to_string(), Some("1".to_string())), + ("b".to_string(), Some("2".to_string())), + ("a".to_string(), Some("3".to_string())), + ], + "repeated keys repeat the entry" + ); - let empty = eval("use ngx;\nngx::decode_args(\"a=&b\", null);\n"); - let empty_args = expect_map(map_get(expect_map(&empty), "args")); - assert_eq!(map_get(empty_args, "a"), &Value::string("")); - assert_eq!(map_get(empty_args, "b"), &Value::Bool(true)); + let empty = eval("use ngx;\nngx::decode_args(\"a=&b\", ());\n"); + assert_eq!( + arg_pairs(&empty), + vec![ + ("a".to_string(), Some(String::new())), + ("b".to_string(), None), + ], + "an empty value is Some(\"\"); a bare key is None" + ); let truncated = eval("use ngx;\nngx::decode_args(\"a=1&b=2&c=3\", 2);\n"); let truncated_root = expect_map(&truncated); assert_eq!(map_get(truncated_root, "truncated"), &Value::Bool(true)); - let truncated_args = expect_map(map_get(truncated_root, "args")); - assert!(truncated_args.get(&Value::string("a")).is_some()); - assert!(truncated_args.get(&Value::string("b")).is_some()); - assert!(truncated_args.get(&Value::string("c")).is_none()); + assert_eq!(arg_pairs(&truncated).len(), 2); } #[test] fn decode_args_flags_empty_keys_nul_and_invalid_percent() { let flags = decode_root("foo&bar=1&foo=2", "10"); - let flag_args = expect_map(map_get(expect_map(&flags), "args")); - match map_get(flag_args, "foo") { - Value::Array(values) => { - assert_eq!(values.as_slice(), &[Value::Bool(true), Value::string("2")]); - } - other => panic!("flag then value must become [true, string], got {other:?}"), - } - assert_eq!(map_get(flag_args, "bar"), &Value::string("1")); + assert_eq!( + arg_pairs(&flags), + vec![ + ("foo".to_string(), None), + ("bar".to_string(), Some("1".to_string())), + ("foo".to_string(), Some("2".to_string())), + ], + "a bare key carries Option::None; repeats repeat the entry" + ); let empty_keys = decode_root("=hello&=world&ok=1", "10"); - let empty_args = expect_map(map_get(expect_map(&empty_keys), "args")); - assert!(empty_args.get(&Value::string("")).is_none()); - assert_eq!(map_get(empty_args, "ok"), &Value::string("1")); + let keys = arg_pairs(&empty_keys); + assert!(keys.iter().all(|(name, _)| !name.is_empty())); + assert_eq!(keys, vec![("ok".to_string(), Some("1".to_string()))]); let invalid = decode_root("a=%ZZb&b=%00c", "10"); - let invalid_args = expect_map(map_get(expect_map(&invalid), "args")); - assert_eq!(map_get(invalid_args, "a"), &Value::string("%ZZb")); - assert_eq!(map_get(invalid_args, "b"), &Value::string("\0c")); + assert_eq!( + arg_pairs(&invalid), + vec![ + ("a".to_string(), Some("%ZZb".to_string())), + ("b".to_string(), Some("\0c".to_string())), + ] + ); } #[test] @@ -278,21 +326,19 @@ fn decode_args_max_args_zero_means_hard_max_and_1024_is_accepted() { .collect::>() .join("&"); let at_cap = decode_root(&many, "0"); - let at_cap_root = expect_map(&at_cap); - assert_eq!(map_get(at_cap_root, "truncated"), &Value::Bool(false)); assert_eq!( - expect_map(map_get(at_cap_root, "args")).len(), - MAX_DECODE_ARGS + map_get(expect_map(&at_cap), "truncated"), + &Value::Bool(false) ); + assert_eq!(arg_pairs(&at_cap).len(), MAX_DECODE_ARGS); let over = format!("{many}&extra=1"); let truncated = decode_root(&over, "0"); - let truncated_root = expect_map(&truncated); - assert_eq!(map_get(truncated_root, "truncated"), &Value::Bool(true)); assert_eq!( - expect_map(map_get(truncated_root, "args")).len(), - MAX_DECODE_ARGS + map_get(expect_map(&truncated), "truncated"), + &Value::Bool(true) ); + assert_eq!(arg_pairs(&truncated).len(), MAX_DECODE_ARGS); let explicit_cap = decode_root(&many, "1024"); assert_eq!( @@ -317,18 +363,19 @@ fn decode_args_trailing_empty_ampersand_at_max_args_is_not_truncated() { &Value::Bool(false), "trailing empty & at max_args must not mark truncated" ); - let args = expect_map(map_get(root, "args")); - assert_eq!(args.len(), 2); - assert_eq!(map_get(args, "a"), &Value::string("1")); - assert_eq!(map_get(args, "b"), &Value::string("2")); + assert_eq!( + arg_pairs(&decoded), + vec![ + ("a".to_string(), Some("1".to_string())), + ("b".to_string(), Some("2".to_string())), + ] + ); } #[test] fn encode_base64_padding_and_decode_variants() { assert_eq!( - expect_string(eval( - "use ngx;\nngx::encode_base64(\"hello world\", null);\n" - )), + expect_string(eval("use ngx;\nngx::encode_base64(\"hello world\", ());\n")), "aGVsbG8gd29ybGQ=" ); assert_eq!( @@ -338,47 +385,48 @@ fn encode_base64_padding_and_decode_variants() { "aGVsbG8gd29ybGQ" ); assert_eq!( - expect_bytes(eval( + expect_option_bytes(eval( "use ngx;\nngx::decode_base64(\"aGVsbG8gd29ybGQ=\");\n" )), - b"hello world" + Some(b"hello world".to_vec()) ); assert_eq!( - expect_bytes(eval("use ngx;\nngx::decode_base64(\"aGVsbG8gd29ybGQ\");\n")), - b"hello world" + expect_option_bytes(eval("use ngx;\nngx::decode_base64(\"aGVsbG8gd29ybGQ\");\n")), + Some(b"hello world".to_vec()) ); assert_eq!( - eval("use ngx;\nngx::decode_base64(\"!!!\");\n"), - Value::Null + expect_option_bytes(eval("use ngx;\nngx::decode_base64(\"!!!\");\n")), + None, + "invalid base64 is Option::None" ); - let mime = expect_bytes(eval( + let mime = expect_option_bytes(eval( "use ngx;\nngx::decode_base64mime(\"aGVs bG8g\\nd29y bGQ=\");\n", )); - assert_eq!(mime, b"hello world"); + assert_eq!(mime, Some(b"hello world".to_vec())); assert_eq!( - eval("use ngx;\nngx::decode_base64mime(\"a\");\n"), - Value::Null + expect_option_bytes(eval("use ngx;\nngx::decode_base64mime(\"a\");\n")), + None ); } #[test] fn decode_base64mime_skips_garbage_and_stops_at_first_equals() { assert_eq!( - expect_bytes(eval( + expect_option_bytes(eval( "use ngx;\nngx::decode_base64mime(\"!aGVs bG8gd29ybGQ=\");\n" )), - b"hello world" + Some(b"hello world".to_vec()) ); assert_eq!( - expect_bytes(eval( + expect_option_bytes(eval( "use ngx;\nngx::decode_base64mime(\"aGVs=bG8gd29ybGQ=\");\n" )), - b"hel" + Some(b"hel".to_vec()) ); assert_eq!( - eval("use ngx;\nngx::decode_base64mime(\"zaaLg==\");\n"), - Value::Null + expect_option_bytes(eval("use ngx;\nngx::decode_base64mime(\"zaaLg==\");\n")), + None ); } @@ -409,7 +457,7 @@ fn hash_and_hmac_use_fixed_vectors() { "use ngx;\nngx::hmac_sha1(\"thisisverysecretstuff\", \"some string we want to sign\");\n", )); let encoded = expect_string(eval( - "use ngx;\nngx::encode_base64(ngx::hmac_sha1(\"thisisverysecretstuff\", \"some string we want to sign\"), null);\n", + "use ngx;\nngx::encode_base64(ngx::hmac_sha1(\"thisisverysecretstuff\", \"some string we want to sign\"), ());\n", )); assert_eq!(encoded, "R/pvxzHC4NLtj7S+kXFg/NePTmk="); assert_eq!(mac.len(), 20); diff --git a/tests/ngx_control_flow.rs b/tests/ngx_control_flow.rs index 1b3769f..75bf39a 100644 --- a/tests/ngx_control_flow.rs +++ b/tests/ngx_control_flow.rs @@ -154,20 +154,20 @@ fn exit_and_redirect_are_catalogued_control_hosts() { let exit = catalog.function(EXIT).unwrap(); assert_eq!(exit.params.len(), 1); assert_eq!(exit.params[0].ty, HostTypeSchema::Int); - assert_eq!(exit.return_type, HostTypeSchema::Null); + assert_eq!(exit.return_type, HostTypeSchema::Unit); let redirect = catalog.function(REDIRECT).unwrap(); assert_eq!(redirect.params.len(), 2); assert_eq!(redirect.params[0].ty, HostTypeSchema::String); assert_eq!(redirect.params[1].ty, HostTypeSchema::Unknown); - assert_eq!(redirect.return_type, HostTypeSchema::Null); + assert_eq!(redirect.return_type, HostTypeSchema::Unit); } #[test] fn control_programs_require_request_state() { let program = compile("use ngx;\nngx::exit(403);\n"); assert!(program_requires_request_state(&program)); - let redirect = compile("use ngx;\nngx::redirect(\"/x\", null);\n"); + let redirect = compile("use ngx;\nngx::redirect(\"/x\", ());\n"); assert!(program_requires_request_state(&redirect)); let access = AllowedOps::for_phase(NgxPhase::Access); assert!(access.contains(AllowedOps::CONTROL)); @@ -219,7 +219,7 @@ fn redirect_validates_status_and_rejects_unsafe_uri() { let bad_status = compile("use ngx;\nngx::redirect(\"/x\", 200);\n"); let mut driver = Driver::new(bad_status, 6); assert_eq!(driver.handle(), AccessStatus::Error500); - let unsafe_uri = compile("use ngx;\nngx::redirect(\"/x\\r\\nY: 1\", null);\n"); + let unsafe_uri = compile("use ngx;\nngx::redirect(\"/x\\r\\nY: 1\", ());\n"); let mut driver = Driver::new(unsafe_uri, 7); assert_eq!(driver.handle(), AccessStatus::Error500); } @@ -366,7 +366,7 @@ ngx::log(ngx::log_level::notice(), "OK_CONTINUED"); &prefix.join("conf/redir.rss"), r#" use ngx; -ngx::redirect("/dest", null); +ngx::redirect("/dest", ()); ngx::log(ngx::log_level::notice(), "REDIR_MARKER"); "#, ); diff --git a/tests/ngx_meta.rs b/tests/ngx_meta.rs index 0ffc924..fe733b1 100644 --- a/tests/ngx_meta.rs +++ b/tests/ngx_meta.rs @@ -17,6 +17,12 @@ use ngx_rustscript::host::{ use ngx_rustscript::notify::RequestIdentity; use vm::{HostTypeSchema, Program, Value, Vm, VmStatus}; +/// Serializes tests that share process-global stub or zone state. +fn stub_lock() -> std::sync::MutexGuard<'static, ()> { + static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + LOCK.lock().unwrap_or_else(|error| error.into_inner()) +} + fn compile(source: &str) -> Arc { compile_rss_source(source).unwrap_or_else(|err| panic!("compile failed: {err}")) } @@ -175,6 +181,7 @@ impl Drop for CycleLogGuard { #[test] fn catalog_runtime_and_manifest_agree_on_task4_hosts() { + let _guard = stub_lock(); let catalog_api = ngx_host_catalog(); let catalog: BTreeSet<&str> = catalog_api .functions() @@ -238,6 +245,7 @@ fn catalog_runtime_and_manifest_agree_on_task4_hosts() { #[test] fn task4_hosts_are_synchronous_in_the_manifest() { + let _guard = stub_lock(); for entry in ngx_host_manifest() { if entry.name.starts_with("ngx::config::") || entry.name.starts_with("ngx::worker::") @@ -259,6 +267,7 @@ fn task4_hosts_are_synchronous_in_the_manifest() { #[test] fn constant_hosts_are_zero_arg_ints_matching_nginx_sys_values() { + let _guard = stub_lock(); assert!( catalog_params("ngx::core::ok").is_empty(), "constants are zero-argument hosts" @@ -267,7 +276,7 @@ fn constant_hosts_are_zero_arg_ints_matching_nginx_sys_values() { assert!(catalog_params("ngx::core::null_value").is_empty()); assert_eq!( catalog_schema("ngx::core::null_value"), - HostTypeSchema::Null + HostTypeSchema::Unit ); assert!( compile_rss_source("use ngx;\nngx::core::null();\n").is_err(), @@ -334,6 +343,7 @@ fn constant_hosts_are_zero_arg_ints_matching_nginx_sys_values() { #[test] fn constant_manifest_uses_openresty_names_and_sync_metadata() { + let _guard = stub_lock(); let ok = manifest_named("ngx::core::ok"); assert_eq!(ok.sync, HostSyncKind::Sync); assert_eq!(ok.openresty_name, "ngx.OK"); @@ -379,6 +389,7 @@ fn constant_manifest_uses_openresty_names_and_sync_metadata() { #[test] fn configure_argument_matching_requires_an_exact_token() { + let _guard = stub_lock(); assert!(configure_has_exact_arg( "--with-compat --with-debug --with-http_ssl_module", "--with-debug" @@ -391,6 +402,7 @@ fn configure_argument_matching_requires_an_exact_token() { #[test] fn compile_time_config_metadata_is_owned_and_truthful() { + let _guard = stub_lock(); assert_eq!( expect_string(eval("use ngx;\nngx::config::subsystem();\n")), "http" @@ -425,6 +437,7 @@ fn compile_time_config_metadata_is_owned_and_truthful() { #[test] fn nginx_version_phases_include_init_worker_and_exit_worker() { + let _guard = stub_lock(); assert!(host_allowed_in_phase( "ngx::config::nginx_version", NgxPhase::InitWorker @@ -461,6 +474,7 @@ fn nginx_version_phases_include_init_worker_and_exit_worker() { #[test] fn worker_stub_pid_id_exiting_and_cycle_dependent_hosts() { + let _guard = stub_lock(); assert_eq!(expect_int(eval("use ngx;\nngx::worker::pid();\n")), 0); assert_eq!(expect_int(eval("use ngx;\nngx::worker::id();\n")), 0); assert!(!expect_bool(eval("use ngx;\nngx::worker::exiting();\n"))); @@ -489,6 +503,7 @@ fn worker_stub_pid_id_exiting_and_cycle_dependent_hosts() { #[test] fn worker_pids_mixed_slots_keep_current_and_active_workers_only() { + let _guard = stub_lock(); let _table = ProcessTableGuard::install( 100, 1, @@ -514,6 +529,7 @@ fn worker_pids_mixed_slots_keep_current_and_active_workers_only() { #[test] fn worker_pids_includes_current_pid_when_channels_are_absent() { + let _guard = stub_lock(); let _table = ProcessTableGuard::install(50, 0, &[(0, 50, 0)]); let pids = expect_pids("use ngx;\nngx::worker::pids();\n"); assert_eq!(pids, vec![50]); @@ -521,6 +537,7 @@ fn worker_pids_includes_current_pid_when_channels_are_absent() { #[test] fn worker_pids_invalid_slot_still_includes_live_current_pid() { + let _guard = stub_lock(); let _table = ProcessTableGuard::install(77, -1, &[(2, 88, 3)]); let mut pids = expect_pids("use ngx;\nngx::worker::pids();\n"); pids.sort_unstable(); @@ -536,6 +553,7 @@ fn worker_pids_invalid_slot_still_includes_live_current_pid() { #[test] fn worker_pids_does_not_duplicate_the_current_slot() { + let _guard = stub_lock(); let _table = ProcessTableGuard::install(100, 1, &[(1, 100, 5)]); let pids = expect_pids("use ngx;\nngx::worker::pids();\n"); assert_eq!(pids, vec![100]); @@ -543,6 +561,7 @@ fn worker_pids_does_not_duplicate_the_current_slot() { #[test] fn get_phase_reads_execution_scope_and_fails_outside_a_request() { + let _guard = stub_lock(); let err = eval_err("use ngx;\nngx::get_phase();\n"); let lower = err.to_ascii_lowercase(); assert!( @@ -559,6 +578,7 @@ fn get_phase_reads_execution_scope_and_fails_outside_a_request() { #[test] fn log_and_constant_schemas_are_exact() { + let _guard = stub_lock(); let catalog = ngx_host_catalog(); let log = catalog .function("ngx::log") @@ -568,7 +588,7 @@ fn log_and_constant_schemas_are_exact() { assert_eq!(log.params[0].ty, HostTypeSchema::Int); assert_eq!(log.params[1].name, "message"); assert_eq!(log.params[1].ty, HostTypeSchema::String); - assert_eq!(log.return_type, HostTypeSchema::Null); + assert_eq!(log.return_type, HostTypeSchema::Unit); assert!(catalog_params("ngx::get_phase").is_empty()); assert_eq!(catalog_schema("ngx::get_phase"), HostTypeSchema::String); @@ -579,7 +599,7 @@ fn log_and_constant_schemas_are_exact() { ); assert_eq!( catalog_schema("ngx::core::null_value"), - HostTypeSchema::Null + HostTypeSchema::Unit ); assert_eq!( catalog_schema("ngx::http::status_payment_required"), @@ -590,6 +610,7 @@ fn log_and_constant_schemas_are_exact() { #[test] fn log_without_sink_returns_host_error() { + let _guard = stub_lock(); nginx_stubs::stub_log_reset(); let err = eval_err("use ngx;\nngx::log(ngx::log_level::notice(), \"no-sink\");\n"); let lower = err.to_ascii_lowercase(); @@ -607,6 +628,7 @@ fn log_without_sink_returns_host_error() { #[test] fn log_with_cycle_sink_writes_prefixed_message() { + let _guard = stub_lock(); let _cycle = CycleLogGuard::install(); assert_eq!( eval("use ngx;\nngx::log(ngx::log_level::notice(), \"worker-only\");\n"), @@ -625,13 +647,14 @@ fn log_with_cycle_sink_writes_prefixed_message() { #[test] fn worker_only_log_rejects_invalid_level_and_embedded_nul() { + let _guard = stub_lock(); let params = catalog_params("ngx::log"); assert_eq!(params.len(), 2); assert_eq!(params[0].0, "level"); assert_eq!(params[0].1, HostTypeSchema::Int); assert_eq!(params[1].0, "message"); assert_eq!(params[1].1, HostTypeSchema::String); - assert_eq!(catalog_schema("ngx::log"), HostTypeSchema::Null); + assert_eq!(catalog_schema("ngx::log"), HostTypeSchema::Unit); let log_entry = manifest_named("ngx::log"); assert_eq!(format!("{:?}", log_entry.family), "Log"); diff --git a/tests/ngx_output.rs b/tests/ngx_output.rs index e0182fe..874a746 100644 --- a/tests/ngx_output.rs +++ b/tests/ngx_output.rs @@ -125,29 +125,29 @@ fn output_catalog_signatures_match_openresty_names() { .function(NGX_SEND_HEADERS_NAME) .expect("send_headers schema"); assert!(send.params.is_empty()); - assert_eq!(send.return_type, HostTypeSchema::Null); + assert_eq!(send.return_type, HostTypeSchema::Unit); let print = catalog.function(NGX_PRINT_NAME).expect("print schema"); assert_eq!(print.params.len(), 1); assert_eq!(print.params[0].name, "data"); assert_eq!(print.params[0].ty, HostTypeSchema::Unknown); - assert_eq!(print.return_type, HostTypeSchema::Null); + assert_eq!(print.return_type, HostTypeSchema::Unit); let say = catalog.function(NGX_SAY_NAME).expect("say schema"); assert_eq!(say.params.len(), 1); assert_eq!(say.params[0].name, "data"); assert_eq!(say.params[0].ty, HostTypeSchema::Unknown); - assert_eq!(say.return_type, HostTypeSchema::Null); + assert_eq!(say.return_type, HostTypeSchema::Unit); let flush = catalog.function(NGX_FLUSH_NAME).expect("flush schema"); assert_eq!(flush.params.len(), 1); assert_eq!(flush.params[0].name, "wait"); assert_eq!(flush.params[0].ty, HostTypeSchema::Bool); - assert_eq!(flush.return_type, HostTypeSchema::Null); + assert_eq!(flush.return_type, HostTypeSchema::Unit); let eof = catalog.function(NGX_EOF_NAME).expect("eof schema"); assert!(eof.params.is_empty()); - assert_eq!(eof.return_type, HostTypeSchema::Null); + assert_eq!(eof.return_type, HostTypeSchema::Unit); } #[test] diff --git a/tests/ngx_request_abi.rs b/tests/ngx_request_abi.rs index 324c056..257b6a2 100644 --- a/tests/ngx_request_abi.rs +++ b/tests/ngx_request_abi.rs @@ -125,7 +125,7 @@ fn c_headers_sent_reading_body_and_discard_body_follow_c_fields() { let _body = install_unread_body(ptr); req.set_reading_body(true); - let err = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(null);\n") + let err = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(());\n") .expect_err("reading_body must error"); assert!( err.to_ascii_lowercase().contains("read") || err.to_ascii_lowercase().contains("body"), @@ -134,7 +134,7 @@ fn c_headers_sent_reading_body_and_discard_body_follow_c_fields() { req.set_reading_body(false); req.set_discard_body(true); - let err = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(null);\n") + let err = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(());\n") .expect_err("discard_body must error"); assert!( err.to_ascii_lowercase().contains("discard") || err.to_ascii_lowercase().contains("body"), diff --git a/tests/ngx_request_body.rs b/tests/ngx_request_body.rs index 1f5642c..456ad74 100644 --- a/tests/ngx_request_body.rs +++ b/tests/ngx_request_body.rs @@ -154,6 +154,42 @@ fn install_file_body(req: &mut ngx_http_request_t, path: &mut Vec) -> LiveFi } } +/// Unwraps a canonical guest `Option` carrier into its payload. +fn option_payload(value: &Value) -> Option { + let (some, payload) = vm::option::payload(value).expect("Option carrier"); + if some { + Some(payload.expect("Some payload").clone()) + } else { + None + } +} + +fn expect_option_string(value: Value) -> Option { + option_payload(&value).map(|payload| match payload { + Value::String(text) => (*text).clone(), + other => panic!("expected string payload, got {other:?}"), + }) +} + +/// `(name, value)` pairs from a typed argument entry array. +fn arg_pairs(root: &Value) -> Vec<(String, Option)> { + let entries = match map_get(expect_map(root), "args") { + Value::Array(items) => items, + other => panic!("expected entry array, got {other:?}"), + }; + entries + .iter() + .map(|entry| { + let entry = expect_map(entry); + let name = match map_get(entry, "name") { + Value::String(text) => (**text).clone(), + other => panic!("expected string name, got {other:?}"), + }; + (name, expect_option_string(map_get(entry, "value").clone())) + }) + .collect() +} + fn expect_map(value: &Value) -> &VmMap { match value { Value::Map(map) => map, @@ -281,7 +317,7 @@ fn async_read_and_discard_are_forbidden_in_log() { ); } validate_program_imports_for_phase( - &compile("use ngx;\nngx::req::get_body_data();\nngx::req::get_post_args(null);\n"), + &compile("use ngx;\nngx::req::get_body_data();\nngx::req::get_post_args(());\n"), NgxPhase::Log, ) .expect("completed-body getters must be allowed in log"); @@ -348,12 +384,16 @@ fn get_body_data_copies_memory_and_nulls_file_backed() { let _live = install_in_memory_body(&mut req, &mut chunks); let ptr = (&mut *req) as *mut ngx_http_request_t; let value = eval_in_request(ptr, "use ngx;\nngx::req::get_body_data();\n").expect("data"); - assert_eq!(value, Value::string("hello body")); + assert_eq!(expect_option_string(value), Some("hello body".to_string())); let mut path = b"/tmp/ngx-rss-bodyXXXXXX".to_vec(); let _file = install_file_body(&mut req, &mut path); let file_data = eval_in_request(ptr, "use ngx;\nngx::req::get_body_data();\n").expect("file"); - assert_eq!(file_data, Value::Null); + assert_eq!( + expect_option_string(file_data), + None, + "file-backed body data is Option::None" + ); } #[test] @@ -363,12 +403,19 @@ fn get_body_file_returns_owned_path_and_null_for_memory() { let _live = install_in_memory_body(&mut req, &mut chunks); let ptr = (&mut *req) as *mut ngx_http_request_t; let none = eval_in_request(ptr, "use ngx;\nngx::req::get_body_file();\n").expect("mem"); - assert_eq!(none, Value::Null); + assert_eq!( + expect_option_string(none), + None, + "memory-backed body file is Option::None" + ); let mut path = b"/tmp/ngx-rss-owned-body".to_vec(); let _file = install_file_body(&mut req, &mut path); let got = eval_in_request(ptr, "use ngx;\nngx::req::get_body_file();\n").expect("path"); - assert_eq!(got, Value::string("/tmp/ngx-rss-owned-body")); + assert_eq!( + expect_option_string(got), + Some("/tmp/ngx-rss-owned-body".to_string()) + ); } #[test] @@ -380,18 +427,18 @@ fn get_post_args_parses_file_backed_body_without_deleting_file() { let mut path_bytes = path.to_string_lossy().as_bytes().to_vec(); let _file = install_file_body(&mut req, &mut path_bytes); let ptr = (&mut *req) as *mut ngx_http_request_t; - let decoded = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(null);\n").expect("post"); + let decoded = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(());\n").expect("post"); let root = expect_map(&decoded); assert_eq!(map_get(root, "truncated"), &Value::Bool(false)); - let parsed = expect_map(map_get(root, "args")); - assert_eq!(map_get(parsed, "b"), &Value::string("2")); - assert_eq!(map_get(parsed, "flag"), &Value::Bool(true)); - match map_get(parsed, "a") { - Value::Array(values) => { - assert_eq!(values.as_slice(), &[Value::string("1"), Value::string("3")]); - } - other => panic!("duplicate keys must become an array, got {other:?}"), - } + assert_eq!( + arg_pairs(&decoded), + vec![ + ("a".to_string(), Some("1".to_string())), + ("b".to_string(), Some("2".to_string())), + ("a".to_string(), Some("3".to_string())), + ("flag".to_string(), None), + ] + ); assert!( path.exists(), "get_post_args must not delete NGINX-owned temp files" @@ -404,11 +451,15 @@ fn get_post_args_malformed_percent_is_left_literal() { let mut body = b"a=%ZZ&b=%&c=%2".to_vec(); let _live = install_in_memory_body(&mut req, std::slice::from_mut(&mut body)); let ptr = (&mut *req) as *mut ngx_http_request_t; - let decoded = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(null);\n").expect("post"); - let parsed = expect_map(map_get(expect_map(&decoded), "args")); - assert_eq!(map_get(parsed, "a"), &Value::string("%ZZ")); - assert_eq!(map_get(parsed, "b"), &Value::string("%")); - assert_eq!(map_get(parsed, "c"), &Value::string("%2")); + let decoded = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(());\n").expect("post"); + assert_eq!( + arg_pairs(&decoded), + vec![ + ("a".to_string(), Some("%ZZ".to_string())), + ("b".to_string(), Some("%".to_string())), + ("c".to_string(), Some("%2".to_string())), + ] + ); } #[test] @@ -418,7 +469,7 @@ fn get_body_data_skips_empty_buffers_and_rejects_inverted_or_cyclic() { let _live = install_in_memory_body(&mut req, std::slice::from_mut(&mut empty)); let ptr = (&mut *req) as *mut ngx_http_request_t; let value = eval_in_request(ptr, "use ngx;\nngx::req::get_body_data();\n").expect("empty"); - assert_eq!(value, Value::string("")); + assert_eq!(expect_option_string(value), Some(String::new())); let mut inverted = b"xy".to_vec(); let mut live = install_in_memory_body(&mut req, std::slice::from_mut(&mut inverted)); @@ -650,14 +701,14 @@ fn nginx_request_body_memory_file_chunked_413_repeat_discard_and_disconnect() { r#" use ngx; ngx::req::read_body(); -let data = ngx::req::get_body_data(); -let file = ngx::req::get_body_file(); -if data == null { +let data = ngx::req::get_body_data().unwrap_or(""); +let file = ngx::req::get_body_file().unwrap_or(""); +if data == "" { ngx::say("empty"); } else { ngx::say(data); } -if file == null { +if file == "" { ngx::say("no-file"); } else { ngx::say(file); @@ -670,14 +721,14 @@ if file == null { r#" use ngx; ngx::req::read_body(); -let data = ngx::req::get_body_data(); -let file = ngx::req::get_body_file(); -if data == null { +let data = ngx::req::get_body_data().unwrap_or(""); +let file = ngx::req::get_body_file().unwrap_or(""); +if data == "" { ngx::say("file-null-data"); } else { ngx::say(data); } -if file == null { +if file == "" { ngx::say("no-file"); } else { ngx::say(file); @@ -689,9 +740,12 @@ if file == null { &post, r#" use ngx; +fn arg_pair(entries: [ArgEntry], i: int) -> string { + entries[i]?.name.unwrap_or("") + "=" + entries[i]?.value.unwrap_or(Option::None).unwrap_or("") +} ngx::req::read_body(); -let parsed: RequestArgsResult = ngx::req::get_post_args(null); -ngx::say(ngx::encode_args(parsed.args)); +let parsed: RequestArgsResult = ngx::req::get_post_args(()); +ngx::say(arg_pair(parsed.args, 0)); if parsed.truncated { ngx::say("trunc"); } else { @@ -707,8 +761,8 @@ use ngx; ngx::req::read_body(); ngx::req::read_body(); ngx::req::discard_body(); -let data = ngx::req::get_body_data(); -if data == null { +let data = ngx::req::get_body_data().unwrap_or(""); +if data == "" { ngx::say("empty"); } else { ngx::say(data); @@ -742,7 +796,7 @@ ngx::log(ngx::log_level::notice(), "LOG_BODY=" + data.unwrap_or("")); r#" use ngx; ngx::req::read_body(); -ngx::ctx::set("body", ngx::req::get_body_data()); +ngx::ctx::set("body", ngx::req::get_body_data().unwrap_or("")); "#, ); let content_see = prefix.join("conf/body_content_see.rss"); @@ -750,7 +804,7 @@ ngx::ctx::set("body", ngx::req::get_body_data()); &content_see, r#" use ngx; -ngx::say(ngx::encode_args({ body: ngx::ctx::get("body") })); +ngx::say(ngx::encode_args({ body: ngx::ctx::get("body").unwrap_or("") })); "#, ); let slow = prefix.join("conf/body_slow.rss"); @@ -759,8 +813,8 @@ ngx::say(ngx::encode_args({ body: ngx::ctx::get("body") })); r#" use ngx; ngx::req::read_body(); -let data = ngx::req::get_body_data(); -if data == null { +let data = ngx::req::get_body_data().unwrap_or(""); +if data == "" { ngx::say("empty"); } else { ngx::say(data); @@ -773,7 +827,7 @@ if data == null { r#" use ngx; ngx::req::read_body(); -ngx::say(ngx::req::get_body_data()); +ngx::say(ngx::req::get_body_data().unwrap_or("")); "#, ); diff --git a/tests/ngx_request_body_mutation.rs b/tests/ngx_request_body_mutation.rs index eb5ad39..7e25adb 100644 --- a/tests/ngx_request_body_mutation.rs +++ b/tests/ngx_request_body_mutation.rs @@ -101,6 +101,23 @@ fn eval_in_request(request: *mut ngx_http_request_t, source: &str) -> Result Option { + let (some, payload) = vm::option::payload(value).expect("Option carrier"); + if some { + Some(payload.expect("Some payload").clone()) + } else { + None + } +} + +fn expect_option_string(value: Value) -> Option { + option_payload(&value).map(|payload| match payload { + Value::String(text) => (*text).clone(), + other => panic!("expected string payload, got {other:?}"), + }) +} + fn expect_string(value: Value) -> String { match value { Value::String(text) => (*text).clone(), @@ -220,8 +237,8 @@ fn body_mutation_catalog_schemas_phases_and_capabilities() { "use ngx;\n{name}{};\n", match *name { SET_BODY_DATA => "(\"x\")", - SET_BODY_FILE => "(\"/tmp/x\", null)", - INIT_BODY => "(null)", + SET_BODY_FILE => "(\"/tmp/x\", ())", + INIT_BODY => "(())", APPEND_BODY => "(\"x\")", FINISH_BODY => "()", _ => unreachable!(), @@ -231,33 +248,33 @@ fn body_mutation_catalog_schemas_phases_and_capabilities() { assert_eq!(param_names(SET_BODY_DATA), ["data"]); assert_eq!(param_types(SET_BODY_DATA), [HostTypeSchema::String]); - assert_eq!(schema(SET_BODY_DATA).return_type, HostTypeSchema::Null); + assert_eq!(schema(SET_BODY_DATA).return_type, HostTypeSchema::Unit); assert_eq!(param_names(SET_BODY_FILE), ["path", "auto_clean"]); assert_eq!( param_types(SET_BODY_FILE), [HostTypeSchema::String, HostTypeSchema::Unknown] ); - assert_eq!(schema(SET_BODY_FILE).return_type, HostTypeSchema::Null); + assert_eq!(schema(SET_BODY_FILE).return_type, HostTypeSchema::Unit); assert_eq!(param_names(INIT_BODY), ["buffer_size"]); assert_eq!(param_types(INIT_BODY), [HostTypeSchema::Unknown]); - assert_eq!(schema(INIT_BODY).return_type, HostTypeSchema::Null); + assert_eq!(schema(INIT_BODY).return_type, HostTypeSchema::Unit); assert_eq!(param_names(APPEND_BODY), ["data"]); assert_eq!(param_types(APPEND_BODY), [HostTypeSchema::String]); - assert_eq!(schema(APPEND_BODY).return_type, HostTypeSchema::Null); + assert_eq!(schema(APPEND_BODY).return_type, HostTypeSchema::Unit); assert!(param_names(FINISH_BODY).is_empty()); - assert_eq!(schema(FINISH_BODY).return_type, HostTypeSchema::Null); + assert_eq!(schema(FINISH_BODY).return_type, HostTypeSchema::Unit); } #[test] fn body_mutation_rejected_in_log_and_without_live_request_or_write() { for source in [ "use ngx;\nngx::req::set_body_data(\"x\");\n", - "use ngx;\nngx::req::set_body_file(\"/tmp/x\", null);\n", - "use ngx;\nngx::req::init_body(null);\n", + "use ngx;\nngx::req::set_body_file(\"/tmp/x\", ());\n", + "use ngx;\nngx::req::init_body(());\n", "use ngx;\nngx::req::append_body(\"x\");\n", "use ngx;\nngx::req::finish_body();\n", ] { @@ -366,11 +383,19 @@ ngx::req::get_body_data(); "#, ) .expect("set_body_data"); - assert_eq!(expect_string(value), "alpha=1&beta=2"); - let post = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(null);\n").expect("post"); - let parsed = expect_map(map_get(expect_map(&post), "args")); - assert_eq!(map_get(parsed, "alpha"), &Value::string("1")); - assert_eq!(map_get(parsed, "beta"), &Value::string("2")); + assert_eq!( + expect_option_string(value).expect("body data is Some"), + "alpha=1&beta=2" + ); + let post = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(());\n").expect("post"); + let Value::Array(entries) = map_get(expect_map(&post), "args") else { + panic!("post args must be a typed entry array"); + }; + assert_eq!(entries.len(), 2); + let first = expect_map(&entries[0]); + let second = expect_map(&entries[1]); + assert_eq!(expect_string(map_get(first, "name").clone()), "alpha"); + assert_eq!(expect_string(map_get(second, "name").clone()), "beta"); nginx_stubs::stub_alloc_reset(); } @@ -394,7 +419,10 @@ fn set_body_data_allocation_failure_leaves_prior_body() { assert_eq!(req.request_body, prior, "prior body pointer must remain"); nginx_stubs::stub_alloc_fail_after(64); let kept = eval_in_request(ptr, "use ngx;\nngx::req::get_body_data();\n").expect("kept"); - assert_eq!(expect_string(kept), "keep-me"); + assert_eq!( + expect_option_string(kept).expect("body data is Some"), + "keep-me" + ); nginx_stubs::stub_alloc_reset(); } @@ -424,7 +452,7 @@ fn builder_state_machine_init_append_finish_and_failures() { ptr, r#" use ngx; -ngx::req::init_body(null); +ngx::req::init_body(()); ngx::req::init_body(8); ngx::req::append_body("ab"); ngx::req::append_body("cd"); @@ -433,7 +461,10 @@ ngx::req::get_body_data(); "#, ) .expect("builder"); - assert_eq!(expect_string(value), "abcd"); + assert_eq!( + expect_option_string(value).expect("body data is Some"), + "abcd" + ); let err = eval_in_request(ptr, "use ngx;\nngx::req::finish_body();\n").expect_err("repeated finish"); @@ -452,7 +483,7 @@ ngx::req::get_body_data(); "#, ) .expect("empty"); - assert_eq!(expect_string(empty), ""); + assert_eq!(expect_option_string(empty).expect("body data is Some"), ""); let err = eval_in_request( ptr, @@ -474,14 +505,17 @@ ngx::req::append_body("xyz"); ptr, r#" use ngx; -ngx::req::init_body(null); +ngx::req::init_body(()); ngx::req::append_body("pending"); ngx::req::set_body_data("direct"); ngx::req::get_body_data(); "#, ) .expect("set during builder"); - assert_eq!(expect_string(replaced), "direct"); + assert_eq!( + expect_option_string(replaced).expect("body data is Some"), + "direct" + ); nginx_stubs::stub_alloc_reset(); } @@ -497,7 +531,7 @@ fn builder_finish_install_failure_restores_builder_and_retries() { ptr, r#" use ngx; -ngx::req::init_body(null); +ngx::req::init_body(()); ngx::req::append_body("retry-me"); "#, ) @@ -524,7 +558,10 @@ ngx::req::get_body_data(); "#, ) .expect("retry finish"); - assert_eq!(expect_string(value), "retry-me"); + assert_eq!( + expect_option_string(value).expect("body data is Some"), + "retry-me" + ); nginx_stubs::stub_alloc_reset(); } @@ -618,7 +655,7 @@ fn set_body_file_empty_file_installs_file_backed_body() { fs::write(&path, b"").unwrap(); let lstat = fs::symlink_metadata(&path).unwrap(); let path_s = path.to_string_lossy().replace('\\', "\\\\"); - let got = expect_string( + let got = expect_option_string( eval_in_request( ptr, &format!( @@ -626,7 +663,8 @@ fn set_body_file_empty_file_installs_file_backed_body() { ), ) .expect("empty file"), - ); + ) + .expect("body file is Some"); assert!( got.contains("empty.txt"), "empty file body should keep the path, got {got}" @@ -639,9 +677,10 @@ fn set_body_file_empty_file_installs_file_backed_body() { "set_body_file success path must keep matching lstat/fstat identity" ); let data = eval_in_request(ptr, "use ngx;\nngx::req::get_body_data();\n").expect("data"); - assert!( - matches!(data, Value::Null), - "file-backed empty body get_body_data must be null, got {data:?}" + assert_eq!( + expect_option_string(data), + None, + "file-backed body get_body_data must be Option::None" ); nginx_stubs::stub_alloc_reset(); } @@ -1021,8 +1060,8 @@ fn nginx_body_mutation_upstream_subrequest_cleanup_and_isolation() { use ngx; ngx::req::read_body(); ngx::req::set_body_data("replaced-memory"); -ngx::log(ngx::log_level::notice(), ngx::encode_args({ mem_cl: ngx::var::get("content_length") })); -ngx::say(ngx::req::get_body_data()); +ngx::log(ngx::log_level::notice(), ngx::encode_args({ mem_cl: ngx::var::get("content_length").unwrap_or("") })); +ngx::say(ngx::req::get_body_data().unwrap_or("")); "#, ); let mem_proxy = prefix.join("conf/mut_mem_proxy.rss"); @@ -1032,7 +1071,7 @@ ngx::say(ngx::req::get_body_data()); use ngx; ngx::req::read_body(); ngx::req::set_body_data("replaced-memory"); -ngx::log(ngx::log_level::notice(), ngx::encode_args({ mem_proxy_cl: ngx::var::get("content_length") })); +ngx::log(ngx::log_level::notice(), ngx::encode_args({ mem_proxy_cl: ngx::var::get("content_length").unwrap_or("") })); "#, ); let file_true = prefix.join("conf/mut_file_true.rss"); @@ -1043,8 +1082,8 @@ ngx::log(ngx::log_level::notice(), ngx::encode_args({ mem_proxy_cl: ngx::var::ge use ngx; ngx::req::read_body(); ngx::req::set_body_file("{path}", true); -ngx::log(ngx::log_level::notice(), ngx::encode_args({{ file_true_cl: ngx::var::get("content_length") }})); -ngx::say(ngx::req::get_body_file()); +ngx::log(ngx::log_level::notice(), ngx::encode_args({{ file_true_cl: ngx::var::get("content_length").unwrap_or("") }})); +ngx::say(ngx::req::get_body_file().unwrap_or("")); "#, path = source_true_path.replace('\\', "\\\\") ), @@ -1057,8 +1096,8 @@ ngx::say(ngx::req::get_body_file()); use ngx; ngx::req::read_body(); ngx::req::set_body_file("{path}", false); -ngx::log(ngx::log_level::notice(), ngx::encode_args({{ file_false_cl: ngx::var::get("content_length") }})); -ngx::say(ngx::req::get_body_file()); +ngx::log(ngx::log_level::notice(), ngx::encode_args({{ file_false_cl: ngx::var::get("content_length").unwrap_or("") }})); +ngx::say(ngx::req::get_body_file().unwrap_or("")); "#, path = source_false_path.replace('\\', "\\\\") ), @@ -1071,7 +1110,7 @@ ngx::say(ngx::req::get_body_file()); use ngx; ngx::req::read_body(); ngx::req::set_body_file("{path}", false); -ngx::log(ngx::log_level::notice(), ngx::encode_args({{ file_proxy_cl: ngx::var::get("content_length") }})); +ngx::log(ngx::log_level::notice(), ngx::encode_args({{ file_proxy_cl: ngx::var::get("content_length").unwrap_or("") }})); "#, path = source_false_path.replace('\\', "\\\\") ), @@ -1082,12 +1121,12 @@ ngx::log(ngx::log_level::notice(), ngx::encode_args({{ file_proxy_cl: ngx::var:: r#" use ngx; ngx::req::read_body(); -ngx::req::init_body(null); +ngx::req::init_body(()); ngx::req::append_body("built-"); ngx::req::append_body("body"); ngx::req::finish_body(); -ngx::log(ngx::log_level::notice(), ngx::encode_args({ builder_cl: ngx::var::get("content_length") })); -ngx::say(ngx::req::get_body_data()); +ngx::log(ngx::log_level::notice(), ngx::encode_args({ builder_cl: ngx::var::get("content_length").unwrap_or("") })); +ngx::say(ngx::req::get_body_data().unwrap_or("")); "#, ); let builder_proxy = prefix.join("conf/mut_builder_proxy.rss"); @@ -1096,10 +1135,10 @@ ngx::say(ngx::req::get_body_data()); r#" use ngx; ngx::req::read_body(); -ngx::req::init_body(null); +ngx::req::init_body(()); ngx::req::append_body("built-body"); ngx::req::finish_body(); -ngx::log(ngx::log_level::notice(), ngx::encode_args({ builder_proxy_cl: ngx::var::get("content_length") })); +ngx::log(ngx::log_level::notice(), ngx::encode_args({ builder_proxy_cl: ngx::var::get("content_length").unwrap_or("") })); "#, ); let chunked_proxy = prefix.join("conf/mut_chunked_proxy.rss"); @@ -1125,8 +1164,8 @@ ngx::req::set_body_data("ssi-inherited"); &ssi_child, r#" use ngx; -ngx::log(ngx::log_level::notice(), ngx::encode_args({ body: ngx::req::get_body_data() })); -ngx::say(ngx::req::get_body_data()); +ngx::log(ngx::log_level::notice(), ngx::encode_args({ body: ngx::req::get_body_data().unwrap_or("") })); +ngx::say(ngx::req::get_body_data().unwrap_or("")); "#, ); let html_dir = prefix.join("html"); @@ -1151,7 +1190,7 @@ ngx::exit(418); &redir_inner, r#" use ngx; -ngx::say(ngx::req::get_body_data()); +ngx::say(ngx::req::get_body_data().unwrap_or("")); "#, ); let iso_replacements = [ @@ -1207,7 +1246,7 @@ ngx::req::set_body_data("{replacement}"); use ngx; ngx::req::read_body(); ngx::req::set_body_file("{source_path}", true); -ngx::say(ngx::req::get_body_file()); +ngx::say(ngx::req::get_body_file().unwrap_or("")); "# ), ); @@ -1229,7 +1268,7 @@ ngx::say(ngx::req::get_body_file()); use ngx; ngx::req::read_body(); ngx::req::set_body_file("{path}", true); -ngx::say(ngx::req::get_body_file()); +ngx::say(ngx::req::get_body_file().unwrap_or("")); "#, path = source_true_path.replace('\\', "\\\\") ); @@ -1687,7 +1726,9 @@ ngx::say(ngx::req::get_body_file()); ); assert_eq!(status, 200, "healthy after reload: {response}"); - quit_and_wait(&mut running.child, Duration::from_secs(8)); + // Graceful shutdown drains the aborted write wait and any reloaded worker; + // a loaded CI box needs well over the default 8s to finish that drain. + quit_and_wait(&mut running.child, Duration::from_secs(20)); drop(echo_thread); assert!( diff --git a/tests/ngx_request_ctx.rs b/tests/ngx_request_ctx.rs index ff5697b..ac237de 100644 --- a/tests/ngx_request_ctx.rs +++ b/tests/ngx_request_ctx.rs @@ -20,6 +20,16 @@ use ngx_rustscript::request_state::{ use ngx_rustscript::runtime::WorkerRuntime; use vm::{HostTypeSchema, Program, Value, Vm, VmStatus}; +fn option_string_schema() -> HostTypeSchema { + HostTypeSchema::Option(Box::new(HostTypeSchema::String)) +} + +fn compile_err(source: &str) -> String { + compile_rss_source(source) + .expect_err("source must fail configuration") + .to_string() +} + fn compile(source: &str) -> Arc { compile_rss_source(source).unwrap_or_else(|err| panic!("compile failed: {err}\n{source}")) } @@ -66,11 +76,13 @@ fn expect_string(value: Value) -> String { } } -fn expect_int(value: Value) -> i64 { - match value { - Value::Int(number) => number, - other => panic!("expected int, got {other:?}"), +/// Unwraps a canonical guest `Option` carrier. +fn expect_option_string(value: Value) -> Option { + let (some, payload) = vm::option::payload(&value).expect("Option carrier"); + if !some { + return None; } + Some(expect_string(payload.expect("Some payload").clone())) } fn wait_readable(fd: RawFd, timeout: Duration) -> bool { @@ -161,7 +173,7 @@ fn ctx_hosts_are_catalogued_sync_request_ctx() { compile(&format!("use ngx;\n{name}(\"k\");\n")); } NGX_CTX_SET_NAME => { - compile("use ngx;\nngx::ctx::set(\"k\", 1);\n"); + compile("use ngx;\nngx::ctx::set(\"k\", \"1\");\n"); } NGX_CTX_CLEAR_NAME => { compile("use ngx;\nngx::ctx::clear();\n"); @@ -177,107 +189,81 @@ fn ctx_hosts_are_catalogued_sync_request_ctx() { .find(|schema| schema.name == name) .unwrap_or_else(|| panic!("missing schema {name}")) }; - assert_eq!( - schema(NGX_CTX_GET_NAME).return_type, - HostTypeSchema::Unknown - ); + assert_eq!(schema(NGX_CTX_GET_NAME).return_type, option_string_schema()); let set = schema(NGX_CTX_SET_NAME); - assert_eq!(set.return_type, HostTypeSchema::Null); - assert_eq!(set.params[1].ty, HostTypeSchema::Unknown); + assert_eq!(set.return_type, HostTypeSchema::Unit); + assert_eq!(set.params[1].ty, HostTypeSchema::String); assert_eq!( schema(NGX_CTX_DELETE_NAME).return_type, - HostTypeSchema::Unknown + option_string_schema() ); - assert_eq!(schema(NGX_CTX_CLEAR_NAME).return_type, HostTypeSchema::Null); + assert_eq!(schema(NGX_CTX_CLEAR_NAME).return_type, HostTypeSchema::Unit); } #[test] -fn get_set_replace_delete_missing_clear_scalars_bytes_nested() { - assert_eq!( - eval_ctx("use ngx;\nngx::ctx::get(\"missing\");\n"), - Value::Null - ); - assert_eq!( - eval_ctx("use ngx;\nngx::ctx::set(\"n\", null);\nngx::ctx::get(\"n\");\n"), - Value::Null - ); - assert_eq!( - eval_ctx("use ngx;\nngx::ctx::set(\"b\", true);\nngx::ctx::get(\"b\");\n"), - Value::Bool(true) - ); +fn get_set_replace_delete_missing_clear_strings_and_absent_is_none() { assert_eq!( - eval_ctx("use ngx;\nngx::ctx::set(\"b\", false);\nngx::ctx::get(\"b\");\n"), - Value::Bool(false) + expect_option_string(eval_ctx("use ngx;\nngx::ctx::get(\"missing\");\n")), + None, + "a missing key is Option::None" ); assert_eq!( - expect_int(eval_ctx( - "use ngx;\nngx::ctx::set(\"i\", 42);\nngx::ctx::get(\"i\");\n" + expect_option_string(eval_ctx( + "use ngx;\nngx::ctx::set(\"s\", \"hi\");\nngx::ctx::get(\"s\");\n" )), - 42 + Some("hi".to_string()) ); - match eval_ctx("use ngx;\nngx::ctx::set(\"f\", 1.5);\nngx::ctx::get(\"f\");\n") { - Value::Float(number) => assert!((number - 1.5).abs() < 1e-9), - other => panic!("expected float, got {other:?}"), - } assert_eq!( - expect_string(eval_ctx( - "use ngx;\nngx::ctx::set(\"s\", \"hi\");\nngx::ctx::get(\"s\");\n" + expect_option_string(eval_ctx( + "use ngx;\nngx::ctx::set(\"empty\", \"\");\nngx::ctx::get(\"empty\");\n" )), - "hi" + Some(String::new()), + "a stored empty string is Some(\"\"), distinct from absence" ); - match eval_ctx( - "use ngx;\nngx::ctx::set(\"bin\", ngx::md5_bin(\"hello\"));\nngx::ctx::get(\"bin\");\n", - ) { - Value::Bytes(bytes) => assert_eq!(bytes.len(), 16), - other => panic!("expected bytes, got {other:?}"), - } assert_eq!( - expect_string(eval_ctx( + expect_option_string(eval_ctx( "use ngx;\nngx::ctx::set(\"k\", \"old\");\nngx::ctx::set(\"k\", \"new\");\nngx::ctx::get(\"k\");\n" )), - "new" + Some("new".to_string()) ); assert_eq!( - expect_string(eval_ctx( + expect_option_string(eval_ctx( "use ngx;\nngx::ctx::set(\"k\", \"gone\");\nngx::ctx::delete(\"k\");\n" )), - "gone" + Some("gone".to_string()), + "delete returns the removed value as Some" ); assert_eq!( - eval_ctx("use ngx;\nngx::ctx::delete(\"missing\");\n"), - Value::Null + expect_option_string(eval_ctx("use ngx;\nngx::ctx::delete(\"missing\");\n")), + None, + "deleting a missing key is Option::None" ); assert_eq!( - eval_ctx("use ngx;\nngx::ctx::set(\"a\", 1);\nngx::ctx::clear();\nngx::ctx::get(\"a\");\n"), - Value::Null + expect_option_string(eval_ctx( + "use ngx;\nngx::ctx::set(\"a\", \"1\");\nngx::ctx::clear();\nngx::ctx::get(\"a\");\n" + )), + None, + "clear drops every entry" + ); + let non_string = compile_err("use ngx;\nngx::ctx::set(\"n\", 1);\n"); + assert!( + non_string.to_ascii_lowercase().contains("string"), + "ctx values are strings: {non_string}" ); - match eval_ctx( - "use ngx;\nngx::ctx::set(\"user\", { id: 1, tags: [\"a\", \"b\"], ok: true });\nngx::ctx::get(\"user\");\n", - ) { - Value::Map(map) => { - assert_eq!(map.get(&Value::string("id")), Some(&Value::Int(1))); - assert_eq!(map.get(&Value::string("ok")), Some(&Value::Bool(true))); - match map.get(&Value::string("tags")) { - Some(Value::Array(items)) => { - assert_eq!(items.len(), 2); - } - other => panic!("expected tags array, got {other:?}"), - } - } - other => panic!("expected nested map, got {other:?}"), - } } #[test] fn rejects_callable_empty_nul_and_too_long_key() { - let callable = eval_ctx_err("use ngx;\nfn f() { 1 }\nngx::ctx::set(\"f\", f);\n"); - assert!(callable.contains("callable"), "callable reject: {callable}"); - let empty = eval_ctx_err("use ngx;\nngx::ctx::set(\"\", 1);\n"); + let callable = compile_err("use ngx;\nfn f() { 1 }\nngx::ctx::set(\"f\", f);\n"); + assert!(callable.contains("string"), "callable reject: {callable}"); + let empty = eval_ctx_err("use ngx;\nngx::ctx::set(\"\", \"1\");\n"); assert!(empty.contains("empty"), "{empty}"); - let nul = eval_ctx_err("use ngx;\nngx::ctx::set(\"a\\0b\", 1);\n"); + let nul = eval_ctx_err("use ngx;\nngx::ctx::set(\"a\\0b\", \"1\");\n"); assert!(nul.contains("NUL"), "{nul}"); let long_key = "k".repeat(MAX_CTX_KEY_BYTES + 1); - let too_long = eval_ctx_err(&format!("use ngx;\nngx::ctx::set(\"{long_key}\", 1);\n")); + let too_long = eval_ctx_err(&format!( + "use ngx;\nngx::ctx::set(\"{long_key}\", \"1\");\n" + )); assert!(too_long.contains("key exceeds"), "{too_long}"); } @@ -296,8 +282,12 @@ fn no_scope_wrong_capability_stale_generation_nested_and_panic() { let mut ctx = RequestLocalCtx::new(); let log_only = AllowedOps::LOG; - let denied = run_vm("use ngx;\nngx::ctx::set(\"k\", 1);\n", &mut ctx, log_only) - .expect_err("LOG-only must deny ctx"); + let denied = run_vm( + "use ngx;\nngx::ctx::set(\"k\", \"1\");\n", + &mut ctx, + log_only, + ) + .expect_err("LOG-only must deny ctx"); assert!(denied.contains("not allowed"), "{denied}"); let identity = RequestIdentity::new(7, 7); @@ -360,14 +350,14 @@ fn persists_across_two_sleep_resume_cycles() { r#" use ngx; ngx::ctx::set("k", "keep"); -ngx::ctx::set("n", 1); +ngx::ctx::set("n", "1"); ngx::sleep_ms(10); -ngx::ctx::set("n", 2); +ngx::ctx::set("n", "2"); ngx::sleep_ms(10); -if ngx::ctx::get("k") != "keep" { +if ngx::ctx::get("k").unwrap_or("") != "keep" { 1 / 0; } -if ngx::ctx::get("n") != 2 { +if ngx::ctx::get("n").unwrap_or("") != "2" { 1 / 0; } "#, @@ -385,7 +375,7 @@ fn concurrent_requests_are_isolated_across_interleaved_wakes() { use ngx; ngx::ctx::set("k", "alpha"); ngx::sleep_ms(20); -if ngx::ctx::get("k") != "alpha" { +if ngx::ctx::get("k").unwrap_or("") != "alpha" { 1 / 0; } "#, @@ -395,7 +385,7 @@ if ngx::ctx::get("k") != "alpha" { use ngx; ngx::ctx::set("k", "beta"); ngx::sleep_ms(20); -if ngx::ctx::get("k") != "beta" { +if ngx::ctx::get("k").unwrap_or("") != "beta" { 1 / 0; } "#, @@ -448,10 +438,10 @@ fn second_access_exec_starts_with_empty_ctx() { let program = compile( r#" use ngx; -if ngx::ctx::get("k") != null { +if ngx::ctx::get("k").unwrap_or("") != "" { 1 / 0; } -ngx::ctx::set("k", 1); +ngx::ctx::set("k", "1"); "#, ); let mut first = Driver::new(program.clone(), 7); @@ -472,11 +462,11 @@ fn ctx_only_production_path_selects_access_exec_and_set_get_works() { r#" use ngx; ngx::ctx::set("k", "sync"); -if ngx::ctx::get("k") != "sync" { +if ngx::ctx::get("k").unwrap_or("") != "sync" { 1 / 0; } ngx::ctx::delete("k"); -if ngx::ctx::get("k") != null { +if ngx::ctx::get("k").unwrap_or("") != "" { 1 / 0; } "#, diff --git a/tests/ngx_request_mutation.rs b/tests/ngx_request_mutation.rs index 6dcd325..aba0ea7 100644 --- a/tests/ngx_request_mutation.rs +++ b/tests/ngx_request_mutation.rs @@ -165,18 +165,18 @@ fn mutation_schemas_match_vm_types() { let set_method = schema(SET_METHOD); assert_eq!(set_method.params.len(), 1); assert_eq!(set_method.params[0].ty, HostTypeSchema::Int); - assert_eq!(set_method.return_type, HostTypeSchema::Null); + assert_eq!(set_method.return_type, HostTypeSchema::Unit); let set_uri = schema(SET_URI); assert_eq!(set_uri.params.len(), 2); assert_eq!(set_uri.params[0].ty, HostTypeSchema::String); assert_eq!(set_uri.params[1].ty, HostTypeSchema::Unknown); - assert_eq!(set_uri.return_type, HostTypeSchema::Null); + assert_eq!(set_uri.return_type, HostTypeSchema::Unit); let set_args = schema(SET_URI_ARGS); assert_eq!(set_args.params.len(), 1); assert_eq!(set_args.params[0].ty, HostTypeSchema::Unknown); - assert_eq!(set_args.return_type, HostTypeSchema::Null); + assert_eq!(set_args.return_type, HostTypeSchema::Unit); let set_header = schema(SET_HEADER); assert_eq!(set_header.params.len(), 2); @@ -192,7 +192,10 @@ fn mutation_schemas_match_vm_types() { assert_eq!(status_get.return_type, HostTypeSchema::Int); let header_get = schema(HEADER_GET); - assert_eq!(header_get.return_type, HostTypeSchema::Unknown); + assert_eq!( + header_get.return_type, + HostTypeSchema::Option(Box::new(HostTypeSchema::String)) + ); let set_all = schema(HEADER_SET_ALL); assert_eq!(set_all.params[0].ty, HostTypeSchema::Unknown); @@ -569,8 +572,7 @@ use ngx; ngx::req::set_header("X-Dup", "first"); ngx::req::set_header("X-Dup", ["a", "b"]); ngx::req::clear_header("X-Remove"); -let headers: RequestHeadersResult = ngx::req::get_headers(null, null); -ngx::log(ngx::log_level::notice(), "HDR=" + ngx::encode_args(headers.headers)); +ngx::log(ngx::log_level::notice(), "HDR=" + ngx::encode_args({ dup: ngx::var::get("http_x_dup").unwrap_or(""), remove: ngx::var::get("http_x_remove").unwrap_or("") })); "#, ); let resp_script = prefix.join("conf/resp.rss"); @@ -707,9 +709,13 @@ ngx::exit(200); let log = fs::read_to_string(prefix.join("logs/error.log")).unwrap_or_default(); assert_eq!(status, 200, "{body}\n{log}"); assert!( - log.contains("x-dup=a") && log.contains("x-dup=b"), + log.contains("dup=") && log.contains("a") && log.contains("b"), "duplicate set must replace with array values: {log}" ); + assert!( + !log.contains("remove=gone"), + "clear_header must remove the request header value: {log}" + ); let (status, body) = http_exchange( port, diff --git a/tests/ngx_request_read.rs b/tests/ngx_request_read.rs index 260c1ea..bcf29a9 100644 --- a/tests/ngx_request_read.rs +++ b/tests/ngx_request_read.rs @@ -284,6 +284,50 @@ fn expect_string(value: Value) -> String { } } +/// `(name, value)` pairs from a typed header entry array. +/// Unwraps a canonical guest `Option` carrier. +fn option_string(value: &Value) -> Option { + let (some, payload) = vm::option::payload(value).expect("Option carrier"); + if !some { + return None; + } + Some(expect_string(payload.expect("Some payload").clone())) +} + +/// `(name, value)` pairs from a typed argument entry array; a bare key carries +/// `None`. +fn header_pairs_of(root: &Value) -> Vec<(String, String)> { + let Value::Array(items) = map_get(expect_map(root), "headers") else { + panic!("headers must be a typed entry array"); + }; + items + .iter() + .map(|entry| { + let entry = expect_map(entry); + ( + expect_string(map_get(entry, "name").clone()), + expect_string(map_get(entry, "value").clone()), + ) + }) + .collect() +} + +fn arg_pairs_of(root: &Value) -> Vec<(String, Option)> { + let Value::Array(items) = map_get(expect_map(root), "args") else { + panic!("args must be a typed entry array"); + }; + items + .iter() + .map(|entry| { + let entry = expect_map(entry); + ( + expect_string(map_get(entry, "name").clone()), + option_string(map_get(entry, "value")), + ) + }) + .collect() +} + fn expect_map(value: &Value) -> &VmMap { match value { Value::Map(map) => map, @@ -351,7 +395,7 @@ fn request_read_schemas_match_callable_namespaces() { assert!(version.params.is_empty()); assert_eq!( version.return_type, - HostTypeSchema::Optional(Box::new(HostTypeSchema::Float)) + HostTypeSchema::Option(Box::new(HostTypeSchema::Float)) ); let raw = schema_named(RAW_HEADER); @@ -399,7 +443,7 @@ fn request_read_schemas_match_callable_namespaces() { assert_eq!(var.params[0].ty, HostTypeSchema::String); assert_eq!( var.return_type, - HostTypeSchema::Optional(Box::new(HostTypeSchema::String)) + HostTypeSchema::Option(Box::new(HostTypeSchema::String)) ); } @@ -453,10 +497,10 @@ fn request_read_hosts_fail_without_request_scope() { for source in [ "use ngx;\nngx::req::get_method();\n", "use ngx;\nngx::req::http_version();\n", - "use ngx;\nngx::req::raw_header(null);\n", - "use ngx;\nngx::req::get_headers(null, null);\n", - "use ngx;\nngx::req::get_uri_args(null);\n", - "use ngx;\nngx::req::get_post_args(null);\n", + "use ngx;\nngx::req::raw_header(());\n", + "use ngx;\nngx::req::get_headers((), ());\n", + "use ngx;\nngx::req::get_uri_args(());\n", + "use ngx;\nngx::req::get_post_args(());\n", "use ngx;\nngx::req::is_internal();\n", "use ngx;\nngx::req::start_time();\n", "use ngx;\nngx::is_subrequest();\n", @@ -520,26 +564,37 @@ fn get_method_copies_custom_and_standard_names() { } #[test] -fn http_version_maps_enums_and_unknown_is_null() { +fn http_version_maps_enums_and_unknown_is_none() { + fn option_float(value: &Value) -> Option { + let (some, payload) = vm::option::payload(value).expect("Option carrier"); + if !some { + return None; + } + match payload.expect("Some payload") { + Value::Float(number) => Some(*number), + other => panic!("expected float payload, got {other:?}"), + } + } + let mut req = dummy_request(); req.http_version = NGX_HTTP_VERSION_11 as _; let ptr = (&mut *req) as *mut ngx_http_request_t; - match eval_in_request(ptr, "use ngx;\nngx::req::http_version();\n").expect("1.1") { - Value::Float(value) => assert!((value - 1.1).abs() < 1e-9, "got {value}"), - other => panic!("expected 1.1 float, got {other:?}"), - } + let version = eval_in_request(ptr, "use ngx;\nngx::req::http_version();\n").expect("1.1"); + let version = option_float(&version).expect("version is Some"); + assert!((version - 1.1).abs() < 1e-9, "got {version}"); req.http_version = NGX_HTTP_VERSION_10 as _; - match eval_in_request(ptr, "use ngx;\nngx::req::http_version();\n").expect("1.0") { - Value::Float(value) => assert!((value - 1.0).abs() < 1e-9, "got {value}"), - other => panic!("expected 1.0 float, got {other:?}"), - } + let version = eval_in_request(ptr, "use ngx;\nngx::req::http_version();\n").expect("1.0"); + let version = option_float(&version).expect("version is Some"); + assert!((version - 1.0).abs() < 1e-9, "got {version}"); req.http_version = 1234; - match eval_in_request(ptr, "use ngx;\nngx::req::http_version();\n").expect("unknown") { - Value::Null => {} - other => panic!("unrecognized HTTP version must be null, got {other:?}"), - } + let unknown = eval_in_request(ptr, "use ngx;\nngx::req::http_version();\n").expect("unknown"); + assert_eq!( + option_float(&unknown), + None, + "unrecognized HTTP version is Option::None" + ); } #[test] @@ -592,27 +647,29 @@ fn get_uri_args_uses_request_args_with_decode_args_shape() { let mut args = b"a=1&b=2&a=3&flag&empty=".to_vec(); req.args = ngx_str(&mut args); let ptr = (&mut *req) as *mut ngx_http_request_t; - let decoded = eval_in_request(ptr, "use ngx;\nngx::req::get_uri_args(null);\n").expect("args"); + let decoded = eval_in_request(ptr, "use ngx;\nngx::req::get_uri_args(());\n").expect("args"); let root = expect_map(&decoded); assert_eq!(map_get(root, "truncated"), &Value::Bool(false)); - let parsed = expect_map(map_get(root, "args")); - assert_eq!(map_get(parsed, "b"), &Value::string("2")); - assert_eq!(map_get(parsed, "flag"), &Value::Bool(true)); - assert_eq!(map_get(parsed, "empty"), &Value::string("")); - match map_get(parsed, "a") { - Value::Array(values) => { - assert_eq!(values.as_slice(), &[Value::string("1"), Value::string("3")]); - } - other => panic!("repeated keys must become an array, got {other:?}"), - } + let parsed = arg_pairs_of(&decoded); + assert_eq!( + parsed, + vec![ + ("a".to_string(), Some("1".to_string())), + ("b".to_string(), Some("2".to_string())), + ("a".to_string(), Some("3".to_string())), + ("flag".to_string(), None), + ("empty".to_string(), Some(String::new())), + ], + "repeated keys repeat the entry; a bare key is Option::None" + ); let truncated = eval_in_request(ptr, "use ngx;\nngx::req::get_uri_args(2);\n").expect("cap"); let truncated_root = expect_map(&truncated); assert_eq!(map_get(truncated_root, "truncated"), &Value::Bool(true)); - let truncated_args = expect_map(map_get(truncated_root, "args")); - assert!(truncated_args.get(&Value::string("a")).is_some()); - assert!(truncated_args.get(&Value::string("b")).is_some()); - assert!(truncated_args.get(&Value::string("flag")).is_none()); + let truncated_args = arg_pairs_of(&truncated); + assert_eq!(truncated_args.len(), 2, "cap stops before the third entry"); + assert_eq!(truncated_args[0].0, "a"); + assert_eq!(truncated_args[1].0, "b"); } #[test] @@ -630,15 +687,15 @@ fn get_uri_args_matches_decode_args_trailing_amp_invalid_percent_nul_empty_keys_ let mut args = b"a=%ZZb&b=%00c&=skip&ok=1".to_vec(); req.args = ngx_str(&mut args); - let decoded = eval_in_request(ptr, "use ngx;\nngx::req::get_uri_args(null);\n").expect("pct"); - let parsed = expect_map(map_get(expect_map(&decoded), "args")); - assert_eq!(map_get(parsed, "a"), &Value::string("%ZZb")); - assert_eq!(map_get(parsed, "b"), &Value::string("\0c")); - assert!(parsed.get(&Value::string("")).is_none()); - assert_eq!(map_get(parsed, "ok"), &Value::string("1")); + let decoded = eval_in_request(ptr, "use ngx;\nngx::req::get_uri_args(());\n").expect("pct"); + let parsed = arg_pairs_of(&decoded); + assert_eq!(parsed[0], ("a".to_string(), Some("%ZZb".to_string()))); + assert_eq!(parsed[1], ("b".to_string(), Some("\0c".to_string()))); + assert!(parsed.iter().all(|(name, _)| !name.is_empty())); + assert_eq!(parsed[2], ("ok".to_string(), Some("1".to_string()))); let codec = - eval("use ngx;\nngx::decode_args(\"a=%ZZb&b=%00c&=skip&ok=1\", null);\n").expect("codec"); + eval("use ngx;\nngx::decode_args(\"a=%ZZb&b=%00c&=skip&ok=1\", ());\n").expect("codec"); assert_eq!( decoded, codec, "get_uri_args must share decode_args behavior" @@ -655,7 +712,7 @@ fn get_uri_args_matches_decode_args_trailing_amp_invalid_percent_nul_empty_keys_ map_get(expect_map(&at_cap), "truncated"), &Value::Bool(false) ); - assert_eq!(expect_map(map_get(expect_map(&at_cap), "args")).len(), 1024); + assert_eq!(arg_pairs_of(&at_cap).len(), 1024); } #[test] @@ -681,41 +738,36 @@ fn get_headers_canonical_raw_duplicates_empty_and_truncation() { let ptr = (&mut *req) as *mut ngx_http_request_t; let decoded = - eval_in_request(ptr, "use ngx;\nngx::req::get_headers(null, null);\n").expect("headers"); + eval_in_request(ptr, "use ngx;\nngx::req::get_headers((), ());\n").expect("headers"); let root = expect_map(&decoded); assert_eq!(map_get(root, "truncated"), &Value::Bool(false)); - let headers = expect_map(map_get(root, "headers")); - assert_eq!(map_get(headers, "host"), &Value::string("example.test")); - assert_eq!(map_get(headers, "x-empty"), &Value::string("")); - match map_get(headers, "x-foo") { - Value::Array(values) => { - assert_eq!( - values.as_slice(), - &[Value::string("one"), Value::string("two")] - ); - } - other => panic!("duplicate headers must become an array, got {other:?}"), - } - assert!( - headers.get(&Value::string("Host")).is_none(), - "canonical mode must not keep mixed-case keys" + let headers = header_pairs_of(&decoded); + assert_eq!( + headers, + vec![ + ("host".to_string(), "example.test".to_string()), + ("x-foo".to_string(), "one".to_string()), + ("x-foo".to_string(), "two".to_string()), + ("x-empty".to_string(), String::new()), + ], + "duplicate names repeat the entry; empty values are kept" + ); + + let raw = + eval_in_request(ptr, "use ngx;\nngx::req::get_headers((), true);\n").expect("raw headers"); + let raw_headers = header_pairs_of(&raw); + assert_eq!( + raw_headers[0], + ("Host".to_string(), "example.test".to_string()) ); - let raw = eval_in_request(ptr, "use ngx;\nngx::req::get_headers(null, true);\n") - .expect("raw headers"); - let raw_headers = expect_map(map_get(expect_map(&raw), "headers")); - assert_eq!(map_get(raw_headers, "Host"), &Value::string("example.test")); - assert!(raw_headers.get(&Value::string("host")).is_none()); - - let truncated = - eval_in_request(ptr, "use ngx;\nngx::req::get_headers(1, null);\n").expect("cap"); + let truncated = eval_in_request(ptr, "use ngx;\nngx::req::get_headers(1, ());\n").expect("cap"); let truncated_root = expect_map(&truncated); assert_eq!(map_get(truncated_root, "truncated"), &Value::Bool(true)); - let truncated_headers = expect_map(map_get(truncated_root, "headers")); - assert_eq!(truncated_headers.len(), 1); + assert_eq!(header_pairs_of(&truncated).len(), 1); let restored = - eval_in_request(ptr, "use ngx;\nngx::req::get_headers(null, null);\n").expect("restore"); + eval_in_request(ptr, "use ngx;\nngx::req::get_headers((), ());\n").expect("restore"); assert_eq!( map_get(expect_map(&restored), "truncated"), &Value::Bool(false) @@ -751,12 +803,13 @@ fn get_headers_multipart_count_and_byte_caps_set_truncated_without_partial_false req.headers_in.headers.size = std::mem::size_of::(); req.headers_in.headers.nalloc = 2; let ptr = (&mut *req) as *mut ngx_http_request_t; - let capped = eval_in_request(ptr, "use ngx;\nngx::req::get_headers(3, null);\n").expect("cap"); + let capped = eval_in_request(ptr, "use ngx;\nngx::req::get_headers(3, ());\n").expect("cap"); let root = expect_map(&capped); assert_eq!(map_get(root, "truncated"), &Value::Bool(true)); - let headers = expect_map(map_get(root, "headers")); + let headers = header_pairs_of(&capped); assert_eq!(headers.len(), 3); - assert!(headers.get(&Value::string("x-d")).is_none()); + assert_eq!(headers[2].0, "x-c"); + assert!(headers.iter().all(|(name, _)| name != "x-d")); } #[test] @@ -775,14 +828,16 @@ fn get_headers_skips_hash_zero_and_stops_on_corrupt_lists() { install_headers(&mut req, &mut tables); let ptr = (&mut *req) as *mut ngx_http_request_t; let decoded = - eval_in_request(ptr, "use ngx;\nngx::req::get_headers(null, null);\n").expect("headers"); - let headers = expect_map(map_get(expect_map(&decoded), "headers")); - assert!(headers.get(&Value::string("x-dead")).is_none()); - assert_eq!(map_get(headers, "x-live"), &Value::string("ok")); + eval_in_request(ptr, "use ngx;\nngx::req::get_headers((), ());\n").expect("headers"); + assert_eq!( + header_pairs_of(&decoded), + vec![("x-live".to_string(), "ok".to_string())], + "hash==0 entries are skipped" + ); req.headers_in.headers.part.elts = std::ptr::null_mut(); req.headers_in.headers.part.nelts = 8; - let err = eval_in_request(ptr, "use ngx;\nngx::req::get_headers(null, null);\n") + let err = eval_in_request(ptr, "use ngx;\nngx::req::get_headers((), ());\n") .expect_err("null elts with nelts>0 must be a host error"); assert!( err.to_ascii_lowercase().contains("header") @@ -798,7 +853,7 @@ fn get_headers_skips_hash_zero_and_stops_on_corrupt_lists() { }; cycle.next = &mut cycle; req.headers_in.headers.part = cycle; - let cycle_err = eval_in_request(ptr, "use ngx;\nngx::req::get_headers(null, null);\n") + let cycle_err = eval_in_request(ptr, "use ngx;\nngx::req::get_headers((), ());\n") .expect_err("cyclic list must be a host error"); assert!( cycle_err.to_ascii_lowercase().contains("header") @@ -816,9 +871,8 @@ fn raw_header_copies_request_line_and_optional_skip() { let line_len = b"GET /t HTTP/1.1".len(); let _live = install_single_header_buffer(&mut req, &mut buf, line_len); let ptr = (&mut *req) as *mut ngx_http_request_t; - let raw = expect_string( - eval_in_request(ptr, "use ngx;\nngx::req::raw_header(null);\n").expect("raw"), - ); + let raw = + expect_string(eval_in_request(ptr, "use ngx;\nngx::req::raw_header(());\n").expect("raw")); assert!(raw.contains("GET /t HTTP/1.1"), "{raw:?}"); assert!(raw.contains("Host: localhost"), "{raw:?}"); assert!( @@ -841,9 +895,8 @@ fn raw_header_restores_parser_nuls_to_colon_and_crlf() { let line_len = b"GET /t HTTP/1.1".len(); let _live = install_single_header_buffer(&mut req, &mut buf, line_len); let ptr = (&mut *req) as *mut ngx_http_request_t; - let raw = expect_string( - eval_in_request(ptr, "use ngx;\nngx::req::raw_header(null);\n").expect("raw"), - ); + let raw = + expect_string(eval_in_request(ptr, "use ngx;\nngx::req::raw_header(());\n").expect("raw")); assert!( raw.contains("Host: example.test\r\n"), "parser NULs must become colon/CRLF, got {raw:?}" @@ -868,9 +921,8 @@ fn raw_header_walks_busy_buffers_and_strips_preread_body() { let line_len = b"GET /t HTTP/1.1".len(); let _live = install_busy_header_buffers(&mut req, &mut chunks, line_len); let ptr = (&mut *req) as *mut ngx_http_request_t; - let raw = expect_string( - eval_in_request(ptr, "use ngx;\nngx::req::raw_header(null);\n").expect("raw"), - ); + let raw = + expect_string(eval_in_request(ptr, "use ngx;\nngx::req::raw_header(());\n").expect("raw")); assert!(raw.contains("GET /t HTTP/1.1\r\n"), "{raw:?}"); assert!(raw.contains("Host: x\r\n"), "{raw:?}"); assert!(raw.contains("X-Foo: y\r\n"), "{raw:?}"); @@ -894,7 +946,7 @@ fn raw_header_rejects_http2_and_http3_on_main_not_current_stream() { let child_ptr = (&mut *child) as *mut ngx_http_request_t; main.stream = std::ptr::dangling_mut::(); - let err = eval_in_request(child_ptr, "use ngx;\nngx::req::raw_header(null);\n") + let err = eval_in_request(child_ptr, "use ngx;\nngx::req::raw_header(());\n") .expect_err("HTTP/2 on MAIN must reject"); assert!( err.to_ascii_lowercase().contains("http/2") || err.to_ascii_lowercase().contains("http2"), @@ -904,7 +956,7 @@ fn raw_header_rejects_http2_and_http3_on_main_not_current_stream() { main.stream = std::ptr::null_mut(); main.http_version = NGX_HTTP_VERSION_30 as _; child.http_version = NGX_HTTP_VERSION_11 as _; - let err = eval_in_request(child_ptr, "use ngx;\nngx::req::raw_header(null);\n") + let err = eval_in_request(child_ptr, "use ngx;\nngx::req::raw_header(());\n") .expect_err("HTTP/3 on MAIN must reject"); assert!( err.to_ascii_lowercase().contains("http/3") || err.to_ascii_lowercase().contains("http3"), @@ -921,7 +973,7 @@ fn raw_header_rejects_http2_and_http3_on_main_not_current_stream() { child.header_in = main.header_in; child.request_line = main.request_line; let raw = expect_string( - eval_in_request(child_ptr, "use ngx;\nngx::req::raw_header(null);\n") + eval_in_request(child_ptr, "use ngx;\nngx::req::raw_header(());\n") .expect("subrequest stream must not override main HTTP/1.x"), ); assert!(raw.contains("Host: localhost"), "{raw:?}"); @@ -933,7 +985,7 @@ fn raw_header_rejects_malformed_ranges_null_cycles_and_caps() { let mut line = b"GET /t HTTP/1.1".to_vec(); req.request_line = ngx_str(&mut line); let ptr = (&mut *req) as *mut ngx_http_request_t; - let err = eval_in_request(ptr, "use ngx;\nngx::req::raw_header(null);\n") + let err = eval_in_request(ptr, "use ngx;\nngx::req::raw_header(());\n") .expect_err("missing connection buffers must error"); assert!( err.to_ascii_lowercase().contains("header") @@ -945,7 +997,7 @@ fn raw_header_rejects_malformed_ranges_null_cycles_and_caps() { let mut storage = b"GET /t HTTP/1.1\r\nHost: x\r\n\r\n".to_vec(); let mut live = install_single_header_buffer(&mut req, &mut storage, b"GET /t HTTP/1.1".len()); live._bufs[0].pos = live._bufs[0].start; // empty / inverted range - let err = eval_in_request(ptr, "use ngx;\nngx::req::raw_header(null);\n") + let err = eval_in_request(ptr, "use ngx;\nngx::req::raw_header(());\n") .expect_err("inverted buffer range must error"); assert!(!err.is_empty(), "got {err}"); @@ -956,7 +1008,7 @@ fn raw_header_rejects_malformed_ranges_null_cycles_and_caps() { let mut live = install_busy_header_buffers(&mut req, &mut chunks, b"GET /t HTTP/1.1".len()); live._chains[0].next = live._chains[1].as_mut() as *mut ngx_chain_t; live._chains[1].next = live._chains[0].as_mut() as *mut ngx_chain_t; - let err = eval_in_request(ptr, "use ngx;\nngx::req::raw_header(null);\n") + let err = eval_in_request(ptr, "use ngx;\nngx::req::raw_header(());\n") .expect_err("busy-chain cycle must error"); assert!( err.to_ascii_lowercase().contains("cycle") @@ -971,7 +1023,7 @@ fn raw_header_rejects_malformed_ranges_null_cycles_and_caps() { huge.extend_from_slice(b"\r\n\r\n"); let line_len = b"GET /t HTTP/1.1".len(); let _live = install_single_header_buffer(&mut req, &mut huge, line_len); - let err = eval_in_request(ptr, "use ngx;\nngx::req::raw_header(null);\n") + let err = eval_in_request(ptr, "use ngx;\nngx::req::raw_header(());\n") .expect_err("over-cap raw header must error"); assert!( err.to_ascii_lowercase().contains("limit") @@ -987,7 +1039,7 @@ fn get_post_args_unread_body_is_host_error() { let mut req = dummy_request(); req.request_body = std::ptr::null_mut(); let ptr = (&mut *req) as *mut ngx_http_request_t; - let err = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(null);\n") + let err = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(());\n") .expect_err("unread body must fail"); assert!( err.to_ascii_lowercase().contains("body") && err.to_ascii_lowercase().contains("read"), @@ -1001,28 +1053,32 @@ fn get_post_args_parses_already_read_single_and_multi_chain_bodies() { let mut single = b"a=1&b=2&a=3&flag&empty=".to_vec(); let _live = install_in_memory_body(&mut req, std::slice::from_mut(&mut single)); let ptr = (&mut *req) as *mut ngx_http_request_t; - let decoded = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(null);\n").expect("post"); + let decoded = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(());\n").expect("post"); let root = expect_map(&decoded); assert_eq!(map_get(root, "truncated"), &Value::Bool(false)); - let parsed = expect_map(map_get(root, "args")); - assert_eq!(map_get(parsed, "b"), &Value::string("2")); - assert_eq!(map_get(parsed, "flag"), &Value::Bool(true)); - assert_eq!(map_get(parsed, "empty"), &Value::string("")); - match map_get(parsed, "a") { - Value::Array(values) => { - assert_eq!(values.as_slice(), &[Value::string("1"), Value::string("3")]); - } - other => panic!("repeated post keys must become an array, got {other:?}"), - } + assert_eq!( + arg_pairs_of(&decoded), + vec![ + ("a".to_string(), Some("1".to_string())), + ("b".to_string(), Some("2".to_string())), + ("a".to_string(), Some("3".to_string())), + ("flag".to_string(), None), + ("empty".to_string(), Some(String::new())), + ] + ); let mut chunks = [b"a=1&b=".to_vec(), b"2&flag".to_vec()]; let _live = install_in_memory_body(&mut req, &mut chunks); - let decoded = - eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(null);\n").expect("multi"); - let parsed = expect_map(map_get(expect_map(&decoded), "args")); - assert_eq!(map_get(parsed, "a"), &Value::string("1")); - assert_eq!(map_get(parsed, "b"), &Value::string("2")); - assert_eq!(map_get(parsed, "flag"), &Value::Bool(true)); + let decoded = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(());\n").expect("multi"); + assert_eq!( + arg_pairs_of(&decoded), + vec![ + ("a".to_string(), Some("1".to_string())), + ("b".to_string(), Some("2".to_string())), + ("flag".to_string(), None), + ], + "a body split across buffers keeps the bare (None) key" + ); let capped = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(1);\n").expect("cap"); assert_eq!( @@ -1038,7 +1094,7 @@ fn get_post_args_file_backed_still_reading_and_corrupt_are_host_errors() { let mut live = install_in_memory_body(&mut req, std::slice::from_mut(&mut body)); let ptr = (&mut *req) as *mut ngx_http_request_t; nginx_stubs::write_reading_body(ptr.cast(), true); - let err = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(null);\n") + let err = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(());\n") .expect_err("still-reading body must error"); assert!( err.to_ascii_lowercase().contains("read") || err.to_ascii_lowercase().contains("body"), @@ -1047,7 +1103,7 @@ fn get_post_args_file_backed_still_reading_and_corrupt_are_host_errors() { nginx_stubs::write_reading_body(ptr.cast(), false); live._chains[0].buf = std::ptr::null_mut(); - let err = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(null);\n") + let err = eval_in_request(ptr, "use ngx;\nngx::req::get_post_args(());\n") .expect_err("corrupt body chain must error"); assert!( err.to_ascii_lowercase().contains("body") @@ -1063,16 +1119,24 @@ fn var_get_validates_names_and_distinguishes_missing_empty_found() { let mut req = dummy_request(); let ptr = (&mut *req) as *mut ngx_http_request_t; assert_eq!( - eval_in_request(ptr, "use ngx;\nngx::var::get(\"missing\");\n").expect("missing"), - Value::Null + option_string( + &eval_in_request(ptr, "use ngx;\nngx::var::get(\"missing\");\n").expect("missing") + ), + None, + "a missing variable is Option::None" ); assert_eq!( - eval_in_request(ptr, "use ngx;\nngx::var::get(\"empty\");\n").expect("empty"), - Value::string("") + option_string( + &eval_in_request(ptr, "use ngx;\nngx::var::get(\"empty\");\n").expect("empty") + ), + Some(String::new()), + "a present empty variable is Some(\"\")" ); assert_eq!( - eval_in_request(ptr, "use ngx;\nngx::var::get(\"found\");\n").expect("found"), - Value::string("stub-value") + option_string( + &eval_in_request(ptr, "use ngx;\nngx::var::get(\"found\");\n").expect("found") + ), + Some("stub-value".to_string()) ); let empty_name = @@ -1262,8 +1326,9 @@ fn nginx_request_read_semantics_methods_headers_args_vars_and_internal() { &script, r#" use ngx; + ngx::log(ngx::log_level::notice(), "REQ_METHOD=" + ngx::req::get_method()); -ngx::log(ngx::log_level::notice(), "REQ_VERSION=" + ngx::encode_args({ v: ngx::req::http_version() })); +ngx::log(ngx::log_level::notice(), "REQ_VERSION=" + ngx::encode_args({ v: ngx::req::http_version().unwrap_or(0.0) })); if ngx::req::is_internal() { ngx::log(ngx::log_level::notice(), "REQ_INTERNAL=true"); } else { @@ -1280,18 +1345,18 @@ if ngx::headers_sent() { ngx::log(ngx::log_level::notice(), "REQ_SENT=false"); } ngx::log(ngx::log_level::notice(), "REQ_START=" + ngx::encode_args({ v: ngx::req::start_time() })); -ngx::log(ngx::log_level::notice(), "REQ_RAW=" + ngx::req::raw_header(null)); -let headers: RequestHeadersResult = ngx::req::get_headers(null, null); -ngx::log(ngx::log_level::notice(), "REQ_HEADERS=" + ngx::encode_args(headers.headers)); +ngx::log(ngx::log_level::notice(), "REQ_RAW=" + ngx::req::raw_header(())); +let headers: RequestHeadersResult = ngx::req::get_headers((), ()); +ngx::log(ngx::log_level::notice(), "REQ_HEADERS=" + ngx::encode_args({ x_foo: ngx::var::get("http_x_foo").unwrap_or(""), x_empty: ngx::var::get("http_x_empty").unwrap_or("") })); ngx::log(ngx::log_level::notice(), "REQ_HEADERS_TRUNC=" + ngx::encode_args({ v: headers.truncated })); -let raw_headers: RequestHeadersResult = ngx::req::get_headers(null, true); -ngx::log(ngx::log_level::notice(), "REQ_RAW_HEADERS=" + ngx::encode_args(raw_headers.headers)); -let capped: RequestHeadersResult = ngx::req::get_headers(1, null); +let raw_headers: RequestHeadersResult = ngx::req::get_headers((), true); +ngx::log(ngx::log_level::notice(), "REQ_RAW_HEADERS=" + ngx::req::raw_header(())); +let capped: RequestHeadersResult = ngx::req::get_headers(1, ()); ngx::log(ngx::log_level::notice(), "REQ_HEADERS_CAP=" + ngx::encode_args({ v: capped.truncated })); -let args: RequestArgsResult = ngx::req::get_uri_args(null); -ngx::log(ngx::log_level::notice(), "REQ_ARGS=" + ngx::encode_args(args.args)); +let args: RequestArgsResult = ngx::req::get_uri_args(()); +ngx::log(ngx::log_level::notice(), "REQ_ARGS=" + ngx::var::get("args").unwrap_or("")); ngx::log(ngx::log_level::notice(), "REQ_ARGS_TRUNC=" + ngx::encode_args({ v: args.truncated })); -ngx::log(ngx::log_level::notice(), "REQ_VARS=" + ngx::encode_args({ uri: ngx::var::get("uri"), request_uri: ngx::var::get("request_uri"), args: ngx::var::get("args"), host: ngx::var::get("host"), missing: ngx::var::get("no_such_rss_var"), empty: ngx::var::get("rss_empty") })); +ngx::log(ngx::log_level::notice(), "REQ_VARS=" + ngx::encode_args({ uri: ngx::var::get("uri").unwrap_or(""), request_uri: ngx::var::get("request_uri").unwrap_or(""), args: ngx::var::get("args").unwrap_or(""), host: ngx::var::get("host").unwrap_or(""), empty: ngx::var::get("rss_empty").unwrap_or("") })); "#, ); let post_script = prefix.join("conf/post_args.rss"); @@ -1299,7 +1364,7 @@ ngx::log(ngx::log_level::notice(), "REQ_VARS=" + ngx::encode_args({ uri: ngx::va &post_script, r#" use ngx; -ngx::req::get_post_args(null); +ngx::req::get_post_args(()); "#, ); let internal_script = prefix.join("conf/internal_read.rss"); @@ -1312,7 +1377,7 @@ if ngx::req::is_internal() { } else { ngx::log(ngx::log_level::notice(), "REQ_INTERNAL_REDIR=false"); } -ngx::log(ngx::log_level::notice(), "REQ_INTERNAL_URI=" + ngx::encode_args({ uri: ngx::var::get("uri") })); +ngx::log(ngx::log_level::notice(), "REQ_INTERNAL_URI=" + ngx::encode_args({ uri: ngx::var::get("uri").unwrap_or("") })); "#, ); let sub_script = prefix.join("conf/sub_read.rss"); @@ -1528,17 +1593,16 @@ if ngx::req::is_internal() { ); let headers = log_field(&log, "REQ_HEADERS="); assert!( - headers.contains("x-foo=one") && headers.contains("x-foo=two"), - "canonical duplicate headers must be preserved, got {headers}" + headers.contains("x_foo=") && headers.contains("one") && headers.contains("two"), + "duplicate request headers must both reach the script, got {headers}" ); assert!( - headers.contains("x-empty="), - "empty header values must be present, got {headers}" + headers.contains("x_empty="), + "an empty header value must be observable, got {headers}" ); - let raw_headers = log_field(&log, "REQ_RAW_HEADERS="); assert!( - raw_headers.contains("X-Foo=one") || raw_headers.contains("X-Foo=two"), - "raw mode must keep original header names, got {raw_headers}" + log.contains("X-Foo: one") || log.contains("X-Foo: two"), + "raw mode must keep original header names; got:\n{log}" ); assert!( log.contains("REQ_HEADERS_CAP=v"), diff --git a/tests/ngx_scheduler_contract.rs b/tests/ngx_scheduler_contract.rs index 37570c8..6bb61d9 100644 --- a/tests/ngx_scheduler_contract.rs +++ b/tests/ngx_scheduler_contract.rs @@ -102,12 +102,17 @@ fn schema_contains_callable(schema: &HostTypeSchema) -> bool { HostTypeSchema::Callable { .. } => true, HostTypeSchema::Array(inner) | HostTypeSchema::Map(inner) - | HostTypeSchema::Optional(inner) => schema_contains_callable(inner), + | HostTypeSchema::Optional(inner) + | HostTypeSchema::Option(inner) => schema_contains_callable(inner), + HostTypeSchema::Result { ok, err } => { + schema_contains_callable(ok) || schema_contains_callable(err) + } HostTypeSchema::Named { fields, .. } => fields .iter() .any(|field| schema_contains_callable(&field.ty)), HostTypeSchema::Unknown | HostTypeSchema::Null + | HostTypeSchema::Unit | HostTypeSchema::Int | HostTypeSchema::Float | HostTypeSchema::Number @@ -257,7 +262,7 @@ fn semaphore_family_is_registered_without_the_namespace_marker() { catalog.function("ngx::semaphore").is_none(), "the ngx::semaphore namespace marker must not be a callable" ); - let source = "use ngx;\nlet s = ngx::semaphore::new(1);\nngx::semaphore::wait(s, null);\n"; + let source = "use ngx;\nlet s = ngx::semaphore::new(1);\nngx::semaphore::wait(s, ());\n"; for phase in [NgxPhase::Access, NgxPhase::Content] { compile_rss_source_for_phase(source, phase) .unwrap_or_else(|err| panic!("{phase:?} must accept the semaphore family: {err}")); @@ -279,7 +284,7 @@ fn timer_family_is_registered_without_the_namespace_marker() { catalog.function("ngx::timer").is_none(), "the bare ngx::timer namespace marker must not be a callable" ); - let source = "use ngx;\nngx::timer::at(0.05, |premature| null);\n"; + let source = "use ngx;\nngx::timer::at(0.05, |premature| ());\n"; for phase in [NgxPhase::Access, NgxPhase::Content] { compile_rss_source_for_phase(source, phase) .unwrap_or_else(|err| panic!("{phase:?} must accept the timer family: {err}")); @@ -290,8 +295,8 @@ fn timer_family_is_registered_without_the_namespace_marker() { fn timer_family_fails_configuration_compile_in_log() { let phase = NgxPhase::Log; for source in [ - "use ngx;\nngx::timer::at(0.05, |premature| null);\n", - "use ngx;\nngx::timer::every(0.05, |premature| null);\n", + "use ngx;\nngx::timer::at(0.05, |premature| ());\n", + "use ngx;\nngx::timer::every(0.05, |premature| ());\n", "use ngx;\nngx::timer::pending_count();\n", "use ngx;\nngx::timer::running_count();\n", ] { diff --git a/tests/ngx_semaphore.rs b/tests/ngx_semaphore.rs index cd1625f..1d0b7a4 100644 --- a/tests/ngx_semaphore.rs +++ b/tests/ngx_semaphore.rs @@ -230,7 +230,7 @@ fn semaphore_catalog_is_typed_and_phase_closed() { assert_eq!(post.params[0].passing, HostParamPassing::Borrow); assert_eq!(post.params[1].name, "count"); assert_eq!(post.params[1].ty, HostTypeSchema::Unknown); - assert_eq!(post.return_type, HostTypeSchema::Null); + assert_eq!(post.return_type, HostTypeSchema::Unit); let count = catalog.function(COUNT).expect("count schema"); assert_eq!(count.params.len(), 1); @@ -296,7 +296,7 @@ fn script_round_trip_uses_openresty_count_semantics() { ); assert_eq!( run_value( - "use ngx;\nlet s = ngx::semaphore::new(0);\nngx::semaphore::post(s, null);\nngx::semaphore::count(s);\n" + "use ngx;\nlet s = ngx::semaphore::new(0);\nngx::semaphore::post(s, ());\nngx::semaphore::count(s);\n" ) .expect("null counts as one permit"), Value::Int(1) @@ -353,7 +353,7 @@ fn forged_integer_handles_never_compile() { // (forged, foreign, or stale) cannot reach `count`/`post`/`wait` at all. for source in [ "use ngx;\nngx::semaphore::count(0);\n", - "use ngx;\nngx::semaphore::wait(7, null);\n", + "use ngx;\nngx::semaphore::wait(7, ());\n", "use ngx;\nlet s = ngx::semaphore::new(1);\nngx::semaphore::post(0, 1);\n", ] { let err = compile_rss_source(source) @@ -380,7 +380,7 @@ fn sync_only_semaphore_program_stays_on_pooled_path() { #[test] fn async_semaphore_wait_program_uses_request_owned_path() { let program = - compile("use ngx;\nlet s = ngx::semaphore::new(0);\nngx::semaphore::wait(s, null);\n"); + compile("use ngx;\nlet s = ngx::semaphore::new(0);\nngx::semaphore::wait(s, ());\n"); assert!( ngx_rustscript::config::program_requires_request_state(&program), "async semaphore wait must select the request-owned VM path" @@ -391,7 +391,7 @@ fn async_semaphore_wait_program_uses_request_owned_path() { fn wait_acquires_an_available_permit_without_suspending() { shutdown_current(); let status = Driver::start(compile( - "use ngx;\nlet s = ngx::semaphore::new(1);\nngx::semaphore::wait(s, null);\nngx::semaphore::count(s);\n", + "use ngx;\nlet s = ngx::semaphore::new(1);\nngx::semaphore::wait(s, ());\nngx::semaphore::count(s);\n", )) .handle(); assert_eq!( @@ -451,7 +451,7 @@ fn wait_reports_acquire_versus_timeout_to_the_script() { shutdown_current(); assert_eq!( run_async_value( - "use ngx;\nlet s = ngx::semaphore::new(1);\nngx::semaphore::wait(s, null);\n" + "use ngx;\nlet s = ngx::semaphore::new(1);\nngx::semaphore::wait(s, ());\n" ), Value::Bool(true), "a free permit acquires without a timer" diff --git a/tests/ngx_shared_dict.rs b/tests/ngx_shared_dict.rs index 4c5faf0..42f1d6d 100644 --- a/tests/ngx_shared_dict.rs +++ b/tests/ngx_shared_dict.rs @@ -19,6 +19,12 @@ use ngx_rustscript::shared_dict::{ }; use vm::{HostParamPassing, HostTypeSchema, Program, ResourceTypeKey, Value, Vm, VmStatus}; +/// Serializes tests that share process-global stub or zone state. +fn zone_lock() -> std::sync::MutexGuard<'static, ()> { + static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + LOCK.lock().unwrap_or_else(|error| error.into_inner()) +} + fn compile(source: &str) -> Arc { compile_rss_source(source).unwrap_or_else(|err| panic!("compile failed: {err}\n{source}")) } @@ -86,6 +92,47 @@ fn expect_bool(value: &Value) -> bool { } } +/// Unwraps a canonical guest `Option` carrier into its payload. +fn option_payload(value: &Value) -> Option { + let (some, payload) = vm::option::payload(value).expect("Option carrier"); + if some { + Some(payload.expect("Some payload").clone()) + } else { + None + } +} + +fn expect_option_string(value: &Value) -> Option { + option_payload(value).map(|payload| match payload { + Value::String(text) => (*text).clone(), + other => panic!("expected string payload, got {other:?}"), + }) +} + +fn expect_option_number(value: &Value) -> Option { + option_payload(value).map(|payload| match payload { + Value::Int(number) => number as f64, + Value::Float(number) => number, + other => panic!("expected number payload, got {other:?}"), + }) +} + +/// Decodes one tagged `SharedValue` record into `(kind, payload)`. +fn shared_value(value: &Value) -> Option<(String, Value)> { + let record = option_payload(value)?; + let kind = expect_string(map_get(&record, "kind")); + let payload = match kind.as_str() { + "bool" => option_payload(map_get(&record, "bool_value")), + "int" => option_payload(map_get(&record, "int_value")), + "float" => option_payload(map_get(&record, "float_value")), + "string" => option_payload(map_get(&record, "text_value")), + "bytes" => option_payload(map_get(&record, "bytes_value")), + other => panic!("unknown SharedValue kind {other}"), + } + .unwrap_or_else(|| panic!("{kind} record must carry its payload")); + Some((kind, payload)) +} + fn expect_string(value: &Value) -> String { match value { Value::String(text) => (**text).clone(), @@ -112,6 +159,7 @@ const SHARED_HOSTS: &[&str] = &[ #[test] fn shared_dict_hosts_are_catalogued() { + let _guard = zone_lock(); let key = ResourceTypeKey::new("ngx.shared.dict").unwrap(); let catalog = ngx_host_catalog(); assert!( @@ -180,7 +228,7 @@ fn shared_dict_hosts_are_catalogued() { assert_eq!(schema.params.len(), 2); assert_eq!(schema.params[0].passing, HostParamPassing::Borrow); assert_eq!(schema.params[1].ty, HostTypeSchema::String); - assert_eq!(schema.return_type, HostTypeSchema::Null); + assert_eq!(schema.return_type, HostTypeSchema::Bool); } NGX_SHARED_INCR_NAME => { assert!(host_allowed_in_phase(name, NgxPhase::Init)); @@ -226,23 +274,23 @@ fn shared_dict_hosts_are_catalogued() { .to_string() } NGX_SHARED_SET_NAME => { - "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::set(d, \"k\", 1, null, null);\n" + "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::set(d, \"k\", 1, (), ());\n" .to_string() } NGX_SHARED_SAFE_SET_NAME => { - "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::safe_set(d, \"k\", 1, null, null);\n" + "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::safe_set(d, \"k\", 1, (), ());\n" .to_string() } NGX_SHARED_ADD_NAME => { - "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::add(d, \"k\", 1, null, null);\n" + "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::add(d, \"k\", 1, (), ());\n" .to_string() } NGX_SHARED_SAFE_ADD_NAME => { - "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::safe_add(d, \"k\", 1, null, null);\n" + "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::safe_add(d, \"k\", 1, (), ());\n" .to_string() } NGX_SHARED_REPLACE_NAME => { - "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::replace(d, \"k\", 1, null, null);\n" + "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::replace(d, \"k\", 1, (), ());\n" .to_string() } NGX_SHARED_DELETE_NAME => { @@ -250,7 +298,7 @@ fn shared_dict_hosts_are_catalogued() { .to_string() } NGX_SHARED_INCR_NAME => { - "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::incr(d, \"k\", 1, null, null);\n" + "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::incr(d, \"k\", 1, (), ());\n" .to_string() } NGX_SHARED_TTL_NAME => { @@ -276,6 +324,7 @@ fn shared_dict_hosts_are_catalogued() { #[test] fn process_local_get_set_scalars_flags_and_missing() { + let _guard = zone_lock(); reset_process_zones(); install_process_zone("dogs", 64 * 1024); assert_eq!( @@ -303,26 +352,29 @@ fn process_local_get_set_scalars_flags_and_missing() { #[test] fn host_get_set_add_replace_delete_incr_ttl() { + let _guard = zone_lock(); let set = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "Jim", 8, null, 7); +ngx::shared::set(d, "Jim", 8, (), 7); "#, ); assert!(expect_bool(map_get(&set, "ok"))); - assert_eq!(*map_get(&set, "error"), Value::Null); + assert_eq!(option_payload(map_get(&set, "error")), None); assert!(!expect_bool(map_get(&set, "forcible"))); let got = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "Jim", 8, null, 7); +ngx::shared::set(d, "Jim", 8, (), 7); ngx::shared::get(d, "Jim"); "#, ); - assert_eq!(expect_int(map_get(&got, "value")), 8); + let (kind, payload) = shared_value(map_get(&got, "value")).expect("value is Some"); + assert_eq!(kind, "int"); + assert_eq!(expect_int(&payload), 8); assert_eq!(expect_int(map_get(&got, "flags")), 7); let missing = eval( @@ -332,64 +384,85 @@ let d = ngx::shared::dict("dogs"); ngx::shared::get(d, "nope"); "#, ); - assert_eq!(*map_get(&missing, "value"), Value::Null); + assert_eq!( + option_payload(map_get(&missing, "value")), + None, + "a missing key has value: Option::None" + ); let exists = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "k", "v", null, null); -ngx::shared::add(d, "k", "other", null, null); +ngx::shared::set(d, "k", "v", (), ()); +ngx::shared::add(d, "k", "other", (), ()); "#, ); assert!(!expect_bool(map_get(&exists, "ok"))); - assert_eq!(expect_string(map_get(&exists, "error")), "exists"); + assert_eq!( + expect_option_string(map_get(&exists, "error")).expect("error is Some"), + "exists" + ); let not_found = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::replace(d, "missing", 1, null, null); +ngx::shared::replace(d, "missing", 1, (), ()); "#, ); assert!(!expect_bool(map_get(¬_found, "ok"))); - assert_eq!(expect_string(map_get(¬_found, "error")), "not found"); + assert_eq!( + expect_option_string(map_get(¬_found, "error")).expect("error is Some"), + "not found" + ); let deleted = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "k", true, null, null); +ngx::shared::set(d, "k", true, (), ()); ngx::shared::delete(d, "k"); ngx::shared::get(d, "k"); "#, ); - assert_eq!(*map_get(&deleted, "value"), Value::Null); + assert_eq!( + option_payload(map_get(&deleted, "value")), + None, + "a deleted key has value: Option::None" + ); let incr = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::incr(d, "n", 2, 10, null); +ngx::shared::incr(d, "n", 2, 10, ()); "#, ); - assert_eq!(expect_int(map_get(&incr, "value")), 12); - assert_eq!(*map_get(&incr, "error"), Value::Null); + assert_eq!( + expect_option_number(map_get(&incr, "value")).expect("value is Some") as i64, + 12 + ); + assert_eq!(option_payload(map_get(&incr, "error")), None); let not_number = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "s", "x", null, null); -ngx::shared::incr(d, "s", 1, null, null); +ngx::shared::set(d, "s", "x", (), ()); +ngx::shared::incr(d, "s", 1, (), ()); "#, ); - assert_eq!(expect_string(map_get(¬_number, "error")), "not a number"); - assert_eq!(*map_get(¬_number, "value"), Value::Null); + assert_eq!( + expect_option_string(map_get(¬_number, "error")).expect("error is Some"), + "not a number" + ); + assert_eq!(option_payload(map_get(¬_number, "value")), None); } #[test] fn ttl_expire_get_stale_and_clock() { + let _guard = zone_lock(); reset_process_zones(); install_process_zone("dogs", 64 * 1024); set_clock_ms(Some(1_000_000)); @@ -433,6 +506,7 @@ fn ttl_expire_get_stale_and_clock() { #[test] fn lru_eviction_forcible_and_safe_set() { + let _guard = zone_lock(); reset_process_zones(); install_process_zone("tiny", 200); let payload = DictValue::String("abcdefghij".into()); @@ -481,6 +555,7 @@ fn lru_eviction_forcible_and_safe_set() { #[test] fn capacity_free_space_and_unknown_zone() { + let _guard = zone_lock(); let cap = eval( r#" use ngx; @@ -509,6 +584,7 @@ ngx::shared::dict("missing"); #[test] fn empty_key_and_bad_value_type() { + let _guard = zone_lock(); let empty = eval( r#" use ngx; @@ -516,12 +592,15 @@ let d = ngx::shared::dict("dogs"); ngx::shared::get(d, ""); "#, ); - assert_eq!(expect_string(map_get(&empty, "error")), "empty key"); + assert_eq!( + expect_option_string(map_get(&empty, "error")).expect("error is Some"), + "empty key" + ); let err = eval_err( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "k", { a: 1 }, null, null); +ngx::shared::set(d, "k", { a: 1 }, (), ()); "#, ) .to_ascii_lowercase(); @@ -532,6 +611,7 @@ ngx::shared::set(d, "k", { a: 1 }, null, null); #[test] fn shared_dict_does_not_require_request_state() { + let _guard = zone_lock(); let program = compile( r#" use ngx; @@ -554,16 +634,17 @@ fn map_has_key(value: &Value, key: &str) -> bool { #[test] fn add_and_replace_reject_null() { + let _guard = zone_lock(); let add = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::add(d, "k", null, null, null); +ngx::shared::add(d, "k", (), (), ()); "#, ); assert!(!expect_bool(map_get(&add, "ok"))); assert_eq!( - expect_string(map_get(&add, "error")), + expect_option_string(map_get(&add, "error")).expect("error is Some"), "attempt to add or replace nil values" ); @@ -571,38 +652,44 @@ ngx::shared::add(d, "k", null, null, null); r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "k", 1, null, null); -ngx::shared::replace(d, "k", null, null, null); +ngx::shared::set(d, "k", 1, (), ()); +ngx::shared::replace(d, "k", (), (), ()); "#, ); assert!(!expect_bool(map_get(&replace, "ok"))); assert_eq!( - expect_string(map_get(&replace, "error")), + expect_option_string(map_get(&replace, "error")).expect("error is Some"), "attempt to add or replace nil values" ); let still = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "k", 1, null, null); -ngx::shared::replace(d, "k", null, null, null); +ngx::shared::set(d, "k", 1, (), ()); +ngx::shared::replace(d, "k", (), (), ()); ngx::shared::get(d, "k"); "#, ); - assert_eq!(expect_int(map_get(&still, "value")), 1); + let (kind, payload) = shared_value(map_get(&still, "value")).expect("value is Some"); + assert_eq!(kind, "int"); + assert_eq!(expect_int(&payload), 1); } #[test] fn incr_result_schema_is_value_error_forcible() { + let _guard = zone_lock(); let incr = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::incr(d, "n", 2, 10, null); +ngx::shared::incr(d, "n", 2, 10, ()); "#, ); - assert_eq!(expect_int(map_get(&incr, "value")), 12); - assert_eq!(*map_get(&incr, "error"), Value::Null); + assert_eq!( + expect_option_number(map_get(&incr, "value")).expect("value is Some") as i64, + 12 + ); + assert_eq!(option_payload(map_get(&incr, "error")), None); assert!(!expect_bool(map_get(&incr, "forcible"))); assert!( !map_has_key(&incr, "ok"), @@ -612,6 +699,7 @@ ngx::shared::incr(d, "n", 2, 10, null); #[test] fn zone_name_cap_is_256_bytes() { + let _guard = zone_lock(); assert_eq!(MAX_ZONE_NAME_BYTES, 256); assert!(shared_dict::validate_zone_name(&"a".repeat(256)).is_ok()); assert!(shared_dict::validate_zone_name(&"a".repeat(257)).is_err()); @@ -620,6 +708,7 @@ fn zone_name_cap_is_256_bytes() { #[test] fn tiny_positive_exptime_is_rejected_and_huge_exptime_is_rejected() { + let _guard = zone_lock(); reset_process_zones(); install_process_zone("dogs", 64 * 1024); set_clock_ms(Some(1_000_000)); @@ -649,6 +738,7 @@ fn tiny_positive_exptime_is_rejected_and_huge_exptime_is_rejected() { #[test] fn replace_is_allocation_transactional() { + let _guard = zone_lock(); reset_process_zones(); install_process_zone("tiny", 120); let small = DictValue::String("ab".into()); @@ -665,6 +755,7 @@ fn replace_is_allocation_transactional() { #[test] fn existing_numeric_incr_is_in_place_under_pressure() { + let _guard = zone_lock(); reset_process_zones(); install_process_zone("tiny", 160); let filler = DictValue::String("abcdefghij".into()); @@ -699,6 +790,7 @@ fn existing_numeric_incr_is_in_place_under_pressure() { #[test] fn handle_rejects_stale_and_cross_zone_confusion() { + let _guard = zone_lock(); reset_process_zones(); install_process_zone("dogs", 64 * 1024); install_process_zone("cats", 64 * 1024); @@ -738,6 +830,7 @@ fn handle_rejects_stale_and_cross_zone_confusion() { #[test] fn rbtree_orders_equal_hash_keys_with_nginx_memn2cmp() { + let _guard = zone_lock(); let left = shared_dict::nginx_memn2cmp(b"aa", b"ab"); let right = shared_dict::nginx_memn2cmp(b"ab", b"aa"); let prefix = shared_dict::nginx_memn2cmp(b"aa", b"aaa"); diff --git a/tests/ngx_shared_dict_list.rs b/tests/ngx_shared_dict_list.rs index 356381e..e7699b0 100644 --- a/tests/ngx_shared_dict_list.rs +++ b/tests/ngx_shared_dict_list.rs @@ -18,6 +18,12 @@ use ngx_rustscript::shared_dict::{ }; use vm::{HostParamPassing, HostTypeSchema, Program, ResourceTypeKey, Value, Vm, VmStatus}; +/// Serializes tests that share process-global stub or zone state. +fn zone_lock() -> std::sync::MutexGuard<'static, ()> { + static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + LOCK.lock().unwrap_or_else(|error| error.into_inner()) +} + fn compile(source: &str) -> Arc { compile_rss_source(source).unwrap_or_else(|err| panic!("compile failed: {err}\n{source}")) } @@ -66,6 +72,47 @@ fn expect_int(value: &Value) -> i64 { } } +/// Unwraps a canonical guest `Option` carrier into its payload. +fn option_payload(value: &Value) -> Option { + let (some, payload) = vm::option::payload(value).expect("Option carrier"); + if some { + Some(payload.expect("Some payload").clone()) + } else { + None + } +} + +fn expect_option_number(value: &Value) -> Option { + option_payload(value).map(|payload| match payload { + Value::Int(number) => number as f64, + Value::Float(number) => number, + other => panic!("expected number payload, got {other:?}"), + }) +} + +fn expect_option_string(value: &Value) -> Option { + option_payload(value).map(|payload| match payload { + Value::String(text) => (*text).clone(), + other => panic!("expected string payload, got {other:?}"), + }) +} + +/// Decodes one tagged `SharedValue` record. +fn shared_value(value: &Value) -> Option<(String, Value)> { + let record = option_payload(value)?; + let kind = expect_string(map_get(&record, "kind")); + let payload = match kind.as_str() { + "bool" => option_payload(map_get(&record, "bool_value")), + "int" => option_payload(map_get(&record, "int_value")), + "float" => option_payload(map_get(&record, "float_value")), + "string" => option_payload(map_get(&record, "text_value")), + "bytes" => option_payload(map_get(&record, "bytes_value")), + other => panic!("unknown SharedValue kind {other}"), + } + .unwrap_or_else(|| panic!("{kind} record must carry its payload")); + Some((kind, payload)) +} + fn expect_string(value: &Value) -> String { match value { Value::String(text) => (**text).clone(), @@ -93,6 +140,7 @@ const LIST_HOSTS: &[&str] = &[ #[test] fn list_and_maintenance_hosts_are_catalogued() { + let _guard = zone_lock(); assert_eq!( production_runtime_host_names().len(), 199, @@ -134,7 +182,7 @@ fn list_and_maintenance_hosts_are_catalogued() { } NGX_SHARED_FLUSH_ALL_NAME => { assert_eq!(schema.params.len(), 1); - assert_eq!(schema.return_type, HostTypeSchema::Null); + assert_eq!(schema.return_type, HostTypeSchema::Unit); } NGX_SHARED_FLUSH_EXPIRED_NAME => { assert_eq!(schema.params.len(), 2); @@ -177,11 +225,11 @@ fn list_and_maintenance_hosts_are_catalogued() { .to_string() } NGX_SHARED_FLUSH_EXPIRED_NAME => { - "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::flush_expired(d, null);\n" + "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::flush_expired(d, ());\n" .to_string() } NGX_SHARED_GET_KEYS_NAME => { - "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::get_keys(d, null);\n" + "use ngx;\nlet d = ngx::shared::dict(\"dogs\");\nngx::shared::get_keys(d, ());\n" .to_string() } _ => unreachable!(), @@ -191,6 +239,7 @@ fn list_and_maintenance_hosts_are_catalogued() { #[test] fn lpush_rpush_preserve_order_and_llen() { + let _guard = zone_lock(); let result = eval( r#" use ngx; @@ -201,8 +250,8 @@ ngx::shared::lpush(d, "q", "z"); ngx::shared::llen(d, "q"); "#, ); - assert_eq!(expect_int(map_get(&result, "value")), 3); - assert_eq!(*map_get(&result, "error"), Value::Null); + assert_eq!(expect_option_number(map_get(&result, "value")), Some(3.0)); + assert_eq!(option_payload(map_get(&result, "error")), None); let left = eval( r#" @@ -214,7 +263,9 @@ ngx::shared::lpush(d, "q", "z"); ngx::shared::lpop(d, "q"); "#, ); - assert_eq!(expect_string(map_get(&left, "value")), "z"); + let (kind, payload) = shared_value(map_get(&left, "value")).expect("lpop value"); + assert_eq!(kind, "string"); + assert_eq!(expect_string(&payload), "z"); let right = eval( r#" @@ -226,11 +277,14 @@ ngx::shared::lpush(d, "q", "z"); ngx::shared::rpop(d, "q"); "#, ); - assert_eq!(expect_string(map_get(&right, "value")), "b"); + let (kind, payload) = shared_value(map_get(&right, "value")).expect("rpop value"); + assert_eq!(kind, "string"); + assert_eq!(expect_string(&payload), "b"); } #[test] fn pop_and_llen_on_missing_key() { + let _guard = zone_lock(); let llen = eval( r#" use ngx; @@ -238,8 +292,8 @@ let d = ngx::shared::dict("dogs"); ngx::shared::llen(d, "missing"); "#, ); - assert_eq!(expect_int(map_get(&llen, "value")), 0); - assert_eq!(*map_get(&llen, "error"), Value::Null); + assert_eq!(expect_option_number(map_get(&llen, "value")), Some(0.0)); + assert_eq!(option_payload(map_get(&llen, "error")), None); let pop = eval( r#" @@ -248,12 +302,13 @@ let d = ngx::shared::dict("dogs"); ngx::shared::lpop(d, "missing"); "#, ); - assert_eq!(*map_get(&pop, "value"), Value::Null); - assert_eq!(*map_get(&pop, "error"), Value::Null); + assert_eq!(option_payload(map_get(&pop, "value")), None); + assert_eq!(option_payload(map_get(&pop, "error")), None); } #[test] fn last_pop_removes_the_list_key() { + let _guard = zone_lock(); let result = eval( r#" use ngx; @@ -263,55 +318,67 @@ ngx::shared::lpop(d, "q"); ngx::shared::llen(d, "q"); "#, ); - assert_eq!(expect_int(map_get(&result, "value")), 0); + assert_eq!(expect_option_number(map_get(&result, "value")), Some(0.0)); } #[test] fn scalar_list_conflict_returns_structured_error_and_does_not_corrupt() { + let _guard = zone_lock(); let push = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "k", "scalar", null, 9); +ngx::shared::set(d, "k", "scalar", (), 9); ngx::shared::lpush(d, "k", "x"); "#, ); - assert_eq!(*map_get(&push, "value"), Value::Null); - assert_eq!(expect_string(map_get(&push, "error")), "value not a list"); + assert_eq!(option_payload(map_get(&push, "value")), None); + assert_eq!( + expect_option_string(map_get(&push, "error")).expect("error is Some"), + "value not a list" + ); let got = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "k", "scalar", null, 9); +ngx::shared::set(d, "k", "scalar", (), 9); ngx::shared::lpush(d, "k", "x"); ngx::shared::get(d, "k"); "#, ); - assert_eq!(expect_string(map_get(&got, "value")), "scalar"); + let (kind, payload) = shared_value(map_get(&got, "value")).expect("get value"); + assert_eq!(kind, "string"); + assert_eq!(expect_string(&payload), "scalar"); assert_eq!(expect_int(map_get(&got, "flags")), 9); let llen = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "k", "scalar", null, null); +ngx::shared::set(d, "k", "scalar", (), ()); ngx::shared::llen(d, "k"); "#, ); - assert_eq!(*map_get(&llen, "value"), Value::Null); - assert_eq!(expect_string(map_get(&llen, "error")), "value not a list"); + assert_eq!(option_payload(map_get(&llen, "value")), None); + assert_eq!( + expect_option_string(map_get(&llen, "error")).expect("error is Some"), + "value not a list" + ); let pop = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "k", "scalar", null, null); +ngx::shared::set(d, "k", "scalar", (), ()); ngx::shared::rpop(d, "k"); "#, ); - assert_eq!(*map_get(&pop, "value"), Value::Null); - assert_eq!(expect_string(map_get(&pop, "error")), "value not a list"); + assert_eq!(option_payload(map_get(&pop, "value")), None); + assert_eq!( + expect_option_string(map_get(&pop, "error")).expect("error is Some"), + "value not a list" + ); let list_get = eval( r#" @@ -321,9 +388,9 @@ ngx::shared::lpush(d, "q", 1); ngx::shared::get(d, "q"); "#, ); - assert_eq!(*map_get(&list_get, "value"), Value::Null); + assert_eq!(option_payload(map_get(&list_get, "value")), None); assert_eq!( - expect_string(map_get(&list_get, "error")), + expect_option_string(map_get(&list_get, "error")).expect("error is Some"), "value is a list" ); @@ -332,24 +399,28 @@ ngx::shared::get(d, "q"); use ngx; let d = ngx::shared::dict("dogs"); ngx::shared::lpush(d, "q", 1); -ngx::shared::incr(d, "q", 1, null, null); +ngx::shared::incr(d, "q", 1, (), ()); "#, ); - assert_eq!(expect_string(map_get(&incr, "error")), "not a number"); + assert_eq!( + expect_option_string(map_get(&incr, "error")).expect("error is Some"), + "not a number" + ); let still = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); ngx::shared::lpush(d, "q", 1); -ngx::shared::incr(d, "q", 1, null, null); +ngx::shared::incr(d, "q", 1, (), ()); ngx::shared::llen(d, "q"); "#, ); - assert_eq!(expect_int(map_get(&still, "value")), 1); + assert_eq!(expect_option_number(map_get(&still, "value")), Some(1.0)); } #[test] fn list_push_rejects_bool_and_null() { + let _guard = zone_lock(); let bad = eval( r#" use ngx; @@ -357,20 +428,27 @@ let d = ngx::shared::dict("dogs"); ngx::shared::lpush(d, "q", true); "#, ); - assert_eq!(*map_get(&bad, "value"), Value::Null); - assert_eq!(expect_string(map_get(&bad, "error")), "bad value type"); + assert_eq!(option_payload(map_get(&bad, "value")), None); + assert_eq!( + expect_option_string(map_get(&bad, "error")).expect("error is Some"), + "bad value type" + ); let nil = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::rpush(d, "q", null); +ngx::shared::rpush(d, "q", ()); "#, ); - assert_eq!(expect_string(map_get(&nil, "error")), "bad value type"); + assert_eq!( + expect_option_string(map_get(&nil, "error")).expect("error is Some"), + "bad value type" + ); } #[test] fn list_push_is_allocation_transactional() { + let _guard = zone_lock(); reset_process_zones(); install_process_zone("tiny", 80); let failed = shared_dict::dict_lpush("tiny", b"q", DictValue::String("x".repeat(200))).unwrap(); @@ -383,6 +461,7 @@ fn list_push_is_allocation_transactional() { #[test] fn list_length_is_bounded() { + let _guard = zone_lock(); assert_eq!(MAX_LIST_LEN, 65_536); reset_process_zones(); install_process_zone("dogs", 16 * 1024 * 1024); @@ -398,6 +477,7 @@ fn list_length_is_bounded() { #[test] fn flush_all_marks_expired_without_requiring_delete() { + let _guard = zone_lock(); reset_process_zones(); install_process_zone("dogs", 64 * 1024); set_clock_ms(Some(1_000_000)); @@ -426,6 +506,7 @@ fn flush_all_marks_expired_without_requiring_delete() { #[test] fn flush_expired_honors_max_and_hard_cap() { + let _guard = zone_lock(); assert_eq!(MAX_FLUSH_BATCH, 1024); reset_process_zones(); install_process_zone("dogs", 256 * 1024); @@ -452,6 +533,7 @@ fn flush_expired_honors_max_and_hard_cap() { #[test] fn get_keys_skips_expired_and_obeys_hard_cap() { + let _guard = zone_lock(); assert_eq!(MAX_GET_KEYS, 1024); reset_process_zones(); install_process_zone("dogs", 512 * 1024); @@ -485,9 +567,9 @@ fn get_keys_skips_expired_and_obeys_hard_cap() { r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "a", 1, null, null); -ngx::shared::set(d, "b", 2, null, null); -ngx::shared::set(d, "c", 3, null, null); +ngx::shared::set(d, "a", 1, (), ()); +ngx::shared::set(d, "b", 2, (), ()); +ngx::shared::set(d, "c", 3, (), ()); ngx::shared::get_keys(d, 1); "#, ); @@ -513,27 +595,31 @@ ngx::shared::get_keys(d, 1); #[test] fn set_replaces_list_without_leaving_list_nodes() { + let _guard = zone_lock(); let got = eval( r#" use ngx; let d = ngx::shared::dict("dogs"); ngx::shared::lpush(d, "k", "x"); ngx::shared::lpush(d, "k", "y"); -ngx::shared::set(d, "k", "scalar", null, null); +ngx::shared::set(d, "k", "scalar", (), ()); ngx::shared::get(d, "k"); "#, ); - assert_eq!(expect_string(map_get(&got, "value")), "scalar"); + let (kind, payload) = shared_value(map_get(&got, "value")).expect("get value"); + assert_eq!(kind, "string"); + assert_eq!(expect_string(&payload), "scalar"); } #[test] fn list_hosts_do_not_require_request_state() { + let _guard = zone_lock(); let program = compile( r#" use ngx; let d = ngx::shared::dict("dogs"); ngx::shared::lpush(d, "q", 1); -ngx::shared::get_keys(d, null); +ngx::shared::get_keys(d, ()); "#, ); assert!( @@ -544,6 +630,7 @@ ngx::shared::get_keys(d, null); #[test] fn list_ops_keep_sibling_scalar_lookup() { + let _guard = zone_lock(); reset_process_zones(); install_process_zone("dogs", 64 * 1024); shared_dict::dict_store( @@ -579,6 +666,7 @@ fn list_ops_keep_sibling_scalar_lookup() { #[test] fn get_keys_is_read_only_and_preserves_every_live_key() { + let _guard = zone_lock(); reset_process_zones(); install_process_zone("dogs", 64 * 1024); set_clock_ms(Some(3_000_000)); @@ -612,6 +700,7 @@ fn get_keys_is_read_only_and_preserves_every_live_key() { #[test] fn get_keys_bounds_visits_independently_of_result_hits() { + let _guard = zone_lock(); reset_process_zones(); install_process_zone("dogs", 2 * 1024 * 1024); set_clock_ms(Some(4_000_000)); @@ -650,6 +739,7 @@ fn get_keys_bounds_visits_independently_of_result_hits() { #[test] fn flush_expired_bounds_visits_independently_of_hits() { + let _guard = zone_lock(); reset_process_zones(); install_process_zone("dogs", 2 * 1024 * 1024); set_clock_ms(Some(5_000_000)); @@ -699,6 +789,7 @@ fn flush_expired_bounds_visits_independently_of_hits() { #[test] fn flush_all_repeated_calls_complete_beyond_batch() { + let _guard = zone_lock(); reset_process_zones(); install_process_zone("dogs", 4 * 1024 * 1024); set_clock_ms(Some(6_000_000)); diff --git a/tests/ngx_task7_hardening.rs b/tests/ngx_task7_hardening.rs index 17ea581..d914e05 100644 --- a/tests/ngx_task7_hardening.rs +++ b/tests/ngx_task7_hardening.rs @@ -37,6 +37,12 @@ unsafe extern "C" { ) -> i32; } +/// Serializes tests that share process-global stub or zone state. +fn stub_lock() -> std::sync::MutexGuard<'static, ()> { + static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + LOCK.lock().unwrap_or_else(|error| error.into_inner()) +} + fn compile(source: &str) -> Arc { compile_rss_source(source).unwrap_or_else(|err| panic!("compile failed: {err}\n{source}")) } @@ -148,6 +154,7 @@ fn is_host_err(err: &str) -> bool { #[test] fn invalid_host_is_rejected_before_list_mutation() { + let _guard = stub_lock(); enable_alloc(64); let mut req = dummy_request(); let mut storage = [unsafe { MaybeUninit::::zeroed().assume_init() }; 8]; @@ -173,6 +180,7 @@ fn invalid_host_is_rejected_before_list_mutation() { #[test] fn host_with_port_normalizes_server_and_invalidates_cached_host() { + let _guard = stub_lock(); enable_alloc(64); nginx_stubs::stub_var_reset(); let mut req = dummy_request(); @@ -198,6 +206,7 @@ fn host_with_port_normalizes_server_and_invalidates_cached_host() { #[test] fn user_agent_recomputes_browser_flags_and_clears_them() { + let _guard = stub_lock(); enable_alloc(64); let mut req = dummy_request(); let mut storage = [unsafe { MaybeUninit::::zeroed().assume_init() }; 8]; @@ -237,6 +246,7 @@ fn user_agent_recomputes_browser_flags_and_clears_them() { #[test] fn connection_keep_alive_close_is_close_substring_semantics() { + let _guard = stub_lock(); enable_alloc(64); let mut req = dummy_request(); let mut storage = [unsafe { MaybeUninit::::zeroed().assume_init() }; 8]; @@ -266,6 +276,7 @@ fn connection_keep_alive_close_is_close_substring_semantics() { #[test] fn invalid_content_length_rejects_before_any_mutation() { + let _guard = stub_lock(); enable_alloc(64); let mut req = dummy_request(); let mut storage = [unsafe { MaybeUninit::::zeroed().assume_init() }; 8]; @@ -296,6 +307,7 @@ fn invalid_content_length_rejects_before_any_mutation() { #[test] fn malformed_request_header_list_does_not_mutate() { + let _guard = stub_lock(); enable_alloc(64); let mut req = dummy_request(); req.pool = std::ptr::dangling_mut(); @@ -324,6 +336,7 @@ fn malformed_request_header_list_does_not_mutate() { #[test] fn request_header_list_push_failure_rolls_back_old_entries() { + let _guard = stub_lock(); enable_alloc(3); let mut req = dummy_request(); let mut storage = [unsafe { MaybeUninit::::zeroed().assume_init() }; 1]; @@ -351,6 +364,7 @@ fn request_header_list_push_failure_rolls_back_old_entries() { #[test] fn set_method_allocation_failure_leaves_method_untouched() { + let _guard = stub_lock(); nginx_stubs::stub_alloc_reset(); let mut req = dummy_request(); req.method = nginx_sys::NGX_HTTP_GET as ngx_uint_t; @@ -370,6 +384,7 @@ fn set_method_allocation_failure_leaves_method_untouched() { #[test] fn status_get_prefers_err_status_then_headers_out_then_http09() { + let _guard = stub_lock(); let mut req = dummy_request(); let ptr = req.as_mut() as *mut ngx_http_request_t; nginx_stubs::test_set_err_status(ptr.cast(), 500); @@ -390,13 +405,14 @@ fn status_get_prefers_err_status_then_headers_out_then_http09() { #[test] fn redirect_without_disposition_does_not_mutate_location_or_status() { + let _guard = stub_lock(); enable_alloc(64); let mut req = dummy_request(); let mut storage = [unsafe { MaybeUninit::::zeroed().assume_init() }; 8]; writable_out_list(&mut req, &mut storage, 0); req.headers_out.status = 200; let ptr = req.as_mut() as *mut ngx_http_request_t; - let program = compile("use ngx;\nngx::redirect(\"/x\", null);\n"); + let program = compile("use ngx;\nngx::redirect(\"/x\", ());\n"); let mut vm = Vm::try_new((*program).clone()).expect("vm"); bind_ngx_hosts(&mut vm); let err = with_request_scope( @@ -420,6 +436,7 @@ fn redirect_without_disposition_does_not_mutate_location_or_status() { #[test] fn redirect_first_wins_leaves_previous_disposition_and_location() { + let _guard = stub_lock(); enable_alloc(64); let mut req = dummy_request(); let mut storage = [unsafe { MaybeUninit::::zeroed().assume_init() }; 8]; @@ -445,6 +462,7 @@ fn redirect_first_wins_leaves_previous_disposition_and_location() { #[test] fn response_set_all_allocation_failure_restores_specials() { + let _guard = stub_lock(); enable_alloc(2); let mut req = dummy_request(); let mut storage = [unsafe { MaybeUninit::::zeroed().assume_init() }; 1]; @@ -476,6 +494,7 @@ fn response_set_all_allocation_failure_restores_specials() { #[test] fn send_last_response_matrix_and_helper_failure() { + let _guard = stub_lock(); nginx_stubs::stub_send_reset(); let mut req = dummy_request(); let ptr = req.as_mut() as *mut ngx_http_request_t; @@ -527,6 +546,7 @@ fn send_last_response_matrix_and_helper_failure() { #[test] fn variable_set_c_abi_branches() { + let _guard = stub_lock(); nginx_stubs::stub_var_reset(); enable_alloc(32); let mut req = dummy_request(); @@ -645,6 +665,7 @@ fn variable_set_c_abi_branches() { #[test] fn disposition_mut_participates_in_exclusive_request_borrow() { + let _guard = stub_lock(); let mut req = dummy_request(); let mut disposition = RequestDisposition::Continue; let identity = RequestIdentity::new(9, 9); @@ -677,6 +698,7 @@ fn disposition_mut_participates_in_exclusive_request_borrow() { #[test] fn var_get_name_limit_is_1024() { + let _guard = stub_lock(); let mut req = dummy_request(); let ptr = req.as_mut() as *mut ngx_http_request_t; let name_ok = "a".repeat(1024); @@ -690,6 +712,7 @@ fn var_get_name_limit_is_1024() { #[test] fn cookie_and_content_type_dedicated_pointers() { + let _guard = stub_lock(); enable_alloc(64); let mut req = dummy_request(); let mut storage = [unsafe { MaybeUninit::::zeroed().assume_init() }; 8]; diff --git a/tests/ngx_tcp_keepalive.rs b/tests/ngx_tcp_keepalive.rs index 9bb1910..cbf63c0 100644 --- a/tests/ngx_tcp_keepalive.rs +++ b/tests/ngx_tcp_keepalive.rs @@ -195,7 +195,7 @@ fn catalog_declares_keepalive_hosts() { assert_eq!(keepalive.params[1].ty, HostTypeSchema::Unknown); assert_eq!(keepalive.params[2].name, "pool_size"); assert_eq!(keepalive.params[2].ty, HostTypeSchema::Unknown); - assert_eq!(keepalive.return_type, HostTypeSchema::Null); + assert_eq!(keepalive.return_type, HostTypeSchema::Unit); let reused = catalog .function(GETREUSEDTIMES) diff --git a/tests/ngx_tcp_socket.rs b/tests/ngx_tcp_socket.rs index 83f0ace..ab6356b 100644 --- a/tests/ngx_tcp_socket.rs +++ b/tests/ngx_tcp_socket.rs @@ -538,7 +538,7 @@ fn receive_line_strips_crlf() { }); let status = pump( &format!( - "use ngx;\nlet sock = ngx::socket::tcp();\nngx::socket::tcp::settimeout(sock, 1000);\nngx::socket::tcp::connect(sock, \"127.0.0.1\", {port});\nlet a = ngx::socket::tcp::receive(sock, null);\nngx::socket::tcp::send(sock, a);\nlet b = ngx::socket::tcp::receive(sock, \"*l\");\nngx::socket::tcp::send(sock, b);\nngx::socket::tcp::close(sock);\n" + "use ngx;\nlet sock = ngx::socket::tcp();\nngx::socket::tcp::settimeout(sock, 1000);\nngx::socket::tcp::connect(sock, \"127.0.0.1\", {port});\nlet a = ngx::socket::tcp::receive(sock, ());\nngx::socket::tcp::send(sock, a);\nlet b = ngx::socket::tcp::receive(sock, \"*l\");\nngx::socket::tcp::send(sock, b);\nngx::socket::tcp::close(sock);\n" ), Duration::from_secs(3), ); @@ -803,14 +803,18 @@ fn send_and_receive_caps_are_rejected() { #[test] fn dns_host_over_the_length_cap_is_rejected() { + // Pay the lazy catalog initialization before the clock starts: this test + // measures the connect path, not the first compile in the process. + let _ = ngx_host_catalog(); let host = "a".repeat(256); + let program = compile(&format!( + "use ngx;\nlet sock = ngx::socket::tcp();\nngx::socket::tcp::settimeout(sock, 1000);\nngx::socket::tcp::connect(sock, \"{host}\", 80);\n" + )); + // The worker runtime start is harness cost, not connect latency: build it + // before the clock so the bound measures the rejected lookup only. + let driver = Driver::new(program); let started = Instant::now(); - let status = pump( - &format!( - "use ngx;\nlet sock = ngx::socket::tcp();\nngx::socket::tcp::settimeout(sock, 1000);\nngx::socket::tcp::connect(sock, \"{host}\", 80);\n" - ), - Duration::from_secs(3), - ); + let status = driver.pump_until(Duration::from_secs(3)); let elapsed = started.elapsed(); assert_eq!(status, AccessStatus::Error500); assert!( diff --git a/tests/ngx_tcp_tls.rs b/tests/ngx_tcp_tls.rs index 010079f..c91fc7f 100644 --- a/tests/ngx_tcp_tls.rs +++ b/tests/ngx_tcp_tls.rs @@ -223,7 +223,7 @@ ngx::socket::tcp::settimeout(sock, 2000); ngx::socket::tcp::connect(sock, \"127.0.0.1\", {port}); let store = ngx::socket::tcp::trustedstore(\"{ca}\"); ngx::socket::tcp::settrustedstore(sock, store); -ngx::socket::tcp::sslhandshake(sock, null, \"{server_name}\", {verify}, null); +ngx::socket::tcp::sslhandshake(sock, (), \"{server_name}\", {verify}, ()); ngx::socket::tcp::send(sock, \"{payload}\"); ngx::socket::tcp::receive(sock, {len}); ngx::socket::tcp::close(sock); @@ -249,16 +249,16 @@ fn catalog_declares_tls_hosts() { assert_eq!(handshake.params[2].name, "server_name"); assert_eq!(handshake.params[3].name, "ssl_verify"); assert_eq!(handshake.params[4].name, "send_status_req"); - assert_eq!(handshake.return_type, HostTypeSchema::Null); + assert_eq!(handshake.return_type, HostTypeSchema::Unit); let store = catalog.function(SETTRUSTEDSTORE).expect("settrustedstore"); assert_eq!(store.params.len(), 2); - assert_eq!(store.return_type, HostTypeSchema::Null); + assert_eq!(store.return_type, HostTypeSchema::Unit); let cert = catalog.function(SETCLIENTCERT).expect("setclientcert"); assert_eq!(cert.params.len(), 2); assert_eq!(cert.params[1].name, "cert"); - assert_eq!(cert.return_type, HostTypeSchema::Null); + assert_eq!(cert.return_type, HostTypeSchema::Unit); let trustedstore = catalog .function("ngx::socket::tcp::trustedstore") @@ -405,7 +405,7 @@ fn tls_verify_false_skips_certificate_checks() { let sock = ngx::socket::tcp(); ngx::socket::tcp::settimeout(sock, 2000); ngx::socket::tcp::connect(sock, \"127.0.0.1\", {port}); -ngx::socket::tcp::sslhandshake(sock, null, \"wrong.example\", false, null); +ngx::socket::tcp::sslhandshake(sock, (), \"wrong.example\", false, ()); ngx::socket::tcp::send(sock, \"ping\"); ngx::socket::tcp::receive(sock, 4); ngx::socket::tcp::close(sock); @@ -434,7 +434,7 @@ fn tls_handshake_timeout() { let sock = ngx::socket::tcp(); ngx::socket::tcp::settimeout(sock, 80); ngx::socket::tcp::connect(sock, \"127.0.0.1\", {port}); -ngx::socket::tcp::sslhandshake(sock, null, \"localhost\", false, null); +ngx::socket::tcp::sslhandshake(sock, (), \"localhost\", false, ()); " ); assert_eq!( @@ -461,7 +461,7 @@ fn tls_handshake_cancel_does_not_leak_inflight() { let sock = ngx::socket::tcp(); ngx::socket::tcp::settimeout(sock, 0); ngx::socket::tcp::connect(sock, \"127.0.0.1\", {port}); -ngx::socket::tcp::sslhandshake(sock, null, \"localhost\", false, null); +ngx::socket::tcp::sslhandshake(sock, (), \"localhost\", false, ()); " )), 1, @@ -518,7 +518,7 @@ ngx::socket::tcp::settimeout(sock, 2000); ngx::socket::tcp::connect(sock, \"127.0.0.1\", {port}); let store = ngx::socket::tcp::trustedstore(\"{ca}\"); ngx::socket::tcp::settrustedstore(sock, store); -ngx::socket::tcp::sslhandshake(sock, null, \"localhost\", true, null); +ngx::socket::tcp::sslhandshake(sock, (), \"localhost\", true, ()); ngx::socket::tcp::send(sock, \"one\"); ngx::socket::tcp::receive(sock, 3); ngx::socket::tcp::setkeepalive(sock, 60000, 8); @@ -535,7 +535,7 @@ ngx::exit(500); }} let store = ngx::socket::tcp::trustedstore(\"{ca}\"); ngx::socket::tcp::settrustedstore(sock, store); -ngx::socket::tcp::sslhandshake(sock, null, \"localhost\", true, null); +ngx::socket::tcp::sslhandshake(sock, (), \"localhost\", true, ()); let n = ngx::socket::tcp::getreusedtimes(sock); if n != 1 {{ ngx::exit(500); @@ -608,7 +608,7 @@ ngx::socket::tcp::settimeout(sock, 2000); ngx::socket::tcp::connect(sock, \"127.0.0.1\", {port}); let store = ngx::socket::tcp::trustedstore(\"{ca}\"); ngx::socket::tcp::settrustedstore(sock, store); -ngx::socket::tcp::sslhandshake(sock, null, \"localhost\", true, null); +ngx::socket::tcp::sslhandshake(sock, (), \"localhost\", true, ()); ngx::socket::tcp::send(sock, \"one\"); ngx::socket::tcp::receive(sock, 3); ngx::socket::tcp::setkeepalive(sock, 60000, 8); @@ -619,7 +619,7 @@ ngx::socket::tcp::setkeepalive(sock, 60000, 8); let sock = ngx::socket::tcp(); ngx::socket::tcp::settimeout(sock, 200); ngx::socket::tcp::connect(sock, \"127.0.0.1\", {port}); -ngx::socket::tcp::sslhandshake(sock, null, \"other.example\", false, null); +ngx::socket::tcp::sslhandshake(sock, (), \"other.example\", false, ()); " ); let mut session = Session::new(); @@ -653,7 +653,7 @@ ngx::socket::tcp::settimeout(sock, 2000); ngx::socket::tcp::connect(sock, \"127.0.0.1\", {port}); let store = ngx::socket::tcp::trustedstore(\"{ca}\"); ngx::socket::tcp::settrustedstore(sock, store); -ngx::socket::tcp::sslhandshake(sock, null, \"localhost\", true, null); +ngx::socket::tcp::sslhandshake(sock, (), \"localhost\", true, ()); ngx::socket::tcp::send(sock, \"one\"); ngx::socket::tcp::receive(sock, 3); ngx::socket::tcp::setkeepalive(sock, 60000, 8); @@ -811,7 +811,7 @@ let store = ngx::socket::tcp::trustedstore(\"{ca}\"); ngx::socket::tcp::settrustedstore(sock, store); let client = ngx::socket::tcp::clientcert(\"{cert}\", \"{key}\"); ngx::socket::tcp::setclientcert(sock, client); -ngx::socket::tcp::sslhandshake(sock, null, \"localhost\", true, null); +ngx::socket::tcp::sslhandshake(sock, (), \"localhost\", true, ()); ngx::socket::tcp::send(sock, \"ping\"); ngx::socket::tcp::receive(sock, 4); ngx::socket::tcp::close(sock); @@ -848,7 +848,7 @@ ngx::socket::tcp::settimeout(sock, 2000); ngx::socket::tcp::connect(sock, \"127.0.0.1\", {port}); let store = ngx::socket::tcp::trustedstore(\"{ca}\"); ngx::socket::tcp::settrustedstore(sock, store); -ngx::socket::tcp::sslhandshake(sock, null, \"localhost\", true, null); +ngx::socket::tcp::sslhandshake(sock, (), \"localhost\", true, ()); ngx::socket::tcp::send(sock, \"ping\"); ngx::socket::tcp::receive(sock, 4); " @@ -911,7 +911,7 @@ fn reused_session_handle_is_unsupported() { let sock = ngx::socket::tcp(); ngx::socket::tcp::settimeout(sock, 500); ngx::socket::tcp::connect(sock, \"127.0.0.1\", {port}); -ngx::socket::tcp::sslhandshake(sock, 1, \"localhost\", false, null); +ngx::socket::tcp::sslhandshake(sock, 1, \"localhost\", false, ()); " ); let mut session = Session::new(); @@ -934,7 +934,7 @@ fn send_status_req_false_is_unsupported() { let sock = ngx::socket::tcp(); ngx::socket::tcp::settimeout(sock, 500); ngx::socket::tcp::connect(sock, \"127.0.0.1\", {port}); -ngx::socket::tcp::sslhandshake(sock, null, \"localhost\", false, false); +ngx::socket::tcp::sslhandshake(sock, (), \"localhost\", false, false); " ); let mut session = Session::new(); @@ -964,7 +964,7 @@ fn send_status_req_true_puts_ocsp_status_request_in_client_hello() { let sock = ngx::socket::tcp(); ngx::socket::tcp::settimeout(sock, 800); ngx::socket::tcp::connect(sock, \"127.0.0.1\", {port}); -ngx::socket::tcp::sslhandshake(sock, null, \"localhost\", false, true); +ngx::socket::tcp::sslhandshake(sock, (), \"localhost\", false, true); " ); let mut session = Session::new(); @@ -997,7 +997,7 @@ ngx::socket::tcp::settimeout(sock, 2000); ngx::socket::tcp::connect(sock, \"127.0.0.1\", {port}); let store = ngx::socket::tcp::trustedstore(\"{ca}\"); ngx::socket::tcp::settrustedstore(sock, store); -ngx::socket::tcp::sslhandshake(sock, false, \"localhost\", true, null); +ngx::socket::tcp::sslhandshake(sock, false, \"localhost\", true, ()); ngx::socket::tcp::send(sock, \"ping\"); ngx::socket::tcp::receive(sock, 4); ngx::socket::tcp::close(sock); diff --git a/tests/ngx_time.rs b/tests/ngx_time.rs index 2d4516f..3ad605d 100644 --- a/tests/ngx_time.rs +++ b/tests/ngx_time.rs @@ -15,6 +15,12 @@ use ngx_rustscript::host::{ }; use vm::{Program, Value, Vm, VmStatus}; +/// Serializes tests that share process-global stub or zone state. +fn clock_lock() -> std::sync::MutexGuard<'static, ()> { + static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + LOCK.lock().unwrap_or_else(|error| error.into_inner()) +} + fn compile(source: &str) -> Arc { compile_rss_source(source).unwrap_or_else(|err| panic!("compile failed: {err}")) } @@ -48,6 +54,23 @@ fn expect_string(value: Value) -> String { } } +/// Unwraps a canonical guest `Option` carrier into its payload. +fn option_payload(value: &Value) -> Option { + let (some, payload) = vm::option::payload(value).expect("Option carrier"); + if some { + Some(payload.expect("Some payload").clone()) + } else { + None + } +} + +fn expect_option_int(value: Value) -> Option { + option_payload(&value).map(|payload| match payload { + Value::Int(number) => number, + other => panic!("expected int payload, got {other:?}"), + }) +} + fn expect_int(value: Value) -> i64 { match value { Value::Int(number) => number, @@ -69,6 +92,7 @@ fn reset_clock(sec: i64, msec: u32) { #[test] fn http_cookie_and_parse_time_are_fixed() { + let _guard = clock_lock(); assert_eq!( expect_string(eval("use ngx;\nngx::http_time(1290079655);\n")), "Thu, 18 Nov 2010 11:27:35 GMT" @@ -78,38 +102,44 @@ fn http_cookie_and_parse_time_are_fixed() { "Thu, 18-Nov-10 11:27:35 GMT" ); assert_eq!( - expect_int(eval( + expect_option_int(eval( "use ngx;\nngx::parse_http_time(\"Thu, 18 Nov 2010 11:27:35 GMT\");\n" )), - 1290079655 + Some(1290079655) ); assert_eq!( - eval("use ngx;\nngx::parse_http_time(\"not a date\");\n"), - Value::Null + expect_option_int(eval("use ngx;\nngx::parse_http_time(\"not a date\");\n")), + None, + "an unparsable date is Option::None, not a physical Null" ); } #[test] fn parse_http_time_accepts_rfc850_and_asctime() { + let _guard = clock_lock(); assert_eq!( - expect_int(eval( + expect_option_int(eval( "use ngx;\nngx::parse_http_time(\"Thursday, 18-Nov-10 11:27:35 GMT\");\n" )), - 1290079655 + Some(1290079655) ); assert_eq!( - expect_int(eval( + expect_option_int(eval( "use ngx;\nngx::parse_http_time(\"Thu Nov 18 11:27:35 2010\");\n" )), - 1290079655 + Some(1290079655) ); } #[test] fn parse_http_time_rejects_pre_1970_and_over_128_bytes() { + let _guard = clock_lock(); assert_eq!( - eval("use ngx;\nngx::parse_http_time(\"Wed, 31 Dec 1969 23:59:59 GMT\");\n"), - Value::Null + expect_option_int(eval( + "use ngx;\nngx::parse_http_time(\"Wed, 31 Dec 1969 23:59:59 GMT\");\n" + )), + None, + "pre-1970 dates are Option::None" ); let over = "x".repeat(129); let err = eval_err(&format!("use ngx;\nngx::parse_http_time(\"{over}\");\n")); @@ -122,6 +152,7 @@ fn parse_http_time_rejects_pre_1970_and_over_128_bytes() { #[test] fn time_is_int_and_now_is_float_from_the_same_cache() { + let _guard = clock_lock(); let seconds = expect_int(eval("use ngx;\nngx::time();\n")); let now = expect_float(eval("use ngx;\nngx::now();\n")); assert!( @@ -137,6 +168,7 @@ fn time_is_int_and_now_is_float_from_the_same_cache() { #[test] fn today_localtime_and_utctime_use_fixed_formats() { + let _guard = clock_lock(); let today = expect_string(eval("use ngx;\nngx::today();\n")); assert!( today.chars().count() == 10 && today.as_bytes()[4] == b'-' && today.as_bytes()[7] == b'-', @@ -156,6 +188,7 @@ fn today_localtime_and_utctime_use_fixed_formats() { #[test] fn intra_script_now_reads_share_the_cached_snapshot() { + let _guard = clock_lock(); reset_clock(1_700_000_000, 250); let value = eval("use ngx;\nlet a = ngx::now();\nlet b = ngx::now();\na == b;\n"); assert_eq!(value, Value::Bool(true)); @@ -164,6 +197,7 @@ fn intra_script_now_reads_share_the_cached_snapshot() { #[test] fn event_boundary_and_update_time_refresh_cached_clock_across_vms() { + let _guard = clock_lock(); reset_clock(1_700_000_000, 0); let first = expect_float(eval("use ngx;\nngx::now();\n")); assert!((first - 1_700_000_000.0).abs() < f64::EPSILON); @@ -196,6 +230,7 @@ fn event_boundary_and_update_time_refresh_cached_clock_across_vms() { #[test] fn access_program_turn_refreshes_cached_clock_without_direct_hook() { + let _guard = clock_lock(); override_os_clock(1_710_000_000, 0); let primed = expect_float(eval("use ngx;\nngx::update_time();\nngx::now();\n")); assert!((primed - 1_710_000_000.0).abs() < f64::EPSILON); @@ -221,6 +256,7 @@ fn access_program_turn_refreshes_cached_clock_without_direct_hook() { #[test] fn async_ready_event_turn_refreshes_cached_clock_without_direct_hook() { + let _guard = clock_lock(); override_os_clock(1_720_000_000, 0); let primed = expect_float(eval("use ngx;\nngx::update_time();\nngx::now();\n")); assert!((primed - 1_720_000_000.0).abs() < f64::EPSILON); @@ -241,6 +277,7 @@ fn async_ready_event_turn_refreshes_cached_clock_without_direct_hook() { #[test] fn update_time_samples_os_immediately_without_event_turn() { + let _guard = clock_lock(); override_os_clock(1_730_000_000, 0); let primed = expect_float(eval("use ngx;\nngx::update_time();\nngx::now();\n")); assert!((primed - 1_730_000_000.0).abs() < f64::EPSILON); diff --git a/tests/ngx_timer.rs b/tests/ngx_timer.rs index 7b5281c..2f412e2 100644 --- a/tests/ngx_timer.rs +++ b/tests/ngx_timer.rs @@ -131,8 +131,8 @@ fn timer_hosts_are_catalogued_and_manifested_in_access_and_content() { fn access_and_content_accept_the_timer_family() { for phase in [NgxPhase::Access, NgxPhase::Content] { for source in [ - "use ngx;\nngx::timer::at(0.05, |premature| null);\n", - "use ngx;\nngx::timer::every(0.05, |premature| null);\n", + "use ngx;\nngx::timer::at(0.05, |premature| ());\n", + "use ngx;\nngx::timer::every(0.05, |premature| ());\n", "use ngx;\nngx::timer::pending_count();\n", "use ngx;\nngx::timer::running_count();\n", ] { @@ -145,7 +145,7 @@ fn access_and_content_accept_the_timer_family() { #[test] fn log_phase_rejects_timer_registration_at_configuration_compile() { for source in [ - "use ngx;\nngx::timer::at(0.05, |premature| null);\n", + "use ngx;\nngx::timer::at(0.05, |premature| ());\n", "use ngx;\nngx::timer::pending_count();\n", ] { let err = compile_rss_source_for_phase(source, NgxPhase::Log) @@ -162,7 +162,7 @@ fn log_phase_rejects_timer_registration_at_configuration_compile() { #[test] fn timer_imports_require_request_state() { - let program = compile_rss_source("use ngx;\nngx::timer::at(0.05, |premature| null);\n") + let program = compile_rss_source("use ngx;\nngx::timer::at(0.05, |premature| ());\n") .expect("timer script must compile"); assert!( program_requires_request_state(&program), @@ -199,7 +199,7 @@ fn at_registers_one_fake_request_with_an_ordinary_identity() { // Catalog/registry install is paid during VM construction. Time only the // host call so a slow first bind cannot be mistaken for waiting on the // 10s deadline. - let mut vm = timer_vm("use ngx;\nngx::timer::at(10, |premature| null);\n", &worker); + let mut vm = timer_vm("use ngx;\nngx::timer::at(10, |premature| ());\n", &worker); let start = Instant::now(); vm.run() .map(|_| ()) @@ -230,7 +230,7 @@ fn at_registers_one_fake_request_with_an_ordinary_identity() { // A second registration takes the next worker-unique fake request id; two // live registrations never share an identity. - run_script("use ngx;\nngx::timer::at(20, |premature| null);\n", &worker).expect("registration"); + run_script("use ngx;\nngx::timer::at(20, |premature| ());\n", &worker).expect("registration"); let both = live_timer_identities(); assert_eq!(both.len(), 2); assert_ne!( @@ -260,7 +260,7 @@ fn deadline_fires_the_callback_once_with_premature_false() { run_script( // A `premature=true` round would call the request-only host and fail; // a clean completion witnesses `premature=false`. - "use ngx;\nngx::timer::at(0.05, |premature| if premature => { ngx::req::get_method(); null } else => { null });\n", + "use ngx;\nngx::timer::at(0.05, |premature| if premature => { ngx::req::get_method(); () } else => { () });\n", &worker, ) .expect("registration"); @@ -285,7 +285,7 @@ fn deadline_fires_the_callback_once_with_premature_false() { fn callback_waits_on_an_async_host_and_resumes_through_the_same_identity() { let mut worker = WorkerRuntime::start().expect("worker runtime"); run_script( - "use ngx;\nngx::timer::at(0.02, |premature| if true => { ngx::sleep_ms(50); null } else => { null });\n", + "use ngx;\nngx::timer::at(0.02, |premature| if true => { ngx::sleep_ms(50); () } else => { () });\n", &worker, ) .expect("registration"); @@ -314,7 +314,7 @@ fn callback_waits_on_an_async_host_and_resumes_through_the_same_identity() { fn callback_failure_is_isolated_and_reported_once() { let mut worker = WorkerRuntime::start().expect("worker runtime"); run_script( - "use ngx;\nngx::timer::at(0.02, |premature| ngx::req::get_method());\n", + "use ngx;\nngx::timer::at(0.02, |premature| if true => { ngx::req::get_method(); () } else => { () });\n", &worker, ) .expect("registration"); @@ -328,7 +328,7 @@ fn callback_failure_is_isolated_and_reported_once() { assert_eq!(live_timer_count(), 0, "`at` retires after the failed round"); // The worker keeps serving: another registration still works. - run_script("use ngx;\nngx::timer::at(10, |premature| null);\n", &worker).expect("registration"); + run_script("use ngx;\nngx::timer::at(10, |premature| ());\n", &worker).expect("registration"); assert_eq!(live_timer_count(), 1); worker.shutdown(); assert_eq!(live_timer_count(), 0); @@ -340,7 +340,7 @@ fn every_serializes_rounds_and_keeps_running_after_each_round() { // Each round calls a request-only host, so a completed round is visible as // exactly one reported error. run_script( - "use ngx;\nngx::timer::every(0.03, |premature| ngx::req::get_method());\n", + "use ngx;\nngx::timer::every(0.03, |premature| if true => { ngx::req::get_method(); () } else => { () });\n", &worker, ) .expect("registration"); @@ -383,7 +383,7 @@ fn every_rounds_rotate_identity_and_retire_the_previous_generation() { // A completed round is visible as exactly one reported error, because the // callback calls a request-only host. run_script( - "use ngx;\nngx::timer::every(0.05, |premature| ngx::req::get_method());\n", + "use ngx;\nngx::timer::every(0.05, |premature| if true => { ngx::req::get_method(); () } else => { () });\n", &worker, ) .expect("registration"); @@ -435,7 +435,7 @@ fn duplicate_stale_wakes_cannot_start_the_next_every_round_early() { let interval = Duration::from_millis(500); run_script( &format!( - "use ngx;\nngx::timer::every({}, |premature| ngx::req::get_method());\n", + "use ngx;\nngx::timer::every({}, |premature| if true => {{ ngx::req::get_method(); () }} else => {{ () }});\n", interval.as_secs_f64() ), &worker, @@ -501,7 +501,7 @@ fn stale_async_wake_from_a_previous_round_is_ignored() { run_script( // The round parks on `ngx::sleep_ms`; the sleep completion wakes the // round's own identity through the worker eventfd. - "use ngx;\nngx::timer::every(10, |premature| if true => { ngx::sleep_ms(30); ngx::req::get_method(); null } else => { null });\n", + "use ngx;\nngx::timer::every(10, |premature| if true => { ngx::sleep_ms(30); ngx::req::get_method(); () } else => { () });\n", &worker, ) .expect("registration"); @@ -546,9 +546,11 @@ fn http_standard_import_and_ngx_timer_bind_through_the_worker_registry() { let program = compile_rss_source( r#"use http; use ngx; -let response: map = http::client::request({"method": "GET", "url": "https://example.test/", "headers": {"accept": "application/json"}, "body": null}); -let status: int = response["status"]; -ngx::timer::at(0.05, |premature| null); +let mut request = http::request::new("GET", "https://example.test/"); +http::request::set_header(&mut request, "accept", "application/json"); +let response = http::client::request(request); +let status: int = http::response::status(response); +ngx::timer::at(0.05, |premature| ()); "#, ) .expect("merged catalog compiles HTTP + timer"); @@ -568,7 +570,7 @@ ngx::timer::at(0.05, |premature| null); fn pending_limit_rejects_the_next_registration() { let worker = WorkerRuntime::start().expect("worker runtime"); let program = compile_rss_source( - "use ngx;\nngx::timer::at(30, |premature| null);\nngx::timer::at(30, |premature| null);\n", + "use ngx;\nngx::timer::at(30, |premature| ());\nngx::timer::at(30, |premature| ());\n", ) .expect("compile"); let mut vm = Vm::new_shared(program); @@ -608,19 +610,19 @@ fn invalid_seconds_are_rejected_by_the_registration_host() { let mut worker = WorkerRuntime::start().expect("worker runtime"); for (source, needle) in [ ( - "use ngx;\nngx::timer::at(-1, |premature| null);\n", + "use ngx;\nngx::timer::at(-1, |premature| ());\n", "non-negative", ), ( - "use ngx;\nngx::timer::every(0, |premature| null);\n", + "use ngx;\nngx::timer::every(0, |premature| ());\n", "positive", ), ( - "use ngx;\nlet zero = 0.0;\nngx::timer::at(1.0 / zero, |premature| null);\n", + "use ngx;\nlet zero = 0.0;\nngx::timer::at(1.0 / zero, |premature| ());\n", "infinite", ), ( - "use ngx;\nlet zero = 0.0;\nngx::timer::at(zero / zero, |premature| null);\n", + "use ngx;\nlet zero = 0.0;\nngx::timer::at(zero / zero, |premature| ());\n", "NaN", ), ] { @@ -643,7 +645,7 @@ fn worker_shutdown_runs_pending_callbacks_with_premature_true_once() { shutdown_current(); let mut worker = WorkerRuntime::start().expect("worker runtime"); run_script( - "use ngx;\nngx::timer::at(30, |premature| if premature => { ngx::req::get_method(); null } else => { null });\n", + "use ngx;\nngx::timer::at(30, |premature| if premature => { ngx::req::get_method(); () } else => { () });\n", &worker, ) .expect("registration"); @@ -669,7 +671,7 @@ fn cancelled_waiting_callback_releases_its_async_step() { shutdown_current(); let mut worker = WorkerRuntime::start().expect("worker runtime"); run_script( - "use ngx;\nngx::timer::at(0.02, |premature| if true => { ngx::sleep_ms(30000); null } else => { null });\n", + "use ngx;\nngx::timer::at(0.02, |premature| if true => { ngx::sleep_ms(30000); () } else => { () });\n", &worker, ) .expect("registration"); diff --git a/tests/ngx_udp_socket.rs b/tests/ngx_udp_socket.rs index 67d7fe2..dcb09bf 100644 --- a/tests/ngx_udp_socket.rs +++ b/tests/ngx_udp_socket.rs @@ -328,16 +328,16 @@ fn oversized_datagram_is_rejected() { let peer = bind_echo_peer(); let port = peer.local_addr().unwrap().port(); let oversized = vec![b'x'; 64]; + let driver = Driver::new(compile(&format!( + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, 8);\n" + ))); let handle = thread::spawn(move || { let mut buf = [0u8; 16]; let (n, addr) = peer.recv_from(&mut buf).expect("recv ping"); assert_eq!(&buf[..n], b"ping"); peer.send_to(&oversized, addr).expect("send oversized"); }); - let status = Driver::new(compile(&format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, 8);\n" - ))) - .pump_until(Duration::from_secs(3)); + let status = driver.pump_until(Duration::from_secs(3)); handle.join().expect("peer thread"); assert_eq!(status, AccessStatus::Error500); } @@ -347,7 +347,7 @@ fn udp_echo_round_trip() { let peer = bind_echo_peer(); let port = peer.local_addr().unwrap().port(); let driver = Driver::new(compile(&format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, null);\nngx::socket::udp::close(sock);\n" + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, ());\nngx::socket::udp::close(sock);\n" ))); let echo = spawn_echo(peer, b"pong"); let status = driver.pump_until(Duration::from_secs(3)); @@ -359,11 +359,11 @@ fn udp_echo_round_trip() { fn receive_timeout_fails_the_request() { let peer = bind_echo_peer(); let port = peer.local_addr().unwrap().port(); + let driver = Driver::new(compile(&format!( + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 50);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::receive(sock, ());\n" + ))); let started = Instant::now(); - let status = Driver::new(compile(&format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 50);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::receive(sock, null);\n" - ))) - .pump_until(Duration::from_secs(3)); + let status = driver.pump_until(Duration::from_secs(3)); let elapsed = started.elapsed(); assert_eq!(status, AccessStatus::Error500); assert!( @@ -381,7 +381,7 @@ fn request_cleanup_cancels_a_pending_receive() { let peer = bind_echo_peer(); let port = peer.local_addr().unwrap().port(); let mut driver = Driver::new(compile(&format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 60000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::receive(sock, null);\n" + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 60000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::receive(sock, ());\n" ))); let started = Instant::now(); loop { @@ -411,7 +411,7 @@ fn worker_shutdown_drops_a_pending_receive() { let mut exec = worker .bind_access_exec( compile(&format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 60000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::receive(sock, null);\n" + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 60000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::receive(sock, ());\n" )), 1, ) @@ -444,7 +444,7 @@ fn bind_then_echo_round_trip() { let peer = bind_echo_peer(); let port = peer.local_addr().unwrap().port(); let driver = Driver::new(compile(&format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::bind(sock, \"127.0.0.1\", 0);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, null);\nngx::socket::udp::close(sock);\n" + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::bind(sock, \"127.0.0.1\", 0);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, ());\nngx::socket::udp::close(sock);\n" ))); let echo = spawn_echo(peer, b"pong"); let status = driver.pump_until(Duration::from_secs(3)); diff --git a/tests/shared_dict_config.rs b/tests/shared_dict_config.rs index 007e17e..edb8de0 100644 --- a/tests/shared_dict_config.rs +++ b/tests/shared_dict_config.rs @@ -58,7 +58,7 @@ fn shared_dict_hosts_compile_without_request_state() { r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "k", 1, null, null); +ngx::shared::set(d, "k", 1, (), ()); ngx::shared::get(d, "k"); "#, ) @@ -161,7 +161,7 @@ fn rss_shared_dict_and_access_script_nginx_t() { r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "Jim", "8", null, null); +ngx::shared::set(d, "Jim", "8", (), ()); "#, ); let script_q = nginx_quote_path(&script).unwrap(); diff --git a/tests/shared_dict_nginx.rs b/tests/shared_dict_nginx.rs index c03599f..3071c9f 100644 --- a/tests/shared_dict_nginx.rs +++ b/tests/shared_dict_nginx.rs @@ -7,6 +7,7 @@ use std::io::{Read, Write}; use std::net::{TcpListener, TcpStream}; use std::path::{Path, PathBuf}; use std::process::{Child, Command, Stdio}; +use std::sync::Mutex; use std::sync::atomic::{AtomicUsize, Ordering}; use std::thread; use std::time::Duration; @@ -18,6 +19,9 @@ use support::nginx::{ }; use support::temp::{OwnedTempDir, isolated_prefix}; +// Multi-worker servers compete for runtime initialization resources when started together. +static SERVER_TEST_LOCK: Mutex<()> = Mutex::new(()); + struct RunningNginx { child: Child, prefix: PathBuf, @@ -172,7 +176,7 @@ fn setup_real() -> (PathBuf, u16, OwnedTempDir, RunningNginx) { r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "Jim", "8", null, null); +ngx::shared::set(d, "Jim", "8", (), ()); ngx::header::set("X-Ok", "1"); ngx::exit(200); "#, @@ -181,9 +185,52 @@ ngx::exit(200); &scripts.join("get.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); let r: SharedGetResult = ngx::shared::get(d, "Jim"); -ngx::header::set("X-Value", r.value); +ngx::header::set("X-Value", sv_text(r.value)); ngx::exit(200); "#, ); @@ -191,9 +238,52 @@ ngx::exit(200); &scripts.join("incr.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); -let r: SharedIncrResult = ngx::shared::incr(d, "hits", 1, 0, null); -ngx::header::set("X-Value", ngx::encode_args({ n: r.value })); +let r: SharedIncrResult = ngx::shared::incr(d, "hits", 1, 0, ()); +ngx::header::set("X-Value", ngx::encode_args({ n: opt_number(r.value) })); ngx::exit(200); "#, ); @@ -201,9 +291,52 @@ ngx::exit(200); &scripts.join("read_hits.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); let r: SharedGetResult = ngx::shared::get(d, "hits"); -ngx::header::set("X-Value", ngx::encode_args({ n: r.value })); +ngx::header::set("X-Value", ngx::encode_args({ n: sv_number(r.value) })); ngx::exit(200); "#, ); @@ -211,24 +344,67 @@ ngx::exit(200); &scripts.join("types.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "b", true, null, 3); -ngx::shared::set(d, "i", 42, null, null); -ngx::shared::set(d, "f", 1.5, null, null); -ngx::shared::set(d, "s", "hi", null, null); +ngx::shared::set(d, "b", true, (), 3); +ngx::shared::set(d, "i", 42, (), ()); +ngx::shared::set(d, "f", 1.5, (), ()); +ngx::shared::set(d, "s", "hi", (), ()); let gb: SharedGetResult = ngx::shared::get(d, "b"); let gi: SharedGetResult = ngx::shared::get(d, "i"); let gf: SharedGetResult = ngx::shared::get(d, "f"); let gs: SharedGetResult = ngx::shared::get(d, "s"); -if gb.value { +if sv_bool(gb.value) { ngx::header::set("X-Bool", "1"); } else { ngx::header::set("X-Bool", "0"); } ngx::header::set("X-Flags", ngx::encode_args({ n: gb.flags })); -ngx::header::set("X-Int", ngx::encode_args({ n: gi.value })); -ngx::header::set("X-Float", ngx::encode_args({ n: gf.value })); -ngx::header::set("X-Str", gs.value); +ngx::header::set("X-Int", ngx::encode_args({ n: sv_number(gi.value) })); +ngx::header::set("X-Float", ngx::encode_args({ n: sv_float(gf.value) })); +ngx::header::set("X-Str", sv_text(gs.value)); ngx::exit(200); "#, ); @@ -236,20 +412,63 @@ ngx::exit(200); &scripts.join("add_replace.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); -let add1: SharedStoreResult = ngx::shared::add(d, "once", "a", null, null); -let add2: SharedStoreResult = ngx::shared::add(d, "once", "b", null, null); -let rep: SharedStoreResult = ngx::shared::replace(d, "once", "c", null, null); -let niladd: SharedStoreResult = ngx::shared::add(d, "nilk", null, null, null); +let add1: SharedStoreResult = ngx::shared::add(d, "once", "a", (), ()); +let add2: SharedStoreResult = ngx::shared::add(d, "once", "b", (), ()); +let rep: SharedStoreResult = ngx::shared::replace(d, "once", "c", (), ()); +let niladd: SharedStoreResult = ngx::shared::add(d, "nilk", (), (), ()); if add1.ok { ngx::header::set("X-Add1", "1"); } else { ngx::header::set("X-Add1", "0"); } -ngx::header::set("X-Add2", add2.error); +ngx::header::set("X-Add2", opt_text(add2.error)); let replaced: SharedGetResult = ngx::shared::get(d, "once"); -ngx::header::set("X-Rep", replaced.value); -ngx::header::set("X-Nil", niladd.error); +ngx::header::set("X-Rep", sv_text(replaced.value)); +ngx::header::set("X-Nil", opt_text(niladd.error)); ngx::exit(200); "#, ); @@ -257,10 +476,53 @@ ngx::exit(200); &scripts.join("ttl.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "ttlkey", "v", 30, null); +ngx::shared::set(d, "ttlkey", "v", 30, ()); let t: SharedNumberResult = ngx::shared::ttl(d, "ttlkey"); -ngx::header::set("X-Ttl", ngx::encode_args({ n: t.value })); +ngx::header::set("X-Ttl", ngx::encode_args({ n: opt_number(t.value) })); ngx::exit(200); "#, ); @@ -280,14 +542,57 @@ ngx::exit(200); &scripts.join("cats.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let dogs = ngx::shared::dict("dogs"); let cats = ngx::shared::dict("cats"); -ngx::shared::set(dogs, "k", "woof", null, null); -ngx::shared::set(cats, "k", "meow", null, null); +ngx::shared::set(dogs, "k", "woof", (), ()); +ngx::shared::set(cats, "k", "meow", (), ()); let dog: SharedGetResult = ngx::shared::get(dogs, "k"); let cat: SharedGetResult = ngx::shared::get(cats, "k"); -ngx::header::set("X-Dogs", dog.value); -ngx::header::set("X-Cats", cat.value); +ngx::header::set("X-Dogs", sv_text(dog.value)); +ngx::header::set("X-Cats", sv_text(cat.value)); ngx::exit(200); "#, ); @@ -295,9 +600,52 @@ ngx::exit(200); &scripts.join("empty.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); -let r: SharedStoreResult = ngx::shared::set(d, "", "x", null, null); -ngx::header::set("X-Err", r.error); +let r: SharedStoreResult = ngx::shared::set(d, "", "x", (), ()); +ngx::header::set("X-Err", opt_text(r.error)); ngx::exit(200); "#, ); @@ -336,6 +684,9 @@ ngx::exit(200); #[test] fn multi_worker_set_is_visible_on_get() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let (_nginx, port, _tmp, _running) = setup_real(); let (status, body) = http_get(port, "/set"); assert_eq!(status, 200, "set failed: {body}\n{}", error_log(&_tmp)); @@ -350,6 +701,9 @@ fn multi_worker_set_is_visible_on_get() { #[test] fn multi_worker_incr_is_atomic() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let (_nginx, port, _tmp, _running) = setup_real(); let n = 40; let done = AtomicUsize::new(0); @@ -372,6 +726,9 @@ fn multi_worker_incr_is_atomic() { #[test] fn reload_preserves_zone() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let (nginx, port, tmp, _running) = setup_real(); let (status, body) = http_get(port, "/set"); assert_eq!(status, 200, "set failed: {body}"); @@ -407,6 +764,9 @@ fn reload_preserves_zone() { #[test] fn production_shm_covers_types_flags_add_replace_ttl_space_two_zones_and_empty_key() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let (_nginx, port, tmp, _running) = setup_real(); let (status, body) = http_get(port, "/types"); assert_eq!(status, 200, "types failed: {body}\n{}", error_log(&tmp)); @@ -471,6 +831,9 @@ fn production_shm_covers_types_flags_add_replace_ttl_space_two_zones_and_empty_k #[test] fn production_shm_stale_ttl_and_tiny_zone_lru() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let tmp = isolated_prefix("sd-tiny"); @@ -484,7 +847,7 @@ fn production_shm_stale_ttl_and_tiny_zone_lru() { r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "keep", "alive", null, null); +ngx::shared::set(d, "keep", "alive", (), ()); ngx::shared::set(d, "k", "v", 0.05, 9); ngx::exit(200); "#, @@ -493,6 +856,49 @@ ngx::exit(200); &scripts.join("stale_get.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} ngx::update_time(); let d = ngx::shared::dict("dogs"); let live: SharedGetResult = ngx::shared::get(d, "k"); @@ -503,7 +909,7 @@ if stale.stale { } else { ngx::header::set("X-Stale", "0"); } -ngx::header::set("X-Val", stale.value); +ngx::header::set("X-Val", sv_text(stale.value)); ngx::header::set("X-Flags", ngx::encode_args({ n: stale.flags })); ngx::exit(200); "#, @@ -512,21 +918,64 @@ ngx::exit(200); &scripts.join("fill.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("tiny"); let blob = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; -ngx::shared::set(d, "a", blob, null, null); -ngx::shared::set(d, "b", blob, null, null); -ngx::shared::set(d, "c", blob, null, null); -ngx::shared::set(d, "d", blob, null, null); -ngx::shared::set(d, "e", blob, null, null); -ngx::shared::set(d, "f", blob, null, null); -let safe: SharedSafeStoreResult = ngx::shared::safe_set(d, "final", blob, null, null); +ngx::shared::set(d, "a", blob, (), ()); +ngx::shared::set(d, "b", blob, (), ()); +ngx::shared::set(d, "c", blob, (), ()); +ngx::shared::set(d, "d", blob, (), ()); +ngx::shared::set(d, "e", blob, (), ()); +ngx::shared::set(d, "f", blob, (), ()); +let safe: SharedSafeStoreResult = ngx::shared::safe_set(d, "final", blob, (), ()); if safe.ok { ngx::header::set("X-Ok", "1"); } else { ngx::header::set("X-Ok", "0"); } -ngx::header::set("X-Safe", safe.error); +ngx::header::set("X-Safe", opt_text(safe.error)); ngx::exit(200); "#, ); @@ -613,6 +1062,9 @@ http {{ #[test] fn two_workers_list_flush_keys_reload_and_pressure() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let tmp = isolated_prefix("shared-dict-list-nginx"); @@ -624,11 +1076,54 @@ fn two_workers_list_flush_keys_reload_and_pressure() { &scripts.join("push.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); ngx::shared::lpush(d, "q", "left"); ngx::shared::rpush(d, "q", "right"); let len: SharedNumberResult = ngx::shared::llen(d, "q"); -ngx::header::set("X-Len", ngx::encode_args({ n: len.value })); +ngx::header::set("X-Len", ngx::encode_args({ n: opt_number(len.value) })); ngx::header::set("X-Worker", ngx::encode_args({ n: ngx::worker::id() })); ngx::exit(200); "#, @@ -637,13 +1132,56 @@ ngx::exit(200); &scripts.join("pop.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); let left: SharedGetResult = ngx::shared::lpop(d, "q"); let right: SharedGetResult = ngx::shared::rpop(d, "q"); -ngx::header::set("X-Left", left.value); -ngx::header::set("X-Right", right.value); +ngx::header::set("X-Left", sv_text(left.value)); +ngx::header::set("X-Right", sv_text(right.value)); let len: SharedNumberResult = ngx::shared::llen(d, "q"); -ngx::header::set("X-Len", ngx::encode_args({ n: len.value })); +ngx::header::set("X-Len", ngx::encode_args({ n: opt_number(len.value) })); ngx::header::set("X-Worker", ngx::encode_args({ n: ngx::worker::id() })); ngx::exit(200); "#, @@ -652,12 +1190,55 @@ ngx::exit(200); &scripts.join("conflict.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "scalar", "keep", null, null); +ngx::shared::set(d, "scalar", "keep", (), ()); let push: SharedNumberResult = ngx::shared::lpush(d, "scalar", "x"); let got: SharedGetResult = ngx::shared::get(d, "scalar"); -ngx::header::set("X-Err", push.error); -ngx::header::set("X-Val", got.value); +ngx::header::set("X-Err", opt_text(push.error)); +ngx::header::set("X-Val", sv_text(got.value)); ngx::exit(200); "#, ); @@ -665,12 +1246,55 @@ ngx::exit(200); &scripts.join("list_get.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); ngx::shared::rpush(d, "lst", "a"); let got: SharedGetResult = ngx::shared::get(d, "lst"); -ngx::header::set("X-Err", got.error); +ngx::header::set("X-Err", opt_text(got.error)); let list_len: SharedNumberResult = ngx::shared::llen(d, "lst"); -ngx::header::set("X-Len", ngx::encode_args({ n: list_len.value })); +ngx::header::set("X-Len", ngx::encode_args({ n: opt_number(list_len.value) })); ngx::exit(200); "#, ); @@ -681,9 +1305,9 @@ use ngx; let d = ngx::shared::dict("dogs"); ngx::shared::flush_all(d); ngx::shared::flush_expired(d, 0); -ngx::shared::set(d, "a", "1", null, null); -ngx::shared::set(d, "b", "2", null, null); -ngx::shared::set(d, "c", "3", null, null); +ngx::shared::set(d, "a", "1", (), ()); +ngx::shared::set(d, "b", "2", (), ()); +ngx::shared::set(d, "c", "3", (), ()); ngx::shared::rpush(d, "lst2", "x"); ngx::exit(200); "#, @@ -692,6 +1316,49 @@ ngx::exit(200); &scripts.join("get_keys.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); let keys = ngx::shared::get_keys(d, 0); ngx::header::set("X-N", ngx::encode_args({ n: keys.length })); @@ -702,11 +1369,11 @@ ngx::header::set("X-3", keys[3]); let ga: SharedGetResult = ngx::shared::get(d, "a"); let gb2: SharedGetResult = ngx::shared::get(d, "b"); let gc: SharedGetResult = ngx::shared::get(d, "c"); -ngx::header::set("X-A", ga.value); -ngx::header::set("X-B", gb2.value); -ngx::header::set("X-C", gc.value); +ngx::header::set("X-A", sv_text(ga.value)); +ngx::header::set("X-B", sv_text(gb2.value)); +ngx::header::set("X-C", sv_text(gc.value)); let lst_len: SharedNumberResult = ngx::shared::llen(d, "lst2"); -ngx::header::set("X-Lst", ngx::encode_args({ n: lst_len.value })); +ngx::header::set("X-Lst", ngx::encode_args({ n: opt_number(lst_len.value) })); ngx::exit(200); "#, ); @@ -715,7 +1382,7 @@ ngx::exit(200); r#" use ngx; let d = ngx::shared::dict("dogs"); -ngx::shared::set(d, "live", "1", 1, null); +ngx::shared::set(d, "live", "1", 1, ()); ngx::shared::flush_all(d); let n = ngx::shared::flush_expired(d, 0); let live: SharedGetResult = ngx::shared::get(d, "live"); @@ -742,10 +1409,53 @@ ngx::exit(200); &scripts.join("expire_list_get.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} ngx::update_time(); let d = ngx::shared::dict("dogs"); let n: SharedNumberResult = ngx::shared::llen(d, "exp"); -ngx::header::set("X-Len", ngx::encode_args({ n: n.value })); +ngx::header::set("X-Len", ngx::encode_args({ n: opt_number(n.value) })); ngx::exit(200); "#, ); @@ -753,6 +1463,49 @@ ngx::exit(200); &scripts.join("list_fill.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("tiny"); let blob = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; let a: SharedNumberResult = ngx::shared::lpush(d, "q", blob); @@ -761,7 +1514,7 @@ let c: SharedNumberResult = ngx::shared::lpush(d, "q", blob); let d2: SharedNumberResult = ngx::shared::lpush(d, "q", blob); let e: SharedNumberResult = ngx::shared::lpush(d, "q", blob); let last: SharedNumberResult = ngx::shared::lpush(d, "q", blob); -ngx::header::set("X-Err", last.error); +ngx::header::set("X-Err", opt_text(last.error)); ngx::exit(200); "#, ); @@ -769,11 +1522,54 @@ ngx::exit(200); &scripts.join("reload_check.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); let n: SharedNumberResult = ngx::shared::llen(d, "reload"); -ngx::header::set("X-Len", ngx::encode_args({ n: n.value })); +ngx::header::set("X-Len", ngx::encode_args({ n: opt_number(n.value) })); let popped: SharedGetResult = ngx::shared::lpop(d, "reload"); -ngx::header::set("X-Pop", popped.value); +ngx::header::set("X-Pop", sv_text(popped.value)); ngx::exit(200); "#, ); @@ -986,11 +1782,54 @@ http {{ &scripts.join("reload_check.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); let n: SharedNumberResult = ngx::shared::llen(d, "reload"); -ngx::header::set("X-Len", ngx::encode_args({ n: n.value })); +ngx::header::set("X-Len", ngx::encode_args({ n: opt_number(n.value) })); let popped: SharedGetResult = ngx::shared::lpop(d, "reload"); -ngx::header::set("X-Pop", popped.value); +ngx::header::set("X-Pop", sv_text(popped.value)); ngx::exit(200); "#, ); @@ -1072,7 +1911,7 @@ fn rss_seed_numbered(dict: &str, prefix: &str, count: usize, exptime: &str) -> S let mut body = format!("use ngx;\nlet d = ngx::shared::dict(\"{dict}\");\n"); for i in 0..count { body.push_str(&format!( - "ngx::shared::set(d, \"{prefix}{i:04}\", 1, {exptime}, null);\n" + "ngx::shared::set(d, \"{prefix}{i:04}\", 1, {exptime}, ());\n" )); } body.push_str("ngx::exit(200);\n"); @@ -1081,6 +1920,9 @@ fn rss_seed_numbered(dict: &str, prefix: &str, count: usize, exptime: &str) -> S #[test] fn shm_get_keys_flush_all_visit_caps_and_list_pop() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let nginx = nginx_binary(); let module = module_cdylib(); let tmp = isolated_prefix("shared-dict-shm-caps"); @@ -1090,7 +1932,7 @@ fn shm_get_keys_flush_all_visit_caps_and_list_pop() { write_script( &scripts.join("seed_many.rss"), - &rss_seed_numbered("dogs", "k", 1100, "null"), + &rss_seed_numbered("dogs", "k", 1100, "()"), ); write_script( &scripts.join("flush_all.rss"), @@ -1115,15 +1957,58 @@ ngx::exit(200); &scripts.join("ends_probe.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); let k0: SharedGetResult = ngx::shared::get(d, "k0000"); let kn: SharedGetResult = ngx::shared::get(d, "k1099"); -if k0.value == null { +if !sv_some(k0.value.copy()) { ngx::header::set("X-K0", "missing"); } else { ngx::header::set("X-K0", "1"); } -if kn.value == null { +if !sv_some(kn.value.copy()) { ngx::header::set("X-KN", "missing"); } else { ngx::header::set("X-KN", "1"); @@ -1140,7 +2025,7 @@ ngx::exit(200); r#" use ngx; let d = ngx::shared::dict("visit"); -ngx::shared::set(d, "live", "1", null, null); +ngx::shared::set(d, "live", "1", (), ()); ngx::exit(200); "#, ); @@ -1148,26 +2033,69 @@ ngx::exit(200); &scripts.join("get_keys_visit.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} ngx::update_time(); let d = ngx::shared::dict("visit"); let keys = ngx::shared::get_keys(d, 10); ngx::header::set("X-N", ngx::encode_args({ n: keys.length })); let live: SharedGetResult = ngx::shared::get(d, "live"); -ngx::header::set("X-Live", live.value); +ngx::header::set("X-Live", sv_text(live.value)); ngx::exit(200); "#, ); let mut flush_expired_seed = String::from( - "use ngx;\nlet d = ngx::shared::dict(\"cats\");\nngx::shared::set(d, \"expired-lru\", 0, 5, null);\n", + "use ngx;\nlet d = ngx::shared::dict(\"cats\");\nngx::shared::set(d, \"expired-lru\", 0, 5, ());\n", ); for i in 0..1024 { flush_expired_seed.push_str(&format!( - "ngx::shared::set(d, \"live{i:04}\", 1, null, null);\n" + "ngx::shared::set(d, \"live{i:04}\", 1, (), ());\n" )); } for i in 0..5 { flush_expired_seed.push_str(&format!( - "ngx::shared::set(d, \"expired-mru-{i}\", 2, 5, null);\n" + "ngx::shared::set(d, \"expired-mru-{i}\", 2, 5, ());\n" )); } flush_expired_seed.push_str("ngx::exit(200);\n"); @@ -1179,12 +2107,55 @@ ngx::exit(200); &scripts.join("flush_expired_visit.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} ngx::update_time(); let d = ngx::shared::dict("cats"); let n = ngx::shared::flush_expired(d, 10); let stale: SharedGetResult = ngx::shared::get_stale(d, "expired-mru-4"); ngx::header::set("X-Freed", ngx::encode_args({ n: n })); -if stale.value == null { +if !sv_some(stale.value.copy()) { ngx::header::set("X-Stale", "missing"); } else { ngx::header::set("X-Stale", "present"); @@ -1196,18 +2167,61 @@ ngx::exit(200); &scripts.join("list_pop_ok.rss"), r#" use ngx; + +fn sv_text(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn sv_number(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} + +fn sv_some(value: Option) -> bool { + match value { + Option::Some(_) => true, + Option::None => false + } +} + +fn sv_bool(value: Option) -> bool { + match value { + Option::Some(entry) => entry.bool_value.unwrap_or(false), + Option::None => false + } +} + +fn sv_float(value: Option) -> number { + match value { + Option::Some(entry) => entry.float_value.unwrap_or(0.0), + Option::None => 0.0 + } +} + +fn opt_text(value: Option) -> string { + value.unwrap_or("") +} + +fn opt_number(value: Option) -> number { + value.unwrap_or(0) +} let d = ngx::shared::dict("dogs"); ngx::shared::rpush(d, "decode", "keep"); ngx::shared::rpush(d, "decode", "tail"); let popped: SharedGetResult = ngx::shared::lpop(d, "decode"); let rest: SharedNumberResult = ngx::shared::llen(d, "decode"); -ngx::header::set("X-Pop", popped.value); -if popped.error == null { +ngx::header::set("X-Pop", sv_text(popped.value)); +if opt_text(popped.error.copy()) == "" { ngx::header::set("X-Err", "none"); } else { ngx::header::set("X-Err", "err"); } -ngx::header::set("X-Len", ngx::encode_args({ n: rest.value })); +ngx::header::set("X-Len", ngx::encode_args({ n: opt_number(rest.value) })); ngx::exit(200); "#, ); diff --git a/tests/socket_lifecycle.rs b/tests/socket_lifecycle.rs index 6b0c352..5dcab34 100644 --- a/tests/socket_lifecycle.rs +++ b/tests/socket_lifecycle.rs @@ -214,13 +214,11 @@ fn dns_setpeername_echo_round_trip() { assert!(resolved.ip().is_loopback()); let peer = bind_echo_peer(&resolved.to_string()); let port = peer.local_addr().unwrap().port(); + let driver = Driver::new(compile(&format!( + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"localhost\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, ());\nngx::socket::udp::close(sock);\n" + ))); let echo = spawn_echo(peer, b"ping", b"pong"); - let status = pump( - &format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"localhost\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, null);\nngx::socket::udp::close(sock);\n" - ), - Duration::from_secs(3), - ); + let status = driver.pump_until(Duration::from_secs(3)); echo.join().expect("echo thread"); assert_eq!(status, AccessStatus::Ok); } @@ -229,13 +227,11 @@ fn dns_setpeername_echo_round_trip() { fn ipv6_bind_setpeername_echo_round_trip() { let peer = bind_echo_peer("[::1]:0"); let port = peer.local_addr().unwrap().port(); + let driver = Driver::new(compile(&format!( + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::bind(sock, \"::1\", 0);\nngx::socket::udp::setpeername(sock, \"::1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, ());\nngx::socket::udp::close(sock);\n" + ))); let echo = spawn_echo(peer, b"ping", b"pong"); - let status = pump( - &format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::bind(sock, \"::1\", 0);\nngx::socket::udp::setpeername(sock, \"::1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, null);\nngx::socket::udp::close(sock);\n" - ), - Duration::from_secs(3), - ); + let status = driver.pump_until(Duration::from_secs(3)); echo.join().expect("echo thread"); assert_eq!(status, AccessStatus::Ok); } @@ -250,7 +246,7 @@ fn two_sockets_may_pend_independently() { let echo_right = spawn_echo(right, b"R", b"r"); let status = pump( &format!( - "use ngx;\nlet a = ngx::socket::udp();\nlet b = ngx::socket::udp();\nngx::socket::udp::settimeout(a, 1000);\nngx::socket::udp::settimeout(b, 1000);\nngx::socket::udp::setpeername(a, \"127.0.0.1\", {left_port});\nngx::socket::udp::setpeername(b, \"127.0.0.1\", {right_port});\nngx::socket::udp::send(a, \"L\");\nngx::socket::udp::send(b, \"R\");\nngx::socket::udp::receive(a, null);\nngx::socket::udp::receive(b, null);\nngx::socket::udp::close(a);\nngx::socket::udp::close(b);\n" + "use ngx;\nlet a = ngx::socket::udp();\nlet b = ngx::socket::udp();\nngx::socket::udp::settimeout(a, 1000);\nngx::socket::udp::settimeout(b, 1000);\nngx::socket::udp::setpeername(a, \"127.0.0.1\", {left_port});\nngx::socket::udp::setpeername(b, \"127.0.0.1\", {right_port});\nngx::socket::udp::send(a, \"L\");\nngx::socket::udp::send(b, \"R\");\nngx::socket::udp::receive(a, ());\nngx::socket::udp::receive(b, ());\nngx::socket::udp::close(a);\nngx::socket::udp::close(b);\n" ), Duration::from_secs(3), ); @@ -265,7 +261,7 @@ fn request_cleanup_closes_the_udp_fd() { let port = probe.local_addr().unwrap().port(); drop(probe); let mut driver = Driver::new(compile(&format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 60000);\nngx::socket::udp::bind(sock, \"127.0.0.1\", {port});\nngx::socket::udp::receive(sock, null);\n" + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 60000);\nngx::socket::udp::bind(sock, \"127.0.0.1\", {port});\nngx::socket::udp::receive(sock, ());\n" ))); driver.wait_until_waiting(Duration::from_secs(2)); assert_eq!(driver.worker.in_flight().unwrap(), 1); @@ -279,7 +275,7 @@ fn request_cleanup_cancels_a_pending_receive() { let peer = bind_echo_peer("127.0.0.1:0"); let port = peer.local_addr().unwrap().port(); let mut driver = Driver::new(compile(&format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 60000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::receive(sock, null);\n" + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 60000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::receive(sock, ());\n" ))); driver.wait_until_waiting(Duration::from_secs(2)); assert_eq!(driver.worker.in_flight().unwrap(), 1); @@ -291,7 +287,7 @@ fn request_cleanup_cancels_a_pending_send_when_waiting() { let peer = bind_echo_peer("127.0.0.1:0"); let port = peer.local_addr().unwrap().port(); let mut driver = Driver::new(compile(&format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 60000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, null);\n" + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 60000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, ());\n" ))); let started = Instant::now(); loop { @@ -326,11 +322,11 @@ fn worker_runtimes_isolate_udp_sockets() { let echo_left = spawn_echo(left, b"A", b"a"); let echo_right = spawn_echo(right, b"B", b"b"); let left_status = Driver::new(compile(&format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {left_port});\nngx::socket::udp::send(sock, \"A\");\nngx::socket::udp::receive(sock, null);\nngx::socket::udp::close(sock);\n" + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {left_port});\nngx::socket::udp::send(sock, \"A\");\nngx::socket::udp::receive(sock, ());\nngx::socket::udp::close(sock);\n" ))) .pump_until(Duration::from_secs(3)); let right_status = Driver::new(compile(&format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {right_port});\nngx::socket::udp::send(sock, \"B\");\nngx::socket::udp::receive(sock, null);\nngx::socket::udp::close(sock);\n" + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {right_port});\nngx::socket::udp::send(sock, \"B\");\nngx::socket::udp::receive(sock, ());\nngx::socket::udp::close(sock);\n" ))) .pump_until(Duration::from_secs(3)); echo_left.join().expect("left echo"); @@ -393,13 +389,11 @@ fn unresolved_error_host_name_is_rejected() { #[test] fn dns_host_over_the_length_cap_is_rejected() { let host = "a".repeat(256); + let driver = Driver::new(compile(&format!( + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"{host}\", 53);\n" + ))); let started = Instant::now(); - let status = pump( - &format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"{host}\", 53);\n" - ), - Duration::from_secs(3), - ); + let status = driver.pump_until(Duration::from_secs(3)); let elapsed = started.elapsed(); assert_eq!(status, AccessStatus::Error500); assert!( @@ -416,7 +410,7 @@ fn timeout_zero_does_not_fire_a_timer() { let started = Instant::now(); let status = pump( &format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 0);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, null);\nngx::socket::udp::close(sock);\n" + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 0);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, ());\nngx::socket::udp::close(sock);\n" ), Duration::from_secs(3), ); @@ -443,18 +437,16 @@ fn oversized_datagram_names_the_host() { let peer = bind_echo_peer("127.0.0.1:0"); let port = peer.local_addr().unwrap().port(); let oversized = vec![b'x'; 64]; + let driver = Driver::new(compile(&format!( + "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, 8);\n" + ))); let handle = thread::spawn(move || { let mut buf = [0u8; 16]; let (n, addr) = peer.recv_from(&mut buf).expect("recv ping"); assert_eq!(&buf[..n], b"ping"); peer.send_to(&oversized, addr).expect("send oversized"); }); - let status = pump( - &format!( - "use ngx;\nlet sock = ngx::socket::udp();\nngx::socket::udp::settimeout(sock, 1000);\nngx::socket::udp::setpeername(sock, \"127.0.0.1\", {port});\nngx::socket::udp::send(sock, \"ping\");\nngx::socket::udp::receive(sock, 8);\n" - ), - Duration::from_secs(3), - ); + let status = driver.pump_until(Duration::from_secs(3)); handle.join().expect("peer thread"); assert_eq!(status, AccessStatus::Error500); } diff --git a/tests/support/hooks.rs b/tests/support/hooks.rs index 6a68546..9167299 100644 --- a/tests/support/hooks.rs +++ b/tests/support/hooks.rs @@ -238,11 +238,13 @@ unsafe extern "C" { ) -> *mut std::ffi::c_void; } +fn is_package_build_hash(name: &str) -> bool { + name.len() == PACKAGE_BUILD_HASH_LEN && name.bytes().all(|b| b.is_ascii_hexdigit()) +} + fn is_ngx_rustscript_package_build_dir(name: &str) -> bool { - let Some(rest) = name.strip_prefix(PACKAGE_BUILD_DIR_PREFIX) else { - return false; - }; - rest.len() == PACKAGE_BUILD_HASH_LEN && rest.bytes().all(|b| b.is_ascii_hexdigit()) + name.strip_prefix(PACKAGE_BUILD_DIR_PREFIX) + .is_some_and(is_package_build_hash) } fn parse_build_script_output(text: &str) -> Result<(u8, bool), String> { @@ -305,21 +307,39 @@ fn list_package_build_outputs(target_dir: &Path, profile: &str) -> Vec = if is_ngx_rustscript_package_build_dir(name) { + vec![entry.path()] + } else if name == "ngx-rustscript" { + fs::read_dir(entry.path()) + .into_iter() + .flatten() + .flatten() + .filter(|child| { + child + .file_name() + .to_str() + .is_some_and(is_package_build_hash) + }) + .map(|child| child.path()) + .collect() + } else { continue; }; - if !meta.is_file() { - continue; + for dir in dirs { + for output in [dir.join("output"), dir.join("run/stdout")] { + let Ok(meta) = fs::metadata(&output) else { + continue; + }; + if !meta.is_file() { + continue; + } + stamps.push(OutputStamp { + path: output, + modified: meta.modified().unwrap_or(SystemTime::UNIX_EPOCH), + len: meta.len(), + }); + } } - stamps.push(OutputStamp { - path: output, - modified: meta.modified().unwrap_or(SystemTime::UNIX_EPOCH), - len: meta.len(), - }); } stamps.sort_by(|a, b| a.path.cmp(&b.path)); stamps @@ -408,10 +428,10 @@ fn invoked_mtime(output_path: &Path) -> SystemTime { } fn cargo_ran_build_script(stderr: &str, stdout: &str) -> bool { - stderr - .lines() - .chain(stdout.lines()) - .any(|line| line.contains("Running") && line.contains("build-script-build")) + stderr.lines().chain(stdout.lines()).any(|line| { + line.contains("Running") + && (line.contains("/build-script-build") || line.contains("/build_script_build")) + }) } fn kill_process_group(pid: u32) { @@ -860,6 +880,67 @@ cargo::rustc-cfg=ngx_rustscript_integration_hooks ); } +#[test] +fn list_package_build_outputs_recognizes_both_cargo_layouts() { + let target = super::temp::unique_temp_dir("build-outputs"); + let build = target.join("debug/build"); + let old = build.join("ngx-rustscript-0123456789abcdef/output"); + let new = build.join("ngx-rustscript/0123456789ABCDEF/output"); + for path in [&old, &new] { + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, b"fingerprint").unwrap(); + } + for relative in [ + "ngx-rustscript-nginx-test-stubs-0123456789abcdef/output", + "ngx-rustscript-0123456789abcde/output", + "ngx-rustscript-0123456789abcdeg/output", + "ngx-rustscript/0123456789abcde/output", + "ngx-rustscript/0123456789abcdeg/output", + "ngx-rustscript/0123456789abcdef/out/output", + "ngx-rustscript-nginx-test-stubs/0123456789abcdef/output", + ] { + let path = build.join(relative); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, b"invalid").unwrap(); + } + + let stamps = list_package_build_outputs(&target, "debug"); + let paths: Vec<_> = stamps.iter().map(|stamp| &stamp.path).collect(); + assert_eq!(paths, vec![&new, &old]); + for stamp in stamps { + let metadata = fs::metadata(&stamp.path).unwrap(); + assert_eq!(stamp.modified, metadata.modified().unwrap()); + assert_eq!(stamp.len, metadata.len()); + } +} + +#[test] +fn list_package_build_outputs_recognizes_cargo_run_stdout() { + let target = super::temp::unique_temp_dir("build-run-stdout"); + let output = target.join("debug/build/ngx-rustscript/0123456789abcdef/run/stdout"); + fs::create_dir_all(output.parent().unwrap()).unwrap(); + fs::write(&output, b"fingerprint").unwrap(); + let stamps = list_package_build_outputs(&target, "debug"); + assert_eq!(stamps.len(), 1); + assert_eq!(stamps[0].path, output); +} + +#[test] +fn detects_cargo_build_script_executable_names() { + assert!(cargo_ran_build_script( + "Running `/target/debug/build/ngx-rustscript-0123456789abcdef/build-script-build`", + "" + )); + assert!(cargo_ran_build_script( + "Running `/target/debug/build/ngx-rustscript/0123456789abcdef/out/build_script_build`", + "" + )); + assert!(!cargo_ran_build_script( + "Running `rustc --crate-name build_script_build build.rs`", + "" + )); +} + #[test] fn package_build_dir_name_rejects_stubs_and_short_hashes() { assert!(is_ngx_rustscript_package_build_dir( diff --git a/tests/test_release_packaging.py b/tests/test_release_packaging.py index 4d60f01..74c166c 100644 --- a/tests/test_release_packaging.py +++ b/tests/test_release_packaging.py @@ -89,7 +89,7 @@ def directory_payload(path: Path) -> dict[str, bytes]: } -FROZEN_RUSTSCRIPT_REV = "87d8c5d5cb42b3381aecadb383c0b7bf34ff204f" +FROZEN_RUSTSCRIPT_REV = "fc8b0c8179640a8ffe460a64b3690e35c2f7eb73" STALE_RUSTSCRIPT_PINS = ( "f5f71eb", "f5f71ebc2f38e276c35f2aebaf63f4e2780efe99", diff --git a/tests/timer_nginx.rs b/tests/timer_nginx.rs index 9a303ef..45a650a 100644 --- a/tests/timer_nginx.rs +++ b/tests/timer_nginx.rs @@ -7,18 +7,21 @@ //! reload isolation, and graceful worker shutdown. //! //! All cases use the shared typed harness (`tests/support/nginx_harness.rs`) -//! and run serially with `--test-threads=1`. Each case owns an isolated prefix +//! and serialize their server fixtures. Each case owns an isolated prefix //! and process group; the harness Drop removes both. mod support; use std::io::{Read, Write}; use std::net::TcpListener; +use std::sync::Mutex; use std::thread; use std::time::{Duration, Instant}; use support::nginx_harness::{HttpRequest, NginxConfig, NginxHarness, RunningNginx}; +static SERVER_TEST_LOCK: Mutex<()> = Mutex::new(()); + const ERROR_LOG: &str = "logs/error.log"; const READY: Duration = Duration::from_secs(8); const QUIET: Duration = Duration::from_secs(8); @@ -184,6 +187,9 @@ fn location(path: &str, script: &str) -> String { #[test] fn timer_at_fires_after_the_creating_request_completed() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let harness = NginxHarness::new("timer-nginx-at-fire"); write_script( &harness, @@ -193,11 +199,11 @@ use ngx; let d = ngx::shared::dict("timers"); ngx::timer::at(5, |premature| if true => { let d = ngx::shared::dict("timers"); - ngx::shared::set(d, "at_fired", "yes", null, null); + ngx::shared::set(d, "at_fired", "yes", (), ()); ngx::log(ngx::log_level::error(), "TIMER_AT_FIRED"); - null -} else => { null }); -ngx::shared::set(d, "creating_done", "yes", null, null); + () +} else => { () }); +ngx::shared::set(d, "creating_done", "yes", (), ()); assert(ngx::timer::pending_count() == 1); ngx::log(ngx::log_level::error(), "DIAG pending_count is 1 after registration"); ngx::exit(200); @@ -208,9 +214,23 @@ ngx::exit(200); "scripts/read_fired.rss", r#" use ngx; + +fn text_of(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn number_of(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} let d = ngx::shared::dict("timers"); let r: SharedGetResult = ngx::shared::get(d, "at_fired"); -ngx::say(r.value); +ngx::say(text_of(r.value)); "#, ); let locations = format!( @@ -249,6 +269,9 @@ ngx::say(r.value); #[test] fn timer_at_passes_premature_false_on_normal_expiry() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let harness = NginxHarness::new("timer-nginx-premature-false"); write_script( &harness, @@ -257,10 +280,10 @@ fn timer_at_passes_premature_false_on_normal_expiry() { use ngx; ngx::timer::at(0.05, |premature| if premature => { ngx::log(ngx::log_level::error(), "TIMER_PREMATURE_TRUE"); - null + () } else => { ngx::log(ngx::log_level::error(), "TIMER_PREMATURE_FALSE"); - null + () }); ngx::exit(200); "#, @@ -281,6 +304,9 @@ ngx::exit(200); #[test] fn timer_callback_resumes_after_ngx_sleep() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let harness = NginxHarness::new("timer-nginx-sleep"); write_script( &harness, @@ -290,8 +316,8 @@ use ngx; ngx::timer::at(0.05, |premature| if true => { ngx::sleep_ms(40); ngx::log(ngx::log_level::error(), "TIMER_SLEPT"); - null -} else => { null }); + () +} else => { () }); ngx::exit(200); "#, ); @@ -305,6 +331,9 @@ ngx::exit(200); #[test] fn timer_callback_performs_outbound_async_work() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let listener = TcpListener::bind("127.0.0.1:0").expect("echo listener"); let port = listener.local_addr().expect("listener addr").port(); let echo = thread::spawn(move || { @@ -329,8 +358,8 @@ ngx::timer::at(0.05, |premature| if true => {{ ngx::socket::tcp::send(s, "ping"); ngx::socket::tcp::close(s); ngx::log(ngx::log_level::error(), "TIMER_TCP_OK"); - null -}} else => {{ null }}); + () +}} else => {{ () }}); ngx::exit(200); "# ), @@ -346,6 +375,9 @@ ngx::exit(200); #[test] fn timer_callback_cannot_use_request_hosts_and_keeps_serving() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let harness = NginxHarness::new("timer-nginx-denied"); write_script( &harness, @@ -354,8 +386,8 @@ fn timer_callback_cannot_use_request_hosts_and_keeps_serving() { use ngx; ngx::timer::at(0.05, |premature| if true => { ngx::req::get_method(); - null -} else => { null }); + () +} else => { () }); ngx::exit(200); "#, ); @@ -374,6 +406,9 @@ ngx::exit(200); #[test] fn timer_every_serializes_rounds_with_a_persistent_capture() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let harness = NginxHarness::new("timer-nginx-every"); write_script( &harness, @@ -384,9 +419,9 @@ let mut rounds = 0; ngx::timer::every(0.05, |premature| if true => { rounds = rounds + 1; let d = ngx::shared::dict("timers"); - ngx::shared::set(d, "rounds", rounds, null, null); - null -} else => { null }); + ngx::shared::set(d, "rounds", rounds, (), ()); + () +} else => { () }); ngx::exit(200); "#, ); @@ -395,9 +430,23 @@ ngx::exit(200); "scripts/read_rounds.rss", r#" use ngx; + +fn text_of(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn number_of(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} let d = ngx::shared::dict("timers"); let r: SharedGetResult = ngx::shared::get(d, "rounds"); -if r.value == 3 { ngx::say("ROUNDS_3"); } else { ngx::say("ROUNDS_OTHER"); } +if number_of(r.value) == 3 { ngx::say("ROUNDS_3"); } else { ngx::say("ROUNDS_OTHER"); } "#, ); let locations = format!( @@ -430,6 +479,9 @@ if r.value == 3 { ngx::say("ROUNDS_3"); } else { ngx::say("ROUNDS_OTHER"); } #[test] fn timer_every_never_overlaps_a_slow_callback() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let harness = NginxHarness::new("timer-nginx-no-overlap"); write_script( &harness, @@ -440,8 +492,8 @@ ngx::timer::every(0.05, |premature| if true => { ngx::log(ngx::log_level::error(), "SLOW_START"); ngx::sleep_ms(120); ngx::log(ngx::log_level::error(), "SLOW_END"); - null -} else => { null }); + () +} else => { () }); ngx::exit(200); "#, ); @@ -471,6 +523,9 @@ ngx::exit(200); #[test] fn timer_counts_change_at_registration_start_and_completion() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let harness = NginxHarness::new("timer-nginx-counts"); write_script( &harness, @@ -481,8 +536,8 @@ ngx::timer::at(0.2, |premature| if true => { assert(ngx::timer::running_count() == 1); assert(ngx::timer::pending_count() == 0); ngx::log(ngx::log_level::error(), "TIMER_COUNTS_OK"); - null -} else => { null }); + () +} else => { () }); assert(ngx::timer::pending_count() == 1); assert(ngx::timer::running_count() == 0); ngx::exit(200); @@ -498,6 +553,9 @@ ngx::exit(200); #[test] fn reload_leaves_old_workers_with_their_timers_and_new_workers_empty() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let harness = NginxHarness::new("timer-nginx-reload"); write_script( &harness, @@ -506,10 +564,10 @@ fn reload_leaves_old_workers_with_their_timers_and_new_workers_empty() { use ngx; ngx::timer::at(30, |premature| if premature => { ngx::log(ngx::log_level::error(), "TIMER_RELOAD_PREMATURE_TRUE"); - null + () } else => { ngx::log(ngx::log_level::error(), "TIMER_RELOAD_PREMATURE_FALSE"); - null + () }); ngx::exit(200); "#, @@ -565,6 +623,9 @@ ngx::exit(200); #[test] fn timer_callback_uses_the_worker_semaphore_family() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let harness = NginxHarness::new("timer-nginx-semaphore"); write_script( &harness, @@ -584,10 +645,10 @@ ngx::timer::at(0.05, |premature| if true => { let timed_out = ngx::semaphore::wait(sem, 0.05); assert(timed_out == false); let d = ngx::shared::dict("timers"); - ngx::shared::set(d, "semaphore_ok", "yes", null, null); + ngx::shared::set(d, "semaphore_ok", "yes", (), ()); ngx::log(ngx::log_level::error(), "TIMER_SEMAPHORE_OK"); - null -} else => { null }); + () +} else => { () }); ngx::exit(200); "#, ); @@ -609,6 +670,9 @@ ngx::exit(200); #[test] fn graceful_worker_exit_runs_a_pending_every_round_once_with_premature_true() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let harness = NginxHarness::new("timer-nginx-every-exit"); write_script( &harness, @@ -617,10 +681,10 @@ fn graceful_worker_exit_runs_a_pending_every_round_once_with_premature_true() { use ngx; ngx::timer::every(30, |premature| if premature => { ngx::log(ngx::log_level::error(), "TIMER_EVERY_EXIT_PREMATURE_TRUE"); - null + () } else => { ngx::log(ngx::log_level::error(), "TIMER_EVERY_EXIT_PREMATURE_FALSE"); - null + () }); ngx::exit(200); "#, @@ -658,6 +722,9 @@ ngx::exit(200); #[test] fn timer_counts_track_registration_waiting_completion_and_every_rearm() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let harness = NginxHarness::new("timer-nginx-counts-boundaries"); write_script( &harness, @@ -668,24 +735,24 @@ let d = ngx::shared::dict("timers"); ngx::timer::every(0.6, |premature| if true => { let d = ngx::shared::dict("timers"); if ngx::timer::running_count() == 1 && ngx::timer::pending_count() == 0 { - ngx::shared::set(d, "round_start_counts", "ok", null, null); + ngx::shared::set(d, "round_start_counts", "ok", (), ()); } else { - ngx::shared::set(d, "round_start_counts", "bad", null, null); + ngx::shared::set(d, "round_start_counts", "bad", (), ()); } ngx::log(ngx::log_level::error(), "TIMER_COUNTS_ROUND_START"); ngx::sleep_ms(300); if ngx::timer::running_count() == 1 && ngx::timer::pending_count() == 0 { - ngx::shared::set(d, "waiting_counts", "ok", null, null); + ngx::shared::set(d, "waiting_counts", "ok", (), ()); } else { - ngx::shared::set(d, "waiting_counts", "bad", null, null); + ngx::shared::set(d, "waiting_counts", "bad", (), ()); } ngx::log(ngx::log_level::error(), "TIMER_COUNTS_ROUND_DONE"); - null -} else => { null }); + () +} else => { () }); if ngx::timer::pending_count() == 1 && ngx::timer::running_count() == 0 { - ngx::shared::set(d, "registered_counts", "ok", null, null); + ngx::shared::set(d, "registered_counts", "ok", (), ()); } else { - ngx::shared::set(d, "registered_counts", "bad", null, null); + ngx::shared::set(d, "registered_counts", "bad", (), ()); } ngx::exit(200); "#, @@ -695,13 +762,27 @@ ngx::exit(200); "scripts/observe_counts.rss", r#" use ngx; + +fn text_of(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn number_of(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} let d = ngx::shared::dict("timers"); let registered: SharedGetResult = ngx::shared::get(d, "registered_counts"); let round_start: SharedGetResult = ngx::shared::get(d, "round_start_counts"); let waiting: SharedGetResult = ngx::shared::get(d, "waiting_counts"); -ngx::say(registered.value); -ngx::say(round_start.value); -ngx::say(waiting.value); +ngx::say(text_of(registered.value)); +ngx::say(text_of(round_start.value)); +ngx::say(text_of(waiting.value)); if ngx::timer::pending_count() == 1 { if ngx::timer::running_count() == 0 { ngx::say("P1R0"); @@ -770,6 +851,9 @@ if ngx::timer::pending_count() == 1 { #[test] fn http_standard_import_and_ngx_timer_run_together_through_the_registry() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let listener = TcpListener::bind("127.0.0.1:0").expect("upstream listener"); let port = listener.local_addr().expect("listener addr").port(); let upstream = thread::spawn(move || { @@ -796,20 +880,15 @@ use ngx; use http; ngx::timer::at(0.05, |premature| if true => {{ let d = ngx::shared::dict("timers"); - ngx::shared::set(d, "timer_fired", "yes", null, null); + ngx::shared::set(d, "timer_fired", "yes", (), ()); ngx::log(ngx::log_level::error(), "TIMER_HTTP_IMPORT_FIRED"); - null -}} else => {{ null }}); -let response: map = http::client::request({{ - method: "GET", - url: "http://127.0.0.1:{port}/source", - headers: [ - {{ name: "accept", value: "text/plain" }}, - {{ name: "accept", value: "application/json" }} - ], - body: null -}}); -let status: int = response["status"]; + () +}} else => {{ () }}); +let mut request = http::request::new("GET", "http://127.0.0.1:{port}/source"); +http::request::set_header(&mut request, "accept", "text/plain"); +http::request::set_header(&mut request, "accept", "application/json"); +let response = http::client::request(request); +let status: int = http::response::status(response); assert(status == 200); ngx::exit(200); "# @@ -820,9 +899,23 @@ ngx::exit(200); "scripts/read_http_timer.rss", r#" use ngx; + +fn text_of(value: Option) -> string { + match value { + Option::Some(entry) => entry.text_value.unwrap_or(""), + Option::None => "" + } +} + +fn number_of(value: Option) -> number { + match value { + Option::Some(entry) => entry.int_value.unwrap_or(0), + Option::None => 0 + } +} let d = ngx::shared::dict("timers"); let r: SharedGetResult = ngx::shared::get(d, "timer_fired"); -ngx::say(r.value); +ngx::say(text_of(r.value)); "#, ); let locations = format!( @@ -859,6 +952,9 @@ ngx::say(r.value); #[test] fn timer_callback_does_not_inherit_the_registering_location_http_policy() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let harness = NginxHarness::new("timer-nginx-callback-http-policy"); write_script( &harness, @@ -867,10 +963,11 @@ fn timer_callback_does_not_inherit_the_registering_location_http_policy() { use ngx; use http; ngx::timer::at(0.05, |premature| if true => { - let response: map = http::client::request({"method": "GET", "url": "http://127.0.0.1:9/source", "body": null}); + let mut request = http::request::new("GET", "http://127.0.0.1:9/source"); + let response = http::client::request(request); ngx::log(ngx::log_level::error(), "TIMER_CALLBACK_HTTP_ALLOWED"); - null -} else => { null }); + () +} else => { () }); ngx::exit(200); "#, ); @@ -906,6 +1003,9 @@ ngx::exit(200); #[test] fn graceful_worker_exit_runs_pending_callbacks_with_premature_true() { + let _guard = SERVER_TEST_LOCK + .lock() + .unwrap_or_else(|err| err.into_inner()); let harness = NginxHarness::new("timer-nginx-exit"); write_script( &harness, @@ -914,10 +1014,10 @@ fn graceful_worker_exit_runs_pending_callbacks_with_premature_true() { use ngx; ngx::timer::at(30, |premature| if premature => { ngx::log(ngx::log_level::error(), "TIMER_EXIT_PREMATURE_TRUE"); - null + () } else => { ngx::log(ngx::log_level::error(), "TIMER_EXIT_PREMATURE_FALSE"); - null + () }); ngx::exit(200); "#, diff --git a/tests/vm_sleep.rs b/tests/vm_sleep.rs index ef879da..0b66484 100644 --- a/tests/vm_sleep.rs +++ b/tests/vm_sleep.rs @@ -46,10 +46,11 @@ fn wait_readable(fd: RawFd, timeout: Duration) -> bool { false } -fn run_waiting_program_to_halt(program: Arc, timeout: Duration) { +fn run_waiting_program_to_halt(program: Arc, timeout: Duration) -> Duration { let mut worker = WorkerRuntime::start().unwrap(); let mut exec = worker.bind_access_exec(program, 1).unwrap(); let mut registry = IdentityRegistry::with_capacity(DEFAULT_MAX_IN_FLIGHT); + let started = Instant::now(); assert_eq!( exec.handle_access(&mut registry, &worker), AccessStatus::Again, @@ -66,7 +67,9 @@ fn run_waiting_program_to_halt(program: Arc, timeout: Duration) { ReadyEffect::Halted ); assert_eq!(exec.handle_access(&mut registry, &worker), AccessStatus::Ok); + let elapsed = started.elapsed(); worker.shutdown(); + elapsed } #[test] @@ -77,7 +80,7 @@ fn catalog_and_wrapper_are_macro_sleep_ms() { assert_eq!(schema.params.len(), 1); assert_eq!(schema.params[0].name, "ms"); assert_eq!(schema.params[0].ty, HostTypeSchema::Int); - assert_eq!(schema.return_type, HostTypeSchema::Null); + assert_eq!(schema.return_type, HostTypeSchema::Unit); let mut worker = WorkerRuntime::start().unwrap(); let mut exec = worker.bind_access_exec(sleep_ms_once(1), 1).unwrap(); let mut registry = IdentityRegistry::with_capacity(DEFAULT_MAX_IN_FLIGHT); @@ -98,7 +101,7 @@ fn catalog_sleep_is_seconds_number() { assert_eq!(schema.params.len(), 1); assert_eq!(schema.params[0].name, "seconds"); assert_eq!(schema.params[0].ty, HostTypeSchema::Number); - assert_eq!(schema.return_type, HostTypeSchema::Null); + assert_eq!(schema.return_type, HostTypeSchema::Unit); } #[test] @@ -183,12 +186,10 @@ fn two_consecutive_async_calls() { #[test] fn sleep_seconds_float_executes_through_real_vm() { - let started = Instant::now(); - run_waiting_program_to_halt( + let elapsed = run_waiting_program_to_halt( compile("use ngx;\nngx::sleep(0.03);\n"), Duration::from_secs(2), ); - let elapsed = started.elapsed(); assert!( elapsed >= Duration::from_millis(20), "ngx::sleep(0.03) should wait about 30ms, elapsed {elapsed:?}"