Support for Lua scripts

This commit is contained in:
Marcos Kirsch 2015-02-15 16:51:21 -06:00
parent 00475af342
commit c0d6218397

View File

@ -37,7 +37,6 @@ local function onError(connection, errorCode, errorString)
print(errorCode .. ": " .. errorString) print(errorCode .. ": " .. errorString)
connection:send("HTTP/1.0 " .. errorCode .. " " .. errorString .. "\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n") connection:send("HTTP/1.0 " .. errorCode .. " " .. errorString .. "\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n")
connection:send("<html><head><title>" .. errorCode .. " - " .. errorString .. "</title></head><body><h1>" .. errorCode .. " - " .. errorString .. "</h1></body></html>\r\n") connection:send("<html><head><title>" .. errorCode .. " - " .. errorString .. "</title></head><body><h1>" .. errorCode .. " - " .. errorString .. "</h1></body></html>\r\n")
connection:close()
end end
local function parseUri(uri) local function parseUri(uri)
@ -51,6 +50,7 @@ local function parseUri(uri)
r.args = uri:sub(questionMarkPos+1, #uri) r.args = uri:sub(questionMarkPos+1, #uri)
end end
_, r.ext = r.file:match("(.+)%.(.+)") _, r.ext = r.file:match("(.+)%.(.+)")
r.isScript = r.ext == "lua"
return r return r
end end
@ -79,28 +79,34 @@ local function onGet(connection, uri)
if not fileExists then if not fileExists then
onError(connection, 404, "Not Found") onError(connection, 404, "Not Found")
else else
-- Use HTTP/1.0 to ensure client closes connection. if uri.isScript then
connection:send("HTTP/1.0 200 OK\r\nContent-Type: " .. getMimeType(uri.ext) .. "\r\Cache-Control: private, no-store\r\n\r\n") file.close()
-- Send file in little 128-byte chunks dofile(uriToFilename(uri.file))(connection, uri.args)
while true do else
local chunk = file.read(128) -- Use HTTP/1.0 to ensure client closes connection.
if chunk == nil then break end connection:send("HTTP/1.0 200 OK\r\nContent-Type: " .. getMimeType(uri.ext) .. "\r\Cache-Control: private, no-store\r\n\r\n")
connection:send(chunk) -- Send file in little 128-byte chunks
while true do
local chunk = file.read(128)
if chunk == nil then break end
connection:send(chunk)
end
file.close()
end end
connection:close()
file.close()
end end
collectgarbage()
end end
local function onReceive(connection, payload) local function onReceive(connection, payload)
print ("onReceive: We have a customer!")
--print(payload) -- for debugging --print(payload) -- for debugging
-- parse payload and decide what to serve. -- parse payload and decide what to serve.
parsedRequest = parseRequest(payload) local parsedRequest = parseRequest(payload)
print("Requested URI: " .. parsedRequest.uri)
parsedRequest.method = validateMethod(parsedRequest.method) parsedRequest.method = validateMethod(parsedRequest.method)
if parsedRequest.method == nil then onError(connection, 400, "Bad Request") if parsedRequest.method == nil then onError(connection, 400, "Bad Request")
elseif parsedRequest.method == "GET" then onGet(connection, parsedRequest.uri) elseif parsedRequest.method == "GET" then onGet(connection, parsedRequest.uri)
else onNotImplemented(connection, 501, "Not Implemented") end else onNotImplemented(connection, 501, "Not Implemented") end
connection:close()
end end
local function onSent(connection) local function onSent(connection)