Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support dash in args (#4519) #4676

Merged
merged 19 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apisix/admin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ end
local function run()
local api_ctx = {}
core.ctx.set_vars_meta(api_ctx)
ngx.ctx.api_ctx = api_ctx

local ok, err = check_token(api_ctx)
if not ok then
Expand Down Expand Up @@ -185,6 +186,7 @@ end
local function run_stream()
local api_ctx = {}
core.ctx.set_vars_meta(api_ctx)
ngx.ctx.api_ctx = api_ctx

local local_conf = core.config.local_conf()
if not local_conf.apisix.stream_proxy then
Expand Down Expand Up @@ -255,6 +257,7 @@ end
local function get_plugins_list()
local api_ctx = {}
core.ctx.set_vars_meta(api_ctx)
ngx.ctx.api_ctx = api_ctx

local ok, err = check_token(api_ctx)
if not ok then
Expand All @@ -270,6 +273,7 @@ end
local function post_reload_plugins()
local api_ctx = {}
core.ctx.set_vars_meta(api_ctx)
ngx.ctx.api_ctx = api_ctx

local ok, err = check_token(api_ctx)
if not ok then
Expand Down
13 changes: 12 additions & 1 deletion apisix/core/ctx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,18 @@ do
val, err = cookie:get(sub_str(key, 8))
if err then
log.warn("failed to fetch cookie value by key: ",
key, " error: ", err)
key, " error: ", err)
nanamikon marked this conversation as resolved.
Show resolved Hide resolved
end
end

elseif core_str.has_prefix(key, "arg_") then
local arg_key = sub_str(key, 5)
local args = request.get_uri_args()[arg_key]
if args then
if type(args) == "table" then
val = args[1]
else
val = args
end
end

Expand Down
6 changes: 2 additions & 4 deletions t/core/ctx.t
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ server_port: 1984



=== TEST 2: http header + arg
=== TEST 2: http header
--- config
location /t {
content_by_lua_block {
Expand All @@ -55,14 +55,12 @@ server_port: 1984
core.ctx.set_vars_meta(ctx)

ngx.say("http_host: ", ctx.var["http_host"])
ngx.say("arg_a: ", ctx.var["arg_a"])
}
}
--- request
GET /t?a=aaa
GET /t
--- response_body
http_host: localhost
arg_a: aaa
--- no_error_log
[error]

Expand Down
52 changes: 52 additions & 0 deletions t/core/ctx2.t
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,55 @@ query repo {
hello world
--- error_log
find ctx._graphql: true



=== TEST 7: support dash in the args
--- 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,
[=[{
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello",
"vars": [["arg_a-b", "==", "ab"]]
}]=]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}



=== TEST 8: check (support dash in the args)
--- request
GET /hello?a-b=ab
--- response_body
hello world



=== TEST 9: support dash in the args(Multi args with the same name, only fetch the first one)
--- request
GET /hello?a-b=ab&a-b=ccc
--- response_body
hello world



=== TEST 10: support dash in the args(arg is missing)
--- request
GET /hello
--- error_code: 404
--- response_body
{"error_msg":"404 Route Not Found"}