summaryrefslogtreecommitdiff
path: root/libexec/httpd/printenv.lua
diff options
context:
space:
mode:
authormbalmer <mbalmer@NetBSD.org>2013-10-12 17:24:06 +0000
committermbalmer <mbalmer@NetBSD.org>2013-10-12 17:24:06 +0000
commitc58dfba623533ca1a8cf58acc2232ed0200b2638 (patch)
tree8e0fdc885eee11d6b3221563dbf1c5a33079e555 /libexec/httpd/printenv.lua
parent37c63df0d4cbb99d42b5f11a70911443db9fb63e (diff)
add Lua scripting support to bozohttpd, see httpd(8) for details
Diffstat (limited to 'libexec/httpd/printenv.lua')
-rw-r--r--libexec/httpd/printenv.lua83
1 files changed, 83 insertions, 0 deletions
diff --git a/libexec/httpd/printenv.lua b/libexec/httpd/printenv.lua
new file mode 100644
index 00000000000..5ef8305293d
--- /dev/null
+++ b/libexec/httpd/printenv.lua
@@ -0,0 +1,83 @@
+-- this small Lua script demonstrates the use of Lua in (bozo)httpd
+-- it will simply output the "environment"
+
+-- Keep in mind that bozohttpd forks for each request when started in
+-- daemon mode, you can set global veriables here, but they will have
+-- the same value on each invocation. You can not keep state between
+-- two calls.
+
+local httpd = require 'httpd'
+
+function printenv(env, headers, query)
+
+ -- we get the "environment" in the env table, the values are more
+ -- or less the same as the variable for a CGI program
+
+ if count == nil then
+ count = 1
+ end
+
+ -- output a header
+ print([[
+ <html>
+ <head>
+ <title>Bozotic Lua Environment</title>
+ </head>
+ <body>
+ <h1>Bozotic Lua Environment</h1>
+ ]])
+
+ print('module version: ' .. httpd._VERSION .. '<br>')
+
+ print('<h2>Server Environment</h2>')
+ -- print the list of "environment" variables
+ for k, v in pairs(env) do
+ print(k .. '=' .. v .. '<br/>')
+ end
+
+ print('<h2>Request Headers</h2>')
+ for k, v in pairs(headers) do
+ print(k .. '=' .. v .. '<br/>')
+ end
+
+ if query ~= nil then
+ print('<h2>Query Variables</h2>')
+ for k, v in pairs(query) do
+ print(k .. '=' .. v .. '<br/>')
+ end
+ end
+
+ print('<h2>Form Test</h2>')
+
+ print([[
+ <form method="POST" action="/rest/form?sender=me">
+ <input type="text" name="a_value">
+ <input type="submit">
+ </form>
+ ]])
+ -- output a footer
+ print([[
+ </body>
+ </html>
+ ]])
+end
+
+function form(env, header, query)
+ if query ~= nil then
+ print('<h2>Form Variables</h2>')
+
+ if env.CONTENT_TYPE ~= nil then
+ print('Content-type: ' .. env.CONTENT_TYPE .. '<br>')
+ end
+
+ for k, v in pairs(query) do
+ print(k .. '=' .. v .. '<br/>')
+ end
+ else
+ print('No values')
+ end
+end
+
+-- register this handler for http://<hostname>/<prefix>/printenv
+httpd.register_handler('printenv', printenv)
+httpd.register_handler('form', form)