Files

93 lines
4.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`) are not found locally (e.g. Pixi/Conda packages that omit headers), `just native` automatically fetches the version-matched runtime files from `WebAssembly/wabt` into `build/`, or honors `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`, `\\`, `%%`.
## Compile to a static ELF (`wasm2elf`, spike)
`just elf` builds `./curl-elf`, a ~1 MB static x86-64 Linux executable with no libc, no
runtime and no OpenSSL, using [wasm2elf-mb](https://git.site.latha.org/nandi/wasm2elf-mb).
`elf/flatten.py` merges the component's allocator module and core module into one core
module; `elf/shim.wat` replaces the canon-lowered `wasi:*` imports with hand-written WAT
that calls Linux syscalls through a few `sys::*` intrinsics (wasm2elf gained those plus bulk-memory
`memory.copy`/`memory.fill` upstream; the revision is pinned in `elf/wasm2elf.rev`).
The shim speaks HTTP/1.0 over IPv4 TCP with a minimal UDP DNS resolver (nameserver from
`/etc/resolv.conf`). `just test-elf` passes 26 of 27 tests; the failure is
`live_https_example_com`: **`https://` is not supported** (no TLS stack).