Skip to content

Commit 9974975

Browse files
committed
feature: allow use of the request body data in nginx variables for main requests by always reading the request body. we used to always discard the request body just like the standard ngx_memcached module. thanks Ristona Hua for sharing this usage.
1 parent 1e14554 commit 9974975

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/ngx_http_redis2_handler.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ ngx_http_redis2_handler(ngx_http_request_t *r)
2828
ngx_str_t target;
2929
ngx_url_t url;
3030

31-
rc = ngx_http_discard_request_body(r);
32-
33-
if (rc != NGX_OK) {
34-
return rc;
35-
}
36-
3731
if (ngx_http_set_content_type(r) != NGX_OK) {
3832
return NGX_HTTP_INTERNAL_SERVER_ERROR;
3933
}
@@ -101,9 +95,11 @@ ngx_http_redis2_handler(ngx_http_request_t *r)
10195
u->input_filter = ngx_http_redis2_filter;
10296
u->input_filter_ctx = ctx;
10397

104-
r->main->count++;
98+
rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
10599

106-
ngx_http_upstream_init(r);
100+
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
101+
return rc;
102+
}
107103

108104
return NGX_DONE;
109105
}

t/sanity.t

Lines changed: 25 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() * 2 * blocks();
8+
plan tests => repeat_each() * (2 * blocks() + 1);
99

1010
$ENV{TEST_NGINX_REDIS_PORT} ||= 6379;
1111

@@ -449,3 +449,27 @@ probe end {
449449
--- response_body eval
450450
"+OK\r\n\$5\r\nworld\r\n"
451451

452+
453+
454+
=== TEST 20: request body
455+
--- config
456+
location /t {
457+
# these two settings must be the same to prevent
458+
# automatic buffering large request bodies
459+
# to temp files:
460+
client_body_buffer_size 8k;
461+
client_max_body_size 8k;
462+
463+
redis2_query flushall;
464+
redis2_query lpush q1 $echo_request_body;
465+
redis2_query lpop q1;
466+
redis2_pass 127.0.0.1:$TEST_NGINX_REDIS_PORT;
467+
}
468+
--- request
469+
POST /t
470+
hello world
471+
--- response_body eval
472+
"+OK\r\n:1\r\n\$11\r\nhello world\r\n"
473+
--- no_error_log
474+
[error]
475+

0 commit comments

Comments
 (0)