forked from lionello/nodemcu-httpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpserver.lua
181 lines (157 loc) · 5.72 KB
/
httpserver.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
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
-- Small HTTP server for NodeMCU
--
-- PUT creates a new file in flash
-- GET returns the contents of a file in flash
-- DELETE remove the file from flash
-- POST executes the given lua script (may return a function that receives the payload)
-- OPTIONS returns minimal CORS headers allowing POST from any origin
--
-- TODO: need a way to return flash contents vs. index.html
local requests = {}
local indexhtml = "index.html"
local function close(c)
c:close()
end
local function onConnect(connection)
local function onReceive(connection, request)
collectgarbage()
local method, uri = request:match("^([A-Z]+) /([^?]*).- HTTP/[1-9]+.[0-9]+\r\n")
if method == "PUT" and uri ~= "" then
file.close()
if not file.open("temp/put", "w") then
connection:send("HTTP/1.1 500 Error\r\nConnection: close\r\n\r\nCreate error\n", close)
else
-- Track Content-Length so we know when we're done
local contentLength = tonumber(request:match("\r\nContent%-Length: (%S+)\r\n"))
local function done(connection)
collectgarbage()
file.close()
if contentLength <= 0 then
file.remove(uri)
file.rename("temp/put", uri)
else
file.remove("temp/put")
end
end
connection:on("disconnection", done)
-- From now on, send everything to the file
local function writeFile(connection, payload)
collectgarbage()
if file.write(payload) then
contentLength = contentLength - #payload
if contentLength <= 0 then
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nSaved\n", close)
done()
end
else
connection:send("HTTP/1.1 500 Error\r\nConnection: close\r\n\r\nWrite error\n", close)
done()
end
end
connection:on("receive", writeFile)
if request:find("\r\nExpect: 100-continue\r\n", 1, true) then
-- Send 100 Continue if the client expects it
connection:send("HTTP/1.1 100 Continue\r\nConnection: close\r\n\r\n")
else
-- Find the start of the body (if any)
local body_start = request:find("\r\n\r\n", 1, true) + 4
writeFile(connection, request:sub(body_start))
end
end
elseif method == "DELETE" then
-- Delete the file from flash
file.remove(uri)
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nDeleted\n", close)
elseif method == "POST" then
if uri == "" then
-- Reboot the device
connection:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nRebooting\n", node.restart)
else
-- Poor man's CGI
local func = dofile(uri)
if func then
func(connection, request)
-- If you want your function to receive any other packets as well, do this:
--connection:on("receive", func)
else
connection:send("HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n", close)
end
end
elseif method == "GET" then
-- Check for websocket header
local secwebsocketkey = request:find("\r\nSec-WebSocket-Key:", 1, true)
if secwebsocketkey then
local func = dofile(uri)
if func then
func(connection, request)
return
end
end
local function nextFile()
collectgarbage()
if #requests == 0 then return end
local connection, uri = unpack(requests[1])
print(uri)
-- Send the file contents to the client
local headers = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n"
--local encoding = request:find("\r\nAccept%-Encoding: gzip")
if #uri < 30 and file.open(uri .. ".gz", "r") then
headers = "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nConnection: close\r\n\r\n"
elseif not file.open(uri, "r") then
connection:send("HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\nFile not found\n", close)
table.remove(requests, 1)
nextFile()
return
end
local function onSent(connection)
collectgarbage()
local c = file.read(500)
if c then
connection:send(c)
else
file.close()
connection:close()
table.remove(requests, 1)
nextFile()
end
end
connection:on("sent", onSent)
connection:send(headers)
end
-- Default document handler
if uri == "" then
uri = indexhtml
end
table.insert(requests, { connection, uri })
if #requests == 1 then
nextFile()
end
elseif method == "OPTIONS" then
connection:send("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: POST,OPTIONS\r\nConnection: close\r\n\r\n", close)
else
connection:send("HTTP/1.1 501 Not Implemented\r\nConnection: close\r\n\r\n", close)
end
end
connection:on("receive", onReceive)
end
return function (port, index)
local s = net.createServer(net.TCP, 28800) -- 10 seconds client timeout
s:listen(port, onConnect)
-- false and nil evaluate as false
local ip = wifi.sta.getip()
local mac = wifi.sta.getmac()
if not ip then ip = wifi.ap.getip() mac = wifi.ap.getmac() end
print("nodemcu-httpd running at http://" .. ip .. ":" .. port)
if index then
indexhtml = index
end
-- register with backend
local http = net.createConnection(net.TCP, 0)
http:on("connection", function(c)
print(mac)
c:send("PUT /ip?mac=" .. mac .. "&ip=" .. ip ..
" HTTP/1.1\r\nHost: soundswarm.azurewebsites.net\r\nContent-Length: 0\r\nConnection: close\r\n\r\n", close)
end)
http:connect(80, "23.99.110.192")
return s
end