The httpd module implements a basic HTTP web server. Besides unencrypted connections it also supports TLS by means of mbed TLS.
The built-in web server is coupled with the Lua system so the full programming ability of Lua is available for serving web pages.
Supported request types are GET, POST, PUT, PATCH and DELETE.
In addition to the applications own web pages, there is a secondary server root directory at /lib/htdocs. This is provided by the SDK and contains pages for remote system configuration, update and debug. The service homepage of the device is reachable at http://xx.xx.xx.xx/settings/.
Apart from serving static files, there are three ways to mix HTML with scripting:
- file.shtml — Server parsed HTML
-
Files ending in .shtml are parsed by the server before they are sent to the client.
- <!-- #include virtual="filename" -->
- Includes the contents of the file into the output. If the filename is an absolute path starting with '/', the path is made relative to the server root directory. If the filename is a relative path, it is seen relative to the directory of the file that contains the include directive.
- file.lp — Lua Page
-
Files ending in .lp are parsed by the server before they are sent to the client. These files allow server-side includes as above, but they also allow included Lua code. The included code has access to all global variables.
- <?lua commands ?>
- Runs the given commands. Anything written by the commands is merged into the output at the place where the commands had been. Alternatively the syntax <% commands %> can be used.
- <?lua= expression ?>
- This markup processes expression in the same way as print() would. The output is inserted at the place where the expression had been. Alternatively the syntax <%= expression %> can be used.
- file.lua — Lua Script
- Files ending in .lua are processed by the Lua interpreter like any other Lua program. They run in the same environment as the underlying event loop. They have access to all global variables.
Lua commands and expressions inside a Lua Page (.lp) or a Lua Script (.lua) always have access to two predefined local variables:
- httpd
- The base instance of the web server running the request.
- cgi
- A table with information about the current request.
Both of these variables are local to the running cgi script and lose their scope once the page has been delivered.
Lua scripting can write output to the page using cgi:write(). Do not use print(). The print() command sends its output to stdout as usual, and not to the web page.