Skip to content

Commit f20e81f

Browse files
committed
feature: now srcache_store discards responses that are obviously truncated when the actual output data is shorter than what is declared in its Content-Length response header. thanks Greg Grensteiner.
1 parent bb043bb commit f20e81f

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/ngx_http_srcache_filter_module.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ typedef struct {
1515
ngx_str_t method_name;
1616
ngx_http_complex_value_t location;
1717
ngx_http_complex_value_t args;
18+
1819
} ngx_http_srcache_request_t;
1920

2021

@@ -25,6 +26,7 @@ typedef struct {
2526
ngx_str_t args;
2627
ngx_http_request_body_t *request_body;
2728
ssize_t content_length_n;
29+
2830
} ngx_http_srcache_parsed_request_t;
2931

3032

@@ -67,6 +69,7 @@ typedef struct {
6769
typedef struct {
6870
unsigned postponed_to_access_phase_end;
6971
ngx_hash_t headers_in_hash;
72+
7073
} ngx_http_srcache_main_conf_t;
7174

7275

@@ -80,6 +83,7 @@ struct ngx_http_srcache_ctx_s {
8083
ngx_chain_t *body_from_cache;
8184
ngx_chain_t *body_to_cache;
8285
size_t response_length;
86+
size_t response_body_length;
8387
void *store_wev_handler_ctx;
8488
ngx_http_request_t *fetch_sr;
8589

src/ngx_http_srcache_store.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ ngx_http_srcache_header_filter(ngx_http_request_t *r)
117117
return ngx_http_srcache_next_header_filter(r);
118118
}
119119

120+
/* slcf->store != NULL */
121+
120122
#if 1
121123
if (!(r->method & slcf->cache_methods)) {
122124
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -245,6 +247,7 @@ ngx_http_srcache_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
245247
ngx_chain_t *cl;
246248
ngx_flag_t last;
247249
ngx_http_srcache_loc_conf_t *slcf;
250+
size_t len;
248251

249252
dd_enter();
250253

@@ -374,7 +377,9 @@ ngx_http_srcache_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
374377

375378
for (cl = in; cl; cl = cl->next) {
376379
if (ngx_buf_in_memory(cl->buf)) {
377-
ctx->response_length += ngx_buf_size(cl->buf);
380+
len = ngx_buf_size(cl->buf);
381+
ctx->response_length += len;
382+
ctx->response_body_length += len;
378383
}
379384

380385
if (cl->buf->last_buf) {
@@ -406,13 +411,30 @@ ngx_http_srcache_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
406411
}
407412

408413
if (last && r == r->main) {
414+
415+
#if 1
416+
if (r->headers_out.content_length_n >
417+
(off_t) ctx->response_body_length)
418+
{
419+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
420+
"srcache_store: skipped because response body "
421+
"truncated: %O < %uz",
422+
r->headers_out.content_length_n,
423+
ctx->response_body_length);
424+
425+
ctx->store_response = 0;
426+
goto done;
427+
}
428+
#endif
429+
409430
rc = ngx_http_srcache_store_subrequest(r, ctx);
410431

411432
if (rc != NGX_OK) {
412433
ctx->store_response = 0;
413434
goto done;
414435
}
415436
}
437+
416438
} else {
417439
dd("NO store response");
418440
}

0 commit comments

Comments
 (0)