- enc = enc .. ( #enc > 0 and "&" or "" ) ..
- util.urlencode(k) .. "=" .. util.urlencode(v2)
- end
- else
- enc = enc .. ( #enc > 0 and "&" or "" ) ..
- util.urlencode(k) .. "=" .. util.urlencode(v)
- end
- end
-
- return enc
-end
-
--- (Internal function)
--- Initialize given parameter and coerce string into table when the parameter
--- already exists.
-local function __initval( tbl, key )
- if tbl[key] == nil then
- tbl[key] = ""
- elseif type(tbl[key]) == "string" then
- tbl[key] = { tbl[key], "" }
- else
- table.insert( tbl[key], "" )
- end
-end
-
--- (Internal function)
--- Initialize given file parameter.
-local function __initfileval( tbl, key, filename, fd )
- if tbl[key] == nil then
- tbl[key] = { file=filename, fd=fd, name=key, "" }
- else
- table.insert( tbl[key], "" )
- end
-end
-
--- (Internal function)
--- Append given data to given parameter, either by extending the string value
--- or by appending it to the last string in the parameter's value table.
-local function __appendval( tbl, key, chunk )
- if type(tbl[key]) == "table" then
- tbl[key][#tbl[key]] = tbl[key][#tbl[key]] .. chunk
- else
- tbl[key] = tbl[key] .. chunk
- end
-end
-
--- (Internal function)
--- Finish the value of given parameter, either by transforming the string value
--- or - in the case of multi value parameters - the last element in the
--- associated values table.
-local function __finishval( tbl, key, handler )
- if handler then
- if type(tbl[key]) == "table" then
- tbl[key][#tbl[key]] = handler( tbl[key][#tbl[key]] )
- else
- tbl[key] = handler( tbl[key] )
- end
- end
-end
-
-
--- Table of our process states
-local process_states = { }
-
--- Extract "magic", the first line of a http message.
--- Extracts the message type ("get", "post" or "response"), the requested uri
--- or the status code if the line descripes a http response.
-process_states['magic'] = function( msg, chunk, err )
-
- if chunk ~= nil then
- -- ignore empty lines before request
- if #chunk == 0 then
- return true, nil
- end
-
- -- Is it a request?
- local method, uri, http_ver = chunk:match("^([A-Z]+) ([^ ]+) HTTP/([01]%.[019])$")
-
- -- Yup, it is
- if method then
-
- msg.type = "request"
- msg.request_method = method:lower()
- msg.request_uri = uri
- msg.http_version = tonumber( http_ver )
- msg.headers = { }