mirror of
https://gitee.com/iresty/apisix.git
synced 2024-12-02 12:07:35 +08:00
bugfix: fail to get the http request header by ngx.var. *
(#1348)
* bugfix: to get the HTTP request header by `ngx.var.*`, we need to convert the name to lower case with dashes replaced by underscores. more information: http://nginx.org/en/docs/http/ngx_http_core_module.html#var_http_ * change: the key should be a string object when fetching a value from `ctx.var`. * bug(make lint): If the code style is bad, do exit with code 1.
This commit is contained in:
parent
f06f36186e
commit
51684de77b
@ -25,6 +25,9 @@ local C = ffi.C
|
||||
local sub_str = string.sub
|
||||
local rawset = rawset
|
||||
local ngx_var = ngx.var
|
||||
local re_gsub = ngx.re.gsub
|
||||
local type = type
|
||||
local error = error
|
||||
|
||||
|
||||
ffi.cdef[[
|
||||
@ -61,6 +64,10 @@ do
|
||||
|
||||
local mt = {
|
||||
__index = function(t, key)
|
||||
if type(key) ~= "string" then
|
||||
error("invalid argument, expect string value", 2)
|
||||
end
|
||||
|
||||
local val
|
||||
local method = var_methods[key]
|
||||
if method then
|
||||
@ -77,6 +84,11 @@ do
|
||||
end
|
||||
end
|
||||
|
||||
elseif C.memcmp(key, "http_", 5) == 0 then
|
||||
key = key:lower()
|
||||
key = re_gsub(key, "-", "_", "jo")
|
||||
val = get_var(key, t._request)
|
||||
|
||||
else
|
||||
val = get_var(key, t._request)
|
||||
end
|
||||
|
42
t/core/ctx.t
42
t/core/ctx.t
@ -113,3 +113,45 @@ cookie_c: ccc
|
||||
cookie_d: nil
|
||||
--- no_error_log
|
||||
[error]
|
||||
|
||||
|
||||
|
||||
=== TEST 5: key is nil
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local ctx = {}
|
||||
core.ctx.set_vars_meta(ctx)
|
||||
|
||||
ngx.say("cookie_a: ", ctx.var[nil])
|
||||
}
|
||||
}
|
||||
--- more_headers
|
||||
Cookie: a=a; b=bb; c=ccc
|
||||
--- request
|
||||
GET /t?a=aaa
|
||||
--- error_code: 500
|
||||
--- error_log
|
||||
invalid argument, expect string value
|
||||
|
||||
|
||||
|
||||
=== TEST 6: key is number
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local ctx = {}
|
||||
core.ctx.set_vars_meta(ctx)
|
||||
|
||||
ngx.say("cookie_a: ", ctx.var[2222])
|
||||
}
|
||||
}
|
||||
--- more_headers
|
||||
Cookie: a=a; b=bb; c=ccc
|
||||
--- request
|
||||
GET /t?a=aaa
|
||||
--- error_code: 500
|
||||
--- error_log
|
||||
invalid argument, expect string value
|
||||
|
@ -14,16 +14,6 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
BEGIN {
|
||||
if ($ENV{TEST_NGINX_CHECK_LEAK}) {
|
||||
$SkipReason = "unavailable for the hup tests";
|
||||
|
||||
} else {
|
||||
$ENV{TEST_NGINX_USE_HUP} = 1;
|
||||
undef $ENV{TEST_NGINX_USE_STAP};
|
||||
}
|
||||
}
|
||||
|
||||
use t::APISIX 'no_plan';
|
||||
|
||||
repeat_each(1);
|
||||
|
@ -535,3 +535,93 @@ chash_key fetch is nil, use default chash_key remote_addr: 127.0.0.1
|
||||
chash_key fetch is nil, use default chash_key remote_addr: 127.0.0.1
|
||||
chash_key fetch is nil, use default chash_key remote_addr: 127.0.0.1
|
||||
chash_key fetch is nil, use default chash_key remote_addr: 127.0.0.1
|
||||
|
||||
|
||||
|
||||
=== TEST 11: set route(key contains uppercase letters and hyphen)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local t = require("lib.test_admin").test
|
||||
local code, body = t('/apisix/admin/routes/1',
|
||||
ngx.HTTP_PUT,
|
||||
[[{
|
||||
"uri": "/server_port",
|
||||
"upstream": {
|
||||
"key": "X-Sessionid",
|
||||
"type": "chash",
|
||||
"hash_on": "header",
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1,
|
||||
"127.0.0.1:1981": 1
|
||||
}
|
||||
}
|
||||
}]]
|
||||
)
|
||||
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
--- no_error_log
|
||||
[error]
|
||||
|
||||
|
||||
|
||||
=== TEST 12: hit routes with header
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local http = require "resty.http"
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port
|
||||
.. "/server_port"
|
||||
|
||||
local ports_count = {}
|
||||
for i = 1, 6 do
|
||||
local httpc = http.new()
|
||||
local res, err = httpc:request_uri(uri, {
|
||||
method = "GET",
|
||||
headers = {
|
||||
["X-Sessionid"] = "chash_val_" .. i
|
||||
}
|
||||
})
|
||||
if not res then
|
||||
ngx.say(err)
|
||||
return
|
||||
end
|
||||
ports_count[res.body] = (ports_count[res.body] or 0) + 1
|
||||
end
|
||||
|
||||
local ports_arr = {}
|
||||
for port, count in pairs(ports_count) do
|
||||
table.insert(ports_arr, {port = port, count = count})
|
||||
end
|
||||
|
||||
local function cmd(a, b)
|
||||
return a.port > b.port
|
||||
end
|
||||
table.sort(ports_arr, cmd)
|
||||
|
||||
ngx.say(require("cjson").encode(ports_arr))
|
||||
ngx.exit(200)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
[{"count":3,"port":"1981"},{"count":3,"port":"1980"}]
|
||||
--- no_error_log
|
||||
[error]
|
||||
--- error_log
|
||||
chash_key: "chash_val_1"
|
||||
chash_key: "chash_val_2"
|
||||
chash_key: "chash_val_3"
|
||||
chash_key: "chash_val_4"
|
||||
chash_key: "chash_val_5"
|
||||
chash_key: "chash_val_6"
|
||||
|
@ -68,7 +68,6 @@ passed
|
||||
|
||||
|
||||
|
||||
|
||||
=== TEST 2: /not_found
|
||||
--- request
|
||||
GET /not_found
|
||||
|
@ -32,10 +32,9 @@ luacheck -q lua
|
||||
lua/apisix/plugins/limit-count/*.lua > \
|
||||
/tmp/check.log 2>&1 || (cat /tmp/check.log && exit 1)
|
||||
|
||||
count=`grep -E ".lua:[0-9]+:" /tmp/check.log -c | true`
|
||||
|
||||
if [ $count -ne 0 ]
|
||||
then
|
||||
grep -E "ERROR.*.lua:" /tmp/check.log > /tmp/error.log | true
|
||||
if [ -s /tmp/error.log ]; then
|
||||
echo "=====bad style====="
|
||||
cat /tmp/check.log
|
||||
exit 1
|
||||
fi
|
||||
|
Loading…
Reference in New Issue
Block a user