+++ /dev/null
--- Some basic checks\r
-if mg.request_info.request_method ~= "POST" or mg.request_info.content_type:lower():sub(1,19) ~= 'multipart/form-data' then\r
- mg.write("HTTP/1.0 400 OK\r\n")\r
- mg.write("Connection: close\r\n")\r
- mg.write("Content-Type: text/plain; charset=utf-8\r\n")\r
- mg.write("Cache-Control: max-age=0, must-revalidate\r\n")\r
- mg.write("\r\n")\r
- mg.write("Bad request\r\n\r\n")\r
- return\r
-end\r
-\r
--- HTTP headers\r
-mg.write("HTTP/1.0 200 OK\r\n")\r
-mg.write("Connection: close\r\n")\r
-mg.write("Content-Type: text/plain; charset=utf-8\r\n")\r
-mg.write("Cache-Control: max-age=0, must-revalidate\r\n")\r
-mg.write("\r\n")\r
-\r
--- Which form sent the data?\r
-mg.write("Read POST data from " .. mg.request_info.http_headers.Referer .. ":\r\n\r\n")\r
-\r
--- Count some data fields\r
-local fields = 0\r
-local datasize = 0\r
-\r
--- Read the entire body data (POST content) into "bdata" variable.\r
--- Use a string builder pattern for performance reasons\r
-stringtab = {}\r
-bdata = ""\r
-repeat\r
- local add_data = mg.read()\r
- if add_data then\r
- stringtab[#stringtab+1] = add_data\r
- end\r
-until (add_data == nil);\r
-bdata = table.concat(stringtab)\r
-stringtab = nil\r
-\r
--- Get the boundary string.\r
-bs = "--" .. ((mg.request_info.content_type):upper():match("BOUNDARY=(.*)"));\r
-\r
--- The POST data has to start with the boundary string.\r
--- Check this and remove the starting boundary.\r
-if bdata:sub(1, #bs) ~= bs then\r
- error "invalid format of POST data"\r
-end\r
-bdata = bdata:sub(#bs)\r
-\r
--- The boundary now starts with CR LF.\r
-bs = "\r\n" .. bs\r
-\r
--- Now loop through all the parts\r
-while #bdata>4 do\r
- -- Find the header of new part.\r
- part_header_end = bdata:find("\r\n\r\n", 1, true)\r
-\r
- -- Parse the header.\r
- local form_field_name, file_name\r
- h = bdata:sub(1, part_header_end+2)\r
- for key,val in h:gmatch("([^%:\r\n]*)%s*%:%s*([^\r\n]*)\r\n") do\r
- if key:upper() == "CONTENT-DISPOSITION" then\r
- form_field_name = val:match('name=%"([^%"]*)%"')\r
- file_name = val:match('filename=%"([^%"]*)%"')\r
- end\r
- end\r
-\r
- -- Remove the header from "bdata".\r
- bdata = bdata:sub(part_header_end+4)\r
-\r
- -- Find the end of the body by locating the boundary string.\r
- local part_body_end = bdata:find(bs, 1, true)\r
-\r
- -- Isolate the content, and drop it from "bdata".\r
- local form_field_value = bdata:sub(1,part_body_end-1)\r
- bdata = bdata:sub(part_body_end+#bs)\r
-\r
- -- Now the data (file content or field value) is isolated: We know form_field_name and form_field_value.\r
- -- Here the script should do something useful with the data. This example just sends it back to the client.\r
- if form_field_name then\r
- mg.write("Field name: " .. form_field_name .. "\r\n")\r
- end\r
-\r
- local len = #form_field_value\r
- mg.write("Field data length: " .. len .. "\r\n")\r
-\r
- if file_name then\r
- mg.write("File name: " .. file_name .. "\r\n")\r
- mg.write("File content:\r\n")\r
- local maxlen\r
- if len>320 then maxlen=320 else maxlen=len end\r
-\r
- for l=0,maxlen,16 do\r
- for m=1,16 do\r
- local b = form_field_value:byte(l+m)\r
- if (b) then\r
- mg.write(string.format("%02x ", b))\r
- else\r
- mg.write(" ")\r
- end\r
- end\r
- mg.write(" - " .. form_field_value:sub(l+1,l+16):gsub("[%c%z%s]", " ") .. "\r\n")\r
- end\r
- if maxlen<len then\r
- mg.write(string.format("... (+ %u bytes)\r\n", len-maxlen))\r
- end\r
-\r
- else\r
- -- not a file\r
- if len<50 then\r
- mg.write("Field value: " .. form_field_value .. "\r\n")\r
- else\r
- mg.write("Field value: " .. form_field_value:sub(1, 40) .. " .. (" .. len .. " bytes)\r\n")\r
- end\r
- end\r
-\r
-\r
- mg.write("\r\n")\r
- fields = fields + 1\r
- datasize = datasize + len\r
-\r
-end\r
-\r
-mg.write("Got " .. fields .. " input fields with " .. datasize .. " bytes total\r\n");\r