Skip to content

Commit 310ae32

Browse files
authored
feat: set proxy_request_buffering (#35)
1 parent 874393a commit 310ae32

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

lib/resty/apisix/client.lua

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ ngx_http_apisix_enable_mirror(ngx_http_request_t *r);
2323
ngx_int_t
2424
ngx_http_apisix_set_real_ip(ngx_http_request_t *r, const u_char *text, size_t len,
2525
unsigned int port);
26+
27+
ngx_int_t
28+
ngx_http_apisix_set_proxy_request_buffering(ngx_http_request_t *r, int on);
2629
]])
2730
local _M = {}
2831

@@ -65,4 +68,15 @@ function _M.set_real_ip(ip, port)
6568
end
6669

6770

71+
function _M.set_proxy_request_buffering(on)
72+
local r = get_request()
73+
local ret = C.ngx_http_apisix_set_proxy_request_buffering(r, on and 1 or 0)
74+
if ret == NGX_ERROR then
75+
return nil, "error while setting proxy_request_buffering"
76+
end
77+
78+
return true
79+
end
80+
81+
6882
return _M
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
diff --git src/http/modules/ngx_http_proxy_module.c src/http/modules/ngx_http_proxy_module.c
2+
index 3f05235..41c2313 100644
3+
--- src/http/modules/ngx_http_proxy_module.c
4+
+++ src/http/modules/ngx_http_proxy_module.c
5+
@@ -8,6 +8,9 @@
6+
#include <ngx_config.h>
7+
#include <ngx_core.h>
8+
#include <ngx_http.h>
9+
+#if (NGX_HTTP_APISIX)
10+
+#include "ngx_http_apisix_module.h"
11+
+#endif
12+
13+
14+
#define NGX_HTTP_PROXY_COOKIE_SECURE 0x0001
15+
@@ -1013,7 +1016,11 @@ ngx_http_proxy_handler(ngx_http_request_t *r)
16+
17+
u->accel = 1;
18+
19+
+#if (NGX_HTTP_APISIX)
20+
+ if (!ngx_http_apisix_is_request_buffering(r, plcf->upstream.request_buffering)
21+
+#else
22+
if (!plcf->upstream.request_buffering
23+
+#endif
24+
&& plcf->body_values == NULL && plcf->upstream.pass_request_body
25+
&& (!r->headers_in.chunked
26+
|| plcf->http_version == NGX_HTTP_VERSION_11))

src/ngx_http_apisix_module.c

+33
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,36 @@ ngx_http_apisix_is_mirror_enabled(ngx_http_request_t *r)
498498
ctx = ngx_http_apisix_get_module_ctx(r);
499499
return ctx != NULL && ctx->mirror_enabled;
500500
}
501+
502+
503+
ngx_int_t
504+
ngx_http_apisix_set_proxy_request_buffering(ngx_http_request_t *r, int on)
505+
{
506+
ngx_http_apisix_ctx_t *ctx;
507+
508+
ctx = ngx_http_apisix_get_module_ctx(r);
509+
510+
if (ctx == NULL) {
511+
return NGX_ERROR;
512+
}
513+
514+
ctx->request_buffering = on;
515+
ctx->request_buffering_set = 1;
516+
return NGX_OK;
517+
}
518+
519+
520+
ngx_int_t
521+
ngx_http_apisix_is_request_buffering(ngx_http_request_t *r, ngx_flag_t static_conf)
522+
{
523+
ngx_http_apisix_ctx_t *ctx;
524+
525+
ctx = ngx_http_apisix_get_module_ctx(r);
526+
527+
if (ctx != NULL && ctx->request_buffering_set) {
528+
return ctx->request_buffering;
529+
}
530+
531+
/* use the static conf if we haven't changed it dynamically */
532+
return static_conf;
533+
}

src/ngx_http_apisix_module.h

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ typedef struct {
2727

2828
unsigned client_max_body_size_set:1;
2929
unsigned mirror_enabled:1;
30+
unsigned request_buffering:1;
31+
unsigned request_buffering_set:1;
3032
} ngx_http_apisix_ctx_t;
3133

3234

@@ -43,4 +45,6 @@ ngx_int_t ngx_http_apisix_get_gzip_compress_level(ngx_http_request_t *r);
4345
ngx_int_t ngx_http_apisix_is_mirror_enabled(ngx_http_request_t *r);
4446

4547

48+
ngx_int_t ngx_http_apisix_is_request_buffering(ngx_http_request_t *r, ngx_flag_t static_conf);
49+
4650
#endif /* _NGX_HTTP_APISIX_H_INCLUDED_ */

t/proxy_request_buffering.t

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use t::APISIX_NGINX 'no_plan';
2+
3+
run_tests;
4+
5+
__DATA__
6+
7+
=== TEST 1: proxy_request_buffering off
8+
--- config
9+
proxy_request_buffering off;
10+
location /t {
11+
proxy_pass http://127.0.0.1:1984/up;
12+
}
13+
location /up {
14+
return 200;
15+
}
16+
--- request eval
17+
"POST /t
18+
" . "12345" x 10240
19+
--- grep_error_log eval
20+
qr/a client request body is buffered to a temporary file/
21+
--- grep_error_log_out
22+
23+
24+
25+
=== TEST 2: proxy_request_buffering off by Lua API
26+
--- config
27+
proxy_request_buffering on;
28+
location /t {
29+
access_by_lua_block {
30+
local client = require("resty.apisix.client")
31+
assert(client.set_proxy_request_buffering(false))
32+
}
33+
proxy_pass http://127.0.0.1:1984/up;
34+
}
35+
location /up {
36+
return 200;
37+
}
38+
--- request eval
39+
"POST /t
40+
" . "12345" x 10240
41+
--- grep_error_log eval
42+
qr/a client request body is buffered to a temporary file/
43+
--- grep_error_log_out
44+
45+
46+
47+
=== TEST 3: proxy_request_buffering on
48+
--- config
49+
proxy_request_buffering on;
50+
location /t {
51+
proxy_pass http://127.0.0.1:1984/up;
52+
}
53+
location /up {
54+
return 200;
55+
}
56+
--- request eval
57+
"POST /t
58+
" . "12345" x 10240
59+
--- grep_error_log eval
60+
qr/a client request body is buffered to a temporary file/
61+
--- grep_error_log_out
62+
a client request body is buffered to a temporary file
63+
64+
65+
66+
=== TEST 4: proxy_request_buffering on by Lua API
67+
--- config
68+
proxy_request_buffering off;
69+
location /t {
70+
access_by_lua_block {
71+
local client = require("resty.apisix.client")
72+
assert(client.set_proxy_request_buffering(true))
73+
}
74+
proxy_pass http://127.0.0.1:1984/up;
75+
}
76+
location /up {
77+
return 200;
78+
}
79+
--- request eval
80+
"POST /t
81+
" . "12345" x 10240
82+
--- grep_error_log eval
83+
qr/a client request body is buffered to a temporary file/
84+
--- grep_error_log_out
85+
a client request body is buffered to a temporary file

0 commit comments

Comments
 (0)