Skip to content

Commit 974d5d1

Browse files
authored
feature: add FFI implementation for ngx.arg getter (openresty#2021)
1 parent bd92edf commit 974d5d1

5 files changed

+53
-74
lines changed

src/ngx_http_lua_bodyfilterby.c

+47-31
Original file line numberDiff line numberDiff line change
@@ -396,60 +396,58 @@ ngx_http_lua_body_filter_init(void)
396396

397397

398398
int
399-
ngx_http_lua_body_filter_param_get(lua_State *L, ngx_http_request_t *r)
399+
ngx_http_lua_ffi_get_body_filter_param_eof(ngx_http_request_t *r)
400400
{
401-
u_char *data, *p;
402-
size_t size;
403401
ngx_chain_t *cl;
404-
ngx_buf_t *b;
405-
int idx;
406402
ngx_chain_t *in;
407403

408404
ngx_http_lua_main_conf_t *lmcf;
409405

410-
idx = luaL_checkint(L, 2);
406+
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
407+
in = lmcf->body_filter_chain;
411408

412-
dd("index: %d", idx);
409+
/* asking for the eof argument */
413410

414-
if (idx != 1 && idx != 2) {
415-
lua_pushnil(L);
416-
return 1;
411+
for (cl = in; cl; cl = cl->next) {
412+
if (cl->buf->last_buf || cl->buf->last_in_chain) {
413+
return 1;
414+
}
417415
}
418416

419-
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
420-
in = lmcf->body_filter_chain;
417+
return 0;
418+
}
421419

422-
if (idx == 2) {
423-
/* asking for the eof argument */
424420

425-
for (cl = in; cl; cl = cl->next) {
426-
if (cl->buf->last_buf || cl->buf->last_in_chain) {
427-
lua_pushboolean(L, 1);
428-
return 1;
429-
}
430-
}
421+
int
422+
ngx_http_lua_ffi_get_body_filter_param_body(ngx_http_request_t *r,
423+
u_char **data_p, size_t *len_p)
424+
{
425+
size_t size;
426+
ngx_chain_t *cl;
427+
ngx_buf_t *b;
428+
ngx_chain_t *in;
431429

432-
lua_pushboolean(L, 0);
433-
return 1;
434-
}
430+
ngx_http_lua_main_conf_t *lmcf;
435431

436-
/* idx == 1 */
432+
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
433+
in = lmcf->body_filter_chain;
437434

438435
size = 0;
439436

440437
if (in == NULL) {
441438
/* being a cleared chain on the Lua land */
442-
lua_pushliteral(L, "");
443-
return 1;
439+
*len_p = 0;
440+
return NGX_OK;
444441
}
445442

446443
if (in->next == NULL) {
447444

448445
dd("seen only single buffer");
449446

450447
b = in->buf;
451-
lua_pushlstring(L, (char *) b->pos, b->last - b->pos);
452-
return 1;
448+
*data_p = b->pos;
449+
*len_p = b->last - b->pos;
450+
return NGX_OK;
453451
}
454452

455453
dd("seen multiple buffers");
@@ -464,7 +462,26 @@ ngx_http_lua_body_filter_param_get(lua_State *L, ngx_http_request_t *r)
464462
}
465463
}
466464

467-
data = (u_char *) lua_newuserdata(L, size);
465+
/* the buf is need and is not allocated from Lua land yet, return with
466+
* the actual size */
467+
*len_p = size;
468+
return NGX_AGAIN;
469+
}
470+
471+
472+
int
473+
ngx_http_lua_ffi_copy_body_filter_param_body(ngx_http_request_t *r,
474+
u_char *data)
475+
{
476+
u_char *p;
477+
ngx_chain_t *cl;
478+
ngx_buf_t *b;
479+
ngx_chain_t *in;
480+
481+
ngx_http_lua_main_conf_t *lmcf;
482+
483+
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
484+
in = lmcf->body_filter_chain;
468485

469486
for (p = data, cl = in; cl; cl = cl->next) {
470487
b = cl->buf;
@@ -475,8 +492,7 @@ ngx_http_lua_body_filter_param_get(lua_State *L, ngx_http_request_t *r)
475492
}
476493
}
477494

478-
lua_pushlstring(L, (char *) data, size);
479-
return 1;
495+
return NGX_OK;
480496
}
481497

482498

