dreamworkAPI
Ctrl K
GitHub
classDocs / http / Server

http.Server

classserversource
Object → http.Server

An HTTP server backed by HolyLib's httpserver.

lua
local server = http.Server()

server:get( "/hello", function( req, res )
    res.content = "hello " .. req.remote_addr
end )

server:start( "0.0.0.0", 8080 )

Handlers can defer the reply:

  • return a Future, the request finishes when it settles, a failed or cancelled future replies via the error handler;
  • return false, then call req:finish() (or res:finish()) later.

A deferred request that is dropped without :finish is finished when it is collected. Stopping or restarting the server answers every deferred request with 503.

A running server is kept alive until :stop or :destroy, a stopped one is destroyed when collected.

Constructor

http.Server(name: string?): http.Server

Fields

NameTypeDescription
namestringThe server name used by http.Server.findByName, max length is 63 characters.
addressstringThe bind address from the last :start. Read-only.
portintegerThe listen port from the last :start, or 0 before start. Read-only.
runningbooleantrue if the server is currently listening, false once destroyed. Read-only.
error_handlerhttp.Server.ErrorHandler | nilCalled when a route handler throws or its future fails, nil (and the fallback when it throws itself) replies 500 Internal Server Error.
tcp_nodelayboolean | niltrue sets TCP_NODELAY on accepted sockets. nil until assigned (HolyLib default).
read_timeoutnumber | nilThe maximum time allowed for a read, in seconds (fractions are microseconds). nil until assigned (HolyLib default).
write_timeoutnumber | nilThe maximum time allowed for a write, in seconds (fractions are microseconds). nil until assigned (HolyLib default).
payload_max_lengthinteger | nilThe maximum accepted request body length in bytes. nil until assigned (HolyLib default).
keep_alive_timeoutinteger | nilHow long keep-alive connections may stay open, in seconds. nil until assigned (HolyLib default).
keep_alive_max_countinteger | nilHow many requests a keep-alive connection may serve. nil until assigned (HolyLib default).
thread_sleepinteger | nilHow long worker threads sleep (ms) while waiting for the main thread to handle a request. nil until assigned (HolyLib default).
availableboolean

Static functions

findByNameFinds an existing server by its name property.
getAllReturns every currently existing HolyLib HttpServer wrapped as http.Server objects.

#http.Server.findByName

serversource
function http.Server.findByName(name: string): http.Server | nil

Finds an existing server by its name property.

Returns a dreamwork wrapper around the HolyLib object, or nil if none matched. Wrappers of servers created elsewhere (e.g. before a reload) do not destroy them when collected.

Parameters
NameTypeDescription
namestringThe server name to look for.
Returns
TypeNameDescription
http.Server | nilserver

#http.Server.getAll

serversource
function http.Server.getAll(): http.Server[]

Returns every currently existing HolyLib HttpServer wrapped as http.Server objects.

Wrappers of servers created elsewhere do not destroy them when collected.

Returns
TypeNameDescription
http.Server[]servers

Methods

addProxyTrusts proxy_address as a reverse proxy and reads the client IP from header_name.
deleteRegisters a DELETE handler.
destroyStops and destroys the underlying HolyLib server, even one found with findByName / getAll.
getRegisters a GET handler.
mountMounts a filesystem folder at the given URL path.
optionsRegisters an OPTIONS handler.
patchRegisters a PATCH handler.
postRegisters a POST handler.
prepareQueues a prepared response for the next matching request from the given player user_id.
putRegisters a PUT handler.
setThreadsConfigures the worker thread pool size.
startStarts (or restarts) the server on the given address and port.
stopStops the server if it is running, deferred requests are answered with 503.
unmountRemoves every mount previously set for path.

#Server:addProxy

serversource
function http.Server:addProxy(proxy_address: string, header_name: string, use_last_ip?: boolean)

Trusts proxy_address as a reverse proxy and reads the client IP from header_name.

Parameters
NameTypeDescription
proxy_addressstringThe remote address of the trusted proxy.
header_namestringHeader that carries the real client IP, e.g. "X-Forwarded-For".
use_last_ipoptionalbooleanPrefer the last IP in a comma-separated header. Defaults to false.

#Server:delete

serversource
function http.Server:delete(path: string, handler: http.Server.Handler, ip_whitelist?: boolean)

Registers a DELETE handler.

