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