Files
curl-wasm/README.md
T
Sprite dba2cbcdca Make wasm2c native build portably discover runtime and OpenSSL
Add native/build_native.py to auto-discover WABT's wasm2c runtime headers and sources (wasm-rt.h, wasm-rt-*.c, and .inc files, with WASM2C_RUNTIME_DIR override) and OpenSSL flags across Linux distros and macOS, instead of assuming /usr/include/wasm-rt.h and -lwasm-rt-impl. Guard _DARWIN_C_SOURCE vs _POSIX_C_SOURCE in native/wasi_host.c, delegate just native to native/build_native.py, and document WASM2C_RUNTIME_DIR in README.md.
2026-11-20 00:22:20 +00:00

80 lines
3.7 KiB
Markdown

# 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
```
## Compile to Native (`wasm2c`)
You can also compile `curl.wasm` into a standalone native binary (`./curl-native`, backed by POSIX sockets and OpenSSL) using `wasm2c` via `just`:
```sh
just native
just run-native -fsSI https://example.com
just test-native
```
If WABT's `wasm2c` runtime sources (`wasm-rt.h` and `wasm-rt-impl.c`) live in a non-standard path, set `WASM2C_RUNTIME_DIR=/path/to/wabt/wasm2c`.
## 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`, `\\`, `%%`.