Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set proxy_request_buffering #35

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/resty/apisix/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ ngx_http_apisix_enable_mirror(ngx_http_request_t *r);
ngx_int_t
ngx_http_apisix_set_real_ip(ngx_http_request_t *r, const u_char *text, size_t len,
unsigned int port);

ngx_int_t
ngx_http_apisix_set_proxy_request_buffering(ngx_http_request_t *r, int on);
]])
local _M = {}

Expand Down Expand Up @@ -65,4 +68,15 @@ function _M.set_real_ip(ip, port)
end


function _M.set_proxy_request_buffering(on)
local r = get_request()
local ret = C.ngx_http_apisix_set_proxy_request_buffering(r, on and 1 or 0)
if ret == NGX_ERROR then
return nil, "error while setting proxy_request_buffering"
end

return true
end


return _M
26 changes: 26 additions & 0 deletions patch/1.19.9/nginx-proxy_request_buffering.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
diff --git src/http/modules/ngx_http_proxy_module.c src/http/modules/ngx_http_proxy_module.c
index 3f05235..41c2313 100644
--- src/http/modules/ngx_http_proxy_module.c
+++ src/http/modules/ngx_http_proxy_module.c
@@ -8,6 +8,9 @@
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
+#if (NGX_HTTP_APISIX)
+#include "ngx_http_apisix_module.h"
+#endif


#define NGX_HTTP_PROXY_COOKIE_SECURE 0x0001
@@ -1013,7 +1016,11 @@ ngx_http_proxy_handler(ngx_http_request_t *r)

u->accel = 1;

+#if (NGX_HTTP_APISIX)
+ if (!ngx_http_apisix_is_request_buffering(r, plcf->upstream.request_buffering)
+#else
if (!plcf->upstream.request_buffering
+#endif
&& plcf->body_values == NULL && plcf->upstream.pass_request_body
&& (!r->headers_in.chunked
|| plcf->http_version == NGX_HTTP_VERSION_11))
33 changes: 33 additions & 0 deletions src/ngx_http_apisix_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,36 @@ ngx_http_apisix_is_mirror_enabled(ngx_http_request_t *r)
ctx = ngx_http_apisix_get_module_ctx(r);
return ctx != NULL && ctx->mirror_enabled;
}


ngx_int_t
ngx_http_apisix_set_proxy_request_buffering(ngx_http_request_t *r, int on)
{
ngx_http_apisix_ctx_t *ctx;

ctx = ngx_http_apisix_get_module_ctx(r);

if (ctx == NULL) {
return NGX_ERROR;
}

ctx->request_buffering = on;
ctx->request_buffering_set = 1;
return NGX_OK;
}


ngx_int_t
ngx_http_apisix_is_request_buffering(ngx_http_request_t *r, ngx_flag_t static_conf)
{
ngx_http_apisix_ctx_t *ctx;

ctx = ngx_http_apisix_get_module_ctx(r);

if (ctx != NULL && ctx->request_buffering_set) {
return ctx->request_buffering;
}

/* use the static conf if we haven't changed it dynamically */
return static_conf;
}
4 changes: 4 additions & 0 deletions src/ngx_http_apisix_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ typedef struct {

unsigned client_max_body_size_set:1;
unsigned mirror_enabled:1;
unsigned request_buffering:1;
unsigned request_buffering_set:1;
} ngx_http_apisix_ctx_t;


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


ngx_int_t ngx_http_apisix_is_request_buffering(ngx_http_request_t *r, ngx_flag_t static_conf);

#endif /* _NGX_HTTP_APISIX_H_INCLUDED_ */
85 changes: 85 additions & 0 deletions t/proxy_request_buffering.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use t::APISIX_NGINX 'no_plan';

run_tests;

__DATA__

=== TEST 1: proxy_request_buffering off
--- config
proxy_request_buffering off;
location /t {
proxy_pass http://127.0.0.1:1984/up;
}
location /up {
return 200;
}
--- request eval
"POST /t
" . "12345" x 10240
--- grep_error_log eval
qr/a client request body is buffered to a temporary file/
--- grep_error_log_out



=== TEST 2: proxy_request_buffering off by Lua API
--- config
proxy_request_buffering on;
location /t {
access_by_lua_block {
local client = require("resty.apisix.client")
assert(client.set_proxy_request_buffering(false))
}
proxy_pass http://127.0.0.1:1984/up;
}
location /up {
return 200;
}
--- request eval
"POST /t
" . "12345" x 10240
--- grep_error_log eval
qr/a client request body is buffered to a temporary file/
--- grep_error_log_out



=== TEST 3: proxy_request_buffering on
--- config
proxy_request_buffering on;
location /t {
proxy_pass http://127.0.0.1:1984/up;
}
location /up {
return 200;
}
--- request eval
"POST /t
" . "12345" x 10240
--- grep_error_log eval
qr/a client request body is buffered to a temporary file/
--- grep_error_log_out
a client request body is buffered to a temporary file



=== TEST 4: proxy_request_buffering on by Lua API
--- config
proxy_request_buffering off;
location /t {
access_by_lua_block {
local client = require("resty.apisix.client")
assert(client.set_proxy_request_buffering(true))
}
proxy_pass http://127.0.0.1:1984/up;
}
location /up {
return 200;
}
--- request eval
"POST /t
" . "12345" x 10240
--- grep_error_log eval
qr/a client request body is buffered to a temporary file/
--- grep_error_log_out
a client request body is buffered to a temporary file