-
Notifications
You must be signed in to change notification settings - Fork 34
/
ngx_http_form_input_module.c
523 lines (385 loc) · 13.5 KB
/
ngx_http_form_input_module.c
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
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include <ndk.h>
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#define form_urlencoded_type "application/x-www-form-urlencoded"
#define form_urlencoded_type_len (sizeof(form_urlencoded_type) - 1)
typedef struct {
unsigned used; /* :1 */
} ngx_http_form_input_main_conf_t;
typedef struct {
unsigned done:1;
unsigned waiting_more_body:1;
} ngx_http_form_input_ctx_t;
static ngx_int_t ngx_http_set_form_input(ngx_http_request_t *r, ngx_str_t *res,
ngx_http_variable_value_t *v);
static char *ngx_http_set_form_input_conf_handler(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static void *ngx_http_form_input_create_main_conf(ngx_conf_t *cf);
static ngx_int_t ngx_http_form_input_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_form_input_handler(ngx_http_request_t *r);
static void ngx_http_form_input_post_read(ngx_http_request_t *r);
static ngx_int_t ngx_http_form_input_arg(ngx_http_request_t *r, u_char *name,
size_t len, ngx_str_t *value, ngx_flag_t multi);
static ngx_command_t ngx_http_form_input_commands[] = {
{ ngx_string("set_form_input"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
ngx_http_set_form_input_conf_handler,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("set_form_input_multi"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
ngx_http_set_form_input_conf_handler,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_form_input_module_ctx = {
NULL, /* preconfiguration */
ngx_http_form_input_init, /* postconfiguration */
ngx_http_form_input_create_main_conf, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_form_input_module = {
NGX_MODULE_V1,
&ngx_http_form_input_module_ctx, /* module context */
ngx_http_form_input_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit precess */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_int_t
ngx_http_set_form_input(ngx_http_request_t *r, ngx_str_t *res,
ngx_http_variable_value_t *v)
{
ngx_http_form_input_ctx_t *ctx;
ngx_int_t rc;
dd_enter();
dd("set default return value");
ngx_str_set(res, "");
if (r->done) {
dd("request done");
return NGX_OK;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_form_input_module);
if (ctx == NULL) {
dd("ndk handler:null ctx");
return NGX_OK;
}
if (!ctx->done) {
dd("ctx not done");
return NGX_OK;
}
rc = ngx_http_form_input_arg(r, v->data, v->len, res, 0);
return rc;
}
static ngx_int_t
ngx_http_set_form_input_multi(ngx_http_request_t *r, ngx_str_t *res,
ngx_http_variable_value_t *v)
{
ngx_http_form_input_ctx_t *ctx;
ngx_int_t rc;
dd_enter();
dd("set default return value");
ngx_str_set(res, "");
/* dd("set default return value"); */
if (r->done) {
return NGX_OK;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_form_input_module);
if (ctx == NULL) {
dd("ndk handler:null ctx");
return NGX_OK;
}
if (!ctx->done) {
dd("ctx not done");
return NGX_OK;
}
rc = ngx_http_form_input_arg(r, v->data, v->len, res, 1);
return rc;
}
/* fork from ngx_http_arg.
* read argument(s) with name arg_name and length arg_len into value variable,
* if multi flag is set, multi arguments with name arg_name will be read and
* stored in an ngx_array_t struct, this can be operated by directives in
* array-var-nginx-module */
static ngx_int_t
ngx_http_form_input_arg(ngx_http_request_t *r, u_char *arg_name, size_t arg_len,
ngx_str_t *value, ngx_flag_t multi)
{
u_char *p, *v, *last, *buf;
ngx_chain_t *cl;
size_t len = 0;
ngx_array_t *array = NULL;
ngx_str_t *s;
ngx_buf_t *b;
if (multi) {
array = ngx_array_create(r->pool, 1, sizeof(ngx_str_t));
if (array == NULL) {
return NGX_ERROR;
}
value->data = (u_char *)array;
value->len = sizeof(ngx_array_t);
} else {
ngx_str_set(value, "");
}
/* we read data from r->request_body->bufs */
if (r->request_body == NULL || r->request_body->bufs == NULL) {
dd("empty rb or empty rb bufs");
return NGX_OK;
}
if (r->request_body->bufs->next != NULL) {
/* more than one buffer...we should copy the data out... */
len = 0;
for (cl = r->request_body->bufs; cl; cl = cl->next) {
b = cl->buf;
if (b->in_file) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"form-input: in-file buffer found. aborted. "
"consider increasing your "
"client_body_buffer_size setting");
return NGX_OK;
}
len += b->last - b->pos;
}
dd("len=%d", (int) len);
if (len == 0) {
return NGX_OK;
}
buf = ngx_palloc(r->pool, len);
if (buf == NULL) {
return NGX_ERROR;
}
p = buf;
last = p + len;
for (cl = r->request_body->bufs; cl; cl = cl->next) {
p = ngx_copy(p, cl->buf->pos, cl->buf->last - cl->buf->pos);
}
dd("p - buf = %d, last - buf = %d", (int) (p - buf),
(int) (last - buf));
dd("copied buf (len %d): %.*s", (int) len, (int) len,
buf);
} else {
dd("XXX one buffer only");
b = r->request_body->bufs->buf;
if (ngx_buf_size(b) == 0) {
return NGX_OK;
}
buf = b->pos;
last = b->last;
}
for (p = buf; p < last; p++) {
/* we need '=' after name, so drop one char from last */
p = ngx_strlcasestrn(p, last - 1, arg_name, arg_len - 1);
if (p == NULL) {
return NGX_OK;
}
dd("found argument name, offset: %d", (int) (p - buf));
if ((p == buf || *(p - 1) == '&') && *(p + arg_len) == '=') {
v = p + arg_len + 1;
dd("v = %d...", (int) (v - buf));
dd("buf now (len %d): %.*s",
(int) (last - v), (int) (last - v), v);
p = ngx_strlchr(v, last, '&');
if (p == NULL) {
dd("& not found, pointing it to last...");
p = last;
} else {
dd("found &, pointing it to %d...", (int) (p - buf));
}
if (multi) {
s = ngx_array_push(array);
if (s == NULL) {
return NGX_ERROR;
}
s->data = v;
s->len = p - v;
dd("array var:%.*s", (int) s->len, s->data);
} else {
value->data = v;
value->len = p - v;
dd("value: [%.*s]", (int) value->len, value->data);
return NGX_OK;
}
}
}
#if 0
if (multi) {
value->data = (u_char *) array;
value->len = sizeof(ngx_array_t);
}
#endif
return NGX_OK;
}
static char *
ngx_http_set_form_input_conf_handler(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
ndk_set_var_t filter;
ngx_str_t *value, s;
u_char *p;
ngx_http_form_input_main_conf_t *fmcf;
#if defined(nginx_version) && nginx_version >= 8042 && nginx_version <= 8053
return "does not work with " NGINX_VER;
#endif
fmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_form_input_module);
fmcf->used = 1;
filter.type = NDK_SET_VAR_MULTI_VALUE;
filter.size = 1;
value = cf->args->elts;
if ((value->len == sizeof("set_form_input_multi") - 1) &&
ngx_strncmp(value->data, "set_form_input_multi", value->len) == 0)
{
dd("use ngx_http_form_input_multi");
filter.func = (void *) ngx_http_set_form_input_multi;
} else {
filter.func = (void *) ngx_http_set_form_input;
}
value++;
if (cf->args->nelts == 2) {
p = value->data;
p++;
s.len = value->len - 1;
s.data = p;
} else if (cf->args->nelts == 3) {
s.len = (value + 1)->len;
s.data = (value + 1)->data;
}
return ndk_set_var_multi_value_core (cf, value, &s, &filter);
}
/* register a new rewrite phase handler */
static ngx_int_t
ngx_http_form_input_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
ngx_http_form_input_main_conf_t *fmcf;
fmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_form_input_module);
if (!fmcf->used) {
return NGX_OK;
}
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_form_input_handler;
return NGX_OK;
}
/* an rewrite phase handler */
static ngx_int_t
ngx_http_form_input_handler(ngx_http_request_t *r)
{
ngx_http_form_input_ctx_t *ctx;
ngx_str_t value;
ngx_int_t rc;
dd_enter();
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http form_input rewrite phase handler");
ctx = ngx_http_get_module_ctx(r, ngx_http_form_input_module);
if (ctx != NULL) {
if (ctx->done) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http form_input rewrite phase handler done");
return NGX_DECLINED;
}
return NGX_DONE;
}
if (r->method != NGX_HTTP_POST && r->method != NGX_HTTP_PUT) {
return NGX_DECLINED;
}
if (r->headers_in.content_type == NULL
|| r->headers_in.content_type->value.data == NULL)
{
dd("content_type is %p", r->headers_in.content_type);
return NGX_DECLINED;
}
value = r->headers_in.content_type->value;
dd("r->headers_in.content_length_n:%d",
(int) r->headers_in.content_length_n);
/* just focus on x-www-form-urlencoded */
if (value.len < form_urlencoded_type_len
|| ngx_strncasecmp(value.data, (u_char *) form_urlencoded_type,
form_urlencoded_type_len) != 0)
{
dd("not application/x-www-form-urlencoded");
return NGX_DECLINED;
}
dd("content type is application/x-www-form-urlencoded");
dd("create new ctx");
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_form_input_ctx_t));
if (ctx == NULL) {
return NGX_ERROR;
}
/* set by ngx_pcalloc:
* ctx->done = 0;
* ctx->waiting_more_body = 0;
*/
ngx_http_set_ctx(r, ctx, ngx_http_form_input_module);
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http form_input start to read client request body");
rc = ngx_http_read_client_request_body(r, ngx_http_form_input_post_read);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
#if (nginx_version < 1002006) || \
(nginx_version >= 1003000 && nginx_version < 1003009)
r->main->count--;
#endif
return rc;
}
if (rc == NGX_AGAIN) {
ctx->waiting_more_body = 1;
return NGX_DONE;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http form_input has read the request body in one run");
return NGX_DECLINED;
}
static void
ngx_http_form_input_post_read(ngx_http_request_t *r)
{
ngx_http_form_input_ctx_t *ctx;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http form_input post read request body");
ctx = ngx_http_get_module_ctx(r, ngx_http_form_input_module);
ctx->done = 1;
#if defined(nginx_version) && nginx_version >= 8011
dd("count--");
r->main->count--;
#endif
dd("waiting more body: %d", (int) ctx->waiting_more_body);
/* waiting_more_body my rewrite phase handler */
if (ctx->waiting_more_body) {
ctx->waiting_more_body = 0;
ngx_http_core_run_phases(r);
}
}
static void *
ngx_http_form_input_create_main_conf(ngx_conf_t *cf)
{
ngx_http_form_input_main_conf_t *fmcf;
fmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_form_input_main_conf_t));
if (fmcf == NULL) {
return NULL;
}
/* set by ngx_pcalloc:
* fmcf->used = 0;
*/
return fmcf;
}