* luci/libs/http: remove left over field initialisation
[project/luci.git] / libs / http / luasrc / http / protocol.lua
index ac58578..cb5f786 100644 (file)
@@ -16,7 +16,6 @@ $Id$
 module("luci.http.protocol", package.seeall)
 
 local ltn12 = require("luci.ltn12")
-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
@@ -24,14 +23,18 @@ HTTP_URLENC_MAXKEYLEN = 1024                -- maximum allowd size of urlencoded parameter nam
 
 -- Decode an urlencoded string.
 -- Returns the decoded value.
-function urldecode( str )
+function urldecode( str, no_plus )
 
        local function __chrdec( hex )
                return string.char( tonumber( hex, 16 ) )
        end
 
        if type(str) == "string" then
-               str = str:gsub( "+", " " ):gsub( "%%([a-fA-F0-9][a-fA-F0-9])", __chrdec )
+               if not no_plus then
+                       str = str:gsub( "+", " " )
+               end
+
+               str = str:gsub( "%%([a-fA-F0-9][a-fA-F0-9])", __chrdec )
        end
 
        return str
@@ -84,7 +87,7 @@ function urlencode( str )
 
        if type(str) == "string" then
                str = str:gsub(
-                       "([^a-zA-Z0-9$_%-%.+!*'(),])",
+                       "([^a-zA-Z0-9$_%-%.%+!*'(),])",
                        __chrenc
                )
        end
@@ -108,6 +111,42 @@ function urlencode_params( tbl )
 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
+               tbl[key] = ""
+       end
+
+       return multival
+end
+
+local function __appendval( tbl, key, multival, chunk )
+       if multival 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 )
+       if handler then
+               if multival 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 = { }
 
@@ -282,9 +321,9 @@ process_states['mime-headers'] = function( msg, chunk, filecb )
 
                                        -- Treat as form field
                                        else
-                                               msg.params[field] = ""
+                                               local mv = __initval( msg.params, field )
                                                msg._mimecallback = function(chunk,eof)
-                                                       msg.params[field] = msg.params[field] .. chunk
+                                                       __appendval( msg.params, field, mv, chunk )
                                                end
                                        end
 
@@ -427,7 +466,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 )
 
@@ -437,12 +475,13 @@ process_states['urldecode-key'] = function( msg, chunk, filecb )
                                                filecb( field, chunk, eof )
                                        end
                                else
+                                       local mv = __initval( msg.params, key )
                                        msg._urldeccallback = function( chunk, eof )
-                                               msg.params[key] = msg.params[key] .. chunk
+                                               __appendval( msg.params, key, mv, chunk )
 
                                                -- FIXME: Use a filter
                                                if eof then
-                                                       msg.params[key] = urldecode( msg.params[key] )
+                                                       __finishval( msg.params, key, mv, urldecode )
                                                end
                                        end
                                end
@@ -536,7 +575,7 @@ function header_source( sock )
                if chunk == nil then
                        if err ~= "timeout" then
                                return nil, part
-                                       and "Line exceeds maximum allowed length["..part.."]"
+                                       and "Line exceeds maximum allowed length"
                                        or  "Unexpected EOF"
                        else
                                return nil, err