From 16c64e5f35704bd3129f27960a2a1aafb23f2a1a Mon Sep 17 00:00:00 2001 From: Robert Abraham Date: Wed, 7 Mar 2012 21:08:32 +0100 Subject: [PATCH] actually use config parser --- bin/main.lua | 17 ++++++++--------- config/bad.conf | 1 + lib/answerRequest.lua | 4 ++-- lib/readConf.lua | 31 ++++++++++++++++++++++++++++--- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/bin/main.lua b/bin/main.lua index 98a0257..a6b2143 100644 --- a/bin/main.lua +++ b/bin/main.lua @@ -4,15 +4,12 @@ require( "../lib/getRequest" ) require( "../lib/answerRequest" ) require( "../lib/readConf" ) -local socket = require( "socket" ) - -local server = assert( socket.bind( "127.0.0.1", "9035" ) ) +readConfig() +local socket = require( "socket" ) +local server = assert( socket.bind( getIP(), getPort() ) ) local ip, port = server:getsockname() - local selectlist = {} -readConfig() -getConf() while true do local client = server:accept() @@ -23,11 +20,12 @@ while true do table.insert( selectlist, client ) end - list, _, err = socket.select( selectlist, nil, 0.1 ) + readlist, _, err = socket.select( selectlist, nil, 0.1 ) if err then print( err ) end - for i, cli in ipairs( list ) do + for i, cli in ipairs( readlist ) do + -- request stores the header + body local request = {} while true do @@ -55,6 +53,7 @@ while true do client:send( answerRequest( getList[2] ) ) end end + if request.POST ~= nil then local getList = getRequest( request.POST, " " ) if getList[1] ~= nil then @@ -62,7 +61,7 @@ while true do end end - --Debug Information + -- Debug Information prints complete request --for k,l in pairs( request ) do -- print( " key: " .. k .. " value: " .. l ) --end diff --git a/config/bad.conf b/config/bad.conf index 0e415d2..2a89d7a 100644 --- a/config/bad.conf +++ b/config/bad.conf @@ -1,2 +1,3 @@ root /home/roa/programming/badmoonrising port 9035 +listen 0.0.0.0 diff --git a/lib/answerRequest.lua b/lib/answerRequest.lua index 81257ea..d74bfbb 100644 --- a/lib/answerRequest.lua +++ b/lib/answerRequest.lua @@ -3,11 +3,11 @@ function answerRequest ( request ) local fileStatus = 0; request = cutTrailingSlash( request ) - local fh , err = io.open( "./" .. request, "r" ) + local fh , err = io.open( "./content/" .. request, "r" ) if fh == nil then fileStatus = 1; - fh , err = io.open( "./error.html", "r" ) + fh , err = io.open( "./content/error.html", "r" ) return prepareHeader( fileStatus ) .. prepareContent( fh ) else return prepareHeader( fileStatus ) .. prepareContent( fh ) diff --git a/lib/readConf.lua b/lib/readConf.lua index 3618f3f..b03f860 100644 --- a/lib/readConf.lua +++ b/lib/readConf.lua @@ -1,9 +1,13 @@ #!/usr/bin/lua +require( "lfs" ) + +local workDir = lfs.currentdir() + config = {} function readConfig () - confFile = "/home/roa/programming/badmoonrising/config/bad.conf" + confFile = workDir .. "/config/bad.conf" io.input( confFile ) while true do @@ -27,10 +31,13 @@ function cutLine ( line ) key = line:sub( 0, e -1 ) value = line:sub( e + 1 ) end + if key == "root" then validateRoot( key, value ) elseif key == "port" then validatePort( key, value ) + elseif key == "listen" then + validateIP( key, value) else return nil end @@ -44,8 +51,26 @@ function validatePort ( key, value ) config[key] = value; end -function getConf () +function validateIP ( key, value ) + config[key] = value; +end + +function getConf ( key ) for k, v in pairs(config) do - print("key: " .. k .. " value: " .. v) + if k == key then + return v + end end end + +function getRoot () + return getConf( "root" ) +end + +function getPort () + return getConf( "port") +end + +function getIP () + return getConf( "listen" ) +end