-
Notifications
You must be signed in to change notification settings - Fork 255
/
main.c
378 lines (313 loc) · 11.2 KB
/
main.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
#define DDEBUG 0
#include "ddebug.h"
#include "handler.h"
#include "filter.h"
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_log.h>
/* config init handler */
static void* ngx_http_echo_create_conf(ngx_conf_t *cf);
/* config directive handlers */
static char* ngx_http_echo_echo(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char* ngx_http_echo_echo_client_request_headers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char* ngx_http_echo_echo_sleep(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char* ngx_http_echo_echo_flush(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char* ngx_http_echo_echo_blocking_sleep(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char* ngx_http_echo_echo_reset_timer(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char* ngx_http_echo_echo_before_body(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char* ngx_http_echo_echo_after_body(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char* ngx_http_echo_echo_location_async(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char* ngx_http_echo_echo_location(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
/*
* TODO
static char* ngx_http_echo_echo_location(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
*/
static char* ngx_http_echo_echo_duplicate(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char* ngx_http_echo_helper(ngx_http_echo_opcode_t opcode,
ngx_http_echo_cmd_category_t cat,
ngx_conf_t *cf, ngx_command_t *cmd, void* conf);
static ngx_http_module_t ngx_http_echo_module_ctx = {
/* TODO we could add our own variables here... */
ngx_http_echo_handler_init, /* preconfiguration */
ngx_http_echo_filter_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_echo_create_conf, /* create location configuration */
NULL /* merge location configuration */
};
static ngx_command_t ngx_http_echo_commands[] = {
{ ngx_string("echo"),
NGX_HTTP_LOC_CONF|NGX_CONF_ANY,
ngx_http_echo_echo,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_echo_loc_conf_t, handler_cmds),
NULL },
/* TODO echo_client_request_headers should take an
* optional argument to change output format to
* "html" or other things */
{ ngx_string("echo_client_request_headers"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_echo_echo_client_request_headers,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_echo_loc_conf_t, handler_cmds),
NULL },
{ ngx_string("echo_sleep"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_echo_echo_sleep,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_echo_loc_conf_t, handler_cmds),
NULL },
{ ngx_string("echo_flush"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_echo_echo_flush,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_echo_loc_conf_t, handler_cmds),
NULL },
{ ngx_string("echo_blocking_sleep"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_echo_echo_blocking_sleep,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_echo_loc_conf_t, handler_cmds),
NULL },
{ ngx_string("echo_reset_timer"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_echo_echo_reset_timer,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_echo_loc_conf_t, handler_cmds),
NULL },
{ ngx_string("echo_before_body"),
NGX_HTTP_LOC_CONF|NGX_CONF_ANY,
ngx_http_echo_echo_before_body,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_echo_loc_conf_t, before_body_cmds),
NULL },
{ ngx_string("echo_after_body"),
NGX_HTTP_LOC_CONF|NGX_CONF_ANY,
ngx_http_echo_echo_after_body,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_echo_loc_conf_t, after_body_cmds),
NULL },
{ ngx_string("echo_location_async"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
ngx_http_echo_echo_location_async,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("echo_location"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
ngx_http_echo_echo_location,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("echo_duplicate"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
ngx_http_echo_echo_duplicate,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
ngx_module_t ngx_http_echo_module = {
NGX_MODULE_V1,
&ngx_http_echo_module_ctx, /* module context */
ngx_http_echo_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 process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static void*
ngx_http_echo_create_conf(ngx_conf_t *cf) {
ngx_http_echo_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_echo_loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
return conf;
}
static char*
ngx_http_echo_helper(ngx_http_echo_opcode_t opcode,
ngx_http_echo_cmd_category_t cat,
ngx_conf_t *cf, ngx_command_t *cmd, void* conf) {
ngx_http_core_loc_conf_t *clcf;
/* ngx_http_echo_loc_conf_t *ulcf = conf; */
ngx_array_t **args_ptr;
ngx_http_script_compile_t sc;
ngx_str_t *raw_args;
ngx_http_echo_arg_template_t *arg;
ngx_array_t **cmds_ptr;
ngx_http_echo_cmd_t *echo_cmd;
ngx_uint_t i, n;
/* cmds_ptr points to ngx_http_echo_loc_conf_t's
* handler_cmds, before_body_cmds, or after_body_cmds
* array, depending on the actual offset */
cmds_ptr = (ngx_array_t**)(((u_char*)conf) + cmd->offset);
if (*cmds_ptr == NULL) {
*cmds_ptr = ngx_array_create(cf->pool, 1,
sizeof(ngx_http_echo_cmd_t));
if (*cmds_ptr == NULL) {
return NGX_CONF_ERROR;
}
if (cat == echo_handler_cmd) {
DD("registering the content handler");
/* register the content handler */
clcf = ngx_http_conf_get_module_loc_conf(cf,
ngx_http_core_module);
if (clcf == NULL) {
return NGX_CONF_ERROR;
}
DD("registering the content handler (2)");
clcf->handler = ngx_http_echo_handler;
} else {
DD("filter used = 1");
ngx_http_echo_filter_used = 1;
}
}
echo_cmd = ngx_array_push(*cmds_ptr);
if (echo_cmd == NULL) {
return NGX_CONF_ERROR;
}
echo_cmd->opcode = opcode;
args_ptr = &echo_cmd->args;
*args_ptr = ngx_array_create(cf->pool, 1,
sizeof(ngx_http_echo_arg_template_t));
if (*args_ptr == NULL) {
return NGX_CONF_ERROR;
}
raw_args = cf->args->elts;
/* we skip the first arg and start from the second */
for (i = 1 ; i < cf->args->nelts; i++) {
arg = ngx_array_push(*args_ptr);
if (arg == NULL) {
return NGX_CONF_ERROR;
}
arg->raw_value = raw_args[i];
DD("found raw arg %s", raw_args[i].data);
arg->lengths = NULL;
arg->values = NULL;
n = ngx_http_script_variables_count(&arg->raw_value);
if (n > 0) {
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
sc.cf = cf;
sc.source = &arg->raw_value;
sc.lengths = &arg->lengths;
sc.values = &arg->values;
sc.variables = n;
sc.complete_lengths = 1;
sc.complete_values = 1;
if (ngx_http_script_compile(&sc) != NGX_OK) {
return NGX_CONF_ERROR;
}
}
} /* end for */
return NGX_CONF_OK;
}
static char*
ngx_http_echo_echo(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
DD("in echo_echo...");
return ngx_http_echo_helper(echo_opcode_echo,
echo_handler_cmd,
cf, cmd, conf);
}
static char*
ngx_http_echo_echo_client_request_headers(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf) {
return ngx_http_echo_helper(
echo_opcode_echo_client_request_headers,
echo_handler_cmd,
cf, cmd, conf);
}
static char*
ngx_http_echo_echo_sleep(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
DD("in echo_sleep...");
return ngx_http_echo_helper(echo_opcode_echo_sleep,
echo_handler_cmd,
cf, cmd, conf);
}
static char*
ngx_http_echo_echo_flush(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
DD("in echo_flush...");
return ngx_http_echo_helper(echo_opcode_echo_flush,
echo_handler_cmd,
cf, cmd, conf);
}
static char*
ngx_http_echo_echo_blocking_sleep(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
DD("in echo_blocking_sleep...");
return ngx_http_echo_helper(echo_opcode_echo_blocking_sleep,
echo_handler_cmd,
cf, cmd, conf);
}
static char*
ngx_http_echo_echo_reset_timer(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf) {
return ngx_http_echo_helper(echo_opcode_echo_reset_timer,
echo_handler_cmd,
cf, cmd, conf);
}
static char*
ngx_http_echo_echo_before_body(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf) {
DD("processing echo_before_body directive...");
return ngx_http_echo_helper(echo_opcode_echo_before_body,
echo_filter_cmd,
cf, cmd, conf);
}
static char*
ngx_http_echo_echo_after_body(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf) {
return ngx_http_echo_helper(echo_opcode_echo_after_body,
echo_filter_cmd,
cf, cmd, conf);
}
static char*
ngx_http_echo_echo_location_async(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf) {
#if ! defined(nginx_version)
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"Directive echo_location_async does not work with nginx "
"versions ealier than 0.7.46.");
return NGX_CONF_ERROR;
#else
return ngx_http_echo_helper(echo_opcode_echo_location_async,
echo_handler_cmd,
cf, cmd, conf);
#endif
}
static char*
ngx_http_echo_echo_location(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf) {
#if ! defined(nginx_version)
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"Directive echo_location does not work with nginx "
"versions ealier than 0.7.46.");
return NGX_CONF_ERROR;
#else
return ngx_http_echo_helper(echo_opcode_echo_location,
echo_handler_cmd,
cf, cmd, conf);
#endif
}
static char*
ngx_http_echo_echo_duplicate(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf) {
return ngx_http_echo_helper(echo_opcode_echo_duplicate,
echo_handler_cmd,
cf, cmd, conf);
}