Skip to content

Commit 577dfeb

Browse files
authored
feature: add max_bytes argument for get_body_data() function.
1 parent 339b08a commit 577dfeb

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

README.markdown

+3-1
Original file line numberDiff line numberDiff line change
@@ -5467,12 +5467,14 @@ See also [ngx.req.read_body](#ngxreqread_body).
54675467
ngx.req.get_body_data
54685468
---------------------
54695469

5470-
**syntax:** *data = ngx.req.get_body_data()*
5470+
**syntax:** *data = ngx.req.get_body_data(max_bytes?)*
54715471

54725472
**context:** *rewrite_by_lua*, access_by_lua*, content_by_lua*, log_by_lua**
54735473

54745474
Retrieves in-memory request body data. It returns a Lua string rather than a Lua table holding all the parsed query arguments. Use the [ngx.req.get_post_args](#ngxreqget_post_args) function instead if a Lua table is required.
54755475

5476+
The optional `max_bytes` argument can be used when you don't need the entire body.
5477+
54765478
This function returns `nil` if
54775479

54785480
1. the request body has not been read,

doc/HttpLuaModule.wiki

+3-1
Original file line numberDiff line numberDiff line change
@@ -4587,12 +4587,14 @@ See also [[#ngx.req.read_body|ngx.req.read_body]].
45874587
45884588
== ngx.req.get_body_data ==
45894589
4590-
'''syntax:''' ''data = ngx.req.get_body_data()''
4590+
'''syntax:''' ''data = ngx.req.get_body_data(max_bytes?)''
45914591
45924592
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*, log_by_lua*''
45934593
45944594
Retrieves in-memory request body data. It returns a Lua string rather than a Lua table holding all the parsed query arguments. Use the [[#ngx.req.get_post_args|ngx.req.get_post_args]] function instead if a Lua table is required.
45954595
4596+
The optional <code>max_bytes</code> function argument can be used when you don't need the entire body.
4597+
45964598
This function returns <code>nil</code> if
45974599
45984600
# the request body has not been read,

src/ngx_http_lua_req_body.c

+26-6
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,21 @@ ngx_http_lua_ngx_req_get_body_data(lua_State *L)
246246
{
247247
ngx_http_request_t *r;
248248
int n;
249-
size_t len;
249+
size_t len, max;
250+
size_t size, rest;
250251
ngx_chain_t *cl;
251252
u_char *p;
252253
u_char *buf;
253254

254255
n = lua_gettop(L);
255256

256-
if (n != 0) {
257-
return luaL_error(L, "expecting 0 arguments but seen %d", n);
257+
if (n != 0 && n != 1) {
258+
return luaL_error(L, "expecting 0 or 1 arguments but seen %d", n);
259+
}
260+
261+
max = 0;
262+
if (n == 1) {
263+
max = (size_t) luaL_checknumber(L, 1);
258264
}
259265

260266
r = ngx_http_lua_get_req(L);
@@ -282,6 +288,7 @@ ngx_http_lua_ngx_req_get_body_data(lua_State *L)
282288
return 1;
283289
}
284290

291+
len = (max > 0 && len > max) ? max : len;
285292
lua_pushlstring(L, (char *) cl->buf->pos, len);
286293
return 1;
287294
}
@@ -292,7 +299,13 @@ ngx_http_lua_ngx_req_get_body_data(lua_State *L)
292299

293300
for (; cl; cl = cl->next) {
294301
dd("body chunk len: %d", (int) ngx_buf_size(cl->buf));
295-
len += cl->buf->last - cl->buf->pos;
302+
size = cl->buf->last - cl->buf->pos;
303+
if (max > 0 && (len + size > max)) {
304+
len = max;
305+
break;
306+
}
307+
308+
len += size;
296309
}
297310

298311
if (len == 0) {
@@ -303,8 +316,15 @@ ngx_http_lua_ngx_req_get_body_data(lua_State *L)
303316
buf = (u_char *) lua_newuserdata(L, len);
304317

305318
p = buf;
306-
for (cl = r->request_body->bufs; cl; cl = cl->next) {
307-
p = ngx_copy(p, cl->buf->pos, cl->buf->last - cl->buf->pos);
319+
rest = len;
320+
for (cl = r->request_body->bufs; cl != NULL && rest > 0; cl = cl->next) {
321+
size = ngx_buf_size(cl->buf);
322+
if (size > rest) { /* reach limit*/
323+
size = rest;
324+
}
325+
326+
p = ngx_copy(p, cl->buf->pos, size);
327+
rest -= size;
308328
}
309329

310330
lua_pushlstring(L, (char *) buf, len);

t/010-request_body.t

+17
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,20 @@ Expect: 100-Continue
270270
http finalize request: 500, "/echo_body?" a:1, c:2
271271
http finalize request: 500, "/echo_body?" a:1, c:0
272272
--- log_level: debug
273+
274+
275+
276+
=== TEST 13: test reading the first n bytes of request body
277+
--- config
278+
location /echo_body {
279+
lua_need_request_body on;
280+
content_by_lua_block {
281+
local data = ngx.req.get_body_data(1)
282+
ngx.say(data)
283+
}
284+
}
285+
--- request
286+
POST /echo_body
287+
hello
288+
--- response_body
289+
h

0 commit comments

Comments
 (0)