Add plain WAT WASI 0.2 curl clone (curl-wasm)

Implement a curl clone directly in plain WebAssembly Text format (curl.wat) targeting WASI 0.2 (wasi:cli, wasi:io, wasi:clocks, wasi:filesystem, wasi:http). Includes the executable curl-wasm wrapper script, Makefile, integration test suite (tests/test_curl.py), README.md, and .gitignore.
This commit is contained in:
Sprite committed 2026-11-19 22:50:14 +00:00
1 parent 21c7dbe0a4
commit c265fd1adf
6 files changed
+4464

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
# curl-wasm
A `curl` clone implemented **100% in plain WebAssembly Text format (`curl.wat`)**, targeting the **WASI 0.2 Component Model** (`wasi:cli`, `wasi:io`, `wasi:clocks`, `wasi:filesystem`, `wasi:http`).
There is no Rust, C, Go, or JavaScript anywhere in the implementation — `curl.wat` assembles directly into a standalone WASI 0.2 component (`curl.wasm`) with `wasm-tools` and executes HTTP and HTTPS (TLS) transfers via `wasmtime`.
## Build & Run
Prerequisites:
- `wasm-tools`
- `wasmtime` (with WASI 0.2 / `wasi:http` support)
```sh
make build
./curl-wasm https://example.com
```
Or run `curl.wasm` directly with `wasmtime` (note the `--` so `wasmtime` does not intercept flags like `-O`, `-D`, `-h`, or `-V`):
```sh
wasmtime run -S http --dir=. -- ./curl.wasm -fsSI https://example.com
```
Run the test suite:
```sh
make test
```
## Supported Options
| Option | Description |
| --- | --- |
| `-h`, `--help` | Show usage help and exit |
| `-V`, `--version` | Show version/protocols and exit |
| `-X`, `--request <method>` | Custom HTTP method (`GET`, `POST`, `PATCH`, `DELETE`, custom verbs) |
| `-H`, `--header <header>` | Add/override header (`Name: Value`), suppress (`Name:`), or send empty (`Name;`) |
| `-d`, `--data`, `--data-ascii <data>` | Send request body (default `POST`, `@file` / `@-` strips CR/LF) |
| `--data-raw <data>` | Send literal request body (`@` not treated as file) |
| `--data-binary <data>` | Send raw binary body (`@file` / `@-` preserves bytes) |
| `--data-urlencode <data>` | URL-encode data (`content`, `=content`, `name=content`, `@file`, `name@file`) |
| `-G`, `--get` | Append `-d` data to URL query string and use `GET` |
| `-T`, `--upload-file <file>` | Upload local file (or `-` for stdin) via `PUT` |
| `-i`, `--include` | Include response status line and headers in output |
| `-I`, `--head` | Fetch headers only (`HEAD` request) |
| `-L`, `--location` | Follow `301`/`302`/`303`/`307`/`308` redirects |
| `--max-redirs <num>` | Maximum redirects allowed (default `50`, `-1` unlimited) |
| `-o`, `--output <file>` | Write body to `<file>` (`-` for stdout, `/dev/null` discard sink) |
| `-O`, `--remote-name` | Write output to local file named from remote URL path |
| `-D`, `--dump-header <file>` | Write response headers to `<file>` (or `-` for stdout) |
| `-s`, `--silent` | Silent mode (suppress error messages) |
| `-S`, `--show-error` | Show errors even when `-s` is enabled |
| `-v`, `--verbose` | Print `*` info, `>` request headers, and `<` response headers to stderr |
| `-f`, `--fail` | Fail silently (exit non-zero, no body/file written) on HTTP status >= 400 |
| `-u`, `--user <user:pass>` | Send `Authorization: Basic ...` header |
| `-A`, `--user-agent <str>` | Set custom `User-Agent` |
| `-e`, `--referer <url>` | Set `Referer` header |
| `-b`, `--cookie <data>` | Set `Cookie` header |
| `-r`, `--range <range>` | Set `Range: bytes=<range>` header |
| `-w`, `--write-out <fmt>` | Print transfer stats template after completion |
| `--connect-timeout <sec>` | Connection timeout in seconds |
| `-m`, `--max-time <sec>` | Total transfer timeout in seconds |
| `--` | End of options |
### `--write-out` Variables
`%{http_code}`, `%{response_code}`, `%{size_download}`, `%{size_upload}`, `%{time_total}`, `%{url_effective}`, `%{content_type}`, `%{redirect_url}`, `%{num_redirects}`, `%{scheme}`, `%{method}`, `\n`, `\r`, `\t`, `\\`, `%%`.