Module moonwalk.connection.abstract
Abstract connection.
To add support for a new host environment, create a class module with methods matching those listed here, and insert the module path into api.connection_modules . Moonwalk will call connection:detect_host on each module in that table. The first one to return a truthy value will be used as a base class for the connection .
Static methods
connection:detect_host () | Detect the host environment. |
Instance methods
connection:get_method () | Get the request method. |
connection:get_path () | Get the path info. |
connection:get_scheme () | Get the URI scheme. |
connection:get_script () | Get the script's name. |
connection:get_query () | Get the query string. |
connection:get_header (name) | Get the value of an HTTP header. |
connection:send (data) | Send the response body. |
connection:send_head (status, headers) | Send the response head. |
connection:receive () | Receive the request body. |
Static methods
Called on a "connection class," rather than an instance of one.- connection:detect_host ()
-
Detect the host environment. This function should check whether
this connection class can support the current host environment.
If supported, return an appropriate name for the host environment,
otherwise return
nil
.Returns:
-
The name of the host environment if detected, else
nil
.
Instance methods
Called on a connection instance.- connection:get_method ()
- Get the request method. Equivalent to CGI's REQUEST_METHOD.
- connection:get_path ()
-
Get the path info. Equivalent to CGI's
PATH_INFO.
This function should return the part of the path that follows the script name in a CGI-style path. In other words, the last part of the resource path, without the directory the script is in. If the handler script is in the root path, this should return "
/
".For example, if you all rewrite requests for
/somewhere/...
to/somewhere/index.lua
, then the path info for a request to/somewhere/something/123/
should be/something/123/
Returns:
-
The path info.
See also:
- connection:get_scheme ()
-
Get the URI scheme.
Returns:
-
The URI scheme. Should be
http
orhttps
. - connection:get_script ()
-
Get the script's name. Equivalent to CGI's
SCRIPT_NAME.
This represents the path to the script that was requested, relative to the web root.
For example, if you all rewrite requests for
/somewhere/...
to/somewhere/index.lua
, then the script for a request to/somewhere/something/123/
should besomewhere/index.lua
Returns:
-
The path to the script
See also:
- connection:get_query ()
-
Get the query string. Equivalent to CGI's
QUERY_STRING.
Returns:
-
The query string.
- connection:get_header (name)
-
Get the value of an HTTP header.
Parameters:
- name The name of the header.
Returns:
-
String value of the header, or
nil
if the header wasn't found. - connection:send (data)
-
Send the response body.
This may be called multiple times per request after connection:send_head has been called. It should be called at least once per request.
Parameters:
- data A string containing (part of) the request body.
- connection:send_head (status, headers)
-
Send the response head.
This should only be called once per request.
Parameters:
- status
A string containing the HTTP status code and reason,
like
500 Internal Server Error
. - headers A table of header names and values.
- status
A string containing the HTTP status code and reason,
like
- connection:receive ()
-
Receive the request body.
This may be called multiple times per request. It will be called at least once for each request with a
Content-Length
header. When there is no more data to receive, be sure to returnnil
.Returns:
-
String data received from consumer, or
nil
.