以下代码功能如题
-- 获取请求参数
-- return table
function _M:get_request()
local args = nil
local request_method = ngx.var.request_method
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST" == request_method then
local headers = ngx.req.get_headers()
local content_type = headers["content-type"]
content_type = self:str_replace("\r\n", "", content_type)
ngx.req.read_body()
if string.sub(content_type, 1, 19) == "multipart/form-data" then
args = {}
local body = ngx.req.get_body_data()
local boundary = "--" .. string.sub(headers["content-type"], 31)
local data_table = self:explode(boundary, tostring(body))
for i,v in pairs(data_table) do
t = self:explode("\r\n", v)
if table.getn(t) == 5 then
local start_pos,end_pos,param_name = string.find(t[2], 'Content%-Disposition: form%-data; name="(.*)"')
args[param_name] = t[4]
end
end
elseif string.sub(content_type, 1, 34) == "application/x-www-form-urlencoded" then
args = ngx.req.get_post_args()
elseif string.sub(content_type, 1, 16) == "application/json" then
local json = ngx.req.get_body_data()
if json ~= nil then
args = cjson.decode(json)
self.table_merge(ngx.req.get_uri_args(), args)
end
else
args = ngx.req.get_body_data()
end
if args == nil then
args = ngx.req.get_uri_args()
end
end
return args
end
-- table 合并表 把from_table的key都设置到to_table
function _M.table_merge(from_table, to_table)
for k, v in pairs(from_table) do
to_table[k] = v
end
end