Hew packages

hew.net.http

Actor-owned HTTP/1.1 server with typed errors and one-request handling

Use version 0.7.0-rc2

hew add hew.net.http --version 0.7.0-rc2
Declared Hew requirement
>=0.10.0, <0.12.0
Licence
MIT OR Apache-2.0
Published
2026-11-12T18:48:28.203Z

Dependencies

No package dependencies.

Package README

# hew.net.http

`hew.net.http` is a small HTTP/1.1 server with actor-owned listener and
connection lifecycles plus typed errors. Each `accept_one` call waits for one
request for up to 250 milliseconds; the package does not hide a loop or promise
an unavailable request generator.

```hew
import hew.net.http;

fn main() {
    let server = spawn http.Server { addr: "127.0.0.1:8080" };
    var attempts_left = 120;
    var served = false;
    while attempts_left > 0 && !served {
        attempts_left = attempts_left - 1;
        match server.accept_one() {
            .Ok(request) => {
                println(f"{request.method()} {request.path()}");
                match server.respond_text(200, "Hello from Hew!\n") {
                    .Ok(_) => {}
                    .Err(.Failed(error)) => println(http.error_message(error)),
                    .Err(_) => println("HTTP server actor stopped before replying"),
                }
                served = true;
            }
            // An idle quarter second is the documented outcome of
            // accept_one(), so it is a retry rather than a failure.
            .Err(.Failed(.Accept(_))) => {}
            .Err(.Failed(error)) => {
                println(http.error_message(error));
                served = true;
            }
            .Err(_) => {
                println("HTTP server actor stopped before replying");
                served = true;
            }
        }
    }
    stop(server);
    stopped(server);
}
```

The retry loop is not ceremony: without it the 250-millisecond timeout fires
before anyone can send a request, so the only outcome a reader could ever
observe is the `Accept` error.

The checked example at [`examples/hello.hew`](examples/hello.hew) is this
program with the loop moved into `serve_one`, which returns the request line,
plus the listening banner and a message for the case where the thirty seconds
elapse unused. Its test serves one request while
`std.net.http.client` sends `GET /hello`, then checks the body the client
received and the request line the server saw. Run it from the ecosystem
checkout:

```sh
hew run --pkg-path . net/http/examples/hello.hew
```

then, from a second terminal within those thirty seconds:

```sh
curl http://127.0.0.1:8080/hello
```

The server prints `GET /hello` and exits; `curl` prints `Hello from Hew!`. With
no request at all it prints `no request arrived within 30 seconds` and still
exits successfully.

`server.local_addr()` binds the actor's listener if needed and returns its
actual local address. A successful call establishes readiness without
accepting a request. For an OS-assigned port, spawn with `addr: "127.0.0.1:0"`
and use the address it returns. Later calls and `accept_one()` use that same
owned listener; a bind failure returns `HttpError.Listen`. The example's test
uses this readiness call and an assigned port, so it does not depend on port
8080 being free. Its checked client retains HTTP status and transport-error
details when a request fails.

Every `Server` method that can fail is declared `-> T fails HttpError`, so a
call replies `Result<T, ActorError<HttpError>>`: the handler's error arrives as
`.Err(.Failed(error))`, and any other `.Err` means the actor did not reply (for
example `.Dead` after `stop`). The actor retains
each accepted connection until a response method or actor stop releases it.
It also releases listener and request handles from its stop hook, including
failure and caller-abandonment paths.

Only one request may be pending in the actor at a time. Call one of the
response methods before accepting another request. Stop the server when it is
done handling requests. An idle
`accept_one()` returns an `Accept` error after 250 milliseconds, which keeps a
queued actor stop from being trapped behind a blocking socket call;
servers normally retry `accept_one()` after that timeout.

Header and response methods return `HttpError.NoPending` when the server is
open but no request is awaiting a response.

`server.header(name)` performs a case-insensitive lookup on the pending
request and fails with `HttpError.MissingHeader(name)` when the header is
absent. An empty header value is therefore distinct from a missing header.
Incoming HTTP/1.1 requests must contain exactly one valid `Host` header.
Response methods accept final status codes from 200 through 599; informational
responses are rejected because this one-response API cannot send the required
later final response.

Strings cross the native ABI as managed Hew string values carrying an exact
byte length, so embedded NUL bytes survive in both directions: a request body
containing NUL is delivered whole, and a response body containing NUL is
transmitted without truncation. HTTP header values containing NUL are still
rejected, incoming and outgoing, because NUL is not a valid HTTP field value
character.

`server.url_decode(text)` decodes percent escapes as UTF-8 and converts
`+` to a space. Malformed escapes and decoded bytes that are not UTF-8 return
`HttpError.Decode`. `server.form_value(body, key)` applies the same
strict decoding to URL-encoded form fields; a missing key returns
`HttpError.MissingFormField` rather than an empty-string sentinel.