1-- $NetBSD: printenv.lua,v 1.5 2021/02/28 16:10:00 rillig Exp $
2
3-- this small Lua script demonstrates the use of Lua in (bozo)httpd
4-- it will simply output the "environment"
5
6-- Keep in mind that bozohttpd forks for each request when started in
7-- daemon mode, you can set global variables here, but they will have
8-- the same value on each invocation.  You can not keep state between
9-- two calls.
10
11-- You can test this example by running the following command:
12-- /usr/libexec/httpd -b -f -I 8080 -L test printenv.lua .
13-- and then navigate to: http://127.0.0.1:8080/test/printenv
14
15local httpd = require 'httpd'
16
17function escape_html(s)
18  return s:gsub('&', '&amp;'):gsub('<', '&lt;'):gsub('>', '&gt;'):gsub('"', '&quot;')
19end
20
21function printenv(env, headers, query)
22
23          -- we get the "environment" in the env table, the values are more
24          -- or less the same as the variable for a CGI program
25
26          -- output headers using httpd.write()
27          -- httpd.write() will not append newlines
28          httpd.write("HTTP/1.1 200 Ok\r\n")
29          httpd.write("Content-Type: text/html\r\n\r\n")
30
31          -- output html using httpd.print()
32          -- you can also use print() and io.write() but they will not work with SSL
33          httpd.print([[
34                    <html>
35                              <head>
36                                        <title>Bozotic Lua Environment</title>
37                              </head>
38                              <body>
39                                        <h1>Bozotic Lua Environment</h1>
40          ]])
41
42          httpd.print('module version: ' .. httpd._VERSION .. '<br>')
43
44          httpd.print('<h2>Server Environment</h2>')
45          -- print the list of "environment" variables
46          for k, v in pairs(env) do
47                    httpd.print(escape_html(k) .. '=' .. escape_html(v) .. '<br/>')
48          end
49
50          httpd.print('<h2>Request Headers</h2>')
51          for k, v in pairs(headers) do
52                    httpd.print(escape_html(k) .. '=' .. escape_html(v) .. '<br/>')
53          end
54
55          if query ~= nil then
56                    httpd.print('<h2>Query Variables</h2>')
57                    for k, v in pairs(query) do
58                              httpd.print(escape_html(k) .. '=' .. escape_html(v) .. '<br/>')
59                    end
60          end
61
62          httpd.print('<h2>Form Test</h2>')
63
64          httpd.print([[
65          <form method="POST" action="form?sender=me">
66          <input type="text" name="a_value">
67          <input type="submit">
68          </form>
69          ]])
70          -- output a footer
71          httpd.print([[
72                    </body>
73          </html>
74          ]])
75end
76
77function form(env, header, query)
78
79          httpd.write("HTTP/1.1 200 Ok\r\n")
80          httpd.write("Content-Type: text/html\r\n\r\n")
81
82          if query ~= nil then
83                    httpd.print('<h2>Form Variables</h2>')
84
85                    if env.CONTENT_TYPE ~= nil then
86                              httpd.print('Content-type: ' .. env.CONTENT_TYPE .. '<br>')
87                    end
88
89                    for k, v in pairs(query) do
90                              httpd.print(escape_html(k) .. '=' .. escape_html(v) .. '<br/>')
91                    end
92          else
93                    httpd.print('No values')
94          end
95end
96
97-- register this handler for http://<hostname>/<prefix>/printenv
98httpd.register_handler('printenv', printenv)
99httpd.register_handler('form', form)
100