changeset 10:4f3879d9b6f6

nginx-0.0.1-2002-09-11-19:18:33 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 11 Sep 2002 15:18:33 +0000
parents 6f58641241bb
children f323b4f74e4a
files src/core/ngx_alloc.c src/core/ngx_config_command.c src/core/ngx_config_command.h src/core/ngx_core.h src/http/modules/ngx_http_header_filter.c src/http/modules/ngx_http_index_handler.c src/http/modules/ngx_http_index_handler.h src/http/modules/ngx_http_static_handler.c src/http/ngx_http.c src/http/ngx_http.h src/http/ngx_http_config.c src/http/ngx_http_config.h src/http/ngx_http_event.c src/http/ngx_http_header_filter.c src/http/ngx_http_modules.c src/http/ngx_http_output_filter.c src/http/ngx_http_output_filter.h src/http/ngx_http_write_filter.c src/http/ngx_http_write_filter.h
diffstat 18 files changed, 414 insertions(+), 159 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/ngx_alloc.c
+++ b/src/core/ngx_alloc.c
@@ -15,7 +15,7 @@ void *ngx_alloc(size_t size, ngx_log_t *
         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
                       "malloc() %d bytes failed", size);
 
-    ngx_log_debug(log, "malloc: %x" _ p);
+    ngx_log_debug(log, "malloc: %x:%d" _ p _ size);
 
     return p;
 }
@@ -51,10 +51,13 @@ void ngx_destroy_pool(ngx_pool_t *pool)
     ngx_pool_t        *p, *n;
     ngx_pool_large_t  *l;
 
