require("ltn12")
require("luci.util")
+require("luci.http.protocol.filter")
HTTP_MAX_CONTENT = 1024*4 -- 4 kB maximum content size
HTTP_URLENC_MAXKEYLEN = 1024 -- maximum allowd size of urlencoded parameter names
msg.type = "request"
msg.request_method = method:lower()
msg.request_uri = uri
- msg.http_version = http_ver
+ msg.http_version = tonumber( http_ver )
msg.headers = { }
-- We're done, next state is header parsing
msg.type = "response"
msg.status_code = code
msg.status_message = message
- msg.http_version = http_ver
+ msg.http_version = tonumber( http_ver )
msg.headers = { }
-- We're done, next state is header parsing
if chunk ~= nil then
-- Check for Content-Length
- if msg.headers['Content-Length'] then
- msg.content_length = tonumber(msg.headers['Content-Length'])
+ if msg.env.CONTENT_LENGTH then
+ msg.content_length = tonumber(msg.env.CONTENT_LENGTH)
if msg.content_length <= HTTP_MAX_CONTENT then
-- Initialize buffer
-- Process urldecoding stream, read and validate parameter key
process_states['urldecode-key'] = function( msg, chunk, filecb )
-
if chunk ~= nil then
-- Prevent oversized requests
else
msg._urldeccallback = function( chunk, eof )
msg.params[key] = msg.params[key] .. chunk
+
+ -- FIXME: Use a filter
+ if eof then
+ msg.params[key] = urldecode( msg.params[key] )
+ end
end
end
function mimedecode_message_body( source, msg, filecb )
-- Find mime boundary
- if msg and msg.headers['Content-Type'] then
+ if msg and msg.env.CONTENT_TYPE then
- local bound = msg.headers['Content-Type']:match("^multipart/form%-data; boundary=(.+)")
+ local bound = msg.env.CONTENT_TYPE:match("^multipart/form%-data; boundary=(.+)")
if bound then
msg.mime_boundary = bound
REQUEST_METHOD = msg.request_method:upper();
REQUEST_URI = msg.request_uri;
SCRIPT_NAME = msg.request_uri:gsub("?.+$","");
- SCRIPT_FILENAME = "" -- XXX implement me
+ SCRIPT_FILENAME = ""; -- XXX implement me
+ SERVER_PROTOCOL = "HTTP/" .. msg.http_version
}
-- Populate HTTP_* environment variables
-- Parse a http message body
function parse_message_body( source, msg, filecb )
+ -- Install an additional filter if we're operating on chunked transfer
+ -- coding and client is HTTP/1.1 capable
+ if msg.http_version == 1.1 and
+ msg.headers['Transfer-Encoding'] and
+ msg.headers['Transfer-Encoding']:find("chunked")
+ then
+ source = ltn12.source.chain(
+ source, luci.http.protocol.filter.decode_chunked
+ )
+ end
+
+
-- Is it multipart/mime ?
if msg.env.REQUEST_METHOD == "POST" and msg.env.CONTENT_TYPE and
msg.env.CONTENT_TYPE:match("^multipart/form%-data")
elseif msg.env.REQUEST_METHOD == "POST" and msg.env.CONTENT_TYPE and
msg.env.CONTENT_TYPE == "application/x-www-form-urlencoded"
then
-
return urldecode_message_body( source, msg, filecb )
+
-- Unhandled encoding
-- If a file callback is given then feed it line by line, else
-- store whole buffer in message.content
else
local sink
- local length = 0
-- If we have a file callback then feed it
if type(filecb) == "function" then
msg.content_length = 0
sink = function( chunk )
- if ( msg.content_length ) + #chunk <= HTTP_MAX_CONTENT then
+ if ( msg.content_length + #chunk ) <= HTTP_MAX_CONTENT then
msg.content = msg.content .. chunk
msg.content_length = msg.content_length + #chunk