Uhttpd CGI programming

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.

Thank you for your help,
Levente

Not sure if you need any other libs perse.

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

And then read from the stdin of the script.
Something like : https://www.lua.org/pil/21.1.html

I'm not completely sure about all this, but this is how it works with CGI shell scripts.
So, it should also work for lua.

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.

Levente

You could borrow code from LuCI (luci.http.protocol contains a parser for MIME and url encoded POST bodies) or use "haserl" for this task.

Can you give me some pointers where can I find this?

Thanks,
Levente

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()

I'm sure this is not that simple...

Lev

@leventelist - Were you able to close down on the HTTP post usage or did you change your approach?

We have a similar issue where the file upload is required via HTTP Post.

I changed the approach... But I made it work somhow, but it was a PITA.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.