http.server
http.server objects are used to encapsulate the accept() and dispatch of http clients. Each new client request will invoke the onstream callback in a new cqueues managed coroutine. In addition to constructing and returning a HTTP response, an onstream handler may decide to take ownership of the connection for other purposes, e.g. upgrade from a HTTP 1.1 connection to a WebSocket connection.
For examples of how to use the server library, please see the examples directory in the source tree.
new(options) {#http.server.new}
Creates a new instance of an HTTP server listening on the given socket.
.socket(cqueues.socket): the socket thataccept()will be called on.onerror(function): Function that will be called when an error occurs (default handler throws an error). See server:onerror().onstream(function): Callback function for handling a new client request. The function receives the server and the new stream as parameters. If the callback throws an error it will be reported from:step()or:loop().tls(boolean): Specifies if the system should use Transport Layer Security. Values are:nil: Allow both tls and non-tls connectionstrue: Allows tls connections onlyfalse: Allows non-tls connections only
.ctx(context object): Anopenssl.ssl.contextobject to use for tls connections. Ifnilis passed, a self-signed context will be generated..connection_setup_timeout(number): Timeout (in seconds) to wait for client to send first bytes and/or complete TLS handshake. Default is 10 seconds..intra_stream_timeout(number): Timeout (in seconds) to wait for a new stream on an idle connection before giving up and closing the connection.version(number): The http version to allow to connect (default: any).cq(cqueue): A cqueues controller to use as a main loop. The default is a new controller for the server..max_concurrent(number): Maximum number of connections to allow live at a time. Default is infinity.
listen(options) {#http.server.listen}
Creates a new socket and returns an HTTP server that will accept() from it.
Parameters are the same as new(options) except instead of .socket you provide the following:
.host(string): Local IP address in dotted decimal or IPV6 notation. This value is required if.pathis not specified..port(number): IP port for the local socket. Specify 0 for automatic port selection. Ports 1-1024 require the application has root privilege to run. Maximum value is 65535. If.tls == nilthen this value is required. Otherwise, the defaults are:80if.tls == false443if.tls == true
.path(string): Path to UNIX a socket. This value is required if.hostis not specified..family(string): Protocol family. Default is"AF_INET".v6only(boolean): Specifytrueto limit all connections to ipv6 only (no ipv4-mapped-ipv6). Default isfalse..mode(string):fchmodorchmodsocket after creating UNIX domain socket..mask(boolean): Set and restore umask when binding UNIX domain socket..unlink(boolean):truemeans unlink socket path before binding..reuseaddr(boolean): Turn onSO_REUSEADDRflag..reuseport(boolean): Turn onSO_REUSEPORTflag.
server:onerror(new_handler) {#http.server:onerror}
If called with parameters, the function replaces the current error handler function with new_handler and returns a reference to the old function. Calling the function with no parameters returns the current error handler. The default handler throws an error. The onerror function for the server can be set during instantiation through the options table passed to the server.listen(options) function.
server:listen(timeout) {#http.server:listen}
Initializes the server socket and if required, resolves DNS. server:listen() is required if localname is called before step or loop. On error, returns nil, an error message and an error number.
server:localname() {#http.server:localname}
Returns the connection information for the local socket. Returns address family, IP address and port for an external socket. For Unix domain sockets, the function returns AF_UNIX and the path. If the connection object is not connected, returns AF_UNSPEC (0). On error, returns nil, an error message and an error number.
server:pause() {#http.server:pause}
Cause the server loop to stop processing new clients until resume is called. Existing client connections will run until closed.
server:resume() {#http.server:resume}
Resumes a paused server and processes new client requests.
server:close() {#http.server:close}
Shutdown the server and close the socket. A closed server cannot be reused.
server:pollfd() {#http.server:pollfd}
Returns a file descriptor (as an integer) or nil.
The file descriptor can be passed to a system API like select or kqueue to wait on anything this server object wants to do. This method is used for integrating with other main loops, and should be used in combination with :events() and :timeout().
server:events() {#http.server:events}
Returns a string indicating the type of events the object is waiting on: the string will contain "r" if it wants to be steped when the file descriptor returned by pollfd() has had POLLIN indicated; "w" for POLLOUT or "p" for POLLPRI.
This method is used for integrating with other main loops, and should be used in combination with :pollfd() and :timeout().
server:timeout() {#http.server:timeout}
The maximum time (in seconds) to wait before calling server:step().
This method is used for integrating with other main loops, and should be used in combination with :pollfd() and :events().
server:empty() {#http.server:empty}
Returns true if the master socket and all client connection have been closed, false otherwise.
server:step(timeout) {#http.server:step}
Step once through server's main loop: any waiting clients will be accept()-ed, any pending streams will start getting processed, and each onstream handler will get be run at most once. This method will block for up to timeout seconds. On error, returns nil, an error message and an error number.
This can be used for integration with external main loops.
server:loop(timeout) {#http.server:loop}
Run the server as a blocking loop for up to timeout seconds. The server will continue to listen and accept client requests until either :pause() or :close() is called, or an error is experienced.
server:add_socket(socket) {#http.server:add_socket}
Add a new connection socket to the server for processing. The server will use the current onstream request handler and all options currently specified through the server.listen(options) constructor. add_socket can be used to process connection sockets obtained from an external source such as:
- Another cqueues thread with some other master socket.
- From inetd for start on demand daemons.
- A Unix socket with
SCM_RIGHTS.
server:add_stream(stream) {#http.server:add_stream}
Add an existing stream to the server for processing.