forked from marcoskirsch/nodemcu-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver-static.lua
More file actions
39 lines (36 loc) · 1.52 KB
/
httpserver-static.lua
File metadata and controls
39 lines (36 loc) · 1.52 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
-- httpserver-static.lua
-- Part of nodemcu-httpserver, handles sending static files to client.
-- Author: Marcos Kirsch
local function getMimeType(ext)
-- A few MIME types. Keep list short. If you need something that is missing, let's add it.
local mt = {css = "text/css", gif = "image/gif", html = "text/html", ico = "image/x-icon", jpeg = "image/jpeg", jpg = "image/jpeg", js = "application/javascript", json="application/json", png = "image/png"}
if mt[ext] then return mt[ext] else return "text/plain" end
end
local function sendHeader(connection, code, codeString, mimeType)
connection:send("HTTP/1.0 " .. code .. " " .. codeString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\nConnection: close\r\n\r\n")
end
return function (connection, args)
sendHeader(connection, 200, "OK", getMimeType(args.ext))
--print("Begin sending: ", args.file)
-- Send file in little chunks
local continue = true
local bytesSent = 0
while continue do
-- NodeMCU file API lets you open 1 file at a time.
-- So we need to open, seek, close each time in order
-- to support multiple simultaneous clients.
file.open(args.file)
file.seek("set", bytesSent)
local chunk = file.read(512)
file.close()
if chunk == nil then
continue = false
else
coroutine.yield()
connection:send(chunk)
bytesSent = bytesSent + #chunk
--print("Sent " .. args.file, bytesSent)
end
end
--print("Finished sending:", args.file)
end