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