Zeroserve Script Create
Overview
Zeroserve is a high-performance, scriptable HTTP server that uses io_uring and eBPF. It
serves a static website from a tarball, and optionally runs eBPF request scripts.
It supports HTTP, HTTPS, hot reload, a small templating pass for text responses, and
an opt-in reverse proxy from scripts.
This skill creates a Zeroserve request script in C from requirements, using the SDK helpers
and eBPF constraints. Output is a single .c file ready to place under .zeroserve/scripts/.
It includes JSON helper usage for parsing structured inputs from headers or params.
Basic zeroserve usage
Serve a prebuilt tarball:
zeroserve --addr 0.0.0.0:8080 site.tar
Package a directory into a tarball and serve it:
# Create a tarball from the current directory
zeroserve --pack . > site.tar
# Serve it
zeroserve --addr 0.0.0.0:8080 site.tar
Packaging a site: Zeroserve expects a tarball whose root corresponds to the site root.
Use --pack to build one from a directory:
zeroserve --pack ./public > site.tar
Packaging notes:
- All regular files under the directory are added to the tarball.
- Request scripts live under
.zeroserve/scripts/.
- Any
.c file in .zeroserve/scripts/ is compiled to an .o eBPF object.
The resulting .o is included in the tarball and the .c is omitted.
- If a
.c and .o share the same name, the .o is skipped in favor of recompiling.
- Script compilation requires
clang and llc on your PATH.
If you want the SDK header without packing:
zeroserve --dump-sdk > zeroserve.h
Workflow
- Gather requirements
- Trigger: path, method, header, query param, peer, or scheme.
- Action: log, mutate URI/headers, set metadata, respond, or reverse proxy.
- Short-circuit: confirm if the script should terminate the chain with
zs_respond or zs_reverse_proxy.
- Choose a base
- Start from
assets/script_template.c for a new script.
- Keep the entry signature and
ZS_ENTRY section if editing an existing script.
- Implement logic
- Read request data with
zs_req_* helpers into fixed buffers; clamp lengths before use.
- Use
ZS_STR("literal") for helper calls needing (ptr, len) for string literals.
- For JSON parsing, use
zs_json_parse/zs_json_get/zs_json_array_get and
zs_json_read_*, then free handles with zs_object_free (handle table is limited).
- To parse the request body as JSON, use
zs_req_body_json() which returns a handle
(-1 on empty body, body > 256KB, or invalid JSON). The body is read lazily and cached.
- To build JSON dynamically, use
zs_json_new_object/zs_json_new_array and modify
with zs_json_set, zs_json_array_push, zs_json_set_string, zs_json_set_i64, etc.
- To send a JSON response, use
zs_json_respond(status, handle) which auto-sets Content-Type.
- To parse a static JSON file from the tarball, call
zs_load_static_json(path, path_len)
and treat the returned handle like any other JSON handle.
- To read tarball entry metadata as JSON, call
zs_load_file_metadata(path, path_len)
and access size, etag, and mtime.
- For response headers, set metadata keys
zs.response.header.<name>.
- To rate limit, use
zs_rate_limit(key, key_len, per_second, per_minute, per_hour);
it returns a result code indicating allowed or which limit was exceeded.
- Call
zs_respond, zs_json_respond, or zs_reverse_proxy to stop later scripts.
- Validate eBPF constraints
- Avoid unbounded loops and recursion.
- Keep stack usage small (BPF stack is limited).
- Deliver result
- Provide the
.c file and note it should live under .zeroserve/scripts/.
- Remind that
zeroserve --pack compiles .c to .o automatically.
- If needed, dump the SDK header with
zeroserve --dump-sdk to inspect the full API.
References
references/sdk_api.md for the SDK helper list and notes.
references/scripting_behavior.md for execution order, short-circuit rules, and packaging.
references/examples.md for common patterns (logging, health response, reverse proxy, templating).
assets/script_template.c for a starter skeleton.
1---2name: zeroserve-script-create3description: Create Zeroserve eBPF request-processing scripts in C for `.zeroserve/scripts` using the Zeroserve SDK (`zeroserve.h`). Use when you need to implement request inspection, header/query parsing, metadata templating, custom responses, or reverse-proxy behavior in a script.4---56# Zeroserve Script Create78## Overview910Zeroserve is a high-performance, scriptable HTTP server that uses `io_uring` and eBPF. It11serves a static website from a tarball, and optionally runs eBPF request scripts.12It supports HTTP, HTTPS, hot reload, a small templating pass for text responses, and13an opt-in reverse proxy from scripts.1415This skill creates a Zeroserve request script in C from requirements, using the SDK helpers16and eBPF constraints. Output is a single `.c` file ready to place under `.zeroserve/scripts/`.17It includes JSON helper usage for parsing structured inputs from headers or params.1819## Basic zeroserve usage2021Serve a prebuilt tarball:2223```bash24zeroserve --addr 0.0.0.0:8080 site.tar25```2627Package a directory into a tarball and serve it:2829```bash30# Create a tarball from the current directory31zeroserve --pack . > site.tar3233# Serve it34zeroserve --addr 0.0.0.0:8080 site.tar35```3637Packaging a site: Zeroserve expects a tarball whose root corresponds to the site root.38Use `--pack` to build one from a directory:3940```bash41zeroserve --pack ./public > site.tar42```4344Packaging notes:45- All regular files under the directory are added to the tarball.46- Request scripts live under `.zeroserve/scripts/`.47- Any `.c` file in `.zeroserve/scripts/` is compiled to an `.o` eBPF object.48 The resulting `.o` is included in the tarball and the `.c` is omitted.49- If a `.c` and `.o` share the same name, the `.o` is skipped in favor of recompiling.50- Script compilation requires `clang` and `llc` on your `PATH`.5152If you want the SDK header without packing:5354```bash55zeroserve --dump-sdk > zeroserve.h56```5758## Workflow59601. Gather requirements61 - Trigger: path, method, header, query param, peer, or scheme.62 - Action: log, mutate URI/headers, set metadata, respond, or reverse proxy.63 - Short-circuit: confirm if the script should terminate the chain with `zs_respond` or `zs_reverse_proxy`.642. Choose a base65 - Start from `assets/script_template.c` for a new script.66 - Keep the entry signature and `ZS_ENTRY` section if editing an existing script.673. Implement logic68 - Read request data with `zs_req_*` helpers into fixed buffers; clamp lengths before use.69 - Use `ZS_STR("literal")` for helper calls needing `(ptr, len)` for string literals.70 - For JSON parsing, use `zs_json_parse`/`zs_json_get`/`zs_json_array_get` and71 `zs_json_read_*`, then free handles with `zs_object_free` (handle table is limited).72 - To parse the request body as JSON, use `zs_req_body_json()` which returns a handle73 (-1 on empty body, body > 256KB, or invalid JSON). The body is read lazily and cached.74 - To build JSON dynamically, use `zs_json_new_object`/`zs_json_new_array` and modify75 with `zs_json_set`, `zs_json_array_push`, `zs_json_set_string`, `zs_json_set_i64`, etc.76 - To send a JSON response, use `zs_json_respond(status, handle)` which auto-sets Content-Type.77 - To parse a static JSON file from the tarball, call `zs_load_static_json(path, path_len)`78 and treat the returned handle like any other JSON handle.79 - To read tarball entry metadata as JSON, call `zs_load_file_metadata(path, path_len)`80 and access `size`, `etag`, and `mtime`.81 - For response headers, set metadata keys `zs.response.header.<name>`.82 - To rate limit, use `zs_rate_limit(key, key_len, per_second, per_minute, per_hour)`;83 it returns a result code indicating allowed or which limit was exceeded.84 - Call `zs_respond`, `zs_json_respond`, or `zs_reverse_proxy` to stop later scripts.854. Validate eBPF constraints86 - Avoid unbounded loops and recursion.87 - Keep stack usage small (BPF stack is limited).885. Deliver result89 - Provide the `.c` file and note it should live under `.zeroserve/scripts/`.90 - Remind that `zeroserve --pack` compiles `.c` to `.o` automatically.91 - If needed, dump the SDK header with `zeroserve --dump-sdk` to inspect the full API.9293## References9495- `references/sdk_api.md` for the SDK helper list and notes.96- `references/scripting_behavior.md` for execution order, short-circuit rules, and packaging.97- `references/examples.md` for common patterns (logging, health response, reverse proxy, templating).98- `assets/script_template.c` for a starter skeleton.