cgilua

Last updated 23 days ago

Provides support functions in the cgilua module for Hammerspoon Minimal Web Server Lua templates.

This file contains functions which attempt to mimic as closely as possible the functions available to lua template files in the CGILua module provided by the Kepler Project at http://keplerproject.github.io/cgilua/index.html

The goal of this file is to provide most of the same functionality that CGILua does for template files. Any differences in the results or errors are most likely due to this code and you should direct all error reports or code change suggestions to the Hammerspoon GitHub repository.

Do not include this file directly in your Lua templates. This library is provided automatically in the cgilua table (module) in Lua template web server files. This submodule will only work from within that environment and should not be used in any other code.

Submodules

API Overview

API Documentation

Variables

Signature

hs.httpserver.hsminweb.cgilua.script_file

Type

Variable

Description

The file name of the running script. Obtained from cgilua.script_path.

Notes

Signature

hs.httpserver.hsminweb.cgilua.script_path

Type

Variable

Description

The system path of the running script. Equivalent to the CGI environment variable SCRIPT_FILENAME.

Notes

Signature

hs.httpserver.hsminweb.cgilua.script_pdir

Type

Variable

Description

The directory of the running script. Obtained from cgilua.script_path.

Notes

Signature

hs.httpserver.hsminweb.cgilua.script_vdir

Type

Variable

Description

If PATH_INFO represents a directory (i.e. ends with "/"), then this is equal to cgilua.script_vpath. Otherwise, this contains the directory portion of cgilua.script_vpath.

Notes

Signature

hs.httpserver.hsminweb.cgilua.script_vpath

Type

Variable

Description

Equivalent to the CGI environment variable PATH_INFO or "/", if no PATH_INFO is set.

Notes

tmp_path

Signature

hs.httpserver.hsminweb.cgilua.tmp_path

Type

Variable

Description

The directory used by cgilua.tmpfile

urlpath

Signature

hs.httpserver.hsminweb.cgilua.urlpath

Type

Variable

Description

The name of the script as requested in the URL. Equivalent to the CGI environment variable SCRIPT_NAME.

Notes

Functions

Signature

hs.httpserver.hsminweb.cgilua.contentheader(maintype, subtype) -> none

Type

Function

Description

Sets the HTTP response type for the content being generated to maintype/subtype.

Parameters

Returns

Notes

doif

Signature

hs.httpserver.hsminweb.cgilua.doif(filename) -> results

Type

Function

Description

Executes a lua file (given by filepath) if it exists.

Parameters

Returns

Notes

doscript

Signature

hs.httpserver.hsminweb.cgilua.doscript(filename) -> results

Type

Function

Description

Executes a lua file (given by filepath).

Parameters

Returns

Notes

errorlog

Signature

hs.httpserver.hsminweb.cgilua.errorlog(msg) -> nil

Type

Function

Description

Sends the message to the hs.httpserver.hsminweb log, tagged as an error.

Parameters

Returns

Notes

Signature

hs.httpserver.hsminweb.cgilua.header(key, value) -> none

Type

Function

Description

Sets the HTTP response header key to value

Parameters

Returns

Notes

Signature

hs.httpserver.hsminweb.cgilua.htmlheader() -> none

Type

Function

Description

Sets the HTTP response type to "text/html"

Parameters

Returns

Notes

Signature

hs.httpserver.hsminweb.cgilua.mkabsoluteurl(uri) -> string

Type

Function

Description

Returns an absolute URL for the given URI by prepending the path with the scheme, hostname, and port of this web server.

Parameters

Returns

Notes

mkurlpath

Signature

hs.httpserver.hsminweb.cgilua.mkurlpath(uri, [args]) -> string

Type

Function

Description

Creates a full document URI from a partial URI, including query arguments if present.

Parameters

Returns

Notes

print

Signature

hs.httpserver.hsminweb.cgilua.print(...) -> nil

Type

Function

Description

Appends the given arguments to the response body.

Parameters

Returns

Notes

put

Signature

hs.httpserver.hsminweb.cgilua.put(...) -> nil

Type

Function

Description

Appends the given arguments to the response body.

Parameters

Returns

Notes

redirect

Signature

hs.httpserver.hsminweb.cgilua.redirect(url, [args]) -> none

Type

Function

Description

Sends the headers to force a redirection to the given URL adding the parameters in table args to the new URL.

Parameters

Returns

Notes

Signature

hs.httpserver.hsminweb.cgilua.servervariable(varname) -> string

Type

Function

Description

Returns a string with the value of the CGI environment variable correspoding to varname.

Parameters

Returns

Notes

Signature

hs.httpserver.hsminweb.cgilua.splitfirst(path) -> path component, path remainder

Type

Function

Description

Returns two strings with the "first directory" and the "remaining paht" of the given path string splitted on the first separator ("/" or "\").

Parameters

Returns

Signature

hs.httpserver.hsminweb.cgilua.splitonlast(path) -> directory, file

Type

Function

Description

Returns two strings with the "directory path" and "file" parts of the given path string splitted on the last separator ("/" or "\").

Parameters

Returns

Notes

tmpfile

Signature

hs.httpserver.hsminweb.cgilua.tmpfile([dir], [namefunction]) -> file[, err]

Type

Function

Description

Returns the file handle to a temporary file for writing, or nil and an error message if the file could not be created for any reason.

Parameters

Returns

Notes

tmpname

Signature

hs.httpserver.hsminweb.cgilua.tmpname() -> string

Type

Function

Description

Returns a temporary file name used by cgilua.tmpfile.

Parameters

Returns

Notes