NetBSD Problem Report #50502

From www@NetBSD.org  Mon Dec  7 02:41:44 2015
Return-Path: <www@NetBSD.org>
Received: from mail.netbsd.org (mail.NetBSD.org [199.233.217.200])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(Client CN "mail.netbsd.org", Issuer "Postmaster NetBSD.org" (verified OK))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 4B398A6551
	for <gnats-bugs@gnats.NetBSD.org>; Mon,  7 Dec 2015 02:41:44 +0000 (UTC)
Message-Id: <20151207024143.2E519A65EE@mollari.NetBSD.org>
Date: Mon,  7 Dec 2015 02:41:43 +0000 (UTC)
From: Tr@visPaul.me
Reply-To: Tr@visPaul.me
To: gnats-bugs@NetBSD.org
Subject: Update printenv.lua (bozohttpd lua example)
X-Send-Pr-Version: www-1.0

>Number:         50502
>Category:       misc
>Synopsis:       Update printenv.lua (bozohttpd lua example)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    misc-bug-people
>State:          closed
>Class:          doc-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Dec 07 02:45:00 +0000 2015
>Closed-Date:    Mon Dec 07 03:17:33 +0000 2015
>Last-Modified:  Mon Dec 07 03:17:33 +0000 2015
>Originator:     Travis Paul
>Release:        current
>Organization:
>Environment:
>Description:
src/libexec/httpd/printenv.lua which is intended as an example for using the Lua features of bozohttpd could be updated to be a better example.

The patch below updates printenv.lua so that it works when using SSL and sends the correct headers for HTML to be rendered by a client.
>How-To-Repeat:
Attempt to use printenv.lua over SSL, or submit the example form using a web browser, the result page will not be rendered as HTML.
>Fix:
Index: printenv.lua
===================================================================
RCS file: /cvsroot/src/libexec/httpd/printenv.lua,v
retrieving revision 1.2
diff -u -r1.2 printenv.lua
--- printenv.lua	2 Jan 2014 08:21:38 -0000	1.2
+++ printenv.lua	7 Dec 2015 02:30:48 -0000
@@ -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 @@
 	-- 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([[
 		<html>
 			<head>
 				<title>Bozotic Lua Environment</title>
@@ -29,54 +35,58 @@
 				<h1>Bozotic Lua Environment</h1>
 	]])

-	print('module version: ' .. httpd._VERSION .. '<br>')
+	httpd.print('module version: ' .. httpd._VERSION .. '<br>')

-	print('<h2>Server Environment</h2>')
+	httpd.print('<h2>Server Environment</h2>')
 	-- print the list of "environment" variables
 	for k, v in pairs(env) do
-		print(k .. '=' .. v .. '<br/>')
+		httpd.print(k .. '=' .. v .. '<br/>')
 	end

-	print('<h2>Request Headers</h2>')
+	httpd.print('<h2>Request Headers</h2>')
 	for k, v in pairs(headers) do
-		print(k .. '=' .. v .. '<br/>')
+		httpd.print(k .. '=' .. v .. '<br/>')
 	end

 	if query ~= nil then
-		print('<h2>Query Variables</h2>')
+		httpd.print('<h2>Query Variables</h2>')
 		for k, v in pairs(query) do
-			print(k .. '=' .. v .. '<br/>')
+			httpd.print(k .. '=' .. v .. '<br/>')
 		end
 	end

-	print('<h2>Form Test</h2>')
+	httpd.print('<h2>Form Test</h2>')

-	print([[
-	<form method="POST" action="/rest/form?sender=me">
+	httpd.print([[
+	<form method="POST" action="form?sender=me">
 	<input type="text" name="a_value">
 	<input type="submit">
 	</form>
 	]])
 	-- output a footer
-	print([[
+	httpd.print([[
 		</body>
 	</html>
 	]])
 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('<h2>Form Variables</h2>')
+		httpd.print('<h2>Form Variables</h2>')

 		if env.CONTENT_TYPE ~= nil then
-			print('Content-type: ' .. env.CONTENT_TYPE .. '<br>')
+			httpd.print('Content-type: ' .. env.CONTENT_TYPE .. '<br>')
 		end

 		for k, v in pairs(query) do
-			print(k .. '=' .. v .. '<br/>')
+			httpd.print(k .. '=' .. v .. '<br/>')
 		end
 	else
-		print('No values')
+		httpd.print('No values')
 	end
 end


>Release-Note:

>Audit-Trail:
From: "Kamil Rytarowski" <kamil@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/50502 CVS commit: src/libexec/httpd
Date: Mon, 7 Dec 2015 03:11:48 +0000

 Module Name:	src
 Committed By:	kamil
 Date:		Mon Dec  7 03:11:48 UTC 2015

 Modified Files:
 	src/libexec/httpd: printenv.lua

 Log Message:
 Improve the httpd(8) printenv.lua Lua example

 Stop using Lua builtin print function and replace them with http.* ones.
 httpd.print and http.write wraps SSL support when needed.

 Print http headers, without them browser may interpret page as raw text.

 No need to hardcode prefix path in the form.

 Add comments for a user with tips how to use this script.

 Patch by Travis Paul

 Closes PR misc/50502


 To generate a diff of this commit:
 cvs rdiff -u -r1.2 -r1.3 src/libexec/httpd/printenv.lua

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->closed
State-Changed-By: kamil@NetBSD.org
State-Changed-When: Mon, 07 Dec 2015 04:17:33 +0100
State-Changed-Why:
Patch has been committed


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2014 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.