Shortened code.

Reimplemented parseRequest (simpler). Shortened onReceive()
This commit is contained in:
Marcos Kirsch 2015-02-14 01:48:49 -06:00
parent 6e58f58b6d
commit b75d610e6a

View File

@ -20,23 +20,13 @@ local function validateMethod(method)
return nil return nil
end end
local function parseRequest(request) function parseRequest(request)
local result = {} local e = request:find("\r\n", 1, true)
local matchEnd = 0 if not e then return nil end
local line = request:sub(1, e - 1)
local matchBegin = matchEnd + 1 local r = {}
matchEnd = string.find (request, " ", matchBegin) _, i, r.method, r.uri = line:find("^([A-Z]+) (.-) HTTP/[1-9]+.[1-9]+$")
result.method = string.sub(request, matchBegin, matchEnd-1) return r
matchBegin = matchEnd + 1
matchEnd = string.find(request, " ", matchBegin)
result.uri = string.sub(request, matchBegin, matchEnd-1)
matchBegin = matchEnd + 1
matchEnd = string.find(request, "\r\n", matchBegin)
result.version = string.sub(request, matchBegin, matchEnd-1)
return result
end end
local function uriToFilename(uri) local function uriToFilename(uri)
@ -73,24 +63,12 @@ end
local function onReceive(connection, payload) local function onReceive(connection, payload)
print ("onReceive: We have a customer!") 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) parsedRequest = parseRequest(payload)
parsedRequest.method = validateMethod(parsedRequest.method)
method = validateMethod(parsedRequest.method) if parsedRequest.method == nil then onError(connection, 400, "Bad Request")
elseif parsedRequest.method == "GET" then onGet(connection, parsedRequest.uri)
if method == nil then else onNotImplemented(connection, 501, "Not Implemented") end
onError(connection, 400, "Bad Request")
return
end
if method == "GET" then
onGet(connection, parsedRequest.uri)
return
end
onNotImplemented(connection, 501, "Not Implemented")
end end
local function onSent(connection) local function onSent(connection)