forked from marcoskirsch/nodemcu-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.lua
More file actions
215 lines (191 loc) · 9.08 KB
/
httpserver.lua
File metadata and controls
215 lines (191 loc) · 9.08 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
-- httpserver
-- Author: Marcos Kirsch
-- Starts web server in the specified port.
return function (port)
local s = net.createServer(net.TCP, 10) -- 10 seconds client timeout
s:listen(
port,
function (connection)
-- This variable holds the thread (actually a Lua coroutine) used for sending data back to the user.
-- We do it in a separate thread because we need to send in little chunks and wait for the onSent event
-- before we can send more, or we risk overflowing the mcu's buffer.
local connectionThread
local fileInfo
local allowStatic = {GET=true, HEAD=true, POST=false, PUT=false, DELETE=false, TRACE=false, OPTIONS=false, CONNECT=false, PATCH=false}
-- Pretty log function.
local function log(connection, msg, optionalMsg)
local port, ip = connection:getpeer()
if(optionalMsg == nil) then
print(ip .. ":" .. port, msg)
else
print(ip .. ":" .. port, msg, optionalMsg)
end
end
local function startServingStatic(connection, req, args)
fileInfo = dofile("httpserver-static.lc")(connection, req, args)
end
local function startServing(fileServeFunction, connection, req, args)
connectionThread = coroutine.create(function(fileServeFunction, bufferedConnection, req, args)
fileServeFunction(bufferedConnection, req, args)
-- The bufferedConnection may still hold some data that hasn't been sent. Flush it before closing.
if not bufferedConnection:flush() then
log(connection, "closing connetion", "no (more) data")
connection:close()
connectionThread = nil
collectgarbage()
end
end)
local BufferedConnectionClass = dofile("httpserver-connection.lc")
local bufferedConnection = BufferedConnectionClass:new(connection)
BufferedConnectionClass = nil
local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args)
if not status then
log(connection, "Error: "..err)
log(connection, "closing connetion", "error")
connection:close()
connectionThread = nil
collectgarbage()
end
end
local function handleRequest(connection, req, handleError)
collectgarbage()
local method = req.method
local uri = req.uri
local fileServeFunction = nil
if not allowStatic[method] and not uri.isScript then
return handleError(connection, req, 405, "Allow: GET, HEAD")
end
if #(uri.file) > 32 then
-- nodemcu-firmware cannot handle long filenames.
return handleError(connection, req, 404)
else
local fileExists = file.open(uri.file, "r")
file.close()
if not fileExists then
-- gzip check
fileExists = file.open(uri.file .. ".gz", "r")
file.close()
if fileExists then
--print("gzip variant exists, serving that one")
uri.file = uri.file .. ".gz"
uri.isGzipped = true
end
end
if not fileExists then
return handleError(connection, req, 404)
elseif uri.isScript then
fileServeFunction = dofile(uri.file)
startServing(fileServeFunction, connection, req, uri.args)
else
uri.args = {file = uri.file, ext = uri.ext, isGzipped = uri.isGzipped}
startServingStatic(connection, req, uri.args)
end
end
end
local function handleError(connection, request, code, header)
dofile("httpserver-geterrorpage.lc")(connection, request, code, header)
handleRequest(connection, request, handleError)
end
local function onReceive(connection, payload)
collectgarbage()
local conf = dofile("httpserver-conf.lc")
local auth
local user = "Anonymous"
-- as suggest by anyn99 (https://github.com/marcoskirsch/nodemcu-httpserver/issues/36#issuecomment-167442461)
-- Some browsers send the POST data in multiple chunks.
-- Collect data packets until the size of HTTP body meets the Content-Length stated in header
if payload:find("Content%-Length:") or bBodyMissing then
if fullPayload then fullPayload = fullPayload .. payload else fullPayload = payload end
if (tonumber(string.match(fullPayload, "%d+", fullPayload:find("Content%-Length:")+16)) > #fullPayload:sub(fullPayload:find("\r\n\r\n", 1, true)+4, #fullPayload)) then
bBodyMissing = true
return
else
--print("HTTP packet assembled! size: "..#fullPayload)
payload = fullPayload
fullPayload, bBodyMissing = nil
end
end
collectgarbage()
-- parse payload and decide what to serve.
local req = dofile("httpserver-request.lc")(payload)
if not req then
-- create minimal req to allow geterrorpage to do its work
req = {uri = {}}
log(connection, "Empty request")
return handleError(connection, req, 400)
end
log(connection, req.method, req.request)
if conf.auth.enabled then
auth = dofile("httpserver-basicauth.lc")
user = auth.authenticate(payload) -- authenticate returns nil on failed auth
end
if user and req.methodIsValid and (req.method == "GET" or req.method == "POST" or req.method == "PUT") then
handleRequest(connection, req, handleError)
else
if not user then -- Not Authorized
return handleError(connection, req, 401, auth.authErrorHeader())
elseif req.methodIsValid then -- Not Implemented
return handleError(connection, req, 501)
else -- Bad Request
return handleError(connection, req, 400)
end
end
end
local function onSent(connection, payload)
collectgarbage()
if connectionThread then
local connectionThreadStatus = coroutine.status(connectionThread)
if connectionThreadStatus == "suspended" then
-- Not finished sending file, resume.
local status, err = coroutine.resume(connectionThread)
if not status then
log(connection, "Error: "..err)
log(connection, "closing connetion", "error")
connection:close()
connectionThread = nil
collectgarbage()
end
elseif connectionThreadStatus == "dead" then
-- We're done sending file.
log(connection, "closing connetion","thread is dead")
connection:close()
connectionThread = nil
collectgarbage()
end
elseif fileInfo then
local fileSize = file.list()[fileInfo.file]
-- Chunks larger than 1024 don't work.
-- https://github.com/nodemcu/nodemcu-firmware/issues/1075
local chunkSize = 1024
local fileHandle = file.open(fileInfo.file)
if fileSize > fileInfo.sent then
fileHandle:seek("set", fileInfo.sent)
local chunk = fileHandle:read(chunkSize)
fileHandle:close()
fileHandle = nil
fileInfo.sent = fileInfo.sent + #chunk
connection:send(chunk)
-- print(fileInfo.file .. ": Sent "..#chunk.. " bytes, " .. fileSize - fileInfo.sent .. " to go.")
chunk = nil
else
log(connection, "closing connetion", "Finished sending: "..fileInfo.file)
connection:close()
fileInfo = nil
end
collectgarbage()
end
end
local function onDisconnect(connection, payload)
print("disconnected")
if connectionThread then
connectionThread = nil
collectgarbage()
end
end
connection:on("receive", onReceive)
connection:on("sent", onSent)
connection:on("disconnection", onDisconnect)
end
)
return s
end