Specifications / Runtime libraries

HTTP

Runtime surface [BUILTIN-HTTP]

The native runtime provides an HTTP server and two client request surfaces. Handles and status codes are int; a negative value reports a runtime or transport failure. The target contract is Result<T, Error>; the int handle and status builtins are legacy, so check each builtin's actual failure form.

Server [HTTP-SERVER]

Request handlers receive the method, path, raw headers, and body. They return the built-in HttpResponse record:

type HttpResponse = {
    status: int,
    headers: string,
    contentType: string,
    streamFd: int,
    isComplete: bool,
    partialBody: string
}

httpCreateServer(port: int, address: string) -> Result<int, Error>
httpListen(serverID: int,
           handler: fn(string, string, string, string) -> HttpResponse) -> Result<int, Error>
httpStopServer(serverID: int) -> Result<int, Error>

httpCreateServer answers a positive handle. httpListen and httpStopServer answer 0. A failed bind or an invalid handle is an Error, so a program cannot claim to be serving without looking. The runtime uses status, headers, and partialBody to form the response and computes Content-Length from the body. The other record fields remain required by the callback ABI but do not affect response transport.

The listener rejects malformed framing, transfer encoding, timeouts, and oversized requests before invoking the handler. It drains response headers and the body with retry-on-interrupt writes.

Client gone [HTTP-SERVER-CLIENT-GONE]

httpClientGone() -> int

A handler that works for a long time (a model generating a turn) asks whether its client still waits. httpClientGone() answers 1 once the client of the request this thread is serving has closed or reset its connection, and 0 while it is still connected or when called outside a handler. It reads nothing from the connection: a byte the client pipelined stays for the listener. The listener serves one request at a time, in the order the kernel accepted them, so a handler that stops early lets the next waiting request start.

Status-only client [HTTP-STATUS-CLIENT]

httpCreateClient(baseUrl: string, timeoutMilliseconds: int) -> Result<int, Error>
httpGet(clientID: int, path: string, headers: string) -> Result<int, Error>
httpPost(clientID: int, path: string, body: string, headers: string) -> Result<int, Error>
httpPut(clientID: int, path: string, body: string, headers: string) -> Result<int, Error>
httpDelete(clientID: int, path: string, headers: string) -> Result<int, Error>
httpCloseClient(clientID: int) -> Result<int, Error>

Each request answers the HTTP status code, such as 200 or 404, and an Error on transport failure. These calls discard the response body and headers.

Response handles [HTTP-RESPONSE-HANDLE]

httpGetResponse(clientID: int, path: string, headers: string)
    -> Result<int, Error>
httpPostResponse(clientID: int, path: string, body: string, headers: string)
    -> Result<int, Error>
httpResponseStatus(responseID: int) -> int
httpResponseBody(responseID: int) -> Result<string, Error>
httpResponseHeader(responseID: int, name: string) -> Result<string, Error>
httpResponseFree(responseID: int) -> Result<int, Error>

httpGetResponse retains one GET response and returns its opaque handle. httpPostResponse does the same for a POST, including non-success HTTP responses; the caller inspects the status before interpreting the body. Transport failures return Error, independently of the HTTP status. httpResponseBody returns a copy, header lookup is case-insensitive, and chunked bodies are decoded before they are returned. Every successful handle must be released once with httpResponseFree; an invalid handle or double free returns Error.

Chunked responses [HTTP-CHUNKED-DECODE]

A response is chunked when chunked is the final coding of its Transfer-Encoding header, compared without case sensitivity. Repeated header lines are one list, and an empty list element names no coding, so chunked, is chunked and a later line holding only commas changes nothing. Any other response is returned as received, subject to [HTTP-RESPONSE-FRAMING].

