diff --git a/README.md b/README.md index e0c03f5..55fcfb1 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,14 @@ A (very) simple web server written in Lua for the ESP8266 running the NodeMCU fi ## Features -* GET +* GET, POST, PUT and minor changes to support other methods * Multiple MIME types * Error pages (404 and others) * Server-side execution of Lua scripts -* Query string argument parsing +* Query string argument parsing with decoding of arguments * Serving .gz compressed files * HTTP Basic Authentication +* Decoding of request bodies in both application/x-www-form-urlencoded and application/json (if cjson is available) ## How to use diff --git a/http/args.lua b/http/args.lua index 734a535..24ea28a 100644 --- a/http/args.lua +++ b/http/args.lua @@ -1,25 +1,26 @@ -return function (connection, args) - dofile("httpserver-header.lc")(connection, 200, 'html') - - connection:send([===[ - Arguments

Arguments

-
- First name:

- Last name:

- MaleFemale
- -
- ]===]) - coroutine.yield() - - if args["submit"] ~= nil then - connection:send("

Received the following values:

\n") -end +return function (connection, args) + dofile("httpserver-header.lc")(connection, 200, 'html') + connection:send([===[ + Arguments

Arguments

+
+ First name:

+ Last name:

+ MaleFemale
+ +
+ ]===]) + coroutine.yield() + + if args["submit"] ~= nil then + connection:send("

Received the following values:

\n") + +end + diff --git a/http/file_list.lua b/http/file_list.lua index 3628dd0..81d657a 100644 --- a/http/file_list.lua +++ b/http/file_list.lua @@ -1,27 +1,28 @@ -return function (connection, args) - dofile("httpserver-header.lc")(connection, 200, 'html') - - connection:send([===[ - Server File Listing - -

Server File Listing

- ]===]) - coroutine.yield() - - local remaining, used, total=file.fsinfo() - connection:send("Total size: " .. total .. " bytes
\n" .. - "In Use: " .. used .. " bytes
\n" .. - "Free: " .. remaining .. " bytes
\n" .. - "

\nFiles:
\n

\n

\n") -end +return function (connection, args) + dofile("httpserver-header.lc")(connection, 200, 'html') + + connection:send([===[ + Server File Listing + +

Server File Listing

+ ]===]) + coroutine.yield() + + local remaining, used, total=file.fsinfo() + connection:send("Total size: " .. total .. " bytes
\n" .. + "In Use: " .. used .. " bytes
\n" .. + "Free: " .. remaining .. " bytes
\n" .. + "

\nFiles:
\n

\n

\n") +end + diff --git a/http/garage_door_opener.lua b/http/garage_door_opener.lua index 610854a..5827a1d 100644 --- a/http/garage_door_opener.lua +++ b/http/garage_door_opener.lua @@ -22,7 +22,7 @@ local function pushTheButton(connection, pin) end -return function (connection, args) +return function (connection, req, args) print('Garage door button was pressed!', args.door) if args.door == "1" then pushTheButton(connection, 1) -- GPIO1 elseif args.door == "2" then pushTheButton(connection, 2) -- GPIO2 diff --git a/http/index.html b/http/index.html index 2620f91..a524f0c 100644 --- a/http/index.html +++ b/http/index.html @@ -22,7 +22,7 @@
  • Index: This page (static)
  • Zipped: A compressed file (static)
  • Arguments: Parses arguments passed in the URL and prints them. (Lua)
  • -
  • Post: A form that uses POST method, should error. (static)
  • +
  • Post: A form that uses POST method. Displays different content based on HTTP method. (Lua)
  • Garage door opener: Control GPIO lines via the server. (Lua)
  • NodeMCU info: Shows some basic NodeMCU(Lua)
  • List all server files: Displays a list of all the server files. (Lua)
  • diff --git a/http/post.html b/http/post.html deleted file mode 100644 index 508c755..0000000 --- a/http/post.html +++ /dev/null @@ -1,7 +0,0 @@ - -Post -

    Post

    -This form uses POST method which is not supported by nodemcu-httpserver.
    -You should get an error when you press submit. -
    - diff --git a/http/post.lua b/http/post.lua new file mode 100644 index 0000000..d1ac03d --- /dev/null +++ b/http/post.lua @@ -0,0 +1,33 @@ +return function (connection, req, args) + connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n") + connection:send('Arguments') + connection:send('') + connection:send('

    Arguments

    ') + + local form = [===[ +
    + First name:

    + Last name:

    + MaleFemale
    + +
    + ]===] + + if req.method == "GET" then + connection:send(form) + elseif req.method == "POST" then + local rd = req.getRequestData() + -- connection:send(cjson.encode(rd)) + connection:send('

    Received the following values:

    ') + connection:send("\n") + else + connection:send("NOT IMPLEMENTED") + end + + connection:send('') +end diff --git a/httpserver-error.lua b/httpserver-error.lua index b265f26..77f2516 100644 --- a/httpserver-error.lua +++ b/httpserver-error.lua @@ -2,7 +2,7 @@ -- Part of nodemcu-httpserver, handles sending error pages to client. -- Author: Marcos Kirsch -return function (connection, args) +return function (connection, req, args) local function getHeader(connection, code, errorString, extraHeaders, mimeType) local header = "HTTP/1.0 " .. code .. " " .. errorString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n" diff --git a/httpserver-header.lua b/httpserver-header.lua index 7486760..6d44bfd 100644 --- a/httpserver-header.lua +++ b/httpserver-header.lua @@ -2,7 +2,7 @@ -- Part of nodemcu-httpserver, knows how to send an HTTP header. -- Author: Marcos Kirsch -return function (connection, code, extension) +return function (connection, code, extension, gzip) local function getHTTPStatusString(code) local codez = {[200]="OK", [400]="Bad Request", [404]="Not Found",} @@ -15,11 +15,6 @@ return function (connection, code, extension) local gzip = false -- 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", xml = "text/xml"} - -- add comressed flag if file ends with gz - if ext:find("%.gz$") then - ext = ext:sub(1, -4) - gzip = true - end if mt[ext] then contentType = mt[ext] else contentType = "text/plain" end return {contentType = contentType, gzip = gzip} end diff --git a/httpserver-request.lua b/httpserver-request.lua index 70d6475..9f77620 100644 --- a/httpserver-request.lua +++ b/httpserver-request.lua @@ -12,17 +12,67 @@ local function uriToFilename(uri) return "http/" .. string.sub(uri, 2, -1) end +local function hex_to_char(x) + return string.char(tonumber(x, 16)) +end + +local function uri_decode(input) + return input:gsub("%+", " "):gsub("%%(%x%x)", hex_to_char) +end + local function parseArgs(args) local r = {}; i=1 if args == nil or args == "" then return r end for arg in string.gmatch(args, "([^&]+)") do local name, value = string.match(arg, "(.*)=(.*)") - if name ~= nil then r[name] = value end + if name ~= nil then r[name] = uri_decode(value) end i = i + 1 end return r end +local function parseFormData(body) + local data = {} + print("Parsing Form Data") + for kv in body.gmatch(body, "%s*&?([^=]+=[^&]+)") do + local key, value = string.match(kv, "(.*)=(.*)") + + print("Parsed: " .. key .. " => " .. value) + data[key] = uri_decode(value) + end + + return data +end + +local function getRequestData(payload) + local requestData + return function () + print("Getting Request Data") + if requestData then + return requestData + else + local mimeType = string.match(payload, "Content%-Type: ([%w/-]+)") + local body_start = payload:find("\r\n\r\n", 1, true) + local body = payload:sub(body_start, #payload) + payload = nil + collectgarbage() + + -- print("mimeType = [" .. mimeType .. "]") + + if mimeType == "application/json" then + print("JSON: " .. body) + requestData = cjson.decode(body) + elseif mimeType == "application/x-www-form-urlencoded" then + requestData = parseFormData(body) + else + requestData = {} + end + + return requestData + end + end +end + local function parseUri(uri) local r = {} local filename @@ -44,7 +94,12 @@ local function parseUri(uri) filename,ext = filename:match("(.+)%.(.+)") table.insert(fullExt,1,ext) end - r.ext = table.concat(fullExt,".") + if #fullExt > 1 and fullExt[#fullExt] == 'gz' then + r.ext = fullExt[#fullExt-1] + r.isGzipped = true + elseif #fullExt >= 1 then + r.ext = fullExt[#fullExt] + end r.isScript = r.ext == "lua" or r.ext == "lc" r.file = uriToFilename(r.file) return r @@ -61,5 +116,6 @@ return function (request) _, i, r.method, r.request = line:find("^([A-Z]+) (.-) HTTP/[1-9]+.[0-9]+$") r.methodIsValid = validateMethod(r.method) r.uri = parseUri(r.request) + r.getRequestData = getRequestData(request) return r end diff --git a/httpserver-static.lua b/httpserver-static.lua index f7f0e68..0cd7ee1 100644 --- a/httpserver-static.lua +++ b/httpserver-static.lua @@ -2,8 +2,8 @@ -- Part of nodemcu-httpserver, handles sending static files to client. -- Author: Marcos Kirsch -return function (connection, args) - dofile("httpserver-header.lc")(connection, 200, args.ext) +return function (connection, req, args) + dofile("httpserver-header.lc")(connection, 200, args.ext, args.gzipped) --print("Begin sending:", args.file) -- Send file in little chunks local continue = true diff --git a/httpserver.lua b/httpserver.lua index 8435178..96bb690 100644 --- a/httpserver.lua +++ b/httpserver.lua @@ -13,10 +13,58 @@ return function (port) -- We do it in a separate thread because we need to yield when sending lots -- of data in order to avoid overflowing the mcu's buffer. local connectionThread + + local allowStatic = {GET=true, HEAD=true, POST=false, PUT=false, DELETE=false, TRACE=false, OPTIONS=false, CONNECT=false, PATCH=false} - local function onGet(connection, uri) + local function startServing(fileServeFunction, connection, req, args) + local bufferedConnection = {} + connectionThread = coroutine.create(function(fileServeFunction, bconnection, req, args) + fileServeFunction(bconnection, req, args) + if not bconnection:flush() then + connection:close() + connectionThread = nil + end + end) + function bufferedConnection:flush() + if self.size > 0 then + connection:send(table.concat(self.data, "")) + self.data = {} + self.size = 0 + return true + end + return false + end + function bufferedConnection:send(payload) + local l = payload:len() + if l + self.size > 1000 then + if self:flush() then + coroutine.yield() + end + end + if l > 800 then + connection:send(payload) + coroutine.yield() + else + table.insert(self.data, payload) + self.size = self.size + l + end + end + bufferedConnection.size = 0 + bufferedConnection.data = {} + local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args) + if not status then + print(err) + end + end + + local function onRequest(connection, req) collectgarbage() + local method = req.method + local uri = req.uri local fileServeFunction = nil + + print("Method: " .. method); + if #(uri.file) > 32 then -- nodemcu-firmware cannot handle long filenames. uri.args = {code = 400, errorString = "Bad Request"} @@ -24,18 +72,35 @@ return function (port) else local fileExists = file.open(uri.file, "r") file.close() + + if not fileExists then + -- gzip check + fileExists = file.open(uri.file .. ".gz", "r") + file.close() + + if fileExists then + print("gzip variant exists, serving that one") + uri.file = uri.file .. ".gz" + uri.isGzipped = true + end + end + if not fileExists then uri.args = {code = 404, errorString = "Not Found"} fileServeFunction = dofile("httpserver-error.lc") elseif uri.isScript then fileServeFunction = dofile(uri.file) else - uri.args = {file = uri.file, ext = uri.ext} - fileServeFunction = dofile("httpserver-static.lc") + if allowStatic[method] then + uri.args = {file = uri.file, ext = uri.ext, gzipped = uri.isGzipped} + fileServeFunction = dofile("httpserver-static.lc") + else + uri.args = {code = 405, errorString = "Method not supported"} + fileServeFunction = dofile("httpserver-error.lc") + end end end - connectionThread = coroutine.create(fileServeFunction) - coroutine.resume(connectionThread, connection, uri.args) + startServing(fileServeFunction, connection, req, uri.args) end local function onReceive(connection, payload) @@ -51,8 +116,9 @@ return function (port) auth = dofile("httpserver-basicauth.lc") user = auth.authenticate(payload) -- authenticate returns nil on failed auth end - if user and req.methodIsValid and req.method == "GET" then - onGet(connection, req.uri) + + if user and req.methodIsValid and (req.method == "GET" or req.method == "POST" or req.method == "PUT") then + onRequest(connection, req) else local args = {} local fileServeFunction = dofile("httpserver-error.lc") @@ -63,18 +129,20 @@ return function (port) else args = {code = 400, errorString = "Bad Request"} end - connectionThread = coroutine.create(fileServeFunction) - coroutine.resume(connectionThread, connection, args) + startServing(fileServeFunction, connection, req, args) end end local function onSent(connection, payload) collectgarbage() if connectionThread then - local connectionThreadStatus = coroutine.status(connectionThread) + local connectionThreadStatus = coroutine.status(connectionThread) if connectionThreadStatus == "suspended" then -- Not finished sending file, resume. - coroutine.resume(connectionThread) + local status, err = coroutine.resume(connectionThread) + if not status then + print(err) + end elseif connectionThreadStatus == "dead" then -- We're done sending file. connection:close() @@ -85,6 +153,12 @@ return function (port) connection:on("receive", onReceive) connection:on("sent", onSent) + connection:on("disconnection",function(c) + if connectionThread then + connectionThread = nil + collectgarbage() + end + end) end ) diff --git a/init.lua b/init.lua index 70bfb68..c40ca8e 100644 --- a/init.lua +++ b/init.lua @@ -1,81 +1,95 @@ --- Begin WiFi configuration - -local wifiConfig = {} - --- Uncomment the WiFi mode you want. -wifiConfig.mode = wifi.STATION -- station: join a WiFi network --- wifiConfig.mode = wifi.AP -- access point: create a WiFi network --- wifiConfig.mode = wifi.STATIONAP -- both station and access point - -wifiConfig.accessPointConfig = {} -wifiConfig.accessPointConfig.ssid = "ESP-"..node.chipid() -- Name of the SSID you want to create -wifiConfig.accessPointConfig.pwd = "ESP-"..node.chipid() -- WiFi password - at least 8 characters - --- Configure fixed IP address -wifi.sta.setip({ip="10.0.7.111q", netmask="255.255.224.0", gateway="24.55.0.1"}) - -wifiConfig.stationPointConfig = {} -wifiConfig.stationPointConfig.ssid = "Internet" -- Name of the WiFi network you want to join -wifiConfig.stationPointConfig.pwd = "" -- Password for the WiFi network - --- Tell the chip to connect to the access point - -wifi.setmode(wifiConfig.mode) -print('set (mode='..wifi.getmode()..')') -print('MAC: ',wifi.sta.getmac()) -print('chip: ',node.chipid()) -print('heap: ',node.heap()) - -wifi.ap.config(wifiConfig.accessPointConfig) -wifi.sta.config(wifiConfig.stationPointConfig.ssid, wifiConfig.stationPointConfig.pwd) -wifiConfig = nil -collectgarbage() - --- End WiFi configuration - --- Compile server code and remove original .lua files. --- This only happens the first time afer the .lua files are uploaded. - -local compileAndRemoveIfNeeded = function(f) - if file.open(f) then - file.close() - print('Compiling:', f) - node.compile(f) - file.remove(f) - collectgarbage() - 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'} -for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end - -compileAndRemoveIfNeeded = nil -serverFiles = nil -collectgarbage() - --- Connect to the WiFi access point. --- Once the device is connected, you may start the HTTP server. - -local joinCounter = 0 -local joinMaxAttempts = 5 -tmr.alarm(0, 3000, 1, function() - local ip = wifi.sta.getip() - if ip == nil and joinCounter < joinMaxAttempts then - print('Connecting to WiFi Access Point ...') - joinCounter = joinCounter +1 - else - if joinCounter == joinMaxAttempts then - print('Failed to connect to WiFi Access Point.') - else - print('IP: ',ip) - -- Uncomment to automatically start the server in port 80 - --dofile("httpserver.lc")(80) - end - tmr.stop(0) - joinCounter = nil - joinMaxAttempts = nil - collectgarbage() - end - -end) - +-- Begin WiFi configuration + +local wifiConfig = {} + +-- wifi.STATION -- station: join a WiFi network +-- wifi.SOFTAP -- access point: create a WiFi network +-- wifi.wifi.STATIONAP -- both station and access point +wifiConfig.mode = wifi.STATIONAP -- both station and access point + +wifiConfig.accessPointConfig = {} +wifiConfig.accessPointConfig.ssid = "ESP-"..node.chipid() -- Name of the SSID you want to create +wifiConfig.accessPointConfig.pwd = "ESP-"..node.chipid() -- WiFi password - at least 8 characters + +wifiConfig.accessPointIpConfig = {} +wifiConfig.accessPointIpConfig.ip = "192.168.111.1" +wifiConfig.accessPointIpConfig.netmask = "255.255.255.0" +wifiConfig.accessPointIpConfig.gateway = "192.168.111.1" + +wifiConfig.stationPointConfig = {} +wifiConfig.stationPointConfig.ssid = "Internet" -- Name of the WiFi network you want to join +wifiConfig.stationPointConfig.pwd = "" -- Password for the WiFi network + +-- Tell the chip to connect to the access point + +wifi.setmode(wifiConfig.mode) +print('set (mode='..wifi.getmode()..')') + +if (wifiConfig.mode == wifi.SOFTAP) or (wifiConfig.mode == wifi.STATIONAP) then + print('AP MAC: ',wifi.ap.getmac()) + wifi.ap.config(wifiConfig.accessPointConfig) + wifi.ap.setip(wifiConfig.accessPointIpConfig) +end +if (wifiConfig.mode == wifi.STATION) or (wifiConfig.mode == wifi.STATIONAP) then + print('Client MAC: ',wifi.sta.getmac()) + wifi.sta.config(wifiConfig.stationPointConfig.ssid, wifiConfig.stationPointConfig.pwd, 1) +end + +print('chip: ',node.chipid()) +print('heap: ',node.heap()) + +wifiConfig = nil +collectgarbage() + +-- End WiFi configuration + +-- Compile server code and remove original .lua files. +-- This only happens the first time afer the .lua files are uploaded. + +local compileAndRemoveIfNeeded = function(f) + if file.open(f) then + file.close() + print('Compiling:', f) + node.compile(f) + file.remove(f) + collectgarbage() + 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'} +for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end + +compileAndRemoveIfNeeded = nil +serverFiles = nil +collectgarbage() + +-- Connect to the WiFi access point. +-- Once the device is connected, you may start the HTTP server. + +if (wifi.getmode() == wifi.STATION) or (wifi.getmode() == wifi.STATIONAP) then + local joinCounter = 0 + local joinMaxAttempts = 5 + tmr.alarm(0, 3000, 1, function() + local ip = wifi.sta.getip() + if ip == nil and joinCounter < joinMaxAttempts then + print('Connecting to WiFi Access Point ...') + joinCounter = joinCounter +1 + else + if joinCounter == joinMaxAttempts then + print('Failed to connect to WiFi Access Point.') + else + print('IP: ',ip) + end + tmr.stop(0) + joinCounter = nil + joinMaxAttempts = nil + collectgarbage() + end + end) +end + +-- Uncomment to automatically start the server in port 80 +if (not not wifi.sta.getip()) or (not not wifi.ap.getip()) then + --dofile("httpserver.lc")(80) +end +