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