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