X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=modules%2Fluci-base%2Fluasrc%2Fhttp%2Fprotocol.lua;h=096ae46f4a28621aa7cfb5fa95aa195af224aa9a;hp=61d7b802fe87c2dbe4f6276a6dcd78893e35ed93;hb=f52c8d0b7f0eeec4259992182103d859f454639b;hpb=ae8e971eef2be269beab9465139869055918d878 diff --git a/modules/luci-base/luasrc/http/protocol.lua b/modules/luci-base/luasrc/http/protocol.lua index 61d7b802f..096ae46f4 100644 --- a/modules/luci-base/luasrc/http/protocol.lua +++ b/modules/luci-base/luasrc/http/protocol.lua @@ -6,27 +6,10 @@ module("luci.http.protocol", package.seeall) local ltn12 = require("luci.ltn12") +local util = require("luci.util") HTTP_MAX_CONTENT = 1024*8 -- 8 kB maximum content size --- the "+" sign to " " - and return the decoded string. -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 - - return str -end - -- from given url or string. Returns a table with urldecoded values. -- Simple parameters are stored as string values associated with the parameter -- name within the table. Parameters with multiple values are stored as array @@ -42,8 +25,8 @@ function urldecode_params( url, tbl ) for pair in url:gmatch( "[^&;]+" ) do -- find key and value - local key = urldecode( pair:match("^([^=]+)") ) - local val = urldecode( pair:match("^[^=]+=(.+)$") ) + local key = util.urldecode( pair:match("^([^=]+)") ) + local val = util.urldecode( pair:match("^[^=]+=(.+)$") ) -- store if type(key) == "string" and key:len() > 0 then @@ -62,24 +45,6 @@ function urldecode_params( url, tbl ) return params end -function urlencode( str ) - - local function __chrenc( chr ) - return string.format( - "%%%02x", string.byte( chr ) - ) - end - - if type(str) == "string" then - str = str:gsub( - "([^a-zA-Z0-9$_%-%.!*'(),])", - __chrenc - ) - end - - return str -end - -- separated by "&". Tables are encoded as parameters with multiple values by -- repeating the parameter name with each value. function urlencode_params( tbl ) @@ -89,11 +54,11 @@ function urlencode_params( tbl ) if type(v) == "table" then for i, v2 in ipairs(v) do enc = enc .. ( #enc > 0 and "&" or "" ) .. - urlencode(k) .. "=" .. urlencode(v2) + util.urlencode(k) .. "=" .. util.urlencode(v2) end else enc = enc .. ( #enc > 0 and "&" or "" ) .. - urlencode(k) .. "=" .. urlencode(v) + util.urlencode(k) .. "=" .. util.urlencode(v) end end @@ -114,6 +79,16 @@ local function __initval( tbl, key ) end -- (Internal function) +-- Initialize given file parameter. +local function __initfileval( tbl, key, filename, fd ) + if tbl[key] == nil then + tbl[key] = { file=filename, fd=fd, name=key, "" } + else + table.insert( tbl[key], "" ) + end +end + +-- (Internal function) -- Append given data to given parameter, either by extending the string value -- or by appending it to the last string in the parameter's value table. local function __appendval( tbl, key, chunk ) @@ -254,7 +229,7 @@ function header_source( sock ) end -- Content-Type. Stores all extracted data associated with its parameter name --- in the params table withing the given message object. Multiple parameter +-- in the params table within the given message object. Multiple parameter -- values are stored as tables, ordinary ones as strings. -- If an optional file callback function is given then it is feeded with the -- file contents chunk by chunk and only the extracted file name is stored @@ -313,6 +288,22 @@ function mimedecode_message_body( src, msg, filecb ) __appendval( msg.params, field.name, field.file ) store = filecb + elseif field.name and field.file then + local nxf = require "nixio" + local fd = nxf.mkstemp(field.name) + __initfileval ( msg.params, field.name, field.file, fd ) + if fd then + store = function(hdr, buf, eof) + fd:write(buf) + if (eof) then + fd:seek(0, "set") + end + end + else + store = function( hdr, buf, eof ) + __appendval( msg.params, field.name, buf ) + end + end elseif field.name then __initval( msg.params, field.name ) @@ -407,7 +398,7 @@ function mimedecode_message_body( src, msg, filecb ) end -- Content-Type. Stores all extracted data associated with its parameter name --- in the params table withing the given message object. Multiple parameter +-- in the params table within the given message object. Multiple parameter -- values are stored as tables, ordinary ones as strings. function urldecode_message_body( src, msg ) @@ -559,14 +550,23 @@ function parse_message_body( src, msg, filecb ) -- If we have a file callback then feed it if type(filecb) == "function" then - sink = filecb - + local meta = { + name = "raw", + encoding = msg.env.CONTENT_TYPE + } + sink = function( chunk ) + if chunk then + return filecb(meta, chunk, false) + else + return filecb(meta, nil, true) + end + end -- ... else append to .content else msg.content = "" msg.content_length = 0 - sink = function( chunk, err ) + sink = function( chunk ) if chunk then if ( msg.content_length + #chunk ) <= HTTP_MAX_CONTENT then msg.content = msg.content .. chunk