Skip to content

Commit fb94bfe

Browse files
committed
now we also cache the Last-Modified response header by default.
1 parent 62606a0 commit fb94bfe

File tree

6 files changed

+227
-10
lines changed

6 files changed

+227
-10
lines changed

src/ngx_http_srcache_filter_module.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ typedef struct {
5353
ngx_array_t *hide_headers;
5454
ngx_array_t *pass_headers;
5555

56-
unsigned hide_content_type;
5756
time_t max_expire;
5857
time_t default_expire;
5958

60-
unsigned postponed_to_access_phase_end;
59+
unsigned postponed_to_access_phase_end:1;
60+
unsigned hide_content_type:1;
61+
unsigned hide_last_modified:1;
6162
} ngx_http_srcache_loc_conf_t;
6263

6364

src/ngx_http_srcache_util.c

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,8 @@ ngx_http_srcache_store_response_header(ngx_http_request_t *r,
810810
ngx_list_part_t *part;
811811
ngx_table_elt_t *header;
812812

813+
u_char buf[sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1];
814+
813815
ngx_http_srcache_loc_conf_t *conf;
814816

815817
conf = ngx_http_get_module_loc_conf(r, ngx_http_srcache_filter_module);
@@ -857,6 +859,29 @@ ngx_http_srcache_store_response_header(ngx_http_request_t *r,
857859
}
858860
}
859861

862+
if (!conf->hide_last_modified) {
863+
if (r->headers_out.last_modified_time != -1) {
864+
if (r->headers_out.status != NGX_HTTP_OK
865+
&& r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT
866+
&& r->headers_out.status != NGX_HTTP_NOT_MODIFIED
867+
&& r->headers_out.status != NGX_HTTP_NO_CONTENT)
868+
{
869+
r->headers_out.last_modified_time = -1;
870+
r->headers_out.last_modified = NULL;
871+
}
872+
}
873+
874+
dd("last modified time: %d", (int) r->headers_out.last_modified_time);
875+
876+
if (r->headers_out.last_modified == NULL
877+
&& r->headers_out.last_modified_time != -1)
878+
{
879+
(void) ngx_http_time(buf, r->headers_out.last_modified_time);
880+
881+
len += sizeof("Last-Modified: ") - 1 + sizeof(buf) + 2;
882+
}
883+
}
884+
860885
part = &r->headers_out.headers.part;
861886
header = part->elts;
862887

@@ -921,6 +946,18 @@ ngx_http_srcache_store_response_header(ngx_http_request_t *r,
921946
*b->last++ = CR; *b->last++ = LF;
922947
}
923948

949+
if (!conf->hide_last_modified
950+
&& r->headers_out.last_modified == NULL
951+
&& r->headers_out.last_modified_time != -1)
952+
{
953+
b->last = ngx_cpymem(b->last, "Last-Modified: ",
954+
sizeof("Last-Modified: ") - 1);
955+
956+
b->last = ngx_cpymem(b->last, buf, sizeof(buf));
957+
958+
*b->last++ = CR; *b->last++ = LF;
959+
}
960+
924961
part = &r->headers_out.headers.part;
925962
header = part->elts;
926963

@@ -940,10 +977,6 @@ ngx_http_srcache_store_response_header(ngx_http_request_t *r,
940977
continue;
941978
}
942979

