http.server.new

http.server

new(config, handle, app) -> server, err

Create a new HTTP server instance

Creates and configures an HTTP server. The handle function is called for each request with (method, query, args, headers, body, context) and must return content, status, response_headers. The args string is the request's query string without the leading ?. The optional app table is an opaque application context passed to the handler as context.app — the place for process-lifetime state such as stores and caches.

In place of the body string, a handler may return a producer, a table:

The server sends the head with transfer-encoding: chunked and no content-length, then each piece as one chunk, then the last chunk; the connection stays open for the next request. After each piece it lets the loop run, even when the send did not block, so a long body does not stall other connections. A streamed body is never compressed. An HTTP/1.0 client gets the pieces unframed with connection: close, and the closed connection ends the body. A stream that fails closes the connection. A 1xx, 204 or 304 status carries no body: the producer is closed unasked.

A handler that knows the length of its stream sets content-length beside the producer. The server then sends that header, no transfer-encoding, and the pieces unframed, to an HTTP/1.0 client too. It stops asking once that many bytes are out. A producer that ends short, or hands out a piece that crosses the length, fails the stream; the crossing piece is not sent. A media player wants this framing on a 206.

Byte ranges are the handler's choice. It opts in by setting accept-ranges: bytes on a 200 response with a string body. For a GET with a Range header the server then:

A body the handler encoded itself is cut as it is. A body the server compresses takes no range: it goes out whole, and the server removes accept-ranges from it. A producer takes no range either. A handler that streams calls http.range.parse itself, and returns 206 or 416 with its own content-range.

Several server instances (each with its own handler, TLS identities and connection budget) can share one event loop via serve_all; a single instance may also bind several addresses via listen.

The config table may override these defaults:

TLS configuration (ssl field):

{
  default = { cert = "path/to/cert", key = "path/to/key" },
  certs = {
    { cert = "...", key = "..." },
  },
}

Each certs entry answers for the hostnames its certificate declares, so one wildcard certificate covering an apex and its subdomains is a single entry.

The server uses a coroutine-per-connection model: each accepted connection is handled in a spawned coroutine within lev.run().

local server = require("http.server")

local srv, err = server.new(
    {
        ip   = "0.0.0.0",
        port = 8443,
        ssl  = {
            default = {
                cert = "/etc/tls/cert.pem",
                key  = "/etc/tls/key.pem",
            },
        },
    },
    function(method, query, args, headers, body, ctx)
        return "Hello!", 200, { ["content-type"] = "text/plain" }
    end
)
if not srv then error(err) end
srv:serve()  -- blocks forever