Skip to content

Commit 9bab121

Browse files
committed
now we do not store responses with response header "Cache-Control: private" to cache by default.
1 parent 576ce3b commit 9bab121

File tree

4 files changed

+138
-3
lines changed

4 files changed

+138
-3
lines changed

src/ngx_http_srcache_filter_module.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,24 @@ ngx_http_srcache_header_filter(ngx_http_request_t *r)
262262

263263
#if 1
264264
if (!(r->method & slcf->cache_methods)) {
265+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
266+
"srcache_store skipped due to request method %V", &r->method_name);
267+
265268
return ngx_http_next_header_filter(r);
266269
}
267270
#endif
268271

272+
if (ngx_http_srcache_response_no_cache(r, slcf) == NGX_OK) {
273+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
274+
"srcache_store skipped due to response Cache-Control setting");
275+
276+
return ngx_http_next_header_filter(r);
277+
}
278+
269279
if (slcf->store_skip != NULL
270-
&& ngx_http_complex_value(r, slcf->store_skip, &skip) == NGX_OK
271-
&& skip.len
272-
&& (skip.len != 1 || skip.data[0] != '0'))
280+
&& ngx_http_complex_value(r, slcf->store_skip, &skip) == NGX_OK
281+
&& skip.len
282+
&& (skip.len != 1 || skip.data[0] != '0'))
273283
{
274284
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
275285
"srcache_store skipped due to the true value fed into "
@@ -743,6 +753,10 @@ ngx_http_srcache_access_handler(ngx_http_request_t *r)
743753
dd("cache methods: %lu", (unsigned long) conf->cache_methods);
744754

745755
if (!(r->method & conf->cache_methods)) {
756+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
757+
"srcache_fetch and srcache_store skipped due to request "
758+
"method %V", &r->method_name);
759+
746760
return NGX_DECLINED;
747761
}
748762

src/ngx_http_srcache_util.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,34 @@ ngx_http_srcache_request_no_cache(ngx_http_request_t *r)
438438
return NGX_DECLINED;
439439
}
440440

441+
442+
ngx_int_t
443+
ngx_http_srcache_response_no_cache(ngx_http_request_t *r,
444+
ngx_http_srcache_loc_conf_t *conf)
445+
{
446+
ngx_table_elt_t **ccp;
447+
ngx_uint_t i;
448+
u_char *p, *last;
449+
450+
ccp = r->headers_out.cache_control.elts;
451+
452+
if (ccp == NULL) {
453+
return NGX_DECLINED;
454+
}
455+
456+
for (i = 0; i < r->headers_out.cache_control.nelts; i++) {
457+
if (!ccp[i]->hash) {
458+
continue;
459+
}
460+
461+
p = ccp[i]->value.data;
462+
last = p + ccp[i]->value.len;
463+
464+
if (ngx_strlcasestrn(p, last, (u_char *) "private", 7 - 1) != NULL) {
465+
return NGX_OK;
466+
}
467+
}
468+
469+
return NGX_DECLINED;
470+
}
471+

src/ngx_http_srcache_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ ngx_int_t ngx_http_srcache_add_copy_chain(ngx_pool_t *pool,
3535
ngx_int_t ngx_http_srcache_post_request_at_head(ngx_http_request_t *r,
3636
ngx_http_posted_request_t *pr);
3737
ngx_int_t ngx_http_srcache_request_no_cache(ngx_http_request_t *r);
38+
ngx_int_t ngx_http_srcache_response_no_cache(ngx_http_request_t *r,
39+
ngx_http_srcache_loc_conf_t *conf);
3840

3941

4042
#endif /* NGX_HTTP_SRCACHE_UTIL_H */

t/private.t

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# vi:filetype=
2+
3+
use lib 'lib';
4+
use Test::Nginx::Socket;
5+
6+
#repeat_each(2);
7+
8+
plan tests => repeat_each() * 4 * blocks();
9+
10+
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
11+
12+
#master_on();
13+
no_shuffle();
14+
15+
run_tests();
16+
17+
__DATA__
18+
19+
=== TEST 1: flush all
20+
--- config
21+
location /flush {
22+
set $memc_cmd 'flush_all';
23+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
24+
}
25+
--- response_headers
26+
Content-Type: text/plain
27+
Content-Length: 4
28+
--- request
29+
GET /flush
30+
--- response_body eval: "OK\r\n"
31+
32+
33+
34+
=== TEST 2: basic fetch (cache miss), and not stored due to Cache-Control: private
35+
--- config
36+
location /foo {
37+
default_type text/css;
38+
srcache_fetch GET /memc $uri;
39+
srcache_store PUT /memc $uri;
40+
41+
content_by_lua '
42+
ngx.header.cache_control = "private"
43+
ngx.say("hello")
44+
';
45+
}
46+
47+
location /memc {
48+
internal;
49+
50+
set $memc_key $query_string;
51+
set $memc_exptime 300;
52+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
53+
}
54+
--- request
55+
GET /foo
56+
--- response_headers
57+
Content-Type: text/css
58+
Content-Length:
59+
--- response_body
60+
hello
61+
62+
63+
64+
=== TEST 3: basic fetch (cache miss again, not stored in the previous case)
65+
--- config
66+
location /foo {
67+
default_type text/css;
68+
srcache_fetch GET /memc $uri;
69+
srcache_store PUT /memc $uri;
70+
71+
echo world;
72+
}
73+
74+
location /memc {
75+
internal;
76+
77+
set $memc_key $query_string;
78+
set $memc_exptime 300;
79+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
80+
}
81+
--- request
82+
GET /foo
83+
--- response_headers
84+
Content-Type: text/css
85+
Content-Length:
86+
--- response_body
87+
world
88+

0 commit comments

Comments
 (0)