943-
dd("header lowcase key: %s", header[i].lowcase_key);
944-
945-
dd("header key: %.*s", (int) header[i].key.len, header[i].key.data);
946-
947980
dd("header hash: %lu, hash lc: %lu", (unsigned long) header[i].hash,
948981
(unsigned long) ngx_hash_key_lc(header[i].key.data,
949982
header[i].key.len));
@@ -1019,6 +1052,7 @@ ngx_http_srcache_hide_headers_hash(ngx_conf_t *cf,
10191052
conf->hide_headers = prev->hide_headers;
10201053
conf->pass_headers = prev->pass_headers;
10211054
conf->hide_content_type = prev->hide_content_type;
1055+
conf->hide_last_modified = prev->hide_last_modified;
10221056

10231057
} else {
10241058
if (conf->hide_headers == NGX_CONF_UNSET_PTR) {
@@ -1073,6 +1107,14 @@ ngx_http_srcache_hide_headers_hash(ngx_conf_t *cf,
10731107
hk->key_hash = ngx_hash_key_lc(h[i].data, h[i].len);
10741108
hk->value = (void *) 1;
10751109

1110+
if (h[i].len == sizeof("Last-Modified") - 1
1111+
&& ngx_strncasecmp(h[i].data, (u_char *) "Last-Modified",
1112+
sizeof("Last-Modified") - 1)
1113+
== 0)
1114+
{
1115+
conf->hide_last_modified = 1;
1116+
}
1117+
10761118
if (h[i].len == sizeof("Content-Type") - 1
10771119
&& ngx_strncasecmp(h[i].data, (u_char *) "Content-Type",
10781120
sizeof("Content-Type") - 1)
@@ -1110,6 +1152,14 @@ ngx_http_srcache_hide_headers_hash(ngx_conf_t *cf,
11101152
conf->hide_content_type = 0;
11111153
}
11121154

1155+
if (h[i].len == sizeof("Last-Modified") - 1
1156+
&& ngx_strncasecmp(h[i].data, (u_char *) "Last-Modified",
1157+
sizeof("Last-Modified") - 1)
1158+
== 0)
1159+
{
1160+
conf->hide_last_modified = 0;
1161+
}
1162+
11131163
if (ngx_strcasecmp(h[i].data, hk[j].key.data) == 0) {
11141164
hk[j].key.data = NULL;
11151165
break;

t/expire-var.t

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,8 @@ hello
533533
6
534534
535535
536-
=== TEST 22: flush all
536+
537+
=== TEST 25: flush all
537538
--- config
538539
location /flush {
539540
set $memc_cmd 'flush_all';
@@ -548,7 +549,7 @@ GET /flush
548549
549550
550551
551-
=== TEST 23: $srcache_expire used too early
552+
=== TEST 26: $srcache_expire used too early
552553
--- config
553554
location /foo {
554555
default_type text/css;

t/static.t

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# vi:filetype=
2+
3+
use lib 'lib';
4+
use Test::Nginx::Socket;
5+
6+
#repeat_each(2);
7+
8+
plan tests => repeat_each() * 5 * 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+
Last-Modified:
29+
--- request
30+
GET /flush
31+
--- response_body eval: "OK\r\n"
32+
33+
34+
35+
=== TEST 2: basic fetch (cache miss)
36+
--- config
37+
location /foo {
38+
default_type text/css;
39+
srcache_fetch GET /memc $uri;
40+
srcache_store PUT /memc $uri;
41+
}
42+
43+
location /memc {
44+
internal;
45+
46+
set $memc_key $query_string;
47+
set $memc_exptime 300;
48+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
49+
}
50+
--- user_files
51+
>>> foo 201103040521.59
52+
hello
53+
--- request
54+
GET /foo
55+
--- response_headers
56+
Content-Type: text/css
57+
Content-Length: 6
58+
Last-Modified: Thu, 03 Mar 2011 21:21:59 GMT
59+
--- response_body
60+
hello
61+
62+
63+
64+
=== TEST 3: basic fetch (cache hit)
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: 6
86+
Last-Modified: Thu, 03 Mar 2011 21:21:59 GMT
87+
--- response_body
88+
hello
89+
90+
91+
92+
=== TEST 4: flush all
93+
--- config
94+
location /flush {
95+
set $memc_cmd 'flush_all';
96+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
97+
}
98+
--- response_headers
99+
Content-Type: text/plain
100+
Content-Length: 4
101+
Last-Modified:
102+
--- request
103+
GET /flush
104+
--- response_body eval: "OK\r\n"
105+
106+
107+
108+
=== TEST 5: basic fetch (cache miss), hide Last-Modified
109+
--- config
110+
location /foo {
111+
default_type text/css;
112+
srcache_fetch GET /memc $uri;
113+
srcache_store PUT /memc $uri;
114+
srcache_store_hide_header Last-Modified;
115+
}
116+
117+
location /memc {
118+
internal;
119+
120+
set $memc_key $query_string;
121+
set $memc_exptime 300;
122+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
123+
}
124+
--- user_files
125+
>>> foo 201103040521.59
126+
hello
127+
--- request
128+
GET /foo
129+
--- response_headers
130+
Content-Type: text/css
131+
Content-Length: 6
132+
Last-Modified: Thu, 03 Mar 2011 21:21:59 GMT
133+
--- response_body
134+
hello
135+
136+
137+
138+
=== TEST 6: basic fetch (cache hit)
139+
--- config
140+
location /foo {
141+
default_type text/css;
142+
srcache_fetch GET /memc $uri;
143+
srcache_store PUT /memc $uri;
144+
145+
echo world;
146+
}
147+
148+
location /memc {
149+
internal;
150+
151+
set $memc_key $query_string;
152+
set $memc_exptime 300;
153+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
154+
}
155+
--- request
156+
GET /foo
157+
--- response_headers
158+
Content-Type: text/css
159+
Content-Length: 6
160+
!Last-Modified
161+
--- response_body
162+
hello
163+

t/store-max-size.t

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ GET /flush
312312
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
313313
}
314314
--- user_files
315-
>>> foo.txt
315+
>>> foo.txt 199801171935.33
316316
hello, world
317317
--- request
318318
GET /foo.txt
@@ -333,6 +333,7 @@ hello, world
333333
--- response_body eval
334334
"HTTP/1.1 200 OK\r
335335
Content-Type: text/css\r
336+
Last-Modified: Sat, 17 Jan 1998 11:35:33 GMT\r
336337
Accept-Ranges: bytes\r
337338
\r
338339
hello, world

t/store-skip.t

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ GET /flush
311311
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
312312
}
313313
--- user_files
314-
>>> foo.txt
314+
>>> foo.txt 201012240310.03
315315
hello, world
316316
--- request
317317
GET /foo.txt
@@ -332,6 +332,7 @@ hello, world
332332
--- response_body eval
333333
"HTTP/1.1 200 OK\r
334334
Content-Type: text/css\r
335+
Last-Modified: Thu, 23 Dec 2010 19:10:03 GMT\r
335336
Accept-Ranges: bytes\r
336337
\r
337338
hello, world

0 commit comments

Comments
 (0)