diff options
author | Jo-Philipp Wich <jow@openwrt.org> | 2008-07-14 16:45:37 +0000 |
---|---|---|
committer | Jo-Philipp Wich <jow@openwrt.org> | 2008-07-14 16:45:37 +0000 |
commit | 6ebbffc727a1d8d0057771b448e86a43c62636ab (patch) | |
tree | 67b7165be03d418e08cc07fcb39ddfba38d70ab2 /libs | |
parent | 426f12416dd59cd24524fb577bdea8b2e4c440f9 (diff) |
* luci/libs/http: implement multi-value support in http.protocol
Diffstat (limited to 'libs')
-rw-r--r-- | libs/http/luasrc/http/protocol.lua | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/libs/http/luasrc/http/protocol.lua b/libs/http/luasrc/http/protocol.lua index b8e962449..a9a6d8dc8 100644 --- a/libs/http/luasrc/http/protocol.lua +++ b/libs/http/luasrc/http/protocol.lua @@ -111,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 = { } @@ -285,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 @@ -440,12 +476,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 |