comparison src/http/ngx_http_core_module.c @ 96:a23d010f356d

nginx-0.0.1-2003-05-27-16:18:54 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 27 May 2003 12:18:54 +0000
parents b48066122884
children 70d2345a903f
comparison
equal deleted inserted replaced
95:b48066122884 96:a23d010f356d
1 1
2 #include <ngx_config.h> 2 #include <ngx_config.h>
3 3 #include <ngx_core.h>
4
5 /* ???? */
4 #include <ngx_listen.h> 6 #include <ngx_listen.h>
5 #include <ngx_core.h> 7
6 #include <ngx_string.h> 8 #include <ngx_http.h>
7 #include <ngx_conf_file.h> 9 #include <ngx_http_output_filter.h>
8 10
9 #include <nginx.h> 11 #include <nginx.h>
10 12
11 #include <ngx_http.h> 13
12 #include <ngx_http_config.h> 14 /* STUB */
13 #include <ngx_http_core_module.h>
14
15
16 /* STUB for r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; */
17 #include <ngx_http_output_filter.h>
18
19 int ngx_http_static_handler(ngx_http_request_t *r); 15 int ngx_http_static_handler(ngx_http_request_t *r);
20 int ngx_http_proxy_handler(ngx_http_request_t *r); 16
21 /**/
22 17
23 static int ngx_http_core_index_handler(ngx_http_request_t *r); 18 static int ngx_http_core_index_handler(ngx_http_request_t *r);
24
25 static int ngx_http_core_init(ngx_pool_t *pool);
26 19
27 static void *ngx_http_core_create_main_conf(ngx_pool_t *pool); 20 static void *ngx_http_core_create_main_conf(ngx_pool_t *pool);
28 static char *ngx_http_core_init_main_conf(ngx_pool_t *pool, void *conf); 21 static char *ngx_http_core_init_main_conf(ngx_pool_t *pool, void *conf);
29 static void *ngx_http_core_create_srv_conf(ngx_pool_t *pool); 22 static void *ngx_http_core_create_srv_conf(ngx_pool_t *pool);
30 static char *ngx_http_core_merge_srv_conf(ngx_pool_t *pool, 23 static char *ngx_http_core_merge_srv_conf(ngx_pool_t *pool,
31 void *parent, void *child); 24 void *parent, void *child);
32 static void *ngx_http_core_create_loc_conf(ngx_pool_t *pool); 25 static void *ngx_http_core_create_loc_conf(ngx_pool_t *pool);
33 static char *ngx_http_core_merge_loc_conf(ngx_pool_t *pool, 26 static char *ngx_http_core_merge_loc_conf(ngx_pool_t *pool,
34 void *parent, void *child); 27 void *parent, void *child);
35 28
36 static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy); 29 static int ngx_http_core_init(ngx_pool_t *pool);
30 static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy);
37 static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd, 31 static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd,
38 char *dummy); 32 void *dummy);
39 static char *ngx_types_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy); 33 static char *ngx_types_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
40 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, char *conf); 34 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
41 35
42 36
43 static ngx_command_t ngx_http_core_commands[] = { 37 static ngx_command_t ngx_http_core_commands[] = {
44 38
45 {ngx_string("server"), 39 {ngx_string("server"),
111 ngx_types_block, 105 ngx_types_block,
112 NGX_HTTP_LOC_CONF_OFFSET, 106 NGX_HTTP_LOC_CONF_OFFSET,
113 0, 107 0,
114 NULL}, 108 NULL},
115 109
110 {ngx_string("default_type"),
111 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
112 ngx_conf_set_str_slot,
113 NGX_HTTP_LOC_CONF_OFFSET,
114 offsetof(ngx_http_core_loc_conf_t, default_type),
115 NULL},
116
116 {ngx_string("root"), 117 {ngx_string("root"),
117 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, 118 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
118 ngx_conf_set_str_slot, 119 ngx_conf_set_str_slot,
119 NGX_HTTP_LOC_CONF_OFFSET, 120 NGX_HTTP_LOC_CONF_OFFSET,
120 offsetof(ngx_http_core_loc_conf_t, doc_root), 121 offsetof(ngx_http_core_loc_conf_t, doc_root),
153 ngx_conf_set_msec_slot, 154 ngx_conf_set_msec_slot,
154 NGX_HTTP_LOC_CONF_OFFSET, 155 NGX_HTTP_LOC_CONF_OFFSET,
155 offsetof(ngx_http_core_loc_conf_t, lingering_timeout), 156 offsetof(ngx_http_core_loc_conf_t, lingering_timeout),
156 NULL}, 157 NULL},
157 158
158 {ngx_null_string, 0, NULL, 0, 0, NULL} 159 ngx_null_command
159 }; 160 };
160 161
161 162
162 ngx_http_module_t ngx_http_core_module_ctx = { 163 ngx_http_module_t ngx_http_core_module_ctx = {
163 NGX_HTTP_MODULE,
164
165 ngx_http_core_create_main_conf, /* create main configuration */ 164 ngx_http_core_create_main_conf, /* create main configuration */
166 ngx_http_core_init_main_conf, /* init main configuration */ 165 ngx_http_core_init_main_conf, /* init main configuration */
167 166
168 ngx_http_core_create_srv_conf, /* create server configuration */ 167 ngx_http_core_create_srv_conf, /* create server configuration */
169 ngx_http_core_merge_srv_conf, /* merge server configuration */ 168 ngx_http_core_merge_srv_conf, /* merge server configuration */
172 ngx_http_core_merge_loc_conf /* merge location configuration */ 171 ngx_http_core_merge_loc_conf /* merge location configuration */
173 }; 172 };
174 173
175 174
176 ngx_module_t ngx_http_core_module = { 175 ngx_module_t ngx_http_core_module = {
176 NGX_MODULE,
177 &ngx_http_core_module_ctx, /* module context */ 177 &ngx_http_core_module_ctx, /* module context */
178 0, /* module index */
179 ngx_http_core_commands, /* module directives */ 178 ngx_http_core_commands, /* module directives */
180 NGX_HTTP_MODULE_TYPE, /* module type */ 179 NGX_HTTP_MODULE, /* module type */
181 ngx_http_core_init /* init module */ 180 ngx_http_core_init /* init module */
182 }; 181 };
183 182
184 183
185 int ngx_http_find_server_conf(ngx_http_request_t *r)
186 {
187 int a, n;
188 socklen_t len;
189 struct sockaddr_in addr_in;
190 ngx_http_in_port_t *in_port;
191 ngx_http_in_addr_t *in_addr;
192 ngx_http_conf_ctx_t *ctx;
193 ngx_http_server_name_t *name;
194
195 /* AF_INET only */
196
197 in_port = (ngx_http_in_port_t *) r->connection->servers;
198 in_addr = (ngx_http_in_addr_t *) in_port->addrs.elts;
199
200 r->port = in_port->port;
201
202 a = 0;
203
204 if (in_port->addrs.nelts > 1) {
205
206 /* there're the several addresses on this port and one of them
207 is "*:port" so getsockname() is needed to determine
208 the server address */
209
210 len = sizeof(struct sockaddr_in);
211 if (getsockname(r->connection->fd, (struct sockaddr *) &addr_in, &len)
212 == -1) {
213 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_socket_errno,
214 "getsockname() failed");
215 return NGX_ERROR;
216 }
217
218 r->in_addr = addr_in.sin_addr.s_addr;
219
220 for ( /* void */ ; a < in_port->addrs.nelts; a++) {
221 if (in_addr[a].addr == r->in_addr) {
222 ngx_log_debug(r->connection->log, "FOUND");
223 break;
224 }
225 }
226
227 /* DEBUG */
228 if (a == in_port->addrs.nelts) {
229 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
230 "addr not found");
231 exit(1);
232 }
233
234 } else {
235 r->in_addr = in_addr[0].addr;
236 }
237
238 /* the default server configuration for this address:port */
239 ctx = in_addr[a].core_srv_conf->ctx;
240
241 if (r->headers_in.host_name_len > 0) {
242
243 /* find the name based server configuration */
244
245 name = (ngx_http_server_name_t *) in_addr[a].names.elts;
246 for (n = 0; n < in_addr[a].names.nelts; n++) {
247 if (r->headers_in.host_name_len != name[n].name.len) {
248 continue;
249 }
250
251 if (ngx_strncasecmp(r->headers_in.host->value.data,
252 name[n].name.data,
253 r->headers_in.host_name_len) == 0) {
254 ctx = name->core_srv_conf->ctx;
255 break;
256 }
257 }
258 }
259
260 r->srv_conf = ctx->srv_conf;
261 r->loc_conf = ctx->loc_conf;
262
263 #if 0
264 ngx_log_debug(r->connection->log, "cxt: %08x" _ ctx);
265 ngx_log_debug(r->connection->log, "srv_conf: %0x" _ r->srv_conf);
266 ngx_log_debug(r->connection->log, "loc_conf: %0x" _ r->loc_conf);
267 #endif
268
269 return NGX_OK;
270 }
271
272
273 void ngx_http_handler(ngx_http_request_t *r) 184 void ngx_http_handler(ngx_http_request_t *r)
274 { 185 {
275 int rc, i; 186 int rc, i;
276 ngx_http_handler_pt *h; 187 ngx_http_handler_pt *h;
277 ngx_http_core_loc_conf_t *lcf, **lcfp; 188 ngx_http_core_loc_conf_t *clcf, **clcfp;
278 ngx_http_core_srv_conf_t *scf; 189 ngx_http_core_srv_conf_t *cscf;
279 190
280 r->connection->unexpected_eof = 0; 191 r->connection->unexpected_eof = 0;
281 192
282 r->keepalive = 1; 193 r->keepalive = 1;
283 194
291 /* TODO: run rewrite url phase */ 202 /* TODO: run rewrite url phase */
292 203
293 204
294 /* find location config */ 205 /* find location config */
295 206
296 scf = (ngx_http_core_srv_conf_t *) 207 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
297 ngx_http_get_module_srv_conf(r, ngx_http_core_module_ctx); 208
298 209 clcfp = cscf->locations.elts;
299 lcfp = (ngx_http_core_loc_conf_t **) scf->locations.elts; 210 for (i = 0; i < cscf->locations.nelts; i++) {
300 for (i = 0; i < scf->locations.nelts; i++) {
301 #if 0 211 #if 0
302 ngx_log_debug(r->connection->log, "trans: %s" _ lcfp[i]->name.data); 212 ngx_log_debug(r->connection->log, "trans: %s" _ clcfp[i]->name.data);
303 #endif 213 #endif
304 if (r->uri.len < lcfp[i]->name.len) { 214 if (r->uri.len < clcfp[i]->name.len) {
305 continue; 215 continue;
306 } 216 }
307 217
308 rc = ngx_rstrncmp(r->uri.data, lcfp[i]->name.data, lcfp[i]->name.len); 218 rc = ngx_rstrncmp(r->uri.data, clcfp[i]->name.data,
219 clcfp[i]->name.len);
309 220
310 if (rc < 0) { 221 if (rc < 0) {
311 break; 222 break;
312 } 223 }
313 224
314 if (rc == 0) { 225 if (rc == 0) {
315 r->loc_conf = lcfp[i]->loc_conf; 226 r->loc_conf = clcfp[i]->loc_conf;
316 } 227 }
317 } 228 }
318 229
319 lcf = (ngx_http_core_loc_conf_t *) 230 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
320 ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx); 231
321 232 if ((ngx_io.flags & NGX_IO_SENDFILE) == 0 || clcf->sendfile == 0) {
322 if ((ngx_io.flags & NGX_IO_SENDFILE) == 0 || lcf->sendfile == 0) {
323 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; 233 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY;
324 } 234 }
325 235
326 /* run translation phase */ 236 /* run translation phase */
327 237
353 } 263 }
354 264
355 265
356 int ngx_http_core_translate_handler(ngx_http_request_t *r) 266 int ngx_http_core_translate_handler(ngx_http_request_t *r)
357 { 267 {
358 int len, port_len, f_offset, l_offset; 268 char *location, *last;
359 char *buf, *location, *last;
360 ngx_err_t err; 269 ngx_err_t err;
361 ngx_table_elt_t *h; 270 ngx_table_elt_t *h;
271 ngx_http_in_port_t *in_port;
362 ngx_http_server_name_t *s_name; 272 ngx_http_server_name_t *s_name;
363 ngx_http_core_srv_conf_t *scf; 273 ngx_http_core_srv_conf_t *cscf;
364 ngx_http_core_loc_conf_t *lcf; 274 ngx_http_core_loc_conf_t *clcf;
365 275
366 lcf = (ngx_http_core_loc_conf_t *) 276 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
367 ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx); 277
368 278 if (clcf->handler) {
369 if (lcf->handler) { 279 r->handler = clcf->handler;
370 r->handler = lcf->handler;
371 return NGX_OK; 280 return NGX_OK;
372 } 281 }
373 282
374 scf = (ngx_http_core_srv_conf_t *) 283 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
375 ngx_http_get_module_srv_conf(r, ngx_http_core_module_ctx);
376 284
377 if (r->uri.data[r->uri.len - 1] == '/') { 285 if (r->uri.data[r->uri.len - 1] == '/') {
378 r->handler = ngx_http_core_index_handler; 286 r->handler = ngx_http_core_index_handler;
379 return NGX_OK; 287 return NGX_OK;
380 } 288 }
381 289
382 ngx_log_debug(r->connection->log, "doc_root: %08x" _ &lcf->doc_root);
383
384 s_name = (ngx_http_server_name_t *) scf->server_names.elts;
385
386 if (r->port == 0) {
387 #if 0
388 struct sockaddr_in *addr_in;
389 addr_in = (struct sockaddr_in *) r->connection->sockaddr;
390 r->port = ntohs(addr_in->sin_port);
391 #else
392 ngx_http_in_port_t *in_port;
393 in_port = (ngx_http_in_port_t *) r->connection->servers;
394 r->port = in_port->port;
395 #endif
396 if (r->port != 80) {
397 ngx_test_null(r->port_name.data, ngx_palloc(r->pool, 7),
398 NGX_HTTP_INTERNAL_SERVER_ERROR);
399 r->port_name.len = ngx_snprintf(r->port_name.data, 7, ":%d",
400 r->port);
401 }
402 }
403
404 port_len = (r->port != 80) ? r->port_name.len : 0;
405
406 /* "+ 7" is "http://" */
407 if (lcf->doc_root.len > 7 + s_name[0].name.len + port_len) {
408 len = lcf->doc_root.len;
409 f_offset = 0;
410 l_offset = len - (7 + s_name[0].name.len + port_len);
411
412 } else {
413 len = 7 + s_name[0].name.len + port_len;
414 f_offset = len - lcf->doc_root.len;
415 l_offset = 0;
416 }
417
418 /* "+ 2" is for trailing '/' in redirect and '\0' */ 290 /* "+ 2" is for trailing '/' in redirect and '\0' */
419 len += r->uri.len + 2; 291 ngx_test_null(r->file.name.data,
420 292 ngx_palloc(r->pool, clcf->doc_root.len + r->uri.len + 2),
421 ngx_test_null(buf, ngx_palloc(r->pool, len),
422 NGX_HTTP_INTERNAL_SERVER_ERROR); 293 NGX_HTTP_INTERNAL_SERVER_ERROR);
423 294
424 r->file.name.data = buf + f_offset; 295 location = ngx_cpymem(r->file.name.data, clcf->doc_root.data,
425 location = buf + l_offset; 296 clcf->doc_root.len),
426 297
427 last = ngx_cpystrn(ngx_cpystrn(r->file.name.data, lcf->doc_root.data, 298 last = ngx_cpystrn(location, r->uri.data, r->uri.len + 1);
428 lcf->doc_root.len + 1),
429 r->uri.data, r->uri.len + 1);
430
431 r->file.name.len = last - r->file.name.data;
432 299
433 ngx_log_debug(r->connection->log, "HTTP filename: '%s'" _ r->file.name.data); 300 ngx_log_debug(r->connection->log, "HTTP filename: '%s'" _ r->file.name.data);
434 301
435 #if (WIN9X) 302 #if (WIN9X)
436 303
507 374
508 /* BROKEN: need to include server name */ 375 /* BROKEN: need to include server name */
509 376
510 ngx_test_null(h, ngx_push_table(r->headers_out.headers), 377 ngx_test_null(h, ngx_push_table(r->headers_out.headers),
511 NGX_HTTP_INTERNAL_SERVER_ERROR); 378 NGX_HTTP_INTERNAL_SERVER_ERROR);
512
513 ngx_memcpy(location, "http://", 7);
514 ngx_memcpy(location + 7, s_name[0].name.data, s_name[0].name.len);
515 if (port_len) {
516 ngx_memcpy(location + 7 + s_name[0].name.len, r->port_name.data,
517 port_len);
518 }
519 379
520 *last++ = '/'; 380 *last++ = '/';
521 *last = '\0'; 381 *last = '\0';
522 h->key.len = 8; 382 h->key.len = 8;
523 h->key.data = "Location" ; 383 h->key.data = "Location" ;
582 ngx_http_close_request(r, 0); 442 ngx_http_close_request(r, 0);
583 return NGX_OK; 443 return NGX_OK;
584 } 444 }
585 445
586 446
587 int ngx_http_error(ngx_http_request_t *r, int error) 447 int ngx_http_error(ngx_http_request_t *r, int error)
588 { 448 {
589 /* STUB */ 449 /* STUB */
590 ngx_log_debug(r->connection->log, "http error: %d" _ error); 450 ngx_log_debug(r->connection->log, "http error: %d" _ error);
591 451
592 /* log request */ 452 /* log request */
625 485
626 return NGX_OK; 486 return NGX_OK;
627 } 487 }
628 488
629 489
630 static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy) 490 static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
631 { 491 {
632 int m; 492 int m;
633 char *rv; 493 char *rv;
634 ngx_http_module_t *module; 494 ngx_http_module_t *module;
635 ngx_conf_t pcf; 495 ngx_conf_t pcf;
655 ngx_test_null(ctx->loc_conf, 515 ngx_test_null(ctx->loc_conf,
656 ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module), 516 ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module),
657 NGX_CONF_ERROR); 517 NGX_CONF_ERROR);
658 518
659 for (m = 0; ngx_modules[m]; m++) { 519 for (m = 0; ngx_modules[m]; m++) {
660 if (ngx_modules[m]->type != NGX_HTTP_MODULE_TYPE) { 520 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
661 continue; 521 continue;
662 } 522 }
663 523
664 module = (ngx_http_module_t *) ngx_modules[m]->ctx; 524 module = ngx_modules[m]->ctx;
665 525
666 if (module->create_srv_conf) { 526 if (module->create_srv_conf) {
667 ngx_test_null(ctx->srv_conf[module->index], 527 ngx_test_null(ctx->srv_conf[ngx_modules[m]->ctx_index],
668 module->create_srv_conf(cf->pool), 528 module->create_srv_conf(cf->pool),
669 NGX_CONF_ERROR); 529 NGX_CONF_ERROR);
670 } 530 }
671 531
672 if (module->create_loc_conf) { 532 if (module->create_loc_conf) {
673 ngx_test_null(ctx->loc_conf[module->index], 533 ngx_test_null(ctx->loc_conf[ngx_modules[m]->ctx_index],
674 module->create_loc_conf(cf->pool), 534 module->create_loc_conf(cf->pool),
675 NGX_CONF_ERROR); 535 NGX_CONF_ERROR);
676 } 536 }
677 } 537 }
678 538
679 /* create links of the srv_conf's */ 539 /* create links of the srv_conf's */
680 540
681 cscf = ctx->srv_conf[ngx_http_core_module_ctx.index]; 541 cscf = ctx->srv_conf[ngx_http_core_module.ctx_index];
682 cscf->ctx = ctx; 542 cscf->ctx = ctx;
683 543
684 cmcf = ctx->main_conf[ngx_http_core_module_ctx.index]; 544 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
685 ngx_test_null(cscfp, ngx_push_array(&cmcf->servers), NGX_CONF_ERROR); 545 ngx_test_null(cscfp, ngx_push_array(&cmcf->servers), NGX_CONF_ERROR);
686 *cscfp = cscf; 546 *cscfp = cscf;
687 547
688 /* parse inside server{} */ 548 /* parse inside server{} */
689 549
696 556
697 return rv; 557 return rv;
698 } 558 }
699 559
700 560
701 static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy) 561 static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
702 { 562 {
703 int i; 563 int m;
704 char *rv; 564 char *rv;
705 ngx_str_t *location; 565 ngx_str_t *location;
706 ngx_http_module_t *module; 566 ngx_http_module_t *module;
707 ngx_conf_t pcf; 567 ngx_conf_t pcf;
708 ngx_http_conf_ctx_t *ctx, *pctx; 568 ngx_http_conf_ctx_t *ctx, *pctx;
719 579
720 ngx_test_null(ctx->loc_conf, 580 ngx_test_null(ctx->loc_conf,
721 ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module), 581 ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module),
722 NGX_CONF_ERROR); 582 NGX_CONF_ERROR);
723 583
724 for (i = 0; ngx_modules[i]; i++) { 584 for (m = 0; ngx_modules[m]; m++) {
725 if (ngx_modules[i]->type != NGX_HTTP_MODULE_TYPE) { 585 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
726 continue; 586 continue;
727 } 587 }
728 588
729 module = (ngx_http_module_t *) ngx_modules[i]->ctx; 589 module = ngx_modules[m]->ctx;
730 590
731 if (module->create_loc_conf) { 591 if (module->create_loc_conf) {
732 ngx_test_null(ctx->loc_conf[module->index], 592 ngx_test_null(ctx->loc_conf[ngx_modules[m]->ctx_index],
733 module->create_loc_conf(cf->pool), 593 module->create_loc_conf(cf->pool),
734 NGX_CONF_ERROR); 594 NGX_CONF_ERROR);
735 } 595 }
736 } 596 }
737 597
738 clcf = ctx->loc_conf[ngx_http_core_module_ctx.index]; 598 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
739 location = (ngx_str_t *) cf->args->elts; 599 location = (ngx_str_t *) cf->args->elts;
740 clcf->name.len = location[1].len; 600 clcf->name.len = location[1].len;
741 clcf->name.data = location[1].data; 601 clcf->name.data = location[1].data;
742 clcf->loc_conf = ctx->loc_conf; 602 clcf->loc_conf = ctx->loc_conf;
743 603
744 cscf = ctx->srv_conf[ngx_http_core_module_ctx.index]; 604 cscf = ctx->srv_conf[ngx_http_core_module.ctx_index];
745 ngx_test_null(clcfp, ngx_push_array(&cscf->locations), NGX_CONF_ERROR); 605 ngx_test_null(clcfp, ngx_push_array(&cscf->locations), NGX_CONF_ERROR);
746 *clcfp = clcf; 606 *clcfp = clcf;
747 607
748 pcf = *cf; 608 pcf = *cf;
749 cf->ctx = ctx; 609 cf->ctx = ctx;
789 649
790 return NGX_CONF_OK; 650 return NGX_CONF_OK;
791 } 651 }
792 652
793 653
794 static char *ngx_types_block(ngx_conf_t *cf, ngx_command_t *cmd, char *conf) 654 static char *ngx_types_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
795 { 655 {
796 char *rv; 656 char *rv;
797 ngx_conf_t pcf; 657 ngx_conf_t pcf;
798 658
799 pcf = *cf; 659 pcf = *cf;
809 static void *ngx_http_core_create_main_conf(ngx_pool_t *pool) 669 static void *ngx_http_core_create_main_conf(ngx_pool_t *pool)
810 { 670 {
811 ngx_http_core_main_conf_t *cmcf; 671 ngx_http_core_main_conf_t *cmcf;
812 672
813 ngx_test_null(cmcf, 673 ngx_test_null(cmcf,
814 ngx_palloc(pool, sizeof(ngx_http_core_main_conf_t)), 674 ngx_palloc(pool, sizeof(ngx_http_core_main_conf_t)),
815 NGX_CONF_ERROR); 675 NGX_CONF_ERROR);
816 676
817 cmcf->connection_pool_size = NGX_CONF_UNSET; 677 cmcf->connection_pool_size = NGX_CONF_UNSET;
818 cmcf->post_accept_timeout = NGX_CONF_UNSET; 678 cmcf->post_accept_timeout = NGX_CONF_UNSET;
819 679
824 } 684 }
825 685
826 686
827 static char *ngx_http_core_init_main_conf(ngx_pool_t *pool, void *conf) 687 static char *ngx_http_core_init_main_conf(ngx_pool_t *pool, void *conf)
828 { 688 {
829 ngx_http_core_main_conf_t *cmcf = (ngx_http_core_main_conf_t *) conf; 689 ngx_http_core_main_conf_t *cmcf = conf;
830 690
831 ngx_conf_init_size_value(cmcf->connection_pool_size, 16384); 691 ngx_conf_init_size_value(cmcf->connection_pool_size, 16384);
832 ngx_conf_init_msec_value(cmcf->post_accept_timeout, 30000); 692 ngx_conf_init_msec_value(cmcf->post_accept_timeout, 30000);
833 693
834 return NGX_CONF_OK; 694 return NGX_CONF_OK;
838 static void *ngx_http_core_create_srv_conf(ngx_pool_t *pool) 698 static void *ngx_http_core_create_srv_conf(ngx_pool_t *pool)
839 { 699 {
840 ngx_http_core_srv_conf_t *cscf; 700 ngx_http_core_srv_conf_t *cscf;
841 701
842 ngx_test_null(cscf, 702 ngx_test_null(cscf,
843 ngx_pcalloc(pool, sizeof(ngx_http_core_srv_conf_t)), 703 ngx_pcalloc(pool, sizeof(ngx_http_core_srv_conf_t)),
844 NGX_CONF_ERROR); 704 NGX_CONF_ERROR);
845 705
846 ngx_init_array(cscf->locations, pool, 5, sizeof(void *), NGX_CONF_ERROR); 706 ngx_init_array(cscf->locations, pool, 5, sizeof(void *), NGX_CONF_ERROR);
847 ngx_init_array(cscf->listen, pool, 5, sizeof(ngx_http_listen_t), 707 ngx_init_array(cscf->listen, pool, 5, sizeof(ngx_http_listen_t),
848 NGX_CONF_ERROR); 708 NGX_CONF_ERROR);
879 if (conf->server_names.nelts == 0) { 739 if (conf->server_names.nelts == 0) {
880 ngx_test_null(n, ngx_push_array(&conf->server_names), NGX_CONF_ERROR); 740 ngx_test_null(n, ngx_push_array(&conf->server_names), NGX_CONF_ERROR);
881 ngx_test_null(n->name.data, ngx_palloc(pool, NGX_MAXHOSTNAMELEN), 741 ngx_test_null(n->name.data, ngx_palloc(pool, NGX_MAXHOSTNAMELEN),
882 NGX_CONF_ERROR); 742 NGX_CONF_ERROR);
883 if (gethostname(n->name.data, NGX_MAXHOSTNAMELEN) == -1) { 743 if (gethostname(n->name.data, NGX_MAXHOSTNAMELEN) == -1) {
884 /* STUB: no log here */ 744 /* TODO: need ngx_errno here */
885 #if 0 745 return "gethostname() failed";
886 ngx_log_error(NGX_LOG_EMERG, scf->log, ngx_errno,
887 "gethostname() failed");
888 #endif
889 return NGX_CONF_ERROR;
890 } 746 }
891 n->name.len = ngx_strlen(n->name.data); 747 n->name.len = ngx_strlen(n->name.data);
892 n->core_srv_conf = conf; 748 n->core_srv_conf = conf;
893 } 749 }
894 750
908 static void *ngx_http_core_create_loc_conf(ngx_pool_t *pool) 764 static void *ngx_http_core_create_loc_conf(ngx_pool_t *pool)
909 { 765 {
910 ngx_http_core_loc_conf_t *lcf; 766 ngx_http_core_loc_conf_t *lcf;
911 767
912 ngx_test_null(lcf, 768 ngx_test_null(lcf,
913 ngx_pcalloc(pool, sizeof(ngx_http_core_loc_conf_t)), 769 ngx_pcalloc(pool, sizeof(ngx_http_core_loc_conf_t)),
914 NGX_CONF_ERROR); 770 NGX_CONF_ERROR);
915 771
916 /* set by ngx_pcalloc(): 772 /* set by ngx_pcalloc():
917 773
918 lcf->doc_root.len = 0; 774 lcf->doc_root.len = 0;
919 lcf->doc_root.data = NULL; 775 lcf->doc_root.data = NULL;
920 lcf->types = NULL; 776 lcf->types = NULL;
777 lcf->default_type.len = 0;
778 lcf->default_type.data = NULL;
921 779
922 */ 780 */
923 781
924 lcf->sendfile = NGX_CONF_UNSET; 782 lcf->sendfile = NGX_CONF_UNSET;
925 783
948 ngx_http_core_loc_conf_t *conf = (ngx_http_core_loc_conf_t *) child; 806 ngx_http_core_loc_conf_t *conf = (ngx_http_core_loc_conf_t *) child;
949 807
950 int i, key; 808 int i, key;
951 ngx_http_type_t *t; 809 ngx_http_type_t *t;
952 810
953 if (conf->doc_root.len == 0) { 811 ngx_conf_merge_str_value(conf->doc_root,
954 if (prev->doc_root.len) { 812 prev->doc_root, "html");
955 conf->doc_root.len = prev->doc_root.len;
956 conf->doc_root.data = prev->doc_root.data;
957
958 } else {
959 conf->doc_root.len = 4;
960 conf->doc_root.data = "html";
961 }
962 }
963 813
964 if (conf->types == NULL) { 814 if (conf->types == NULL) {
965 if (prev->types) { 815 if (prev->types) {
966 conf->types = prev->types; 816 conf->types = prev->types;
967 817
986 t->type.len = default_types[i].type.len; 836 t->type.len = default_types[i].type.len;
987 t->type.data = default_types[i].type.data; 837 t->type.data = default_types[i].type.data;
988 } 838 }
989 } 839 }
990 } 840 }
841
842 ngx_conf_merge_str_value(conf->default_type,
843 prev->default_type, "text/plain");
991 844
992 ngx_conf_merge_value(conf->sendfile, prev->sendfile, 0); 845 ngx_conf_merge_value(conf->sendfile, prev->sendfile, 0);
993 ngx_conf_merge_msec_value(conf->send_timeout, prev->send_timeout, 10000); 846 ngx_conf_merge_msec_value(conf->send_timeout, prev->send_timeout, 10000);
994 ngx_conf_merge_size_value(conf->discarded_buffer_size, 847 ngx_conf_merge_size_value(conf->discarded_buffer_size,
995 prev->discarded_buffer_size, 1500); 848 prev->discarded_buffer_size, 1500);
1002 855
1003 return NGX_CONF_OK; 856 return NGX_CONF_OK;
1004 } 857 }
1005 858
1006 859
1007 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, char *conf) 860 static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1008 { 861 {
1009 ngx_http_core_srv_conf_t *scf = (ngx_http_core_srv_conf_t *) conf; 862 ngx_http_core_srv_conf_t *scf = (ngx_http_core_srv_conf_t *) conf;
1010 863
1011 char *addr; 864 char *addr;
1012 u_int p; 865 u_int p;
866 struct hostent *h;
1013 ngx_str_t *args; 867 ngx_str_t *args;
1014 ngx_http_listen_t *ls; 868 ngx_http_listen_t *ls;
1015 869
1016 /* TODO: check duplicate 'listen' directives */ 870 /* TODO: check duplicate 'listen' directives,
871 add resolved name to server names */
1017 872
1018 ngx_test_null(ls, ngx_push_array(&scf->listen), NGX_CONF_ERROR); 873 ngx_test_null(ls, ngx_push_array(&scf->listen), NGX_CONF_ERROR);
1019 874
1020 /* AF_INET only */ 875 /* AF_INET only */
1021 876
1031 if (addr[p] == ':') { 886 if (addr[p] == ':') {
1032 addr[p++] = '\0'; 887 addr[p++] = '\0';
1033 888
1034 ls->addr = inet_addr(addr); 889 ls->addr = inet_addr(addr);
1035 if (ls->addr == INADDR_NONE) { 890 if (ls->addr == INADDR_NONE) {
1036 /* TODO: gethostbyname() */ 891 h = gethostbyname(addr);
1037 return "can not resolve host name"; 892
893 if (h == NULL || h->h_addr_list[0] == NULL) {
894 return "can not resolve host name";
895 }
896
897 ls->addr = *(u_int32_t *)(h->h_addr_list[0]);
1038 } 898 }
1039 899
1040 break; 900 break;
1041 } 901 }
1042 } 902 }