X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=libs%2Fhttp%2Fluasrc%2Fhttp%2Fprotocol.lua;h=22135741cdf9b7685fd49070a44f97beaae5b029;hp=51cb02df2aad129ce4c7c260c488b94f6d846566;hb=079f606bf90e951a7d1dea46968f6676fa698f5c;hpb=653f5e2361c667b7f6dda0d6b20dbc42b4b7c7da diff --git a/libs/http/luasrc/http/protocol.lua b/libs/http/luasrc/http/protocol.lua index 51cb02df2..22135741c 100644 --- a/libs/http/luasrc/http/protocol.lua +++ b/libs/http/luasrc/http/protocol.lua @@ -23,13 +23,17 @@ 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 + if not no_plus then + str = str:gsub( "+", " " ) + end + str = str:gsub( "%%([a-fA-F0-9][a-fA-F0-9])", __chrdec ) end @@ -107,6 +111,36 @@ function urlencode_params( tbl ) end +-- Parameter helper +local function __initval( tbl, key ) + if tbl[key] == nil then + tbl[key] = "" + elseif type(tbl[key]) == "string" then + tbl[key] = { tbl[key], "" } + else + table.insert( tbl[key], "" ) + end +end + +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, handler ) + if handler then + if type(tbl[key]) == "table" 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 = { } @@ -281,9 +315,10 @@ process_states['mime-headers'] = function( msg, chunk, filecb ) -- Treat as form field else - msg.params[field] = "" + __initval( msg.params, field ) + msg._mimecallback = function(chunk,eof) - msg.params[field] = msg.params[field] .. chunk + __appendval( msg.params, field, chunk ) end end @@ -426,7 +461,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 ) @@ -436,12 +470,14 @@ process_states['urldecode-key'] = function( msg, chunk, filecb ) filecb( field, chunk, eof ) end else + __initval( msg.params, key ) + msg._urldeccallback = function( chunk, eof ) - msg.params[key] = msg.params[key] .. chunk + __appendval( msg.params, key, chunk ) -- FIXME: Use a filter if eof then - msg.params[key] = urldecode( msg.params[key] ) + __finishval( msg.params, key, urldecode ) end end end