printenv.lua revision 1.2.20.2 1 1.2.20.2 tls -- $NetBSD: printenv.lua,v 1.2.20.2 2014/08/20 00:02:22 tls Exp $
2 1.2.20.2 tls
3 1.2.20.2 tls -- this small Lua script demonstrates the use of Lua in (bozo)httpd
4 1.2.20.2 tls -- it will simply output the "environment"
5 1.2.20.2 tls
6 1.2.20.2 tls -- Keep in mind that bozohttpd forks for each request when started in
7 1.2.20.2 tls -- daemon mode, you can set global veriables here, but they will have
8 1.2.20.2 tls -- the same value on each invocation. You can not keep state between
9 1.2.20.2 tls -- two calls.
10 1.2.20.2 tls
11 1.2.20.2 tls local httpd = require 'httpd'
12 1.2.20.2 tls
13 1.2.20.2 tls function printenv(env, headers, query)
14 1.2.20.2 tls
15 1.2.20.2 tls -- we get the "environment" in the env table, the values are more
16 1.2.20.2 tls -- or less the same as the variable for a CGI program
17 1.2.20.2 tls
18 1.2.20.2 tls if count == nil then
19 1.2.20.2 tls count = 1
20 1.2.20.2 tls end
21 1.2.20.2 tls
22 1.2.20.2 tls -- output a header
23 1.2.20.2 tls print([[
24 1.2.20.2 tls <html>
25 1.2.20.2 tls <head>
26 1.2.20.2 tls <title>Bozotic Lua Environment</title>
27 1.2.20.2 tls </head>
28 1.2.20.2 tls <body>
29 1.2.20.2 tls <h1>Bozotic Lua Environment</h1>
30 1.2.20.2 tls ]])
31 1.2.20.2 tls
32 1.2.20.2 tls print('module version: ' .. httpd._VERSION .. '<br>')
33 1.2.20.2 tls
34 1.2.20.2 tls print('<h2>Server Environment</h2>')
35 1.2.20.2 tls -- print the list of "environment" variables
36 1.2.20.2 tls for k, v in pairs(env) do
37 1.2.20.2 tls print(k .. '=' .. v .. '<br/>')
38 1.2.20.2 tls end
39 1.2.20.2 tls
40 1.2.20.2 tls print('<h2>Request Headers</h2>')
41 1.2.20.2 tls for k, v in pairs(headers) do
42 1.2.20.2 tls print(k .. '=' .. v .. '<br/>')
43 1.2.20.2 tls end
44 1.2.20.2 tls
45 1.2.20.2 tls if query ~= nil then
46 1.2.20.2 tls print('<h2>Query Variables</h2>')
47 1.2.20.2 tls for k, v in pairs(query) do
48 1.2.20.2 tls print(k .. '=' .. v .. '<br/>')
49 1.2.20.2 tls end
50 1.2.20.2 tls end
51 1.2.20.2 tls
52 1.2.20.2 tls print('<h2>Form Test</h2>')
53 1.2.20.2 tls
54 1.2.20.2 tls print([[
55 1.2.20.2 tls <form method="POST" action="/rest/form?sender=me">
56 1.2.20.2 tls <input type="text" name="a_value">
57 1.2.20.2 tls <input type="submit">
58 1.2.20.2 tls </form>
59 1.2.20.2 tls ]])
60 1.2.20.2 tls -- output a footer
61 1.2.20.2 tls print([[
62 1.2.20.2 tls </body>
63 1.2.20.2 tls </html>
64 1.2.20.2 tls ]])
65 1.2.20.2 tls end
66 1.2.20.2 tls
67 1.2.20.2 tls function form(env, header, query)
68 1.2.20.2 tls if query ~= nil then
69 1.2.20.2 tls print('<h2>Form Variables</h2>')
70 1.2.20.2 tls
71 1.2.20.2 tls if env.CONTENT_TYPE ~= nil then
72 1.2.20.2 tls print('Content-type: ' .. env.CONTENT_TYPE .. '<br>')
73 1.2.20.2 tls end
74 1.2.20.2 tls
75 1.2.20.2 tls for k, v in pairs(query) do
76 1.2.20.2 tls print(k .. '=' .. v .. '<br/>')
77 1.2.20.2 tls end
78 1.2.20.2 tls else
79 1.2.20.2 tls print('No values')
80 1.2.20.2 tls end
81 1.2.20.2 tls end
82 1.2.20.2 tls
83 1.2.20.2 tls -- register this handler for http://<hostname>/<prefix>/printenv
84 1.2.20.2 tls httpd.register_handler('printenv', printenv)
85 1.2.20.2 tls httpd.register_handler('form', form)
86