Skip to content

Commit e924fe5

Browse files
committed
Implemented httpserver gui
Added httpgui and related scripts
1 parent 7ca8414 commit e924fe5

10 files changed

+161
-14
lines changed
File renamed without changes.

http/httpgui-form-gen.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
return function (utils, connection, httpservConfig, valuetable, badvalues)
2+
local getRadioChecked = utils.getRadioChecked
3+
local getTextFilledValue = utils.getTextFilledValue
4+
local getRadioValue = utils.getRadioValue
5+
local getTextColor = utils.getTextColor
6+
local getInputTypeTextString = utils.getInputTypeTextString
7+
local getInputTypeRadioString = utils.getInputTypeRadioString
8+
9+
10+
local enabled = getRadioValue("httpservConfig.auth.enabled", valuetable) or httpservConfig.auth.enabled
11+
local realm = "httpservConfig.auth.realm"
12+
local user = "httpservConfig.auth.user"
13+
local password = "httpservConfig.auth.password"
14+
15+
connection:send('Authorization:<br>\n')
16+
connection:send(getInputTypeRadioString("httpservConfig.auth.enabled", "true", getRadioChecked(enabled, true), "Enabled"))
17+
connection:send(getInputTypeRadioString("httpservConfig.auth.enabled", "false", getRadioChecked(enabled, false), "Disabled"))
18+
connection:send('<br><table>\n')
19+
connection:send(getInputTypeTextString('Realm (greeting):', httpservConfig.auth.realm, realm, getTextFilledValue(realm, valuetable), getTextColor(realm, badvalues)))
20+
connection:send(getInputTypeTextString('User:' , httpservConfig.auth.user, user, getTextFilledValue(user, valuetable), getTextColor(user, badvalues)))
21+
connection:send(getInputTypeTextString('Password:' , httpservConfig.auth.password, password, getTextFilledValue(password, valuetable), getTextColor(password, badvalues)))
22+
connection:send('</table><br>\n')
23+
24+
end

http/httpgui-form.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
return function (method, connection, wifiConfig, valuetable, badvalues)
2+
local utils = dofile("http/form-utils.lc")
3+
4+
connection:send('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n')
5+
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Web Server Configuration</title></head>\n')
6+
connection:send('<body>')
7+
connection:send('<h1>Web Server Configuration</h1>\n')
8+
9+
if badvalues and next(badvalues) ~= nil then
10+
connection:send('<h2 style="color:#FF0000">Invalid values submitted</h2>\r\n')
11+
end
12+
connection:send('<form method="POST">\r\n')
13+
14+
collectgarbage()
15+
dofile("http/httpgui-form-gen.lc")(utils, connection, wifiConfig, valuetable, badvalues)
16+
collectgarbage()
17+
18+
connection:send('<input type="submit" value="Submit" title="Apply values">\r\n<input type=reset title="Reset all fields">\r\n<button type="cancel" title="Cancel and go back to main webpage" onclick="window.location=\'/\';return false;">Cancel</button>\r\n</form>\r\n</body></html>')
19+
end

http/httpgui-requote.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
return function(rd)
2+
local quotetable = {
3+
["httpservConfig.auth.user"] = true,
4+
["httpservConfig.auth.password"] = true,
5+
["httpservConfig.auth.realm"] = true,
6+
}
7+
8+
for name,value in pairs(rd) do
9+
if quotetable[name] then
10+
rd[name] = "\""..value.."\""
11+
end
12+
end
13+
end

