# HG changeset patch # User Igor Sysoev # Date 1040919983 0 # Node ID 59e7c7f30d495300db66d9832bdeab19814b30f4 # Parent d5d4f3bba6f08bff5b4f27ab19440f46045f98be nginx-0.0.1-2002-12-26-19:26:23 import diff --git a/src/core/nginx.c b/src/core/nginx.c --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -62,7 +62,6 @@ int main(int argc, char *const *argv) ngx_create_array(ngx_pool, 10, sizeof(ngx_str_t)), 1); conf.pool = ngx_pool; conf.log = &ngx_log; - conf.modules = ngx_http_modules; conf_file.len = sizeof("nginx.conf") - 1; conf_file.data = "nginx.conf"; diff --git a/src/core/ngx_config_file.c b/src/core/ngx_config_file.c --- a/src/core/ngx_config_file.c +++ b/src/core/ngx_config_file.c @@ -1,8 +1,6 @@ #include - #include - #include @@ -13,14 +11,13 @@ static int argument_number[] = { }; static int ngx_conf_read_token(ngx_conf_t *cf); -static ngx_command_t *ngx_conf_find_token(ngx_conf_t *cf, - ngx_http_module_t **modules); int ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename) { - int rc; + int rc, i; char *error; + ngx_str_t *name; ngx_fd_t fd; ngx_conf_file_t *prev; ngx_command_t *cmd; @@ -75,7 +72,29 @@ int ngx_conf_parse(ngx_conf_t *cf, ngx_s continue; } - cmd = ngx_conf_find_token(cf); + name = (ngx_str_t *) cf->args->elts; + + for (i = 0; ngx_modules[i]; i++) { + if (cf->type != ngx_modules[i]->type) { + continue; + } + + cmd = ngx_modules[i]->commands; + if (cmd == NULL) { + continue; + } + + while (cmd->name.len) { + if (name->len == cmd->name.len + && ngx_strcmp(name->data, cmd->name.data) == 0) + { +ngx_log_debug(cf->log, "command '%s'" _ cmd->name.data); + cmd->set(cf, cmd, NULL); + } + + cmd++; + } + } #if 0 cmd = ngx_conf_find_token(cf); @@ -368,59 +387,37 @@ ngx_log_debug(cf->log, "FOUND %d:'%s'" _ } -static ngx_command_t *ngx_conf_find_token(ngx_conf_t *cf) -{ - int i; - ngx_command_t *cmd; - - for (i = 0; cf->modules[i]; i++) { - cmd = cf->modules[i]->commands; - if (cmd == NULL) { - continue; - } - - while (cmd->name) { - -ngx_log_debug(cf->log, "command '%s'" _ cmd->name); - - cmd++; - } - - } -} - - -char *ngx_conf_set_size_slot(ngx_conf_t *cf, char *conf) +char *ngx_conf_set_size_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf) { int size; ngx_str_t *value; value = (ngx_str_t *) cf->args->elts; - size = atoi(value.data); + size = atoi(value[1].data); if (size < 0) { return "value must be greater or equal to zero"; } - *(int *) (conf + cf->offset) = size; + *(int *) (conf + cmd->offset) = size; return NULL; } -char *ngx_conf_set_time_slot(ngx_conf_t *cf, char *conf) +char *ngx_conf_set_time_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf) { int size; ngx_str_t *value; value = (ngx_str_t *) cf->args->elts; - size = atoi(value.data); + size = atoi(value[1].data); if (size < 0) { return "value must be greater or equal to zero"; } - *(int *) (conf + offset) = size; + *(int *) (conf + cmd->offset) = size; return NULL; } diff --git a/src/core/ngx_config_file.h b/src/core/ngx_config_file.h --- a/src/core/ngx_config_file.h +++ b/src/core/ngx_config_file.h @@ -16,7 +16,8 @@ #define NGX_CONF_TAKE1 2 #define NGX_CONF_TAKE2 4 -#define NGX_CONF_ITERATE 0 +#define NGX_CONF_ANY 0x10000 +#define NGX_CONF_BLOCK 0x20000 #define NGX_CONF_UNSET -1 @@ -28,13 +29,14 @@ typedef struct ngx_conf_s ngx_conf_t; -typedef struct { +typedef struct ngx_command_s ngx_command_t; +struct ngx_command_s { ngx_str_t name; - char *(*set)(ngx_conf_t *cf); + int type; + char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, char *conf); + int conf; int offset; - int zone; - int type; -} ngx_command_t; +}; typedef struct { @@ -60,9 +62,8 @@ struct ngx_conf_s { ngx_conf_file_t *conf_file; ngx_log_t *log; - ngx_module_t *modules; - void *ctx; + int type; int (*handler)(ngx_conf_t *cf); }; @@ -70,7 +71,11 @@ struct ngx_conf_s { int ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename); -char *ngx_conf_set_size_slot(ngx_conf_t *cf); +char *ngx_conf_set_size_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); + + +extern ngx_module_t *ngx_modules[]; #endif _NGX_HTTP_CONFIG_FILE_H_INCLUDED_ 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 @@ -1,15 +1,19 @@ -#include +#include -extern ngx_http_module_t ngx_http_header_filter_module; + +extern ngx_module_t ngx_http_header_filter_module; -extern ngx_http_module_t ngx_http_write_filter_module; -extern ngx_http_module_t ngx_http_output_filter_module; +extern ngx_module_t ngx_http_write_filter_module; +extern ngx_module_t ngx_http_output_filter_module; -extern ngx_http_module_t ngx_http_core_module; -extern ngx_http_module_t ngx_http_index_module; +extern ngx_module_t ngx_http_core_module; +extern ngx_module_t ngx_http_index_module; -ngx_http_module_t *ngx_http_modules[] = { +extern ngx_module_t ngx_http_module; + + +ngx_module_t *ngx_modules[] = { &ngx_http_header_filter_module, @@ -19,5 +23,7 @@ ngx_http_module_t *ngx_http_modules[] = &ngx_http_index_module, &ngx_http_core_module, + &ngx_http_module, + NULL }; diff --git a/src/core/ngx_string.h b/src/core/ngx_string.h --- a/src/core/ngx_string.h +++ b/src/core/ngx_string.h @@ -19,6 +19,7 @@ typedef struct { #define ngx_memzero ZeroMemory #define strcasecmp stricmp +#define ngx_strcmp strcmp #define ngx_snprintf _snprintf #define ngx_vsnprintf _vsnprintf @@ -27,6 +28,8 @@ typedef struct { #define ngx_memzero bzero +#define ngx_strcmp strcmp + #define ngx_snprintf snprintf #define ngx_vsnprintf vsnprintf diff --git a/src/http/modules/ngx_http_event_proxy_handler.c b/src/http/modules/ngx_http_event_proxy_handler.c --- a/src/http/modules/ngx_http_event_proxy_handler.c +++ b/src/http/modules/ngx_http_event_proxy_handler.c @@ -8,7 +8,7 @@ #include #include -ngx_http_module_t ngx_http_proxy_module; +ngx_http_module_t ngx_http_proxy_module_ctx; static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_request_t *r); @@ -36,10 +36,11 @@ int ngx_http_proxy_handler(ngx_http_requ ngx_chain_t *chain; ngx_http_proxy_ctx_t *p; - p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module); + p = (ngx_http_proxy_ctx_t *) + ngx_http_get_module_ctx(r, ngx_http_proxy_module_ctx); if (p == NULL) - ngx_http_create_ctx(r, p, ngx_http_proxy_module, + ngx_http_create_ctx(r, p, ngx_http_proxy_module_ctx, sizeof(ngx_http_proxy_ctx_t)); chain = ngx_http_proxy_create_request(r); @@ -244,7 +245,8 @@ static int ngx_http_proxy_send_request(n c = (ngx_connection_t *) ev->data; r = (ngx_http_request_t *) c->data; - p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module); + p = (ngx_http_proxy_ctx_t *) + ngx_http_get_module_ctx(r, ngx_http_proxy_module_ctx); chain = ngx_event_write(c, p->out, 0); if (chain == (ngx_chain_t *) -1) @@ -269,7 +271,8 @@ static int ngx_http_proxy_read_response_ c = (ngx_connection_t *) ev->data; r = (ngx_http_request_t *) c->data; - p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module); + p = (ngx_http_proxy_ctx_t *) + ngx_http_get_module_ctx(r, ngx_http_proxy_module_ctx); if (p->header_in == NULL) { ngx_test_null(p->header_in, @@ -389,7 +392,8 @@ static int ngx_http_proxy_read_response_ c = (ngx_connection_t *) ev->data; r = (ngx_http_request_t *) c->data; - p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module); + p = (ngx_http_proxy_ctx_t *) + ngx_http_get_module_ctx(r, ngx_http_proxy_module_ctx); left = 0; @@ -464,7 +468,8 @@ static int ngx_http_proxy_write_to_clien c = (ngx_connection_t *) ev->data; r = (ngx_http_request_t *) c->data; - p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module); + p = (ngx_http_proxy_ctx_t *) + ngx_http_get_module_ctx(r, ngx_http_proxy_module_ctx); do { h = ((ngx_hunk_t **) p->hunks->elts)[p->hunk_n]; 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 @@ -13,32 +13,43 @@ static void *ngx_http_index_create_conf(ngx_pool_t *pool); static void *ngx_http_index_merge_conf(ngx_pool_t *p, void *parent, void *child); -static char *ngx_http_index_set_index(ngx_pool_t *p, void *conf, - ngx_str_t *value); +static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, + char *conf); static ngx_command_t ngx_http_index_commands[] = { - {"index", ngx_http_index_set_index, 0, - NGX_HTTP_LOC_CONF, NGX_CONF_ITERATE, - "set index files"}, + {ngx_string("index"), + NGX_CONF_ANY, + ngx_http_index_set_index, + NGX_HTTP_LOC_CONF, + 0}, + + {ngx_string(""), 0, NULL, 0, 0} +}; + - {NULL} +ngx_http_module_t ngx_http_index_module_ctx = { + NGX_HTTP_MODULE, + + NULL, /* create server config */ + ngx_http_index_create_conf, /* create location config */ + + NULL, /* translate handler */ + + NULL, /* output header filter */ + NULL, /* next output header filter */ + NULL, /* output body filter */ + NULL, /* next output body filter */ }; -ngx_http_module_t ngx_http_index_module = { - NGX_HTTP_MODULE, - - NULL, /* create server config */ - ngx_http_index_create_conf, /* create location config */ +ngx_module_t ngx_http_index_module = { + &ngx_http_index_module_ctx, /* module context */ ngx_http_index_commands, /* module directives */ - - NULL, /* init module */ - NULL, /* translate handler */ - - NULL, /* init output body filter */ + NGX_HTTP_MODULE_TYPE, /* module type */ + NULL /* init module */ }; @@ -53,7 +64,7 @@ int ngx_http_index_handler(ngx_http_requ ngx_http_index_conf_t *cf; cf = (ngx_http_index_conf_t *) - ngx_get_module_loc_conf(r, ngx_http_index_module); + ngx_http_get_module_loc_conf(r, ngx_http_index_module_ctx); ngx_test_null(name, ngx_palloc(r->pool, @@ -71,11 +82,13 @@ int ngx_http_index_handler(ngx_http_requ fd = ngx_open_file(name, NGX_FILE_RDONLY); if (fd == NGX_INVALID_FILE) { err = ngx_errno; - if (err == NGX_ENOENT) + if (err == NGX_ENOENT) { continue; + } #if (WIN32) - if (err == ERROR_PATH_NOT_FOUND) + if (err == ERROR_PATH_NOT_FOUND) { continue; + } #endif ngx_log_error(NGX_LOG_ERR, r->connection->log, err, @@ -117,8 +130,9 @@ static void *ngx_http_index_merge_conf(n ngx_str_t *index; if (conf->max_index_len == 0) { - if (prev->max_index_len != 0) + if (prev->max_index_len != 0) { return prev; + } ngx_test_null(index, ngx_push_array(conf->indices), NULL); index->len = sizeof(NGX_HTTP_INDEX) - 1; @@ -130,18 +144,23 @@ static void *ngx_http_index_merge_conf(n } -static char *ngx_http_index_set_index(ngx_pool_t *p, void *conf, - ngx_str_t *value) +static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, + char *conf) { - ngx_http_index_conf_t *cf = (ngx_http_index_conf_t *) conf; - ngx_str_t *index; + ngx_http_index_conf_t *icf = (ngx_http_index_conf_t *) conf; + int i; + ngx_str_t *index, *value; - ngx_test_null(index, ngx_push_array(cf->indices), NULL); - index->len = value->len; - index->data = value->data; + value = (ngx_str_t *) cf->args->elts; + for (i = 1; i < cf->args->nelts; i++) { + ngx_test_null(index, ngx_push_array(icf->indices), NULL); + index->len = value[i].len; + index->data = value[i].data; - if (cf->max_index_len < index->len) - cf->max_index_len = index->len; + if (icf->max_index_len < index->len) { + icf->max_index_len = index->len; + } + } return NULL; } diff --git a/src/http/modules/ngx_http_index_handler.h b/src/http/modules/ngx_http_index_handler.h --- a/src/http/modules/ngx_http_index_handler.h +++ b/src/http/modules/ngx_http_index_handler.h @@ -9,13 +9,14 @@ #define NGX_HTTP_INDEX "index.html" + typedef struct { ngx_array_t *indices; size_t max_index_len; } ngx_http_index_conf_t; -extern ngx_http_module_t ngx_http_index_module; +extern ngx_module_t ngx_http_index_module; #endif /* _NGX_HTTP_INDEX_HANDLER_H_INCLUDED_ */ 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 @@ -39,7 +39,8 @@ int ngx_http_init(ngx_pool_t *pool, ngx_ ngx_http_server.doc_root_len = strlen(ngx_http_server.doc_root) + 1; - ngx_http_config_modules(pool, ngx_http_modules); + ngx_http_config_modules(pool, ngx_modules); +#if 0 /* STUB */ ngx_http_output_filter_set_stub(pool, ngx_http_modules); @@ -47,7 +48,8 @@ int ngx_http_init(ngx_pool_t *pool, ngx_ ngx_http_index_set_stub(pool, ngx_http_modules); ngx_http_init_modules(pool, ngx_http_modules); - ngx_http_init_filters(pool, ngx_http_modules); +#endif + ngx_http_init_filters(pool, ngx_modules); ls = ngx_push_array(ngx_listening_sockets); ngx_memzero(ls, sizeof(ngx_listen_t)); diff --git a/src/http/ngx_http.h b/src/http/ngx_http.h --- a/src/http/ngx_http.h +++ b/src/http/ngx_http.h @@ -188,29 +188,18 @@ typedef int (*ngx_http_output_body_filte typedef struct { - int index; - - void *(*create_srv_conf)(ngx_pool_t *p); - void *(*create_loc_conf)(ngx_pool_t *p); + int index; - int (*translate_handler)(ngx_http_request_t *r); - - int (*output_header_filter) (ngx_http_request_t *r); - int (*next_output_header_filter) (ngx_http_request_t *r); + void *(*create_srv_conf)(ngx_pool_t *p); + void *(*create_loc_conf)(ngx_pool_t *p); - ngx_http_output_body_filter_p output_body_filter; - ngx_http_output_body_filter_p next_output_body_filter; + int (*translate_handler)(ngx_http_request_t *r); -#if 0 - int (*output_body_filter)(); - int (*next_output_body_filter) - (ngx_http_request_t *r, ngx_chain_t *ch); -#endif + int (*output_header_filter) (ngx_http_request_t *r); + int (*next_output_header_filter) (ngx_http_request_t *r); -#if 0 - int (*next_output_body_filter)(int (**next_filter) - (ngx_http_request_t *r, ngx_chain_t *ch)); -#endif + int (*output_body_filter) (ngx_http_request_t *r, ngx_chain_t *ch); + int (*next_output_body_filter) (ngx_http_request_t *r, ngx_chain_t *ch); } ngx_http_module_t; @@ -219,11 +208,6 @@ typedef struct { #define NGX_HTTP_MODULE_TYPE 0x50545448 /* "HTTP" */ -/* STUB */ -#define ngx_get_module_loc_conf(r, module) r->loc_conf[module.index] -#define ngx_get_module_ctx(r, module) r->ctx[module.index] -/**/ - #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_ctx(r, module) r->ctx[module.index] diff --git a/src/http/ngx_http_config.c b/src/http/ngx_http_config.c --- a/src/http/ngx_http_config.c +++ b/src/http/ngx_http_config.c @@ -3,58 +3,106 @@ #include #include #include +#include +#include #include #include #include -int ngx_max_module; - -int (*ngx_http_top_header_filter) (ngx_http_request_t *r); - -/* STUB: gobal srv and loc conf */ +/* STUB */ void **ngx_srv_conf; void **ngx_loc_conf; +/**/ -#if 0 -int ngx_http_block(ngx_conf_t *cf) + +static int ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy); + + +void *null_loc_conf; + + +static ngx_command_t ngx_http_commands[] = { + + {ngx_string("http"), + NGX_CONF_BLOCK|NGX_CONF_NOARGS, + ngx_http_block, + 0, + 0}, + + {ngx_string(""), 0, NULL, 0, 0} +}; + + +static ngx_http_module_t ngx_http_module_ctx = { + NGX_HTTP_MODULE, + + NULL, /* create server config */ + NULL, /* create location config */ + + NULL, /* translate handler */ + + NULL, /* output header filter */ + NULL, /* next output header filter */ + NULL, /* output body filter */ + NULL /* next output body filter */ +}; + + +ngx_module_t ngx_http_module = { + &ngx_http_module_ctx, /* module context */ + ngx_http_commands, /* module directives */ + 0, /* module type */ + NULL /* init module */ +}; + + +static int ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy) { + int i, j; + ngx_http_module_t *module; ngx_http_conf_ctx_t *ctx; - ngx_test_null(ctx, - ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)), + for (i = 0; ngx_modules[i]; i++) { + if (ngx_modules[i]->type != NGX_HTTP_MODULE_TYPE) { + continue; + } + + module = (ngx_http_module_t *) ngx_modules[i]->ctx; + module->index = i; + } + + ngx_http_max_module = i; + + ngx_test_null(null_loc_conf, + ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module), NGX_ERROR); -#if 0 - /* null server config */ - ngx_test_null(ctx->srv_conf, - ngx_pcalloc(cf->pool, sizeof(void *) * ngx_max_module), - NGX_ERROR); -#endif + ctx->srv_conf = NULL; + ctx->loc_conf = null_loc_conf; - /* null location config */ - ngx_test_null(ctx->loc_conf, - ngx_pcalloc(cf->pool, sizeof(void *) * ngx_max_module), - NGX_ERROR); + for (i = 0, j = 0; ngx_modules[i]; i++) { + if (ngx_modules[i]->type != NGX_HTTP_MODULE_TYPE) { + continue; + } - for (i = 0; modules[i]; i++) { -#if 0 - if (modules[i]->create_srv_conf) - ngx_test_null(ctx->srv_conf[i], - modules[i]->create_srv_conf(cf->pool), + module = (ngx_http_module_t *) ngx_modules[i]->ctx; + module->index = i; + if (module->create_loc_conf) { + ngx_test_null(null_loc_conf, + module->create_loc_conf(cf->pool), NGX_ERROR); -#endif - - if (modules[i]->create_loc_conf) - ngx_test_null(ctx->loc_conf[i], - modules[i]->create_loc_conf(cf->pool), - NGX_ERROR); + j++; + } } cf->ctx = ctx; - return ngx_conf_parse(cf); + cf->type = NGX_HTTP_MODULE_TYPE; + return ngx_conf_parse(cf, NULL); } + +#if 0 int ngx_server_block(ngx_conf_t *cf) { ngx_http_conf_ctx_t *ctx, *prev; @@ -155,21 +203,29 @@ int ngx_location_block(ngx_conf_t *cf) #endif -int ngx_http_config_modules(ngx_pool_t *pool, ngx_http_module_t **modules) + +int ngx_http_config_modules(ngx_pool_t *pool, ngx_module_t **modules) { int i; + ngx_http_module_t *module; for (i = 0; modules[i]; i++) { - modules[i]->index = i; + if (modules[i]->type != NGX_HTTP_MODULE_TYPE) { + continue; + } + + module = (ngx_http_module_t *) modules[i]->ctx; + module->index = i; } - ngx_max_module = i; + ngx_http_max_module = i; +#if 0 ngx_test_null(ngx_srv_conf, - ngx_pcalloc(pool, sizeof(void *) * ngx_max_module), + ngx_pcalloc(pool, sizeof(void *) * ngx_http_max_module), NGX_ERROR); ngx_test_null(ngx_loc_conf, - ngx_pcalloc(pool, sizeof(void *) * ngx_max_module), + ngx_pcalloc(pool, sizeof(void *) * ngx_http_max_module), NGX_ERROR); for (i = 0; modules[i]; i++) { @@ -179,94 +235,36 @@ int ngx_http_config_modules(ngx_pool_t * if (modules[i]->create_loc_conf) ngx_loc_conf[i] = modules[i]->create_loc_conf(pool); } +#endif } -int ngx_http_init_modules(ngx_pool_t *pool, ngx_http_module_t **modules) + +void ngx_http_init_filters(ngx_pool_t *pool, ngx_module_t **modules) { - int i; - - for (i = 0; modules[i]; i++) { - if (modules[i]->init_module) - modules[i]->init_module(pool); - } -} - -int ngx_http_init_filters(ngx_pool_t *pool, ngx_http_module_t **modules) -{ - int i; + int i; + ngx_http_module_t *module; int (*ohf)(ngx_http_request_t *r); int (*obf)(ngx_http_request_t *r, ngx_chain_t *ch); ohf = NULL; + obf = NULL; for (i = 0; modules[i]; i++) { - if (modules[i]->output_header_filter) { - modules[i]->next_output_header_filter = ohf; - ohf = modules[i]->output_header_filter; + if (modules[i]->type != NGX_HTTP_MODULE_TYPE) { + continue; + } + + module = (ngx_http_module_t *) modules[i]->ctx; + if (module->output_header_filter) { + module->next_output_header_filter = ohf; + ohf = module->output_header_filter; + } + + if (module->output_body_filter) { + module->next_output_body_filter = obf; + obf = module->output_body_filter; } } ngx_http_top_header_filter = ohf; - - obf = NULL; - - for (i = 0; modules[i]; i++) { - if (modules[i]->output_body_filter) { - modules[i]->next_output_body_filter = obf; - obf = modules[i]->output_body_filter; - } - } } - - -/* STUB */ -ngx_http_output_filter_set_stub(ngx_pool_t *pool, ngx_http_module_t **modules) -{ - int i; - ngx_command_t *cmd; - - for (i = 0; modules[i]; i++) { - if (modules[i] == &ngx_http_output_filter_module) { - for (cmd = modules[i]->commands; cmd->name; cmd++) { - if (strcmp(cmd->name, "output_buffer") == 0) { - cmd->set(ngx_loc_conf[i], cmd->offset, "32768"); - } - } - } - } -} - -ngx_http_write_filter_set_stub(ngx_pool_t *pool, ngx_http_module_t **modules) -{ - int i; - ngx_command_t *cmd; - - for (i = 0; modules[i]; i++) { - if (modules[i] == &ngx_http_write_filter_module) { - for (cmd = modules[i]->commands; cmd->name; cmd++) { - if (strcmp(cmd->name, "write_buffer") == 0) { - cmd->set(ngx_loc_conf[i], cmd->offset, "1500"); - } - } - } - } -} - -ngx_http_index_set_stub(ngx_pool_t *pool, ngx_http_module_t **modules) -{ - int i; - ngx_str_t index; - ngx_command_t *cmd; - - for (i = 0; modules[i]; i++) { - if (modules[i] == &ngx_http_index_module) { - for (cmd = modules[i]->commands; cmd->name; cmd++) { - if (strcmp(cmd->name, "index") == 0) { - index.len = sizeof("index.html") - 1; - index.data = "index.html"; - cmd->set(pool, ngx_loc_conf[i], &index); - } - } - } - } -} 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 @@ -5,9 +5,21 @@ #include #include -#define NGX_HTTP_LOC_CONF 0 + +typedef struct { + void **srv_conf; + void **loc_conf; +} ngx_http_conf_ctx_t; + -int ngx_http_config_modules(ngx_pool_t *pool, ngx_http_module_t **modules); +#define NGX_HTTP_SRV_CONF offsetof(ngx_http_conf_ctx_t, srv_conf) +#define NGX_HTTP_LOC_CONF offsetof(ngx_http_conf_ctx_t, loc_conf) + + +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.c b/src/http/ngx_http_core.c --- a/src/http/ngx_http_core.c +++ b/src/http/ngx_http_core.c @@ -7,7 +7,6 @@ #include /* STUB */ -#include int ngx_http_static_handler(ngx_http_request_t *r); int ngx_http_index_handler(ngx_http_request_t *r); int ngx_http_proxy_handler(ngx_http_request_t *r); @@ -18,60 +17,85 @@ static void *ngx_http_core_create_loc_co static int ngx_http_core_translate_handler(ngx_http_request_t *r); +int (*ngx_http_top_header_filter) (ngx_http_request_t *r); + +int ngx_http_max_module; + + static ngx_command_t ngx_http_core_commands[] = { - {"send_timeout", ngx_conf_set_time_slot, - offsetof(ngx_http_core_loc_conf_t, send_timeout), - NGX_HTTP_LOC_CONF, NGX_CONF_TAKE1, - "set timeout for sending response"}, + {ngx_string("send_timeout"), + NGX_CONF_TAKE1, + ngx_conf_set_time_slot, + NGX_HTTP_LOC_CONF, + offsetof(ngx_http_core_loc_conf_t, send_timeout)}, - {NULL} - + {ngx_string(""), 0, NULL, 0, 0} }; -ngx_http_module_t ngx_http_core_module = { +ngx_http_module_t ngx_http_core_module_ctx = { NGX_HTTP_MODULE, ngx_http_core_create_srv_conf, /* create server config */ ngx_http_core_create_loc_conf, /* create location config */ - ngx_http_core_commands, /* module directives */ - /* STUB */ NULL, /* init module */ ngx_http_core_translate_handler, /* translate handler */ - NULL /* init output body filter */ + NULL, /* output header filter */ + NULL, /* next output header filter */ + NULL, /* output body filter */ + NULL, /* next output body filter */ +}; + + +ngx_module_t ngx_http_core_module = { + &ngx_http_core_module_ctx, /* module context */ + ngx_http_core_commands, /* module directives */ + NGX_HTTP_MODULE_TYPE, /* module type */ + NULL /* init module */ }; int ngx_http_handler(ngx_http_request_t *r) { int rc, i; + ngx_http_module_t *module; r->connection->unexpected_eof = 0; r->lingering_close = 1; r->keepalive = 0; -#if 1 +#if 0 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; #endif /* run translation phase */ - for (i = 0; ngx_http_modules[i]; i++) { - if (ngx_http_modules[i]->translate_handler) { - rc = ngx_http_modules[i]->translate_handler(r); - if (rc == NGX_OK) - break; + for (i = 0; ngx_modules[i]; i++) { + if (ngx_modules[i]->type != NGX_HTTP_MODULE_TYPE) { + continue; + } - if (rc >= NGX_HTTP_SPECIAL_RESPONSE) - return ngx_http_special_response(r, rc); + module = (ngx_http_module_t *) ngx_modules[i]->ctx; + if (module->translate_handler == NULL) { + continue; + } + + rc = module->translate_handler(r); + if (rc == NGX_OK) { + break; + } + + if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { + return ngx_http_special_response(r, rc); } } rc = r->handler(r); - if (rc >= NGX_HTTP_SPECIAL_RESPONSE) + if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { return ngx_http_special_response(r, rc); + } return rc; } @@ -122,18 +146,20 @@ static int ngx_http_core_translate_handl "ngx_http_core_translate_handler: " ngx_file_type_n " %s failed", r->file.name.data); - if (err == ERROR_FILE_NOT_FOUND) + if (err == ERROR_FILE_NOT_FOUND) { + return NGX_HTTP_NOT_FOUND; + } else if (err == ERROR_PATH_NOT_FOUND) { return NGX_HTTP_NOT_FOUND; - else if (err == ERROR_PATH_NOT_FOUND) - return NGX_HTTP_NOT_FOUND; - else + } else { return NGX_HTTP_INTERNAL_SERVER_ERROR; + } } #else - if (r->file.fd == NGX_INVALID_FILE) + if (r->file.fd == NGX_INVALID_FILE) { r->file.fd = ngx_open_file(r->file.name.data, NGX_FILE_RDONLY); + } if (r->file.fd == NGX_INVALID_FILE) { err = ngx_errno; @@ -141,10 +167,11 @@ static int ngx_http_core_translate_handl "ngx_http_core_handler: " ngx_open_file_n " %s failed", r->file.name.data); - if (err == NGX_ENOENT) + if (err == NGX_ENOENT) { return NGX_HTTP_NOT_FOUND; - else + } else { return NGX_HTTP_INTERNAL_SERVER_ERROR; + } } if (!r->file.info_valid) { @@ -153,10 +180,11 @@ static int ngx_http_core_translate_handl "ngx_http_core_handler: " ngx_stat_fd_n " %s failed", r->file.name.data); - if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) + if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, "ngx_http_core_handler: " ngx_close_file_n " %s failed", r->file.name.data); + } return NGX_HTTP_INTERNAL_SERVER_ERROR; } @@ -169,10 +197,11 @@ static int ngx_http_core_translate_handl ngx_log_debug(r->connection->log, "HTTP DIR: '%s'" _ r->file.name.data); #if !(WIN9X) - if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) + if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, "ngx_http_core_handler: " ngx_close_file_n " %s failed", r->file.name.data); + } #endif /* BROKEN: need to include server name */ diff --git a/src/http/ngx_http_core.h b/src/http/ngx_http_core.h --- a/src/http/ngx_http_core.h +++ b/src/http/ngx_http_core.h @@ -9,16 +9,22 @@ typedef struct { int dummy; } ngx_http_core_conf_t; + typedef struct { int dummy; } ngx_http_core_srv_conf_t; + typedef struct { time_t send_timeout; } ngx_http_core_loc_conf_t; -extern ngx_http_module_t ngx_http_core_module; +extern ngx_http_module_t ngx_http_core_module_ctx; +extern ngx_module_t ngx_http_core_module; + +extern int (*ngx_http_top_header_filter) (ngx_http_request_t *r); +extern int ngx_http_max_module; #endif /* _NGX_HTTP_CORE_H_INCLUDED_ */ diff --git a/src/http/ngx_http_event.c b/src/http/ngx_http_event.c --- a/src/http/ngx_http_event.c +++ b/src/http/ngx_http_event.c @@ -177,7 +177,8 @@ static int ngx_http_init_request(ngx_eve ngx_test_null(r->pool, ngx_create_pool(srv->request_pool_size, ev->log), ngx_http_close_request(r)); - ngx_test_null(r->ctx, ngx_pcalloc(r->pool, sizeof(void *) * ngx_max_module), + ngx_test_null(r->ctx, + ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module), ngx_http_close_request(r)); r->headers_out.headers = ngx_create_table(r->pool, 10); @@ -519,8 +520,8 @@ static int ngx_http_writer(ngx_event_t * if (c->sent > 0) { conf = (ngx_http_core_loc_conf_t *) - ngx_get_module_loc_conf(r->main ? r->main : r, - ngx_http_core_module); + ngx_http_get_module_loc_conf(r->main ? r->main : r, + ngx_http_core_module_ctx); timeout = (ngx_msec_t) (c->sent * conf->send_timeout); 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 @@ -6,19 +6,20 @@ #include #include #include +#include #include +#include static int ngx_http_header_filter(ngx_http_request_t *r); -ngx_http_module_t ngx_http_header_filter_module = { + +ngx_http_module_t ngx_http_header_filter_module_ctx = { NGX_HTTP_MODULE, NULL, /* create server config */ NULL, /* create location config */ - NULL, /* module directives */ - NULL, /* init module */ NULL, /* translate handler */ ngx_http_header_filter, /* output header filter */ @@ -28,12 +29,23 @@ ngx_http_module_t ngx_http_header_filte }; +ngx_module_t ngx_http_header_filter_module = { + &ngx_http_header_filter_module_ctx, /* module context */ + NULL, /* module directives */ + NGX_HTTP_MODULE_TYPE, /* module type */ + NULL /* init module */ +}; + + static char server_string[] = "Server: " NGINX_VER CRLF; static ngx_str_t http_codes[] = { + ngx_string("200 OK"), +#if 0 { 6, "200 OK" }, +#endif { 21, "301 Moved Permanently" }, { 21, "302 Moved Temporarily" }, @@ -59,8 +71,9 @@ static int ngx_http_header_filter(ngx_ht ngx_chain_t *ch; ngx_table_elt_t *header; - if (r->http_version < NGX_HTTP_VERSION_10) + if (r->http_version < NGX_HTTP_VERSION_10) { return NGX_OK; + } /* 9 is for "HTTP/1.x ", 2 is for trailing "\r\n" and 2 is for end of header */ @@ -89,19 +102,21 @@ static int ngx_http_header_filter(ngx_ht /* status line */ if (r->headers_out.status_line.len) { len += r->headers_out.status_line.len; + } else { - if (r->headers_out.status < NGX_HTTP_MOVED_PERMANENTLY) + if (r->headers_out.status < NGX_HTTP_MOVED_PERMANENTLY) { status = r->headers_out.status - NGX_HTTP_OK; - else if (r->headers_out.status < NGX_HTTP_BAD_REQUEST) + } else if (r->headers_out.status < NGX_HTTP_BAD_REQUEST) { status = r->headers_out.status - NGX_HTTP_MOVED_PERMANENTLY + 1; - else if (r->headers_out.status < NGX_HTTP_INTERNAL_SERVER_ERROR) + } else if (r->headers_out.status < NGX_HTTP_INTERNAL_SERVER_ERROR) { status = r->headers_out.status - NGX_HTTP_BAD_REQUEST + 1 + 4; - else + } else { status = r->headers_out.status - NGX_HTTP_INTERNAL_SERVER_ERROR + 1 + 4 + 5; + } len += http_codes[status].len; } @@ -122,8 +137,9 @@ static int ngx_http_header_filter(ngx_ht } /* 2^64 is 20 characters */ - if (r->headers_out.content_length >= 0) + if (r->headers_out.content_length >= 0) { len += 48; + } #if 0 if (r->headers_out.content_type.len) @@ -138,15 +154,17 @@ static int ngx_http_header_filter(ngx_ht len += 46; } - if (r->keepalive) + if (r->keepalive) { len += 24; - else + } else { len += 19; + } header = (ngx_table_elt_t *) r->headers_out.headers->elts; for (i = 0; i < r->headers_out.headers->nelts; i++) { - if (header[i].key.len == 0) + if (header[i].key.len == 0) { continue; + } len += header[i].key.len + 2 + header[i].value.len + 2; } @@ -183,9 +201,10 @@ static int ngx_http_header_filter(ngx_ht } /* 2^64 is 20 characters */ - if (r->headers_out.content_length >= 0) + if (r->headers_out.content_length >= 0) { h->last.mem += ngx_snprintf(h->last.mem, 49, "Content-Length: %u" CRLF, r->headers_out.content_length); + } #if 0 if (r->headers_out.content_type.len) { @@ -219,8 +238,9 @@ static int ngx_http_header_filter(ngx_ht } for (i = 0; i < r->headers_out.headers->nelts; i++) { - if (header[i].key.len == 0) + if (header[i].key.len == 0) { continue; + } ngx_memcpy(h->last.mem, header[i].key.data, header[i].key.len); h->last.mem += header[i].key.len; @@ -239,8 +259,9 @@ static int ngx_http_header_filter(ngx_ht /* end of HTTP header */ *(h->last.mem++) = CR; *(h->last.mem++) = LF; - if (r->header_only) + if (r->header_only) { h->type |= NGX_HUNK_LAST; + } ngx_test_null(ch, ngx_palloc(r->pool, sizeof(ngx_chain_t)), NGX_ERROR); 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 @@ -10,7 +10,6 @@ #include -static int ngx_http_output_filter(ngx_http_request_t *r, ngx_hunk_t *hunk); static int ngx_http_output_filter_copy_hunk(ngx_hunk_t *dst, ngx_hunk_t *src); static void *ngx_http_output_filter_create_conf(ngx_pool_t *pool); @@ -18,12 +17,12 @@ static void *ngx_http_output_filter_crea static ngx_command_t ngx_http_output_filter_commands[] = { {ngx_string("output_buffer"), + NGX_CONF_TAKE1, ngx_conf_set_size_slot, - offsetof(ngx_http_output_filter_conf_t, hunk_size), NGX_HTTP_LOC_CONF, - NGX_CONF_TAKE1}, + offsetof(ngx_http_output_filter_conf_t, hunk_size)}, - {ngx_string(""), NULL, 0, 0, 0} + {ngx_string(""), 0, NULL, 0, 0} }; @@ -37,8 +36,8 @@ static ngx_http_module_t ngx_http_outpu NULL, /* output header filter */ NULL, /* next output header filter */ - (ngx_http_output_body_filter_p) ngx_http_output_filter, - /* output body filter */ + (int (*)(ngx_http_request_t *, ngx_chain_t *)) + ngx_http_output_filter, /* output body filter */ NULL /* next output body filter */ }; @@ -51,7 +50,7 @@ ngx_module_t ngx_http_output_filter_mod }; -static int ngx_http_output_filter(ngx_http_request_t *r, ngx_hunk_t *hunk) +int ngx_http_output_filter(ngx_http_request_t *r, ngx_hunk_t *hunk) { int rc, once; size_t size; diff --git a/src/http/ngx_http_output_filter.h b/src/http/ngx_http_output_filter.h --- a/src/http/ngx_http_output_filter.h +++ b/src/http/ngx_http_output_filter.h @@ -23,6 +23,9 @@ typedef struct { } ngx_http_output_filter_ctx_t; +int ngx_http_output_filter(ngx_http_request_t *r, ngx_hunk_t *hunk); + + extern ngx_module_t ngx_http_output_filter_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 @@ -5,35 +5,30 @@ #include #include #include -#include #include -int ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in); - static void *ngx_http_write_filter_create_conf(ngx_pool_t *pool); static ngx_command_t ngx_http_write_filter_commands[] = { - {"write_buffer", ngx_conf_set_size_slot, - offsetof(ngx_http_write_filter_conf_t, buffer_output), - NGX_HTTP_LOC_CONF, NGX_CONF_TAKE1, - "set write filter size to buffer output"}, + {ngx_string("write_buffer"), + NGX_CONF_TAKE1, + ngx_conf_set_size_slot, + NGX_HTTP_LOC_CONF, + offsetof(ngx_http_write_filter_conf_t, buffer_output)}, - {NULL} - + {ngx_string(""), 0, NULL, 0, 0} }; -ngx_http_module_t ngx_http_write_filter_module = { +ngx_http_module_t ngx_http_write_filter_module_ctx = { NGX_HTTP_MODULE, NULL, /* create server config */ ngx_http_write_filter_create_conf, /* create location config */ - ngx_http_write_filter_commands, /* module directives */ - NULL, /* init module */ NULL, /* translate handler */ NULL, /* output header filter */ @@ -43,6 +38,14 @@ ngx_http_module_t ngx_http_write_filter }; +ngx_module_t ngx_http_write_filter_module = { + &ngx_http_write_filter_module_ctx, /* module context */ + ngx_http_write_filter_commands, /* module directives */ + NGX_HTTP_MODULE_TYPE, /* module type */ + NULL /* init module */ +}; + + int ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in) { int last; @@ -53,12 +56,13 @@ int ngx_http_write_filter(ngx_http_reque ctx = (ngx_http_write_filter_ctx_t *) - ngx_get_module_ctx(r->main ? r->main : r, - ngx_http_write_filter_module); - if (ctx == NULL) + ngx_http_get_module_ctx(r->main ? r->main : r, + ngx_http_write_filter_module_ctx); + if (ctx == NULL) { ngx_http_create_ctx(r, ctx, - ngx_http_write_filter_module, + ngx_http_write_filter_module_ctx, sizeof(ngx_http_write_filter_ctx_t)); + } size = flush = 0; last = 0; @@ -73,11 +77,13 @@ int ngx_http_write_filter(ngx_http_reque ch->hunk->type _ ch->hunk->pos.file _ ch->hunk->last.file - ch->hunk->pos.file); - if (ch->hunk->type & (NGX_HUNK_FLUSH|NGX_HUNK_RECYCLED)) + if (ch->hunk->type & (NGX_HUNK_FLUSH|NGX_HUNK_RECYCLED)) { flush = size; + } - if (ch->hunk->type & NGX_HUNK_LAST) + if (ch->hunk->type & NGX_HUNK_LAST) { last = 1; + } } /* add new chain to existent one */ @@ -94,25 +100,29 @@ int ngx_http_write_filter(ngx_http_reque ch->hunk->type _ ch->hunk->pos.file _ ch->hunk->last.file - ch->hunk->pos.file); - if (ch->hunk->type & (NGX_HUNK_FLUSH|NGX_HUNK_RECYCLED)) + if (ch->hunk->type & (NGX_HUNK_FLUSH|NGX_HUNK_RECYCLED)) { flush = size; + } - if (ch->hunk->type & NGX_HUNK_LAST) + if (ch->hunk->type & NGX_HUNK_LAST) { last = 1; + } } conf = (ngx_http_write_filter_conf_t *) - ngx_get_module_loc_conf(r->main ? r->main : r, - ngx_http_write_filter_module); + ngx_http_get_module_loc_conf(r->main ? r->main : r, + ngx_http_write_filter_module_ctx); ngx_log_debug(r->connection->log, "l:%d f:%d" _ last _ flush); - if (!last && flush == 0 && size < conf->buffer_output) + if (!last && flush == 0 && size < conf->buffer_output) { return NGX_OK; + } chain = ngx_event_write(r->connection, ctx->out, flush); - if (chain == (ngx_chain_t *) -1) + if (chain == (ngx_chain_t *) -1) { return NGX_ERROR; + } ctx->out = chain; diff --git a/src/http/ngx_http_write_filter.h b/src/http/ngx_http_write_filter.h --- a/src/http/ngx_http_write_filter.h +++ b/src/http/ngx_http_write_filter.h @@ -10,6 +10,7 @@ typedef struct { size_t buffer_output; } ngx_http_write_filter_conf_t; + typedef struct { ngx_chain_t *out; } ngx_http_write_filter_ctx_t; @@ -17,7 +18,8 @@ typedef struct { int ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in); -extern ngx_http_module_t ngx_http_write_filter_module; + +extern ngx_module_t ngx_http_write_filter_module; #endif /* _NGX_HTTP_WRITE_FILTER_H_INCLUDED_ */