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