forked from devyte/nodemcu-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpserver-servefunction.lua
61 lines (53 loc) · 2.22 KB
/
httpserver-servefunction.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- httpserver-servefunction
-- Author: Daniel Salazar
-- Based on work by Marcos Kirsch
return function (connection, payload)
local conf = dofile("confload.lc")("httpserver-conf.lc")
local auth
local user = "Anonymous"
-- parse payload and decide what to serve.
if conf.auth.enabled then
auth = dofile("httpserver-basicauth.lc")
user = auth.authenticate(payload, conf) -- authenticate returns nil on failed auth
end
local req = dofile("httpserver-request.lc")(payload)
local serveFunction = nil
local methodIsAllowed = {GET=true, POST=true, PUT=true}
if user and req.methodIsValid and methodIsAllowed[req.method] and #(req.uri.file) <= 31 then
print(req.method .. ": " .. req.request)
local uri = req.uri
local fileExists = file.exists(uri.file)
if not fileExists then
fileExists = file.exists(uri.file .. ".gz")
if fileExists then
uri.file = uri.file .. ".gz"
uri.isGzipped = true
end
end
if not fileExists then
uri.args = {code = 404, errorString = "Not Found"}
serveFunction = dofile("httpserver-error.lc")
elseif uri.isScript then
serveFunction = dofile(uri.file)
else
local allowStatic = {GET=true, HEAD=true, POST=false, PUT=false, DELETE=false, TRACE=false, OPTIONS=false, CONNECT=false, PATCH=false}
if allowStatic[req.method] then
uri.args = {file = uri.file, ext = uri.ext, gzipped = uri.isGzipped}
serveFunction = dofile("httpserver-static.lc")
else
uri.args = {code = 405, errorString = "Method not supported"}
serveFunction = dofile("httpserver-error.lc")
end
end
else
serveFunction = dofile("httpserver-error.lc")
if not user then
req.uri.args = {code = 401, errorString = "Not Authorized", headers = {auth.authErrorHeader(conf)}}
elseif req.methodIsValid then
req.uri.args = {code = 501, errorString = "Not Implemented"}
else
req.uri.args = {code = 400, errorString = "Bad Request"}
end
end
return serveFunction, req
end