Skip to content

Commit 2a5858b

Browse files
committed
now we always set Content-Length for the cached responses. thanks Andre Pascha.
1 parent dc645d5 commit 2a5858b

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

src/ngx_http_srcache_filter_module.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ ngx_http_srcache_access_handler(ngx_http_request_t *r)
551551
ngx_http_srcache_main_conf_t *smcf;
552552
ngx_http_srcache_ctx_t *ctx;
553553
ngx_chain_t *cl;
554+
size_t len;
554555

555556
if (r != r->main) {
556557
return NGX_DECLINED;
@@ -601,29 +602,44 @@ ngx_http_srcache_access_handler(ngx_http_request_t *r)
601602

602603
dd("sending header");
603604

604-
rc = ngx_http_next_header_filter(r);
605-
606-
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
607-
return rc;
608-
}
609-
610-
dd("sent header from cache: %d", (int) rc);
611-
612605
if (ctx->body_from_cache) {
606+
len = 0;
607+
613608
for (cl = ctx->body_from_cache; cl->next; cl = cl->next) {
614-
/* do nothing */
609+
len += ngx_buf_size(cl->buf);
615610
}
616611

612+
len += ngx_buf_size(cl->buf);
613+
617614
cl->buf->last_buf = 1;
618615

616+
r->headers_out.content_length_n = len;
617+
618+
rc = ngx_http_next_header_filter(r);
619+
620+
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
621+
return rc;
622+
}
623+
619624
rc = ngx_http_next_body_filter(r, ctx->body_from_cache);
620625

621626
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
622627
return rc;
623628
}
624629

625630
dd("sent body from cache: %d", (int) rc);
631+
626632
} else {
633+
r->headers_out.content_length_n = 0;
634+
635+
rc = ngx_http_next_header_filter(r);
636+
637+
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
638+
return rc;
639+
}
640+
641+
dd("sent header from cache: %d", (int) rc);
642+
627643
dd("send last buf for the main request");
628644

629645
cl = ngx_alloc_chain_link(r->pool);

t/err-page.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ GET /flush
5555
GET /foo
5656
--- response_body
5757
err
58+
--- LAST
5859
5960
6061

t/main-req.t

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use Test::Nginx::Socket;
55

66
#repeat_each(2);
77

8-
plan tests => repeat_each() * 3 * blocks();
8+
plan tests => repeat_each() * 4 * blocks();
99

1010
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
1111

@@ -23,6 +23,7 @@ __DATA__
2323
}
2424
--- response_headers
2525
Content-Type: text/plain
26+
Content-Length: 4
2627
--- request
2728
GET /flush
2829
--- response_body eval: "OK\r\n"
@@ -50,6 +51,7 @@ GET /flush
5051
GET /foo
5152
--- response_headers
5253
Content-Type: text/css
54+
Content-Length:
5355
--- response_body
5456
hello
5557
@@ -76,6 +78,7 @@ hello
7678
GET /foo
7779
--- response_headers
7880
Content-Type: text/css
81+
Content-Length: 6
7982
--- response_body
8083
hello
8184
@@ -104,6 +107,7 @@ hello
104107
GET /foo
105108
--- response_headers
106109
Content-Type: text/css
110+
Content-Length: 6
107111
--- response_body
108112
hello
109113

0 commit comments

Comments
 (0)