comparison src/http/ngx_http_core_module.c @ 3217:a22bf524a456

refactor http listen code: *) add listen's to the global cmcf->ports array instead of server's one *) rename ngx_http_listen_conf_t to ngx_http_listen_opt_t
author Igor Sysoev <igor@sysoev.ru>
date Wed, 21 Oct 2009 08:19:46 +0000
parents 975f0558aab3
children 81b8416054b0
comparison
equal deleted inserted replaced
3216:79ae445ec57b 3217:a22bf524a456
2805 * set by ngx_pcalloc(): 2805 * set by ngx_pcalloc():
2806 * 2806 *
2807 * conf->client_large_buffers.num = 0; 2807 * conf->client_large_buffers.num = 0;
2808 */ 2808 */
2809 2809
2810 if (ngx_array_init(&cscf->listen, cf->temp_pool, 4,
2811 sizeof(ngx_http_listen_t))
2812 != NGX_OK)
2813 {
2814 return NULL;
2815 }
2816
2817 if (ngx_array_init(&cscf->server_names, cf->temp_pool, 4, 2810 if (ngx_array_init(&cscf->server_names, cf->temp_pool, 4,
2818 sizeof(ngx_http_server_name_t)) 2811 sizeof(ngx_http_server_name_t))
2819 != NGX_OK) 2812 != NGX_OK)
2820 { 2813 {
2821 return NULL; 2814 return NULL;
2837 ngx_http_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) 2830 ngx_http_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
2838 { 2831 {
2839 ngx_http_core_srv_conf_t *prev = parent; 2832 ngx_http_core_srv_conf_t *prev = parent;
2840 ngx_http_core_srv_conf_t *conf = child; 2833 ngx_http_core_srv_conf_t *conf = child;
2841 2834
2842 ngx_http_listen_t *ls; 2835 ngx_http_listen_t ls;
2843 struct sockaddr_in *sin; 2836 struct sockaddr_in *sin;
2844 ngx_http_server_name_t *sn; 2837 ngx_http_server_name_t *sn;
2845 2838
2846 /* TODO: it does not merge, it inits only */ 2839 /* TODO: it does not merge, it inits only */
2847 2840
2848 if (conf->listen.nelts == 0) { 2841 if (!conf->listen) {
2849 ls = ngx_array_push(&conf->listen); 2842 ngx_memzero(&ls, sizeof(ngx_http_listen_t));
2850 if (ls == NULL) { 2843
2851 return NGX_CONF_ERROR; 2844 sin = (struct sockaddr_in *) &ls.sockaddr;
2852 }
2853
2854 ngx_memzero(ls, sizeof(ngx_http_listen_t));
2855
2856 sin = (struct sockaddr_in *) &ls->sockaddr;
2857 2845
2858 sin->sin_family = AF_INET; 2846 sin->sin_family = AF_INET;
2859 #if (NGX_WIN32) 2847 #if (NGX_WIN32)
2860 sin->sin_port = htons(80); 2848 sin->sin_port = htons(80);
2861 #else 2849 #else
2862 sin->sin_port = htons((getuid() == 0) ? 80 : 8000); 2850 sin->sin_port = htons((getuid() == 0) ? 80 : 8000);
2863 #endif 2851 #endif
2864 sin->sin_addr.s_addr = INADDR_ANY; 2852 sin->sin_addr.s_addr = INADDR_ANY;
2865 2853
2866 ls->socklen = sizeof(struct sockaddr_in); 2854 ls.socklen = sizeof(struct sockaddr_in);
2867 2855
2868 ls->conf.backlog = NGX_LISTEN_BACKLOG; 2856 ls.opt.backlog = NGX_LISTEN_BACKLOG;
2869 ls->conf.rcvbuf = -1; 2857 ls.opt.rcvbuf = -1;
2870 ls->conf.sndbuf = -1; 2858 ls.opt.sndbuf = -1;
2871 ls->conf.wildcard = 1; 2859 ls.opt.wildcard = 1;
2872 2860
2873 (void) ngx_sock_ntop((struct sockaddr *) &ls->sockaddr, ls->conf.addr, 2861 (void) ngx_sock_ntop((struct sockaddr *) &ls.sockaddr, ls.opt.addr,
2874 NGX_SOCKADDR_STRLEN, 1); 2862 NGX_SOCKADDR_STRLEN, 1);
2863
2864 if (ngx_http_add_listen(cf, conf, &ls) == NGX_OK) {
2865 return NGX_CONF_OK;
2866 }
2875 } 2867 }
2876 2868
2877 if (conf->server_name.data == NULL) { 2869 if (conf->server_name.data == NULL) {
2878 conf->server_name = cf->cycle->hostname; 2870 conf->server_name = cf->cycle->hostname;
2879 2871
3271 3263
3272 3264
3273 static char * 3265 static char *
3274 ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 3266 ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3275 { 3267 {
3276 ngx_http_core_srv_conf_t *scf = conf; 3268 ngx_http_core_srv_conf_t *cscf = conf;
3277 3269
3278 ngx_str_t *value, size; 3270 ngx_str_t *value, size;
3279 ngx_url_t u; 3271 ngx_url_t u;
3280 ngx_uint_t n; 3272 ngx_uint_t n;
3281 ngx_http_listen_t *ls; 3273 ngx_http_listen_t ls;
3282 3274
3283 /* 3275 cscf->listen = 1;
3284 * TODO: check duplicate 'listen' directives,
3285 * add resolved name to server names ???
3286 */
3287 3276
3288 value = cf->args->elts; 3277 value = cf->args->elts;
3289 3278
3290 ngx_memzero(&u, sizeof(ngx_url_t)); 3279 ngx_memzero(&u, sizeof(ngx_url_t));
3291 3280
3301 } 3290 }
3302 3291
3303 return NGX_CONF_ERROR; 3292 return NGX_CONF_ERROR;
3304 } 3293 }
3305 3294
3306 ls = ngx_array_push(&scf->listen); 3295 ngx_memzero(&ls, sizeof(ngx_http_listen_t));
3307 if (ls == NULL) { 3296
3308 return NGX_CONF_ERROR; 3297 ngx_memcpy(ls.sockaddr, u.sockaddr, u.socklen);
3309 } 3298
3310 3299 ls.socklen = u.socklen;
3311 ngx_memzero(ls, sizeof(ngx_http_listen_t)); 3300 ls.opt.backlog = NGX_LISTEN_BACKLOG;
3312 3301 ls.opt.rcvbuf = -1;
3313 ngx_memcpy(ls->sockaddr, u.sockaddr, u.socklen); 3302 ls.opt.sndbuf = -1;
3314 3303 ls.opt.wildcard = u.wildcard;
3315 ls->socklen = u.socklen; 3304
3316 ls->file_name = cf->conf_file->file.name.data; 3305 (void) ngx_sock_ntop((struct sockaddr *) &ls.sockaddr, ls.opt.addr,
3317 ls->line = cf->conf_file->line;
3318 ls->conf.backlog = NGX_LISTEN_BACKLOG;
3319 ls->conf.rcvbuf = -1;
3320 ls->conf.sndbuf = -1;
3321 ls->conf.wildcard = u.wildcard;
3322
3323 (void) ngx_sock_ntop((struct sockaddr *) &ls->sockaddr, ls->conf.addr,
3324 NGX_SOCKADDR_STRLEN, 1); 3306 NGX_SOCKADDR_STRLEN, 1);
3325 3307
3326 if (cf->args->nelts == 2) { 3308 if (cf->args->nelts > 2 && ngx_strcmp(value[2].data, "default") == 0) {
3327 return NGX_CONF_OK; 3309 ls.opt.default_server = 1;
3328 }
3329
3330 if (ngx_strcmp(value[2].data, "default") == 0) {
3331 ls->conf.default_server = 1;
3332 n = 3; 3310 n = 3;
3333 3311
3334 } else { 3312 } else {
3335 n = 2; 3313 n = 2;
3336 } 3314 }
3337 3315
3338 for ( /* void */ ; n < cf->args->nelts; n++) { 3316 for ( /* void */ ; n < cf->args->nelts; n++) {
3339 3317
3340 if (ls->conf.default_server == 0) { 3318 if (ls.opt.default_server == 0) {
3341 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 3319 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3342 "\"%V\" parameter can be specified for " 3320 "\"%V\" parameter can be specified for "
3343 "the default \"listen\" directive only", 3321 "the default \"listen\" directive only",
3344 &value[n]); 3322 &value[n]);
3345 return NGX_CONF_ERROR; 3323 return NGX_CONF_ERROR;
3346 } 3324 }
3347 3325
3348 if (ngx_strcmp(value[n].data, "bind") == 0) { 3326 if (ngx_strcmp(value[n].data, "bind") == 0) {
3349 ls->conf.bind = 1; 3327 ls.opt.bind = 1;
3350 continue; 3328 continue;
3351 } 3329 }
3352 3330
3353 if (ngx_strncmp(value[n].data, "backlog=", 8) == 0) { 3331 if (ngx_strncmp(value[n].data, "backlog=", 8) == 0) {
3354 ls->conf.backlog = ngx_atoi(value[n].data + 8, value[n].len - 8); 3332 ls.opt.backlog = ngx_atoi(value[n].data + 8, value[n].len - 8);
3355 ls->conf.bind = 1; 3333 ls.opt.bind = 1;
3356 3334
3357 if (ls->conf.backlog == NGX_ERROR || ls->conf.backlog == 0) { 3335 if (ls.opt.backlog == NGX_ERROR || ls.opt.backlog == 0) {
3358 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 3336 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3359 "invalid backlog \"%V\"", &value[n]); 3337 "invalid backlog \"%V\"", &value[n]);
3360 return NGX_CONF_ERROR; 3338 return NGX_CONF_ERROR;
3361 } 3339 }
3362 3340
3365 3343
3366 if (ngx_strncmp(value[n].data, "rcvbuf=", 7) == 0) { 3344 if (ngx_strncmp(value[n].data, "rcvbuf=", 7) == 0) {
3367 size.len = value[n].len - 7; 3345 size.len = value[n].len - 7;
3368 size.data = value[n].data + 7; 3346 size.data = value[n].data + 7;
3369 3347
3370 ls->conf.rcvbuf = ngx_parse_size(&size); 3348 ls.opt.rcvbuf = ngx_parse_size(&size);
3371 ls->conf.bind = 1; 3349 ls.opt.bind = 1;
3372 3350
3373 if (ls->conf.rcvbuf == NGX_ERROR) { 3351 if (ls.opt.rcvbuf == NGX_ERROR) {
3374 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 3352 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3375 "invalid rcvbuf \"%V\"", &value[n]); 3353 "invalid rcvbuf \"%V\"", &value[n]);
3376 return NGX_CONF_ERROR; 3354 return NGX_CONF_ERROR;
3377 } 3355 }
3378 3356
3381 3359
3382 if (ngx_strncmp(value[n].data, "sndbuf=", 7) == 0) { 3360 if (ngx_strncmp(value[n].data, "sndbuf=", 7) == 0) {
3383 size.len = value[n].len - 7; 3361 size.len = value[n].len - 7;
3384 size.data = value[n].data + 7; 3362 size.data = value[n].data + 7;
3385 3363
3386 ls->conf.sndbuf = ngx_parse_size(&size); 3364 ls.opt.sndbuf = ngx_parse_size(&size);
3387 ls->conf.bind = 1; 3365 ls.opt.bind = 1;
3388 3366
3389 if (ls->conf.sndbuf == NGX_ERROR) { 3367 if (ls.opt.sndbuf == NGX_ERROR) {
3390 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 3368 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3391 "invalid sndbuf \"%V\"", &value[n]); 3369 "invalid sndbuf \"%V\"", &value[n]);
3392 return NGX_CONF_ERROR; 3370 return NGX_CONF_ERROR;
3393 } 3371 }
3394 3372
3395 continue; 3373 continue;
3396 } 3374 }
3397 3375
3398 if (ngx_strncmp(value[n].data, "accept_filter=", 14) == 0) { 3376 if (ngx_strncmp(value[n].data, "accept_filter=", 14) == 0) {
3399 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) 3377 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
3400 ls->conf.accept_filter = (char *) &value[n].data[14]; 3378 ls.opt.accept_filter = (char *) &value[n].data[14];
3401 ls->conf.bind = 1; 3379 ls.opt.bind = 1;
3402 #else 3380 #else
3403 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 3381 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3404 "accept filters \"%V\" are not supported " 3382 "accept filters \"%V\" are not supported "
3405 "on this platform, ignored", 3383 "on this platform, ignored",
3406 &value[n]); 3384 &value[n]);
3408 continue; 3386 continue;
3409 } 3387 }
3410 3388
3411 if (ngx_strcmp(value[n].data, "deferred") == 0) { 3389 if (ngx_strcmp(value[n].data, "deferred") == 0) {
3412 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) 3390 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
3413 ls->conf.deferred_accept = 1; 3391 ls.opt.deferred_accept = 1;
3414 ls->conf.bind = 1; 3392 ls.opt.bind = 1;
3415 #else 3393 #else
3416 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 3394 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3417 "the deferred accept is not supported " 3395 "the deferred accept is not supported "
3418 "on this platform, ignored"); 3396 "on this platform, ignored");
3419 #endif 3397 #endif
3422 3400
3423 if (ngx_strncmp(value[n].data, "ipv6only=o", 10) == 0) { 3401 if (ngx_strncmp(value[n].data, "ipv6only=o", 10) == 0) {
3424 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY) 3402 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
3425 struct sockaddr *sa; 3403 struct sockaddr *sa;
3426 3404
3427 sa = (struct sockaddr *) ls->sockaddr; 3405 sa = (struct sockaddr *) ls.sockaddr;
3428 3406
3429 if (sa->sa_family == AF_INET6) { 3407 if (sa->sa_family == AF_INET6) {
3430 3408
3431 if (ngx_strcmp(&value[n].data[10], "n") == 0) { 3409 if (ngx_strcmp(&value[n].data[10], "n") == 0) {
3432 ls->conf.ipv6only = 1; 3410 ls.opt.ipv6only = 1;
3433 3411
3434 } else if (ngx_strcmp(&value[n].data[10], "ff") == 0) { 3412 } else if (ngx_strcmp(&value[n].data[10], "ff") == 0) {
3435 ls->conf.ipv6only = 2; 3413 ls.opt.ipv6only = 2;
3436 3414
3437 } else { 3415 } else {
3438 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 3416 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3439 "invalid ipv6only flags \"%s\"", 3417 "invalid ipv6only flags \"%s\"",
3440 &value[n].data[9]); 3418 &value[n].data[9]);
3441 return NGX_CONF_ERROR; 3419 return NGX_CONF_ERROR;
3442 } 3420 }
3443 3421
3444 ls->conf.bind = 1; 3422 ls.opt.bind = 1;
3445 3423
3446 } else { 3424 } else {
3447 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 3425 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3448 "ipv6only is not supported " 3426 "ipv6only is not supported "
3449 "on addr \"%s\", ignored", 3427 "on addr \"%s\", ignored", ls.opt.addr);
3450 ls->conf.addr);
3451 } 3428 }
3452 3429
3453 continue; 3430 continue;
3454 #else 3431 #else
3455 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 3432 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3459 #endif 3436 #endif
3460 } 3437 }
3461 3438
3462 if (ngx_strcmp(value[n].data, "ssl") == 0) { 3439 if (ngx_strcmp(value[n].data, "ssl") == 0) {
3463 #if (NGX_HTTP_SSL) 3440 #if (NGX_HTTP_SSL)
3464 ls->conf.ssl = 1; 3441 ls.opt.ssl = 1;
3465 continue; 3442 continue;
3466 #else 3443 #else
3467 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 3444 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3468 "the \"ssl\" parameter requires " 3445 "the \"ssl\" parameter requires "
3469 "ngx_http_ssl_module"); 3446 "ngx_http_ssl_module");
3474 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 3451 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3475 "the invalid \"%V\" parameter", &value[n]); 3452 "the invalid \"%V\" parameter", &value[n]);
3476 return NGX_CONF_ERROR; 3453 return NGX_CONF_ERROR;
3477 } 3454 }
3478 3455
3479 return NGX_CONF_OK; 3456 if (ngx_http_add_listen(cf, cscf, &ls) == NGX_OK) {
3457 return NGX_CONF_OK;
3458 }
3459
3460 return NGX_CONF_ERROR;
3480 } 3461 }
3481 3462
3482 3463
3483 static char * 3464 static char *
3484 ngx_http_core_server_name(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 3465 ngx_http_core_server_name(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)