view web/src/luan/modules/web/web_run.luan @ 222:b76fcb72d97d

add AbstractLuanTable and improve HttpLuan git-svn-id: https://luan-java.googlecode.com/svn/trunk@223 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 21 Jul 2014 02:23:05 +0000
parents 5ba136769034
children 392105b660d7
line wrap: on
line source

import "Io"
import "web/Http"
import "String"

local function lines(s)
	local matcher = String.gmatch(s,"([^\n]*)\n|([^\n])+$")
	return function()
		local m1, m2 = matcher()
		return m1 or m2
	end
end

local function print_with_line_numbers(s)
	i = 1
	for line in lines(s) do
		print(i,line)
		i = i + 1
	end
end

function service()
	local content_type = Http.request.parameters.content_type
	if content_type ~= nil then
		Http.response.set_content_type(content_type)
	end
	Io.stdout = Http.response.text_writer()
	local code = Http.request.parameters.code
	local env = {
		request = Http.request;
		response = Http.response;
	}
	try
		local run = load(code,"<web_run>",env)
		run()
	catch e do
		Http.response.set_content_type "text/plain"
		print(e)
		print()
		print()
		print_with_line_numbers(code)
	end
end