luci-base: raise maximum POST value size to 100KB
[project/luci.git] / modules / luci-base / luasrc / http.lua
index aa89136..f4ede4b 100644 (file)
@@ -14,7 +14,7 @@ local table, ipairs, pairs, type, tostring, tonumber, error =
 
 module "luci.http"
 
-HTTP_MAX_CONTENT      = 1024*8         -- 8 kB maximum content size
+HTTP_MAX_CONTENT      = 1024*100               -- 100 kB maximum content size
 
 context = util.threadlocal()
 
@@ -359,6 +359,7 @@ function mimedecode_message_body(src, msg, file_cb)
                        then
                                field.name = lhttp.header_attribute(buffer, "name")
                                field.file = lhttp.header_attribute(buffer, "filename")
+                               field[1] = field.file
                        end
 
                        if field.headers then
@@ -397,7 +398,15 @@ function mimedecode_message_body(src, msg, file_cb)
                                        field.fd:seek(0, "set")
                                end
                        else
-                               msg.params[field.name] = field.value or ""
+                               local val = msg.params[field.name]
+
+                               if type(val) == "table" then
+                                       val[#val+1] = field.value or ""
+                               elseif val ~= nil then
+                                       msg.params[field.name] = { val, field.value or "" }
+                               else
+                                       msg.params[field.name] = field.value or ""
+                               end
                        end
 
                        field = nil
@@ -407,7 +416,7 @@ function mimedecode_message_body(src, msg, file_cb)
                end
 
                return true
-       end)
+       end, HTTP_MAX_CONTENT)
 
        return ltn12.pump.all(src, function (chunk)
                len = len + (chunk and #chunk or 0)
@@ -435,15 +444,23 @@ function urldecode_message_body(src, msg)
                if what == parser.TUPLE then
                        name, value = nil, nil
                elseif what == parser.NAME then
-                       name = lhttp.urldecode(buffer)
+                       name = lhttp.urldecode(buffer, lhttp.DECODE_PLUS)
                elseif what == parser.VALUE and name then
-                       msg.params[name] = lhttp.urldecode(buffer) or ""
+                       local val = msg.params[name]
+
+                       if type(val) == "table" then
+                               val[#val+1] = lhttp.urldecode(buffer, lhttp.DECODE_PLUS) or ""
+                       elseif val ~= nil then
+                               msg.params[name] = { val, lhttp.urldecode(buffer, lhttp.DECODE_PLUS) or "" }
+                       else
+                               msg.params[name] = lhttp.urldecode(buffer, lhttp.DECODE_PLUS) or ""
+                       end
                elseif what == parser.ERROR then
                        err = buffer
                end
 
                return true
-       end)
+       end, HTTP_MAX_CONTENT)
 
        return ltn12.pump.all(src, function (chunk)
                len = len + (chunk and #chunk or 0)
@@ -469,26 +486,22 @@ end
 -- handled then the whole message body will be stored unaltered as "content"
 -- property within the given message object.
 function parse_message_body(src, msg, filecb)
-       local ctype = lhttp.header_attribute(msg.env.CONTENT_TYPE, nil)
-
-       -- Is it multipart/mime ?
-       if msg.env.REQUEST_METHOD == "POST" and
-          ctype == "multipart/form-data"
-       then
-               return mimedecode_message_body(src, msg, filecb)
+       if msg.env.CONTENT_LENGTH or msg.env.REQUEST_METHOD == "POST" then
+               local ctype = lhttp.header_attribute(msg.env.CONTENT_TYPE, nil)
 
-       -- Is it application/x-www-form-urlencoded ?
-       elseif msg.env.REQUEST_METHOD == "POST" and
-              ctype == "application/x-www-form-urlencoded"
-       then
-               return urldecode_message_body(src, msg)
+               -- Is it multipart/mime ?
+               if ctype == "multipart/form-data" then
+                       return mimedecode_message_body(src, msg, filecb)
 
+               -- Is it application/x-www-form-urlencoded ?
+               elseif ctype == "application/x-www-form-urlencoded" then
+                       return urldecode_message_body(src, msg)
 
-       -- Unhandled encoding
-       -- If a file callback is given then feed it chunk by chunk, else
-       -- store whole buffer in message.content
-       else
+               end
 
+               -- Unhandled encoding
+               -- If a file callback is given then feed it chunk by chunk, else
+               -- store whole buffer in message.content
                local sink
 
                -- If we have a file callback then feed it
@@ -536,4 +549,6 @@ function parse_message_body(src, msg, filecb)
 
                return true
        end
+
+       return false
 end