I want to upload files to my device using HTTP POST. I have successfully written some script in Lua that parses the environment variables, and prints the POST content. However I'm looking for some library that parses the content, so I can store the uploaded file directly to the filesysyem.
The preferred scripting language is Lua, but if there's other solution, I'm open.
Technically, if you read the body of a POST request, and use simple lua io, that should work on it's own.
So, something like this for writing files: https://www.lua.org/pil/21.2.html
Yes, but I don't want to reinvent the wheel. I guess there are lots of
implementation that already do this, like cgilua, I just can't integrate it
to my project.
So I tried to put together something like this, but it doesn't seem to work. I get 502 Bad Gateway error.
#!/usr/bin/lua
http = require "luci.http"
proto = require "luci.http.protocol"
local fileOpened = 0
local fileP
function filecb(t, data, isLast)
if fileOpened == 0 then
FileP = io.open("/tmp/blabla", "wb")
fileOpened = 1
end
FileP:write(data)
if isLast then
io.close(FileP)
end
end
src = http.source()
httpMsg = proto.parse_message_header(src)
proto.parse_message_body(src, httpMsg, filecb)
http.close()