Skip to content

Commit 2574298

Browse files
committed
implemented the srcache_store_private directive to control whether to store responses with the header "Cache-Control: private".
1 parent 9bab121 commit 2574298

File tree

4 files changed

+232
-5
lines changed

4 files changed

+232
-5
lines changed

src/ngx_http_srcache_filter_module.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "ddebug.h"
55

66
/*
7-
* Copyright (C) Yichun Zhang (agentzh)
7+
* Copyright (C) Zhang "agentzh" Yichun
88
*/
99

1010

@@ -122,6 +122,13 @@ static ngx_command_t ngx_http_srcache_commands[] = {
122122
offsetof(ngx_http_srcache_loc_conf_t, req_cache_control),
123123
NULL },
124124

125+
{ ngx_string("srcache_store_private"),
126+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
127+
ngx_conf_set_flag_slot,
128+
NGX_HTTP_LOC_CONF_OFFSET,
129+
offsetof(ngx_http_srcache_loc_conf_t, store_private),
130+
NULL },
131+
125132
ngx_null_command
126133
};
127134

@@ -593,12 +600,10 @@ ngx_http_srcache_create_loc_conf(ngx_conf_t *cf)
593600

594601
conf->fetch = NGX_CONF_UNSET_PTR;
595602
conf->store = NGX_CONF_UNSET_PTR;
596-
597603
conf->buf_size = NGX_CONF_UNSET_SIZE;
598-
599604
conf->store_max_size = NGX_CONF_UNSET_SIZE;
600-
601605
conf->req_cache_control = NGX_CONF_UNSET;
606+
conf->store_private = NGX_CONF_UNSET;
602607

603608
return conf;
604609
}
@@ -633,6 +638,7 @@ ngx_http_srcache_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
633638
conf->cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD;
634639

635640
ngx_conf_merge_value(conf->req_cache_control, prev->req_cache_control, 0);
641+
ngx_conf_merge_value(conf->store_private, prev->store_private, 0);
636642

637643
return NGX_CONF_OK;
638644
}

src/ngx_http_srcache_filter_module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ typedef struct {
3434
ngx_http_complex_value_t *store_skip;
3535
ngx_uint_t cache_methods;
3636
ngx_flag_t req_cache_control;
37+
ngx_flag_t store_private;
3738

3839
unsigned postponed_to_access_phase_end;
3940
} ngx_http_srcache_loc_conf_t;

