modules/admin-mini: Added Wifi configuration
[project/luci.git] / libs / http / luasrc / http / protocol.lua
index a9a6d8d..542c314 100644 (file)
@@ -19,6 +19,7 @@ local ltn12 = require("luci.ltn12")
 
 HTTP_MAX_CONTENT      = 1024*4         -- 4 kB maximum content size
 HTTP_URLENC_MAXKEYLEN = 1024           -- maximum allowd size of urlencoded parameter names
+TSRC_BLOCKSIZE        = 2048           -- target block size for throttling sources
 
 
 -- Decode an urlencoded string.
@@ -113,32 +114,26 @@ end
 
 -- Parameter helper
 local function __initval( tbl, key )
-       local multival = ( key:sub( #key - 1, #key ) == "[]" )
-
-       if multival then
-               if type(tbl[key]) == "table" then
-                       table.insert( tbl[key], "" )
-               else
-                       tbl[key] = { "" }
-               end
-       else
+       if tbl[key] == nil then
                tbl[key] = ""
+       elseif type(tbl[key]) == "string" then
+               tbl[key] = { tbl[key], "" }
+       else
+               table.insert( tbl[key], "" )
        end
-
-       return multival
 end
 
-local function __appendval( tbl, key, multival, chunk )
-       if multival then
+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
 
-local function __finishval( tbl, key, multival, handler )
+local function __finishval( tbl, key, handler )
        if handler then
-               if multival then
+               if type(tbl[key]) == "table" then
                        tbl[key][#tbl[key]] = handler( tbl[key][#tbl[key]] )
                else
                        tbl[key] = handler( tbl[key] )
@@ -321,9 +316,10 @@ process_states['mime-headers'] = function( msg, chunk, filecb )
 
                                        -- Treat as form field
                                        else
-                                               local mv = __initval( msg.params, field )
+                                               __initval( msg.params, field )
+
                                                msg._mimecallback = function(chunk,eof)
-                                                       __appendval( msg.params, field, mv, chunk )
+                                                       __appendval( msg.params, field, chunk )
                                                end
                                        end
 
@@ -466,7 +462,6 @@ process_states['urldecode-key'] = function( msg, chunk, filecb )
                                local key = urldecode( buffer:sub( 1, spos - 1 ) )
 
                                -- Prepare buffers
-                               msg.params[key]         = ""
                                msg._urldeclength   = msg._urldeclength + epos
                                msg._urldecbuffer   = buffer:sub( epos + 1, #buffer )
 
@@ -476,13 +471,14 @@ process_states['urldecode-key'] = function( msg, chunk, filecb )
                                                filecb( field, chunk, eof )
                                        end
                                else
-                                       local mv = __initval( msg.params, key )
+                                       __initval( msg.params, key )
+
                                        msg._urldeccallback = function( chunk, eof )
-                                               __appendval( msg.params, key, mv, chunk )
+                                               __appendval( msg.params, key, chunk )
 
                                                -- FIXME: Use a filter
                                                if eof then
-                                                       __finishval( msg.params, key, mv, urldecode )
+                                                       __finishval( msg.params, key, urldecode )
                                                end
                                        end
                                end
@@ -627,7 +623,7 @@ function mimedecode_message_body( source, msg, filecb )
 
                -- XXX: we schould propably keep the maximum buffer size in sync with
                --      the blocksize of our original source... but doesn't really matter
-               if msg._mimebuffer ~= null and #msg._mimebuffer > 256 then
+               if msg._mimebuffer ~= nil and #msg._mimebuffer > TSRC_BLOCKSIZE then
                        return ""
                else
                        return source()
@@ -665,7 +661,7 @@ function urldecode_message_body( source, msg )
        -- Create a throttling LTN12 source
        -- See explaination in mimedecode_message_body().
        local tsrc = function()
-               if msg._urldecbuffer ~= null and #msg._urldecbuffer > 0 then
+               if msg._urldecbuffer ~= nil and #msg._urldecbuffer > 0 then
                        return ""
                else
                        return source()