nginx-0.0.1-2002-12-26-19:26:23 import

This commit is contained in:
Igor Sysoev 2002-12-26 16:26:23 +00:00
parent 960ffa42cb
commit 207ed5a589
20 changed files with 404 additions and 302 deletions

View File

@ -62,7 +62,6 @@ int main(int argc, char *const *argv)
ngx_create_array(ngx_pool, 10, sizeof(ngx_str_t)), 1); ngx_create_array(ngx_pool, 10, sizeof(ngx_str_t)), 1);
conf.pool = ngx_pool; conf.pool = ngx_pool;
conf.log = &ngx_log; conf.log = &ngx_log;
conf.modules = ngx_http_modules;
conf_file.len = sizeof("nginx.conf") - 1; conf_file.len = sizeof("nginx.conf") - 1;
conf_file.data = "nginx.conf"; conf_file.data = "nginx.conf";

View File

@ -1,8 +1,6 @@
#include <ngx_config.h> #include <ngx_config.h>
#include <ngx_core.h> #include <ngx_core.h>
#include <ngx_config_file.h> #include <ngx_config_file.h>
@ -13,14 +11,13 @@ static int argument_number[] = {
}; };
static int ngx_conf_read_token(ngx_conf_t *cf); 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 ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename)
{ {
int rc; int rc, i;
char *error; char *error;
ngx_str_t *name;
ngx_fd_t fd; ngx_fd_t fd;
ngx_conf_file_t *prev; ngx_conf_file_t *prev;
ngx_command_t *cmd; ngx_command_t *cmd;
@ -75,7 +72,29 @@ int ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename)
continue; 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 #if 0
cmd = ngx_conf_find_token(cf); cmd = ngx_conf_find_token(cf);
@ -368,59 +387,37 @@ ngx_log_debug(cf->log, "FOUND %d:'%s'" _ word->len _ word->data);
} }
static ngx_command_t *ngx_conf_find_token(ngx_conf_t *cf) char *ngx_conf_set_size_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
{
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)
{ {
int size; int size;
ngx_str_t *value; ngx_str_t *value;
value = (ngx_str_t *) cf->args->elts; value = (ngx_str_t *) cf->args->elts;
size = atoi(value.data); size = atoi(value[1].data);
if (size < 0) { if (size < 0) {
return "value must be greater or equal to zero"; return "value must be greater or equal to zero";
} }
*(int *) (conf + cf->offset) = size; *(int *) (conf + cmd->offset) = size;
return NULL; 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; int size;
ngx_str_t *value; ngx_str_t *value;
value = (ngx_str_t *) cf->args->elts; value = (ngx_str_t *) cf->args->elts;
size = atoi(value.data); size = atoi(value[1].data);
if (size < 0) { if (size < 0) {
return "value must be greater or equal to zero"; return "value must be greater or equal to zero";
} }
*(int *) (conf + offset) = size; *(int *) (conf + cmd->offset) = size;
return NULL; return NULL;
} }

View File