src/ngx_http_srcache_util.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,9 @@ ngx_http_srcache_response_no_cache(ngx_http_request_t *r,
461461
p = ccp[i]->value.data;
462462
last = p + ccp[i]->value.len;
463463

464-
if (ngx_strlcasestrn(p, last, (u_char *) "private", 7 - 1) != NULL) {
464+
if (!conf->store_private
465+
&& ngx_strlcasestrn(p, last, (u_char *) "private", 7 - 1) != NULL)
466+
{
465467
return NGX_OK;
466468
}
467469
}

t/private.t

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,221 @@ Content-Length:
8686
--- response_body
8787
world
8888
89+
90+
91+
=== TEST 4: flush all
92+
--- config
93+
location /flush {
94+
set $memc_cmd 'flush_all';
95+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
96+
}
97+
--- response_headers
98+
Content-Type: text/plain
99+
Content-Length: 4
100+
--- request
101+
GET /flush
102+
--- response_body eval: "OK\r\n"
103+
104+
105+
106+
=== TEST 5: basic fetch (cache miss), and not stored due to Cache-Control: private (srcache_store_private off)
107+
--- config
108+
location /foo {
109+
default_type text/css;
110+
srcache_fetch GET /memc $uri;
111+
srcache_store PUT /memc $uri;
112+
srcache_store_private off;
113+
114+
content_by_lua '
115+
ngx.header.cache_control = "private"
116+
ngx.say("hello")
117+
';
118+
}
119+
120+
location /memc {
121+
internal;
122+
123+
set $memc_key $query_string;
124+
set $memc_exptime 300;
125+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
126+
}
127+
--- request
128+
GET /foo
129+
--- response_headers
130+
Content-Type: text/css
131+
Content-Length:
132+
--- response_body
133+
hello
134+
135+
136+
137+
=== TEST 6: basic fetch (cache miss again, not stored in the previous case)
138+
--- config
139+
location /foo {
140+
default_type text/css;
141+
srcache_fetch GET /memc $uri;
142+
srcache_store PUT /memc $uri;
143+
144+
echo world;
145+
}
146+
147+
location /memc {
148+
internal;
149+
150+
set $memc_key $query_string;
151+
set $memc_exptime 300;
152+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
153+
}
154+
--- request
155+
GET /foo
156+
--- response_headers
157+
Content-Type: text/css
158+
Content-Length:
159+
--- response_body
160+
world
161+
162+
163+
164+
=== TEST 7: flush all
165+
--- config
166+
location /flush {
167+
set $memc_cmd 'flush_all';
168+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
169+
}
170+
--- response_headers
171+
Content-Type: text/plain
172+
Content-Length: 4
173+
--- request
174+
GET /flush
175+
--- response_body eval: "OK\r\n"
176+
177+
178+
179+
=== TEST 8: basic fetch (cache miss), and stored due to srcache_store_private on
180+
--- config
181+
location /foo {
182+
default_type text/css;
183+
srcache_fetch GET /memc $uri;
184+
srcache_store PUT /memc $uri;
185+
srcache_store_private on;
186+
187+
content_by_lua '
188+
ngx.header.cache_control = "private"
189+
ngx.say("hello")
190+
';
191+
}
192+
193+
location /memc {
194+
internal;
195+
196+
set $memc_key $query_string;
197+
set $memc_exptime 300;
198+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
199+
}
200+
--- request
201+
GET /foo
202+
--- response_headers
203+
Content-Type: text/css
204+
Content-Length:
205+
--- response_body
206+
hello
207+
208+
209+
210+
=== TEST 9: basic fetch (cache miss again, not stored in the previous case)
211+
--- config
212+
location /foo {
213+
default_type text/css;
214+
srcache_fetch GET /memc $uri;
215+
srcache_store PUT /memc $uri;
216+
217+
echo world;
218+
}
219+
220+
location /memc {
221+
internal;
222+
223+
set $memc_key $query_string;
224+
set $memc_exptime 300;
225+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
226+
}
227+
--- request
228+
GET /foo
229+
--- response_headers
230+
Content-Type: text/css
231+
Content-Length: 6
232+
--- response_body
233+
hello
234+
235+
236+
237+
=== TEST 10: flush all
238+
--- config
239+
location /flush {
240+
set $memc_cmd 'flush_all';
241+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
242+
}
243+
--- response_headers
244+
Content-Type: text/plain
245+
Content-Length: 4
246+
--- request
247+
GET /flush
248+
--- response_body eval: "OK\r\n"
249+
250+
251+
252+
=== TEST 11: basic fetch (cache miss), and not stored due to Cache-Control: private
253+
--- config
254+
location /foo {
255+
default_type text/css;
256+
srcache_fetch GET /memc $uri;
257+
srcache_store PUT /memc $uri;
258+
259+
content_by_lua '
260+
ngx.header.cache_control = { "blah", "blah; Private" }
261+
ngx.say("hello")
262+
';
263+
}
264+
265+
location /memc {
266+
internal;
267+
268+
set $memc_key $query_string;
269+
set $memc_exptime 300;
270+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
271+
}
272+
--- request
273+
GET /foo
274+
--- response_headers
275+
Content-Type: text/css
276+
Content-Length:
277+
--- response_body
278+
hello
279+
280+
281+
282+
=== TEST 12: basic fetch (cache miss again, not stored in the previous case)
283+
--- config
284+
location /foo {
285+
default_type text/css;
286+
srcache_fetch GET /memc $uri;
287+
srcache_store PUT /memc $uri;
288+
289+
echo world;
290+
}
291+
292+
location /memc {
293+
internal;
294+
295+
set $memc_key $query_string;
296+
set $memc_exptime 300;
297+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
298+
}
299+
--- request
300+
GET /foo
301+
--- response_headers
302+
Content-Type: text/css
303+
Content-Length:
304+
--- response_body
305+
world
306+

0 commit comments

Comments
 (0)