Minimalist Web Server for Hammerspoon
This module aims to be a minimal, but (mostly) standards-compliant web server for use within Hammerspoon. Expanding upon the Hammerspoon module, hs.httpserver
, this module adds support for serving static pages stored at a specified document root as well as serving dynamic content from Lua Template Files interpreted within the Hammerspoon environment and external executables which support the CGI/1.1 framework.
This module aims to provide a fully functional, and somewhat extendable, web server foundation, but will never replace a true dedicated web server application. Some limitations include:
While some of these limitations may be mitigated to an extent in the future with additional modules and additions to hs.httpserver
, Hammerspoon's web serving capabilities will never replace a dedicated web server when volume or speed is required.
An example web site is provided in the hsdocs
folder of the hs.doc
module. This web site can serve documentation for Hammerspoon dynamically generated from the json file included with the Hammerspoon application for internal documentation. It serves as a basic example of what is possible with this module.
You can start this web server by typing the following into your Hammerspoon console:
require("hs.doc.hsdocs").start()
and then visiting http://localhost:12345/
with your web browser.
Signature | hs.httpserver.hsminweb.dateFormatString |
---|---|
Type | Constant |
Description | A format string, usable with |
Signature | hs.httpserver.hsminweb.statusCodes |
---|---|
Type | Constant |
Description | HTTP Response Status Codes This table contains a list of common HTTP status codes identified from various sources (see Notes below). Because some web servers append a subcode after the official HTTP status codes, the keys in this table are the string representation of the numeric code so a distinction can be made between numerically "identical" keys (for example, "404.1" and "404.10"). You can reference this table with a numeric key, however, and it will be converted to its string representation internally. Notes:
|
Signature | hs.httpserver.hsminweb._accessLog |
---|---|
Type | Variable |
Description | Accessed as |
Signature | hs.httpserver.hsminweb._errorHandlers |
---|---|
Type | Variable |
Description | Accessed as HTTP uses a three digit numeric code for error conditions. Some servers have introduced subcodes, which are appended as a decimal added to the error condition. You can assign your own handler to customize the response for a specific error code by specifying a function for the desired error condition as the value keyed to the error code as a string key in this table. The function should expect three arguments:
If you override the default handler, "default", the function should expect four arguments instead: the error code as a string, followed by the same three arguments defined above. In either case, the function should return three values:
|
Signature | hs.httpserver.hsminweb._serverAdmin |
---|---|
Type | Variable |
Description | Accessed as This value is often used in error messages or on error pages indicating a point of contact for administrative help. It can be accessed from within an error helper functions as |
Signature | hs.httpserver.hsminweb._supportMethods |
---|---|
Type | Variable |
Description | Accessed as The default methods supported internally are:
If you assign If you assign Common HTTP request methods can be found at https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods and https://en.wikipedia.org/wiki/WebDAV. Currently, only HEAD, GET, and POST have built in support for static pages; even if you set other methods to A companion module supporting the methods required for WebDAV is being considered. |
Signature | hs.httpserver.hsminweb.log |
---|---|
Type | Variable |
Description | The |
Signature | hs.httpserver.hsminweb.formattedDate([date]) -> string |
---|---|
Type | Function |
Description | Returns the current or specified time in the format expected for HTTP communications as described in RFC 822, updated by RFC 1123. Parameters:
Returns:
|
Signature | hs.httpserver.hsminweb.urlParts(url) -> table |
---|---|
Type | Function |
Description | Parse the specified URL into it's constituant parts. Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb.new([documentRoot]) -> hsminwebTable |
---|---|
Type | Constructor |
Description | Create a new hsminweb table object representing a Hammerspoon Web Server. Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:accessList([table]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the access-list table for the hsminweb web server Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:allowDirectory([flag]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the whether or not a directory index is returned when the requested URL specifies a directory and no file matching an entry in the directory indexes table is found. Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:bonjour([flag]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the whether or not the web server should advertise itself via Bonjour when it is running. Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:cgiEnabled([flag]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the whether or not CGI file execution is enabled. Parameters:
Returns:
|
Signature | hs.httpserver.hsminweb:cgiExtensions([table]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the file extensions which identify files which should be executed as CGI scripts to provide the results to an HTTP request. Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:directoryIndex([table]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the file names to look for when the requested URL specifies a directory. Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:dnsLookup([flag]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the whether or not DNS lookups are performed. Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:documentRoot([path]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the document root for the web server. Parameters:
Returns:
|
Signature | hs.httpserver.hsminweb:interface([interface]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the network interface that the hsminweb web server will listen on Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:luaTemplateExtension([string]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the extension of files which contain Lua code which should be executed within Hammerspoon to provide the results to an HTTP request. Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:maxBodySize([size]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the maximum body size for an HTTP request Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:name([name]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the name the web server uses in Bonjour advertisement when the web server is running. Parameters:
Returns:
|
Signature | hs.httpserver.hsminweb:password([password]) -> hsminwebTable | boolean |
---|---|
Type | Method |
Description | Set a password for the hsminweb web server, or return a boolean indicating whether or not a password is currently set for the web server. Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:port([port]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the name the port the web server listens on Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:queryLogging([flag]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the whether or not requests to this web server are logged. Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:scriptTimeout([integer]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the timeout for a CGI script Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:ssl([flag]) -> hsminwebTable | current-value |
---|---|
Type | Method |
Description | Get or set the whether or not the web server utilizes SSL for HTTP request and response communications. Parameters:
Returns:
Notes:
|
Signature | hs.httpserver.hsminweb:start() -> hsminwebTable |
---|---|
Type | Method |
Description | Start serving pages for the hsminweb web server. Parameters:
Returns:
|
Signature | hs.httpserver.hsminweb:stop() -> hsminwebTable |
---|---|
Type | Method |
Description | Stop serving pages for the hsminweb web server. Parameters:
Returns:
Notes:
|