@ -16,7 +16,8 @@
#define NGX_CONF_TAKE1 2 #define NGX_CONF_TAKE1 2
#define NGX_CONF_TAKE2 4 #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 #define NGX_CONF_UNSET -1
@ -28,13 +29,14 @@
typedef struct ngx_conf_s ngx_conf_t; 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; ngx_str_t name;
char *(*set)(ngx_conf_t *cf);
int offset;
int zone;
int type; int type;
} ngx_command_t; char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
int conf;
int offset;
};
typedef struct { typedef struct {
@ -60,9 +62,8 @@ struct ngx_conf_s {
ngx_conf_file_t *conf_file; ngx_conf_file_t *conf_file;
ngx_log_t *log; ngx_log_t *log;
ngx_module_t *modules;
void *ctx; void *ctx;
int type;
int (*handler)(ngx_conf_t *cf); 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); 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_ #endif _NGX_HTTP_CONFIG_FILE_H_INCLUDED_

View File

@ -1,15 +1,19 @@
#include <ngx_http.h> #include <ngx_config_file.h>
extern ngx_http_module_t ngx_http_header_filter_module;
extern ngx_http_module_t ngx_http_write_filter_module; extern ngx_module_t ngx_http_header_filter_module;
extern ngx_http_module_t ngx_http_output_filter_module;
extern ngx_http_module_t ngx_http_core_module; extern ngx_module_t ngx_http_write_filter_module;
extern ngx_http_module_t ngx_http_index_module; extern ngx_module_t ngx_http_output_filter_module;
ngx_http_module_t *ngx_http_modules[] = { extern ngx_module_t ngx_http_core_module;
extern ngx_module_t ngx_http_index_module;
extern ngx_module_t ngx_http_module;
ngx_module_t *ngx_modules[] = {
&ngx_http_header_filter_module, &ngx_http_header_filter_module,
@ -19,5 +23,7 @@ ngx_http_module_t *ngx_http_modules[] = {
&ngx_http_index_module, &ngx_http_index_module,
&ngx_http_core_module, &ngx_http_core_module,
&ngx_http_module,
NULL NULL
}; };

View File

@ -19,6 +19,7 @@ typedef struct {
#define ngx_memzero ZeroMemory #define ngx_memzero ZeroMemory
#define strcasecmp stricmp #define strcasecmp stricmp
#define ngx_strcmp strcmp
#define ngx_snprintf _snprintf #define ngx_snprintf _snprintf
#define ngx_vsnprintf _vsnprintf #define ngx_vsnprintf _vsnprintf
@ -27,6 +28,8 @@ typedef struct {
#define ngx_memzero bzero #define ngx_memzero bzero
#define ngx_strcmp strcmp
#define ngx_snprintf snprintf #define ngx_snprintf snprintf
#define ngx_vsnprintf vsnprintf #define ngx_vsnprintf vsnprintf

View File

@ -8,7 +8,7 @@
#include <ngx_http.h> #include <ngx_http.h>
#include <ngx_http_event_proxy_handler.h> #include <ngx_http_event_proxy_handler.h>
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); 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_request_t *r)
ngx_chain_t *chain; ngx_chain_t *chain;
ngx_http_proxy_ctx_t *p; 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) 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)); sizeof(ngx_http_proxy_ctx_t));
chain = ngx_http_proxy_create_request(r); chain = ngx_http_proxy_create_request(r);
@ -244,7 +245,8 @@ static int ngx_http_proxy_send_request(ngx_event_t *ev)
c = (ngx_connection_t *) ev->data; c = (ngx_connection_t *) ev->data;
r = (ngx_http_request_t *) c->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); chain = ngx_event_write(c, p->out, 0);
if (chain == (ngx_chain_t *) -1) if (chain == (ngx_chain_t *) -1)
@ -269,7 +271,8 @@ static int ngx_http_proxy_read_response_header(ngx_event_t *ev)
c = (ngx_connection_t *) ev->data; c = (ngx_connection_t *) ev->data;
r = (ngx_http_request_t *) c->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) { if (p->header_in == NULL) {
ngx_test_null(p->header_in, ngx_test_null(p->header_in,
@ -389,7 +392,8 @@ static int ngx_http_proxy_read_response_body(ngx_event_t *ev)
c = (ngx_connection_t *) ev->data; c = (ngx_connection_t *) ev->data;
r = (ngx_http_request_t *) c->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; left = 0;
@ -464,7 +468,8 @@ static int ngx_http_proxy_write_to_client(ngx_event_t *ev)
c = (ngx_connection_t *) ev->data; c = (ngx_connection_t *) ev->data;
r = (ngx_http_request_t *) c->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 { do {
h = ((ngx_hunk_t **) p->hunks->elts)[p->hunk_n]; h = ((ngx_hunk_t **) p->hunks->elts)[p->hunk_n];

View File

@ -13,32 +13,43 @@
static void *ngx_http_index_create_conf(ngx_pool_t *pool); static void *ngx_http_index_create_conf(ngx_pool_t *pool);
static void *ngx_http_index_merge_conf(ngx_pool_t *p, static void *ngx_http_index_merge_conf(ngx_pool_t *p,
void *parent, void *child); void *parent, void *child);
static char *ngx_http_index_set_index(ngx_pool_t *p, void *conf, static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
ngx_str_t *value); char *conf);
static ngx_command_t ngx_http_index_commands[] = { static ngx_command_t ngx_http_index_commands[] = {
{"index", ngx_http_index_set_index, 0, {ngx_string("index"),
NGX_HTTP_LOC_CONF, NGX_CONF_ITERATE, NGX_CONF_ANY,
"set index files"}, ngx_http_index_set_index,
NGX_HTTP_LOC_CONF,
{NULL} 0},
{ngx_string(""), 0, NULL, 0, 0}
}; };
ngx_http_module_t ngx_http_index_module = { ngx_http_module_t ngx_http_index_module_ctx = {
NGX_HTTP_MODULE, NGX_HTTP_MODULE,
NULL, /* create server config */ NULL, /* create server config */
ngx_http_index_create_conf, /* create location config */ ngx_http_index_create_conf, /* create location config */
ngx_http_index_commands, /* module directives */
NULL, /* init module */
NULL, /* translate handler */ NULL, /* 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_index_module = {
&ngx_http_index_module_ctx, /* module context */
ngx_http_index_commands, /* module directives */
NGX_HTTP_MODULE_TYPE, /* module type */
NULL /* init module */
}; };
@ -53,7 +64,7 @@ int ngx_http_index_handler(ngx_http_request_t *r)
ngx_http_index_conf_t *cf; ngx_http_index_conf_t *cf;
cf = (ngx_http_index_conf_t *) 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_test_null(name,
ngx_palloc(r->pool, ngx_palloc(r->pool,
@ -71,11 +82,13 @@ int ngx_http_index_handler(ngx_http_request_t *r)
fd = ngx_open_file(name, NGX_FILE_RDONLY); fd = ngx_open_file(name, NGX_FILE_RDONLY);
if (fd == NGX_INVALID_FILE) { if (fd == NGX_INVALID_FILE) {
err = ngx_errno; err = ngx_errno;
if (err == NGX_ENOENT) if (err == NGX_ENOENT) {
continue; continue;
}
#if (WIN32) #if (WIN32)
if (err == ERROR_PATH_NOT_FOUND) if (err == ERROR_PATH_NOT_FOUND) {
continue; continue;
}
#endif #endif
ngx_log_error(NGX_LOG_ERR, r->connection->log, err, ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
@ -117,8 +130,9 @@ static void *ngx_http_index_merge_conf(ngx_pool_t *p, void *parent, void *child)
ngx_str_t *index; ngx_str_t *index;
if (conf->max_index_len == 0) { if (conf->max_index_len == 0) {
if (prev->max_index_len != 0) if (prev->max_index_len != 0) {
return prev; return prev;
}
ngx_test_null(index, ngx_push_array(conf->indices), NULL); ngx_test_null(index, ngx_push_array(conf->indices), NULL);
index->len = sizeof(NGX_HTTP_INDEX) - 1; index->len = sizeof(NGX_HTTP_INDEX) - 1;
@ -130,18 +144,23 @@ 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, static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
ngx_str_t *value) char *conf)
{ {
ngx_http_index_conf_t *cf = (ngx_http_index_conf_t *) conf; ngx_http_index_conf_t *icf = (ngx_http_index_conf_t *) conf;
ngx_str_t *index; int i;
ngx_str_t *index, *value;
ngx_test_null(index, ngx_push_array(cf->indices), NULL); value = (ngx_str_t *) cf->args->elts;
index->len = value->len; for (i = 1; i < cf->args->nelts; i++) {
index->data = value->data; 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) if (icf->max_index_len < index->len) {
cf->max_index_len = index->len; icf->max_index_len = index->len;
}
}
return NULL; return NULL;
} }

View File

@ -9,13 +9,14 @@
#define NGX_HTTP_INDEX "index.html" #define NGX_HTTP_INDEX "index.html"
typedef struct { typedef struct {
ngx_array_t *indices; ngx_array_t *indices;
size_t max_index_len; size_t max_index_len;
} ngx_http_index_conf_t; } 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_ */ #endif /* _NGX_HTTP_INDEX_HANDLER_H_INCLUDED_ */

View File

@ -39,7 +39,8 @@ int ngx_http_init(ngx_pool_t *pool, ngx_log_t *log)
ngx_http_server.doc_root_len = strlen(ngx_http_server.doc_root) + 1; 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 */ /* STUB */
ngx_http_output_filter_set_stub(pool, ngx_http_modules); ngx_http_output_filter_set_stub(pool, ngx_http_modules);
@ -47,7 +48,8 @@ int ngx_http_init(ngx_pool_t *pool, ngx_log_t *log)
ngx_http_index_set_stub(pool, ngx_http_modules); ngx_http_index_set_stub(pool, ngx_http_modules);
ngx_http_init_modules(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); ls = ngx_push_array(ngx_listening_sockets);
ngx_memzero(ls, sizeof(ngx_listen_t)); ngx_memzero(ls, sizeof(ngx_listen_t));

View File

@ -188,29 +188,18 @@ typedef int (*ngx_http_output_body_filter_p)
typedef struct { typedef struct {
int index; int index;
void *(*create_srv_conf)(ngx_pool_t *p); void *(*create_srv_conf)(ngx_pool_t *p);
void *(*create_loc_conf)(ngx_pool_t *p); void *(*create_loc_conf)(ngx_pool_t *p);
int (*translate_handler)(ngx_http_request_t *r); int (*translate_handler)(ngx_http_request_t *r);
int (*output_header_filter) (ngx_http_request_t *r); int (*output_header_filter) (ngx_http_request_t *r);
int (*next_output_header_filter) (ngx_http_request_t *r); int (*next_output_header_filter) (ngx_http_request_t *r);
ngx_http_output_body_filter_p output_body_filter; int (*output_body_filter) (ngx_http_request_t *r, ngx_chain_t *ch);
ngx_http_output_body_filter_p next_output_body_filter; int (*next_output_body_filter) (ngx_http_request_t *r, ngx_chain_t *ch);
#if 0
int (*output_body_filter)();
int (*next_output_body_filter)
(ngx_http_request_t *r, ngx_chain_t *ch);
#endif
#if 0
int (*next_output_body_filter)(int (**next_filter)
(ngx_http_request_t *r, ngx_chain_t *ch));
#endif
} ngx_http_module_t; } ngx_http_module_t;
@ -219,11 +208,6 @@ typedef struct {
#define NGX_HTTP_MODULE_TYPE 0x50545448 /* "HTTP" */ #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_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_loc_conf(r, module) r->loc_conf[module.index]
#define ngx_http_get_module_ctx(r, module) r->ctx[module.index] #define ngx_http_get_module_ctx(r, module) r->ctx[module.index]

View File

@ -3,58 +3,106 @@
#include <ngx_core.h> #include <ngx_core.h>
#include <ngx_config_file.h> #include <ngx_config_file.h>
#include <ngx_http.h> #include <ngx_http.h>
#include <ngx_http_core.h>
#include <ngx_http_config.h>
#include <ngx_http_write_filter.h> #include <ngx_http_write_filter.h>
#include <ngx_http_output_filter.h> #include <ngx_http_output_filter.h>
#include <ngx_http_index_handler.h> #include <ngx_http_index_handler.h>
int ngx_max_module; /* STUB */
int (*ngx_http_top_header_filter) (ngx_http_request_t *r);
/* STUB: gobal srv and loc conf */
void **ngx_srv_conf; void **ngx_srv_conf;
void **ngx_loc_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_http_conf_ctx_t *ctx;
ngx_test_null(ctx, for (i = 0; ngx_modules[i]; i++) {
ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)), 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); NGX_ERROR);
#if 0 ctx->srv_conf = NULL;
/* null server config */ ctx->loc_conf = null_loc_conf;
ngx_test_null(ctx->srv_conf,
ngx_pcalloc(cf->pool, sizeof(void *) * ngx_max_module),
NGX_ERROR);
#endif
/* null location config */ for (i = 0, j = 0; ngx_modules[i]; i++) {
ngx_test_null(ctx->loc_conf, if (ngx_modules[i]->type != NGX_HTTP_MODULE_TYPE) {
ngx_pcalloc(cf->pool, sizeof(void *) * ngx_max_module), continue;
NGX_ERROR); }
for (i = 0; modules[i]; i++) { module = (ngx_http_module_t *) ngx_modules[i]->ctx;
#if 0 module->index = i;
if (modules[i]->create_srv_conf) if (module->create_loc_conf) {
ngx_test_null(ctx->srv_conf[i], ngx_test_null(null_loc_conf,
modules[i]->create_srv_conf(cf->pool), 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); NGX_ERROR);
j++;
}
} }
cf->ctx = ctx; 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) int ngx_server_block(ngx_conf_t *cf)
{ {
ngx_http_conf_ctx_t *ctx, *prev; ngx_http_conf_ctx_t *ctx, *prev;
@ -155,21 +203,29 @@ int ngx_location_block(ngx_conf_t *cf)
#endif #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; int i;
ngx_http_module_t *module;
for (i = 0; modules[i]; i++) { 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_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_ERROR);
ngx_test_null(ngx_loc_conf, 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); NGX_ERROR);
for (i = 0; modules[i]; i++) { for (i = 0; modules[i]; i++) {
@ -179,94 +235,36 @@ int ngx_http_config_modules(ngx_pool_t *pool, ngx_http_module_t **modules)
if (modules[i]->create_loc_conf) if (modules[i]->create_loc_conf)
ngx_loc_conf[i] = modules[i]->create_loc_conf(pool); 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)
{
int i;
for (i = 0; modules[i]; i++) { void ngx_http_init_filters(ngx_pool_t *pool, ngx_module_t **modules)
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 (*ohf)(ngx_http_request_t *r);
int (*obf)(ngx_http_request_t *r, ngx_chain_t *ch); int (*obf)(ngx_http_request_t *r, ngx_chain_t *ch);
ohf = NULL; ohf = NULL;
obf = NULL;
for (i = 0; modules[i]; i++) { for (i = 0; modules[i]; i++) {
if (modules[i]->output_header_filter) { if (modules[i]->type != NGX_HTTP_MODULE_TYPE) {
modules[i]->next_output_header_filter = ohf; continue;
ohf = modules[i]->output_header_filter; }
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; 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);
}
}
}
}
} }

View File

@ -5,9 +5,21 @@
#include <ngx_alloc.h> #include <ngx_alloc.h>
#include <ngx_http.h> #include <ngx_http.h>
#define NGX_HTTP_LOC_CONF 0
int ngx_http_config_modules(ngx_pool_t *pool, ngx_http_module_t **modules); typedef struct {
void **srv_conf;
void **loc_conf;
} ngx_http_conf_ctx_t;
#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); extern int (*ngx_http_top_header_filter) (ngx_http_request_t *r);

View File

@ -7,7 +7,6 @@
#include <ngx_http_config.h> #include <ngx_http_config.h>
/* STUB */ /* STUB */
#include <ngx_http_output_filter.h>
int ngx_http_static_handler(ngx_http_request_t *r); int ngx_http_static_handler(ngx_http_request_t *r);
int ngx_http_index_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); int ngx_http_proxy_handler(ngx_http_request_t *r);
@ -18,60 +17,85 @@ static void *ngx_http_core_create_loc_conf(ngx_pool_t *pool);
static int ngx_http_core_translate_handler(ngx_http_request_t *r); 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[] = { static ngx_command_t ngx_http_core_commands[] = {
{"send_timeout", ngx_conf_set_time_slot, {ngx_string("send_timeout"),
offsetof(ngx_http_core_loc_conf_t, send_timeout), NGX_CONF_TAKE1,
NGX_HTTP_LOC_CONF, NGX_CONF_TAKE1, ngx_conf_set_time_slot,
"set timeout for sending response"}, 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_MODULE,
ngx_http_core_create_srv_conf, /* create server config */ ngx_http_core_create_srv_conf, /* create server config */
ngx_http_core_create_loc_conf, /* create location 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 */ 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 ngx_http_handler(ngx_http_request_t *r)
{ {
int rc, i; int rc, i;
ngx_http_module_t *module;
r->connection->unexpected_eof = 0; r->connection->unexpected_eof = 0;
r->lingering_close = 1; r->lingering_close = 1;
r->keepalive = 0; r->keepalive = 0;
#if 1 #if 0
r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY;
#endif #endif
/* run translation phase */ /* run translation phase */
for (i = 0; ngx_http_modules[i]; i++) { for (i = 0; ngx_modules[i]; i++) {
if (ngx_http_modules[i]->translate_handler) { if (ngx_modules[i]->type != NGX_HTTP_MODULE_TYPE) {
rc = ngx_http_modules[i]->translate_handler(r); continue;
if (rc == NGX_OK) }
break;
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) module = (ngx_http_module_t *) ngx_modules[i]->ctx;
return ngx_http_special_response(r, rc); 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); rc = r->handler(r);
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return ngx_http_special_response(r, rc); return ngx_http_special_response(r, rc);
}
return rc; return rc;
} }
@ -122,18 +146,20 @@ static int ngx_http_core_translate_handler(ngx_http_request_t *r)
"ngx_http_core_translate_handler: " "ngx_http_core_translate_handler: "
ngx_file_type_n " %s failed", r->file.name.data); 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; return NGX_HTTP_NOT_FOUND;
else if (err == ERROR_PATH_NOT_FOUND) } else if (err == ERROR_PATH_NOT_FOUND) {
return NGX_HTTP_NOT_FOUND; return NGX_HTTP_NOT_FOUND;
else } else {
return NGX_HTTP_INTERNAL_SERVER_ERROR; return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
} }
#else #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); r->file.fd = ngx_open_file(r->file.name.data, NGX_FILE_RDONLY);
}
if (r->file.fd == NGX_INVALID_FILE) { if (r->file.fd == NGX_INVALID_FILE) {
err = ngx_errno; err = ngx_errno;
@ -141,10 +167,11 @@ static int ngx_http_core_translate_handler(ngx_http_request_t *r)
"ngx_http_core_handler: " "ngx_http_core_handler: "
ngx_open_file_n " %s failed", r->file.name.data); ngx_open_file_n " %s failed", r->file.name.data);
if (err == NGX_ENOENT) if (err == NGX_ENOENT) {
return NGX_HTTP_NOT_FOUND; return NGX_HTTP_NOT_FOUND;
else } else {
return NGX_HTTP_INTERNAL_SERVER_ERROR; return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
} }
if (!r->file.info_valid) { if (!r->file.info_valid) {
@ -153,10 +180,11 @@ static int ngx_http_core_translate_handler(ngx_http_request_t *r)
"ngx_http_core_handler: " "ngx_http_core_handler: "
ngx_stat_fd_n " %s failed", r->file.name.data); 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_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
"ngx_http_core_handler: " "ngx_http_core_handler: "
ngx_close_file_n " %s failed", r->file.name.data); ngx_close_file_n " %s failed", r->file.name.data);
}
return NGX_HTTP_INTERNAL_SERVER_ERROR; return NGX_HTTP_INTERNAL_SERVER_ERROR;
} }
@ -169,10 +197,11 @@ static int ngx_http_core_translate_handler(ngx_http_request_t *r)
ngx_log_debug(r->connection->log, "HTTP DIR: '%s'" _ r->file.name.data); ngx_log_debug(r->connection->log, "HTTP DIR: '%s'" _ r->file.name.data);
#if !(WIN9X) #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_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
"ngx_http_core_handler: " "ngx_http_core_handler: "
ngx_close_file_n " %s failed", r->file.name.data); ngx_close_file_n " %s failed", r->file.name.data);
}
#endif #endif
/* BROKEN: need to include server name */ /* BROKEN: need to include server name */

