4:48 6ˆl–ßi~” ji?u @†àq·”Å£�þDÜ’,Ùdx ×##ÀþÄ ƒ}ÇÙÃÁÀ%Ý---------------------------------------------------------------------------- -- Process POST data. -- This library depends on some functions that read POST data and other -- HTTP information. A beginning is: -- require"post" -- local params = {} -- post.parsedata { -- read = ap.get_client_block or io.input, -- discardinput = ap.discard_request_body, -- content_type = ap.get_header"content-type" or os.getenv"CONTENT_TYPE", -- content_length = ap.get_header"content-length" or os.getenv"CONTENT_LENGTH", -- maxinput = 1024 * 1024, -- maxfilesize = 512 * 1024, -- args = params, -- } -- -- @release $Id: post.lua,v 1.17 2008/04/03 21:55:28 mascarenhas Exp $ ---------------------------------------------------------------------------- local iterate = require"cgilua.readuntil".iterate local urlcode = require"cgilua.urlcode" local tmpfile = require"cgilua".tmpfile local assert, error, pairs, tonumber, tostring, type = assert, error, pairs, tonumber, tostring, type local getn, tinsert = table.getn, table.insert local format, gsub, strfind, strlower, strlen = string.format, string.gsub, string.find, string.lower, string.len local min = math.min --local iterate = cgilua.readuntil.iterate --local urlcode = cgilua.urlcode --local tmpfile = cgilua.tmpfile -- environment for processing multipart/form-data input local boundary = nil -- boundary string that separates each 'part' of input local maxfilesize = nil -- maximum size for file upload local maxinput = nil -- maximum size of total POST data local inputfile = nil -- temporary file for inputting form-data local bytesleft = nil -- number of bytes yet to be read local content_type = nil -- request's content-type -- local functions local discardinput = nil -- discard all remaining input local readuntil = nil -- read until delimiter local read = nil -- basic read function -- -- Extract the boundary string from CONTENT_TYPE metavariable -- local function getboundary () local _,_,boundary = strfind (content_type, "boundary%=(.-)$") return "--"..boundary end -- -- Create a table containing the headers of a multipart/form-data field -- local function breakheaders (hdrdata) local headers = {} gsub (hdrdata, '([^%c%s:]+):%s+([^\n]+)', function(type,val) type = strlower(type) headers[type] = val end) return headers end -- -- Read the headers of the next multipart/form-data field -- -- This function returns a table containing the headers values. Each header -- value is indexed by the corresponding header "type". -- If end of input is reached (no more fields to process) it returns nil. -- local function readfieldheaders () local EOH = "\r\n\r\n" -- local hdrdata = "" local out = function (str) hdrdata = hdrdata..str end if readuntil (EOH, out) then -- parse headers return breakheaders (hdrdata) else -- no header found return nil end end -- -- Extract a field name (and possible filename) from its disposition header -- local function getfieldnames (headers) local disposition_hdr = headers["content-disposition"] local attrs = {} if disposition_hdr then gsub(disposition_hdr, ';%s*([^%s=]+)="(.-)"', function(attr, val) attrs[attr] = val end) else error("Error processing multipart/form-data.".. "\nMissing content-disposition header") end return attrs.name, attrs.filename end -- -- Read the contents of a 'regular' field to a string -- local function readfieldcontents () local value = "" local boundaryline = "\r\n"..boundary local out = function (str) value = value..str end if readuntil (boundaryline, out) then return value else error("Error processing multipart/form-data.\nUnexpected end of input\n") end end -- -- Read the contents of a 'file' field to a temporary file (file upload) -- local function fileupload (filename) -- create a temporary file for uploading the file field local file, err = tmpfile() if file == nil then discardinput(bytesleft) error("Cannot