Skip to content

Commit a243a96

Browse files
committed
bugfix: we should always read the client request body before initiating the fetch subrequest at the "access phase".
1 parent 5627f01 commit a243a96

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/ngx_http_srcache_fetch.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "ngx_http_srcache_fetch.h"
1414
#include "ngx_http_srcache_store.h"
1515
#include "ngx_http_srcache_util.h"
16+
#include <nginx.h>
1617

1718

1819
static ngx_int_t ngx_http_srcache_fetch_subrequest(ngx_http_request_t *r,
@@ -24,6 +25,8 @@ static ngx_int_t ngx_http_srcache_test_not_modified(ngx_http_request_t *r);
2425

2526
static ngx_int_t ngx_http_srcache_test_precondition(ngx_http_request_t *r);
2627

28+
static void ngx_http_srcache_post_read_body(ngx_http_request_t *r);
29+
2730

2831
ngx_int_t
2932
ngx_http_srcache_access_handler(ngx_http_request_t *r)
@@ -124,6 +127,15 @@ ngx_http_srcache_access_handler(ngx_http_request_t *r)
124127
return NGX_AGAIN;
125128
}
126129

130+
if (ctx->waiting_request_body) {
131+
return NGX_AGAIN;
132+
}
133+
134+
if (ctx->request_body_done == 1) {
135+
ctx->request_body_done = 0;
136+
goto do_fetch_subrequest;
137+
}
138+
127139
if (ctx->request_done) {
128140
dd("request done");
129141

@@ -242,6 +254,28 @@ ngx_http_srcache_access_handler(ngx_http_request_t *r)
242254

243255
dd("running phase handler...");
244256

257+
if (!r->request_body) {
258+
dd("reading request body: ctx = %p", ctx);
259+
260+
rc = ngx_http_read_client_request_body(r,
261+
ngx_http_srcache_post_read_body);
262+
if (rc == NGX_ERROR || rc > NGX_OK) {
263+
#if (nginx_version < 1002006) \
264+
|| (nginx_version >= 1003000 && nginx_version < 1003009)
265+
r->main->count--;
266+
#endif
267+
return rc;
268+
}
269+
270+
if (rc == NGX_AGAIN) {
271+
ctx->waiting_request_body = 1;
272+
return NGX_AGAIN;
273+
}
274+
275+
/* rc == NGX_OK */
276+
}
277+
278+
do_fetch_subrequest:
245279
/* issue a subrequest to fetch cached stuff (if any) */
246280

247281
rc = ngx_http_srcache_fetch_subrequest(r, conf, ctx);
@@ -497,4 +531,33 @@ ngx_http_srcache_test_precondition(ngx_http_request_t *r)
497531
NGX_HTTP_PRECONDITION_FAILED);
498532
}
499533

534+
535+
static void
536+
ngx_http_srcache_post_read_body(ngx_http_request_t *r)
537+
{
538+
ngx_http_srcache_ctx_t *ctx;
539+
540+
ctx = ngx_http_get_module_ctx(r, ngx_http_srcache_filter_module);
541+
542+
dd("post read: ctx=%p", ctx);
543+
544+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
545+
"srcache post read for the access phase: wait:%ud c:%ud",
546+
(unsigned) ctx->waiting_request_body, r->main->count);
547+
548+
r->write_event_handler = ngx_http_core_run_phases;
549+
550+
#if defined(nginx_version) && nginx_version >= 8011
551+
r->main->count--;
552+
#endif
553+
554+
dd("c:%u", r->main->count);
555+
556+
if (ctx->waiting_request_body) {
557+
ctx->request_body_done = 1;
558+
ctx->waiting_request_body = 0;
559+
ngx_http_core_run_phases(r);
560+
}
561+
}
562+
500563
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

src/ngx_http_srcache_filter_module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ struct ngx_http_srcache_ctx_s {
131131
unsigned store_skip:1;
132132
unsigned issued_fetch_subrequest:1;
133133
unsigned seen_subreq_eof:1;
134+
unsigned waiting_request_body:1;
135+
unsigned request_body_done:1;
134136
};
135137

136138

0 commit comments

Comments
 (0)