Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/libexec/httpd/printenv.lua,v rcsdiff: /ftp/cvs/cvsroot/src/libexec/httpd/printenv.lua,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.2 retrieving revision 1.2.18.1 diff -u -p -r1.2 -r1.2.18.1 --- src/libexec/httpd/printenv.lua 2014/01/02 08:21:38 1.2 +++ src/libexec/httpd/printenv.lua 2016/04/10 10:33:11 1.2.18.1 @@ -1,4 +1,4 @@ --- $NetBSD: printenv.lua,v 1.2 2014/01/02 08:21:38 mrg Exp $ +-- $NetBSD: printenv.lua,v 1.2.18.1 2016/04/10 10:33:11 martin Exp $ -- this small Lua script demonstrates the use of Lua in (bozo)httpd -- it will simply output the "environment" @@ -8,6 +8,10 @@ -- the same value on each invocation. You can not keep state between -- two calls. +-- You can test this example by running the following command: +-- /usr/libexec/httpd -b -f -I 8080 -L test printenv.lua . +-- and then navigate to: http://127.0.0.1:8080/test/printenv + local httpd = require 'httpd' function printenv(env, headers, query) @@ -15,12 +19,14 @@ function printenv(env, headers, query) -- we get the "environment" in the env table, the values are more -- or less the same as the variable for a CGI program - if count == nil then - count = 1 - end - - -- output a header - print([[ + -- output headers using httpd.write() + -- httpd.write() will not append newlines + httpd.write("HTTP/1.1 200 Ok\r\n") + httpd.write("Content-Type: text/html\r\n\r\n") + + -- output html using httpd.print() + -- you can also use print() and io.write() but they will not work with SSL + httpd.print([[ Bozotic Lua Environment @@ -29,54 +35,58 @@ function printenv(env, headers, query)

Bozotic Lua Environment

]]) - print('module version: ' .. httpd._VERSION .. '
') + httpd.print('module version: ' .. httpd._VERSION .. '
') - print('

Server Environment

') + httpd.print('

Server Environment

') -- print the list of "environment" variables for k, v in pairs(env) do - print(k .. '=' .. v .. '
') + httpd.print(k .. '=' .. v .. '
') end - print('

Request Headers

') + httpd.print('

Request Headers

') for k, v in pairs(headers) do - print(k .. '=' .. v .. '
') + httpd.print(k .. '=' .. v .. '
') end if query ~= nil then - print('

Query Variables

') + httpd.print('

Query Variables

') for k, v in pairs(query) do - print(k .. '=' .. v .. '
') + httpd.print(k .. '=' .. v .. '
') end end - print('

Form Test

') + httpd.print('

Form Test

') - print([[ -
+ httpd.print([[ +
]]) -- output a footer - print([[ + httpd.print([[ ]]) end function form(env, header, query) + + httpd.write("HTTP/1.1 200 Ok\r\n") + httpd.write("Content-Type: text/html\r\n\r\n") + if query ~= nil then - print('

Form Variables

') + httpd.print('

Form Variables

') if env.CONTENT_TYPE ~= nil then - print('Content-type: ' .. env.CONTENT_TYPE .. '
') + httpd.print('Content-type: ' .. env.CONTENT_TYPE .. '
') end for k, v in pairs(query) do - print(k .. '=' .. v .. '
') + httpd.print(k .. '=' .. v .. '
') end else - print('No values') + httpd.print('No values') end end