Skip to content

Commit

Permalink
fix(proxy-rewrite): when conf.headers are missing,conf.method can mak…
Browse files Browse the repository at this point in the history
…e effect (#6300)

Co-authored-by: 高亮亮 <gll287546@alibaba-inc.com>
  • Loading branch information
liangliang4ward and 高亮亮 authored Feb 15, 2022
1 parent b06a2a6 commit bcf13cd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
24 changes: 11 additions & 13 deletions apisix/plugins/proxy-rewrite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,20 @@ function _M.rewrite(conf, ctx)
ctx.var.upstream_uri = upstream_uri
end

if not conf.headers then
return
end

if not conf.headers_arr then
conf.headers_arr = {}
if conf.headers then
if not conf.headers_arr then
conf.headers_arr = {}

for field, value in pairs(conf.headers) do
core.table.insert_tail(conf.headers_arr, field, value)
for field, value in pairs(conf.headers) do
core.table.insert_tail(conf.headers_arr, field, value)
end
end
end

local field_cnt = #conf.headers_arr
for i = 1, field_cnt, 2 do
core.request.set_header(ctx, conf.headers_arr[i],
core.utils.resolve_var(conf.headers_arr[i+1], ctx.var))
local field_cnt = #conf.headers_arr
for i = 1, field_cnt, 2 do
core.request.set_header(ctx, conf.headers_arr[i],
core.utils.resolve_var(conf.headers_arr[i+1], ctx.var))
end
end

if conf.method then
Expand Down
53 changes: 51 additions & 2 deletions t/plugin/proxy-rewrite3.t
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ passed
=== TEST 2: hit route(upstream uri: should be /hello)
--- request
GET /hello
--- grep_error_log_out
--- error_log
plugin_proxy_rewrite get method: POST
Expand Down Expand Up @@ -125,7 +125,7 @@ passed
=== TEST 4: hit route(upstream uri: should be /hello)
--- request
GET /hello
--- grep_error_log_out
--- error_log
plugin_proxy_rewrite get method: GET
Expand All @@ -151,3 +151,52 @@ plugin_proxy_rewrite get method: GET
--- response_body
property "method" validation failed: matches none of the enum values
done
=== TEST 6: set route(rewrite method with headers)
--- 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,
[[{
"methods": ["GET"],
"plugins": {
"proxy-rewrite": {
"uri": "/plugin_proxy_rewrite",
"method": "POST",
"scheme": "http",
"host": "apisix.iresty.com",
"headers":{
"x-api-version":"v1"
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 7: hit route(with header)
--- request
GET /hello
--- error_log
plugin_proxy_rewrite get method: POST

0 comments on commit bcf13cd

Please # to comment.