_keepAliveTimeout: number
_maxRequestsPerConnection: number?
_streamRequestBodies: boolean
_callback: ServerCallback
_trailers: { [string]: string }?
_mode: "empty" | "length" | "chunked"
_onFinish: (((string, { [string]: string }?) → ()))?
ServerRequestMethods:readBody() → string
ServerRequestMethods:readForm() → QueryTable?
ServerRequestMethods:readJson() → any?
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()
Implements: HttpServerFields
Properties
_keepAliveTimeout: number
_maxRequestsPerConnection: number?
_streamRequestBodies: boolean
_callback: ServerCallback
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)
Parameters
callback: ServerCallback
The handler invoked for each incoming request.
Server configuration options.
Returns
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.
HttpServer:listen(callback: (((host: string, port: number) → ()))?) → never
Parameters
callback: (((host: string, port: number) → ()))?
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.
HttpServer:_readRequest(sock: any, remoteAddr: string, remotePort: number, initialBuffer: string?) → (ServerRequest?, string)
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.
HttpServer:_handleConnection(client_sock: any, remoteAddr: string, remotePort: number) → boolean
Parameters
client_sock: any
The accepted client socket.
remoteAddr: string
The peer IP address.
remotePort: number
The peer port.
Returns
boolean
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.
HttpServer:close() → ()
Implements: RequestBodyStreamFields
Properties
_trailers: { [string]: string }?
_mode: "empty" | "length" | "chunked"
_onFinish: (((string, { [string]: string }?) → ()))?
RequestBodyStream:_finish() → ()
RequestBodyStream:_recvChunk() → string
RequestBodyStream:_recvUntil(delim: string) → string
RequestBodyStream:_recvExact(n: number) → string
RequestBodyStream:_readChunkHeader() → ()
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.
RequestBodyStream:read(count: number?) → string
Parameters
count: number?
Maximum number of bytes to read. Defaults to 65536.
Returns
string
The next body chunk, or "" when the stream ends.
Reads the rest of a streamed request body into a string.
RequestBodyStream:readAll() → string
Returns
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.
RequestBodyStream:close() → ()
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.
ServerRequestMethods:readBody() → string
Returns
string
The full request body.
Decodes a request body as multipart/form-data.
In streamed request-body mode this reads the full body on demand.
Returns
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.
ServerRequestMethods:readJson() → any?
Returns
any?
The decoded JSON value, or nil when the request does not declare a JSON content type.
An incoming HTTP request received by an HttpServer.
Header names are lowercased for consistent lookup.
type ServerRequest = {
method: string,
path: string,
pathname: string,
queryString: string,
query: QueryTable,
httpVersion: string,
headers: { [string]: string },
trailers: { [string]: string },
contentType: string?,
body: string,
form: QueryTable?,
multipart: { ParsedMultipartField }?,
bodyStream: RequestBodyStream?,
remoteAddr: string,
remotePort: number,
readBody: ((self: ServerRequest) → string),
readForm: ((self: ServerRequest) → QueryTable?),
readMultipart: ((self: ServerRequest) → { ParsedMultipartField }?),
readJson: ((self: ServerRequest) → any?)
}
ServerRequest:readBody() → string
Reads and buffers the full body on demand.
ServerRequest:readForm() → QueryTable?
Decodes URL-encoded form data on demand.
Decodes multipart form-data on demand.
ServerRequest:readJson() → any?
Decodes JSON request bodies on demand.
headers: { [string]: string }
trailers: { [string]: string }
Available when streamRequestBodies = true.
Options for creating an HttpServer.
type HttpServerOptions = {
host: string?,
port: number?,
backlog: number?,
clientTimeout: number?,
keepAliveTimeout: number?,
maxRequestsPerConnection: number?,
maxHeaderBytes: number?,
maxBodyBytes: number?,
streamRequestBodies: boolean?,
sslCtx: SslContext?
}
keepAliveTimeout: number?
maxRequestsPerConnection: number?
streamRequestBodies: boolean?
When true, request bodies are exposed through req.bodyStream
instead of being buffered eagerly.