0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
|
13 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
4
|
14 static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
|
142
|
15 ngx_http_conf_in_port_t *in_port, ngx_http_listen_t *lscf,
|
48
|
16 ngx_http_core_srv_conf_t *cscf);
|
4
|
17 static ngx_int_t ngx_http_add_names(ngx_conf_t *cf,
|
142
|
18 ngx_http_conf_in_addr_t *in_addr, ngx_http_core_srv_conf_t *cscf);
|
0
|
19 static char *ngx_http_merge_locations(ngx_conf_t *cf,
|
48
|
20 ngx_array_t *locations, void **loc_conf, ngx_http_module_t *module,
|
|
21 ngx_uint_t ctx_index);
|
302
|
22 static int ngx_http_cmp_conf_in_addrs(const void *one, const void *two);
|
142
|
23 static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one,
|
|
24 const void *two);
|
0
|
25
|
142
|
26 ngx_uint_t ngx_http_max_module;
|
|
27
|
|
28 ngx_uint_t ngx_http_total_requests;
|
|
29 uint64_t ngx_http_total_sent;
|
0
|
30
|
|
31
|
|
32 ngx_int_t (*ngx_http_top_header_filter) (ngx_http_request_t *r);
|
|
33 ngx_int_t (*ngx_http_top_body_filter) (ngx_http_request_t *r, ngx_chain_t *ch);
|
|
34
|
|
35
|
|
36 static ngx_command_t ngx_http_commands[] = {
|
|
37
|
48
|
38 { ngx_string("http"),
|
|
39 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
|
|
40 ngx_http_block,
|
|
41 0,
|
|
42 0,
|
|
43 NULL },
|
0
|
44
|
48
|
45 ngx_null_command
|
0
|
46 };
|
|
47
|
126
|
48
|
0
|
49 static ngx_core_module_t ngx_http_module_ctx = {
|
|
50 ngx_string("http"),
|
|
51 NULL,
|
|
52 NULL
|
126
|
53 };
|
0
|
54
|
|
55
|
|
56 ngx_module_t ngx_http_module = {
|
58
|
57 NGX_MODULE_V1,
|
0
|
58 &ngx_http_module_ctx, /* module context */
|
|
59 ngx_http_commands, /* module directives */
|
|
60 NGX_CORE_MODULE, /* module type */
|
90
|
61 NULL, /* init master */
|
0
|
62 NULL, /* init module */
|
90
|
63 NULL, /* init process */
|
|
64 NULL, /* init thread */
|
|
65 NULL, /* exit thread */
|
|
66 NULL, /* exit process */
|
|
67 NULL, /* exit master */
|
|
68 NGX_MODULE_V1_PADDING
|
0
|
69 };
|
|
70
|
|
71
|
48
|
72 static char *
|
|
73 ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
74 {
|
|
75 char *rv;
|
142
|
76 u_char ch;
|
230
|
77 ngx_int_t rc, j;
|
|
78 ngx_uint_t mi, m, s, l, p, a, i, n;
|
|
79 ngx_uint_t find_config_index, use_rewrite, use_access;
|
142
|
80 ngx_uint_t last, bind_all, done;
|
0
|
81 ngx_conf_t pcf;
|
198
|
82 ngx_array_t headers_in, in_ports;
|
|
83 ngx_hash_key_t *hk;
|
142
|
84 ngx_hash_init_t hash;
|
0
|
85 ngx_listening_t *ls;
|
|
86 ngx_http_listen_t *lscf;
|
|
87 ngx_http_module_t *module;
|
198
|
88 ngx_http_header_t *header;
|
142
|
89 ngx_http_in_port_t *hip;
|
0
|
90 ngx_http_handler_pt *h;
|
|
91 ngx_http_conf_ctx_t *ctx;
|
142
|
92 ngx_http_conf_in_port_t *in_port;
|
|
93 ngx_http_conf_in_addr_t *in_addr;
|
|
94 ngx_hash_keys_arrays_t ha;
|
|
95 ngx_http_server_name_t *name;
|
230
|
96 ngx_http_phase_handler_t *ph;
|
142
|
97 ngx_http_virtual_names_t *vn;
|
0
|
98 ngx_http_core_srv_conf_t **cscfp, *cscf;
|
|
99 ngx_http_core_loc_conf_t *clcf;
|
230
|
100 ngx_http_phase_handler_pt checker;
|
0
|
101 ngx_http_core_main_conf_t *cmcf;
|
10
|
102 #if (NGX_WIN32)
|
0
|
103 ngx_iocp_conf_t *iocpcf;
|
|
104 #endif
|
|
105
|
|
106 /* the main http context */
|
26
|
107
|
50
|
108 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
|
|
109 if (ctx == NULL) {
|
34
|
110 return NGX_CONF_ERROR;
|
|
111 }
|
0
|
112
|
|
113 *(ngx_http_conf_ctx_t **) conf = ctx;
|
|
114
|
34
|
115
|
0
|
116 /* count the number of the http modules and set up their indices */
|
|
117
|
|
118 ngx_http_max_module = 0;
|
|
119 for (m = 0; ngx_modules[m]; m++) {
|
|
120 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
121 continue;
|
|
122 }
|
|
123
|
|
124 ngx_modules[m]->ctx_index = ngx_http_max_module++;
|
|
125 }
|
|
126
|
26
|
127
|
34
|
128 /* the http main_conf context, it is the same in the all http contexts */
|
0
|
129
|
34
|
130 ctx->main_conf = ngx_pcalloc(cf->pool,
|
|
131 sizeof(void *) * ngx_http_max_module);
|
|
132 if (ctx->main_conf == NULL) {
|
|
133 return NGX_CONF_ERROR;
|
|
134 }
|
0
|
135
|
|
136
|
34
|
137 /*
|
|
138 * the http null srv_conf context, it is used to merge
|
|
139 * the server{}s' srv_conf's
|
|
140 */
|
|
141
|
|
142 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
143 if (ctx->srv_conf == NULL) {
|
|
144 return NGX_CONF_ERROR;
|
|
145 }
|
|
146
|
|
147
|
|
148 /*
|
|
149 * the http null loc_conf context, it is used to merge
|
|
150 * the server{}s' loc_conf's
|
|
151 */
|
|
152
|
|
153 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
154 if (ctx->loc_conf == NULL) {
|
|
155 return NGX_CONF_ERROR;
|
|
156 }
|
|
157
|
|
158
|
|
159 /*
|
|
160 * create the main_conf's, the null srv_conf's, and the null loc_conf's
|
|
161 * of the all http modules
|
|
162 */
|
0
|
163
|
|
164 for (m = 0; ngx_modules[m]; m++) {
|
|
165 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
166 continue;
|
|
167 }
|
|
168
|
|
169 module = ngx_modules[m]->ctx;
|
|
170 mi = ngx_modules[m]->ctx_index;
|
|
171
|
|
172 if (module->create_main_conf) {
|
50
|
173 ctx->main_conf[mi] = module->create_main_conf(cf);
|
|
174 if (ctx->main_conf[mi] == NULL) {
|
34
|
175 return NGX_CONF_ERROR;
|
|
176 }
|
0
|
177 }
|
|
178
|
|
179 if (module->create_srv_conf) {
|
50
|
180 ctx->srv_conf[mi] = module->create_srv_conf(cf);
|
|
181 if (ctx->srv_conf[mi] == NULL) {
|
34
|
182 return NGX_CONF_ERROR;
|
|
183 }
|
0
|
184 }
|
|
185
|
|
186 if (module->create_loc_conf) {
|
50
|
187 ctx->loc_conf[mi] = module->create_loc_conf(cf);
|
|
188 if (ctx->loc_conf[mi] == NULL) {
|
34
|
189 return NGX_CONF_ERROR;
|
|
190 }
|
0
|
191 }
|
|
192 }
|
|
193
|
58
|
194 pcf = *cf;
|
|
195 cf->ctx = ctx;
|
|
196
|
|
197 for (m = 0; ngx_modules[m]; m++) {
|
|
198 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
199 continue;
|
|
200 }
|
|
201
|
|
202 module = ngx_modules[m]->ctx;
|
|
203 mi = ngx_modules[m]->ctx_index;
|
|
204
|
|
205 if (module->preconfiguration) {
|
|
206 if (module->preconfiguration(cf) != NGX_OK) {
|
|
207 return NGX_CONF_ERROR;
|
|
208 }
|
|
209 }
|
|
210 }
|
34
|
211
|
0
|
212 /* parse inside the http{} block */
|
|
213
|
|
214 cf->module_type = NGX_HTTP_MODULE;
|
|
215 cf->cmd_type = NGX_HTTP_MAIN_CONF;
|
|
216 rv = ngx_conf_parse(cf, NULL);
|
|
217
|
|
218 if (rv != NGX_CONF_OK) {
|
|
219 *cf = pcf;
|
|
220 return rv;
|
|
221 }
|
|
222
|
|
223 /*
|
|
224 * init http{} main_conf's, merge the server{}s' srv_conf's
|
|
225 * and its location{}s' loc_conf's
|
|
226 */
|
|
227
|
|
228 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
|
|
229 cscfp = cmcf->servers.elts;
|
|
230
|
|
231 for (m = 0; ngx_modules[m]; m++) {
|
|
232 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
233 continue;
|
|
234 }
|
|
235
|
|
236 module = ngx_modules[m]->ctx;
|
|
237 mi = ngx_modules[m]->ctx_index;
|
|
238
|
|
239 /* init http{} main_conf's */
|
|
240
|
|
241 if (module->init_main_conf) {
|
|
242 rv = module->init_main_conf(cf, ctx->main_conf[mi]);
|
|
243 if (rv != NGX_CONF_OK) {
|
|
244 *cf = pcf;
|
|
245 return rv;
|
|
246 }
|
|
247 }
|
|
248
|
|
249 for (s = 0; s < cmcf->servers.nelts; s++) {
|
|
250
|
|
251 /* merge the server{}s' srv_conf's */
|
|
252
|
|
253 if (module->merge_srv_conf) {
|
|
254 rv = module->merge_srv_conf(cf,
|
|
255 ctx->srv_conf[mi],
|
|
256 cscfp[s]->ctx->srv_conf[mi]);
|
|
257 if (rv != NGX_CONF_OK) {
|
|
258 *cf = pcf;
|
|
259 return rv;
|
|
260 }
|
|
261 }
|
|
262
|
|
263 if (module->merge_loc_conf) {
|
|
264
|
|
265 /* merge the server{}'s loc_conf */
|
|
266
|
|
267 rv = module->merge_loc_conf(cf,
|
|
268 ctx->loc_conf[mi],
|
|
269 cscfp[s]->ctx->loc_conf[mi]);
|
|
270 if (rv != NGX_CONF_OK) {
|
|
271 *cf = pcf;
|
|
272 return rv;
|
|
273 }
|
|
274
|
|
275 /* merge the locations{}' loc_conf's */
|
|
276
|
|
277 rv = ngx_http_merge_locations(cf, &cscfp[s]->locations,
|
|
278 cscfp[s]->ctx->loc_conf,
|
|
279 module, mi);
|
|
280 if (rv != NGX_CONF_OK) {
|
|
281 *cf = pcf;
|
|
282 return rv;
|
|
283 }
|
|
284 }
|
|
285 }
|
|
286 }
|
|
287
|
34
|
288
|
0
|
289 /* init lists of the handlers */
|
|
290
|
122
|
291 if (ngx_array_init(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers,
|
|
292 cf->pool, 1, sizeof(ngx_http_handler_pt))
|
|
293 != NGX_OK)
|
34
|
294 {
|
|
295 return NGX_CONF_ERROR;
|
|
296 }
|
|
297
|
122
|
298
|
|
299 if (ngx_array_init(&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers,
|
|
300 cf->pool, 1, sizeof(ngx_http_handler_pt))
|
|
301 != NGX_OK)
|
|
302 {
|
|
303 return NGX_CONF_ERROR;
|
|
304 }
|
|
305
|
0
|
306
|
122
|
307 if (ngx_array_init(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers,
|
|
308 cf->pool, 1, sizeof(ngx_http_handler_pt))
|
|
309 != NGX_OK)
|
|
310 {
|
|
311 return NGX_CONF_ERROR;
|
|
312 }
|
|
313
|
|
314
|
130
|
315 if (ngx_array_init(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers,
|
|
316 cf->pool, 1, sizeof(ngx_http_handler_pt))
|
|
317 != NGX_OK)
|
|
318 {
|
|
319 return NGX_CONF_ERROR;
|
|
320 }
|
|
321
|
|
322
|
34
|
323 if (ngx_array_init(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers,
|
130
|
324 cf->pool, 2, sizeof(ngx_http_handler_pt))
|
122
|
325 != NGX_OK)
|
34
|
326 {
|
|
327 return NGX_CONF_ERROR;
|
|
328 }
|
|
329
|
0
|
330
|
34
|
331 if (ngx_array_init(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers,
|
122
|
332 cf->pool, 4, sizeof(ngx_http_handler_pt))
|
|
333 != NGX_OK)
|
34
|
334 {
|
|
335 return NGX_CONF_ERROR;
|
|
336 }
|
|
337
|
0
|
338
|
130
|
339 if (ngx_array_init(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers,
|
|
340 cf->pool, 1, sizeof(ngx_http_handler_pt))
|
|
341 != NGX_OK)
|
|
342 {
|
|
343 return NGX_CONF_ERROR;
|
|
344 }
|
|
345
|
|
346
|
198
|
347 if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t))
|
56
|
348 != NGX_OK)
|
|
349 {
|
|
350 return NGX_CONF_ERROR;
|
|
351 }
|
|
352
|
198
|
353 for (header = ngx_http_headers_in; header->name.len; header++) {
|
|
354 hk = ngx_array_push(&headers_in);
|
|
355 if (hk == NULL) {
|
|
356 return NGX_CONF_ERROR;
|
|
357 }
|
|
358
|
|
359 hk->key = header->name;
|
|
360 hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len);
|
|
361 hk->value = header;
|
|
362 }
|
|
363
|
|
364 hash.hash = &cmcf->headers_in_hash;
|
|
365 hash.key = ngx_hash_key_lc;
|
|
366 hash.max_size = 512;
|
244
|
367 hash.bucket_size = ngx_align(64, ngx_cacheline_size);
|
198
|
368 hash.name = "headers_in_hash";
|
|
369 hash.pool = cf->pool;
|
|
370 hash.temp_pool = NULL;
|
|
371
|
|
372 if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) {
|
|
373 return NGX_CONF_ERROR;
|
|
374 }
|
|
375
|
58
|
376
|
|
377 for (m = 0; ngx_modules[m]; m++) {
|
|
378 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
379 continue;
|
|
380 }
|
|
381
|
|
382 module = ngx_modules[m]->ctx;
|
|
383 mi = ngx_modules[m]->ctx_index;
|
|
384
|
|
385 if (module->postconfiguration) {
|
|
386 if (module->postconfiguration(cf) != NGX_OK) {
|
|
387 return NGX_CONF_ERROR;
|
|
388 }
|
|
389 }
|
|
390 }
|
|
391
|
122
|
392 if (ngx_http_variables_init_vars(cf) != NGX_OK) {
|
|
393 return NGX_CONF_ERROR;
|
|
394 }
|
58
|
395
|
|
396 /*
|
|
397 * http{}'s cf->ctx was needed while the configuration merging
|
|
398 * and in postconfiguration process
|
|
399 */
|
|
400
|
|
401 *cf = pcf;
|
|
402
|
56
|
403
|
230
|
404 cmcf->phase_engine.server_rewrite_index = (ngx_uint_t) -1;
|
322
|
405 cmcf->phase_engine.location_rewrite_index = (ngx_uint_t) -1;
|
230
|
406 find_config_index = 0;
|
|
407 use_rewrite = cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers.nelts ? 1 : 0;
|
|
408 use_access = cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers.nelts ? 1 : 0;
|
|
409
|
|
410 n = use_rewrite + use_access + 1; /* find config phase */
|
|
411
|
|
412 for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
|
|
413 n += cmcf->phases[i].handlers.nelts;
|
|
414 }
|
|
415
|
|
416 ph = ngx_pcalloc(cf->pool,
|
|
417 n * sizeof(ngx_http_phase_handler_t) + sizeof(void *));
|
|
418 if (ph == NULL) {
|
|
419 return NGX_CONF_ERROR;
|
|
420 }
|
|
421
|
|
422 cmcf->phase_engine.handlers = ph;
|
|
423 n = 0;
|
|
424
|
|
425 for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
|
|
426 h = cmcf->phases[i].handlers.elts;
|
|
427
|
|
428 switch (i) {
|
|
429
|
|
430 case NGX_HTTP_SERVER_REWRITE_PHASE:
|
|
431 if (cmcf->phase_engine.server_rewrite_index == (ngx_uint_t) -1) {
|
|
432 cmcf->phase_engine.server_rewrite_index = n;
|
|
433 }
|
|
434 checker = ngx_http_core_generic_phase;
|
|
435
|
|
436 break;
|
|
437
|
|
438 case NGX_HTTP_FIND_CONFIG_PHASE:
|
|
439 find_config_index = n;
|
|
440
|
|
441 ph->checker = ngx_http_core_find_config_phase;
|
|
442 n++;
|
|
443 ph++;
|
|
444
|
|
445 continue;
|
|
446
|
322
|
447 case NGX_HTTP_REWRITE_PHASE:
|
|
448 if (cmcf->phase_engine.location_rewrite_index == (ngx_uint_t) -1) {
|
|
449 cmcf->phase_engine.location_rewrite_index = n;
|
|
450 }
|
|
451 checker = ngx_http_core_generic_phase;
|
|
452
|
|
453 break;
|
|
454
|
230
|
455 case NGX_HTTP_POST_REWRITE_PHASE:
|
|
456 if (use_rewrite) {
|
|
457 ph->checker = ngx_http_core_post_rewrite_phase;
|
|
458 ph->next = find_config_index;
|
|
459 n++;
|
|
460 ph++;
|
|
461 }
|
|
462
|
|
463 continue;
|
|
464
|
|
465 case NGX_HTTP_ACCESS_PHASE:
|
|
466 checker = ngx_http_core_access_phase;
|
|
467 n++;
|
|
468 break;
|
|
469
|
|
470 case NGX_HTTP_POST_ACCESS_PHASE:
|
|
471 if (use_access) {
|
|
472 ph->checker = ngx_http_core_post_access_phase;
|
|
473 ph->next = n;
|
|
474 ph++;
|
|
475 }
|
|
476
|
|
477 continue;
|
|
478
|
|
479 case NGX_HTTP_CONTENT_PHASE:
|
|
480 checker = ngx_http_core_content_phase;
|
|
481 break;
|
|
482
|
|
483 default:
|
|
484 checker = ngx_http_core_generic_phase;
|
|
485 }
|
|
486
|
|
487 n += cmcf->phases[i].handlers.nelts;
|
|
488
|
|
489 for (j = cmcf->phases[i].handlers.nelts - 1; j >=0; j--) {
|
|
490 ph->checker = checker;
|
|
491 ph->handler = h[j];
|
|
492 ph->next = n;
|
|
493 ph++;
|
|
494 }
|
|
495 }
|
|
496
|
|
497
|
0
|
498 /*
|
4
|
499 * create the lists of ports, addresses and server names
|
|
500 * to quickly find the server core module configuration at run-time
|
0
|
501 */
|
|
502
|
142
|
503 if (ngx_array_init(&in_ports, cf->temp_pool, 2,
|
|
504 sizeof(ngx_http_conf_in_port_t))
|
50
|
505 != NGX_OK)
|
4
|
506 {
|
|
507 return NGX_CONF_ERROR;
|
|
508 }
|
0
|
509
|
|
510 /* "server" directives */
|
4
|
511
|
0
|
512 cscfp = cmcf->servers.elts;
|
|
513 for (s = 0; s < cmcf->servers.nelts; s++) {
|
|
514
|
|
515 /* "listen" directives */
|
4
|
516
|
0
|
517 lscf = cscfp[s]->listen.elts;
|
|
518 for (l = 0; l < cscfp[s]->listen.nelts; l++) {
|
|
519
|
|
520 /* AF_INET only */
|
|
521
|
|
522 in_port = in_ports.elts;
|
|
523 for (p = 0; p < in_ports.nelts; p++) {
|
|
524
|
142
|
525 if (lscf[l].port != in_port[p].port) {
|
|
526 continue;
|
|
527 }
|
0
|
528
|
142
|
529 /* the port is already in the port list */
|
0
|
530
|
142
|
531 in_addr = in_port[p].addrs.elts;
|
|
532 for (a = 0; a < in_port[p].addrs.nelts; a++) {
|
0
|
533
|
142
|
534 if (lscf[l].addr != in_addr[a].addr) {
|
|
535 continue;
|
|
536 }
|
0
|
537
|
142
|
538 /* the address is already in the address list */
|
0
|
539
|
142
|
540 if (ngx_http_add_names(cf, &in_addr[a], cscfp[s]) != NGX_OK)
|
|
541 {
|
|
542 return NGX_CONF_ERROR;
|
|
543 }
|
0
|
544
|
142
|
545 /*
|
|
546 * check the duplicate "default" server
|
|
547 * for this address:port
|
|
548 */
|
0
|
549
|
142
|
550 if (lscf[l].conf.default_server) {
|
4
|
551
|
142
|
552 if (in_addr[a].default_server) {
|
|
553 ngx_log_error(NGX_LOG_ERR, cf->log, 0,
|
10
|
554 "the duplicate default server in %V:%d",
|
|
555 &lscf[l].file_name, lscf[l].line);
|
0
|
556
|
142
|
557 return NGX_CONF_ERROR;
|
|
558 }
|
4
|
559
|
142
|
560 in_addr[a].core_srv_conf = cscfp[s];
|
|
561 in_addr[a].default_server = 1;
|
0
|
562 }
|
|
563
|
142
|
564 goto found;
|
4
|
565 }
|
0
|
566
|
142
|
567 /*
|
|
568 * add the address to the addresses list that
|
|
569 * bound to this port
|
|
570 */
|
0
|
571
|
142
|
572 if (ngx_http_add_address(cf, &in_port[p], &lscf[l], cscfp[s])
|
50
|
573 != NGX_OK)
|
4
|
574 {
|
|
575 return NGX_CONF_ERROR;
|
|
576 }
|
142
|
577
|
|
578 goto found;
|
0
|
579 }
|
142
|
580
|
|
581 /* add the port to the in_port list */
|
|
582
|
|
583 in_port = ngx_array_push(&in_ports);
|
|
584 if (in_port == NULL) {
|
|
585 return NGX_CONF_ERROR;
|
|
586 }
|
|
587
|
|
588 in_port->port = lscf[l].port;
|
|
589 in_port->addrs.elts = NULL;
|
|
590
|
|
591 if (ngx_http_add_address(cf, in_port, &lscf[l], cscfp[s]) != NGX_OK)
|
|
592 {
|
|
593 return NGX_CONF_ERROR;
|
|
594 }
|
|
595
|
|
596 found:
|
|
597
|
|
598 continue;
|
0
|
599 }
|
|
600 }
|
|
601
|
4
|
602
|
|
603 /* optimize the lists of ports, addresses and server names */
|
0
|
604
|
|
605 /* AF_INET only */
|
|
606
|
|
607 in_port = in_ports.elts;
|
|
608 for (p = 0; p < in_ports.nelts; p++) {
|
|
609
|
302
|
610 ngx_sort(in_port[p].addrs.elts, (size_t) in_port[p].addrs.nelts,
|
|
611 sizeof(ngx_http_conf_in_addr_t), ngx_http_cmp_conf_in_addrs);
|
72
|
612
|
4
|
613 /*
|
|
614 * check whether all name-based servers have the same configuraiton
|
152
|
615 * as the default server,
|
158
|
616 * or some servers disable optimizing the server names
|
4
|
617 */
|
0
|
618
|
|
619 in_addr = in_port[p].addrs.elts;
|
|
620 for (a = 0; a < in_port[p].addrs.nelts; a++) {
|
|
621
|
|
622 name = in_addr[a].names.elts;
|
152
|
623 for (s = 0; s < in_addr[a].names.nelts; s++) {
|
|
624
|
|
625 if (in_addr[a].core_srv_conf != name[s].core_srv_conf
|
162
|
626 || name[s].core_srv_conf->optimize_server_names == 0)
|
4
|
627 {
|
142
|
628 goto virtual_names;
|
26
|
629 }
|
|
630 }
|
|
631
|
0
|
632 /*
|
4
|
633 * if all name-based servers have the same configuration
|
152
|
634 * as the default server,
|
158
|
635 * and no servers disable optimizing the server names
|
4
|
636 * then we do not need to check them at run-time at all
|
0
|
637 */
|
|
638
|
142
|
639 in_addr[a].names.nelts = 0;
|
|
640
|
|
641 continue;
|
|
642
|
|
643 virtual_names:
|
|
644
|
168
|
645 ngx_memzero(&ha, sizeof(ngx_hash_keys_arrays_t));
|
|
646
|
142
|
647 ha.temp_pool = ngx_create_pool(16384, cf->log);
|
|
648 if (ha.temp_pool == NULL) {
|
|
649 return NGX_CONF_ERROR;
|
26
|
650 }
|
|
651
|
142
|
652 ha.pool = cf->pool;
|
26
|
653
|
142
|
654 if (ngx_hash_keys_array_init(&ha, NGX_HASH_LARGE) != NGX_OK) {
|
|
655 ngx_destroy_pool(ha.temp_pool);
|
|
656 return NGX_CONF_ERROR;
|
|
657 }
|
26
|
658
|
142
|
659 name = in_addr[a].names.elts;
|
|
660 for (s = 0; s < in_addr[a].names.nelts; s++) {
|
26
|
661
|
142
|
662 ch = name[s].name.data[0];
|
26
|
663
|
142
|
664 if (ch == '*' || ch == '.') {
|
|
665 continue;
|
|
666 }
|
|
667
|
|
668 rc = ngx_hash_add_key(&ha, &name[s].name, name[s].core_srv_conf,
|
|
669 0);
|
|
670
|
|
671 if (rc == NGX_ERROR) {
|
26
|
672 return NGX_CONF_ERROR;
|
|
673 }
|
|
674
|
142
|
675 if (rc == NGX_BUSY) {
|
|
676 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
|
180
|
677 "conflicting server name \"%V\" on %s, ignored",
|
|
678 &name[s].name, in_addr[a].listen_conf->addr);
|
142
|
679 }
|
|
680 }
|
|
681
|
|
682 for (s = 0; s < in_addr[a].names.nelts; s++) {
|
|
683
|
|
684 ch = name[s].name.data[0];
|
|
685
|
|
686 if (ch != '*' && ch != '.') {
|
|
687 continue;
|
|
688 }
|
|
689
|
|
690 rc = ngx_hash_add_key(&ha, &name[s].name, name[s].core_srv_conf,
|
|
691 NGX_HASH_WILDCARD_KEY);
|
|
692
|
|
693 if (rc == NGX_ERROR) {
|
|
694 return NGX_CONF_ERROR;
|
26
|
695 }
|
|
696
|
142
|
697 if (rc == NGX_BUSY) {
|
|
698 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
|
180
|
699 "conflicting server name \"%V\" on %s, ignored",
|
|
700 &name[s].name, in_addr[a].listen_conf->addr);
|
142
|
701 }
|
|
702 }
|
26
|
703
|
142
|
704 hash.key = ngx_hash_key_lc;
|
|
705 hash.max_size = cmcf->server_names_hash_max_size;
|
|
706 hash.bucket_size = cmcf->server_names_hash_bucket_size;
|
|
707 hash.name = "server_names_hash";
|
|
708 hash.pool = cf->pool;
|
26
|
709
|
142
|
710 if (ha.keys.nelts) {
|
|
711 hash.hash = &in_addr[a].hash;
|
|
712 hash.temp_pool = NULL;
|
|
713
|
|
714 if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK)
|
|
715 {
|
|
716 ngx_destroy_pool(ha.temp_pool);
|
|
717 return NGX_CONF_ERROR;
|
26
|
718 }
|
0
|
719 }
|
142
|
720
|
|
721 if (ha.dns_wildcards.nelts) {
|
|
722
|
|
723 ngx_qsort(ha.dns_wildcards.elts,
|
|
724 (size_t) ha.dns_wildcards.nelts,
|
|
725 sizeof(ngx_hash_key_t),
|
|
726 ngx_http_cmp_dns_wildcards);
|
|
727
|
|
728 hash.hash = NULL;
|
|
729 hash.temp_pool = ha.temp_pool;
|
|
730
|
|
731 if (ngx_hash_wildcard_init(&hash, ha.dns_wildcards.elts,
|
|
732 ha.dns_wildcards.nelts)
|
|
733 != NGX_OK)
|
|
734 {
|
|
735 ngx_destroy_pool(ha.temp_pool);
|
|
736 return NGX_CONF_ERROR;
|
|
737 }
|
|
738
|
|
739 in_addr[a].dns_wildcards = (ngx_hash_wildcard_t *) hash.hash;
|
|
740 }
|
|
741
|
|
742 ngx_destroy_pool(ha.temp_pool);
|
0
|
743 }
|
|
744
|
142
|
745 in_addr = in_port[p].addrs.elts;
|
|
746 last = in_port[p].addrs.nelts;
|
|
747
|
0
|
748 /*
|
26
|
749 * if there is the binding to the "*:port" then we need to bind()
|
|
750 * to the "*:port" only and ignore the other bindings
|
0
|
751 */
|
|
752
|
142
|
753 if (in_addr[last - 1].addr == INADDR_ANY) {
|
|
754 in_addr[last - 1].bind = 1;
|
|
755 bind_all = 0;
|
0
|
756
|
|
757 } else {
|
142
|
758 bind_all = 1;
|
0
|
759 }
|
|
760
|
142
|
761 for (a = 0; a < last; /* void */ ) {
|
|
762
|
|
763 if (!bind_all && !in_addr[a].bind) {
|
|
764 a++;
|
|
765 continue;
|
|
766 }
|
0
|
767
|
|
768 ls = ngx_listening_inet_stream_socket(cf, in_addr[a].addr,
|
|
769 in_port[p].port);
|
|
770 if (ls == NULL) {
|
|
771 return NGX_CONF_ERROR;
|
|
772 }
|
|
773
|
|
774 ls->addr_ntop = 1;
|
|
775
|
|
776 ls->handler = ngx_http_init_connection;
|
|
777
|
|
778 cscf = in_addr[a].core_srv_conf;
|
|
779 ls->pool_size = cscf->connection_pool_size;
|
72
|
780 ls->post_accept_timeout = cscf->client_header_timeout;
|
0
|
781
|
|
782 clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index];
|
92
|
783
|
|
784 ls->log = *clcf->err_log;
|
|
785 ls->log.data = &ls->addr_text;
|
|
786 ls->log.handler = ngx_accept_log_error;
|
0
|
787
|
10
|
788 #if (NGX_WIN32)
|
0
|
789 iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module);
|
|
790 if (iocpcf->acceptex_read) {
|
|
791 ls->post_accept_buffer_size = cscf->client_header_buffer_size;
|
|
792 }
|
|
793 #endif
|
|
794
|
142
|
795 ls->backlog = in_addr[a].listen_conf->backlog;
|
|
796 ls->rcvbuf = in_addr[a].listen_conf->rcvbuf;
|
|
797 ls->sndbuf = in_addr[a].listen_conf->sndbuf;
|
72
|
798
|
|
799 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
|
142
|
800 ls->accept_filter = in_addr[a].listen_conf->accept_filter;
|
72
|
801 #endif
|
|
802
|
|
803 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
|
142
|
804 ls->deferred_accept = in_addr[a].listen_conf->deferred_accept;
|
72
|
805 #endif
|
|
806
|
142
|
807 hip = ngx_palloc(cf->pool, sizeof(ngx_http_in_port_t));
|
|
808 if (hip == NULL) {
|
|
809 return NGX_CONF_ERROR;
|
|
810 }
|
0
|
811
|
142
|
812 hip->port = in_port[p].port;
|
|
813
|
|
814 hip->port_text.data = ngx_palloc(cf->pool, 7);
|
|
815 if (hip->port_text.data == NULL) {
|
|
816 return NGX_CONF_ERROR;
|
|
817 }
|
0
|
818
|
142
|
819 ls->servers = hip;
|
|
820
|
|
821 hip->port_text.len = ngx_sprintf(hip->port_text.data, ":%d",
|
|
822 hip->port)
|
|
823 - hip->port_text.data;
|
0
|
824
|
142
|
825 in_addr = in_port[p].addrs.elts;
|
0
|
826
|
142
|
827 if (in_addr[a].bind && in_addr[a].addr != INADDR_ANY) {
|
|
828 hip->naddrs = 1;
|
|
829 done = 0;
|
0
|
830
|
142
|
831 } else if (in_port[p].addrs.nelts > 1
|
|
832 && in_addr[last - 1].addr == INADDR_ANY)
|
|
833 {
|
|
834 hip->naddrs = last;
|
|
835 done = 1;
|
0
|
836
|
142
|
837 } else {
|
|
838 hip->naddrs = 1;
|
|
839 done = 0;
|
|
840 }
|
0
|
841
|
142
|
842 #if 0
|
|
843 ngx_log_error(NGX_LOG_ALERT, cf->log, 0,
|
|
844 "%ui: %V %d %ui %ui",
|
|
845 a, &ls->addr_text, in_addr[a].bind,
|
|
846 hip->naddrs, last);
|
|
847 #endif
|
0
|
848
|
142
|
849 hip->addrs = ngx_pcalloc(cf->pool,
|
|
850 hip->naddrs * sizeof(ngx_http_in_addr_t));
|
|
851 if (hip->addrs == NULL) {
|
|
852 return NGX_CONF_ERROR;
|
|
853 }
|
0
|
854
|
142
|
855 for (i = 0; i < hip->naddrs; i++) {
|
|
856 hip->addrs[i].addr = in_addr[i].addr;
|
|
857 hip->addrs[i].core_srv_conf = in_addr[i].core_srv_conf;
|
0
|
858
|
142
|
859 if (in_addr[i].hash.buckets == NULL
|
|
860 && (in_addr[i].dns_wildcards == NULL
|
|
861 || in_addr[i].dns_wildcards->hash.buckets == NULL))
|
|
862 {
|
0
|
863 continue;
|
|
864 }
|
142
|
865
|
|
866 vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
|
|
867 if (vn == NULL) {
|
|
868 return NGX_CONF_ERROR;
|
|
869 }
|
|
870 hip->addrs[i].virtual_names = vn;
|
|
871
|
|
872 vn->hash = in_addr[i].hash;
|
|
873 vn->dns_wildcards = in_addr[i].dns_wildcards;
|
0
|
874 }
|
|
875
|
142
|
876 if (done) {
|
|
877 break;
|
|
878 }
|
|
879
|
|
880 in_addr++;
|
|
881 in_port[p].addrs.elts = in_addr;
|
|
882 last--;
|
|
883
|
|
884 a = 0;
|
0
|
885 }
|
|
886 }
|
|
887
|
138
|
888 #if 0
|
4
|
889 {
|
|
890 u_char address[20];
|
138
|
891 ngx_uint_t p, a;
|
4
|
892
|
0
|
893 in_port = in_ports.elts;
|
|
894 for (p = 0; p < in_ports.nelts; p++) {
|
|
895 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
|
10
|
896 "port: %d %p", in_port[p].port, &in_port[p]);
|
0
|
897 in_addr = in_port[p].addrs.elts;
|
|
898 for (a = 0; a < in_port[p].addrs.nelts; a++) {
|
4
|
899 ngx_inet_ntop(AF_INET, &in_addr[a].addr, address, 20);
|
|
900 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, cf->log, 0,
|
10
|
901 "%s:%d %p",
|
4
|
902 address, in_port[p].port, in_addr[a].core_srv_conf);
|
142
|
903 name = in_addr[a].names.elts;
|
0
|
904 for (n = 0; n < in_addr[a].names.nelts; n++) {
|
4
|
905 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, cf->log, 0,
|
10
|
906 "%s:%d %V %p",
|
142
|
907 address, in_port[p].port, &name[n].name,
|
|
908 name[n].core_srv_conf);
|
0
|
909 }
|
|
910 }
|
|
911 }
|
4
|
912 }
|
0
|
913 #endif
|
|
914
|
|
915 return NGX_CONF_OK;
|
|
916 }
|
|
917
|
|
918
|
4
|
919 /*
|
|
920 * add the server address, the server names and the server core module
|
|
921 * configurations to the port (in_port)
|
|
922 */
|
|
923
|
48
|
924 static ngx_int_t
|
142
|
925 ngx_http_add_address(ngx_conf_t *cf, ngx_http_conf_in_port_t *in_port,
|
48
|
926 ngx_http_listen_t *lscf, ngx_http_core_srv_conf_t *cscf)
|
4
|
927 {
|
142
|
928 ngx_http_conf_in_addr_t *in_addr;
|
4
|
929
|
|
930 if (in_port->addrs.elts == NULL) {
|
142
|
931 if (ngx_array_init(&in_port->addrs, cf->temp_pool, 4,
|
|
932 sizeof(ngx_http_conf_in_addr_t)) != NGX_OK)
|
4
|
933 {
|
|
934 return NGX_ERROR;
|
|
935 }
|
|
936 }
|
|
937
|
50
|
938 in_addr = ngx_array_push(&in_port->addrs);
|
|
939 if (in_addr == NULL) {
|
4
|
940 return NGX_ERROR;
|
|
941 }
|
|
942
|
|
943 in_addr->addr = lscf->addr;
|
142
|
944 in_addr->hash.buckets = NULL;
|
|
945 in_addr->hash.size = 0;
|
|
946 in_addr->dns_wildcards = NULL;
|
4
|
947 in_addr->names.elts = NULL;
|
|
948 in_addr->core_srv_conf = cscf;
|
142
|
949 in_addr->default_server = lscf->conf.default_server;
|
|
950 in_addr->bind = lscf->conf.bind;
|
|
951 in_addr->listen_conf = &lscf->conf;
|
4
|
952
|
|
953 #if (NGX_DEBUG)
|
|
954 {
|
|
955 u_char text[20];
|
|
956 ngx_inet_ntop(AF_INET, &in_addr->addr, text, 20);
|
|
957 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0, "address: %s:%d",
|
|
958 text, in_port->port);
|
|
959 }
|
|
960 #endif
|
|
961
|
|
962 return ngx_http_add_names(cf, in_addr, cscf);
|
|
963 }
|
|
964
|
|
965
|
|
966 /*
|
|
967 * add the server names and the server core module
|
|
968 * configurations to the address:port (in_addr)
|
|
969 */
|
|
970
|
48
|
971 static ngx_int_t
|
142
|
972 ngx_http_add_names(ngx_conf_t *cf, ngx_http_conf_in_addr_t *in_addr,
|
48
|
973 ngx_http_core_srv_conf_t *cscf)
|
4
|
974 {
|
26
|
975 ngx_uint_t i, n;
|
4
|
976 ngx_http_server_name_t *server_names, *name;
|
|
977
|
|
978 if (in_addr->names.elts == NULL) {
|
142
|
979 if (ngx_array_init(&in_addr->names, cf->temp_pool, 4,
|
50
|
980 sizeof(ngx_http_server_name_t)) != NGX_OK)
|
26
|
981 {
|
|
982 return NGX_ERROR;
|
|
983 }
|
|
984 }
|
|
985
|
4
|
986 server_names = cscf->server_names.elts;
|
|
987 for (i = 0; i < cscf->server_names.nelts; i++) {
|
|
988
|
26
|
989 for (n = 0; n < server_names[i].name.len; n++) {
|
|
990 server_names[i].name.data[n] =
|
|
991 ngx_tolower(server_names[i].name.data[n]);
|
|
992 }
|
|
993
|
4
|
994 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
|
10
|
995 "name: %V", &server_names[i].name);
|
4
|
996
|
|
997
|
142
|
998 name = ngx_array_push(&in_addr->names);
|
50
|
999 if (name == NULL) {
|
4
|
1000 return NGX_ERROR;
|
|
1001 }
|
|
1002
|
|
1003 *name = server_names[i];
|
|
1004 }
|
|
1005
|
|
1006 return NGX_OK;
|
|
1007 }
|
|
1008
|
|
1009
|
48
|
1010 static char *
|
|
1011 ngx_http_merge_locations(ngx_conf_t *cf, ngx_array_t *locations,
|
|
1012 void **loc_conf, ngx_http_module_t *module, ngx_uint_t ctx_index)
|
0
|
1013 {
|
|
1014 char *rv;
|
|
1015 ngx_uint_t i;
|
|
1016 ngx_http_core_loc_conf_t **clcfp;
|
|
1017
|
142
|
1018 clcfp = locations->elts;
|
0
|
1019
|
|
1020 for (i = 0; i < locations->nelts; i++) {
|
|
1021 rv = module->merge_loc_conf(cf, loc_conf[ctx_index],
|
|
1022 clcfp[i]->loc_conf[ctx_index]);
|
|
1023 if (rv != NGX_CONF_OK) {
|
|
1024 return rv;
|
|
1025 }
|
|
1026
|
308
|
1027 if (clcfp[i]->locations == NULL) {
|
|
1028 continue;
|
|
1029 }
|
|
1030
|
|
1031 rv = ngx_http_merge_locations(cf, clcfp[i]->locations,
|
0
|
1032 clcfp[i]->loc_conf, module, ctx_index);
|
|
1033 if (rv != NGX_CONF_OK) {
|
|
1034 return rv;
|
|
1035 }
|
|
1036 }
|
|
1037
|
|
1038 return NGX_CONF_OK;
|
|
1039 }
|
142
|
1040
|
|
1041
|
302
|
1042 static int
|
142
|
1043 ngx_http_cmp_conf_in_addrs(const void *one, const void *two)
|
|
1044 {
|
|
1045 ngx_http_conf_in_addr_t *first, *second;
|
|
1046
|
|
1047 first = (ngx_http_conf_in_addr_t *) one;
|
|
1048 second = (ngx_http_conf_in_addr_t *) two;
|
|
1049
|
|
1050 if (first->addr == INADDR_ANY) {
|
|
1051 /* the INADDR_ANY must be the last resort, shift it to the end */
|
|
1052 return 1;
|
|
1053 }
|
|
1054
|
|
1055 if (first->bind && !second->bind) {
|
|
1056 /* shift explicit bind()ed addresses to the start */
|
|
1057 return -1;
|
|
1058 }
|
|
1059
|
|
1060 if (!first->bind && second->bind) {
|
|
1061 /* shift explicit bind()ed addresses to the start */
|
|
1062 return 1;
|
|
1063 }
|
|
1064
|
|
1065 /* do not sort by default */
|
|
1066
|
|
1067 return 0;
|
|
1068 }
|
|
1069
|
|
1070
|
|
1071 static int ngx_libc_cdecl
|
|
1072 ngx_http_cmp_dns_wildcards(const void *one, const void *two)
|
|
1073 {
|
|
1074 ngx_hash_key_t *first, *second;
|
|
1075
|
|
1076 first = (ngx_hash_key_t *) one;
|
|
1077 second = (ngx_hash_key_t *) two;
|
|
1078
|
|
1079 return ngx_strcmp(first->key.data, second->key.data);
|
|
1080 }
|