Update httpserver code, fixes scripts running
This commit is contained in:
parent
dfffb6b03e
commit
708b7cf926
3 changed files with 105 additions and 1 deletions
37
nodemcu/http/wifi_settings.lua
Normal file
37
nodemcu/http/wifi_settings.lua
Normal file
|
@ -0,0 +1,37 @@
|
|||
return function (connection, req, args)
|
||||
local staConfig = {ssid="", pwd=""}
|
||||
|
||||
connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n")
|
||||
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Wifi Settings</title></head><body><h1>Wifi settings</h1><pre>')
|
||||
|
||||
connection:send("SSID: "..staConfig.ssid)
|
||||
connection:send("\nPassword: "..staConfig.pwd)
|
||||
|
||||
connection:send('</pre><br /><br />')
|
||||
|
||||
local form = [===[
|
||||
<form method="POST">
|
||||
SSID:<br><input type="text" name="ssid"><br>
|
||||
Password:<br><input type="text" name="password"><br>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
]===]
|
||||
|
||||
if req.method == "GET" then
|
||||
connection:send(form)
|
||||
elseif req.method == "POST" then
|
||||
local rd = req.getRequestData()
|
||||
-- connection:send(cjson.encode(rd))
|
||||
connection:send('<h2>Received the following values:</h2>')
|
||||
connection:send("<ul>\n")
|
||||
for name, value in pairs(rd) do
|
||||
connection:send('<li><b>' .. name .. ':</b> ' .. tostring(value) .. "<br></li>\n")
|
||||
end
|
||||
|
||||
connection:send("</ul>\n")
|
||||
else
|
||||
connection:send("NOT IMPLEMENTED")
|
||||
end
|
||||
|
||||
connection:send('</body></html>')
|
||||
end
|
67
nodemcu/httpserver-connection.lua
Normal file
67
nodemcu/httpserver-connection.lua
Normal file
|
@ -0,0 +1,67 @@
|
|||
-- httpserver-connection
|
||||
-- Part of nodemcu-httpserver, provides a buffered connection object that can handle multiple
|
||||
-- consecutive send() calls, and buffers small payloads to send once they get big.
|
||||
-- For this to work, it must be used from a coroutine and owner is responsible for the final
|
||||
-- flush() and for closing the connection.
|
||||
-- Author: Philip Gladstone, Marcos Kirsch
|
||||
|
||||
BufferedConnection = {}
|
||||
|
||||
-- parameter is the nodemcu-firmware connection
|
||||
function BufferedConnection:new(connection)
|
||||
local newInstance = {}
|
||||
newInstance.connection = connection
|
||||
newInstance.size = 0
|
||||
newInstance.data = {}
|
||||
|
||||
function newInstance:flush()
|
||||
if self.size > 0 then
|
||||
self.connection:send(table.concat(self.data, ""))
|
||||
self.data = {}
|
||||
self.size = 0
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function newInstance:send(payload)
|
||||
local flushthreshold = 1400
|
||||
|
||||
local newsize = self.size + payload:len()
|
||||
while newsize > flushthreshold do
|
||||
--STEP1: cut out piece from payload to complete threshold bytes in table
|
||||
local piecesize = flushthreshold - self.size
|
||||
local piece = payload:sub(1, piecesize)
|
||||
payload = payload:sub(piecesize + 1, -1)
|
||||
--STEP2: insert piece into table
|
||||
table.insert(self.data, piece)
|
||||
self.size = self.size + piecesize --size should be same as flushthreshold
|
||||
--STEP3: flush entire table
|
||||
if self:flush() then
|
||||
coroutine.yield()
|
||||
end
|
||||
--at this point, size should be 0, because the table was just flushed
|
||||
newsize = self.size + payload:len()
|
||||
end
|
||||
|
||||
--at this point, whatever is left in payload should be <= flushthreshold
|
||||
local plen = payload:len()
|
||||
if plen == flushthreshold then
|
||||
--case 1: what is left in payload is exactly flushthreshold bytes (boundary case), so flush it
|
||||
table.insert(self.data, payload)
|
||||
self.size = self.size + plen
|
||||
if self:flush() then
|
||||
coroutine.yield()
|
||||
end
|
||||
elseif payload:len() then
|
||||
--case 2: what is left in payload is less than flushthreshold, so just leave it in the table
|
||||
table.insert(self.data, payload)
|
||||
self.size = self.size + plen
|
||||
--else, case 3: nothing left in payload, so do nothing
|
||||
end
|
||||
end
|
||||
|
||||
return newInstance
|
||||
end
|
||||
|
||||
return BufferedConnection
|
|
@ -39,7 +39,7 @@ local compileAndRemoveIfNeeded = function(f)
|
|||
end
|
||||
end
|
||||
|
||||
local serverFiles = {'httpserver.lua', 'httpserver-basicauth.lua', 'httpserver-conf.lua', 'httpserver-b64decode.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua'}
|
||||
local serverFiles = {'httpserver.lua', 'httpserver-basicauth.lua', 'httpserver-conf.lua', 'httpserver-b64decode.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua', 'httpserver-connection.lua'}
|
||||
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
|
||||
|
||||
compileAndRemoveIfNeeded = nil
|
||||
|
|
Loading…
Add table
Reference in a new issue