src/ngx_http_lua_bodyfilterby.h

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ ngx_int_t ngx_http_lua_body_filter_inline(ngx_http_request_t *r,
2121
ngx_chain_t *in);
2222
ngx_int_t ngx_http_lua_body_filter_file(ngx_http_request_t *r,
2323
ngx_chain_t *in);
24-
int ngx_http_lua_body_filter_param_get(lua_State *L, ngx_http_request_t *r);
2524
int ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r,
2625
ngx_http_lua_ctx_t *ctx);
2726

src/ngx_http_lua_setby.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,15 @@ ngx_http_lua_set_by_chunk(lua_State *L, ngx_http_request_t *r, ngx_str_t *val,
123123
}
124124

125125

126-
int
127-
ngx_http_lua_setby_param_get(lua_State *L, ngx_http_request_t *r)
126+
void
127+
ngx_http_lua_ffi_get_setby_param(ngx_http_request_t *r, int idx,
128+
u_char **data_p, size_t *len_p)
128129
{
129-
int idx;
130130
int n;
131131

132132
ngx_http_variable_value_t *v;
133133
ngx_http_lua_main_conf_t *lmcf;
134134

135-
idx = luaL_checkint(L, 2);
136135
idx--;
137136

138137
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
@@ -144,13 +143,12 @@ ngx_http_lua_setby_param_get(lua_State *L, ngx_http_request_t *r)
144143
v = lmcf->setby_args;
145144

146145
if (idx < 0 || idx > n - 1) {
147-
lua_pushnil(L);
146+
*len_p = 0;
148147

149148
} else {
150-
lua_pushlstring(L, (const char *) (v[idx].data), v[idx].len);
149+
*data_p = v[idx].data;
150+
*len_p = v[idx].len;
151151
}
152-
153-
return 1;
154152
}
155153

156154

src/ngx_http_lua_setby.h

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
ngx_int_t ngx_http_lua_set_by_chunk(lua_State *L, ngx_http_request_t *r,
88
ngx_str_t *val, ngx_http_variable_value_t *args, size_t nargs,
99
ngx_str_t *script);
10-
int ngx_http_lua_setby_param_get(lua_State *L, ngx_http_request_t *r);
1110

1211

1312
#endif /* _NGX_HTTP_LUA_SET_BY_H_INCLUDED_ */

src/ngx_http_lua_util.c

-33
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ static int ngx_http_lua_thread_traceback(lua_State *L, lua_State *co,
119119
static void ngx_http_lua_inject_ngx_api(lua_State *L,
120120
ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log);
121121
static void ngx_http_lua_inject_arg_api(lua_State *L);
122-
static int ngx_http_lua_param_get(lua_State *L);
123122
static int ngx_http_lua_param_set(lua_State *L);
124123
static ngx_int_t ngx_http_lua_output_filter(ngx_http_request_t *r,
125124
ngx_chain_t *in);
@@ -3099,9 +3098,6 @@ ngx_http_lua_inject_arg_api(lua_State *L)
30993098

31003099
lua_createtable(L, 0 /* narr */, 2 /* nrec */); /* the metatable */
31013100

3102-
lua_pushcfunction(L, ngx_http_lua_param_get);
3103-
lua_setfield(L, -2, "__index");
3104-
31053101
lua_pushcfunction(L, ngx_http_lua_param_set);
31063102
lua_setfield(L, -2, "__newindex");
31073103

@@ -3113,35 +3109,6 @@ ngx_http_lua_inject_arg_api(lua_State *L)
31133109
}
31143110

31153111

3116-
static int
3117-
ngx_http_lua_param_get(lua_State *L)
3118-
{
3119-
ngx_http_lua_ctx_t *ctx;
3120-
ngx_http_request_t *r;
3121-
3122-
r = ngx_http_lua_get_req(L);
3123-
if (r == NULL) {
3124-
return 0;
3125-
}
3126-
3127-
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
3128-
if (ctx == NULL) {
3129-
return luaL_error(L, "ctx not found");
3130-
}
3131-
3132-
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_SET
3133-
| NGX_HTTP_LUA_CONTEXT_BODY_FILTER);
3134-
3135-
if (ctx->context & (NGX_HTTP_LUA_CONTEXT_SET)) {
3136-
return ngx_http_lua_setby_param_get(L, r);
3137-
}
3138-
3139-
/* ctx->context & (NGX_HTTP_LUA_CONTEXT_BODY_FILTER) */
3140-
3141-
return ngx_http_lua_body_filter_param_get(L, r);
3142-
}
3143-
3144-
31453112
static int
31463113
ngx_http_lua_param_set(lua_State *L)
31473114
{

0 commit comments

Comments
 (0)