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