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:
next(self) returns the next piece of the body, a string, or nil at
the end. An empty piece is skipped.close(self, err), optional, is called once whatever the end: err
is nil after the last piece went out, or the reason when next
raised, handed out something not a string, or the client went away.
The place to release what the producer holds.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:
206, content-range: bytes first-last/size and that part of
the body;416, content-range: bytes */size and an empty body when the
range holds no byte of it. The etag, last-modified,
cache-control and content-encoding headers are dropped;http.range.parse does not read, a
list of ranges among them;If-Range is not the
response's etag or last-modified, byte for byte. A weak etag
never matches.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:
ip — bind address (default: "127.0.0.1")port — listen port (default: 8080)listen — optional array of { ip = ..., port = ... } bind specs; when
set it takes precedence over ip/portbacklog — listen backlog (default: 256)connection_limit — max concurrent connections (default: 64)requests_per_connection — requests before connection closes (default: 512)max_body_size — max request body in bytes (default: 5 MB)request_line_limit — max header/request line (default: 8 KB)keepalive_idle_timeout — keep-alive idle timeout (default: 15)request_header_timeout — header read timeout (default: 10)request_body_timeout — body read timeout (default: 30)tls_handshake_timeout — TLS handshake timeout (default: 10)ssl — TLS configuration (see below)compression — compression settings tablelog_level — log level (default: "access")log_headers — request headers to logon_request — optional callback invoked after each response is sent,
with one table argument: { host, method, query, status, elapsed, size }
(status is a number, elapsed in seconds, size the response body
size in bytes). A streamed body fires it at its end, aborted or not,
with the body bytes that went out and the time to the end. It fires
only on the normal response path — proxied requests and premature
protocol errors do not reach it. The callback runs under pcall; a
failure is logged and never affects the requestTLS 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