From ef340bc82a39bbd826a330d06d25f55cd98c34e6 Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Mon, 31 Aug 2015 18:01:06 -0700 Subject: [PATCH] Implement support for more arbitrary HTTP methods. Allows GET PUT and POST. With minor changes needed to support others --- README.md | 7 ++--- http/args.lua | 2 +- http/file_list.lua | 2 +- http/garage_door_opener.lua | 2 +- http/index.html | 2 +- http/node_info.lua | 2 +- http/post.html | 7 ----- http/post.lua | 33 +++++++++++++++++++++++ httpserver-error.lua | 2 +- httpserver-request.lua | 53 ++++++++++++++++++++++++++++++++++++- httpserver-static.lua | 2 +- httpserver.lua | 51 ++++++++++++++++++++++------------- 12 files changed, 128 insertions(+), 37 deletions(-) delete mode 100644 http/post.html create mode 100644 http/post.lua diff --git a/README.md b/README.md index db019c9..aadf9c5 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 @@ -130,7 +131,7 @@ A (very) simple web server written in Lua for the ESP8266 running the NodeMCU fi ## Not supported -* Other methods: HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH +* ~~Other methods: HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH~~ * Encryption * Multiple users (HTTP Basic Authentication) * Only protect certain directories (HTTP Basic Authentication) diff --git a/http/args.lua b/http/args.lua index c9310c4..f7a2790 100644 --- a/http/args.lua +++ b/http/args.lua @@ -1,4 +1,4 @@ -return function (connection, args) +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('') diff --git a/http/file_list.lua b/http/file_list.lua index 384d088..9e92a23 100644 --- a/http/file_list.lua +++ b/http/file_list.lua @@ -1,4 +1,4 @@ -return function (connection, args) +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('Server File Listing') connection:send('') diff --git a/http/garage_door_opener.lua b/http/garage_door_opener.lua index e5dc874..ca67617 100644 --- a/http/garage_door_opener.lua +++ b/http/garage_door_opener.lua @@ -21,7 +21,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 ef81901..0db6ace 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/node_info.lua b/http/node_info.lua index fdd8868..86e55eb 100644 --- a/http/node_info.lua +++ b/http/node_info.lua @@ -7,7 +7,7 @@ local function sendAttr(connection, attr, val) connection:send("
  • ".. attr .. ": " .. val .. "
  • \n") end -return function (connection, args) +return function (connection, req, args) collectgarbage() sendHeader(connection) connection:send('A Lua script sample

    Node info