0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
|
12 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
378
|
13 static ngx_int_t ngx_http_init_phases(ngx_conf_t *cf,
|
|
14 ngx_http_core_main_conf_t *cmcf);
|
|
15 static ngx_int_t ngx_http_init_headers_in_hash(ngx_conf_t *cf,
|
|
16 ngx_http_core_main_conf_t *cmcf);
|
|
17 static ngx_int_t ngx_http_init_phase_handlers(ngx_conf_t *cf,
|
|
18 ngx_http_core_main_conf_t *cmcf);
|
|
19
|
|
20 static ngx_int_t ngx_http_init_server_lists(ngx_conf_t *cf,
|
|
21 ngx_array_t *servers, ngx_array_t *in_ports);
|
4
|
22 static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
|
378
|
23 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_port_t *in_port,
|
|
24 ngx_http_listen_t *listen);
|
4
|
25 static ngx_int_t ngx_http_add_names(ngx_conf_t *cf,
|
378
|
26 ngx_http_core_srv_conf_t *cscf, ngx_http_conf_in_addr_t *in_addr);
|
|
27
|
0
|
28 static char *ngx_http_merge_locations(ngx_conf_t *cf,
|
378
|
29 ngx_queue_t *locations, void **loc_conf, ngx_http_module_t *module,
|
48
|
30 ngx_uint_t ctx_index);
|
378
|
31 static ngx_int_t ngx_http_init_locations(ngx_conf_t *cf,
|
|
32 ngx_http_core_srv_conf_t *cscf, ngx_http_core_loc_conf_t *pclcf);
|
|
33 static ngx_int_t ngx_http_init_static_location_trees(ngx_conf_t *cf,
|
|
34 ngx_http_core_loc_conf_t *pclcf);
|
|
35 static ngx_int_t ngx_http_cmp_locations(const ngx_queue_t *one,
|
|
36 const ngx_queue_t *two);
|
|
37 static ngx_int_t ngx_http_join_exact_locations(ngx_conf_t *cf,
|
|
38 ngx_queue_t *locations);
|
|
39 static void ngx_http_create_locations_list(ngx_queue_t *locations,
|
|
40 ngx_queue_t *q);
|
|
41 static ngx_http_location_tree_node_t *
|
|
42 ngx_http_create_locations_tree(ngx_conf_t *cf, ngx_queue_t *locations,
|
|
43 size_t prefix);
|
|
44
|
|
45 static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf,
|
|
46 ngx_http_core_main_conf_t *cmcf, ngx_array_t *in_ports);
|
372
|
47 static ngx_int_t ngx_http_cmp_conf_in_addrs(const void *one, const void *two);
|
142
|
48 static int ngx_libc_cdecl ngx_http_cmp_dns_wildcards(const void *one,
|
|
49 const void *two);
|
0
|
50
|
378
|
51 static ngx_int_t ngx_http_init_listening(ngx_conf_t *cf,
|
|
52 ngx_http_conf_in_port_t *in_port);
|
142
|
53
|
378
|
54 ngx_uint_t ngx_http_max_module;
|
0
|
55
|
|
56
|
|
57 ngx_int_t (*ngx_http_top_header_filter) (ngx_http_request_t *r);
|
|
58 ngx_int_t (*ngx_http_top_body_filter) (ngx_http_request_t *r, ngx_chain_t *ch);
|
|
59
|
|
60
|
397
|
61 ngx_str_t ngx_http_html_default_types[] = {
|
|
62 ngx_string("text/html"),
|
|
63 ngx_null_string
|
|
64 };
|
|
65
|
|
66
|
0
|
67 static ngx_command_t ngx_http_commands[] = {
|
|
68
|
48
|
69 { ngx_string("http"),
|
|
70 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
|
|
71 ngx_http_block,
|
|
72 0,
|
|
73 0,
|
|
74 NULL },
|
0
|
75
|
48
|
76 ngx_null_command
|
0
|
77 };
|
|
78
|
126
|
79
|
0
|
80 static ngx_core_module_t ngx_http_module_ctx = {
|
|
81 ngx_string("http"),
|
|
82 NULL,
|
|
83 NULL
|
126
|
84 };
|
0
|
85
|
|
86
|
|
87 ngx_module_t ngx_http_module = {
|
58
|
88 NGX_MODULE_V1,
|
0
|
89 &ngx_http_module_ctx, /* module context */
|
|
90 ngx_http_commands, /* module directives */
|
|
91 NGX_CORE_MODULE, /* module type */
|
90
|
92 NULL, /* init master */
|
0
|
93 NULL, /* init module */
|
90
|
94 NULL, /* init process */
|
|
95 NULL, /* init thread */
|
|
96 NULL, /* exit thread */
|
|
97 NULL, /* exit process */
|
|
98 NULL, /* exit master */
|
|
99 NGX_MODULE_V1_PADDING
|
0
|
100 };
|
|
101
|
|
102
|
48
|
103 static char *
|
|
104 ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
105 {
|
|
106 char *rv;
|
378
|
107 ngx_uint_t mi, m, s;
|
0
|
108 ngx_conf_t pcf;
|
378
|
109 ngx_array_t in_ports;
|
0
|
110 ngx_http_module_t *module;
|
|
111 ngx_http_conf_ctx_t *ctx;
|
|
112 ngx_http_core_loc_conf_t *clcf;
|
378
|
113 ngx_http_core_srv_conf_t **cscfp;
|
0
|
114 ngx_http_core_main_conf_t *cmcf;
|
|
115
|
|
116 /* the main http context */
|
26
|
117
|
50
|
118 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
|
|
119 if (ctx == NULL) {
|
34
|
120 return NGX_CONF_ERROR;
|
|
121 }
|
0
|
122
|
|
123 *(ngx_http_conf_ctx_t **) conf = ctx;
|
|
124
|
34
|
125
|
0
|
126 /* count the number of the http modules and set up their indices */
|
|
127
|
|
128 ngx_http_max_module = 0;
|
|
129 for (m = 0; ngx_modules[m]; m++) {
|
|
130 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
131 continue;
|
|
132 }
|
|
133
|
|
134 ngx_modules[m]->ctx_index = ngx_http_max_module++;
|
|
135 }
|
|
136
|
26
|
137
|
34
|
138 /* the http main_conf context, it is the same in the all http contexts */
|
0
|
139
|
34
|
140 ctx->main_conf = ngx_pcalloc(cf->pool,
|
|
141 sizeof(void *) * ngx_http_max_module);
|
|
142 if (ctx->main_conf == NULL) {
|
|
143 return NGX_CONF_ERROR;
|
|
144 }
|
0
|
145
|
|
146
|
34
|
147 /*
|
|
148 * the http null srv_conf context, it is used to merge
|
|
149 * the server{}s' srv_conf's
|
|
150 */
|
|
151
|
|
152 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
153 if (ctx->srv_conf == NULL) {
|
|
154 return NGX_CONF_ERROR;
|
|
155 }
|
|
156
|
|
157
|
|
158 /*
|
|
159 * the http null loc_conf context, it is used to merge
|
|
160 * the server{}s' loc_conf's
|
|
161 */
|
|
162
|
|
163 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
164 if (ctx->loc_conf == NULL) {
|
|
165 return NGX_CONF_ERROR;
|
|
166 }
|
|
167
|
|
168
|
|
169 /*
|
|
170 * create the main_conf's, the null srv_conf's, and the null loc_conf's
|
|
171 * of the all http modules
|
|
172 */
|
0
|
173
|
|
174 for (m = 0; ngx_modules[m]; m++) {
|
|
175 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
176 continue;
|
|
177 }
|
|
178
|
|
179 module = ngx_modules[m]->ctx;
|
|
180 mi = ngx_modules[m]->ctx_index;
|
|
181
|
|
182 if (module->create_main_conf) {
|
50
|
183 ctx->main_conf[mi] = module->create_main_conf(cf);
|
|
184 if (ctx->main_conf[mi] == NULL) {
|
34
|
185 return NGX_CONF_ERROR;
|
|
186 }
|
0
|
187 }
|
|
188
|
|
189 if (module->create_srv_conf) {
|
50
|
190 ctx->srv_conf[mi] = module->create_srv_conf(cf);
|
|
191 if (ctx->srv_conf[mi] == NULL) {
|
34
|
192 return NGX_CONF_ERROR;
|
|
193 }
|
0
|
194 }
|
|
195
|
|
196 if (module->create_loc_conf) {
|
50
|
197 ctx->loc_conf[mi] = module->create_loc_conf(cf);
|
|
198 if (ctx->loc_conf[mi] == NULL) {
|
34
|
199 return NGX_CONF_ERROR;
|
|
200 }
|
0
|
201 }
|
|
202 }
|
|
203
|
58
|
204 pcf = *cf;
|
|
205 cf->ctx = ctx;
|
|
206
|
|
207 for (m = 0; ngx_modules[m]; m++) {
|
|
208 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
209 continue;
|
|
210 }
|
|
211
|
|
212 module = ngx_modules[m]->ctx;
|
|
213
|
|
214 if (module->preconfiguration) {
|
|
215 if (module->preconfiguration(cf) != NGX_OK) {
|
|
216 return NGX_CONF_ERROR;
|
|
217 }
|
|
218 }
|
|
219 }
|
34
|
220
|
0
|
221 /* parse inside the http{} block */
|
|
222
|
|
223 cf->module_type = NGX_HTTP_MODULE;
|
|
224 cf->cmd_type = NGX_HTTP_MAIN_CONF;
|
|
225 rv = ngx_conf_parse(cf, NULL);
|
|
226
|
|
227 if (rv != NGX_CONF_OK) {
|
378
|
228 goto failed;
|
0
|
229 }
|
|
230
|
|
231 /*
|
|
232 * init http{} main_conf's, merge the server{}s' srv_conf's
|
|
233 * and its location{}s' loc_conf's
|
|
234 */
|
|
235
|
|
236 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
|
|
237 cscfp = cmcf->servers.elts;
|
|
238
|
|
239 for (m = 0; ngx_modules[m]; m++) {
|
|
240 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
241 continue;
|
|
242 }
|
|
243
|
|
244 module = ngx_modules[m]->ctx;
|
|
245 mi = ngx_modules[m]->ctx_index;
|
|
246
|
|
247 /* init http{} main_conf's */
|
|
248
|
|
249 if (module->init_main_conf) {
|
|
250 rv = module->init_main_conf(cf, ctx->main_conf[mi]);
|
|
251 if (rv != NGX_CONF_OK) {
|
378
|
252 goto failed;
|
0
|
253 }
|
|
254 }
|
|
255
|
|
256 for (s = 0; s < cmcf->servers.nelts; s++) {
|
|
257
|
|
258 /* merge the server{}s' srv_conf's */
|
|
259
|
|
260 if (module->merge_srv_conf) {
|
378
|
261 rv = module->merge_srv_conf(cf, ctx->srv_conf[mi],
|
0
|
262 cscfp[s]->ctx->srv_conf[mi]);
|
|
263 if (rv != NGX_CONF_OK) {
|
378
|
264 goto failed;
|
0
|
265 }
|
|
266 }
|
|
267
|
|
268 if (module->merge_loc_conf) {
|
|
269
|
|
270 /* merge the server{}'s loc_conf */
|
|
271
|
378
|
272 rv = module->merge_loc_conf(cf, ctx->loc_conf[mi],
|
0
|
273 cscfp[s]->ctx->loc_conf[mi]);
|
|
274 if (rv != NGX_CONF_OK) {
|
378
|
275 goto failed;
|
0
|
276 }
|
|
277
|
|
278 /* merge the locations{}' loc_conf's */
|
|
279
|
378
|
280 clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];
|
|
281
|
|
282 rv = ngx_http_merge_locations(cf, clcf->locations,
|
0
|
283 cscfp[s]->ctx->loc_conf,
|
|
284 module, mi);
|
|
285 if (rv != NGX_CONF_OK) {
|
378
|
286 goto failed;
|
0
|
287 }
|
|
288 }
|
|
289 }
|
|
290 }
|
|
291
|
34
|
292
|
378
|
293 /* create location trees */
|
34
|
294
|
378
|
295 for (s = 0; s < cmcf->servers.nelts; s++) {
|
122
|
296
|
378
|
297 clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];
|
0
|
298
|
378
|
299 if (ngx_http_init_locations(cf, cscfp[s], clcf) != NGX_OK) {
|
|
300 return NGX_CONF_ERROR;
|
|
301 }
|
122
|
302
|
378
|
303 if (ngx_http_init_static_location_trees(cf, clcf) != NGX_OK) {
|
|
304 return NGX_CONF_ERROR;
|
|
305 }
|
34
|
306 }
|
|
307
|
0
|
308
|
378
|
309 if (ngx_http_init_phases(cf, cmcf) != NGX_OK) {
|
130
|
310 return NGX_CONF_ERROR;
|
|
311 }
|
|
312
|
378
|
313 if (ngx_http_init_headers_in_hash(cf, cmcf) != NGX_OK) {
|
198
|
314 return NGX_CONF_ERROR;
|
|
315 }
|
|
316
|
58
|
317
|
|
318 for (m = 0; ngx_modules[m]; m++) {
|
|
319 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
320 continue;
|
|
321 }
|
|
322
|
|
323 module = ngx_modules[m]->ctx;
|
|
324
|
|
325 if (module->postconfiguration) {
|
|
326 if (module->postconfiguration(cf) != NGX_OK) {
|
|
327 return NGX_CONF_ERROR;
|
|
328 }
|
|
329 }
|
|
330 }
|
|
331
|
122
|
332 if (ngx_http_variables_init_vars(cf) != NGX_OK) {
|
|
333 return NGX_CONF_ERROR;
|
|
334 }
|
58
|
335
|
|
336 /*
|
|
337 * http{}'s cf->ctx was needed while the configuration merging
|
|
338 * and in postconfiguration process
|
|
339 */
|
|
340
|
|
341 *cf = pcf;
|
|
342
|
56
|
343
|
378
|
344 if (ngx_http_init_phase_handlers(cf, cmcf) != NGX_OK) {
|
|
345 return NGX_CONF_ERROR;
|
|
346 }
|
|
347
|
|
348
|
|
349 /*
|
|
350 * create the lists of ports, addresses and server names
|
|
351 * to find quickly the server core module configuration at run-time
|
|
352 */
|
|
353
|
|
354 /* AF_INET only */
|
|
355
|
|
356 if (ngx_http_init_server_lists(cf, &cmcf->servers, &in_ports) != NGX_OK) {
|
|
357 return NGX_CONF_ERROR;
|
|
358 }
|
|
359
|
|
360
|
|
361 /* optimize the lists of ports, addresses and server names */
|
|
362
|
|
363 /* AF_INET only */
|
|
364
|
|
365 if (ngx_http_optimize_servers(cf, cmcf, &in_ports) != NGX_OK) {
|
|
366 return NGX_CONF_ERROR;
|
|
367 }
|
|
368
|
|
369 return NGX_CONF_OK;
|
|
370
|
|
371 failed:
|
|
372
|
|
373 *cf = pcf;
|
|
374
|
|
375 return rv;
|
|
376 }
|
|
377
|
|
378
|
|
379 static ngx_int_t
|
|
380 ngx_http_init_phases(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
|
|
381 {
|
|
382 if (ngx_array_init(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers,
|
|
383 cf->pool, 1, sizeof(ngx_http_handler_pt))
|
|
384 != NGX_OK)
|
|
385 {
|
|
386 return NGX_ERROR;
|
|
387 }
|
|
388
|
|
389 if (ngx_array_init(&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers,
|
|
390 cf->pool, 1, sizeof(ngx_http_handler_pt))
|
|
391 != NGX_OK)
|
|
392 {
|
|
393 return NGX_ERROR;
|
|
394 }
|
|
395
|
|
396 if (ngx_array_init(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers,
|
|
397 cf->pool, 1, sizeof(ngx_http_handler_pt))
|
|
398 != NGX_OK)
|
|
399 {
|
|
400 return NGX_ERROR;
|
|
401 }
|
|
402
|
|
403 if (ngx_array_init(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers,
|
|
404 cf->pool, 1, sizeof(ngx_http_handler_pt))
|
|
405 != NGX_OK)
|
|
406 {
|
|
407 return NGX_ERROR;
|
|
408 }
|
|
409
|
|
410 if (ngx_array_init(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers,
|
|
411 cf->pool, 2, sizeof(ngx_http_handler_pt))
|
|
412 != NGX_OK)
|
|
413 {
|
|
414 return NGX_ERROR;
|
|
415 }
|
|
416
|
|
417 if (ngx_array_init(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers,
|
|
418 cf->pool, 4, sizeof(ngx_http_handler_pt))
|
|
419 != NGX_OK)
|
|
420 {
|
|
421 return NGX_ERROR;
|
|
422 }
|
|
423
|
|
424 if (ngx_array_init(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers,
|
|
425 cf->pool, 1, sizeof(ngx_http_handler_pt))
|
|
426 != NGX_OK)
|
|
427 {
|
|
428 return NGX_ERROR;
|
|
429 }
|
|
430
|
|
431 return NGX_OK;
|
|
432 }
|
|
433
|
|
434
|
|
435 static ngx_int_t
|
|
436 ngx_http_init_headers_in_hash(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
|
|
437 {
|
|
438 ngx_array_t headers_in;
|
|
439 ngx_hash_key_t *hk;
|
|
440 ngx_hash_init_t hash;
|
|
441 ngx_http_header_t *header;
|
|
442
|
|
443 if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t))
|
|
444 != NGX_OK)
|
|
445 {
|
|
446 return NGX_ERROR;
|
|
447 }
|
|
448
|
|
449 for (header = ngx_http_headers_in; header->name.len; header++) {
|
|
450 hk = ngx_array_push(&headers_in);
|
|
451 if (hk == NULL) {
|
|
452 return NGX_ERROR;
|
|
453 }
|
|
454
|
|
455 hk->key = header->name;
|
|
456 hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len);
|
|
457 hk->value = header;
|
|
458 }
|
|
459
|
|
460 hash.hash = &cmcf->headers_in_hash;
|
|
461 hash.key = ngx_hash_key_lc;
|
|
462 hash.max_size = 512;
|
|
463 hash.bucket_size = ngx_align(64, ngx_cacheline_size);
|
|
464 hash.name = "headers_in_hash";
|
|
465 hash.pool = cf->pool;
|
|
466 hash.temp_pool = NULL;
|
|
467
|
|
468 if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) {
|
|
469 return NGX_ERROR;
|
|
470 }
|
|
471
|
|
472 return NGX_OK;
|
|
473 }
|
|
474
|
|
475
|
|
476 static ngx_int_t
|
|
477 ngx_http_init_phase_handlers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf)
|
|
478 {
|
|
479 ngx_int_t j;
|
|
480 ngx_uint_t i, n;
|
|
481 ngx_uint_t find_config_index, use_rewrite, use_access;
|
|
482 ngx_http_handler_pt *h;
|
|
483 ngx_http_phase_handler_t *ph;
|
|
484 ngx_http_phase_handler_pt checker;
|
|
485
|
230
|
486 cmcf->phase_engine.server_rewrite_index = (ngx_uint_t) -1;
|
324
|
487 cmcf->phase_engine.location_rewrite_index = (ngx_uint_t) -1;
|
230
|
488 find_config_index = 0;
|
|
489 use_rewrite = cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers.nelts ? 1 : 0;
|
|
490 use_access = cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers.nelts ? 1 : 0;
|
|
491
|
|
492 n = use_rewrite + use_access + 1; /* find config phase */
|
|
493
|
|
494 for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
|
|
495 n += cmcf->phases[i].handlers.nelts;
|
|
496 }
|
|
497
|
|
498 ph = ngx_pcalloc(cf->pool,
|
|
499 n * sizeof(ngx_http_phase_handler_t) + sizeof(void *));
|
|
500 if (ph == NULL) {
|
378
|
501 return NGX_ERROR;
|
230
|
502 }
|
|
503
|
|
504 cmcf->phase_engine.handlers = ph;
|
|
505 n = 0;
|
|
506
|
|
507 for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
|
|
508 h = cmcf->phases[i].handlers.elts;
|
|
509
|
|
510 switch (i) {
|
|
511
|
|
512 case NGX_HTTP_SERVER_REWRITE_PHASE:
|
|
513 if (cmcf->phase_engine.server_rewrite_index == (ngx_uint_t) -1) {
|
|
514 cmcf->phase_engine.server_rewrite_index = n;
|
|
515 }
|
|
516 checker = ngx_http_core_generic_phase;
|
|
517
|
|
518 break;
|
|
519
|
|
520 case NGX_HTTP_FIND_CONFIG_PHASE:
|
|
521 find_config_index = n;
|
|
522
|
|
523 ph->checker = ngx_http_core_find_config_phase;
|
|
524 n++;
|
|
525 ph++;
|
|
526
|
|
527 continue;
|
|
528
|
324
|
529 case NGX_HTTP_REWRITE_PHASE:
|
|
530 if (cmcf->phase_engine.location_rewrite_index == (ngx_uint_t) -1) {
|
|
531 cmcf->phase_engine.location_rewrite_index = n;
|
|
532 }
|
|
533 checker = ngx_http_core_generic_phase;
|
|
534
|
|
535 break;
|
|
536
|
230
|
537 case NGX_HTTP_POST_REWRITE_PHASE:
|
|
538 if (use_rewrite) {
|
|
539 ph->checker = ngx_http_core_post_rewrite_phase;
|
|
540 ph->next = find_config_index;
|
|
541 n++;
|
|
542 ph++;
|
|
543 }
|
|
544
|
|
545 continue;
|
|
546
|
|
547 case NGX_HTTP_ACCESS_PHASE:
|
|
548 checker = ngx_http_core_access_phase;
|
|
549 n++;
|
|
550 break;
|
|
551
|
|
552 case NGX_HTTP_POST_ACCESS_PHASE:
|
|
553 if (use_access) {
|
|
554 ph->checker = ngx_http_core_post_access_phase;
|
|
555 ph->next = n;
|
|
556 ph++;
|
|
557 }
|
|
558
|
|
559 continue;
|
|
560
|
|
561 case NGX_HTTP_CONTENT_PHASE:
|
|
562 checker = ngx_http_core_content_phase;
|
|
563 break;
|
|
564
|
|
565 default:
|
|
566 checker = ngx_http_core_generic_phase;
|
|
567 }
|
|
568
|
|
569 n += cmcf->phases[i].handlers.nelts;
|
|
570
|
|
571 for (j = cmcf->phases[i].handlers.nelts - 1; j >=0; j--) {
|
|
572 ph->checker = checker;
|
|
573 ph->handler = h[j];
|
|
574 ph->next = n;
|
|
575 ph++;
|
|
576 }
|
|
577 }
|
|
578
|
378
|
579 return NGX_OK;
|
|
580 }
|
230
|
581
|
378
|
582
|
|
583 static char *
|
|
584 ngx_http_merge_locations(ngx_conf_t *cf, ngx_queue_t *locations,
|
|
585 void **loc_conf, ngx_http_module_t *module, ngx_uint_t ctx_index)
|
|
586 {
|
|
587 char *rv;
|
|
588 ngx_queue_t *q;
|
|
589 ngx_http_core_loc_conf_t *clcf;
|
|
590 ngx_http_location_queue_t *lq;
|
|
591
|
|
592 if (locations == NULL) {
|
|
593 return NGX_CONF_OK;
|
|
594 }
|
|
595
|
|
596 for (q = ngx_queue_head(locations);
|
|
597 q != ngx_queue_sentinel(locations);
|
|
598 q = ngx_queue_next(q))
|
|
599 {
|
|
600 lq = (ngx_http_location_queue_t *) q;
|
|
601
|
|
602 clcf = lq->exact ? lq->exact : lq->inclusive;
|
|
603
|
|
604 rv = module->merge_loc_conf(cf, loc_conf[ctx_index],
|
|
605 clcf->loc_conf[ctx_index]);
|
|
606 if (rv != NGX_CONF_OK) {
|
|
607 return rv;
|
|
608 }
|
|
609
|
|
610 rv = ngx_http_merge_locations(cf, clcf->locations, clcf->loc_conf,
|
|
611 module, ctx_index);
|
|
612 if (rv != NGX_CONF_OK) {
|
|
613 return rv;
|
|
614 }
|
|
615 }
|
|
616
|
|
617 return NGX_CONF_OK;
|
|
618 }
|
|
619
|
|
620
|
|
621 static ngx_int_t
|
|
622 ngx_http_init_locations(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
|
|
623 ngx_http_core_loc_conf_t *pclcf)
|
|
624 {
|
|
625 ngx_uint_t n;
|
|
626 ngx_queue_t *q, *locations, *named, tail;
|
|
627 ngx_http_core_loc_conf_t *clcf;
|
|
628 ngx_http_location_queue_t *lq;
|
|
629 ngx_http_core_loc_conf_t **clcfp;
|
|
630 #if (NGX_PCRE)
|
|
631 ngx_uint_t r;
|
|
632 ngx_queue_t *regex;
|
|
633 #endif
|
|
634
|
|
635 locations = pclcf->locations;
|
|
636
|
|
637 if (locations == NULL) {
|
|
638 return NGX_OK;
|
|
639 }
|
|
640
|
|
641 ngx_queue_sort(locations, ngx_http_cmp_locations);
|
|
642
|
|
643 named = NULL;
|
|
644 n = 0;
|
|
645 #if (NGX_PCRE)
|
|
646 regex = NULL;
|
|
647 r = 0;
|
|
648 #endif
|
|
649
|
|
650 for (q = ngx_queue_head(locations);
|
|
651 q != ngx_queue_sentinel(locations);
|
|
652 q = ngx_queue_next(q))
|
|
653 {
|
|
654 lq = (ngx_http_location_queue_t *) q;
|
|
655
|
|
656 clcf = lq->exact ? lq->exact : lq->inclusive;
|
|
657
|
|
658 if (ngx_http_init_locations(cf, NULL, clcf) != NGX_OK) {
|
|
659 return NGX_ERROR;
|
|
660 }
|
|
661
|
|
662 #if (NGX_PCRE)
|
|
663
|
|
664 if (clcf->regex) {
|
|
665 r++;
|
|
666
|
|
667 if (regex == NULL) {
|
|
668 regex = q;
|
|
669 }
|
|
670
|
|
671 continue;
|
|
672 }
|
|
673
|
|
674 #endif
|
|
675
|
|
676 if (clcf->named) {
|
|
677 n++;
|
|
678
|
|
679 if (named == NULL) {
|
|
680 named = q;
|
|
681 }
|
|
682
|
|
683 continue;
|
|
684 }
|
|
685
|
|
686 if (clcf->noname) {
|
|
687 break;
|
|
688 }
|
|
689 }
|
|
690
|
|
691 if (q != ngx_queue_sentinel(locations)) {
|
|
692 ngx_queue_split(locations, q, &tail);
|
|
693 }
|
|
694
|
|
695 if (named) {
|
|
696 clcfp = ngx_palloc(cf->pool,
|
|
697 (n + 1) * sizeof(ngx_http_core_loc_conf_t **));
|
|
698 if (clcfp == NULL) {
|
|
699 return NGX_ERROR;
|
|
700 }
|
|
701
|
|
702 cscf->named_locations = clcfp;
|
|
703
|
|
704 for (q = named;
|
|
705 q != ngx_queue_sentinel(locations);
|
|
706 q = ngx_queue_next(q))
|
|
707 {
|
|
708 lq = (ngx_http_location_queue_t *) q;
|
|
709
|
|
710 *(clcfp++) = lq->exact;
|
|
711 }
|
|
712
|
|
713 *clcfp = NULL;
|
|
714
|
|
715 ngx_queue_split(locations, named, &tail);
|
|
716 }
|
|
717
|
|
718 #if (NGX_PCRE)
|
|
719
|
|
720 if (regex) {
|
|
721
|
|
722 clcfp = ngx_palloc(cf->pool,
|
|
723 (r + 1) * sizeof(ngx_http_core_loc_conf_t **));
|
|
724 if (clcfp == NULL) {
|
|
725 return NGX_ERROR;
|
|
726 }
|
|
727
|
|
728 pclcf->regex_locations = clcfp;
|
|
729
|
|
730 for (q = regex;
|
|
731 q != ngx_queue_sentinel(locations);
|
|
732 q = ngx_queue_next(q))
|
|
733 {
|
|
734 lq = (ngx_http_location_queue_t *) q;
|
|
735
|
|
736 *(clcfp++) = lq->exact;
|
|
737 }
|
|
738
|
|
739 *clcfp = NULL;
|
|
740
|
|
741 ngx_queue_split(locations, regex, &tail);
|
|
742 }
|
|
743
|
|
744 #endif
|
|
745
|
|
746 return NGX_OK;
|
|
747 }
|
|
748
|
|
749
|
|
750 static ngx_int_t
|
|
751 ngx_http_init_static_location_trees(ngx_conf_t *cf,
|
|
752 ngx_http_core_loc_conf_t *pclcf)
|
|
753 {
|
|
754 ngx_queue_t *q, *locations;
|
|
755 ngx_http_core_loc_conf_t *clcf;
|
|
756 ngx_http_location_queue_t *lq;
|
|
757
|
|
758 locations = pclcf->locations;
|
|
759
|
|
760 if (locations == NULL) {
|
|
761 return NGX_OK;
|
|
762 }
|
|
763
|
|
764 if (ngx_queue_empty(locations)) {
|
|
765 return NGX_OK;
|
|
766 }
|
|
767
|
|
768 for (q = ngx_queue_head(locations);
|
|
769 q != ngx_queue_sentinel(locations);
|
|
770 q = ngx_queue_next(q))
|
|
771 {
|
|
772 lq = (ngx_http_location_queue_t *) q;
|
|
773
|
|
774 clcf = lq->exact ? lq->exact : lq->inclusive;
|
|
775
|
|
776 if (ngx_http_init_static_location_trees(cf, clcf) != NGX_OK) {
|
|
777 return NGX_ERROR;
|
|
778 }
|
|
779 }
|
|
780
|
|
781 if (ngx_http_join_exact_locations(cf, locations) != NGX_OK) {
|
|
782 return NGX_ERROR;
|
|
783 }
|
|
784
|
|
785 ngx_http_create_locations_list(locations, ngx_queue_head(locations));
|
|
786
|
|
787 pclcf->static_locations = ngx_http_create_locations_tree(cf, locations, 0);
|
|
788 if (pclcf->static_locations == NULL) {
|
|
789 return NGX_ERROR;
|
|
790 }
|
|
791
|
|
792 return NGX_OK;
|
|
793 }
|
|
794
|
|
795
|
|
796 ngx_int_t
|
|
797 ngx_http_add_location(ngx_conf_t *cf, ngx_queue_t **locations,
|
|
798 ngx_http_core_loc_conf_t *clcf)
|
|
799 {
|
|
800 ngx_http_location_queue_t *lq;
|
|
801
|
|
802 if (*locations == NULL) {
|
|
803 *locations = ngx_palloc(cf->temp_pool,
|
|
804 sizeof(ngx_http_location_queue_t));
|
|
805 if (*locations == NULL) {
|
|
806 return NGX_ERROR;
|
|
807 }
|
|
808
|
|
809 ngx_queue_init(*locations);
|
|
810 }
|
|
811
|
|
812 lq = ngx_palloc(cf->temp_pool, sizeof(ngx_http_location_queue_t));
|
|
813 if (lq == NULL) {
|
|
814 return NGX_ERROR;
|
|
815 }
|
|
816
|
|
817 if (clcf->exact_match
|
|
818 #if (NGX_PCRE)
|
|
819 || clcf->regex
|
|
820 #endif
|
|
821 || clcf->named || clcf->noname)
|
|
822 {
|
|
823 lq->exact = clcf;
|
|
824 lq->inclusive = NULL;
|
|
825
|
|
826 } else {
|
|
827 lq->exact = NULL;
|
|
828 lq->inclusive = clcf;
|
|
829 }
|
|
830
|
|
831 lq->name = &clcf->name;
|
|
832 lq->file_name = cf->conf_file->file.name.data;
|
|
833 lq->line = cf->conf_file->line;
|
|
834
|
|
835 ngx_queue_init(&lq->list);
|
|
836
|
|
837 ngx_queue_insert_tail(*locations, &lq->queue);
|
0
|
838
|
378
|
839 return NGX_OK;
|
|
840 }
|
|
841
|
|
842
|
|
843 static ngx_int_t
|
|
844 ngx_http_cmp_locations(const ngx_queue_t *one, const ngx_queue_t *two)
|
|
845 {
|
|
846 ngx_int_t rc;
|
|
847 ngx_http_core_loc_conf_t *first, *second;
|
|
848 ngx_http_location_queue_t *lq1, *lq2;
|
|
849
|
|
850 lq1 = (ngx_http_location_queue_t *) one;
|
|
851 lq2 = (ngx_http_location_queue_t *) two;
|
|
852
|
|
853 first = lq1->exact ? lq1->exact : lq1->inclusive;
|
|
854 second = lq2->exact ? lq2->exact : lq2->inclusive;
|
|
855
|
|
856 if (first->noname && !second->noname) {
|
|
857 /* shift no named locations to the end */
|
|
858 return 1;
|
|
859 }
|
|
860
|
|
861 if (!first->noname && second->noname) {
|
|
862 /* shift no named locations to the end */
|
|
863 return -1;
|
|
864 }
|
|
865
|
|
866 if (first->noname || second->noname) {
|
|
867 /* do not sort no named locations */
|
|
868 return 0;
|
|
869 }
|
|
870
|
|
871 if (first->named && !second->named) {
|
|
872 /* shift named locations to the end */
|
|
873 return 1;
|
|
874 }
|
|
875
|
|
876 if (!first->named && second->named) {
|
|
877 /* shift named locations to the end */
|
|
878 return -1;
|
|
879 }
|
|
880
|
|
881 if (first->named && second->named) {
|
|
882 return ngx_strcmp(first->name.data, second->name.data);
|
|
883 }
|
|
884
|
|
885 #if (NGX_PCRE)
|
|
886
|
|
887 if (first->regex && !second->regex) {
|
|
888 /* shift the regex matches to the end */
|
|
889 return 1;
|
|
890 }
|
|
891
|
|
892 if (!first->regex && second->regex) {
|
|
893 /* shift the regex matches to the end */
|
|
894 return -1;
|
|
895 }
|
|
896
|
|
897 if (first->regex || second->regex) {
|
|
898 /* do not sort the regex matches */
|
|
899 return 0;
|
|
900 }
|
|
901
|
|
902 #endif
|
|
903
|
|
904 rc = ngx_strcmp(first->name.data, second->name.data);
|
|
905
|
|
906 if (rc == 0 && !first->exact_match && second->exact_match) {
|
|
907 /* an exact match must be before the same inclusive one */
|
|
908 return 1;
|
|
909 }
|
|
910
|
|
911 return rc;
|
|
912 }
|
|
913
|
|
914
|
|
915 static ngx_int_t
|
|
916 ngx_http_join_exact_locations(ngx_conf_t *cf, ngx_queue_t *locations)
|
|
917 {
|
|
918 ngx_queue_t *q, *x;
|
|
919 ngx_http_location_queue_t *lq, *lx;
|
|
920
|
|
921 q = ngx_queue_head(locations);
|
|
922
|
|
923 while (q != ngx_queue_last(locations)) {
|
|
924
|
|
925 x = ngx_queue_next(q);
|
|
926
|
|
927 lq = (ngx_http_location_queue_t *) q;
|
|
928 lx = (ngx_http_location_queue_t *) x;
|
|
929
|
|
930 if (ngx_strcmp(lq->name->data, lx->name->data) == 0) {
|
|
931
|
|
932 if ((lq->exact && lx->exact) || (lq->inclusive && lx->inclusive)) {
|
|
933 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
934 "duplicate location \"%V\" in %s:%ui",
|
|
935 lx->name, lx->file_name, lx->line);
|
|
936
|
|
937 return NGX_ERROR;
|
|
938 }
|
|
939
|
|
940 lq->inclusive = lx->inclusive;
|
|
941
|
|
942 ngx_queue_remove(x);
|
|
943
|
|
944 continue;
|
|
945 }
|
|
946
|
|
947 q = ngx_queue_next(q);
|
|
948 }
|
|
949
|
|
950 return NGX_OK;
|
|
951 }
|
|
952
|
|
953
|
|
954 static void
|
|
955 ngx_http_create_locations_list(ngx_queue_t *locations, ngx_queue_t *q)
|
|
956 {
|
|
957 u_char *name;
|
|
958 size_t len;
|
|
959 ngx_queue_t *x, tail;
|
|
960 ngx_http_location_queue_t *lq, *lx;
|
|
961
|
|
962 if (q == ngx_queue_last(locations)) {
|
|
963 return;
|
|
964 }
|
|
965
|
|
966 lq = (ngx_http_location_queue_t *) q;
|
|
967
|
|
968 if (lq->inclusive == NULL) {
|
|
969 ngx_http_create_locations_list(locations, ngx_queue_next(q));
|
|
970 return;
|
|
971 }
|
|
972
|
|
973 len = lq->name->len;
|
|
974 name = lq->name->data;
|
|
975
|
|
976 for (x = ngx_queue_next(q);
|
|
977 x != ngx_queue_sentinel(locations);
|
|
978 x = ngx_queue_next(x))
|
|
979 {
|
|
980 lx = (ngx_http_location_queue_t *) x;
|
|
981
|
|
982 if (len > lx->name->len
|
|
983 || (ngx_strncmp(name, lx->name->data, len) != 0))
|
|
984 {
|
|
985 break;
|
|
986 }
|
|
987 }
|
|
988
|
|
989 q = ngx_queue_next(q);
|
|
990
|
|
991 if (q == x) {
|
|
992 ngx_http_create_locations_list(locations, x);
|
|
993 return;
|
|
994 }
|
|
995
|
|
996 ngx_queue_split(locations, q, &tail);
|
|
997 ngx_queue_add(&lq->list, &tail);
|
|
998
|
|
999 if (x == ngx_queue_sentinel(locations)) {
|
|
1000 ngx_http_create_locations_list(&lq->list, ngx_queue_head(&lq->list));
|
|
1001 return;
|
|
1002 }
|
|
1003
|
|
1004 ngx_queue_split(&lq->list, x, &tail);
|
|
1005 ngx_queue_add(locations, &tail);
|
|
1006
|
|
1007 ngx_http_create_locations_list(&lq->list, ngx_queue_head(&lq->list));
|
|
1008
|
|
1009 ngx_http_create_locations_list(locations, x);
|
|
1010 }
|
|
1011
|
|
1012
|
|
1013 /*
|
|
1014 * to keep cache locality for left leaf nodes, allocate nodes in following
|
|
1015 * order: node, left subtree, right subtree, inclusive subtree
|
|
1016 */
|
|
1017
|
|
1018 static ngx_http_location_tree_node_t *
|
|
1019 ngx_http_create_locations_tree(ngx_conf_t *cf, ngx_queue_t *locations,
|
|
1020 size_t prefix)
|
|
1021 {
|
|
1022 size_t len;
|
|
1023 ngx_queue_t *q, tail;
|
|
1024 ngx_http_location_queue_t *lq;
|
|
1025 ngx_http_location_tree_node_t *node;
|
|
1026
|
|
1027 q = ngx_queue_middle(locations);
|
|
1028
|
|
1029 lq = (ngx_http_location_queue_t *) q;
|
|
1030 len = lq->name->len - prefix;
|
|
1031
|
382
|
1032 node = ngx_palloc(cf->pool,
|
|
1033 offsetof(ngx_http_location_tree_node_t, name) + len);
|
378
|
1034 if (node == NULL) {
|
|
1035 return NULL;
|
|
1036 }
|
|
1037
|
380
|
1038 node->left = NULL;
|
|
1039 node->right = NULL;
|
|
1040 node->tree = NULL;
|
378
|
1041 node->exact = lq->exact;
|
|
1042 node->inclusive = lq->inclusive;
|
|
1043
|
|
1044 node->auto_redirect = (u_char) ((lq->exact && lq->exact->auto_redirect)
|
|
1045 || (lq->inclusive && lq->inclusive->auto_redirect));
|
|
1046
|
|
1047 node->len = (u_char) len;
|
|
1048 ngx_memcpy(node->name, &lq->name->data[prefix], len);
|
|
1049
|
|
1050 ngx_queue_split(locations, q, &tail);
|
|
1051
|
|
1052 if (ngx_queue_empty(locations)) {
|
|
1053 /*
|
|
1054 * ngx_queue_split() insures that if left part is empty,
|
|
1055 * then right one is empty too
|
|
1056 */
|
|
1057 goto inclusive;
|
|
1058 }
|
|
1059
|
|
1060 node->left = ngx_http_create_locations_tree(cf, locations, prefix);
|
|
1061 if (node->left == NULL) {
|
|
1062 return NULL;
|
|
1063 }
|
|
1064
|
|
1065 ngx_queue_remove(q);
|
|
1066
|
|
1067 if (ngx_queue_empty(&tail)) {
|
|
1068 goto inclusive;
|
|
1069 }
|
|
1070
|
|
1071 node->right = ngx_http_create_locations_tree(cf, &tail, prefix);
|
|
1072 if (node->right == NULL) {
|
|
1073 return NULL;
|
|
1074 }
|
|
1075
|
|
1076 inclusive:
|
|
1077
|
|
1078 if (ngx_queue_empty(&lq->list)) {
|
|
1079 return node;
|
|
1080 }
|
|
1081
|
|
1082 node->tree = ngx_http_create_locations_tree(cf, &lq->list, prefix + len);
|
|
1083 if (node->tree == NULL) {
|
|
1084 return NULL;
|
|
1085 }
|
|
1086
|
|
1087 return node;
|
|
1088 }
|
|
1089
|
|
1090
|
|
1091 static ngx_int_t
|
|
1092 ngx_http_init_server_lists(ngx_conf_t *cf, ngx_array_t *servers,
|
|
1093 ngx_array_t *in_ports)
|
|
1094 {
|
|
1095 ngx_uint_t s, l, p, a;
|
|
1096 ngx_http_listen_t *listen;
|
|
1097 ngx_http_conf_in_port_t *in_port;
|
|
1098 ngx_http_conf_in_addr_t *in_addr;
|
|
1099 ngx_http_core_srv_conf_t **cscfp;
|
|
1100
|
|
1101 if (ngx_array_init(in_ports, cf->temp_pool, 2,
|
142
|
1102 sizeof(ngx_http_conf_in_port_t))
|
50
|
1103 != NGX_OK)
|
4
|
1104 {
|
378
|
1105 return NGX_ERROR;
|
4
|
1106 }
|
0
|
1107
|
|
1108 /* "server" directives */
|
4
|
1109
|
378
|
1110 cscfp = servers->elts;
|
|
1111 for (s = 0; s < servers->nelts; s++) {
|
0
|
1112
|
|
1113 /* "listen" directives */
|
4
|
1114
|
378
|
1115 listen = cscfp[s]->listen.elts;
|
0
|
1116 for (l = 0; l < cscfp[s]->listen.nelts; l++) {
|
|
1117
|
|
1118 /* AF_INET only */
|
|
1119
|
378
|
1120 in_port = in_ports->elts;
|
|
1121 for (p = 0; p < in_ports->nelts; p++) {
|
0
|
1122
|
378
|
1123 if (listen[l].port != in_port[p].port) {
|
142
|
1124 continue;
|
|
1125 }
|
0
|
1126
|
142
|
1127 /* the port is already in the port list */
|
0
|
1128
|
142
|
1129 in_addr = in_port[p].addrs.elts;
|
|
1130 for (a = 0; a < in_port[p].addrs.nelts; a++) {
|
0
|
1131
|
378
|
1132 if (listen[l].addr != in_addr[a].addr) {
|
142
|
1133 continue;
|
|
1134 }
|
0
|
1135
|
142
|
1136 /* the address is already in the address list */
|
0
|
1137
|
378
|
1138 if (ngx_http_add_names(cf, cscfp[s], &in_addr[a]) != NGX_OK)
|
142
|
1139 {
|
378
|
1140 return NGX_ERROR;
|
142
|
1141 }
|
0
|
1142
|
142
|
1143 /*
|
|
1144 * check the duplicate "default" server
|
|
1145 * for this address:port
|
|
1146 */
|
0
|
1147
|
378
|
1148 if (listen[l].conf.default_server) {
|
4
|
1149
|
142
|
1150 if (in_addr[a].default_server) {
|
|
1151 ngx_log_error(NGX_LOG_ERR, cf->log, 0,
|
336
|
1152 "the duplicate default server in %s:%ui",
|
378
|
1153 listen[l].file_name, listen[l].line);
|
0
|
1154
|
378
|
1155 return NGX_ERROR;
|
142
|
1156 }
|
4
|
1157
|
142
|
1158 in_addr[a].core_srv_conf = cscfp[s];
|
|
1159 in_addr[a].default_server = 1;
|
410
|
1160 #if (NGX_HTTP_SSL)
|
|
1161 in_addr[a].ssl = listen[l].conf.ssl;
|
|
1162 #endif
|
|
1163 in_addr[a].listen_conf = &listen[l].conf;
|
0
|
1164 }
|
|
1165
|
142
|
1166 goto found;
|
4
|
1167 }
|
0
|
1168
|
142
|
1169 /*
|
|
1170 * add the address to the addresses list that
|
|
1171 * bound to this port
|
|
1172 */
|
0
|
1173
|
378
|
1174 if (ngx_http_add_address(cf, cscfp[s], &in_port[p], &listen[l])
|
50
|
1175 != NGX_OK)
|
4
|
1176 {
|
378
|
1177 return NGX_ERROR;
|
4
|
1178 }
|
142
|
1179
|
|
1180 goto found;
|
0
|
1181 }
|
142
|
1182
|
|
1183 /* add the port to the in_port list */
|
|
1184
|
378
|
1185 in_port = ngx_array_push(in_ports);
|
142
|
1186 if (in_port == NULL) {
|
378
|
1187 return NGX_ERROR;
|
142
|
1188 }
|
|
1189
|
378
|
1190 in_port->port = listen[l].port;
|
142
|
1191 in_port->addrs.elts = NULL;
|
|
1192
|
378
|
1193 if (ngx_http_add_address(cf, cscfp[s], in_port, &listen[l])
|
|
1194 != NGX_OK)
|
142
|
1195 {
|
378
|
1196 return NGX_ERROR;
|
142
|
1197 }
|
|
1198
|
|
1199 found:
|
|
1200
|
|
1201 continue;
|
0
|
1202 }
|
|
1203 }
|
|
1204
|
378
|
1205 return NGX_OK;
|
0
|
1206 }
|
|
1207
|
|
1208
|
4
|
1209 /*
|
|
1210 * add the server address, the server names and the server core module
|
|
1211 * configurations to the port (in_port)
|
|
1212 */
|
|
1213
|
48
|
1214 static ngx_int_t
|
378
|
1215 ngx_http_add_address(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
|
|
1216 ngx_http_conf_in_port_t *in_port, ngx_http_listen_t *listen)
|
4
|
1217 {
|
142
|
1218 ngx_http_conf_in_addr_t *in_addr;
|
4
|
1219
|
|
1220 if (in_port->addrs.elts == NULL) {
|
142
|
1221 if (ngx_array_init(&in_port->addrs, cf->temp_pool, 4,
|
326
|
1222 sizeof(ngx_http_conf_in_addr_t))
|
|
1223 != NGX_OK)
|
4
|
1224 {
|
|
1225 return NGX_ERROR;
|
|
1226 }
|
|
1227 }
|
|
1228
|
50
|
1229 in_addr = ngx_array_push(&in_port->addrs);
|
|
1230 if (in_addr == NULL) {
|
4
|
1231 return NGX_ERROR;
|
|
1232 }
|
|
1233
|
378
|
1234 in_addr->addr = listen->addr;
|
142
|
1235 in_addr->hash.buckets = NULL;
|
|
1236 in_addr->hash.size = 0;
|
312
|
1237 in_addr->wc_head = NULL;
|
|
1238 in_addr->wc_tail = NULL;
|
4
|
1239 in_addr->names.elts = NULL;
|
326
|
1240 #if (NGX_PCRE)
|
|
1241 in_addr->nregex = 0;
|
|
1242 in_addr->regex = NULL;
|
|
1243 #endif
|
4
|
1244 in_addr->core_srv_conf = cscf;
|
378
|
1245 in_addr->default_server = listen->conf.default_server;
|
|
1246 in_addr->bind = listen->conf.bind;
|
410
|
1247 #if (NGX_HTTP_SSL)
|
|
1248 in_addr->ssl = listen->conf.ssl;
|
|
1249 #endif
|
378
|
1250 in_addr->listen_conf = &listen->conf;
|
4
|
1251
|
378
|
1252 return ngx_http_add_names(cf, cscf, in_addr);
|
4
|
1253 }
|
|
1254
|
|
1255
|
|
1256 /*
|
|
1257 * add the server names and the server core module
|
|
1258 * configurations to the address:port (in_addr)
|
|
1259 */
|
|
1260
|
48
|
1261 static ngx_int_t
|
378
|
1262 ngx_http_add_names(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
|
|
1263 ngx_http_conf_in_addr_t *in_addr)
|
4
|
1264 {
|
394
|
1265 ngx_uint_t i;
|
4
|
1266 ngx_http_server_name_t *server_names, *name;
|
|
1267
|
|
1268 if (in_addr->names.elts == NULL) {
|
142
|
1269 if (ngx_array_init(&in_addr->names, cf->temp_pool, 4,
|
326
|
1270 sizeof(ngx_http_server_name_t))
|
|
1271 != NGX_OK)
|
26
|
1272 {
|
|
1273 return NGX_ERROR;
|
|
1274 }
|
|
1275 }
|
|
1276
|
4
|
1277 server_names = cscf->server_names.elts;
|
326
|
1278
|
4
|
1279 for (i = 0; i < cscf->server_names.nelts; i++) {
|
|
1280
|
394
|
1281 ngx_strlow(server_names[i].name.data, server_names[i].name.data,
|
|
1282 server_names[i].name.len);
|
26
|
1283
|
4
|
1284 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
|
10
|
1285 "name: %V", &server_names[i].name);
|
4
|
1286
|
142
|
1287 name = ngx_array_push(&in_addr->names);
|
50
|
1288 if (name == NULL) {
|
4
|
1289 return NGX_ERROR;
|
|
1290 }
|
|
1291
|
|
1292 *name = server_names[i];
|
|
1293 }
|
|
1294
|
|
1295 return NGX_OK;
|
|
1296 }
|
|
1297
|
|
1298
|
378
|
1299 static ngx_int_t
|
|
1300 ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
|
|
1301 ngx_array_t *in_ports)
|
0
|
1302 {
|
378
|
1303 ngx_int_t rc;
|
380
|
1304 ngx_uint_t s, p, a;
|
378
|
1305 ngx_hash_init_t hash;
|
|
1306 ngx_http_server_name_t *name;
|
|
1307 ngx_hash_keys_arrays_t ha;
|
|
1308 ngx_http_conf_in_port_t *in_port;
|
|
1309 ngx_http_conf_in_addr_t *in_addr;
|
|
1310 #if (NGX_PCRE)
|
380
|
1311 ngx_uint_t regex, i;
|
378
|
1312 #endif
|
|
1313
|
|
1314 in_port = in_ports->elts;
|
|
1315 for (p = 0; p < in_ports->nelts; p++) {
|
|
1316
|
|
1317 ngx_sort(in_port[p].addrs.elts, (size_t) in_port[p].addrs.nelts,
|
|
1318 sizeof(ngx_http_conf_in_addr_t), ngx_http_cmp_conf_in_addrs);
|
|
1319
|
|
1320 /*
|
|
1321 * check whether all name-based servers have
|
|
1322 * the same configuraiton as the default server
|
|
1323 */
|
|
1324
|
|
1325 in_addr = in_port[p].addrs.elts;
|
|
1326 for (a = 0; a < in_port[p].addrs.nelts; a++) {
|
|
1327
|
|
1328 name = in_addr[a].names.elts;
|
|
1329 for (s = 0; s < in_addr[a].names.nelts; s++) {
|
|
1330
|
|
1331 if (in_addr[a].core_srv_conf != name[s].core_srv_conf) {
|
|
1332 goto virtual_names;
|
|
1333 }
|
|
1334 }
|
|
1335
|
|
1336 /*
|
|
1337 * if all name-based servers have the same configuration
|
|
1338 * as the default server, then we do not need to check
|
|
1339 * them at run-time at all
|
|
1340 */
|
|
1341
|
|
1342 in_addr[a].names.nelts = 0;
|
|
1343
|
|
1344 continue;
|
|
1345
|
|
1346 virtual_names:
|
|
1347
|
|
1348 ngx_memzero(&ha, sizeof(ngx_hash_keys_arrays_t));
|
|
1349
|
|
1350 ha.temp_pool = ngx_create_pool(16384, cf->log);
|
|
1351 if (ha.temp_pool == NULL) {
|
|
1352 return NGX_ERROR;
|
|
1353 }
|
|
1354
|
|
1355 ha.pool = cf->pool;
|
|
1356
|
|
1357 if (ngx_hash_keys_array_init(&ha, NGX_HASH_LARGE) != NGX_OK) {
|
|
1358 goto failed;
|
|
1359 }
|
|
1360
|
|
1361 #if (NGX_PCRE)
|
|
1362 regex = 0;
|
|
1363 #endif
|
|
1364
|
|
1365 name = in_addr[a].names.elts;
|
|
1366
|
|
1367 for (s = 0; s < in_addr[a].names.nelts; s++) {
|
|
1368
|
|
1369 #if (NGX_PCRE)
|
|
1370 if (name[s].regex) {
|
|
1371 regex++;
|
|
1372 continue;
|
|
1373 }
|
|
1374 #endif
|
|
1375
|
|
1376 rc = ngx_hash_add_key(&ha, &name[s].name, name[s].core_srv_conf,
|
|
1377 NGX_HASH_WILDCARD_KEY);
|
|
1378
|
|
1379 if (rc == NGX_ERROR) {
|
|
1380 return NGX_ERROR;
|
|
1381 }
|
0
|
1382
|
378
|
1383 if (rc == NGX_DECLINED) {
|
|
1384 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
1385 "invalid server name or wildcard \"%V\" on %s",
|
|
1386 &name[s].name, in_addr[a].listen_conf->addr);
|
|
1387 return NGX_ERROR;
|
|
1388 }
|
|
1389
|
|
1390 if (rc == NGX_BUSY) {
|
|
1391 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
|
|
1392 "conflicting server name \"%V\" on %s, ignored",
|
|
1393 &name[s].name, in_addr[a].listen_conf->addr);
|
|
1394 }
|
|
1395 }
|
|
1396
|
|
1397 hash.key = ngx_hash_key_lc;
|
|
1398 hash.max_size = cmcf->server_names_hash_max_size;
|
|
1399 hash.bucket_size = cmcf->server_names_hash_bucket_size;
|
|
1400 hash.name = "server_names_hash";
|
|
1401 hash.pool = cf->pool;
|
|
1402
|
|
1403 if (ha.keys.nelts) {
|
|
1404 hash.hash = &in_addr[a].hash;
|
|
1405 hash.temp_pool = NULL;
|
|
1406
|
|
1407 if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK)
|
|
1408 {
|
|
1409 goto failed;
|
|
1410 }
|
|
1411 }
|
|
1412
|
|
1413 if (ha.dns_wc_head.nelts) {
|
|
1414
|
|
1415 ngx_qsort(ha.dns_wc_head.elts,
|
|
1416 (size_t) ha.dns_wc_head.nelts,
|
|
1417 sizeof(ngx_hash_key_t),
|
|
1418 ngx_http_cmp_dns_wildcards);
|
|
1419
|
|
1420 hash.hash = NULL;
|
|
1421 hash.temp_pool = ha.temp_pool;
|
0
|
1422
|
378
|
1423 if (ngx_hash_wildcard_init(&hash, ha.dns_wc_head.elts,
|
|
1424 ha.dns_wc_head.nelts)
|
|
1425 != NGX_OK)
|
|
1426 {
|
|
1427 goto failed;
|
|
1428 }
|
|
1429
|
|
1430 in_addr[a].wc_head = (ngx_hash_wildcard_t *) hash.hash;
|
|
1431 }
|
|
1432
|
|
1433 if (ha.dns_wc_tail.nelts) {
|
|
1434
|
|
1435 ngx_qsort(ha.dns_wc_tail.elts,
|
|
1436 (size_t) ha.dns_wc_tail.nelts,
|
|
1437 sizeof(ngx_hash_key_t),
|
|
1438 ngx_http_cmp_dns_wildcards);
|
|
1439
|
|
1440 hash.hash = NULL;
|
|
1441 hash.temp_pool = ha.temp_pool;
|
|
1442
|
|
1443 if (ngx_hash_wildcard_init(&hash, ha.dns_wc_tail.elts,
|
|
1444 ha.dns_wc_tail.nelts)
|
|
1445 != NGX_OK)
|
|
1446 {
|
|
1447 goto failed;
|
|
1448 }
|
|
1449
|
|
1450 in_addr[a].wc_tail = (ngx_hash_wildcard_t *) hash.hash;
|
|
1451 }
|
|
1452
|
|
1453 ngx_destroy_pool(ha.temp_pool);
|
|
1454
|
|
1455 #if (NGX_PCRE)
|
|
1456
|
|
1457 if (regex == 0) {
|
|
1458 continue;
|
|
1459 }
|
|
1460
|
|
1461 in_addr[a].nregex = regex;
|
|
1462 in_addr[a].regex = ngx_palloc(cf->pool,
|
|
1463 regex * sizeof(ngx_http_server_name_t));
|
|
1464
|
|
1465 if (in_addr[a].regex == NULL) {
|
|
1466 return NGX_ERROR;
|
|
1467 }
|
|
1468
|
|
1469 for (i = 0, s = 0; s < in_addr[a].names.nelts; s++) {
|
|
1470 if (name[s].regex) {
|
|
1471 in_addr[a].regex[i++] = name[s];
|
|
1472 }
|
|
1473 }
|
|
1474 #endif
|
0
|
1475 }
|
|
1476
|
378
|
1477 if (ngx_http_init_listening(cf, &in_port[p]) != NGX_OK) {
|
|
1478 return NGX_ERROR;
|
0
|
1479 }
|
|
1480 }
|
|
1481
|
378
|
1482 return NGX_OK;
|
|
1483
|
|
1484 failed:
|
|
1485
|
|
1486 ngx_destroy_pool(ha.temp_pool);
|
|
1487
|
|
1488 return NGX_ERROR;
|
0
|
1489 }
|
142
|
1490
|
|
1491
|
372
|
1492 static ngx_int_t
|
142
|
1493 ngx_http_cmp_conf_in_addrs(const void *one, const void *two)
|
|
1494 {
|
|
1495 ngx_http_conf_in_addr_t *first, *second;
|
|
1496
|
|
1497 first = (ngx_http_conf_in_addr_t *) one;
|
|
1498 second = (ngx_http_conf_in_addr_t *) two;
|
|
1499
|
|
1500 if (first->addr == INADDR_ANY) {
|
|
1501 /* the INADDR_ANY must be the last resort, shift it to the end */
|
|
1502 return 1;
|
|
1503 }
|
|
1504
|
|
1505 if (first->bind && !second->bind) {
|
|
1506 /* shift explicit bind()ed addresses to the start */
|
|
1507 return -1;
|
|
1508 }
|
|
1509
|
|
1510 if (!first->bind && second->bind) {
|
|
1511 /* shift explicit bind()ed addresses to the start */
|
|
1512 return 1;
|
|
1513 }
|
|
1514
|
|
1515 /* do not sort by default */
|
|
1516
|
|
1517 return 0;
|
|
1518 }
|
|
1519
|
|
1520
|
|
1521 static int ngx_libc_cdecl
|
|
1522 ngx_http_cmp_dns_wildcards(const void *one, const void *two)
|
|
1523 {
|
|
1524 ngx_hash_key_t *first, *second;
|
|
1525
|
|
1526 first = (ngx_hash_key_t *) one;
|
|
1527 second = (ngx_hash_key_t *) two;
|
|
1528
|
|
1529 return ngx_strcmp(first->key.data, second->key.data);
|
|
1530 }
|
378
|
1531
|
|
1532
|
|
1533 static ngx_int_t
|
|
1534 ngx_http_init_listening(ngx_conf_t *cf, ngx_http_conf_in_port_t *in_port)
|
|
1535 {
|
|
1536 ngx_uint_t i, a, last, bind_all, done;
|
|
1537 ngx_listening_t *ls;
|
|
1538 ngx_http_in_port_t *hip;
|
|
1539 ngx_http_conf_in_addr_t *in_addr;
|
|
1540 ngx_http_virtual_names_t *vn;
|
|
1541 ngx_http_core_loc_conf_t *clcf;
|
|
1542 ngx_http_core_srv_conf_t *cscf;
|
|
1543
|
|
1544 in_addr = in_port->addrs.elts;
|
|
1545 last = in_port->addrs.nelts;
|
|
1546
|
|
1547 /*
|
|
1548 * if there is a binding to a "*:port" then we need to bind()
|
|
1549 * to the "*:port" only and ignore other bindings
|
|
1550 */
|
|
1551
|
|
1552 if (in_addr[last - 1].addr == INADDR_ANY) {
|
|
1553 in_addr[last - 1].bind = 1;
|
|
1554 bind_all = 0;
|
|
1555
|
|
1556 } else {
|
|
1557 bind_all = 1;
|
|
1558 }
|
|
1559
|
|
1560 a = 0;
|
|
1561
|
|
1562 while (a < last) {
|
|
1563
|
|
1564 if (!bind_all && !in_addr[a].bind) {
|
|
1565 a++;
|
|
1566 continue;
|
|
1567 }
|
|
1568
|
|
1569 ls = ngx_listening_inet_stream_socket(cf, in_addr[a].addr,
|
|
1570 in_port->port);
|
|
1571 if (ls == NULL) {
|
|
1572 return NGX_ERROR;
|
|
1573 }
|
|
1574
|
|
1575 ls->addr_ntop = 1;
|
|
1576
|
|
1577 ls->handler = ngx_http_init_connection;
|
|
1578
|
|
1579 cscf = in_addr[a].core_srv_conf;
|
|
1580 ls->pool_size = cscf->connection_pool_size;
|
|
1581 ls->post_accept_timeout = cscf->client_header_timeout;
|
|
1582
|
|
1583 clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index];
|
|
1584
|
|
1585 ls->log = *clcf->err_log;
|
|
1586 ls->log.data = &ls->addr_text;
|
|
1587 ls->log.handler = ngx_accept_log_error;
|
|
1588
|
|
1589 #if (NGX_WIN32)
|
|
1590 {
|
|
1591 ngx_iocp_conf_t *iocpcf;
|
|
1592
|
|
1593 iocpcf = ngx_event_get_conf(cf->cycle->conf_ctx, ngx_iocp_module);
|
|
1594 if (iocpcf->acceptex_read) {
|
|
1595 ls->post_accept_buffer_size = cscf->client_header_buffer_size;
|
|
1596 }
|
|
1597 }
|
|
1598 #endif
|
|
1599
|
|
1600 ls->backlog = in_addr[a].listen_conf->backlog;
|
|
1601 ls->rcvbuf = in_addr[a].listen_conf->rcvbuf;
|
|
1602 ls->sndbuf = in_addr[a].listen_conf->sndbuf;
|
|
1603
|
|
1604 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
|
|
1605 ls->accept_filter = in_addr[a].listen_conf->accept_filter;
|
|
1606 #endif
|
|
1607
|
|
1608 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
|
|
1609 ls->deferred_accept = in_addr[a].listen_conf->deferred_accept;
|
|
1610 #endif
|
|
1611
|
|
1612 hip = ngx_palloc(cf->pool, sizeof(ngx_http_in_port_t));
|
|
1613 if (hip == NULL) {
|
|
1614 return NGX_ERROR;
|
|
1615 }
|
|
1616
|
|
1617 hip->port = in_port->port;
|
|
1618
|
382
|
1619 hip->port_text.data = ngx_pnalloc(cf->pool, 7);
|
378
|
1620 if (hip->port_text.data == NULL) {
|
|
1621 return NGX_ERROR;
|
|
1622 }
|
|
1623
|
|
1624 ls->servers = hip;
|
|
1625
|
|
1626 hip->port_text.len = ngx_sprintf(hip->port_text.data, ":%d", hip->port)
|
|
1627 - hip->port_text.data;
|
|
1628
|
|
1629 in_addr = in_port->addrs.elts;
|
|
1630
|
|
1631 if (in_addr[a].bind && in_addr[a].addr != INADDR_ANY) {
|
|
1632 hip->naddrs = 1;
|
|
1633 done = 0;
|
|
1634
|
|
1635 } else if (in_port->addrs.nelts > 1
|
|
1636 && in_addr[last - 1].addr == INADDR_ANY)
|
|
1637 {
|
|
1638 hip->naddrs = last;
|
|
1639 done = 1;
|
|
1640
|
|
1641 } else {
|
|
1642 hip->naddrs = 1;
|
|
1643 done = 0;
|
|
1644 }
|
|
1645
|
|
1646 hip->addrs = ngx_pcalloc(cf->pool,
|
|
1647 hip->naddrs * sizeof(ngx_http_in_addr_t));
|
|
1648 if (hip->addrs == NULL) {
|
|
1649 return NGX_ERROR;
|
|
1650 }
|
|
1651
|
|
1652 for (i = 0; i < hip->naddrs; i++) {
|
|
1653 hip->addrs[i].addr = in_addr[i].addr;
|
|
1654 hip->addrs[i].core_srv_conf = in_addr[i].core_srv_conf;
|
|
1655
|
410
|
1656 #if (NGX_HTTP_SSL)
|
|
1657 hip->addrs[i].ssl = in_addr[i].ssl;
|
|
1658 #endif
|
|
1659
|
378
|
1660 if (in_addr[i].hash.buckets == NULL
|
|
1661 && (in_addr[i].wc_head == NULL
|
|
1662 || in_addr[i].wc_head->hash.buckets == NULL)
|
|
1663 && (in_addr[i].wc_head == NULL
|
|
1664 || in_addr[i].wc_head->hash.buckets == NULL))
|
|
1665 {
|
|
1666 continue;
|
|
1667 }
|
|
1668
|
|
1669 vn = ngx_palloc(cf->pool, sizeof(ngx_http_virtual_names_t));
|
|
1670 if (vn == NULL) {
|
|
1671 return NGX_ERROR;
|
|
1672 }
|
|
1673 hip->addrs[i].virtual_names = vn;
|
|
1674
|
|
1675 vn->names.hash = in_addr[i].hash;
|
|
1676 vn->names.wc_head = in_addr[i].wc_head;
|
|
1677 vn->names.wc_tail = in_addr[i].wc_tail;
|
|
1678 #if (NGX_PCRE)
|
|
1679 vn->nregex = in_addr[i].nregex;
|
|
1680 vn->regex = in_addr[i].regex;
|
|
1681 #endif
|
|
1682 }
|
|
1683
|
|
1684 if (done) {
|
|
1685 return NGX_OK;
|
|
1686 }
|
|
1687
|
|
1688 in_addr++;
|
|
1689 in_port->addrs.elts = in_addr;
|
|
1690 last--;
|
|
1691
|
|
1692 a = 0;
|
|
1693 }
|
|
1694
|
|
1695 return NGX_OK;
|
|
1696 }
|
394
|
1697
|
|
1698
|
|
1699 char *
|
|
1700 ngx_http_types_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1701 {
|
|
1702 char *p = conf;
|
|
1703
|
|
1704 ngx_array_t **types;
|
|
1705 ngx_str_t *value, *default_type;
|
|
1706 ngx_uint_t i, n, hash;
|
|
1707 ngx_hash_key_t *type;
|
|
1708
|
|
1709 types = (ngx_array_t **) (p + cmd->offset);
|
|
1710
|
|
1711 default_type = cmd->post;
|
|
1712
|
|
1713 if (*types == NULL) {
|
|
1714 *types = ngx_array_create(cf->temp_pool, 1, sizeof(ngx_hash_key_t));
|
|
1715 if (*types == NULL) {
|
|
1716 return NGX_CONF_ERROR;
|
|
1717 }
|
|
1718
|
|
1719 if (default_type) {
|
|
1720 type = ngx_array_push(*types);
|
|
1721 if (type == NULL) {
|
|
1722 return NGX_CONF_ERROR;
|
|
1723 }
|
|
1724
|
|
1725 type->key = *default_type;
|
|
1726 type->key_hash = ngx_hash_key(default_type->data,
|
|
1727 default_type->len);
|
|
1728 type->value = (void *) 4;
|
|
1729 }
|
|
1730 }
|
|
1731
|
|
1732 value = cf->args->elts;
|
|
1733
|
|
1734 for (i = 1; i < cf->args->nelts; i++) {
|
|
1735
|
|
1736 hash = ngx_hash_strlow(value[i].data, value[i].data, value[i].len);
|
|
1737 value[i].data[value[i].len] = '\0';
|
|
1738
|
|
1739 type = (*types)->elts;
|
|
1740 for (n = 0; n < (*types)->nelts; n++) {
|
|
1741
|
|
1742 if (ngx_strcmp(value[i].data, type[n].key.data) == 0) {
|
|
1743 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1744 "duplicate MIME type \"%V\"", &value[i]);
|
|
1745 continue;
|
|
1746 }
|
|
1747 }
|
|
1748
|
|
1749 type = ngx_array_push(*types);
|
|
1750 if (type == NULL) {
|
|
1751 return NGX_CONF_ERROR;
|
|
1752 }
|
|
1753
|
|
1754 type->key = value[i];
|
|
1755 type->key_hash = hash;
|
|
1756 type->value = (void *) 4;
|
|
1757 }
|
|
1758
|
|
1759 return NGX_CONF_OK;
|
|
1760 }
|
|
1761
|
|
1762
|
|
1763 char *
|
|
1764 ngx_http_merge_types(ngx_conf_t *cf, ngx_array_t *keys, ngx_hash_t *types_hash,
|
400
|
1765 ngx_array_t *prev_keys, ngx_hash_t *prev_types_hash,
|
394
|
1766 ngx_str_t *default_types)
|
|
1767 {
|
|
1768 ngx_hash_init_t hash;
|
|
1769
|
400
|
1770 if (keys) {
|
394
|
1771
|
400
|
1772 hash.hash = types_hash;
|
|
1773 hash.key = NULL;
|
|
1774 hash.max_size = 2048;
|
|
1775 hash.bucket_size = 64;
|
|
1776 hash.name = "test_types_hash";
|
|
1777 hash.pool = cf->pool;
|
|
1778 hash.temp_pool = NULL;
|
|
1779
|
|
1780 if (ngx_hash_init(&hash, keys->elts, keys->nelts) != NGX_OK) {
|
|
1781 return NGX_CONF_ERROR;
|
394
|
1782 }
|
|
1783
|
400
|
1784 return NGX_CONF_OK;
|
|
1785 }
|
|
1786
|
|
1787 if (prev_types_hash->buckets == NULL) {
|
|
1788
|
|
1789 if (prev_keys == NULL) {
|
|
1790
|
410
|
1791 if (ngx_http_set_default_types(cf, &prev_keys, default_types)
|
400
|
1792 != NGX_OK)
|
|
1793 {
|
410
|
1794 return NGX_CONF_ERROR;
|
|
1795 }
|
400
|
1796 }
|
|
1797
|
|
1798 hash.hash = prev_types_hash;
|
|
1799 hash.key = NULL;
|
|
1800 hash.max_size = 2048;
|
|
1801 hash.bucket_size = 64;
|
|
1802 hash.name = "test_types_hash";
|
|
1803 hash.pool = cf->pool;
|
|
1804 hash.temp_pool = NULL;
|
|
1805
|
|
1806 if (ngx_hash_init(&hash, prev_keys->elts, prev_keys->nelts) != NGX_OK) {
|
394
|
1807 return NGX_CONF_ERROR;
|
|
1808 }
|
|
1809 }
|
|
1810
|
400
|
1811 *types_hash = *prev_types_hash;
|
394
|
1812
|
|
1813 return NGX_CONF_OK;
|
|
1814
|
|
1815 }
|
|
1816
|
|
1817
|
400
|
1818 ngx_int_t
|
394
|
1819 ngx_http_set_default_types(ngx_conf_t *cf, ngx_array_t **types,
|
|
1820 ngx_str_t *default_type)
|
|
1821 {
|
|
1822 ngx_hash_key_t *type;
|
|
1823
|
|
1824 *types = ngx_array_create(cf->temp_pool, 1, sizeof(ngx_hash_key_t));
|
|
1825 if (*types == NULL) {
|
400
|
1826 return NGX_ERROR;
|
394
|
1827 }
|
|
1828
|
|
1829 while (default_type->len) {
|
|
1830
|
|
1831 type = ngx_array_push(*types);
|
|
1832 if (type == NULL) {
|
400
|
1833 return NGX_ERROR;
|
394
|
1834 }
|
|
1835
|
|
1836 type->key = *default_type;
|
|
1837 type->key_hash = ngx_hash_key(default_type->data,
|
|
1838 default_type->len);
|
|
1839 type->value = (void *) 4;
|
|
1840
|
|
1841 default_type++;
|
|
1842 }
|
|
1843
|
400
|
1844 return NGX_OK;
|
394
|
1845 }
|