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