Support for Lua scripts
This commit is contained in:
parent
00475af342
commit
c0d6218397
@ -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
|
||||||
|
|
||||||
@ -78,6 +78,10 @@ local function onGet(connection, uri)
|
|||||||
local fileExists = file.open(uriToFilename(uri.file), "r")
|
local fileExists = file.open(uriToFilename(uri.file), "r")
|
||||||
if not fileExists then
|
if not fileExists then
|
||||||
onError(connection, 404, "Not Found")
|
onError(connection, 404, "Not Found")
|
||||||
|
else
|
||||||
|
if uri.isScript then
|
||||||
|
file.close()
|
||||||
|
dofile(uriToFilename(uri.file))(connection, uri.args)
|
||||||
else
|
else
|
||||||
-- Use HTTP/1.0 to ensure client closes connection.
|
-- Use HTTP/1.0 to ensure client closes connection.
|
||||||
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("HTTP/1.0 200 OK\r\nContent-Type: " .. getMimeType(uri.ext) .. "\r\Cache-Control: private, no-store\r\n\r\n")
|
||||||
@ -87,20 +91,22 @@ local function onGet(connection, uri)
|
|||||||
if chunk == nil then break end
|
if chunk == nil then break end
|
||||||
connection:send(chunk)
|
connection:send(chunk)
|
||||||
end
|
end
|
||||||
connection:close()
|
|
||||||
file.close()
|
file.close()
|
||||||
end
|
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)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user