From eb703a1165d99cb6671287cb72daa406f57dac33 Mon Sep 17 00:00:00 2001 From: Ollo Date: Sun, 9 Apr 2023 14:33:26 +0200 Subject: [PATCH] Handle special characters of HTTP POST --- webserver.lua | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/webserver.lua b/webserver.lua index 019abfb..360f52f 100644 --- a/webserver.lua +++ b/webserver.lua @@ -2,6 +2,16 @@ local configFile="config.lua" local httpSending=false local sentBytes=0 + +-- Source https://stackoverflow.com/questions/28916182/parse-parameters-out-of-url-in-lua#28921280 +function urldecode(s) + s = s:gsub('+', ' ') + :gsub('%%(%x%x)', function(h) + return string.char(tonumber(h, 16)) + end) + return s +end + function sendPage(conn, nameOfFile, replaceMap) collectgarbage() print("Sending " .. nameOfFile .. " " .. sentBytes .. "B already; " .. node.heap() .. "B in heap") @@ -179,7 +189,7 @@ function startWebServer() file.remove(configFile .. ".new") sec, _ = rtctime.get() file.open(configFile.. ".new", "w+") - file.write("-- Config\n" .. "station_cfg={}\nstation_cfg.ssid=\"" .. _POST.ssid .. "\"\nstation_cfg.pwd=\"" .. _POST.password .. "\"\nstation_cfg.save=false\nwifi.sta.config(station_cfg)\n") + file.write("-- Config\n" .. "station_cfg={}\nstation_cfg.ssid='" .. urldecode(_POST.ssid) .. "'\nstation_cfg.pwd='" .. urldecode(_POST.password) .. "'\nstation_cfg.save=false\nwifi.sta.config(station_cfg)\n") file.write("sntpserverhostname=\"" .. _POST.sntpserver .. "\"\n" .. "timezoneoffset=\"" .. _POST.timezoneoffset .. "\"\n".. "inv46=nil\n") if ( _POST.fcolor ~= nil) then