#hs.httpserver

Simple HTTP server

Notes:

  • Running an HTTP server is potentially dangerous, you should seriously consider the security implications of exposing your Hammerspoon instance to a network - especially to the Internet
  • As a user of Hammerspoon, you are assumed to be highly capable, and aware of the security issues

#Submodules


#API Overview

Functions - API calls offered directly by the extension

Methods - API calls which can only be made on an object returned by a constructor


#API Documentation

#Functions

Signaturehs.httpserver.new([ssl], [bonjour]) -> object
TypeFunction
DescriptionCreates a new HTTP or HTTPS server
Parameters
  • ssl - An optional boolean. If true, the server will start using HTTPS. Defaults to false.
  • bonjour - An optional boolean. If true, the server will advertise itself with Bonjour. Defaults to true. Note that in order to change this, you must supply a true or false value for the ssl argument.
Returns
  • An hs.httpserver object
Notes
  • By default, the server will start on a random TCP port and advertise itself with Bonjour. You can check the port with hs.httpserver:getPort()
  • By default, the server will listen on all network interfaces. You can override this with hs.httpserver:setInterface() before starting the server
  • Currently, in HTTPS mode, the server will use a self-signed certificate, which most browsers will warn about. If you want/need to be able to use hs.httpserver with a certificate signed by a trusted Certificate Authority, please file an bug on Hammerspoon requesting support for this.
ExamplesNone
Sourceextensions/httpserver/libhttpserver.m line 374

#Methods

Signaturehs.httpserver:getInterface() -> string or nil
TypeMethod
DescriptionGets the network interface the server is configured to listen on
Parameters
  • None
Returns
  • A string containing the network interface name, or nil if the server will listen on all interfaces
NotesNone
ExamplesNone
Sourceextensions/httpserver/libhttpserver.m line 647

Signaturehs.httpserver:getName() -> string
TypeMethod
DescriptionGets the Bonjour name the server is configured to advertise itself as
Parameters
  • None
Returns
  • A string containing the Bonjour name of this server
Notes
  • This is not the hostname of the server, just its name in Bonjour service lists (e.g. Safari's Bonjour bookmarks menu)
ExamplesNone
Sourceextensions/httpserver/libhttpserver.m line 689

Signaturehs.httpserver:getPort() -> number
TypeMethod
DescriptionGets the TCP port the server is configured to listen on
Parameters
  • None
Returns
  • A number containing the TCP port
NotesNone
ExamplesNone
Sourceextensions/httpserver/libhttpserver.m line 616

Signaturehs.httpserver:maxBodySize([size]) -> object | current-value
TypeMethod
DescriptionGet or set the maximum allowed body size for an incoming HTTP request.
Parameters
  • size - An optional integer value specifying the maximum body size allowed for an incoming HTTP request in bytes. Defaults to 10485760 (10 MB).
Returns
  • If a new size is specified, returns the hs.httpserver object; otherwise the current value.
Notes
  • Because the Hammerspoon http server processes incoming requests completely in memory, this method puts a limit on the maximum size for a POST or PUT request.
ExamplesNone
Sourceextensions/httpserver/libhttpserver.m line 513

Signaturehs.httpserver:send(message) -> object
TypeMethod
DescriptionSends a message to the websocket client
Parameters
  • message - A string containing the message to send
Returns
  • The hs.httpserver object
NotesNone
ExamplesNone
Sourceextensions/httpserver/libhttpserver.m line 446

Signaturehs.httpserver:setCallback([callback]) -> object
TypeMethod
DescriptionSets the request handling callback for an HTTP server object
Parameters
  • callback - An optional function that will be called to process each incoming HTTP request, or nil to remove an existing callback. See the notes section below for more information about this callback
Returns
  • The hs.httpserver object
Notes
  • The callback will be passed four arguments:
  • A string containing the type of request (i.e. GET/POST/DELETE/etc)
  • A string containing the path element of the request (e.g. /index.html)
  • A table containing the request headers
  • A string containing the raw contents of the request body, or the empty string if no body is included in the request.
  • The callback must return three values:
  • A string containing the body of the response
  • An integer containing the response code (e.g. 200 for a successful request)
  • A table containing additional HTTP headers to set (or an empty table, {}, if no extra headers are required)
  • A POST request, often used by HTML forms, will store the contents of the form in the body of the request.
ExamplesNone
Sourceextensions/httpserver/libhttpserver.m line 466

Signaturehs.httpserver:setInterface(interface) -> object
TypeMethod
DescriptionSets the network interface the server is configured to listen on
Parameters
  • interface - A string containing an interface name
Returns
  • The hs.httpserver object
Notes
  • As well as real interface names (e.g. en0) the following values are valid:
  • An IP address of one of your interfaces
  • localhost
  • loopback
  • nil (which means all interfaces, and is the default)
ExamplesNone
Sourceextensions/httpserver/libhttpserver.m line 662

Signaturehs.httpserver:setName(name) -> object
TypeMethod
DescriptionSets the Bonjour name the server should advertise itself as
Parameters
  • name - A string containing the Bonjour name for the server
Returns
  • The hs.httpserver object
Notes
  • This is not the hostname of the server, just its name in Bonjour service lists (e.g. Safari's Bonjour bookmarks menu)
ExamplesNone
Sourceextensions/httpserver/libhttpserver.m line 707

Signaturehs.httpserver:setPassword([password]) -> object
TypeMethod
DescriptionSets a password for an HTTP server object
Parameters
  • password - An optional string that contains the server password, or nil to remove an existing password
Returns
  • The hs.httpserver object
Notes
  • It is not currently possible to set multiple passwords for different users, or passwords only on specific paths
ExamplesNone
Sourceextensions/httpserver/libhttpserver.m line 539

Signaturehs.httpserver:setPort(port) -> object
TypeMethod
DescriptionSets the TCP port the server is configured to listen on
Parameters
  • port - An integer containing a TCP port to listen on
Returns
  • The hs.httpserver object
NotesNone
ExamplesNone
Sourceextensions/httpserver/libhttpserver.m line 631

Signaturehs.httpserver:start() -> object
TypeMethod
DescriptionStarts an HTTP server object
Parameters
  • None
Returns
  • The hs.httpserver object
NotesNone
ExamplesNone
Sourceextensions/httpserver/libhttpserver.m line 573

Signaturehs.httpserver:stop() -> object
TypeMethod
DescriptionStops an HTTP server object
Parameters
  • None
Returns
  • The hs.httpserver object
NotesNone
ExamplesNone
Sourceextensions/httpserver/libhttpserver.m line 599

Signaturehs.httpserver:websocket(path, callback) -> object
TypeMethod
DescriptionEnables a websocket endpoint on the HTTP server
Parameters
  • path - A string containing the websocket path such as '/ws'
  • callback - A function returning a string for each received websocket message
Returns
  • The hs.httpserver object
Notes
  • The callback is passed one string parameter containing the received message
  • The callback must return a string containing the response message
  • Given a path '/mysock' and a port of 8000, the websocket URL is as follows:
  • ws://localhost:8000/mysock
  • wss://localhost:8000/mysock (if SSL enabled)
ExamplesNone
Sourceextensions/httpserver/libhttpserver.m line 415