-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_conf_debug_module.c
407 lines (313 loc) · 11.7 KB
/
ngx_http_conf_debug_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
#include <ngx_http.h>
#include <ngx_config.h>
/*
* version: 1.0.0
*/
#define NGX_HTTP_NOT_ACCEPTABLE 406
extern ngx_module_t ngx_http_proxy_module;
typedef struct {
ngx_str_t key_start;
ngx_str_t schema;
ngx_str_t host_header;
ngx_str_t port;
ngx_str_t uri;
} ngx_http_conf_debug_proxy_vars_t;
/* should be same as ngx_http_proxy_vars_t in ngx_http_proxy_module.c */
typedef struct {
ngx_array_t *flushes;
ngx_array_t *lengths;
ngx_array_t *values;
ngx_hash_t hash;
} ngx_http_conf_debug_proxy_headers_t;
/* should be same as ngx_http_proxy_headers_t in ngx_http_proxy_module.c */
typedef struct {
ngx_http_upstream_conf_t upstream;
ngx_array_t *body_flushes;
ngx_array_t *body_lengths;
ngx_array_t *body_values;
ngx_str_t body_source;
ngx_http_conf_debug_proxy_headers_t headers;
#if (NGX_HTTP_CACHE)
ngx_http_conf_debug_proxy_headers_t headers_cache;
#endif
ngx_array_t *headers_source;
ngx_array_t *proxy_lengths;
ngx_array_t *proxy_values;
ngx_array_t *redirects;
ngx_array_t *cookie_domains;
ngx_array_t *cookie_paths;
ngx_array_t *cookie_flags;
ngx_http_complex_value_t *method;
ngx_str_t location;
ngx_str_t url;
#if (NGX_HTTP_CACHE)
ngx_http_complex_value_t cache_key;
#endif
ngx_http_conf_debug_proxy_vars_t vars;
ngx_flag_t redirect;
ngx_uint_t http_version;
ngx_uint_t headers_hash_max_size;
ngx_uint_t headers_hash_bucket_size;
#if (NGX_HTTP_SSL)
ngx_uint_t ssl;
ngx_uint_t ssl_protocols;
ngx_str_t ssl_ciphers;
ngx_uint_t ssl_verify_depth;
ngx_str_t ssl_trusted_certificate;
ngx_str_t ssl_crl;
ngx_array_t *ssl_conf_commands;
#endif
} ngx_http_conf_debug_proxy_loc_conf_t;
/* should be same as ngx_http_proxy_loc_conf_t in ngx_http_proxy_module.c */
typedef struct {
ngx_flag_t enabled;
ngx_flag_t interrupt;
ngx_uint_t status_code;
} ngx_http_conf_debug_loc_conf_t;
static void* ngx_http_conf_debug_create_loc_conf(ngx_conf_t *cf);
static char* ngx_http_conf_debug_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_int_t ngx_http_conf_debug_preconfiguration(ngx_conf_t *cf);
static ngx_int_t ngx_http_conf_debug_postconfiguration(ngx_conf_t *cf);
static ngx_int_t ngx_http_conf_debug_location_mode(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_conf_debug_location_name(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_conf_debug_location_upstream(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_conf_debug_handler(ngx_http_request_t *r);
static ngx_str_t ngx_http_conf_debug_location_mode_exact = ngx_string("=");
static ngx_str_t ngx_http_conf_debug_location_mode_prefix_prior = ngx_string("^~");
static ngx_str_t ngx_http_conf_debug_location_mode_regular_case_insensitive = ngx_string("~*");
static ngx_str_t ngx_http_conf_debug_location_mode_regular_case_sensitive = ngx_string("~");
static ngx_str_t ngx_http_conf_debug_location_mode_prefix_normal = ngx_string("blank");
static ngx_http_variable_t ngx_http_conf_debug_variables[] = {
{ ngx_string("conf_debug_location_mode"), NULL,
ngx_http_conf_debug_location_mode,
0, 0, 0 },
{ ngx_string("conf_debug_location_name"), NULL,
ngx_http_conf_debug_location_name,
0, 0, 0 },
{ ngx_string("conf_debug_location_upstream"), NULL,
ngx_http_conf_debug_location_upstream,
0, 0, 0 },
{ ngx_null_string, NULL, NULL, 0, 0, 0}
};
static ngx_command_t ngx_http_conf_debug_commands[] = {
{ ngx_string("conf_debug_enable"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_conf_debug_loc_conf_t, enabled),
NULL },
{ ngx_string("conf_debug_interrupt"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_conf_debug_loc_conf_t, interrupt),
NULL },
{ ngx_string("conf_debug_interrupt_status_code"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_conf_debug_loc_conf_t, status_code),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_conf_debug_module_ctx = {
ngx_http_conf_debug_preconfiguration, /* preconfiguration */
ngx_http_conf_debug_postconfiguration, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_conf_debug_create_loc_conf, /* create location configuration */
ngx_http_conf_debug_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_conf_debug_module = {
NGX_MODULE_V1,
&ngx_http_conf_debug_module_ctx, /* module context */
ngx_http_conf_debug_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_conf_debug_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_conf_debug_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_debug_loc_conf_t));
if (conf == NULL) {
return NULL;
}
conf->enabled = NGX_CONF_UNSET;
conf->interrupt = NGX_CONF_UNSET;
conf->status_code = NGX_CONF_UNSET_UINT;
return conf;
}
static char*
ngx_http_conf_debug_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_conf_debug_loc_conf_t *prev = parent;
ngx_http_conf_debug_loc_conf_t *conf = child;
ngx_conf_merge_value(conf->enabled, prev->enabled, 0);
ngx_conf_merge_value(conf->interrupt, prev->interrupt, 0);
ngx_conf_merge_uint_value(conf->status_code, prev->status_code, NGX_HTTP_NOT_ACCEPTABLE);
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_conf_debug_preconfiguration(ngx_conf_t *cf)
{
ngx_http_variable_t *var, *v;
for (v = ngx_http_conf_debug_variables; v->name.len; v++) {
var = ngx_http_add_variable(cf, &v->name, v->flags);
if (var == NULL) {
return NGX_ERROR;
}
var->get_handler = v->get_handler;
var->data = v->data;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_conf_debug_postconfiguration(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_conf_debug_handler;
return NGX_OK;
}
static ngx_int_t
ngx_http_conf_debug_location_mode(ngx_http_request_t *r, ngx_http_variable_value_t *v,
uintptr_t data)
{
int n, options;
ngx_http_core_loc_conf_t *clcf;
ngx_http_conf_debug_loc_conf_t *cdlcf;
options = 0;
cdlcf = ngx_http_get_module_loc_conf(r, ngx_http_conf_debug_module);
if (!cdlcf->enabled) {
v->not_found = 1;
return NGX_OK;
}
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
v->valid = 0;
if (clcf->exact_match) {
v->valid = 1;
v->len = ngx_http_conf_debug_location_mode_exact.len;
v->data = ngx_http_conf_debug_location_mode_exact.data;
goto matched;
}
if (clcf->noregex) {
v->valid = 1;
v->len = ngx_http_conf_debug_location_mode_prefix_prior.len;
v->data = ngx_http_conf_debug_location_mode_prefix_prior.data;
goto matched;
}
if (
!clcf->exact_match
&& !clcf->noregex
#if (NGX_PCRE)
&& clcf->regex == NULL
#endif
) {
v->valid = 1;
v->len = ngx_http_conf_debug_location_mode_prefix_normal.len;
v->data = ngx_http_conf_debug_location_mode_prefix_normal.data;
goto matched;
}
#if (NGX_PCRE)
if (
clcf->regex == NULL
|| clcf->regex->regex == NULL
|| clcf->regex->regex->code == NULL
) {
return NGX_ERROR;
}
n = pcre_fullinfo(clcf->regex->regex->code, NULL, PCRE_INFO_OPTIONS, &options);
if (n < 0) {
return NGX_ERROR;
}
if (options & PCRE_CASELESS) {
v->valid = 1;
v->len = ngx_http_conf_debug_location_mode_regular_case_insensitive.len;
v->data = ngx_http_conf_debug_location_mode_regular_case_insensitive.data;
goto matched;
} else {
v->valid = 1;
v->len = ngx_http_conf_debug_location_mode_regular_case_sensitive.len;
v->data = ngx_http_conf_debug_location_mode_regular_case_sensitive.data;
goto matched;
}
#endif
if (!v->valid) {
return NGX_ERROR;
}
matched:
v->no_cacheable = 0;
v->not_found = 0;
return NGX_OK;
}
static ngx_int_t
ngx_http_conf_debug_location_name(ngx_http_request_t *r, ngx_http_variable_value_t *v,
uintptr_t data)
{
ngx_http_core_loc_conf_t *clcf;
ngx_http_conf_debug_loc_conf_t *cdlcf;
cdlcf = ngx_http_get_module_loc_conf(r, ngx_http_conf_debug_module);
if (!cdlcf->enabled) {
v->not_found = 1;
return NGX_OK;
}
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
v->len = clcf->name.len;
v->data = clcf->name.data;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
return NGX_OK;
}
static ngx_int_t
ngx_http_conf_debug_location_upstream(ngx_http_request_t *r, ngx_http_variable_value_t *v,
uintptr_t data)
{
ngx_http_conf_debug_loc_conf_t *cdlcf;
ngx_http_conf_debug_proxy_loc_conf_t *cdplcf;
cdlcf = ngx_http_get_module_loc_conf(r, ngx_http_conf_debug_module);
if (!cdlcf->enabled) {
v->not_found = 1;
return NGX_OK;
}
cdplcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
if (cdplcf->vars.host_header.len == 0
|| cdplcf->vars.host_header.data == NULL) {
v->not_found = 1;
return NGX_OK;
}
v->len = cdplcf->vars.host_header.len;
v->data = cdplcf->vars.host_header.data;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
return NGX_OK;
}
static ngx_int_t
ngx_http_conf_debug_handler(ngx_http_request_t *r)
{
ngx_http_conf_debug_loc_conf_t *cdlcf;
cdlcf = ngx_http_get_module_loc_conf(r, ngx_http_conf_debug_module);
if (cdlcf->enabled && cdlcf->interrupt) {
return cdlcf->status_code;
}
return NGX_DECLINED;
}