[WIP] lua file upload, progress bar, lua textboard September 28, 2015 02:45PM |
Registered: 9 years ago Posts: 4 |
-- cgi util module local prevbuf = "" local blocksize = 4096 local _M = {} _M.statusmsg = { [200] = "OK", [206] = "Partial Content", [301] = "Moved Permanently", [302] = "Found", [304] = "Not Modified", [400] = "Bad Request", [403] = "Forbidden", [404] = "Not Found", [405] = "Method Not Allowed", [408] = "Request Time-out", [411] = "Length Required", [412] = "Precondition Failed", [416] = "Requested range not satisfiable", [500] = "Internal Server Error", [503] = "Server Unavailable", } -- call this function passing an empy table. use that table for successive calls. function _M.new(req) req.content_length = os.getenv("CONTENT_LENGTH") req.request_method = os.getenv("REQUEST_METHOD") if req.request_method == "POST" then req.content_type, req.boundary = string.match(os.getenv("CONTENT_TYPE"),"^(multipart/form%-data); boundary=\"?(.+)\"?$") req.boundary = "--" .. req.boundary end -- this is useful only if you have /tmp on tmpfs like in openwrt. otherwise can be set to "" req.tempdir = "/mnt/piratebox/tmp" req.post = {} end -- this function is needed to clean temp file since and hide implementation details function _M.cleanup(req) for k, v in pairs(req.post) do for j, v in pairs(req.post[k]) do if req.post[k][j].tempname then os.remove(req.post[k][j].tempname) -- if file unused os.remove("/tmp/" .. string.match(req.post[k][j].tempname,"^" .. req.tempdir .. "(.+)")) end end end end -- del: delimiter -- return chunk (string), found (boolean) local function chunkread(del) local buf, found = 0 local del = del or "\r\n" buf = io.read(math.max(0,blocksize + #del - #prevbuf)) if prevbuf ~= "" then buf = prevbuf .. ( buf or "" ); prevbuf = "" end if not buf then return end s, e = string.find(buf,del,1,true) if s and e then found = 1 prevbuf = string.sub(buf,e+1) buf = string.sub(buf,1,s-1) else prevbuf = string.sub(buf,math.min(blocksize,#buf)+1) buf = string.sub(buf,1,math.min(blocksize,#buf)) end return buf, found end function _M.parse_request_body (req) local chunk, found, type, tempname, tempfile local param = {} -- read first boundary line chunk, found = chunkread(req.boundary) chunk, found = chunkread("\r\n") while chunk == "" do -- read part headers and get parameters value repeat chunk, found = chunkread("\r\n") if not found then return 400, "Malformed POST. Missing Part Header or Part Header too long." end string.gsub(chunk, ';%s*([^%s=]+)="(.-[^\\])"', function(k, v) param[k] = v end) param.type = param.type or string.match(chunk, "^Content%-Type: (.+)$") until chunk == "" -- prepare file data read if not param.name then return 400, "Malformed POST. Check Header parameters." end param.size=0 param.tempname = req.tempdir .. string.match(os.tmpname(), "^/tmp(.+)") tempfile = io.open(param.tempname, "w") -- read part body content until boundary repeat chunk, found = chunkread("\r\n" .. req.boundary) if not chunk then return 400, "Malformed POST. Incomplete Part received." end tempfile:write(chunk) param.size = param.size + #chunk until found tempfile:close() req.post[param.name] = req.post[param.name] or {} table.insert(req.post[param.name], 1, param) param = {} -- read after boundary. if CRLF ("") repeat. if "--" end POST processing chunk, found = chunkread("\r\n") end if found and chunk == "--" then return 0, "OK" end return 400, "Malformed POST. Boundary not properly ended with CRLF or --." end return _M
<!DOCTYPE html> <html> <head> <title>Index Page</title> </head> <body> <h1>This is a heading</h1> <p>test2.lua</p> <form action="/cgi-bin/test2.lua" enctype="multipart/form-data" method="post"> <input type="file" name="file1234" multiple="yes"><br> <input type="submit" value="Submit"> </form> <p>test3.lua</p> <form action="/cgi-bin/test3.lua" enctype="multipart/form-data" method="post"> <input type="file" name="file1234" multiple="yes"><br> <input type="submit" value="Submit"> </form> <a href='/share/'>share folder</a> </body> </html>
#!/usr/bin/pcall.lua local cgi = require "cgi" local req = {} local function printf (s,...) return io.write(s:format(...)) end cgi.new(req) if req.boundary then ret, msg = cgi.parse_request_body(req) end io.write("Content-Type: text/html\r\n\r\n") io.write("<!DOCTYPE html>") io.write("<html><body><p>hello world! </p><pre>") printf("<p>post processing: %d %s </p>" ,ret, msg) for k, v in pairs(req.post) do io.write("<p>input table " .. k .. "</p>") for j, v in pairs(req.post[k]) do io.write("<p> file index " .. tostring(j) .. "</p>") io.write("<p> type " .. req.post[k][j].type .. "</p>") io.write("<p> tempname " .. req.post[k][j].tempname .. "</p>") io.write("<p> filename " .. req.post[k][j].filename .. "</p>") io.write("<p> size " .. req.post[k][j].size .. "</p>") os.rename(req.post[k][j].tempname, "/mnt/piratebox/www/share/" .. req.post[k][j].filename) end end cgi.cleanup(req) io.write("<p>bye bye!</p>") io.write("</pre></body></html>")
#!/usr/bin/lua ok, res = pcall(dofile,(...)) if not ok then io.write("Status: 500 Internal Server Error\r\nContent-Type: text/plain\r\n\r\n" .. res .. "\r\n") end
#!/usr/bin/pcall.lua local cgi = require "cgi" local req = {} local function printf (s,...) return io.write(s:format(...)) end cgi.new(req) if req.request_method == "GET" then io.write("Content-Type: text/plain\r\nContent-Length: 0\r\n\r\n") end if req.request_method == "POST" then ret, msg = cgi.parse_request_body(req) report = "{\"files\": [" for j, n in pairs(req.post) do for k, v in pairs(req.post[j]) do if #report > 11 then report = report .. ", " end if req.post[j][k].filename then report = report .. "{ \"name\": \"" .. req.post[j][k].filename .. "\", \"size\": " .. tostring(req.post[j][k].size) .. ", \"url\": \"http:\\/\\/192.168.1.5\\/share\\/" .. req.post[j][k].filename .. "\" }" os.rename(req.post[j][k].tempname, "/mnt/piratebox/www/share/" .. req.post[j][k].filename) end end end report = report .. " ]}\r\n" if req.post.redirect then tempfile=io.open(req.post.redirect[1].tempname, "r") url=tempfile:read() tempfile:close() io.write("Content-Type: text/html\r\n\r\n") io.write([[<!DOCTYPE html><html><head><meta http-equiv="refresh" content="2; url=/index2.html"></head><p>File Uploaded</p><body></body></html>]]) else io.write("Content-Type: text/plain\r\nContent-Length: " .. tostring(#report) .. "\r\n\r\n") io.write(report) end cgi.cleanup(req) end
Re: [WIP] lua file upload, progress bar, lua textboard October 22, 2015 09:56AM |
Registered: 9 years ago Posts: 4 |
Re: [WIP] lua file upload, progress bar, lua textboard October 22, 2015 07:42PM |
Admin Registered: 13 years ago Posts: 4,680 |
Re: [WIP] lua file upload, progress bar, lua textboard October 23, 2015 08:43AM |
Registered: 9 years ago Posts: 4 |
Re: [WIP] lua file upload, progress bar, lua textboard October 28, 2015 04:40PM |
Registered: 9 years ago Posts: 4 |
Re: [WIP] lua file upload, progress bar, lua textboard September 07, 2017 10:38AM |