Skip to content

Commit

Permalink
fix: don't override cache relative headers
Browse files Browse the repository at this point in the history
Fix apache#3373
Fix apache#3677

Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander committed Mar 9, 2021
1 parent c530135 commit 63d883b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 21 deletions.
7 changes: 0 additions & 7 deletions apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,6 @@ http {
set $upstream_cache_key '';
set $upstream_cache_bypass '';
set $upstream_no_cache '';
set $upstream_hdr_expires '';
set $upstream_hdr_cache_control '';
proxy_cache $upstream_cache_zone;
proxy_cache_valid any {% if proxy_cache.cache_ttl then %} {* proxy_cache.cache_ttl *} {% else %} 10s {% end %};
Expand All @@ -505,11 +503,6 @@ http {
proxy_no_cache $upstream_no_cache;
proxy_cache_bypass $upstream_cache_bypass;
proxy_hide_header Cache-Control;
proxy_hide_header Expires;
add_header Cache-Control $upstream_hdr_cache_control;
add_header Expires $upstream_hdr_expires;
add_header Apisix-Cache-Status $upstream_cache_status always;
{% end %}
proxy_pass $upstream_scheme://apisix_backend$upstream_uri;
Expand Down
2 changes: 0 additions & 2 deletions apisix/core/ctx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ do
upstream_no_cache = true,
upstream_cache_key = true,
upstream_cache_bypass = true,
upstream_hdr_expires = true,
upstream_hdr_cache_control = true,
}

local mt = {
Expand Down
15 changes: 11 additions & 4 deletions apisix/plugins/proxy-cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,21 @@ function _M.header_filter(conf, ctx)
end
end

local upstream_hdr_cache_control
local upstream_hdr_expires

if conf.hide_cache_headers == true then
ctx.var.upstream_hdr_cache_control = ""
ctx.var.upstream_hdr_expires = ""
upstream_hdr_cache_control = ""
upstream_hdr_expires = ""
else
ctx.var.upstream_hdr_cache_control = ctx.var.upstream_http_cache_control
ctx.var.upstream_hdr_expires = ctx.var.upstream_http_expires
upstream_hdr_cache_control = ctx.var.upstream_http_cache_control
upstream_hdr_expires = ctx.var.upstream_http_expires
end

core.response.set_header("Cache-Control", upstream_hdr_cache_control,
"Expires", upstream_hdr_expires,
"Apisix-Cache-Status", ctx.var.upstream_cache_status)

ctx.var.upstream_no_cache = no_cache
core.log.info("proxy-cache no cache:", no_cache)
end
Expand Down
8 changes: 0 additions & 8 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,6 @@ _EOC_
set \$upstream_cache_key '';
set \$upstream_cache_bypass '';
set \$upstream_no_cache '';
set \$upstream_hdr_expires '';
set \$upstream_hdr_cache_control '';
proxy_cache \$upstream_cache_zone;
proxy_cache_valid any 10s;
Expand All @@ -552,12 +550,6 @@ _EOC_
proxy_no_cache \$upstream_no_cache;
proxy_cache_bypass \$upstream_cache_bypass;
proxy_hide_header Cache-Control;
proxy_hide_header Expires;
add_header Cache-Control \$upstream_hdr_cache_control;
add_header Expires \$upstream_hdr_expires;
add_header Apisix-Cache-Status \$upstream_cache_status always;
access_by_lua_block {
-- wait for etcd sync
ngx.sleep($wait_etcd_sync)
Expand Down
49 changes: 49 additions & 0 deletions t/plugin/proxy-cache.t
Original file line number Diff line number Diff line change
Expand Up @@ -688,3 +688,52 @@ GET /t
qr/failed to check the configuration of plugin proxy-cache err: cache_key variable \$request_method unsupported/
--- no_error_log
[error]
=== TEST 25: don't override cache relative 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,
[[{
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/echo"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 26: hit route
--- request
GET /echo
--- more_headers
Apisix-Cache-Status: Foo
Cache-Control: bar
Expires: any
--- response_headers
Apisix-Cache-Status: Foo
Cache-Control: bar
Expires: any
--- no_error_log
[error]

0 comments on commit 63d883b

Please sign in to comment.