View File

@ -9,16 +9,22 @@ typedef struct {
int dummy; int dummy;
} ngx_http_core_conf_t; } ngx_http_core_conf_t;
typedef struct { typedef struct {
int dummy; int dummy;
} ngx_http_core_srv_conf_t; } ngx_http_core_srv_conf_t;
typedef struct { typedef struct {
time_t send_timeout; time_t send_timeout;
} ngx_http_core_loc_conf_t; } 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_ */ #endif /* _NGX_HTTP_CORE_H_INCLUDED_ */

View File

@ -177,7 +177,8 @@ static int ngx_http_init_request(ngx_event_t *ev)
ngx_test_null(r->pool, ngx_create_pool(srv->request_pool_size, ev->log), ngx_test_null(r->pool, ngx_create_pool(srv->request_pool_size, ev->log),
ngx_http_close_request(r)); 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)); ngx_http_close_request(r));
r->headers_out.headers = ngx_create_table(r->pool, 10); r->headers_out.headers = ngx_create_table(r->pool, 10);
@ -519,8 +520,8 @@ static int ngx_http_writer(ngx_event_t *ev)
if (c->sent > 0) { if (c->sent > 0) {
conf = (ngx_http_core_loc_conf_t *) conf = (ngx_http_core_loc_conf_t *)
ngx_get_module_loc_conf(r->main ? r->main : r, ngx_http_get_module_loc_conf(r->main ? r->main : r,
ngx_http_core_module); ngx_http_core_module_ctx);
timeout = (ngx_msec_t) (c->sent * conf->send_timeout); timeout = (ngx_msec_t) (c->sent * conf->send_timeout);

View File

@ -6,19 +6,20 @@
#include <ngx_string.h> #include <ngx_string.h>
#include <ngx_table.h> #include <ngx_table.h>
#include <ngx_hunk.h> #include <ngx_hunk.h>
#include <ngx_config_file.h>
#include <ngx_http.h> #include <ngx_http.h>
#include <ngx_http_write_filter.h>
static int ngx_http_header_filter(ngx_http_request_t *r); 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, NGX_HTTP_MODULE,
NULL, /* create server config */ NULL, /* create server config */
NULL, /* create location config */ NULL, /* create location config */
NULL, /* module directives */
NULL, /* init module */
NULL, /* translate handler */ NULL, /* translate handler */
ngx_http_header_filter, /* output header filter */ ngx_http_header_filter, /* output header filter */
@ -28,12 +29,23 @@ ngx_http_module_t ngx_http_header_filter_module = {
}; };
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 char server_string[] = "Server: " NGINX_VER CRLF;
static ngx_str_t http_codes[] = { static ngx_str_t http_codes[] = {
ngx_string("200 OK"),
#if 0
{ 6, "200 OK" }, { 6, "200 OK" },
#endif
{ 21, "301 Moved Permanently" }, { 21, "301 Moved Permanently" },
{ 21, "302 Moved Temporarily" }, { 21, "302 Moved Temporarily" },
@ -59,8 +71,9 @@ static int ngx_http_header_filter(ngx_http_request_t *r)
ngx_chain_t *ch; ngx_chain_t *ch;
ngx_table_elt_t *header; ngx_table_elt_t *header;
if (r->http_version < NGX_HTTP_VERSION_10) if (r->http_version < NGX_HTTP_VERSION_10) {
return NGX_OK; return NGX_OK;
}
/* 9 is for "HTTP/1.x ", 2 is for trailing "\r\n" /* 9 is for "HTTP/1.x ", 2 is for trailing "\r\n"
and 2 is for end of header */ and 2 is for end of header */
@ -89,19 +102,21 @@ static int ngx_http_header_filter(ngx_http_request_t *r)
/* status line */ /* status line */
if (r->headers_out.status_line.len) { if (r->headers_out.status_line.len) {
len += r->headers_out.status_line.len; len += r->headers_out.status_line.len;
} else { } 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; 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; 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; status = r->headers_out.status - NGX_HTTP_BAD_REQUEST + 1 + 4;
else } else {
status = r->headers_out.status status = r->headers_out.status
- NGX_HTTP_INTERNAL_SERVER_ERROR + 1 + 4 + 5; - NGX_HTTP_INTERNAL_SERVER_ERROR + 1 + 4 + 5;
}
len += http_codes[status].len; len += http_codes[status].len;
} }
@ -122,8 +137,9 @@ static int ngx_http_header_filter(ngx_http_request_t *r)
} }
/* 2^64 is 20 characters */ /* 2^64 is 20 characters */
if (r->headers_out.content_length >= 0) if (r->headers_out.content_length >= 0) {
len += 48; len += 48;
}
#if 0 #if 0
if (r->headers_out.content_type.len) if (r->headers_out.content_type.len)
@ -138,15 +154,17 @@ static int ngx_http_header_filter(ngx_http_request_t *r)
len += 46; len += 46;
} }
if (r->keepalive) if (r->keepalive) {
len += 24; len += 24;
else } else {
len += 19; len += 19;
}
header = (ngx_table_elt_t *) r->headers_out.headers->elts; header = (ngx_table_elt_t *) r->headers_out.headers->elts;
for (i = 0; i < r->headers_out.headers->nelts; i++) { for (i = 0; i < r->headers_out.headers->nelts; i++) {
if (header[i].key.len == 0) if (header[i].key.len == 0) {
continue; continue;
}
len += header[i].key.len + 2 + header[i].value.len + 2; len += header[i].key.len + 2 + header[i].value.len + 2;
} }
@ -183,9 +201,10 @@ static int ngx_http_header_filter(ngx_http_request_t *r)
} }
/* 2^64 is 20 characters */ /* 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, h->last.mem += ngx_snprintf(h->last.mem, 49, "Content-Length: %u" CRLF,
r->headers_out.content_length); r->headers_out.content_length);
}
#if 0 #if 0
if (r->headers_out.content_type.len) { if (r->headers_out.content_type.len) {
@ -219,8 +238,9 @@ static int ngx_http_header_filter(ngx_http_request_t *r)
} }
for (i = 0; i < r->headers_out.headers->nelts; i++) { for (i = 0; i < r->headers_out.headers->nelts; i++) {
if (header[i].key.len == 0) if (header[i].key.len == 0) {
continue; continue;
}
ngx_memcpy(h->last.mem, header[i].key.data, header[i].key.len); ngx_memcpy(h->last.mem, header[i].key.data, header[i].key.len);
h->last.mem += header[i].key.len; h->last.mem += header[i].key.len;
@ -239,8 +259,9 @@ static int ngx_http_header_filter(ngx_http_request_t *r)
/* end of HTTP header */ /* end of HTTP header */
*(h->last.mem++) = CR; *(h->last.mem++) = LF; *(h->last.mem++) = CR; *(h->last.mem++) = LF;
if (r->header_only) if (r->header_only) {
h->type |= NGX_HUNK_LAST; h->type |= NGX_HUNK_LAST;
}
ngx_test_null(ch, ngx_palloc(r->pool, sizeof(ngx_chain_t)), NGX_ERROR); ngx_test_null(ch, ngx_palloc(r->pool, sizeof(ngx_chain_t)), NGX_ERROR);

