Skip to content

Commit

Permalink
update http server to accept POST bodies split in multiple packets
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisisnate committed Feb 20, 2018
1 parent b5f51f0 commit 98c2e9c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion spec/device_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("device", function()
end)

it("has expected properties", function()
assert.equal(device.swVersion, "2.1.2")
assert.equal(device.swVersion, "2.1.3")
assert.equal(device.hwVersion, "2.0.5")
assert.equal(device.name, "Konnected")
end)
Expand Down
2 changes: 1 addition & 1 deletion src/device.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local me = {
id = "uuid:8f655392-a778-4fee-97b9-4825918" .. string.format("%x", node.chipid()),
name = "Konnected",
hwVersion = "2.0.5",
swVersion = "2.1.2",
swVersion = "2.1.3",
http_port = math.floor(node.chipid()/1000) + 8000,
urn = "urn:schemas-konnected-io:device:Security:1"
}
Expand Down
8 changes: 4 additions & 4 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"src/actuators.lua":"47a1a50c0c8cfb547554ded22a8134eaed76c0a3",
"src/application.lua":"758d600770046868d054b7d54bf2144a9fc255e1",
"src/device.lua":"2b962df6dfac2bf071a3f0b6946fb94fd7f2c441",
"src/device.lua":"8e931ae5d81fb4344587d70ed64c33aa3c4a8585",
"src/enduser_setup.html.gz":"fbf730b33c309ceab15e88f162f43c232593f7da",
"src/http_favicon.ico.gz":"483f371c590b0407c380d019bcc6938cf1d62f0a",
"src/http_index.html.gz":"fd636bc1a6080c94949c77953598964493aec2b0",
"src/httpd_req.lua":"80053674f20f087e58e1cfd490fc71e6cdd92acf",
"src/httpd_res.lua":"7226f19699168e171970cda73b953a49450b7aa8",
"src/init.lua":"6f145587fef8616a5446a1e3f953789a0470278f",
"src/led_flip.lua":"590e7be1afe686f4213d097d0e9dba9e7a3910a0",
"src/manifest.json":"107b5e9a23e5720e0fd61333f5cc773479d623b2",
"src/manifest.json":"cc3917ac4171aefff4a8f4ef02fd564d7629a4fc",
"src/sensors.lua":"d0d94fa73f713dcf2c517da9262f59f306b81d2b",
"src/server.lua":"8d23c80551120e6415689b0a08ef9f175bf727a6",
"src/server.lua":"dcd67fa56836f63472efe1bfd695e47fd70737b1",
"src/server_device.lua":"21bdbc50f7ba48f2261af622edd8dfae724b9d60",
"src/server_settings.lua":"6c77ab7a31be9c87ec654bca79091fbd9720748b",
"src/server_status.lua":"eb099e23239f57363683e8661266934eff3e0503",
Expand All @@ -23,5 +23,5 @@
"src/update_process.lua":"b6ac63039a0edf5e816bcd17b44704f9deb6000d",
"src/variables_build.lua":"fce4b2ce56bef0f50b09e0341cdf5eb7b9be0241",
"src/variables_set.lua":"2c2d9c13c8acb577d80f40e4b513c7cd23ffdd1f",
"updated_at":"2018-02-01:14:16:11"
"updated_at":"2018-02-20:02:20:58"
}
20 changes: 18 additions & 2 deletions src/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,24 @@ end)
print("Heap: ", node.heap(), "HTTP: ", "Starting server at http://" .. wifi.sta.getip() .. ":" .. device.http_port)
local http = net.createServer(net.TCP, 10)
http:listen(device.http_port, function(conn)
conn:on("receive", function( sck, data )
local request = require("httpd_req").new(data)
conn:on("receive", function( sck, payload )

-- Some clients send POST data in multiple chunks.
-- Collect data packets until the size of HTTP body meets the Content-Length stated in header
-- this snippet borrowed from https://github.com/marcoskirsch/nodemcu-httpserver/blob/master/httpserver.lua
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
payload = fullPayload
fullPayload, bBodyMissing = nil
end
end
collectgarbage()

local request = require("httpd_req").new(payload)
local response = require("httpd_res").new(sck)

if request.path == "/" then
Expand Down

0 comments on commit 98c2e9c

Please sign in to comment.