From 59e0218a72c8366f2eb23dcd393cbf14f1cfe4a2 Mon Sep 17 00:00:00 2001 From: Marcos Kirsch Date: Mon, 15 Feb 2016 23:19:29 -0600 Subject: [PATCH] Fix https://github.com/marcoskirsch/nodemcu-httpserver/issues/36 --- httpserver.lua | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/httpserver.lua b/httpserver.lua index 5c32039..02cc4ef 100644 --- a/httpserver.lua +++ b/httpserver.lua @@ -83,7 +83,7 @@ return function (port) file.close() if fileExists then - print("gzip variant exists, serving that one") + --print("gzip variant exists, serving that one") uri.file = uri.file .. ".gz" uri.isGzipped = true end @@ -113,6 +113,22 @@ return function (port) local auth local user = "Anonymous" + -- as suggest by anyn99 (https://github.com/marcoskirsch/nodemcu-httpserver/issues/36#issuecomment-167442461) + -- Some browsers send the POST data in multiple chunks. + -- Collect data packets until the size of HTTP body meets the Content-Length stated in header + if payload:find("Content%-Length:") or bBodyMissing then + if fullPayload then fullPayload = fullPayload .. payload else fullPayload = payload end + if (tonumber(string.match(fullPayload, "%d+", fullPayload:find("Content%-Length:")+16)) > #fullPayload:sub(fullPayload:find("\r\n\r\n", 1, true)+4, #fullPayload)) then + bBodyMissing = true + return + else + --print("HTTP packet assembled! size: "..#fullPayload) + payload = fullPayload + fullPayload, bBodyMissing = nil + end + end + collectgarbage() + -- parse payload and decide what to serve. local req = dofile("httpserver-request.lc")(payload) print("Requested URI: " .. req.request)