Skip to content

Commit

Permalink
fix(traffic-split): post_arg match fails because content-type contain…
Browse files Browse the repository at this point in the history
…s charset (#10372)
  • Loading branch information
Sn0rt committed Oct 23, 2023
1 parent 3b16fce commit d579365
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
4 changes: 3 additions & 1 deletion apisix/core/ctx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ do

elseif core_str.has_prefix(key, "post_arg_") then
-- only match default post form
if request.header(nil, "Content-Type") == "application/x-www-form-urlencoded" then
local content_type = request.header(nil, "Content-Type")
if content_type ~= nil and core_str.has_prefix(content_type,
"application/x-www-form-urlencoded") then
local arg_key = sub_str(key, 10)
local args = request.get_post_args()[arg_key]
if args then
Expand Down
25 changes: 19 additions & 6 deletions t/core/ctx2.t
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,20 @@ find ctx.req_post_args.test: true
=== TEST 13: missed (post_arg_test is missing)
=== TEST 13: hit with charset
--- request
POST /hello
test=test
--- more_headers
Content-Type: application/x-www-form-urlencoded;charset=utf-8
--- response_body
hello world
--- error_log
find ctx.req_post_args.test: true
=== TEST 14: missed (post_arg_test is missing)
--- request
POST /hello
--- more_headers
Expand All @@ -303,7 +316,7 @@ Content-Type: application/x-www-form-urlencoded
=== TEST 14: missed (post_arg_test is mismatch)
=== TEST 15: missed (post_arg_test is mismatch)
--- request
POST /hello
test=tesy
Expand All @@ -315,7 +328,7 @@ Content-Type: application/x-www-form-urlencoded
=== TEST 15: register custom variable
=== TEST 16: register custom variable
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -351,7 +364,7 @@ Content-Type: application/x-www-form-urlencoded
=== TEST 16: hit
=== TEST 17: hit
--- config
location /t {
content_by_lua_block {
Expand All @@ -375,7 +388,7 @@ find ctx.var.a6_labels_zone: Singapore
=== TEST 17: register custom variable with no cacheable
=== TEST 18: register custom variable with no cacheable
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -412,7 +425,7 @@ find ctx.var.a6_labels_zone: Singapore
=== TEST 18: hit
=== TEST 19: hit
--- config
location /t {
content_by_lua_block {
Expand Down
75 changes: 75 additions & 0 deletions t/plugin/traffic-split5.t
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,78 @@ GET /server_port?name=jack
--- error_log eval
qr/event timer add: \d+: 12345000:\d+/
--- error_code: 502
=== TEST 9: set upstream for post_arg_id test case
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin").test
local data = {
uri = "/hello",
plugins = {
["traffic-split"] = {
rules = {
{
match = { {
vars = { { "post_arg_id", "==", "1" } }
} },
weighted_upstreams = {
{
upstream = {
name = "upstream_A",
type = "roundrobin",
nodes = {
["127.0.0.1:1970"] = 1
}
},
weight = 1
}
}
}
}
}
},
upstream = {
type = "roundrobin",
nodes = {
["127.0.0.1:1974"] = 1
}
}
}
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
json.encode(data)
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 10: post_arg_id = 1 without content-type charset
--- request
POST /hello
id=1
--- more_headers
Content-Type: application/x-www-form-urlencoded
--- response_body
1970
=== TEST 11: post_arg_id = 1 with content-type charset
--- request
POST /hello
id=1
--- more_headers
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
--- response_body
1970

0 comments on commit d579365

Please sign in to comment.