HTTP Server Lua Module

From Hopmod Wiki

Jump to: navigation, search

Contents

http_server.listener(ip, port)

Create listener object bound to the given local address

listener.set_root(resource)

Set the root resource object

listener.start()

Start the server listener

listener.stop()

Stop the server listener. Note: stopping the listener doesn't shutdown established client connections

http_server.resource(methodFunctionTable)

Create a resource object. Keys supported for the methodFunctionTable argument: resolve, get, put, post, delete. Each of the callback functions (except resolve) when called are given a request object as the first argument. The callback for the resolve handler is given a string of the URI, the server expects this callback function to return a resource object. The callback for the method handlers must create a response object and send a response.

request.uri()

Return a string of the URI

request.uri_query()

Return a strinf of the URI query string component

request.header(name)

Find header field by name and return the field value as a string. The function returns nil if the header is not found.

request.content_length()

Return the value of the content-length field as a number.

request.content_type()

Return the value of the major type part of the content-type field as a string

request.content_subtype()

Return the sub type part of the content-type field as a string

request.host()

Return the value of the host field

request.client_ip()

Return the client ip address

request.async_read_content(callback)

Wait for the request content to be received and then call the callback function with the content. The content argument is passed as a string.

http_server.response(request)

Create a response object from the given request object.

response.header(name, value)

Add header

response.set_content_length(value)

Add content-length field

response.async_send_header(callback)

Send header. The callback function is given a boolean value for the first argument.

response.send_header()

Send header.

response.async_send_body(content, callback)

Send body content. The callback function is given a boolean value for the first argument.

response.send_body(content)

Send body content.