Reads a chunk from a streamed request body.
In streamed request-body mode, handlers receive req.bodyStream, which
exposes read, readAll, and close for incremental upload
processing.
@eryx/http/HttpServer ModuleReads a chunk from a streamed request body.
In streamed request-body mode, handlers receive req.bodyStream, which
exposes read, readAll, and close for incremental upload
processing.
Maximum number of bytes to read. Defaults to 65536.
The next body chunk, or nil when the stream ends.
Reads the rest of a streamed request body into a string.
The remaining request body.
Consumes and closes a streamed request body.
This is useful when a handler wants to abandon the body early but still allow the connection to be reused safely.
An incoming HTTP request received by an HttpServer.
Header names are lowercased for consistent lookup.
Available when streamRequestBodies = true.
Reads and buffers the full body on demand.
Decodes URL-encoded form data on demand.
Decodes multipart form-data on demand.
Decodes JSON request bodies on demand.
A single-threaded HTTP (or HTTPS) server.
Create one with createServer, then call HttpServer.listen to
start accepting connections. Provide an sslCtx in the options
to enable HTTPS.
local server = http.createServer(function(req, res)
print(req.method, req.path)
res:send(200, "OK")
end, { port = 8080 })
server:listen()
TLS fields (nil for plain HTTP)
Creates a new HttpServer with the given request handler.
Call HttpServer.listen on the returned server to start accepting connections.
local server = http.createServer(function(req, res)
print(req.method, req.path)
res:send(200, "Hello, world!")
end)
server:listen(function(host, port)
print("Listening on " .. host .. ":" .. tostring(port))
end)
The handler invoked for each incoming request.
Server configuration options.
The new server (not yet listening).
Starts listening for connections and serves requests.
This call blocks until HttpServer.close is called or the process is interrupted. The optional callback is invoked once the socket is bound and ready to accept connections.
Called with (host, port) when the server is ready.
Parse an incoming HTTP request from a client socket. Returns nil if the connection was closed before a full request arrived.
In the default mode, request bodies are buffered eagerly and exposed
through req.body, req.form, and req.multipart. When
streamRequestBodies = true, requests instead expose req.bodyStream
along with req:readBody(), req:readForm(), req:readMultipart(),
and req:readJson() for on-demand buffering/decoding. Chunked
request trailers are exposed on req.trailers once the body has
been fully consumed.
Handles one accepted client connection.
This may serve multiple requests over the same socket when keep-alive is in use. Unread streamed request bodies are drained automatically before reusing the connection.
The accepted client socket.
The peer IP address.
The peer port.
true when the connection was upgraded and the socket ownership has been transferred elsewhere.
Stops the server by closing the listening socket.
This unblocks the HttpServer.listen call, allowing the server loop to exit cleanly.
Reads and buffers the full request body on demand.
In default buffered mode this simply returns req.body. When streamed
request-body mode is enabled, this drains req.bodyStream and caches
the result.
The full request body.
Decodes a request body as application/x-www-form-urlencoded.
In streamed request-body mode this reads the full body on demand.
The decoded form fields, or nil when the request is not URL-encoded.
Decodes a request body as multipart/form-data.
In streamed request-body mode this reads the full body on demand.
Parsed multipart fields, or nil when the request is not multipart form-data.
Decodes a request body as JSON.
In streamed request-body mode this reads the full body on demand.
The decoded JSON value, or nil when the request does not declare a JSON content type.
Options for creating an HttpServer.
When true, request bodies are exposed through req.bodyStream
instead of being buffered eagerly.