Doc - rttp


_G.rttp

Variables

Type Name Description
table responseCodes Table of standard response codes (HTTP response codes)

Functions

Return Name Description
nil handleMsg(func: function) Set the RTTP message handler function
nil runServer() Runs a blocking RTTP server using the handler from handleMsg
nil registerHandler() Registers a message handler for RTTP messages and passes it to the handler from handleMsg
number send(dest: string|number, path: string, method: string, contentType: string, body: any) Runs an RTTP request, and returns the message id
rttp.Message|string sendSync(dest: string|number, path: string, method: string, contentType: string, body: any) Runs an RTTP request, and returns the response or error
number reply(msg: rttp.Message, code: number, method: string, contentType: string, body: any) Replies an RTTP request with response code, and returns the message id
boolean valid(message: table) Checks if the provided message is a valid RTTP message
rttp.Message|string waitForMsg(timeout: number) Waits for the next RTTP message within time, or returns error string. Set timeout to 0 to disable
number get(dest: string|number, path: string) Runs an RTTP GET request, and returns the message id
rttp.Message|string getSync(dest: string|number, path: string) Runs an RTTP GET request, and returns the response or error
number post(dest: string|number, path: string, cType: string, body: any) Runs an RTTP POST request with content type and body, and returns the message id
rttp.Message|string postSync(dest: string|number, path: string, cType: string, body: any) Runs an RTTP POST request with content type and body, and returns the response or error
string codeName(code: number) Returns a string name for given response code
string stringMessage(message: rttp.Message) Returns a string version of a message for debug and logging

handleMsg(func: function):

Sets the message handler function:

Handler should take: method: string, head: table, body: any, msg: rttp.Message
Handler should return: code: number, contentType: string, body: any, head: table|nil
body may not be nil, and header is optional, and will be filled with the default RTTP header fields

responseCodes:

Table of common RTTP response codes, used by codeName(code).
This is based on HTTP response codes

Name Value Description
switchingProtocols 101 Switch protocol to the one indicated in header.upgrade
okay 200 Request processed okay
noContent 204 No content, only header change
movedPermanently 301 Page has PERMENTLY moved to header.location
movedTemporarily 302 Page has TEMPORARILY moved to header.location
seeOther 303 Go see this other page, then you can return here
badRequest 400 Bad request formatting, or missing parameters
unauthorized 401 Client should authenticate first
forbidden 403 Client does not have access
notFound 404 Requested content was not found on the server
methodNotAllowed 405 The requested method was not allowed
imATeapot 418 The server refuses the attempt to brew coffee with a teapot
failedDependency 424 Dependency was not present for request
upgradeRequired 426 Request must be performed with the protocol in header.upgrade
internalServerError 500 Server encountered an internal error possessing the request
notImplemented 501 The request is not yet implemented
serviceUnavailable 503 The requested service is not ready to handle the request

Struct: rttp.Message

Extends net.Message

In net.Message.header

Type Name Description
string type Net message type: 'rttp'
string method (REQUEST ONLY) RTTP method: 'GET' or 'POST'
string path (REQUEST ONLY) URI path
number code (REPLY ONLY) RTTP response code
string rspDomain (REPLY ONLY) Domain request was sent to
string contentType POST/Reply body content type: ex. 'none', 'text/plain', 'table/rtml', 'table'