http/httpgui-validate.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
return function(rd)
2+
local function validateRange(arg, min, max)
3+
return arg >= min and arg <= max
4+
end
5+
6+
local function validateRealm(arg)
7+
return validateRange(#arg, 5, 64)
8+
end
9+
10+
local function validateUser(arg)
11+
return validateRange(#arg, 5, 32)
12+
end
13+
14+
local function validatePwd(arg)
15+
return validateRange(#arg, 8, 64)
16+
end
17+
18+
local function validateBoolean(arg)
19+
if arg == "true" or arg == "false" then
20+
return true
21+
end
22+
return false
23+
end
24+
25+
local validatetable = {
26+
["httpservConfig.auth.enabled"] = validateBoolean,
27+
["httpservConfig.auth.user"] = validateUser,
28+
["httpservConfig.auth.password"] = validatePwd,
29+
["httpservConfig.auth.realm"] = validateRealm,
30+
}
31+
32+
local badvalues = {}
33+
for name, value in pairs(rd) do
34+
if validatetable[name] then
35+
if not validatetable[name](value) then
36+
badvalues[name] = value
37+
end
38+
end
39+
end
40+
41+
return badvalues
42+
end

http/httpgui.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
return function (connection, req, args)
2+
local httpservConfig = dofile("httpserver-conf.lc")
3+
collectgarbage()
4+
assert(httpservConfig ~= nil, "httpservConfig is nil")
5+
6+
7+
if req.method == "GET" then
8+
dofile("http/httpgui-form.lc")(req.method, connection, httpservConfig)
9+
collectgarbage()
10+
elseif req.method == "POST" then
11+
local rd = req.getRequestData()
12+
local badvalues = dofile("http/httpgui-validate.lc")(rd)
13+
collectgarbage()
14+
15+
if next(badvalues) == nil then
16+
if next(rd) ~= nil then
17+
-- at this point all values should be ok, so...
18+
-- fix strings
19+
dofile("http/httpgui-requote.lc")(rd)
20+
collectgarbage()
21+
--merge values into the httpservConfig
22+
tmr.wdclr()
23+
for name, value in pairs(rd) do
24+
local f = loadstring("return function(httpservConfig) "..name.."="..value.." end")()
25+
f(httpservConfig)
26+
f = nil
27+
collectgarbage()
28+
end
29+
--write out the config, compile, apply config
30+
dofile("httpserver-confwrite.lc")(httpservConfig, "httpserver-conf.lua")
31+
dofile("compile.lc")("httpserver-conf.lua")
32+
collectgarbage()
33+
end
34+
35+
--serve the form again
36+
dofile("http/httpgui-form.lc")(req.method, connection, httpservConfig)
37+
collectgarbage()
38+
else
39+
dofile("http/httpgui-form.lc")(req.method, connection, httpservConfig, rd, badvalues)
40+
end
41+
else
42+
connection:send("NOT IMPLEMENTED")
43+
end
44+
45+
collectgarbage()
46+
end

http/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ <h1>NodeMCU Platform</h1>
1818
<h3>Index</h3>
1919
<ul>
2020
<li><a href="wifigui.lc">Wifi Configuration UI</a>: Set wifi parameters for Access Point and Station modes</li>
21+
<li><a href="httpgui.lc">Web Server Configuration UI</a>: Set access control for webserver</li>
2122
<li><a href="node_info.lc">NodeMCU info</a>: Shows some basic NodeMCU info</li>
2223
<li><a href="file_list.lc">Server file list</a>: Displays a list of all http server files</li>
2324
<li><a href="garage_door_opener.html">Garage door opener</a>: Control GPIO lines via the server</li>

http/wifigui-form.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
return function (method, connection, wifiConfig, valuetable, badvalues)
2-
local util = dofile("http/wifigui-form-util.lc")
2+
local utils = dofile("http/form-utils.lc")
33

44
connection:send('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n')
55
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Wifi Configuration</title></head>\n')
@@ -12,15 +12,15 @@ return function (method, connection, wifiConfig, valuetable, badvalues)
1212
connection:send('<form method="POST">\r\n')
1313

1414
collectgarbage()
15-
dofile("http/wifigui-form-gen.lc")(util, connection, wifiConfig, valuetable, badvalues)
15+
dofile("http/wifigui-form-gen.lc")(utils, connection, wifiConfig, valuetable, badvalues)
1616
collectgarbage()
17-
dofile("http/wifigui-form-ap.lc") (util, connection, wifiConfig, valuetable, badvalues)
17+
dofile("http/wifigui-form-ap.lc") (utils, connection, wifiConfig, valuetable, badvalues)
1818
collectgarbage()
19-
dofile("http/wifigui-form-apnet.lc") (util, connection, wifiConfig, valuetable, badvalues)
19+
dofile("http/wifigui-form-apnet.lc") (utils, connection, wifiConfig, valuetable, badvalues)
2020
collectgarbage()
21-
dofile("http/wifigui-form-sta.lc")(util, connection, wifiConfig, valuetable, badvalues)
21+
dofile("http/wifigui-form-sta.lc")(utils, connection, wifiConfig, valuetable, badvalues)
2222
collectgarbage()
23-
dofile("http/wifigui-form-stanet.lc")(util, connection, wifiConfig, valuetable, badvalues)
23+
dofile("http/wifigui-form-stanet.lc")(utils, connection, wifiConfig, valuetable, badvalues)
2424
collectgarbage()
2525

2626
connection:send('<input type="submit" value="Submit" title="Apply values">\r\n<input type=reset title="Reset all fields">\r\n<button type="cancel" title="Cancel and go back to main webpage" onclick="window.location=\'/\';return false;">Cancel</button>\r\n</form>\r\n</body></html>')

httpserver-confmakedefault.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
local conf = {}
1+
local httpservConfig = {}
22

33
-- Basic Authentication Conf
4-
conf.auth = {}
5-
conf.auth.enabled = true
6-
conf.auth.realm = "ESP-"..node.chipid().." httpserver" -- displayed in the login dialog users get
7-
conf.auth.user = "develo"
8-
conf.auth.password = "theballismine" -- PLEASE change this
4+
httpservConfig.auth = {}
5+
httpservConfig.auth.enabled = true
6+
httpservConfig.auth.realm = "ESP-"..node.chipid().." httpserver" -- displayed in the login dialog users get
7+
httpservConfig.auth.user = "develo"
8+
httpservConfig.auth.password = "theballismine" -- PLEASE change this
99

10-
return conf
10+
return httpservConfig

tools.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ function tools.mv2http()
9494
"underconstruction.gif",
9595
"zipped.html.gz",
9696
"apple.*png",
97-
"wifigui.*"
97+
"wifigui.*",
98+
"httpgui.*",
99+
"form.*"
98100
}
99101

100102
for i,f in ipairs(filelist) do

0 commit comments

Comments
 (0)