# HG changeset patch # User Igor Sysoev # Date 1053098868 0 # Node ID 37530da31268ab7a8cabc39ac6e07762308fe5a3 # Parent 29bf798b583f50c09ebe985211a8953f4c58aef2 nginx-0.0.1-2003-05-16-19:27:48 import diff --git a/src/core/nginx.c b/src/core/nginx.c --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -38,6 +38,7 @@ ngx_pool_t *ngx_pool; int ngx_max_module; +void *ctx_conf; int ngx_connection_counter; @@ -81,8 +82,14 @@ int main(int argc, char *const *argv) } ngx_memzero(&conf, sizeof(ngx_conf_t)); - ngx_test_null(conf.args, - ngx_create_array(ngx_pool, 10, sizeof(ngx_str_t)), 1); + + ngx_test_null(conf.args, ngx_create_array(ngx_pool, 10, sizeof(ngx_str_t)), + 1); + + ngx_test_null(conf.ctx, + ngx_pcalloc(ngx_pool, ngx_max_module * sizeof(void *)), + 1); + conf.pool = ngx_pool; conf.log = &ngx_log; conf.module_type = NGX_CORE_MODULE_TYPE; diff --git a/src/core/ngx_conf_file.c b/src/core/ngx_conf_file.c --- a/src/core/ngx_conf_file.c +++ b/src/core/ngx_conf_file.c @@ -27,6 +27,8 @@ char *ngx_conf_parse(ngx_conf_t *cf, ngx if (filename) { + /* open configuration file */ + fd = ngx_open_file(filename->data, NGX_FILE_RDONLY); if (fd == NGX_INVALID_FILE) { ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno, @@ -60,7 +62,8 @@ char *ngx_conf_parse(ngx_conf_t *cf, ngx for ( ;; ) { rc = ngx_conf_read_token(cf); - /* NGX_OK, NGX_ERROR, NGX_CONF_FILE_DONE, NGX_CONF_BLOCK_DONE */ + /* ngx_conf_read_token() returns NGX_OK, NGX_ERROR, + NGX_CONF_FILE_DONE or NGX_CONF_BLOCK_DONE */ #if 0 ngx_log_debug(cf->log, "token %d" _ rc); @@ -76,6 +79,8 @@ ngx_log_debug(cf->log, "token %d" _ rc); if (cf->handler) { + /* custom handler, i.e. used in http "types { ... }" directive */ + rv = (*cf->handler)(cf, NULL, cf->handler_conf); if (rv == NGX_CONF_OK) { continue; @@ -97,6 +102,9 @@ ngx_log_debug(cf->log, "token %d" _ rc); found = 0; for (i = 0; !found && ngx_modules[i]; i++) { + + /* look up the directive in the appropriate modules */ + if (ngx_modules[i]->type != NGX_CONF_MODULE_TYPE && ngx_modules[i]->type != cf->module_type) { @@ -116,6 +124,7 @@ ngx_log_debug(cf->log, "token %d" _ rc); #if 0 ngx_log_debug(cf->log, "command '%s'" _ cmd->name.data); #endif + /* is the directive's location right ? */ if ((cmd->type & cf->cmd_type) == 0) { ngx_log_error(NGX_LOG_EMERG, cf->log, 0, @@ -127,6 +136,8 @@ ngx_log_debug(cf->log, "command '%s'" _ return NGX_CONF_ERROR; } + /* is the directive's argument count right ? */ + if (!(cmd->type & NGX_CONF_ANY) && ((cmd->type & NGX_CONF_FLAG && cf->args->nelts != 2) || (!(cmd->type & NGX_CONF_FLAG) @@ -145,8 +156,14 @@ ngx_log_debug(cf->log, "command '%s'" _ return NGX_CONF_ERROR; } + /* set up the directive's configuration context */ + conf = NULL; - if (cf->ctx) { + + if (cf->module_type == NGX_CORE_MODULE_TYPE) { + conf = &(((void **) cf->ctx)[ngx_modules[i]->index]); + + } else if (cf->ctx) { pconf = *(void **) ((char *) cf->ctx + cmd->conf); if (pconf) { @@ -197,7 +214,7 @@ ngx_log_debug(cf->log, "rv: %d" _ rv); cf->conf_file = prev; if (ngx_close_file(fd) == NGX_FILE_ERROR) { - ngx_log_error(NGX_LOG_ERR, cf->log, ngx_errno, + ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno, ngx_close_file_n " %s failed", cf->conf_file->file.name.data); return NGX_CONF_ERROR; @@ -450,6 +467,26 @@ char *ngx_conf_set_str_slot(ngx_conf_t * } +char *ngx_conf_set_num_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf) +{ + int num, len; + ngx_str_t *value; + + value = (ngx_str_t *) cf->args->elts; + + len = value[1].len; + + num = ngx_atoi(value[1].data, len); + if (num == NGX_ERROR) { + return "invalid value"; + } + + *(int *) (conf + cmd->offset) = num; + + return NGX_CONF_OK; +} + + char *ngx_conf_set_size_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf) { int size, len, scale; diff --git a/src/core/ngx_conf_file.h b/src/core/ngx_conf_file.h --- a/src/core/ngx_conf_file.h +++ b/src/core/ngx_conf_file.h @@ -17,18 +17,17 @@ * AAAA number of agruments * FF command flags * TT command type, i.e. HTTP "location" or "server" command - * 00 */ -#define NGX_CONF_NOARGS 0x0000000001 -#define NGX_CONF_TAKE1 0x0000000002 -#define NGX_CONF_TAKE2 0x0000000004 -#define NGX_CONF_ARGS_NUMBER 0x000000ffff -#define NGX_CONF_ANY 0x0000010000 -#define NGX_CONF_BLOCK 0x0000020000 -#define NGX_CONF_FLAG 0x0000040000 +#define NGX_CONF_NOARGS 0x00000001 +#define NGX_CONF_TAKE1 0x00000002 +#define NGX_CONF_TAKE2 0x00000004 +#define NGX_CONF_ARGS_NUMBER 0x0000ffff +#define NGX_CONF_ANY 0x00010000 +#define NGX_CONF_BLOCK 0x00020000 +#define NGX_CONF_FLAG 0x00040000 -#define NGX_MAIN_CONF 0x0001000000 +#define NGX_MAIN_CONF 0x01000000 @@ -61,8 +60,8 @@ struct ngx_command_s { typedef struct { + void *ctx; int index; - void *ctx; ngx_command_t *commands; int type; int (*init_module)(ngx_pool_t *p); @@ -121,6 +120,7 @@ char *ngx_conf_parse(ngx_conf_t *cf, ngx char *ngx_conf_set_flag_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf); char *ngx_conf_set_str_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf); +char *ngx_conf_set_num_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf); char *ngx_conf_set_size_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf); char *ngx_conf_set_msec_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf); char *ngx_conf_set_time_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf); diff --git a/src/core/ngx_modules.c b/src/core/ngx_modules.c --- a/src/core/ngx_modules.c +++ b/src/core/ngx_modules.c @@ -4,6 +4,10 @@ #include +extern ngx_module_t ngx_events_module; +extern ngx_module_t ngx_event_module; + + extern ngx_module_t ngx_http_module; extern ngx_module_t ngx_http_core_module; @@ -17,6 +21,13 @@ extern ngx_module_t ngx_http_proxy_modu ngx_module_t *ngx_modules[] = { + /* events */ + + &ngx_events_module, + &ngx_event_module, + + /* http */ + &ngx_http_module, &ngx_http_core_module, diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c --- a/src/event/modules/ngx_kqueue_module.c +++ b/src/event/modules/ngx_kqueue_module.c @@ -10,6 +10,7 @@ #include #include #include +#include #include @@ -18,6 +19,10 @@ #define KQUEUE_NEVENTS 512 +static int ngx_kqueue_changes; +static int ngx_kqueue_events; + + /* should be per-thread if threads are used without thread pool */ #if 1 int kq; @@ -32,6 +37,37 @@ static ngx_event_t *timer_queue; /* */ +static ngx_str_t kqueue_name = ngx_string("kqueue"); + +static ngx_command_t ngx_kqueue_commands[] = { + + {ngx_string("kqueue_changes"), + NGX_EVENT_CONF|NGX_CONF_TAKE1, + ngx_conf_set_num_slot, + 0, + addressof(ngx_kqueue_changes), + NULL}, + + {ngx_string("kqueue_events"), + NGX_EVENT_CONF|NGX_CONF_TAKE1, + ngx_conf_set_num_slot, + 0, + addressof(ngx_kqueue_events), + NULL}, + + {ngx_string(""), 0, NULL, 0, 0, NULL} +}; + +ngx_module_t ngx_kqueue_module = { + &kqueue_name, /* module context */ + 0, /* module index */ + ngx_kqueue_commands, /* module directives */ + NGX_EVENT_MODULE_TYPE, /* module type */ + NULL /* init module */ +}; + + + int ngx_kqueue_init(int max_connections, ngx_log_t *log) { int change_size, event_size; diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c --- a/src/event/ngx_event.c +++ b/src/event/ngx_event.c @@ -9,6 +9,7 @@ #include #include #include +#include #include @@ -34,6 +35,9 @@ #endif +static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy); + + ngx_connection_t *ngx_connections; ngx_event_t *ngx_read_events, *ngx_write_events; @@ -68,6 +72,66 @@ static int (*ngx_event_init[]) (int max_ #endif /* USE_KQUEUE */ +static int ngx_event_connections; + + +static ngx_str_t events_name = ngx_string("events"); + +static ngx_command_t ngx_events_commands[] = { + + {ngx_string("events"), + NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, + ngx_events_block, + 0, + 0, + NULL}, + + {ngx_string(""), 0, NULL, 0, 0, NULL} +}; + + +ngx_module_t ngx_events_module = { + &events_name, /* module context */ + 0, /* module index */ + ngx_events_commands, /* module directives */ + NGX_CORE_MODULE_TYPE, /* module type */ + NULL /* init module */ +}; + + + +static ngx_command_t ngx_event_commands[] = { + + {ngx_string("connections"), + NGX_EVENT_CONF|NGX_CONF_TAKE1, + ngx_conf_set_num_slot, + 0, + addressof(ngx_event_connections), + NULL}, + +#if 0 + {ngx_string("type"), + NGX_EVENT_CONF|NGX_CONF_TAKE1, + ngx_event_set_type, + 0, + 0, + NULL}, +#endif + + {ngx_string(""), 0, NULL, 0, 0, NULL} +}; + + +ngx_module_t ngx_event_module = { + NULL, /* module context */ + 0, /* module index */ + ngx_events_commands, /* module directives */ + NGX_EVENT_MODULE_TYPE, /* module type */ + NULL /* init module */ +}; + + + void ngx_pre_thread(ngx_array_t *ls, ngx_pool_t *pool, ngx_log_t *log) { int i, fd; @@ -173,6 +237,7 @@ void ngx_pre_thread(ngx_array_t *ls, ngx } } + void ngx_worker(ngx_log_t *log) { for ( ;; ) { @@ -181,3 +246,44 @@ void ngx_worker(ngx_log_t *log) ngx_process_events(log); } } + + +static char *ngx_events_init(ngx_pool_t *pool) +{ + ngx_event_connections = -1; + ngx_event_type = -1; + + return NGX_CONF_OK; +} + + +static char *ngx_events_postconf(ngx_pool_t *pool) +{ + if (ngx_event_connections == -1) { + ngx_event_connections = 512; + } + + return NGX_CONF_OK; +} + + +static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, char *conf) +{ + char *rv; + ngx_conf_t pcf; + +#if 0 + *(ngx_events_conf_ctx_t **) conf = ctx; +#endif + + pcf = *cf; + cf->module_type = NGX_EVENT_MODULE_TYPE; + cf->cmd_type = NGX_EVENT_CONF; + rv = ngx_conf_parse(cf, NULL); + *cf = pcf; + + if (rv != NGX_CONF_OK) + return rv; + + return NGX_CONF_OK; +} diff --git a/src/event/ngx_event.h b/src/event/ngx_event.h --- a/src/event/ngx_event.h +++ b/src/event/ngx_event.h @@ -298,6 +298,12 @@ extern int ngx_event_f #endif +#define NGX_EVENT_MODULE_TYPE 0x544E5645 /* "EVNT" */ + +#define NGX_EVENT_CONF 0x00200000 + + + void ngx_event_accept(ngx_event_t *ev); diff --git a/src/http/modules/ngx_http_index_handler.c b/src/http/modules/ngx_http_index_handler.c --- a/src/http/modules/ngx_http_index_handler.c +++ b/src/http/modules/ngx_http_index_handler.c @@ -47,8 +47,8 @@ ngx_http_module_t ngx_http_index_module ngx_module_t ngx_http_index_module = { + &ngx_http_index_module_ctx, /* module context */ 0, /* module index */ - &ngx_http_index_module_ctx, /* module context */ ngx_http_index_commands, /* module directives */ NGX_HTTP_MODULE_TYPE, /* module type */ ngx_http_index_init /* init module */ diff --git a/src/http/ngx_http.c b/src/http/ngx_http.c --- a/src/http/ngx_http.c +++ b/src/http/ngx_http.c @@ -53,15 +53,15 @@ static ngx_command_t ngx_http_commands[ ngx_module_t ngx_http_module = { + &http_name, /* module context */ 0, /* module index */ - &http_name, /* module context */ ngx_http_commands, /* module directives */ NGX_CORE_MODULE_TYPE, /* module type */ NULL /* init module */ }; -static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy) +static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, char *conf) { int i, s, l, p, a, n, start; int port_found, addr_found, virtual_names; @@ -70,7 +70,7 @@ static char *ngx_http_block(ngx_conf_t * ngx_array_t in_ports; ngx_listen_t *ls; ngx_http_module_t *module; - ngx_conf_t prev; + ngx_conf_t pcf; ngx_http_conf_ctx_t *ctx; ngx_http_in_port_t *in_port, *inport; ngx_http_in_addr_t *in_addr, *inaddr; @@ -85,6 +85,8 @@ static char *ngx_http_block(ngx_conf_t * ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)), NGX_CONF_ERROR); + *(ngx_http_conf_ctx_t **) conf = ctx; + ngx_http_max_module = 0; for (i = 0; ngx_modules[i]; i++) { if (ngx_modules[i]->type != NGX_HTTP_MODULE_TYPE) { @@ -96,7 +98,20 @@ static char *ngx_http_block(ngx_conf_t * module->index = ngx_http_max_module++; } - /* null loc_conf */ + /* TODO: http main_conf */ + + ngx_test_null(ctx->main_conf, + ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module), + NGX_CONF_ERROR); + + /* TODO: http srv_conf */ + + ngx_test_null(ctx->srv_conf, + ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module), + NGX_CONF_ERROR); + + /* http null loc_conf */ + ngx_test_null(ctx->loc_conf, ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module), NGX_CONF_ERROR); @@ -115,12 +130,12 @@ static char *ngx_http_block(ngx_conf_t * } } - prev = *cf; + pcf = *cf; cf->ctx = ctx; cf->module_type = NGX_HTTP_MODULE_TYPE; cf->cmd_type = NGX_HTTP_MAIN_CONF; rv = ngx_conf_parse(cf, NULL); - *cf = prev; + *cf = pcf; if (rv != NGX_CONF_OK) return rv; diff --git a/src/http/ngx_http_config.h b/src/http/ngx_http_config.h --- a/src/http/ngx_http_config.h +++ b/src/http/ngx_http_config.h @@ -7,6 +7,7 @@ typedef struct { + void **main_conf; void **srv_conf; void **loc_conf; } ngx_http_conf_ctx_t; @@ -38,20 +39,19 @@ typedef struct { #define NGX_HTTP_LOC_CONF 0x8000000 -#define NGX_HTTP_SRV_CONF_OFFSET offsetof(ngx_http_conf_ctx_t, srv_conf) -#define NGX_HTTP_LOC_CONF_OFFSET offsetof(ngx_http_conf_ctx_t, loc_conf) +#define NGX_HTTP_MAIN_CONF_OFFSET offsetof(ngx_http_conf_ctx_t, main_conf) +#define NGX_HTTP_SRV_CONF_OFFSET offsetof(ngx_http_conf_ctx_t, srv_conf) +#define NGX_HTTP_LOC_CONF_OFFSET offsetof(ngx_http_conf_ctx_t, loc_conf) -#define ngx_http_get_module_srv_conf(r, module) r->srv_conf[module.index] -#define ngx_http_get_module_loc_conf(r, module) r->loc_conf[module.index] +#define ngx_http_get_module_main_conf(r, ctx) r->main_conf[ctx.index] +#define ngx_http_get_module_srv_conf(r, ctx) r->srv_conf[ctx.index] +#define ngx_http_get_module_loc_conf(r, ctx) r->loc_conf[ctx.index] int ngx_http_config_modules(ngx_pool_t *pool, ngx_module_t **modules); -extern ngx_module_t ngx_http_module; - - extern int (*ngx_http_top_header_filter) (ngx_http_request_t *r); diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c --- a/src/http/ngx_http_core_module.c +++ b/src/http/ngx_http_core_module.c @@ -74,8 +74,8 @@ static ngx_command_t ngx_http_core_comm {ngx_string("client_header_buffer_size"), NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, ngx_conf_set_size_slot, - 0, - addressof(ngx_http_client_header_buffer_size), + NGX_HTTP_MAIN_CONF_OFFSET, + offsetof(ngx_http_core_main_conf_t, client_header_buffer_size), NULL}, {ngx_string("large_client_header"), @@ -157,8 +157,8 @@ ngx_http_module_t ngx_http_core_module_ ngx_module_t ngx_http_core_module = { + &ngx_http_core_module_ctx, /* module context */ 0, /* module index */ - &ngx_http_core_module_ctx, /* module context */ ngx_http_core_commands, /* module directives */ NGX_HTTP_MODULE_TYPE, /* module type */ ngx_http_core_init /* init module */ @@ -615,7 +615,7 @@ static char *ngx_server_block(ngx_conf_t char *rv; ngx_http_module_t *module; ngx_conf_t pcf; - ngx_http_conf_ctx_t *ctx, *pctx; + ngx_http_conf_ctx_t *ctx, *tctx, *pctx; ngx_http_core_srv_conf_t *scf; ngx_http_core_loc_conf_t **plcf; @@ -623,12 +623,17 @@ static char *ngx_server_block(ngx_conf_t ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)), NGX_CONF_ERROR); - /* server config */ + tctx = (ngx_http_conf_ctx_t *) cf->ctx; + ctx->main_conf = tctx->main_conf; + + /* server configuration */ + ngx_test_null(ctx->srv_conf, ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module), NGX_CONF_ERROR); - /* server location config */ + /* server location configuration */ + ngx_test_null(ctx->loc_conf, ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module), NGX_CONF_ERROR); diff --git a/src/http/ngx_http_core_module.h b/src/http/ngx_http_core_module.h --- a/src/http/ngx_http_core_module.h +++ b/src/http/ngx_http_core_module.h @@ -18,6 +18,12 @@ typedef struct { typedef struct { + int request_pool_size; + int client_header_buffer_size; +} ngx_http_core_main_conf_t; + + +typedef struct { ngx_array_t locations; /* array of ngx_http_core_loc_conf_t */ ngx_array_t listen; /* 'listen', array of ngx_http_listen_t */ diff --git a/src/http/ngx_http_header_filter.c b/src/http/ngx_http_header_filter.c --- a/src/http/ngx_http_header_filter.c +++ b/src/http/ngx_http_header_filter.c @@ -30,8 +30,8 @@ ngx_http_module_t ngx_http_header_filte ngx_module_t ngx_http_header_filter_module = { + &ngx_http_header_filter_module_ctx, /* module context */ 0, /* module index */ - &ngx_http_header_filter_module_ctx, /* module context */ NULL, /* module directives */ NGX_HTTP_MODULE_TYPE, /* module type */ ngx_http_header_filter_init /* init module */ diff --git a/src/http/ngx_http_output_filter.c b/src/http/ngx_http_output_filter.c --- a/src/http/ngx_http_output_filter.c +++ b/src/http/ngx_http_output_filter.c @@ -45,8 +45,8 @@ static ngx_http_module_t ngx_http_outpu ngx_module_t ngx_http_output_filter_module = { + &ngx_http_output_filter_module_ctx, /* module context */ 0, /* module index */ - &ngx_http_output_filter_module_ctx, /* module context */ ngx_http_output_filter_commands, /* module directives */ NGX_HTTP_MODULE_TYPE, /* module type */ NULL /* init module */ diff --git a/src/http/ngx_http_write_filter.c b/src/http/ngx_http_write_filter.c --- a/src/http/ngx_http_write_filter.c +++ b/src/http/ngx_http_write_filter.c @@ -44,8 +44,8 @@ ngx_http_module_t ngx_http_write_filter ngx_module_t ngx_http_write_filter_module = { + &ngx_http_write_filter_module_ctx, /* module context */ 0, /* module index */ - &ngx_http_write_filter_module_ctx, /* module context */ ngx_http_write_filter_commands, /* module directives */ NGX_HTTP_MODULE_TYPE, /* module type */ ngx_http_write_filter_init /* init module */