diff --git a/utils.lua b/utils.lua index 7359afa..b35e619 100644 --- a/utils.lua +++ b/utils.lua @@ -63,9 +63,24 @@ _m.tobase64 = function(data) return table.concat(out) end -_m.parse_form_entry = function(data) - - +--- @param entry string +--- @return string name the name of the form field. +--- @return string? content_type the content type of the attached file, or nil if entry is not a file. +--- @return string data the value of the entry. +_m.parse_form_entry = function(entry) + local name, content_type, data + local _, end_of_header = entry:find("\r\n\r\n") + for dataline in entry:gmatch("(.-)\r\n") do + if dataline:sub(1, 19) == "Content-Disposition" then + name = dataline:match("name=\"(.*)\"") + end + if dataline:sub(1, 12) == "Content-Type" then + content_type = dataline:match("Content-Type: (.*)\r\n") + end + if dataline == "" then break end + end + data = entry:sub(end_of_header + 1) + return name, content_type, data end return _m diff --git a/yqtemplate.lua b/yqtemplate.lua index b3d289e..6ca875c 100644 --- a/yqtemplate.lua +++ b/yqtemplate.lua @@ -10,18 +10,25 @@ return { run = function(fcgi) fcgi.print("Content-Type: text/html; charset=utf-8\r\n\r\n") local data = "" - --[[if fcgi.getenv("REQUEST_METHOD") == "POST" then + if fcgi.getenv("REQUEST_METHOD") == "POST" then local content_type = fcgi.getenv("CONTENT_TYPE") local boundary = content_type:match("; boundary=(.*)$") local post_data = fcgi.post() data = "Content type:" .. content_type .. "\n Boundary: " .. boundary .. "\n" local args = {} for w in string.gmatch(post_data, "(.-)" .. boundary) do - local dataheaders, _ = utils.parse_form_entry(w) - args[#args + 1] = dataheaders + local name, dtype, value = utils.parse_form_entry(w) + args[name] = { t = dtype, v = value } end - data = table.concat(args, "\n\n") - end ]] + for k, v in pairs(args) do + data = data .. k .. ": " + if v.t then + data = data .. "[FILE:" .. v.t .. "]\n" + else + data = data .. v.v .. "\n" + end + end + end fcgi.print( form { data = data,