The client decodes a chunked body strictly and treats the framing as untrusted:

  • A chunk size is one or more hex digits, optionally followed by spaces or tabs and ; extensions, and ends with CRLF. No sign, 0x prefix or leading whitespace is accepted, and an extension holds no control byte except a tab, so a bare CR or LF can't hide framing inside it.
  • A size that overflows, or that exceeds the bytes remaining in the response, is malformed. No copy is ever made from a size that has not passed this check.
  • Each chunk's data is followed by CRLF.
  • The body ends with a zero-size chunk, any trailer lines, and a blank line. Trailers are discarded, and bytes after the blank line are never read. A trailer line holds no control byte except a tab.

httpGetResponse and httpPostResponse answer malformed framing with Error, whose message begins http response: chunked body: and names the fault. No partial body is returned, no response handle is created, and the client stays usable.

Response framing [HTTP-RESPONSE-FRAMING]

These rules apply to every response to httpGetResponse and httpPostResponse, chunked or not. A refusal is an Error whose message begins http response:, no response handle is created, and the client stays usable.

  • NUL in the response head is refused with head: contains NUL. Chunk sizes and trailers also reject NUL as malformed framing. The returned body preserves every framed payload byte, including NUL, for fixed-length, chunked and close-delimited responses.
  • A status is exactly three digits, the first not zero, followed by a space, CR or the end of the bytes. Anything else, or any other protocol than HTTP/1.0 or HTTP/1.1, is refused: status line: not HTTP/1.x.
  • Interim 1xx responses are skipped, so the status, headers and framing are the final response's. 101 is final. The status-only builtins report the final status too. An interim head is held to the same rules as a final one, so a malformed one can't move the end of the head into the body.
  • A head that never reaches its blank line is refused: head: truncated.
  • A head that could hide framing from the client's header scan is refused: head: malformed. That is a control byte other than a tab, a bare CR or LF, a folded line, a line with no colon or no name, or a space before the colon.
  • Transfer-Encoding naming a coding on an HTTP/1.0 response is faulty framing (RFC 9112 6.1), even with a Content-Length and on a status with no body: head: Transfer-Encoding on HTTP/1.0. An interim response's head isn't held to this, as it has no body to frame.
  • 101, 204 and 304 responses end at the blank line after their headers (RFC 9112 6.3). Their body is empty, whatever their headers say and whatever follows.
  • A response that isn't chunked is framed by its Content-Length, when it has one. Fewer bytes are refused (body: truncated), and bytes past it are dropped. A value that isn't digits, or lines that disagree, are refused (body: invalid Content-Length). Any Transfer-Encoding coding overrides Content-Length (RFC 9112 6.3), which is then ignored.
  • Otherwise a body ends when the peer closes the connection in good order. A read that fails or times out first is refused (body: truncated), and so is a TLS connection that ends without its close_notify, as a TCP close can be forged on the wire. A plain TCP close carries no such proof: a body cut short by one can't be told from a whole one.

The client stops reading at the end of the framing: the blank line of a status with no body, the last byte of a Content-Length, the blank line after a zero chunk's trailers. A server that holds the socket open costs nothing, and bytes past that point are never read, so nothing in them is refused. The client also stops as soon as the framing is one it will refuse. Only a body that ends with the close waits for one. This applies to the status-only builtins too, and so do these two limits:

  • A response, heads and framing included, is limited to 64 MiB (HTTP_MAX_RESPONSE_BYTES). Past that it is refused with code -15: size: over the limit. The whole response is buffered, so the limit is what bounds the memory a server can make the client use: at most twice that while it reads, and the decoded copy of a chunked body after.
  • The whole read is limited to 10 times the client's timeout, or 10 minutes for a timeout of 0, which leaves a single read unlimited. Past that it is refused with code -16: read: over the time limit. The timeout alone applies to each wait for bytes, so a server sending one byte just inside it would otherwise never end. A slow response that finishes inside the limit is only slow. The limit applies inside a TLS record too: reads don't block, so a dripped record can't outlast it. It covers reading the response, not connecting or sending the request, whose reads and writes are limited only by the timeout each.