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