-    for (l = pool->large; l; l = l->next)
+    for (l = pool->large; l; l = l->next) {
+        ngx_log_debug(pool->log, "free: %x" _ l->alloc);
         free(l->alloc);
+    }
 
     for (p = pool, n = pool->next; /* void */; p = n, n = n->next) {
+        ngx_log_debug(pool->log, "free: %x" _ p);
         free(p);
 
         if (n == NULL)
@@ -110,6 +113,7 @@ void *ngx_palloc(ngx_pool_t *pool, size_
         if (large == NULL) {
             ngx_test_null(large, ngx_palloc(pool, sizeof(ngx_pool_large_t)),
                           NULL);
+            large->next = NULL;
         }
 
         ngx_test_null(p, ngx_alloc(size, pool->log), NULL);
new file mode 100644
--- /dev/null
+++ b/src/core/ngx_config_command.c
@@ -0,0 +1,14 @@
+
+#include <ngx_config.h>
+
+char *ngx_conf_set_size_slot(char *conf, int offset, char *value)
+{
+    int size;
+
+    size = atoi(value);
+    if (size < 0)
+        return "value must be greater or equal to zero";
+
+    *(int *) (conf + offset) = size;
+    return NULL;
+}
new file mode 100644
--- /dev/null
+++ b/src/core/ngx_config_command.h
@@ -0,0 +1,22 @@
+#ifndef _NGX_HTTP_CONFIG_COMMAND_H_INCLUDED_
+#define _NGX_HTTP_CONFIG_COMMAND_H_INCLUDED_
+
+
+#define NGX_CONF_TAKE1     0
+#define NGX_CONF_ITERATE   0
+
+#define NGX_CONF_UNSET   -1
+
+typedef struct {
+    char    *name;
+    char  *(*set)();
+    int      offset;
+    int      zone;
+    int      type;
+    char    *description;
+} ngx_command_t;
+
+char *ngx_conf_set_size_slot(char *conf, int offset, char *value);
+
+
+#endif _NGX_HTTP_CONFIG_COMMAND_H_INCLUDED_
--- a/src/core/ngx_core.h
+++ b/src/core/ngx_core.h
@@ -2,9 +2,10 @@
 #define _NGX_CORE_H_INCLUDED_
 
 
-#define  NGX_OK      0
-#define  NGX_ERROR  -1
-#define  NGX_AGAIN  -2
+#define  NGX_OK          0
+#define  NGX_ERROR      -1
+#define  NGX_AGAIN      -2
+#define  NGX_DECLINED   -3
 
 
 #endif /* _NGX_CORE_H_INCLUDED_ */
--- a/src/http/modules/ngx_http_index_handler.c
+++ b/src/http/modules/ngx_http_index_handler.c
@@ -1,23 +1,53 @@
 
 #include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_errno.h>
+#include <ngx_string.h>
+#include <ngx_files.h>
+#include <ngx_config_command.h>
+#include <ngx_http.h>
+#include <ngx_http_config.h>
+#include <ngx_http_index_handler.h>
 
-#include <ngx_strings.h>
-#include <ngx_open.h>
-#include <ngx_stat.h>
+
+static void *ngx_http_index_create_conf(ngx_pool_t *pool);
+static char *ngx_http_index_set_index(ngx_pool_t *p, void *conf, char *value);
+
+static ngx_command_t ngx_http_index_commands[];
+
 
-#include <ngx_http.h>
+ngx_http_module_t  ngx_http_index_module = {
+    NGX_HTTP_MODULE,
+    NULL,                                  /* create server config */
+    ngx_http_index_create_conf,            /* create location config */
+    ngx_http_index_commands,               /* module directives */
+    NULL,                                  /* init module */
+    NULL,                                  /* init output body filter */
+};
+
+
+static ngx_command_t ngx_http_index_commands[] = {
+
+    {"index", ngx_http_index_set_index, NULL,
+     NGX_HTTP_LOC_CONF, NGX_CONF_ITERATE,
+     "set index files"},
+
+    {NULL}
+
+};
 
 int ngx_http_index_handler(ngx_http_request_t *r)
 {
-    int          index_len, err, i;
-    char        *name, *loc, *file
-    ngx_file_t   fd;
+    int          index_len, i;
+    char        *name, *loc, *file;
+    ngx_err_t    err;
+    ngx_fd_t     fd;
 
-    ngx_http_index_t  *index;
-    ngx_http_index_handler_loc_conf_t  *cf;
+    ngx_http_index_file_t  *index;
+    ngx_http_index_conf_t  *cf;
 
-    cf = (ngx_http_index_handler_loc_conf_t *)
-                    ngx_get_module_loc_conf(r, &ngx_http_index_handler_module);
+    cf = (ngx_http_index_conf_t *)
+                            ngx_get_module_loc_conf(r, ngx_http_index_module);
 
     index_len = (*(r->uri_end - 1) == '/') ? cf->max_index_len : 0;
 
@@ -29,96 +59,73 @@ int ngx_http_index_handler(ngx_http_requ
     loc = ngx_cpystrn(name, r->server->doc_root, r->server->doc_root_len);
     file = ngx_cpystrn(loc, r->uri_start, r->uri_end - r->uri_start + 1);
 
-    /* URI without / on the end - check directory */
-    if (index_len == 0) {
+    index = (ngx_http_index_file_t *) cf->indices->elts;
+    for (i = 0; i < cf->indices->nelts; i++) {
+        ngx_memcpy(file, index[i].name, index[i].len);
 
-        if (ngx_stat(name, &r->stat) == -1) {
+        fd = ngx_open_file(name, NGX_FILE_RDONLY);
+        if (fd == -1) {
             err = ngx_errno;
-            ngx_log_error(GX_LOG_ERR, r->connection->log, err,
-                         "ngx_http_handler: " ngx_stat_n " %s failed", name);
-
             if (err == NGX_ENOENT)
-                return NGX_HTTP_NOT_FOUND;
-            else
-                return NGX_HTTP_INTERNAL_SERVER_ERROR;
-        }
+                continue;
 
-        if (ngx_is_dir(r->stat)) {
-            *file++ = '/';
-            *file = '\0';
-            r->headers_out->location = loc;
-            return NGX_HTTP_MOVED_PERMANENTLY;
+            ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
+                          ngx_open_file_n " %s failed", name);
+
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
         }
 
-        r->file = name;
-        r->stat_valid = 1;
+        r->filename = name; 
+        r->fd = fd; 
 
-        return NGX_OK;
+        return ngx_http_internal_redirect(r, loc);
     }
 
-    /* look for index file */
-    index = (ngx_http_index_t *) cf->indices->elts;
-    for (i = 0; i < cf->indices->nelts; i++) {
-        ngx_memcpy(file, index[i].name; index[i].len);
-
-        fd = ngx_open(name, O_RDONLY);
-        if (fd != -1) {
-            r->file = name; 
-            r->fd = fd; 
-            return NGX_OK;
-        }
-    }
-
-    return NGX_HTTP_FORBIDDEN;
+    return NGX_DECLINED;
 }
 
-/*
+static void *ngx_http_index_create_conf(ngx_pool_t *pool)
+{
+    ngx_http_index_conf_t  *conf;
 
-static void *ngx_create_index_config()
-{
-    ngx_http_index_handler_loc_conf_t  *cf;
+    ngx_test_null(conf, ngx_pcalloc(pool, sizeof(ngx_http_index_conf_t)), NULL);
 
-    ngx_check_null(cf, ngx_alloc(p, sizeof(ngx_http_index_handler_loc_conf)),
-                   NULL);
+    ngx_test_null(conf->indices,
+                  ngx_create_array(pool, sizeof(ngx_http_index_file_t), 3),
+                  NULL);
 
-    cf->indices = ngx_create_array(p, sizeof(ngx_http_index_t), 5);
-    if (cf->indices == NULL)
-        return NULL;
-
-    cf->max_index_len = 0;
-
-    return cf;
+    return conf;
 }
 
-static void *ngx_merge_index_config()
+static void *ngx_http_index_merge_conf(ngx_pool_t *p, void *parent, void *child)
 {
-    if (p->indices->nelts > 0) {
+    ngx_http_index_conf_t *prev = (ngx_http_index_conf_t *) parent;
+    ngx_http_index_conf_t *conf = (ngx_http_index_conf_t *) child;
+    ngx_http_index_file_t *index;
 
-        copy and check dups
+    if (conf->max_index_len == 0) {
+        if (prev->max_index_len != 0)
+            return prev;
 
-        if (c->max_index_len < c->max_index_len)
-            c->max_index_len < c->max_index_len);
+        ngx_test_null(index, ngx_push_array(conf->indices), NULL);
+        index->name = NGX_HTTP_INDEX;
+        conf->max_index_len = index->len = sizeof(NGX_HTTP_INDEX);
     }
+
+    return conf;
 }
 
-static void *ngx_set_index()
+static char *ngx_http_index_set_index(ngx_pool_t *p, void *conf, char *value)
 {
-    if (*conf == NULL) {
-        cf = ngx_create_index_conf();
-        if (cf == NULL)
-            return "can not create config";
-    }
+    ngx_http_index_conf_t *cf = (ngx_http_index_conf_t *) conf;
+    ngx_http_index_file_t *index;
 
-    while (args) {
-       index = ngx_push_array(cf->indices);
-       index->name = arg;
-       index->len = ngx_strlen(arg) + 1;
+    ngx_test_null(index, ngx_push_array(cf->indices), NULL);
+    index->name = value;
+    index->len = strlen(value);
 
-       if (cf->max_index_len < index->len)
-           cf->max_index_len = index->len;
-    }
+    if (cf->max_index_len < index->len)
+        cf->max_index_len = index->len;
 
-    *conf = cf;
+    return NULL;
 }
-
-*/
new file mode 100644
--- /dev/null
+++ b/src/http/modules/ngx_http_index_handler.h
@@ -0,0 +1,26 @@
+#ifndef _NGX_HTTP_INDEX_HANDLER_H_INCLUDED_
+#define _NGX_HTTP_INDEX_HANDLER_H_INCLUDED_
+
+
+#include <ngx_config.h>
+#include <ngx_array.h>
+#include <ngx_http.h>
+
+
+#define NGX_HTTP_INDEX   "index.html"
+
+typedef struct {
+    ngx_array_t  *indices;
+    size_t        max_index_len;
+} ngx_http_index_conf_t;
+
+typedef struct {
+    char   *name;
+    size_t  len;
+} ngx_http_index_file_t;
+
+
+extern ngx_http_module_t  ngx_http_index_module;
+
+
+#endif /* _NGX_HTTP_INDEX_HANDLER_H_INCLUDED_ */
--- a/src/http/modules/ngx_http_static_handler.c
+++ b/src/http/modules/ngx_http_static_handler.c
@@ -45,16 +45,17 @@ int ngx_http_static_handler(ngx_http_req
         ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
                       "ngx_http_static_handler: "
                       ngx_open_file_n " %s failed", r->filename);
-        /* STUB */
-        return -1;
+
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
 
     if (ngx_stat_fd(r->fd, &r->fileinfo) == -1) {
         ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
                       "ngx_http_static_handler: "
                       ngx_stat_fd_n " %s failed", r->filename);
-        /* STUB */
-        return -1;
+
+        /* close fd */
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
 
     r->headers_out->status = NGX_HTTP_OK;
--- a/src/http/ngx_http.c
+++ b/src/http/ngx_http.c
@@ -4,6 +4,7 @@
 #include <ngx_socket.h>
 #include <ngx_listen.h>
 #include <ngx_http.h>
+#include <ngx_http_config.h>
 
 extern ngx_array_t *ngx_listening_sockets;
 
@@ -28,13 +29,18 @@ int ngx_http_init(ngx_pool_t *pool, ngx_
 #else
     ngx_http_server.doc_root = "/home/is/work/xml/site-1.0.0/html";
 #endif
-    ngx_http_server.doc_root = "html";
     ngx_http_server.doc_root_len = strlen(ngx_http_server.doc_root) + 1;
 
 
-    ngx_http_output_filter_init();
-    ngx_http_write_filter_init();
+    ngx_http_config_modules(pool, ngx_http_modules);
 
+    /* STUB */
+    ngx_http_output_filter_set_stub(pool, ngx_http_modules);
+    ngx_http_write_filter_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_filters(pool, ngx_http_modules);
 
     ls = ngx_push_array(ngx_listening_sockets);
     ngx_memzero(ls, sizeof(ngx_listen_t));
--- a/src/http/ngx_http.h
+++ b/src/http/ngx_http.h
@@ -7,6 +7,7 @@
 #include <ngx_hunk.h>
 #include <ngx_files.h>
 #include <ngx_connection.h>
+#include <ngx_config_command.h>
 
 
 #define NGX_HTTP_GET   1
@@ -139,7 +140,13 @@ typedef struct {
 
 
 typedef struct {
-    int    index;
+    int               index;
+    void           *(*create_srv_conf)(ngx_pool_t *p);
+    void           *(*create_loc_conf)(ngx_pool_t *p);
+    ngx_command_t    *commands;
+    int             (*init_module)(ngx_pool_t *p);
+    int             (*init_output_body_filter)(int (**next_filter)
+                                     (ngx_http_request_t *r, ngx_chain_t *ch));
 } ngx_http_module_t;
 
 #define NGX_HTTP_MODULE  0
@@ -147,6 +154,12 @@ typedef struct {
 #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_create_ctx(r, ctx, module, size)                             \
+            do {                                                              \
+               ngx_test_null(ctx, ngx_pcalloc(r->pool, size), NGX_ERROR);     \
+               r->ctx[module.index] = ctx;                                    \
+            } while (0)
+
 
 
 /* STUB */
@@ -159,4 +172,10 @@ int ngx_http_init(ngx_pool_t *pool, ngx_
 int ngx_http_init_connection(ngx_connection_t *c);
 
 
+extern int ngx_max_module;
+
+extern ngx_http_module_t *ngx_http_modules[];
+
+
+
 #endif /* _NGX_HTTP_H_INCLUDED_ */
new file mode 100644
--- /dev/null
+++ b/src/http/ngx_http_config.c
@@ -0,0 +1,114 @@
+
+#include <ngx_core.h>
+#include <ngx_config_command.h>
+#include <ngx_http.h>
+#include <ngx_http_write_filter.h>
+#include <ngx_http_output_filter.h>
+#include <ngx_http_index_handler.h>
+
+
+int ngx_max_module;
+
+/* STUB: gobal srv and loc conf */
+void **ngx_srv_conf;
+void **ngx_loc_conf;
+
+
+int ngx_http_config_modules(ngx_pool_t *pool, ngx_http_module_t **modules)
+{
+    int i;
+
+    for (i = 0; modules[i]; i++) {
+        modules[i]->index = i;
+    }
+
+    ngx_max_module = i;
+
+    ngx_test_null(ngx_srv_conf,
+                  ngx_pcalloc(pool, sizeof(void *) * ngx_max_module),
+                  NGX_ERROR);
+    ngx_test_null(ngx_loc_conf,
+                  ngx_pcalloc(pool, sizeof(void *) * ngx_max_module),
+                  NGX_ERROR);
+
+    for (i = 0; modules[i]; i++) {
+        if (modules[i]->create_srv_conf)
+            ngx_srv_conf[i] = modules[i]->create_srv_conf(pool);
+
+        if (modules[i]->create_loc_conf)
+            ngx_loc_conf[i] = modules[i]->create_loc_conf(pool);
+    }
+}
+
+int ngx_http_init_modules(ngx_pool_t *pool, ngx_http_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 (*filter)(ngx_http_request_t *r, ngx_chain_t *ch);
+
+    filter = ngx_http_write_filter;
+
+    for (i = 0; modules[i]; i++) {
+        if (modules[i]->init_output_body_filter)
+            modules[i]->init_output_body_filter(&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_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) {
+                    cmd->set(pool, ngx_loc_conf[i], "index.html");
+                }
+            }
+        }
+    }
+}
new file mode 100644
--- /dev/null
+++ b/src/http/ngx_http_config.h
@@ -0,0 +1,17 @@
+#ifndef _NGX_HTTP_CONFIG_H_INCLUDED_
+#define _NGX_HTTP_CONFIG_H_INCLUDED_
+
+
+#include <ngx_alloc.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);
+
+
+extern void **ngx_srv_conf;
+extern void **ngx_loc_conf;
+
+
+#endif _NGX_HTTP_CONFIG_H_INCLUDED_
--- a/src/http/ngx_http_event.c
+++ b/src/http/ngx_http_event.c
@@ -7,13 +7,14 @@
 #include <ngx_alloc.h>
 #include <ngx_hunk.h>
 #include <ngx_connection.h>
-
 #include <ngx_http.h>
+#include <ngx_http_config.h>
 
 /* STUB */
 #include <ngx_http_output_filter.h>
-
 int ngx_http_static_handler(ngx_http_request_t *r);
+int ngx_http_index_handler(ngx_http_request_t *r);
+/* */
 
 
 int ngx_http_init_connection(ngx_connection_t *c);
@@ -131,9 +132,15 @@ int ngx_http_init_request(ngx_event_t *e
     r->connection = c;
     r->server = srv;
 
+    r->srv_conf = ngx_srv_conf;
+    r->loc_conf = ngx_loc_conf;
+
     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_http_close_request(r));
+
     ngx_test_null(r->header_in,
                   ngx_create_temp_hunk(r->pool, srv->header_buffer_size, 0, 0),
                   ngx_http_close_request(r));
@@ -432,6 +439,16 @@ static int ngx_http_handler(ngx_http_req
 */
 }
 
+int ngx_http_internal_redirect(ngx_http_request_t *r, char *uri)
+{
+    ngx_log_debug(r->connection->log, "internal redirect: '%s'" _ uri);
+
+    r->uri = uri;
+    r->uri_start = uri;
+    r->uri_end = uri + strlen(uri);
+    return ngx_http_handler(r);
+}
+
 static int ngx_http_set_default_handler(ngx_http_request_t *r)
 {
     int   err, rc;
@@ -442,11 +459,8 @@ static int ngx_http_set_default_handler(
                   NGX_HTTP_INTERNAL_SERVER_ERROR);
 
     if (*(r->uri_end - 1) == '/') {
-        return NGX_HTTP_INTERNAL_SERVER_ERROR;
-#if 0
         r->handler = ngx_http_index_handler;
         return NGX_OK;
-#endif
     }
 
     /* 20 bytes is spare space for some index name, i.e. index.html */
rename from src/http/modules/ngx_http_header_filter.c
rename to src/http/ngx_http_header_filter.c
--- a/src/http/ngx_http_modules.c
+++ b/src/http/ngx_http_modules.c
@@ -1,7 +1,13 @@
+
+#include <ngx_http.h>
 
 extern ngx_http_module_t ngx_http_output_filter_module;
+extern ngx_http_module_t ngx_http_write_filter_module;
+extern ngx_http_module_t ngx_http_index_module;
 
 ngx_http_module_t *ngx_http_modules[] = {
-    ngx_http_output_filter_module,
+    &ngx_http_write_filter_module,
+    &ngx_http_output_filter_module,
+    &ngx_http_index_module,
     NULL
 };
--- a/src/http/ngx_http_output_filter.c
+++ b/src/http/ngx_http_output_filter.c
@@ -1,44 +1,48 @@
-
 
 #include <ngx_core.h>
 #include <ngx_files.h>
 #include <ngx_string.h>
 #include <ngx_hunk.h>
+#include <ngx_config_command.h>
 #include <ngx_http.h>
+#include <ngx_http_config.h>
 #include <ngx_http_output_filter.h>
 
 
 static int ngx_http_output_filter_copy_hunk(ngx_hunk_t *dst, ngx_hunk_t *src);
 static int ngx_http_output_filter_init(
-            int (*next_filter)(ngx_http_request_t *r, ngx_chain_t *ch));
+            int (**next_filter)(ngx_http_request_t *r, ngx_chain_t *ch));
 static void *ngx_http_output_filter_create_conf(ngx_pool_t *pool);
-static void *ngx_http_output_filter_set_hunk_size(ngx_pool_t *pool, void *conf,
-                                                  char *size);
+
+
+static ngx_command_t ngx_http_output_filter_commands[];
 
 
 ngx_http_module_t  ngx_http_output_filter_module = {
-    NGX_HTTP_MODULE
+    NGX_HTTP_MODULE,
+    NULL,                                  /* create server config */
+    ngx_http_output_filter_create_conf,    /* create location config */
+    ngx_http_output_filter_commands,       /* module directives */
+    NULL,                                  /* init module */
+    ngx_http_output_filter_init            /* init output body filter */
+};
+
+
+static ngx_command_t ngx_http_output_filter_commands[] = {
+
+    {"output_buffer", ngx_conf_set_size_slot,
+     offsetof(ngx_http_output_filter_conf_t, hunk_size),
+     NGX_HTTP_LOC_CONF, NGX_CONF_TAKE1,
+     "set output filter buffer size"},
+
+    {NULL}
+
 };
 
 
 static int (*ngx_http_output_next_filter)(ngx_http_request_t *r,
                                           ngx_chain_t *ch);
 
-/* STUB */
-int ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *ch);
-
-int ngx_http_output_filter_stub_init(ngx_pool_t *pool, void *loc_conf)
-{
-    ngx_http_output_filter_conf_t *conf;
-
-    ngx_http_output_filter_init(ngx_http_write_filter);
-    conf = ngx_http_output_filter_create_conf(pool);
-    ngx_http_output_filter_set_hunk_size(pool, conf, "32");
-
-    loc_conf = conf;
-}
-/* */
-
 
 int ngx_http_output_filter(ngx_http_request_t *r, ngx_hunk_t *hunk)
 {
@@ -54,9 +58,9 @@ int ngx_http_output_filter(ngx_http_requ
                                                 ngx_http_output_filter_module);
 
     if (ctx == NULL) {
-        ngx_test_null(ctx,
-                    ngx_pcalloc(r->pool, sizeof(ngx_http_output_filter_ctx_t)),
-                    NGX_ERROR);
+        ngx_http_create_ctx(r, ctx,
+                            ngx_http_output_filter_module,
+                            sizeof(ngx_http_output_filter_ctx_t));
 
         ctx->next_filter = ngx_http_output_next_filter;
     }
@@ -67,7 +71,7 @@ int ngx_http_output_filter(ngx_http_requ
     if (hunk && (hunk->type & NGX_HUNK_LAST))
         ctx->last = 1;
 
-    for (once = 1; once || ctx->in; once--) {
+    for (once = 1; once || ctx->in; once = 0) {
 
          /* input chain is not empty */
         if (ctx->in) {
@@ -267,14 +271,6 @@ static int ngx_http_output_filter_copy_h
 }
 
 
-static int ngx_http_output_filter_init(
-            int (*next_filter)(ngx_http_request_t *r, ngx_chain_t *ch))
-{
-    ngx_http_output_next_filter = next_filter;
-
-    return NGX_OK;
-}
-
 static void *ngx_http_output_filter_create_conf(ngx_pool_t *pool)
 {
     ngx_http_output_filter_conf_t *conf;
@@ -283,18 +279,16 @@ static void *ngx_http_output_filter_crea
                   ngx_pcalloc(pool, sizeof(ngx_http_output_filter_conf_t)),
                   NULL);
 
-    conf->hunk_size = 16384;
+    conf->hunk_size = NGX_CONF_UNSET;
+
+    return conf;
 }
 
-static void *ngx_http_output_filter_set_hunk_size(ngx_pool_t *pool, void *conf,
-                                                  char *size)
+static int ngx_http_output_filter_init(
+            int (**next_filter)(ngx_http_request_t *r, ngx_chain_t *ch))
 {
-    ngx_http_output_filter_conf_t *cf = (ngx_http_output_filter_conf_t *) conf;
+    ngx_http_output_next_filter = *next_filter;
+    *next_filter = NULL;
 
-    cf->hunk_size = atoi(size);
-    if (cf->hunk_size <= 0)
-        return "Error";
-
-    cf->hunk_size *= 1024;
-    return NULL;
+    return NGX_OK;
 }
--- a/src/http/ngx_http_output_filter.h
+++ b/src/http/ngx_http_output_filter.h
@@ -25,5 +25,7 @@ typedef struct {
 
 int ngx_http_output_filter(ngx_http_request_t *r, ngx_hunk_t *hunk);
 
+extern ngx_http_module_t  ngx_http_output_filter_module;
+
 
 #endif /* _NGX_HTTP_OUTPUT_FILTER_H_INCLUDED_ */
--- a/src/http/ngx_http_write_filter.c
+++ b/src/http/ngx_http_write_filter.c
@@ -4,17 +4,35 @@
 #include <ngx_hunk.h>
 #include <ngx_event_write.h>
 #include <ngx_http.h>
+#include <ngx_http_config.h>
 #include <ngx_http_output_filter.h>
 #include <ngx_http_write_filter.h>
 
 
+static ngx_command_t ngx_http_write_filter_commands[];
+
+static void *ngx_http_write_filter_create_conf(ngx_pool_t *pool);
+
 ngx_http_module_t  ngx_http_write_filter_module = {
-    NGX_HTTP_MODULE
+    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                                   /* init output body filter */
 };
 
 
-/* STUB */
-/* */
+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"},
+
+    {NULL}
+
+};
 
 
 int ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
@@ -30,9 +48,9 @@ int ngx_http_write_filter(ngx_http_reque
                               ngx_get_module_ctx(r->main ? r->main : r,
                                                  ngx_http_write_filter_module);
     if (ctx == NULL)
-        ngx_test_null(ctx,
-                      ngx_pcalloc(r->pool, sizeof(ngx_http_write_filter_ctx_t)),
-                      NGX_ERROR);
+        ngx_http_create_ctx(r, ctx,
+                            ngx_http_write_filter_module,
+                            sizeof(ngx_http_write_filter_ctx_t));
 
     size = flush = 0;
     last = 0;
@@ -99,22 +117,10 @@ static void *ngx_http_write_filter_creat
     ngx_http_write_filter_conf_t *conf;
 
     ngx_test_null(conf,
-                  ngx_pcalloc(pool, sizeof(ngx_http_write_filter_conf_t)),
+                  ngx_palloc(pool, sizeof(ngx_http_write_filter_conf_t)),
                   NULL);
 
-    conf->buffer_output = 16384;
-}
-
-static void *ngx_http_write_filter_set_hunk_size(ngx_pool_t *pool, void *conf,
-                                                  char *size)
-{
-    ngx_http_write_filter_conf_t *cf = (ngx_http_write_filter_conf_t *) conf;
+    conf->buffer_output = NGX_CONF_UNSET;
 
-    cf->buffer_output = atoi(size);
-    if (cf->buffer_output <= 0)
-        return "Error";
-
-    cf->buffer_output *= 1024;
-    return NULL;
+    return conf;
 }
-
--- a/src/http/ngx_http_write_filter.h
+++ b/src/http/ngx_http_write_filter.h
@@ -17,5 +17,7 @@ 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;
+
 
 #endif /* _NGX_HTTP_WRITE_FILTER_H_INCLUDED_ */