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);
|
52
|
14 static int ngx_libc_cdecl ngx_cmp_server_names(const void *one,
|
|
15 const void *two);
|
4
|
16 static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
|
48
|
17 ngx_http_in_port_t *in_port, ngx_http_listen_t *lscf,
|
|
18 ngx_http_core_srv_conf_t *cscf);
|
4
|
19 static ngx_int_t ngx_http_add_names(ngx_conf_t *cf,
|
48
|
20 ngx_http_in_addr_t *in_addr, ngx_http_core_srv_conf_t *cscf);
|
0
|
21 static char *ngx_http_merge_locations(ngx_conf_t *cf,
|
48
|
22 ngx_array_t *locations, void **loc_conf, ngx_http_module_t *module,
|
|
23 ngx_uint_t ctx_index);
|
0
|
24
|
34
|
25 ngx_uint_t ngx_http_max_module;
|
0
|
26
|
|
27 ngx_uint_t ngx_http_total_requests;
|
|
28 uint64_t ngx_http_total_sent;
|
|
29
|
|
30
|
|
31 ngx_int_t (*ngx_http_top_header_filter) (ngx_http_request_t *r);
|
|
32 ngx_int_t (*ngx_http_top_body_filter) (ngx_http_request_t *r, ngx_chain_t *ch);
|
|
33
|
|
34
|
|
35 static ngx_command_t ngx_http_commands[] = {
|
|
36
|
48
|
37 { ngx_string("http"),
|
|
38 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
|
|
39 ngx_http_block,
|
|
40 0,
|
|
41 0,
|
|
42 NULL },
|
0
|
43
|
48
|
44 ngx_null_command
|
0
|
45 };
|
|
46
|
|
47
|
|
48 static ngx_core_module_t ngx_http_module_ctx = {
|
|
49 ngx_string("http"),
|
|
50 NULL,
|
|
51 NULL
|
|
52 };
|
|
53
|
|
54
|
|
55 ngx_module_t ngx_http_module = {
|
|
56 NGX_MODULE,
|
|
57 &ngx_http_module_ctx, /* module context */
|
|
58 ngx_http_commands, /* module directives */
|
|
59 NGX_CORE_MODULE, /* module type */
|
|
60 NULL, /* init module */
|
48
|
61 NULL /* init process */
|
0
|
62 };
|
|
63
|
|
64
|
48
|
65 static char *
|
|
66 ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
67 {
|
|
68 char *rv;
|
|
69 ngx_uint_t mi, m, s, l, p, a, n;
|
26
|
70 ngx_uint_t port_found, addr_found, virtual_names, key;
|
0
|
71 ngx_conf_t pcf;
|
|
72 ngx_array_t in_ports;
|
|
73 ngx_listening_t *ls;
|
|
74 ngx_http_listen_t *lscf;
|
|
75 ngx_http_module_t *module;
|
|
76 ngx_http_handler_pt *h;
|
|
77 ngx_http_conf_ctx_t *ctx;
|
|
78 ngx_http_in_port_t *in_port, *inport;
|
|
79 ngx_http_in_addr_t *in_addr, *inaddr;
|
|
80 ngx_http_server_name_t *s_name, *name;
|
|
81 ngx_http_core_srv_conf_t **cscfp, *cscf;
|
|
82 ngx_http_core_loc_conf_t *clcf;
|
|
83 ngx_http_core_main_conf_t *cmcf;
|
10
|
84 #if (NGX_WIN32)
|
0
|
85 ngx_iocp_conf_t *iocpcf;
|
|
86 #endif
|
|
87
|
4
|
88 #if (NGX_SUPPRESS_WARN)
|
34
|
89 /* MSVC thinks "in_ports" may be used without having been initialized */
|
4
|
90 ngx_memzero(&in_ports, sizeof(ngx_array_t));
|
|
91 #endif
|
|
92
|
26
|
93
|
0
|
94 /* the main http context */
|
26
|
95
|
50
|
96 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
|
|
97 if (ctx == NULL) {
|
34
|
98 return NGX_CONF_ERROR;
|
|
99 }
|
0
|
100
|
|
101 *(ngx_http_conf_ctx_t **) conf = ctx;
|
|
102
|
34
|
103
|
0
|
104 /* count the number of the http modules and set up their indices */
|
|
105
|
|
106 ngx_http_max_module = 0;
|
|
107 for (m = 0; ngx_modules[m]; m++) {
|
|
108 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
109 continue;
|
|
110 }
|
|
111
|
|
112 ngx_modules[m]->ctx_index = ngx_http_max_module++;
|
|
113 }
|
|
114
|
26
|
115
|
34
|
116 /* the http main_conf context, it is the same in the all http contexts */
|
0
|
117
|
34
|
118 ctx->main_conf = ngx_pcalloc(cf->pool,
|
|
119 sizeof(void *) * ngx_http_max_module);
|
|
120 if (ctx->main_conf == NULL) {
|
|
121 return NGX_CONF_ERROR;
|
|
122 }
|
0
|
123
|
|
124
|
34
|
125 /*
|
|
126 * the http null srv_conf context, it is used to merge
|
|
127 * the server{}s' srv_conf's
|
|
128 */
|
|
129
|
|
130 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
131 if (ctx->srv_conf == NULL) {
|
|
132 return NGX_CONF_ERROR;
|
|
133 }
|
|
134
|
|
135
|
|
136 /*
|
|
137 * the http null loc_conf context, it is used to merge
|
|
138 * the server{}s' loc_conf's
|
|
139 */
|
|
140
|
|
141 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
142 if (ctx->loc_conf == NULL) {
|
|
143 return NGX_CONF_ERROR;
|
|
144 }
|
|
145
|
|
146
|
|
147 /*
|
|
148 * create the main_conf's, the null srv_conf's, and the null loc_conf's
|
|
149 * of the all http modules
|
|
150 */
|
0
|
151
|
|
152 for (m = 0; ngx_modules[m]; m++) {
|
|
153 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
154 continue;
|
|
155 }
|
|
156
|
|
157 module = ngx_modules[m]->ctx;
|
|
158 mi = ngx_modules[m]->ctx_index;
|
|
159
|
|
160 if (module->pre_conf) {
|
|
161 if (module->pre_conf(cf) != NGX_OK) {
|
|
162 return NGX_CONF_ERROR;
|
|
163 }
|
|
164 }
|
|
165
|
|
166 if (module->create_main_conf) {
|
50
|
167 ctx->main_conf[mi] = module->create_main_conf(cf);
|
|
168 if (ctx->main_conf[mi] == NULL) {
|
34
|
169 return NGX_CONF_ERROR;
|
|
170 }
|
0
|
171 }
|
|
172
|
|
173 if (module->create_srv_conf) {
|
50
|
174 ctx->srv_conf[mi] = module->create_srv_conf(cf);
|
|
175 if (ctx->srv_conf[mi] == NULL) {
|
34
|
176 return NGX_CONF_ERROR;
|
|
177 }
|
0
|
178 }
|
|
179
|
|
180 if (module->create_loc_conf) {
|
50
|
181 ctx->loc_conf[mi] = module->create_loc_conf(cf);
|
|
182 if (ctx->loc_conf[mi] == NULL) {
|
34
|
183 return NGX_CONF_ERROR;
|
|
184 }
|
0
|
185 }
|
|
186 }
|
|
187
|
34
|
188
|
0
|
189 /* parse inside the http{} block */
|
|
190
|
|
191 pcf = *cf;
|
|
192 cf->ctx = ctx;
|
|
193 cf->module_type = NGX_HTTP_MODULE;
|
|
194 cf->cmd_type = NGX_HTTP_MAIN_CONF;
|
|
195 rv = ngx_conf_parse(cf, NULL);
|
|
196
|
|
197 if (rv != NGX_CONF_OK) {
|
|
198 *cf = pcf;
|
|
199 return rv;
|
|
200 }
|
|
201
|
|
202 /*
|
|
203 * init http{} main_conf's, merge the server{}s' srv_conf's
|
|
204 * and its location{}s' loc_conf's
|
|
205 */
|
|
206
|
|
207 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
|
|
208 cscfp = cmcf->servers.elts;
|
|
209
|
|
210 for (m = 0; ngx_modules[m]; m++) {
|
|
211 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
212 continue;
|
|
213 }
|
|
214
|
|
215 module = ngx_modules[m]->ctx;
|
|
216 mi = ngx_modules[m]->ctx_index;
|
|
217
|
|
218 /* init http{} main_conf's */
|
|
219
|
|
220 if (module->init_main_conf) {
|
|
221 rv = module->init_main_conf(cf, ctx->main_conf[mi]);
|
|
222 if (rv != NGX_CONF_OK) {
|
|
223 *cf = pcf;
|
|
224 return rv;
|
|
225 }
|
|
226 }
|
|
227
|
|
228 for (s = 0; s < cmcf->servers.nelts; s++) {
|
|
229
|
|
230 /* merge the server{}s' srv_conf's */
|
|
231
|
|
232 if (module->merge_srv_conf) {
|
|
233 rv = module->merge_srv_conf(cf,
|
|
234 ctx->srv_conf[mi],
|
|
235 cscfp[s]->ctx->srv_conf[mi]);
|
|
236 if (rv != NGX_CONF_OK) {
|
|
237 *cf = pcf;
|
|
238 return rv;
|
|
239 }
|
|
240 }
|
|
241
|
|
242 if (module->merge_loc_conf) {
|
|
243
|
|
244 /* merge the server{}'s loc_conf */
|
|
245
|
|
246 rv = module->merge_loc_conf(cf,
|
|
247 ctx->loc_conf[mi],
|
|
248 cscfp[s]->ctx->loc_conf[mi]);
|
|
249 if (rv != NGX_CONF_OK) {
|
|
250 *cf = pcf;
|
|
251 return rv;
|
|
252 }
|
|
253
|
|
254 /* merge the locations{}' loc_conf's */
|
|
255
|
|
256 rv = ngx_http_merge_locations(cf, &cscfp[s]->locations,
|
|
257 cscfp[s]->ctx->loc_conf,
|
|
258 module, mi);
|
|
259 if (rv != NGX_CONF_OK) {
|
|
260 *cf = pcf;
|
|
261 return rv;
|
|
262 }
|
|
263 }
|
|
264 }
|
|
265 }
|
|
266
|
34
|
267
|
|
268 /* we needed http{}'s cf->ctx while the merging configuration */
|
50
|
269
|
0
|
270 *cf = pcf;
|
|
271
|
34
|
272
|
0
|
273 /* init lists of the handlers */
|
|
274
|
34
|
275 if (ngx_array_init(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers,
|
50
|
276 cf->pool, 1, sizeof(ngx_http_handler_pt)) != NGX_OK)
|
34
|
277 {
|
|
278 return NGX_CONF_ERROR;
|
|
279 }
|
|
280
|
0
|
281 cmcf->phases[NGX_HTTP_REWRITE_PHASE].type = NGX_OK;
|
|
282
|
|
283
|
34
|
284 /* the special find config phase for a single handler */
|
0
|
285
|
34
|
286 if (ngx_array_init(&cmcf->phases[NGX_HTTP_FIND_CONFIG_PHASE].handlers,
|
50
|
287 cf->pool, 1, sizeof(ngx_http_handler_pt)) != NGX_OK)
|
34
|
288 {
|
|
289 return NGX_CONF_ERROR;
|
|
290 }
|
|
291
|
0
|
292 cmcf->phases[NGX_HTTP_FIND_CONFIG_PHASE].type = NGX_OK;
|
|
293
|
50
|
294 h = ngx_array_push(&cmcf->phases[NGX_HTTP_FIND_CONFIG_PHASE].handlers);
|
34
|
295 if (h == NULL) {
|
|
296 return NGX_CONF_ERROR;
|
|
297 }
|
|
298
|
0
|
299 *h = ngx_http_find_location_config;
|
|
300
|
|
301
|
34
|
302 if (ngx_array_init(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers,
|
50
|
303 cf->pool, 1, sizeof(ngx_http_handler_pt)) != NGX_OK)
|
34
|
304 {
|
|
305 return NGX_CONF_ERROR;
|
|
306 }
|
|
307
|
0
|
308 cmcf->phases[NGX_HTTP_ACCESS_PHASE].type = NGX_DECLINED;
|
|
309
|
|
310
|
34
|
311 if (ngx_array_init(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers,
|
50
|
312 cf->pool, 4, sizeof(ngx_http_handler_pt)) != NGX_OK)
|
34
|
313 {
|
|
314 return NGX_CONF_ERROR;
|
|
315 }
|
|
316
|
0
|
317 cmcf->phases[NGX_HTTP_CONTENT_PHASE].type = NGX_OK;
|
|
318
|
|
319
|
|
320 /*
|
4
|
321 * create the lists of ports, addresses and server names
|
|
322 * to quickly find the server core module configuration at run-time
|
0
|
323 */
|
|
324
|
4
|
325 if (ngx_array_init(&in_ports, cf->pool, 10, sizeof(ngx_http_in_port_t))
|
50
|
326 != NGX_OK)
|
4
|
327 {
|
|
328 return NGX_CONF_ERROR;
|
|
329 }
|
0
|
330
|
|
331 /* "server" directives */
|
4
|
332
|
0
|
333 cscfp = cmcf->servers.elts;
|
|
334 for (s = 0; s < cmcf->servers.nelts; s++) {
|
|
335
|
|
336 /* "listen" directives */
|
4
|
337
|
0
|
338 lscf = cscfp[s]->listen.elts;
|
|
339 for (l = 0; l < cscfp[s]->listen.nelts; l++) {
|
|
340
|
|
341 port_found = 0;
|
|
342
|
|
343 /* AF_INET only */
|
|
344
|
|
345 in_port = in_ports.elts;
|
|
346 for (p = 0; p < in_ports.nelts; p++) {
|
|
347
|
|
348 if (lscf[l].port == in_port[p].port) {
|
|
349
|
|
350 /* the port is already in the port list */
|
|
351
|
|
352 port_found = 1;
|
|
353 addr_found = 0;
|
|
354
|
|
355 in_addr = in_port[p].addrs.elts;
|
|
356 for (a = 0; a < in_port[p].addrs.nelts; a++) {
|
|
357
|
|
358 if (lscf[l].addr == in_addr[a].addr) {
|
|
359
|
4
|
360 /* the address is already in the address list */
|
0
|
361
|
4
|
362 if (ngx_http_add_names(cf, &in_addr[a], cscfp[s])
|
50
|
363 != NGX_OK)
|
4
|
364 {
|
|
365 return NGX_CONF_ERROR;
|
0
|
366 }
|
|
367
|
|
368 /*
|
4
|
369 * check the duplicate "default" server
|
|
370 * for this address:port
|
0
|
371 */
|
|
372
|
|
373 if (lscf[l].default_server) {
|
4
|
374
|
0
|
375 if (in_addr[a].default_server) {
|
|
376 ngx_log_error(NGX_LOG_ERR, cf->log, 0,
|
10
|
377 "the duplicate default server in %V:%d",
|
|
378 &lscf[l].file_name, lscf[l].line);
|
0
|
379
|
|
380 return NGX_CONF_ERROR;
|
|
381 }
|
|
382
|
|
383 in_addr[a].core_srv_conf = cscfp[s];
|
|
384 in_addr[a].default_server = 1;
|
|
385 }
|
|
386
|
|
387 addr_found = 1;
|
|
388
|
|
389 break;
|
|
390
|
|
391 } else if (in_addr[a].addr == INADDR_ANY) {
|
|
392
|
4
|
393 /* the INADDR_ANY is always the last address */
|
|
394
|
50
|
395 inaddr = ngx_array_push(&in_port[p].addrs);
|
|
396 if (inaddr == NULL) {
|
4
|
397 return NGX_CONF_ERROR;
|
|
398 }
|
|
399
|
0
|
400 /*
|
4
|
401 * the INADDR_ANY must be the last resort
|
|
402 * so we move it to the end of the address list
|
|
403 * and put the new address in its place
|
0
|
404 */
|
|
405
|
|
406 ngx_memcpy(inaddr, &in_addr[a],
|
|
407 sizeof(ngx_http_in_addr_t));
|
|
408
|
|
409 in_addr[a].addr = lscf[l].addr;
|
4
|
410 in_addr[a].names.elts = NULL;
|
26
|
411 in_addr[a].hash = NULL;
|
|
412 in_addr[a].wildcards.elts = NULL;
|
0
|
413 in_addr[a].default_server = lscf[l].default_server;
|
|
414 in_addr[a].core_srv_conf = cscfp[s];
|
|
415
|
4
|
416 if (ngx_http_add_names(cf, &in_addr[a], cscfp[s])
|
50
|
417 != NGX_OK)
|
4
|
418 {
|
|
419 return NGX_CONF_ERROR;
|
|
420 }
|
0
|
421
|
|
422 addr_found = 1;
|
|
423
|
|
424 break;
|
|
425 }
|
|
426 }
|
|
427
|
|
428 if (!addr_found) {
|
|
429
|
|
430 /*
|
|
431 * add the address to the addresses list that
|
|
432 * bound to this port
|
|
433 */
|
|
434
|
4
|
435 if (ngx_http_add_address(cf, &in_port[p], &lscf[l],
|
50
|
436 cscfp[s]) != NGX_OK)
|
4
|
437 {
|
|
438 return NGX_CONF_ERROR;
|
|
439 }
|
0
|
440 }
|
|
441 }
|
|
442 }
|
|
443
|
|
444 if (!port_found) {
|
|
445
|
|
446 /* add the port to the in_port list */
|
|
447
|
50
|
448 in_port = ngx_array_push(&in_ports);
|
|
449 if (in_port == NULL) {
|
4
|
450 return NGX_CONF_ERROR;
|
|
451 }
|
0
|
452
|
|
453 in_port->port = lscf[l].port;
|
4
|
454 in_port->addrs.elts = NULL;
|
0
|
455
|
50
|
456 in_port->port_text.data = ngx_palloc(cf->pool, 7);
|
|
457 if (in_port->port_text.data == NULL) {
|
4
|
458 return NGX_CONF_ERROR;
|
|
459 }
|
0
|
460
|
4
|
461 in_port->port_text.len = ngx_sprintf(in_port->port_text.data,
|
|
462 ":%d", in_port->port)
|
|
463 - in_port->port_text.data;
|
0
|
464
|
4
|
465 if (ngx_http_add_address(cf, in_port, &lscf[l], cscfp[s])
|
50
|
466 != NGX_OK)
|
4
|
467 {
|
|
468 return NGX_CONF_ERROR;
|
|
469 }
|
0
|
470 }
|
|
471 }
|
|
472 }
|
|
473
|
4
|
474
|
|
475 /* optimize the lists of ports, addresses and server names */
|
0
|
476
|
|
477 /* AF_INET only */
|
|
478
|
|
479 in_port = in_ports.elts;
|
|
480 for (p = 0; p < in_ports.nelts; p++) {
|
|
481
|
4
|
482 /*
|
|
483 * check whether all name-based servers have the same configuraiton
|
|
484 * as the default server, or some servers restrict the host names
|
|
485 */
|
0
|
486
|
|
487 in_addr = in_port[p].addrs.elts;
|
|
488 for (a = 0; a < in_port[p].addrs.nelts; a++) {
|
|
489
|
|
490 virtual_names = 0;
|
|
491
|
|
492 name = in_addr[a].names.elts;
|
|
493 for (n = 0; n < in_addr[a].names.nelts; n++) {
|
4
|
494 if (in_addr[a].core_srv_conf != name[n].core_srv_conf
|
|
495 || name[n].core_srv_conf->restrict_host_names
|
50
|
496 != NGX_HTTP_RESTRICT_HOST_OFF)
|
4
|
497 {
|
0
|
498 virtual_names = 1;
|
|
499 break;
|
|
500 }
|
|
501 }
|
|
502
|
26
|
503 if (!virtual_names) {
|
|
504 name = in_addr[a].wildcards.elts;
|
|
505 for (n = 0; n < in_addr[a].wildcards.nelts; n++) {
|
|
506 if (in_addr[a].core_srv_conf != name[n].core_srv_conf
|
|
507 || name[n].core_srv_conf->restrict_host_names
|
50
|
508 != NGX_HTTP_RESTRICT_HOST_OFF)
|
26
|
509 {
|
|
510 virtual_names = 1;
|
|
511 break;
|
|
512 }
|
|
513 }
|
|
514 }
|
|
515
|
0
|
516 /*
|
4
|
517 * if all name-based servers have the same configuration
|
|
518 * as the default server, and no servers restrict the host names
|
|
519 * then we do not need to check them at run-time at all
|
0
|
520 */
|
|
521
|
|
522 if (!virtual_names) {
|
|
523 in_addr[a].names.nelts = 0;
|
26
|
524 continue;
|
|
525 }
|
|
526
|
|
527
|
|
528 ngx_qsort(in_addr[a].names.elts, in_addr[a].names.nelts,
|
|
529 sizeof(ngx_http_server_name_t), ngx_cmp_server_names);
|
|
530
|
|
531
|
|
532 /* create a hash for many names */
|
|
533
|
|
534 if (in_addr[a].names.nelts > cmcf->server_names_hash_threshold) {
|
|
535 in_addr[a].hash = ngx_palloc(cf->pool,
|
|
536 cmcf->server_names_hash
|
|
537 * sizeof(ngx_array_t));
|
|
538 if (in_addr[a].hash == NULL) {
|
|
539 return NGX_CONF_ERROR;
|
|
540 }
|
|
541
|
|
542 for (n = 0; n < cmcf->server_names_hash; n++) {
|
|
543 if (ngx_array_init(&in_addr[a].hash[n], cf->pool, 5,
|
50
|
544 sizeof(ngx_http_server_name_t)) != NGX_OK)
|
26
|
545 {
|
|
546 return NGX_CONF_ERROR;
|
|
547 }
|
|
548 }
|
|
549
|
|
550 name = in_addr[a].names.elts;
|
|
551 for (s = 0; s < in_addr[a].names.nelts; s++) {
|
|
552 ngx_http_server_names_hash_key(key, name[s].name.data,
|
|
553 name[s].name.len,
|
|
554 cmcf->server_names_hash);
|
|
555
|
50
|
556 s_name = ngx_array_push(&in_addr[a].hash[key]);
|
|
557 if (s_name == NULL) {
|
26
|
558 return NGX_CONF_ERROR;
|
|
559 }
|
|
560
|
|
561 *s_name = name[s];
|
|
562 }
|
0
|
563 }
|
|
564 }
|
|
565
|
|
566 /*
|
26
|
567 * if there is the binding to the "*:port" then we need to bind()
|
|
568 * to the "*:port" only and ignore the other bindings
|
0
|
569 */
|
|
570
|
|
571 if (in_addr[a - 1].addr == INADDR_ANY) {
|
|
572 a--;
|
|
573
|
|
574 } else {
|
|
575 a = 0;
|
|
576 }
|
|
577
|
|
578 in_addr = in_port[p].addrs.elts;
|
|
579 while (a < in_port[p].addrs.nelts) {
|
|
580
|
|
581 ls = ngx_listening_inet_stream_socket(cf, in_addr[a].addr,
|
|
582 in_port[p].port);
|
|
583 if (ls == NULL) {
|
|
584 return NGX_CONF_ERROR;
|
|
585 }
|
|
586
|
|
587 ls->backlog = -1;
|
50
|
588
|
0
|
589 ls->addr_ntop = 1;
|
|
590
|
|
591 ls->handler = ngx_http_init_connection;
|
|
592
|
|
593 cscf = in_addr[a].core_srv_conf;
|
|
594 ls->pool_size = cscf->connection_pool_size;
|
|
595 ls->post_accept_timeout = cscf->post_accept_timeout;
|
|
596
|
|
597 clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index];
|
|
598 ls->log = clcf->err_log;
|
|
599
|
10
|
600 #if (NGX_WIN32)
|
0
|
601 iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module);
|
|
602 if (iocpcf->acceptex_read) {
|
|
603 ls->post_accept_buffer_size = cscf->client_header_buffer_size;
|
|
604 }
|
|
605 #endif
|
|
606
|
|
607 ls->ctx = ctx;
|
|
608
|
|
609 if (in_port[p].addrs.nelts > 1) {
|
|
610
|
|
611 in_addr = in_port[p].addrs.elts;
|
|
612 if (in_addr[in_port[p].addrs.nelts - 1].addr != INADDR_ANY) {
|
|
613
|
|
614 /*
|
|
615 * if this port has not the "*:port" binding then create
|
|
616 * the separate ngx_http_in_port_t for the all bindings
|
|
617 */
|
|
618
|
50
|
619 inport = ngx_palloc(cf->pool, sizeof(ngx_http_in_port_t));
|
|
620 if (inport == NULL) {
|
|
621 return NGX_CONF_ERROR;
|
|
622 }
|
0
|
623
|
|
624 inport->port = in_port[p].port;
|
|
625 inport->port_text = in_port[p].port_text;
|
|
626
|
|
627 /* init list of the addresses ... */
|
|
628
|
50
|
629 if (ngx_array_init(&inport->addrs, cf->pool, 1,
|
|
630 sizeof(ngx_http_in_addr_t)) != NGX_OK)
|
|
631 {
|
|
632 return NGX_CONF_ERROR;
|
|
633 }
|
0
|
634
|
|
635 /* ... and set up it with the first address */
|
|
636
|
|
637 inport->addrs.nelts = 1;
|
|
638 inport->addrs.elts = in_port[p].addrs.elts;
|
|
639
|
|
640 ls->servers = inport;
|
|
641
|
|
642 /* prepare for the next cycle */
|
|
643
|
|
644 in_port[p].addrs.elts = (char *) in_port[p].addrs.elts
|
50
|
645 + in_port[p].addrs.size;
|
0
|
646 in_port[p].addrs.nelts--;
|
|
647
|
|
648 in_addr = (ngx_http_in_addr_t *) in_port[p].addrs.elts;
|
|
649 a = 0;
|
|
650
|
|
651 continue;
|
|
652 }
|
|
653 }
|
|
654
|
|
655 ls->servers = &in_port[p];
|
|
656 a++;
|
|
657 }
|
|
658 }
|
|
659
|
|
660 #if (NGX_DEBUG)
|
4
|
661 {
|
|
662 u_char address[20];
|
|
663 ngx_uint_t p, a, n;
|
|
664
|
0
|
665 in_port = in_ports.elts;
|
|
666 for (p = 0; p < in_ports.nelts; p++) {
|
|
667 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
|
10
|
668 "port: %d %p", in_port[p].port, &in_port[p]);
|
0
|
669 in_addr = in_port[p].addrs.elts;
|
|
670 for (a = 0; a < in_port[p].addrs.nelts; a++) {
|
4
|
671 ngx_inet_ntop(AF_INET, &in_addr[a].addr, address, 20);
|
|
672 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, cf->log, 0,
|
10
|
673 "%s:%d %p",
|
4
|
674 address, in_port[p].port, in_addr[a].core_srv_conf);
|
0
|
675 s_name = in_addr[a].names.elts;
|
|
676 for (n = 0; n < in_addr[a].names.nelts; n++) {
|
4
|
677 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, cf->log, 0,
|
10
|
678 "%s:%d %V %p",
|
|
679 address, in_port[p].port, &s_name[n].name,
|
0
|
680 s_name[n].core_srv_conf);
|
|
681 }
|
|
682 }
|
|
683 }
|
4
|
684 }
|
0
|
685 #endif
|
|
686
|
|
687 return NGX_CONF_OK;
|
|
688 }
|
|
689
|
|
690
|
52
|
691 static int ngx_libc_cdecl
|
48
|
692 ngx_cmp_server_names(const void *one, const void *two)
|
26
|
693 {
|
|
694 ngx_http_server_name_t *first = (ngx_http_server_name_t *) one;
|
|
695 ngx_http_server_name_t *second = (ngx_http_server_name_t *) two;
|
|
696
|
|
697 return ngx_strcmp(first->name.data, second->name.data);
|
|
698 }
|
|
699
|
|
700
|
4
|
701 /*
|
|
702 * add the server address, the server names and the server core module
|
|
703 * configurations to the port (in_port)
|
|
704 */
|
|
705
|
48
|
706 static ngx_int_t
|
|
707 ngx_http_add_address(ngx_conf_t *cf, ngx_http_in_port_t *in_port,
|
|
708 ngx_http_listen_t *lscf, ngx_http_core_srv_conf_t *cscf)
|
4
|
709 {
|
|
710 ngx_http_in_addr_t *in_addr;
|
|
711
|
|
712 if (in_port->addrs.elts == NULL) {
|
|
713 if (ngx_array_init(&in_port->addrs, cf->pool, 10,
|
50
|
714 sizeof(ngx_http_in_addr_t)) != NGX_OK)
|
4
|
715 {
|
|
716 return NGX_ERROR;
|
|
717 }
|
|
718 }
|
|
719
|
50
|
720 in_addr = ngx_array_push(&in_port->addrs);
|
|
721 if (in_addr == NULL) {
|
4
|
722 return NGX_ERROR;
|
|
723 }
|
|
724
|
|
725 in_addr->addr = lscf->addr;
|
|
726 in_addr->names.elts = NULL;
|
26
|
727 in_addr->hash = NULL;
|
|
728 in_addr->wildcards.elts = NULL;
|
4
|
729 in_addr->default_server = lscf->default_server;
|
|
730 in_addr->core_srv_conf = cscf;
|
|
731
|
|
732 #if (NGX_DEBUG)
|
|
733 {
|
|
734 u_char text[20];
|
|
735 ngx_inet_ntop(AF_INET, &in_addr->addr, text, 20);
|
|
736 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0, "address: %s:%d",
|
|
737 text, in_port->port);
|
|
738 }
|
|
739 #endif
|
|
740
|
|
741 return ngx_http_add_names(cf, in_addr, cscf);
|
|
742 }
|
|
743
|
|
744
|
|
745 /*
|
|
746 * add the server names and the server core module
|
|
747 * configurations to the address:port (in_addr)
|
|
748 */
|
|
749
|
48
|
750 static ngx_int_t
|
|
751 ngx_http_add_names(ngx_conf_t *cf, ngx_http_in_addr_t *in_addr,
|
|
752 ngx_http_core_srv_conf_t *cscf)
|
4
|
753 {
|
26
|
754 ngx_uint_t i, n;
|
|
755 ngx_array_t *array;
|
4
|
756 ngx_http_server_name_t *server_names, *name;
|
|
757
|
|
758 if (in_addr->names.elts == NULL) {
|
|
759 if (ngx_array_init(&in_addr->names, cf->pool, 10,
|
50
|
760 sizeof(ngx_http_server_name_t)) != NGX_OK)
|
4
|
761 {
|
|
762 return NGX_ERROR;
|
|
763 }
|
|
764 }
|
|
765
|
26
|
766 if (in_addr->wildcards.elts == NULL) {
|
|
767 if (ngx_array_init(&in_addr->wildcards, cf->pool, 10,
|
50
|
768 sizeof(ngx_http_server_name_t)) != NGX_OK)
|
26
|
769 {
|
|
770 return NGX_ERROR;
|
|
771 }
|
|
772 }
|
|
773
|
4
|
774 server_names = cscf->server_names.elts;
|
|
775 for (i = 0; i < cscf->server_names.nelts; i++) {
|
|
776
|
26
|
777 for (n = 0; n < server_names[i].name.len; n++) {
|
|
778 server_names[i].name.data[n] =
|
|
779 ngx_tolower(server_names[i].name.data[n]);
|
|
780 }
|
|
781
|
4
|
782 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
|
10
|
783 "name: %V", &server_names[i].name);
|
4
|
784
|
|
785 /* TODO: duplicate names can be checked here */
|
|
786
|
26
|
787
|
|
788 if (server_names[i].wildcard) {
|
|
789 array = &in_addr->wildcards;
|
|
790
|
|
791 } else {
|
|
792 array = &in_addr->names;
|
|
793 }
|
|
794
|
50
|
795 name = ngx_array_push(array);
|
|
796 if (name == NULL) {
|
4
|
797 return NGX_ERROR;
|
|
798 }
|
|
799
|
|
800 *name = server_names[i];
|
|
801 }
|
|
802
|
|
803 return NGX_OK;
|
|
804 }
|
|
805
|
|
806
|
48
|
807 static char *
|
|
808 ngx_http_merge_locations(ngx_conf_t *cf, ngx_array_t *locations,
|
|
809 void **loc_conf, ngx_http_module_t *module, ngx_uint_t ctx_index)
|
0
|
810 {
|
|
811 char *rv;
|
|
812 ngx_uint_t i;
|
|
813 ngx_http_core_loc_conf_t **clcfp;
|
|
814
|
|
815 clcfp = /* (ngx_http_core_loc_conf_t **) */ locations->elts;
|
|
816
|
|
817 for (i = 0; i < locations->nelts; i++) {
|
|
818 rv = module->merge_loc_conf(cf, loc_conf[ctx_index],
|
|
819 clcfp[i]->loc_conf[ctx_index]);
|
|
820 if (rv != NGX_CONF_OK) {
|
|
821 return rv;
|
|
822 }
|
|
823
|
|
824 rv = ngx_http_merge_locations(cf, &clcfp[i]->locations,
|
|
825 clcfp[i]->loc_conf, module, ctx_index);
|
|
826 if (rv != NGX_CONF_OK) {
|
|
827 return rv;
|
|
828 }
|
|
829 }
|
|
830
|
|
831 return NGX_CONF_OK;
|
|
832 }
|