View File

@ -10,7 +10,6 @@
#include <ngx_http_output_filter.h> #include <ngx_http_output_filter.h>
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 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); static void *ngx_http_output_filter_create_conf(ngx_pool_t *pool);
@ -18,12 +17,12 @@ static void *ngx_http_output_filter_create_conf(ngx_pool_t *pool);
static ngx_command_t ngx_http_output_filter_commands[] = { static ngx_command_t ngx_http_output_filter_commands[] = {
{ngx_string("output_buffer"), {ngx_string("output_buffer"),
NGX_CONF_TAKE1,
ngx_conf_set_size_slot, ngx_conf_set_size_slot,
offsetof(ngx_http_output_filter_conf_t, hunk_size),
NGX_HTTP_LOC_CONF, 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_output_filter_module_ctx = {
NULL, /* output header filter */ NULL, /* output header filter */
NULL, /* next output header filter */ NULL, /* next output header filter */
(ngx_http_output_body_filter_p) ngx_http_output_filter, (int (*)(ngx_http_request_t *, ngx_chain_t *))
/* output body filter */ ngx_http_output_filter, /* output body filter */
NULL /* next output body filter */ NULL /* next output body filter */
}; };
@ -51,7 +50,7 @@ ngx_module_t ngx_http_output_filter_module = {
}; };
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; int rc, once;
size_t size; size_t size;

View File

@ -23,6 +23,9 @@ typedef struct {
} ngx_http_output_filter_ctx_t; } 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; extern ngx_module_t ngx_http_output_filter_module;

View File

@ -5,35 +5,30 @@
#include <ngx_event_write.h> #include <ngx_event_write.h>
#include <ngx_http.h> #include <ngx_http.h>
#include <ngx_http_config.h> #include <ngx_http_config.h>
#include <ngx_http_output_filter.h>
#include <ngx_http_write_filter.h> #include <ngx_http_write_filter.h>
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 void *ngx_http_write_filter_create_conf(ngx_pool_t *pool);
static ngx_command_t ngx_http_write_filter_commands[] = { static ngx_command_t ngx_http_write_filter_commands[] = {
{"write_buffer", ngx_conf_set_size_slot, {ngx_string("write_buffer"),
offsetof(ngx_http_write_filter_conf_t, buffer_output), NGX_CONF_TAKE1,
NGX_HTTP_LOC_CONF, NGX_CONF_TAKE1, ngx_conf_set_size_slot,
"set write filter size to buffer output"}, 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, NGX_HTTP_MODULE,
NULL, /* create server config */ NULL, /* create server config */
ngx_http_write_filter_create_conf, /* create location config */ ngx_http_write_filter_create_conf, /* create location config */
ngx_http_write_filter_commands, /* module directives */
NULL, /* init module */
NULL, /* translate handler */ NULL, /* translate handler */
NULL, /* output header filter */ NULL, /* output header filter */
@ -43,6 +38,14 @@ ngx_http_module_t ngx_http_write_filter_module = {
}; };
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 ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
{ {
int last; int last;
@ -53,12 +56,13 @@ int ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
ctx = (ngx_http_write_filter_ctx_t *) ctx = (ngx_http_write_filter_ctx_t *)
ngx_get_module_ctx(r->main ? r->main : r, ngx_http_get_module_ctx(r->main ? r->main : r,
ngx_http_write_filter_module); ngx_http_write_filter_module_ctx);
if (ctx == NULL) if (ctx == NULL) {
ngx_http_create_ctx(r, ctx, ngx_http_create_ctx(r, ctx,
ngx_http_write_filter_module, ngx_http_write_filter_module_ctx,
sizeof(ngx_http_write_filter_ctx_t)); sizeof(ngx_http_write_filter_ctx_t));
}
size = flush = 0; size = flush = 0;
last = 0; last = 0;
@ -73,11 +77,13 @@ int ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
ch->hunk->type _ ch->hunk->pos.file _ ch->hunk->type _ ch->hunk->pos.file _
ch->hunk->last.file - 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; flush = size;
}
if (ch->hunk->type & NGX_HUNK_LAST) if (ch->hunk->type & NGX_HUNK_LAST) {
last = 1; last = 1;
}
} }
/* add new chain to existent one */ /* add new chain to existent one */
@ -94,25 +100,29 @@ int ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
ch->hunk->type _ ch->hunk->pos.file _ ch->hunk->type _ ch->hunk->pos.file _
ch->hunk->last.file - 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; flush = size;
}
if (ch->hunk->type & NGX_HUNK_LAST) if (ch->hunk->type & NGX_HUNK_LAST) {
last = 1; last = 1;
}
} }
conf = (ngx_http_write_filter_conf_t *) conf = (ngx_http_write_filter_conf_t *)
ngx_get_module_loc_conf(r->main ? r->main : r, ngx_http_get_module_loc_conf(r->main ? r->main : r,
ngx_http_write_filter_module); ngx_http_write_filter_module_ctx);
ngx_log_debug(r->connection->log, "l:%d f:%d" _ last _ flush); 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; return NGX_OK;
}
chain = ngx_event_write(r->connection, ctx->out, flush); chain = ngx_event_write(r->connection, ctx->out, flush);
if (chain == (ngx_chain_t *) -1) if (chain == (ngx_chain_t *) -1) {
return NGX_ERROR; return NGX_ERROR;
}
ctx->out = chain; ctx->out = chain;

View File

@ -10,6 +10,7 @@ typedef struct {
size_t buffer_output; size_t buffer_output;
} ngx_http_write_filter_conf_t; } ngx_http_write_filter_conf_t;
typedef struct { typedef struct {
ngx_chain_t *out; ngx_chain_t *out;
} ngx_http_write_filter_ctx_t; } 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); 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_ */ #endif /* _NGX_HTTP_WRITE_FILTER_H_INCLUDED_ */