From b1d5f9fc3559d3eeb8ab81cd7a831248a57b30c3 Mon Sep 17 00:00:00 2001 From: roa Date: Sun, 4 Mar 2012 23:17:00 +0100 Subject: [PATCH] initial --- .gitignore | 3 +++ README | 1 + main.lua | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 .gitignore create mode 100644 README create mode 100644 main.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d0431d6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.html +*.ico +*.so diff --git a/README b/README new file mode 100644 index 0000000..a1b2e0e --- /dev/null +++ b/README @@ -0,0 +1 @@ +Webserver written in Lua diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..dfbedbd --- /dev/null +++ b/main.lua @@ -0,0 +1,77 @@ +#/usr/bin/lua + +function getRequest (request) + local s = 0 + local e = 0 + local list = {} + for counter = 1, 3, 1 do + e = request:find( " ", s ) + if e == nil then break + else + local tmpstr = request:sub( s, e - 1 ) + list[counter] = tmpstr + s = e + 1 + end + end + return list +end + +function answerRequest (request) + request = cutTrailingSlash( request ) + local fh , err = io.open( "./" .. request, "r" ) + if fh == nil then + fh , err = io.open( "./error.html", "r" ) + end + return prepareHeader() .. prepareContent( fh ) +end + +function prepareHeader () + local status = 'HTTP/1.1 200 OK\r\n' + local date = 'Date: Sun, 04 Mar 2012 18:54:40 GMT\r\n' + local servername = 'Server: Apache/2.2.14 (Ubuntu)\r\n' + local conn = 'Connection: close\r\n' + local contenttype = 'Content-Type: text/html; charset=iso-8859-1\r\n\r\n' + + local html = "" + html = html .. status + html = html .. date + html = html .. servername + html = html .. conn + html = html .. contenttype + return html +end + +function prepareContent (fh) + local html = "" + while true do + local line = fh.read(fh) + if not line then break end + html = html .. line + end + return html +end + +function cutTrailingSlash (string) + if string:find( "/" ) == 1 then + return string:sub( 2 ) + end +end + +local socket = require( "socket" ) + +local server = assert( socket.bind( "127.0.0.1", "9035" ) ) + +local ip, port = server:getsockname() + +while 1 do + local client = server:accept() + client:settimeout(10) + local line, err = client:receive() + if not err then + local getList = getRequest(line) + if getList[1] == "GET" then + client:send( answerRequest( getList[2] ) ) + end + end + client:close() +end