This is an automated email from the ASF dual-hosted git repository.
spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new d538105 fix: don't override cache relative headers (#3789)
d538105 is described below
commit d538105b1adcc76819f3c539d9c5bde7e9a5b482
Author: 罗泽轩 <[email protected]>
AuthorDate: Wed Mar 10 09:02:28 2021 +0800
fix: don't override cache relative headers (#3789)
---
apisix/cli/ngx_tpl.lua | 7 ------
apisix/core/ctx.lua | 2 --
apisix/plugins/proxy-cache.lua | 15 +++++++++----
t/APISIX.pm | 8 -------
t/plugin/proxy-cache.t | 49 ++++++++++++++++++++++++++++++++++++++++++
5 files changed, 60 insertions(+), 21 deletions(-)
diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua
index 7255a10..b7c1e17 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -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
%};
@@ -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;
diff --git a/apisix/core/ctx.lua b/apisix/core/ctx.lua
index 962a270..d87342b 100644
--- a/apisix/core/ctx.lua
+++ b/apisix/core/ctx.lua
@@ -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 = {
diff --git a/apisix/plugins/proxy-cache.lua b/apisix/plugins/proxy-cache.lua
index b3acbb0..78a2858 100644
--- a/apisix/plugins/proxy-cache.lua
+++ b/apisix/plugins/proxy-cache.lua
@@ -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
diff --git a/t/APISIX.pm b/t/APISIX.pm
index 1a51d1e..feb82d0 100644
--- a/t/APISIX.pm
+++ b/t/APISIX.pm
@@ -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;
@@ -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)
diff --git a/t/plugin/proxy-cache.t b/t/plugin/proxy-cache.t
index 5a1e244..c1d6dcd 100644
--- a/t/plugin/proxy-cache.t
+++ b/t/plugin/proxy-cache.t
@@ -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]