All pastes #728825 Raw Edit

Something

public text v1 · immutable
·published 2007-10-07 12:23 UTC
rendered paste body
--Load the socket library
require("socket")
require("copas")

--What port do we work out, should be 80 really
PORT = 80

--Webroot dir name
ROOTPATH = "htdocs"

--Define some vars
connections = 0

function printparadiso(text)
	print("Paradiso: "..text)
end

function ClientHandler(client)
	client = copas.wrap(client)
	connections = connections + 1
	local request = client:receive("*l")
	local _,start = string.find(request, 'GET ')
	local final,_ = string.find(request, ' HTTP', start)
	local path = string.sub(request, start+1, final-1);
	client:send("HTTP/1.x 200 OK\r\n")
	client:send("Server: Paradiso\r\n")
	client:send("Content-Type: text/html\r\n")
	client:send("Connection: close\r\n\r\n")
	if path == "/" or path == "" then
		local file = io.open(ROOTPATH.."/index.html", "r")
		if file ~= nil then
			local response = file:read("*a")
			client:send(response)
			file:close()
		else
			local file = io.open("errors/404.html", "r")
			local response = file:read("*a")	
			client:send(response.."1"..path)
			file:close()
		end
	elseif string.find(path,".") == nil then
		if string.sub(path,string.len(path)) == "/" then
			local file = io.open(ROOTPATH.."/"..path.."index.html", "r")
		else
			local file = io.open(ROOTPATH.."/"..path.."/index.html", "r")
		end
		if file ~= nil then
			local response = file:read("*a")
			client:send(response)
			file:close()
		else
			local file = io.open("errors/404.html", "r")
			local response = file:read("*a")	
			client:send(response.."2"..path)
			file:close()
		end	
	else
		local file = io.open(ROOTPATH.."/"..path, "r")
		if file ~= nil then
			local response = file:read("*a")
			client:send(response)
			file:close()
		else
			local file = io.open("errors/404.html", "r")
			local response = file:read("*a")	
			client:send(response.."3"..path)
			file:close()
		end
	end
	connections = connections - 1
end

copas.addserver(socket.bind("*", PORT), ClientHandler)
copas.loop()