Skip to content

Commit 802cc05

Browse files
authored
feat: allow skip pass trailers in nginx grpc upstream (#100)
1 parent 3787343 commit 802cc05

13 files changed

+1753
-0
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jobs:
1212
matrix:
1313
op_version:
1414
- "1.25.3.1"
15+
- "1.27.1.1"
1516

1617
runs-on: "ubuntu-20.04"
1718

@@ -31,6 +32,7 @@ jobs:
3132
sudo apt install -y cpanminus build-essential libncurses5-dev libreadline-dev libssl-dev perl luarocks libpcre3 libpcre3-dev zlib1g-dev
3233
sudo luarocks install lua-resty-http > build.log 2>&1 || (cat build.log && exit 1)
3334
sudo luarocks install lua-resty-openssl > build.log 2>&1 || (cat build.log && exit 1)
35+
cd t/assets/grpc && ./setup.sh > build.log 2>&1 || (cat build.log && exit 1) && cd ../../..
3436
3537
- name: Before install
3638
run: |

lib/resty/apisix/upstream.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local get_phase = ngx.get_phase
55
local C = ffi.C
66
local NGX_ERROR = ngx.ERROR
77
local NGX_OK = ngx.OK
8+
local type = type
89

910

1011
base.allows_subsystem("http")
@@ -15,6 +16,8 @@ typedef intptr_t ngx_int_t;
1516
ngx_int_t ngx_http_apisix_upstream_set_cert_and_key(ngx_http_request_t *r, void *cert, void *key);
1617
ngx_int_t ngx_http_apisix_upstream_set_ssl_trusted_store(ngx_http_request_t *r, void *store);
1718
int ngx_http_apisix_upstream_set_ssl_verify(ngx_http_request_t *r, int verify);
19+
20+
ngx_int_t ngx_http_apisix_set_upstream_pass_trailers(ngx_http_request_t *r, int on);
1821
]])
1922
local _M = {}
2023

@@ -106,4 +109,19 @@ end
106109
_M.set_ssl_verify = set_ssl_verify
107110

108111

112+
function _M.set_pass_trailers(on)
113+
if type(on) ~= 'boolean' then
114+
return nil, "on expects a boolean but found " .. type(on)
115+
end
116+
117+
local r = get_request()
118+
local ret = C.ngx_http_apisix_set_upstream_pass_trailers(r, on and 1 or 0)
119+
if ret == NGX_ERROR then
120+
return nil, "error while setting upstream pass_trailers"
121+
end
122+
123+
return true
124+
end
125+
126+
109127
return _M
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
diff --git src/http/ngx_http_upstream.c src/http/ngx_http_upstream.c
2+
index d04d91e..2dd1102 100644
3+
--- src/http/ngx_http_upstream.c
4+
+++ src/http/ngx_http_upstream.c
5+
@@ -2989,6 +2989,12 @@ ngx_http_upstream_process_trailers(ngx_http_request_t *r,
6+
return NGX_OK;
7+
}
8+
9+
+#if (NGX_HTTP_APISIX)
10+
+ if (!ngx_http_apisix_is_upstream_pass_trailers(r)) {
11+
+ return NGX_OK;
12+
+ }
13+
+#endif
14+
+
15+
part = &u->headers_in.trailers.part;
16+
h = part->elts;
17+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
diff --git src/http/ngx_http_upstream.c src/http/ngx_http_upstream.c
2+
index d04d91e..2dd1102 100644
3+
--- src/http/ngx_http_upstream.c
4+
+++ src/http/ngx_http_upstream.c
5+
@@ -2989,6 +2989,12 @@ ngx_http_upstream_process_trailers(ngx_http_request_t *r,
6+
return NGX_OK;
7+
}
8+
9+
+#if (NGX_HTTP_APISIX)
10+
+ if (!ngx_http_apisix_is_upstream_pass_trailers(r)) {
11+
+ return NGX_OK;
12+
+ }
13+
+#endif
14+
+
15+
part = &u->headers_in.trailers.part;
16+
h = part->elts;
17+

src/ngx_http_apisix_module.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,3 +1048,35 @@ ngx_http_apisix_error_log_request_id(ngx_conf_t *cf, ngx_command_t *cmd, void *c
10481048
return NGX_CONF_OK;
10491049
}
10501050

1051+
1052+
ngx_int_t
1053+
ngx_http_apisix_set_upstream_pass_trailers(ngx_http_request_t *r, int on)
1054+
{
1055+
ngx_http_apisix_ctx_t *ctx;
1056+
1057+
ctx = ngx_http_apisix_get_module_ctx(r);
1058+
1059+
if (ctx == NULL) {
1060+
return NGX_ERROR;
1061+
}
1062+
1063+
ctx->upstream_pass_trailers = on;
1064+
ctx->upstream_pass_trailers_set = 1;
1065+
return NGX_OK;
1066+
}
1067+
1068+
1069+
ngx_int_t
1070+
ngx_http_apisix_is_upstream_pass_trailers(ngx_http_request_t *r)
1071+
{
1072+
ngx_http_apisix_ctx_t *ctx;
1073+
1074+
ctx = ngx_http_apisix_get_module_ctx(r);
1075+
1076+
if (ctx != NULL && ctx->upstream_pass_trailers_set) {
1077+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "apisix upstream pass trailers set: %d", ctx->upstream_pass_trailers);
1078+
return ctx->upstream_pass_trailers;
1079+
}
1080+
1081+
return 1;
1082+
}

src/ngx_http_apisix_module.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ typedef struct {
3737
unsigned body_filter_by_lua_skipped:1;
3838
unsigned upstream_ssl_verify:1;
3939
unsigned upstream_ssl_verify_set:1;
40+
unsigned upstream_pass_trailers:1;
41+
unsigned upstream_pass_trailers_set:1;
4042
} ngx_http_apisix_ctx_t;
4143

4244

@@ -66,4 +68,7 @@ ngx_flag_t ngx_http_apisix_is_ntls_enabled(ngx_http_conf_ctx_t *conf_ctx);
6668
char * ngx_http_apisix_error_log_request_id(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
6769
char * ngx_http_apisix_error_log_init(ngx_conf_t *cf);
6870
char * ngx_http_apisix_error_log_request_id(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
71+
72+
ngx_int_t ngx_http_apisix_set_upstream_pass_trailers(ngx_http_request_t *r, int on);
73+
ngx_int_t ngx_http_apisix_is_upstream_pass_trailers(ngx_http_request_t *r);
6974
#endif /* _NGX_HTTP_APISIX_H_INCLUDED_ */

0 commit comments

Comments
 (0)