1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
/*
* Copyright (C) Xiaozhe Wang (chaoslawful)
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_lua_args.h"
#include "ngx_http_lua_util.h"
static int ngx_http_lua_ngx_req_set_uri_args(lua_State *L);
static int ngx_http_lua_ngx_req_get_post_args(lua_State *L);
uintptr_t
ngx_http_lua_escape_args(u_char *dst, u_char *src, size_t size)
{
ngx_uint_t n;
static u_char hex[] = "0123456789ABCDEF";
/* %00-%20 %7F*/
static uint32_t escape[] = {
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
0x00000001, /* 0000 0000 0000 0000 0000 0000 0000 0001 */
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
};
if (dst == NULL) {
/* find the number of the characters to be escaped */
n = 0;
while (size) {
if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
n++;
}
src++;
size--;
}
return (uintptr_t) n;
}
while (size) {
if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
*dst++ = '%';
*dst++ = hex[*src >> 4];
*dst++ = hex[*src & 0xf];
src++;
} else {
*dst++ = *src++;
}
size--;
}
return (uintptr_t) dst;
}
static int
ngx_http_lua_ngx_req_set_uri_args(lua_State *L)
{
ngx_http_request_t *r;
ngx_str_t args;
const char *msg;
size_t len;
u_char *p;
uintptr_t escape;
if (lua_gettop(L) != 1) {
return luaL_error(L, "expecting 1 argument but seen %d",
lua_gettop(L));
}
r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request object found");
}
ngx_http_lua_check_fake_request(L, r);
switch (lua_type(L, 1)) {
case LUA_TNUMBER:
p = (u_char *) lua_tolstring(L, 1, &len);
args.data = ngx_palloc(r->pool, len);
if (args.data == NULL) {
return luaL_error(L, "no memory");
}
ngx_memcpy(args.data, p, len);
args.len = len;
break;
case LUA_TSTRING:
p = (u_char *) lua_tolstring(L, 1, &len);
escape = ngx_http_lua_escape_args(NULL, p, len);
if (escape > 0) {
args.len = len + 2 * escape;
args.data = ngx_palloc(r->pool, args.len);
if (args.data == NULL) {
return NGX_ERROR;
}
ngx_http_lua_escape_args(args.data, p, len);
} else {
args.data = ngx_palloc(r->pool, len);
if (args.data == NULL) {
return luaL_error(L, "no memory");
}
ngx_memcpy(args.data, p, len);
args.len = len;
}
break;
case LUA_TTABLE:
ngx_http_lua_process_args_option(r, L, 1, &args);
dd("args: %.*s", (int) args.len, args.data);
break;
default:
msg = lua_pushfstring(L, "string, number, or table expected, "
"but got %s", luaL_typename(L, 1));
return luaL_argerror(L, 1, msg);
}
dd("args: %.*s", (int) args.len, args.data);
r->args.data = args.data;
r->args.len = args.len;
r->valid_unparsed_uri = 0;
return 0;
}
static int
ngx_http_lua_ngx_req_get_post_args(lua_State *L)
{
ngx_http_request_t *r;
u_char *buf;
int retval;
size_t len;
ngx_chain_t *cl;
u_char *p;
u_char *last;
int n;
int max;
n = lua_gettop(L);
if (n != 0 && n != 1) {
return luaL_error(L, "expecting 0 or 1 arguments but seen %d", n);
}
if (n == 1) {
max = luaL_checkinteger(L, 1);
lua_pop(L, 1);
} else {
max = NGX_HTTP_LUA_MAX_ARGS;
}
r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request object found");
}
ngx_http_lua_check_fake_request(L, r);
if (r->discard_body) {
lua_createtable(L, 0, 0);
return 1;
}
if (r->request_body == NULL) {
return luaL_error(L, "no request body found; "
"maybe you should turn on lua_need_request_body?");
}
if (r->request_body->temp_file) {
lua_pushnil(L);
lua_pushliteral(L, "request body in temp file not supported");
return 2;
}
if (r->request_body->bufs == NULL) {
lua_createtable(L, 0, 0);
return 1;
}
/* we copy r->request_body->bufs over to buf to simplify
* unescaping query arg keys and values */
len = 0;
for (cl = r->request_body->bufs; cl; cl = cl->next) {
len += cl->buf->last - cl->buf->pos;
}
dd("post body length: %d", (int) len);
if (len == 0) {
lua_createtable(L, 0, 0);
return 1;
}
buf = ngx_palloc(r->pool, len);
if (buf == NULL) {
return luaL_error(L, "no memory");
}
lua_createtable(L, 0, 4);
p = buf;
for (cl = r->request_body->bufs; cl; cl = cl->next) {
p = ngx_copy(p, cl->buf->pos, cl->buf->last - cl->buf->pos);
}
dd("post body: %.*s", (int) len, buf);
last = buf + len;
retval = ngx_http_lua_parse_args(L, buf, last, max);
ngx_pfree(r->pool, buf);
return retval;
}
int
ngx_http_lua_parse_args(lua_State *L, u_char *buf, u_char *last, int max)
{
u_char *p, *q;
u_char *src, *dst;
unsigned parsing_value;
size_t len;
int count = 0;
int top;
top = lua_gettop(L);
p = buf;
parsing_value = 0;
q = p;
while (p != last) {
if (*p == '=' && ! parsing_value) {
/* key data is between p and q */
src = q; dst = q;
ngx_http_lua_unescape_uri(&dst, &src, p - q,
NGX_UNESCAPE_URI_COMPONENT);
dd("pushing key %.*s", (int) (dst - q), q);
/* push the key */
lua_pushlstring(L, (char *) q, dst - q);
/* skip the current '=' char */
p++;
q = p;
parsing_value = 1;
} else if (*p == '&') {
/* reached the end of a key or a value, just save it */
src = q; dst = q;
ngx_http_lua_unescape_uri(&dst, &src, p - q,
NGX_UNESCAPE_URI_COMPONENT);
dd("pushing key or value %.*s", (int) (dst - q), q);
/* push the value or key */
lua_pushlstring(L, (char *) q, dst - q);
/* skip the current '&' char */
p++;
q = p;
if (parsing_value) {
/* end of the current pair's value */
parsing_value = 0;
} else {
/* the current parsing pair takes no value,
* just push the value "true" */
dd("pushing boolean true");
lua_pushboolean(L, 1);
}
(void) lua_tolstring(L, -2, &len);
if (len == 0) {
/* ignore empty string key pairs */
dd("popping key and value...");
lua_pop(L, 2);
} else {
dd("setting table...");
ngx_http_lua_set_multi_value_table(L, top);
}
if (max > 0 && ++count == max) {
lua_pushliteral(L, "truncated");
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"lua hit query args limit %d", max);
return 2;
}
} else {
p++;
}
}
if (p != q || parsing_value) {
src = q; dst = q;
ngx_http_lua_unescape_uri(&dst, &src, p - q,
NGX_UNESCAPE_URI_COMPONENT);
dd("pushing key or value %.*s", (int) (dst - q), q);
lua_pushlstring(L, (char *) q, dst - q);
if (!parsing_value) {
dd("pushing boolean true...");
lua_pushboolean(L, 1);
}
(void) lua_tolstring(L, -2, &len);
if (len == 0) {
/* ignore empty string key pairs */
dd("popping key and value...");
lua_pop(L, 2);
} else {
dd("setting table...");
ngx_http_lua_set_multi_value_table(L, top);
}
}
dd("gettop: %d", lua_gettop(L));
dd("type: %s", lua_typename(L, lua_type(L, 1)));
if (lua_gettop(L) != top) {
return luaL_error(L, "internal error: stack in bad state");
}
return 1;
}
void
ngx_http_lua_inject_req_args_api(lua_State *L)
{
lua_pushcfunction(L, ngx_http_lua_ngx_req_set_uri_args);
lua_setfield(L, -2, "set_uri_args");
lua_pushcfunction(L, ngx_http_lua_ngx_req_get_post_args);
lua_setfield(L, -2, "get_post_args");
}
size_t
ngx_http_lua_ffi_req_get_querystring_len(ngx_http_request_t *r)
{
return r->args.len;
}
int
ngx_http_lua_ffi_req_get_uri_args_count(ngx_http_request_t *r, int max,
int *truncated)
{
int count;
u_char *p, *last;
if (r->connection->fd == (ngx_socket_t) -1) {
return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
}
*truncated = 0;
if (max < 0) {
max = NGX_HTTP_LUA_MAX_ARGS;
}
last = r->args.data + r->args.len;
count = 0;
for (p = r->args.data; p != last; p++) {
if (*p == '&') {
if (count == 0) {
count += 2;
} else {
count++;
}
}
}
if (count) {
if (max > 0 && count > max) {
count = max;
*truncated = 1;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua hit query args limit %d", max);
}
return count;
}
if (r->args.len) {
return 1;
}
return 0;
}
int
ngx_http_lua_ffi_req_get_uri_args(ngx_http_request_t *r, u_char *buf,
ngx_http_lua_ffi_table_elt_t *out, int count)
{
int i, parsing_value = 0;
u_char *last, *p, *q;
u_char *src, *dst;
if (count <= 0) {
return NGX_OK;
}
ngx_memcpy(buf, r->args.data, r->args.len);
i = 0;
last = buf + r->args.len;
p = buf;
q = p;
while (p != last) {
if (*p == '=' && !parsing_value) {
/* key data is between p and q */
src = q; dst = q;
ngx_http_lua_unescape_uri(&dst, &src, p - q,
NGX_UNESCAPE_URI_COMPONENT);
dd("saving key %.*s", (int) (dst - q), q);
out[i].key.data = q;
out[i].key.len = (int) (dst - q);
/* skip the current '=' char */
p++;
q = p;
parsing_value = 1;
} else if (*p == '&') {
/* reached the end of a key or a value, just save it */
src = q; dst = q;
ngx_http_lua_unescape_uri(&dst, &src, p - q,
NGX_UNESCAPE_URI_COMPONENT);
dd("pushing key or value %.*s", (int) (dst - q), q);
if (parsing_value) {
/* end of the current pair's value */
parsing_value = 0;
if (out[i].key.len) {
out[i].value.data = q;
out[i].value.len = (int) (dst - q);
i++;
}
} else {
/* the current parsing pair takes no value,
* just push the value "true" */
dd("pushing boolean true");
if (dst - q) {
out[i].key.data = q;
out[i].key.len = (int) (dst - q);
out[i].value.len = -1;
i++;
}
}
if (i == count) {
return i;
}
/* skip the current '&' char */
p++;
q = p;
} else {
p++;
}
}
if (p != q || parsing_value) {
src = q; dst = q;
ngx_http_lua_unescape_uri(&dst, &src, p - q,
NGX_UNESCAPE_URI_COMPONENT);
dd("pushing key or value %.*s", (int) (dst - q), q);
if (parsing_value) {
if (out[i].key.len) {
out[i].value.data = q;
out[i].value.len = (int) (dst - q);
i++;
}
} else {
if (dst - q) {
out[i].key.data = q;
out[i].key.len = (int) (dst - q);
out[i].value.len = (int) -1;
i++;
}
}
}
return i;
}
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
|