Parameters
NameTypeDescription
pathstringThe URL path pattern.
handlerhttp.Server.HandlerThe route handler.
ip_whitelistoptionalbooleanRestrict to connected players. Defaults to false.

#Server:destroy

serversource
function http.Server:destroy()

Stops and destroys the underlying HolyLib server, even one found with findByName / getAll.

Further method calls raise an error.

#Server:get

serversource
function http.Server:get(path: string, handler: http.Server.Handler, ip_whitelist?: boolean)

Registers a GET handler. Path parameters use :name segments (e.g. "/user/:id").

When ip_whitelist is true, only requests from connected players' IPs are handled.

Parameters
NameTypeDescription
pathstringThe URL path pattern.
handlerhttp.Server.HandlerThe route handler.
ip_whitelistoptionalbooleanRestrict to connected players. Defaults to false.

#Server:mount

serversource
function http.Server:mount(path: string, folder: string)

Mounts a filesystem folder at the given URL path.

Parameters
NameTypeDescription
pathstringThe URL path prefix.
folderstringThe filesystem folder to serve.

#Server:options

serversource
function http.Server:options(path: string, handler: http.Server.Handler, ip_whitelist?: boolean)

Registers an OPTIONS handler.

Parameters
NameTypeDescription
pathstringThe URL path pattern.
handlerhttp.Server.HandlerThe route handler.
ip_whitelistoptionalbooleanRestrict to connected players. Defaults to false.

#Server:patch

serversource
function http.Server:patch(path: string, handler: http.Server.Handler, ip_whitelist?: boolean)

Registers a PATCH handler.

Parameters
NameTypeDescription
pathstringThe URL path pattern.
handlerhttp.Server.HandlerThe route handler.
ip_whitelistoptionalbooleanRestrict to connected players. Defaults to false.

#Server:post

serversource
function http.Server:post(path: string, handler: http.Server.Handler, ip_whitelist?: boolean)

Registers a POST handler.

Parameters
NameTypeDescription
pathstringThe URL path pattern.
handlerhttp.Server.HandlerThe route handler.
ip_whitelistoptionalbooleanRestrict to connected players. Defaults to false.

#Server:prepare

serversource
function http.Server:prepare(user_id: integer, path: string, method: http.RequestMethod | string, required_headers: table<string, string>, handler: fun( response: http.Server.Response ), uses?: integer)

Queues a prepared response for the next matching request from the given player user_id.

handler runs immediately to fill the response, matching requests are then answered on the worker thread without any Lua call. The response is invalid after handler returns.

Parameters
NameTypeDescription
user_idintegerThe player's UserID (-1 for non-player / unmatched clients).
pathstringExact request path to match.
methodhttp.RequestMethod | stringExact HTTP method to match.
required_headerstable<string, string>Headers that must be present with these values.
handlerfun( response: http.Server.Response )Fills the prepared response.
usesoptionalintegerHow many times this entry may match. Defaults to 1.

#Server:put

serversource
function http.Server:put(path: string, handler: http.Server.Handler, ip_whitelist?: boolean)

Registers a PUT handler.

Parameters
NameTypeDescription
pathstringThe URL path pattern.
handlerhttp.Server.HandlerThe route handler.
ip_whitelistoptionalbooleanRestrict to connected players. Defaults to false.

#Server:setThreads

serversource
function http.Server:setThreads(min_threads: integer, max_threads: integer)

Configures the worker thread pool size.

The pool is created on start, a running server restarts on the next frame.

Parameters
NameTypeDescription
min_threadsintegerMinimum number of worker threads.
max_threadsintegerMaximum number of worker threads.

#Server:start

serversource
function http.Server:start(address: string, port: integer)

Starts (or restarts) the server on the given address and port.

HolyLib binds on a separate thread, so running may stay false for a moment and a failed bind is not reported.

Route and setting changes on a running server restart it on the next frame.

Parameters
NameTypeDescription
addressstringBind address, e.g. "0.0.0.0" or "127.0.0.1".
portintegerTCP port to listen on.

#Server:stop

serversource
function http.Server:stop()

Stops the server if it is running, deferred requests are answered with 503.

#Server:unmount

serversource
function http.Server:unmount(path: string)

Removes every mount previously set for path.

Parameters
NameTypeDescription
pathstringThe URL path prefix to unmount.
Generated from the dreamwork meta branch.