Authored by chunhua.zhang

Merge branch 'dev_http_request' into 'master'

提取出 取http请求参数

提取出 取http请求参数方法

See merge request !55
local modname= ...
local M={}
_G[modname]=M
package.loaded[modname]=M
function M.get_req_param(req_param)
local args = nil
local err = nil
local http_req_method = ngx.var.request_method
if "GET" == http_req_method then
args, err = ngx.req.get_uri_args()
elseif "POST" == http_req_method then
ngx.req.read_body()
args, err = ngx.req.get_post_args()
end
--- make sure this is some params
if not args then
ngx.say("failed to get args: ", err)
return nil
end
-- the value may be table, like: /test?foo=bar&bar=baz&bar=blah
local val=args[req_param]
if type(val) == "table" then
for k,v in pairs(val) do
if v then
return v
end
end
else
return val
end
end
function M.get_get_req_param(req_param)
local args , err = ngx.req.get_uri_args()
--- make sure this is some params
if not args then
ngx.say("failed to get args: ", err)
return nil
end
-- the value may be table, like: /test?foo=bar&bar=baz&bar=blah
local val=args[req_param]
if type(val) == "table" then
for k,v in pairs(val) do
if v then
return v
end
end
else
return val
end
end
\ No newline at end of file
... ...
local rateLimit = require "limit_common_flow"
local cjson = require "cjson"
local http_request = require "http_request"
local default_err_code=9999991
local default_err_msg="系统正忙,请稍后重试!"
function get_req_param(req_param)
local ret = nil
local args = nil
local err = nil
local http_req_method = ngx.var.request_method
if "GET" == http_req_method then
args, err = ngx.req.get_uri_args()
elseif "POST" == http_req_method then
ngx.req.read_body()
args, err = ngx.req.get_post_args()
end
--- make sure this is some params
if not args then
ngx.say("failed to get args: ", err)
return nil
end
-- the value may be table, like: /test?foo=bar&bar=baz&bar=blah
for key, val in pairs(args) do
if key == req_param then
if type(val) == "table" then
ret = val[0] or val[1]
else
ret = val
end
end
end
return ret
end
--- get param from get
function get_get_req_param(req_param)
local ret = nil
local args , err = ngx.req.get_uri_args()
--- make sure this is some params
if not args then
ngx.say("failed to get args: ", err)
return nil
end
-- the value may be table, like: /test?foo=bar&bar=baz&bar=blah
for key, val in pairs(args) do
if key == req_param then
if type(val) == "table" then
ret = val[0] or val[1]
else
ret = val
end
end
end
return ret
end
function get_req_uri()
local limit_config=lua_context.configs["api_rate_limit_conf"]
... ... @@ -84,10 +31,10 @@ function extract_limit_method()
return uri_method
end
local method= get_req_param("method")
local method= http_request.get_req_param("method")
-- get bussiness line
local business_line = get_get_req_param("business_line")
local business_line = http_request.get_get_req_param("business_line")
if (business_line == "miniappBrandM1") or (business_line == "miniappBrandM2") then
method = business_line
end
... ...
... ... @@ -7,6 +7,7 @@ local cjson=require "cjson"
local rate_limit = require "limit_common_flow"
local iptool=require "iptool"
local lrucache = require "resty.lrucache"
local http_request = require "http_request"
local cache=lua_context.mal_ip_cache
... ... @@ -29,16 +30,6 @@ function M.in_array(value,arr)
return false
end
function M:get_req_param(req_param)
local method = ngx.var.request_method
if "GET" == method then
return ngx.req.get_uri_args()[req_param]
else
ngx.req.read_body()
return ngx.req.get_post_args()[req_param]
end
end
--1:api 2:service
function M:limit_ip_access()
local limit_ip_config=lua_context.configs["limit_ip_access"]
... ... @@ -79,7 +70,7 @@ function M:limit_ip_access()
local req_uri_method
local method_limit_conf
if(not is_limit) and limit_ip_config.interface_ip_qps_limit then
req_uri_method = self:get_req_param("method")
req_uri_method = http_request.get_req_param("method")
if not req_uri_method then
req_uri_method = ngx.var.uri
end
... ... @@ -136,9 +127,9 @@ end
-- malicious ip
function M:mal_ip()
local method=self:get_req_param("method")
local ips=self:get_req_param("ips")
local expire=self:get_req_param("expire")
local method=http_request.get_req_param("method")
local ips=http_request.get_req_param("ips")
local expire=http_request.get_req_param("expire")
ngx.header["Content-type"]="application/json;charset=utf-8"
if not method then
ngx.say('{"code": 400, "msg": "params error!"}')
... ...