changeset 118:644a7935144b NGINX_0_3_6

nginx 0.3.6 *) Change: now the IMAP/POP3 proxy do not send the empty login to authorization server. *) Feature: the "log_format" supports the variables in the $name form. *) Bugfix: if at least in one server was no the "listen" directive, then nginx did not listen on the 80 port; bug appeared in 0.3.3. *) Bugfix: if the URI part is omitted in "proxy_pass" directive, the the 80 port was always used.
author Igor Sysoev <http://sysoev.ru>
date Mon, 24 Oct 2005 00:00:00 +0400
parents 0821ea4ccfc5
children 85abc07cafba
files CHANGES CHANGES.ru auto/sources auto/threads conf/nginx.conf src/core/nginx.h src/core/ngx_connection.c src/core/ngx_inet.c src/core/ngx_output_chain.c src/http/modules/ngx_http_log_module.c src/http/modules/ngx_http_log_module.h src/http/modules/ngx_http_ssi_filter_module.c src/http/ngx_http_copy_filter_module.c src/http/ngx_http_core_module.c src/http/ngx_http_core_module.h src/http/ngx_http_header_filter_module.c src/http/ngx_http_log_module.c src/http/ngx_http_log_module.h src/http/ngx_http_postpone_filter_module.c src/http/ngx_http_request.c src/http/ngx_http_request.h src/http/ngx_http_upstream.c src/http/ngx_http_upstream.h src/http/ngx_http_variables.c src/imap/ngx_imap_handler.c
diffstat 24 files changed, 1509 insertions(+), 1371 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,17 @@
+
+Changes with nginx 0.3.6                                         24 Oct 2005
+
+    *) Change: now the IMAP/POP3 proxy do not send the empty login to 
+       authorization server.
+
+    *) Feature: the "log_format" supports the variables in the $name form.
+
+    *) Bugfix: if at least in one server was no the "listen" directive, 
+       then nginx did not listen on the 80 port; bug appeared in 0.3.3.
+
+    *) Bugfix: if the URI part is omitted in "proxy_pass" directive, the 
+       the 80 port was always used.
+
 
 Changes with nginx 0.3.5                                         21 Oct 2005
 
--- a/CHANGES.ru
+++ b/CHANGES.ru
@@ -1,3 +1,19 @@
+
+Изменения в nginx 0.3.6                                           24.10.2005
+
+    *) Изменение: IMAP/POP3 прокси теперь не передаёт серверу авторизации 
+       пустой логин.
+
+    *) Добавление: директива log_format поддерживает переменные в виде 
+       $name.
+
+    *) Исправление: если хотя бы в одном сервере не было описано ни одной 
+       директивы listen, то nginx не слушал на 80 порту; ошибка появилась в 
+       0.3.3.
+
+    *) Исправление: если в директиве proxy_pass отсутствовал URI, то всегда 
+       использовался порт 80.
+
 
 Изменения в nginx 0.3.5                                           21.10.2005
 
@@ -142,7 +158,7 @@
 
 Изменения в nginx 0.2.2                                           30.09.2005
 
-    *) Добавление: команда config errmsg в модуле ngx_http_sssi_module.
+    *) Добавление: команда config errmsg в модуле ngx_http_ssi_module.
 
     *) Изменение: переменные модуля ngx_http_geo_module можно 
        переопределять директивой set.
--- a/auto/sources
+++ b/auto/sources
@@ -262,7 +262,7 @@ HTTP_DEPS="src/http/ngx_http.h \
            src/http/ngx_http_script.h \
            src/http/ngx_http_upstream.h \
            src/http/ngx_http_busy_lock.h \
-           src/http/ngx_http_log_module.h"
+           src/http/modules/ngx_http_log_module.h"
 
 HTTP_SRCS="src/http/ngx_http.c \
            src/http/ngx_http_core_module.c \
@@ -272,7 +272,7 @@ HTTP_SRCS="src/http/ngx_http.c \
            src/http/ngx_http_header_filter_module.c \
            src/http/ngx_http_write_filter_module.c \
            src/http/ngx_http_copy_filter_module.c \
-           src/http/ngx_http_log_module.c \
+           src/http/modules/ngx_http_log_module.c \
            src/http/ngx_http_request_body.c \
            src/http/ngx_http_variables.c \
            src/http/ngx_http_script.c \
--- a/auto/threads
+++ b/auto/threads
@@ -50,7 +50,7 @@ case $USE_THREADS in
                 CORE_LIBS="$CORE_LIBS -pthread"
             ;;
 
-            FreeBSD:5*)
+            FreeBSD:[567]*)
                 have=NGX_THREADS . auto/have
                 CORE_SRCS="$CORE_SRCS $PTHREAD_SRCS"
                 CORE_LIBS="$CORE_LIBS -lc_r"
--- a/conf/nginx.conf
+++ b/conf/nginx.conf
@@ -34,6 +34,12 @@ http {
             index  index.html index.htm;
         }
 
+        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
+        #
+        #location ~ \.php$ {
+        #    proxy_pass   http://127.0.0.1;
+        #}
+
         # deny access to .htaccess files
         #
         #location ~ /\.ht {
--- a/src/core/nginx.h
+++ b/src/core/nginx.h
@@ -8,7 +8,7 @@
 #define _NGINX_H_INCLUDED_
 
 
-#define NGINX_VER          "nginx/0.3.5"
+#define NGINX_VER          "nginx/0.3.6"
 
 #define NGINX_VAR          "NGINX"
 #define NGX_OLDPID_EXT     ".oldbin"
--- a/src/core/ngx_connection.c
+++ b/src/core/ngx_connection.c
@@ -367,9 +367,8 @@ ngx_configure_listening_socket(ngx_cycle
                 == -1)
             {
                 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
-                              "setsockopt(SO_RCVBUF) %V failed, ignored",
-                              &ls[i].addr_text);
-                return;
+                              "setsockopt(SO_RCVBUF, %d) %V failed, ignored",
+                              ls[i].rcvbuf, &ls[i].addr_text);
             }
         }
 
@@ -379,9 +378,8 @@ ngx_configure_listening_socket(ngx_cycle
                 == -1)
             {
                 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
-                              "setsockopt(SO_SNDBUF) %V failed, ignored",
-                              &ls[i].addr_text);
-                return;
+                              "setsockopt(SO_SNDBUF, %d) %V failed, ignored",
+                              ls[i].sndbuf, &ls[i].addr_text);
             }
         }
 
--- a/src/core/ngx_inet.c
+++ b/src/core/ngx_inet.c
@@ -452,7 +452,23 @@ ngx_inet_parse_host_port(ngx_inet_upstre
         }
     }
 
-    if (u->port_text.data == NULL) {
+    if (u->port_text.data) {
+
+        if (u->port_text.len == 0) {
+            u->port_text.len = &url->data[i] - u->port_text.data;
+
+            if (u->port_text.len == 0) {
+                return "invalid port";
+            }
+        }
+
+        port = ngx_atoi(u->port_text.data, u->port_text.len);
+
+        if (port == NGX_ERROR || port < 1 || port > 65536) {
+            return "invalid port";
+        }
+
+    } else {
         port = ngx_atoi(url->data, url->len);
 
         if (port == NGX_ERROR) {
@@ -464,18 +480,6 @@ ngx_inet_parse_host_port(ngx_inet_upstre
 
         u->port_text = *url;
         u->wildcard = 1;
-
-    } else {
-        if (u->port_text.len == 0) {
-            u->default_port = 1;
-            return NULL;
-        }
-
-        port = ngx_atoi(u->port_text.data, u->port_text.len);
-
-        if (port == NGX_ERROR || port < 1 || port > 65536) {
-            return "invalid port";
-        }
     }
 
     u->port = (in_port_t) port;
--- a/src/core/ngx_output_chain.c
+++ b/src/core/ngx_output_chain.c
@@ -212,12 +212,12 @@ ngx_output_chain(ngx_output_chain_ctx_t 
 
         last = ctx->output_filter(ctx->filter_ctx, out);
 
+        if (last == NGX_ERROR || last == NGX_DONE) {
+            return last;
+        }
+
         ngx_chain_update_chains(&ctx->free, &ctx->busy, &out, ctx->tag);
         last_out = &out;
-
-        if (last == NGX_ERROR) {
-            return last;
-        }
     }
 }
 
new file mode 100644
--- /dev/null
+++ b/src/http/modules/ngx_http_log_module.c
@@ -0,0 +1,1263 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_http.h>
+#include <nginx.h>
+
+
+static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
+static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
+static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
+static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
+static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
+static u_char *ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
+static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
+static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
+static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
+static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
+
+static size_t ngx_http_log_request_getlen(ngx_http_request_t *r,
+    uintptr_t data);
+static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
+
+static ngx_int_t ngx_http_log_header_in_compile(ngx_conf_t *cf,
+    ngx_http_log_op_t *op, ngx_str_t *value);
+static size_t ngx_http_log_header_in_getlen(ngx_http_request_t *r,
+    uintptr_t data);
+static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
+static size_t ngx_http_log_unknown_header_in_getlen(ngx_http_request_t *r,
+    uintptr_t data);
+static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
+    u_char *buf, ngx_http_log_op_t *op);
+
+static ngx_int_t ngx_http_log_header_out_compile(ngx_conf_t *cf,
+    ngx_http_log_op_t *op, ngx_str_t *value);
+static size_t ngx_http_log_header_out_getlen(ngx_http_request_t *r,
+    uintptr_t data);
+static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
+static size_t ngx_http_log_unknown_header_out_getlen(ngx_http_request_t *r,
+    uintptr_t data);
+static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r,
+    u_char *buf, ngx_http_log_op_t *op);
+
+static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
+    u_char *buf, ngx_http_log_op_t *op);
+static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
+    u_char *buf, ngx_http_log_op_t *op);
+
+static ngx_table_elt_t *ngx_http_log_unknown_header(ngx_list_t *headers,
+    ngx_str_t *value);
+
+static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf,
+    ngx_http_log_op_t *op, ngx_str_t *value);
+static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
+    uintptr_t data);
+static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
+
+
+static ngx_int_t ngx_http_log_set_formats(ngx_conf_t *cf);
+static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
+static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
+static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
+    void *child);
+static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
+    void *conf);
+static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
+    void *conf);
+static ngx_int_t ngx_http_log_init(ngx_cycle_t *cycle);
+
+
+static ngx_command_t  ngx_http_log_commands[] = {
+
+    { ngx_string("log_format"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
+      ngx_http_log_set_format,
+      NGX_HTTP_MAIN_CONF_OFFSET,
+      0,
+      NULL },
+
+    { ngx_string("access_log"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
+      ngx_http_log_set_log,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      0,
+      NULL },
+
+      ngx_null_command
+};
+
+
+ngx_http_module_t  ngx_http_log_module_ctx = {
+    ngx_http_log_set_formats,              /* preconfiguration */
+    NULL,                                  /* postconfiguration */
+
+    ngx_http_log_create_main_conf,         /* create main configuration */
+    NULL,                                  /* init main configuration */
+
+    NULL,                                  /* create server configuration */
+    NULL,                                  /* merge server configuration */
+
+    ngx_http_log_create_loc_conf,          /* create location configration */
+    ngx_http_log_merge_loc_conf            /* merge location configration */
+};
+
+
+ngx_module_t  ngx_http_log_module = {
+    NGX_MODULE_V1,
+    &ngx_http_log_module_ctx,              /* module context */
+    ngx_http_log_commands,                 /* module directives */
+    NGX_HTTP_MODULE,                       /* module type */
+    NULL,                                  /* init master */
+    ngx_http_log_init,                     /* init module */
+    NULL,                                  /* init process */
+    NULL,                                  /* init thread */
+    NULL,                                  /* exit thread */
+    NULL,                                  /* exit process */
+    NULL,                                  /* exit master */
+    NGX_MODULE_V1_PADDING
+};
+
+
+static ngx_str_t http_access_log = ngx_string(NGX_HTTP_LOG_PATH);
+
+
+static ngx_str_t ngx_http_combined_fmt =
+#if 0
+    ngx_string("$remote_addr - $remote_user [%time] "
+               "\"$request\" %status %apache_length "
+               "\"$http_referer\" \"$http_user_agent\"");
+#endif
+    ngx_string("%addr - - [%time] "
+               "\"%request\" %status %apache_length "
+               "\"%{referer}i\" \"%{user-agent}i\"");
+
+
+ngx_http_log_op_name_t ngx_http_log_fmt_ops[] = {
+    { ngx_string("addr"), INET_ADDRSTRLEN - 1, NULL, NULL, ngx_http_log_addr },
+    { ngx_string("conn"), NGX_ATOMIC_T_LEN, NULL, NULL,
+                          ngx_http_log_connection },
+    { ngx_string("pipe"), 1, NULL, NULL, ngx_http_log_pipe },
+    { ngx_string("time"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
+                          NULL, NULL, ngx_http_log_time },
+    { ngx_string("msec"), NGX_TIME_T_LEN + 4, NULL, NULL, ngx_http_log_msec },
+    { ngx_string("request_time"), NGX_TIME_T_LEN, NULL, NULL,
+                          ngx_http_log_request_time },
+    { ngx_string("status"), 3, NULL, NULL, ngx_http_log_status },
+    { ngx_string("length"), NGX_OFF_T_LEN, NULL, NULL, ngx_http_log_length },
+    { ngx_string("apache_length"), NGX_OFF_T_LEN,
+                          NULL, NULL, ngx_http_log_apache_length },
+    { ngx_string("request_length"), NGX_SIZE_T_LEN,
+                          NULL, NULL, ngx_http_log_request_length },
+
+    { ngx_string("request"), 0, NULL,
+                          ngx_http_log_request_getlen,
+                          ngx_http_log_request },
+
+    { ngx_string("i"), 0, ngx_http_log_header_in_compile, NULL,
+                          ngx_http_log_header_in },
+    { ngx_string("o"), 0, ngx_http_log_header_out_compile, NULL,
+                          ngx_http_log_header_out },
+    { ngx_string("v"), 0, ngx_http_log_variable_compile, NULL,
+                          ngx_http_log_variable },
+
+    { ngx_null_string, 0, NULL, NULL, NULL }
+};
+
+
+ngx_int_t
+ngx_http_log_handler(ngx_http_request_t *r)
+{
+    ngx_uint_t                i, l;
+    u_char                   *line, *p;
+    size_t                    len;
+    ngx_http_log_t           *log;
+    ngx_http_log_op_t        *op;
+    ngx_http_log_loc_conf_t  *lcf;
+#if (NGX_WIN32)
+    u_long                    written;
+#endif
+
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http log handler");
+
+    lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
+
+    if (lcf->off) {
+        return NGX_OK;
+    }
+
+    log = lcf->logs->elts;
+    for (l = 0; l < lcf->logs->nelts; l++) {
+
+        len = 0;
+        op = log[l].ops->elts;
+        for (i = 0; i < log[l].ops->nelts; i++) {
+            if (op[i].len == 0) {
+                len += op[i].getlen(r, op[i].data);
+
+            } else {
+                len += op[i].len;
+            }
+        }
+
+#if (NGX_WIN32)
+        len += 2;
+#else
+        len++;
+#endif
+
+        line = ngx_palloc(r->pool, len);
+        if (line == NULL) {
+            return NGX_ERROR;
+        }
+
+        p = line;
+
+        for (i = 0; i < log[l].ops->nelts; i++) {
+            p = op[i].run(r, p, &op[i]);
+        }
+
+#if (NGX_WIN32)
+        *p++ = CR; *p++ = LF;
+        WriteFile(log[l].file->fd, line, p - line, &written, NULL);
+#else
+        *p++ = LF;
+        write(log[l].file->fd, line, p - line);
+#endif
+    }
+
+    return NGX_OK;
+}
+
+
+static u_char *
+ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
+{
+    size_t     len;
+    uintptr_t  data;
+
+    len = op->len;
+    data = op->data;
+
+    while (len--) {
+        *buf++ = (u_char) (data & 0xff);
+        data >>= 8;
+    }
+
+    return buf;
+}
+
+
+static u_char *
+ngx_http_log_copy_long(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
+{
+    return ngx_cpymem(buf, (u_char *) op->data, op->len);
+}
+
+
+static u_char *
+ngx_http_log_addr(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
+{
+    return ngx_cpymem(buf, r->connection->addr_text.data,
+                      r->connection->addr_text.len);
+}
+
+
+static u_char *
+ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
+{
+    return ngx_sprintf(buf, "%ui", r->connection->number);
+}
+
+
+static u_char *
+ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
+{
+    if (r->pipeline) {
+        *buf = 'p';
+    } else {
+        *buf = '.';
+    }
+
+    return buf + 1;
+}
+
+
+static u_char *
+ngx_http_log_time(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
+{
+    return ngx_cpymem(buf, ngx_cached_http_log_time.data,
+                      ngx_cached_http_log_time.len);
+}
+
+
+static u_char *
+ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
+{
+    ngx_time_t  *tp;
+
+    tp = ngx_timeofday();
+
+    return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
+}
+
+
+static u_char *
+ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
+{
+    time_t  elapsed;
+
+    elapsed = ngx_time() - r->start_time;
+
+    return ngx_sprintf(buf, "%T", elapsed);
+}
+
+
+static size_t
+ngx_http_log_request_getlen(ngx_http_request_t *r, uintptr_t data)
+{
+    return r->request_line.len;
+}
+
+
+static u_char *
+ngx_http_log_request(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
+{
+    return ngx_cpymem(buf, r->request_line.data, r->request_line.len);
+}
+
+
+static u_char *
+ngx_http_log_status(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
+{
+    return ngx_sprintf(buf, "%ui",
+                       r->err_status ? r->err_status : r->headers_out.status);
+}
+
+
+static u_char *
+ngx_http_log_length(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
+{
+    return ngx_sprintf(buf, "%O", r->connection->sent);
+}
+
+
+static u_char *
+ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
+{
+    off_t  length;
+
+    length = r->connection->sent - r->header_size;
+
+    if (length > 0) {
+        return ngx_sprintf(buf, "%O", length);
+    }
+
+    *buf = '0';
+
+    return buf + 1;
+}
+
+
+static u_char *
+ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
+{
+    return ngx_sprintf(buf, "%z", r->request_length);
+}
+
+
+static ngx_int_t
+ngx_http_log_header_in_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
+    ngx_str_t *value)
+{
+    ngx_uint_t  i;
+
+    op->len = 0;
+
+    for (i = 0; ngx_http_headers_in[i].name.len != 0; i++) {
+
+        if (ngx_http_headers_in[i].name.len != value->len) {
+            continue;
+        }
+
+        /* STUB: "Cookie" speacial handling */
+        if (ngx_http_headers_in[i].offset == 0) {
+            continue;
+        }
+
+        if (ngx_strncasecmp(ngx_http_headers_in[i].name.data, value->data,
+                            value->len) == 0)
+        {
+            op->getlen = ngx_http_log_header_in_getlen;
+            op->run = ngx_http_log_header_in;
+            op->data = ngx_http_headers_in[i].offset;
+
+            return NGX_OK;
+        }
+    }
+
+    op->getlen = ngx_http_log_unknown_header_in_getlen;
+    op->run = ngx_http_log_unknown_header_in;
+    op->data = (uintptr_t) value;
+
+    return NGX_OK;
+}
+
+
+static size_t
+ngx_http_log_header_in_getlen(ngx_http_request_t *r, uintptr_t data)
+{
+    ngx_table_elt_t  *h;
+
+    h = *(ngx_table_elt_t **) ((char *) &r->headers_in + data);
+
+    if (h) {
+        return h->value.len;
+    }
+
+    return 1;
+}
+
+
+static u_char *
+ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
+{
+    ngx_table_elt_t  *h;
+
+    h = *(ngx_table_elt_t **) ((char *) &r->headers_in + op->data);
+
+    if (h) {
+        return ngx_cpymem(buf, h->value.data, h->value.len);
+    }
+
+    *buf = '-';
+
+    return buf + 1;
+}
+
+
+static size_t
+ngx_http_log_unknown_header_in_getlen(ngx_http_request_t *r, uintptr_t data)
+{
+    ngx_table_elt_t  *h;
+
+    h = ngx_http_log_unknown_header(&r->headers_in.headers, (ngx_str_t *) data);
+
+    if (h) {
+        return h->value.len;
+    }
+
+    return 1;
+}
+
+
+static u_char *
+ngx_http_log_unknown_header_in(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
+{
+    ngx_table_elt_t  *h;
+
+    h = ngx_http_log_unknown_header(&r->headers_in.headers,
+                                    (ngx_str_t *) op->data);
+
+    if (h) {
+        return ngx_cpymem(buf, h->value.data, h->value.len);
+    }
+
+    *buf = '-';
+
+    return buf + 1;
+}
+
+
+static ngx_int_t
+ngx_http_log_header_out_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
+    ngx_str_t *value)
+{
+    ngx_uint_t  i;
+
+    op->len = 0;
+
+    for (i = 0; ngx_http_headers_out[i].name.len != 0; i++) {
+
+        if (ngx_http_headers_out[i].name.len != value->len) {
+            continue;
+        }
+
+        if (ngx_strncasecmp(ngx_http_headers_out[i].name.data, value->data,
+                            value->len) == 0)
+        {
+            op->getlen = ngx_http_log_header_out_getlen;
+            op->run = ngx_http_log_header_out;
+            op->data = ngx_http_headers_out[i].offset;
+
+            return NGX_OK;
+        }
+    }
+
+    if (value->len == sizeof("Connection") - 1
+        && ngx_strncasecmp(value->data, "Connection", value->len) == 0)
+    {
+        op->len = sizeof("keep-alive") - 1;
+        op->getlen = NULL;
+        op->run = ngx_http_log_connection_header_out;
+        op->data = 0;
+        return NGX_OK;
+    }
+
+    if (value->len == sizeof("Transfer-Encoding") - 1
+        && ngx_strncasecmp(value->data, "Transfer-Encoding", value->len) == 0)
+    {
+        op->len = sizeof("chunked") - 1;
+        op->getlen = NULL;
+        op->run = ngx_http_log_transfer_encoding_header_out;
+        op->data = 0;
+        return NGX_OK;
+    }
+
+    op->getlen = ngx_http_log_unknown_header_out_getlen;
+    op->run = ngx_http_log_unknown_header_out;
+    op->data = (uintptr_t) value;
+
+    return NGX_OK;
+}
+
+
+static size_t
+ngx_http_log_header_out_getlen(ngx_http_request_t *r, uintptr_t data)
+{
+    ngx_table_elt_t  *h;
+
+    h = *(ngx_table_elt_t **) ((char *) &r->headers_out + data);
+
+    if (h) {
+        return h->value.len;
+    }
+
+    /*
+     * No header pointer was found.
+     * However, some headers: "Date", "Server", "Content-Length",
+     * and "Last-Modified" have a special handling in the header filter,
+     * but we do not set up their pointers in the filter,
+     * because they are too seldom needed to be logged.
+     */
+
+    if (data == offsetof(ngx_http_headers_out_t, date)) {
+        return ngx_cached_http_time.len;
+    }
+
+    if (data == offsetof(ngx_http_headers_out_t, server)) {
+        return (sizeof(NGINX_VER) - 1);
+    }
+
+    if (data == offsetof(ngx_http_headers_out_t, content_length)) {
+        if (r->headers_out.content_length_n == -1) {
+            return 1;
+        }
+
+        return NGX_OFF_T_LEN;
+    }
+
+    if (data == offsetof(ngx_http_headers_out_t, last_modified)) {
+        if (r->headers_out.last_modified_time == -1) {
+            return 1;
+        }
+
+        return sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
+    }
+
+    return 1;
+}
+
+
+static u_char *
+ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
+{
+    ngx_table_elt_t  *h;
+
+    h = *(ngx_table_elt_t **) ((char *) &r->headers_out + op->data);
+
+    if (h) {
+        return ngx_cpymem(buf, h->value.data, h->value.len);
+    }
+
+    /*
+     * No header pointer was found.
+     * However, some headers: "Date", "Server", "Content-Length",
+     * and "Last-Modified" have a special handling in the header filter,
+     * but we do not set up their pointers in the filter,
+     * because they are too seldom needed to be logged.
+     */
+
+    if (op->data == offsetof(ngx_http_headers_out_t, date)) {
+        return ngx_cpymem(buf, ngx_cached_http_time.data,
+                          ngx_cached_http_time.len);
+    }
+
+    if (op->data == offsetof(ngx_http_headers_out_t, server)) {
+        return ngx_cpymem(buf, NGINX_VER, sizeof(NGINX_VER) - 1);
+    }
+
+    if (op->data == offsetof(ngx_http_headers_out_t, content_length)) {
+        if (r->headers_out.content_length_n == -1) {
+            *buf = '-';
+
+            return buf + 1;
+        }
+
+        return ngx_sprintf(buf, "%O", r->headers_out.content_length_n);
+    }
+
+    if (op->data == offsetof(ngx_http_headers_out_t, last_modified)) {
+        if (r->headers_out.last_modified_time == -1) {
+            *buf = '-';
+
+            return buf + 1;
+        }
+
+        return ngx_http_time(buf, r->headers_out.last_modified_time);
+    }
+
+    *buf = '-';
+
+    return buf + 1;
+}
+
+
+static size_t
+ngx_http_log_unknown_header_out_getlen(ngx_http_request_t *r, uintptr_t data)
+{
+    ngx_table_elt_t  *h;
+
+    h = ngx_http_log_unknown_header(&r->headers_out.headers,
+                                    (ngx_str_t *) data);
+
+    if (h) {
+        return h->value.len;
+    }
+
+    return 1;
+}
+
+
+static u_char *
+ngx_http_log_unknown_header_out(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
+{
+    ngx_table_elt_t  *h;
+
+    h = ngx_http_log_unknown_header(&r->headers_out.headers,
+                                    (ngx_str_t *) op->data);
+
+    if (h) {
+        return ngx_cpymem(buf, h->value.data, h->value.len);
+    }
+
+    *buf = '-';
+
+    return buf + 1;
+}
+
+
+static ngx_table_elt_t *
+ngx_http_log_unknown_header(ngx_list_t *headers, ngx_str_t *value)
+{
+    ngx_uint_t        i;
+    ngx_list_part_t  *part;
+    ngx_table_elt_t  *h;
+
+    part = &headers->part;
+    h = part->elts;
+
+    for (i = 0; /* void */; i++) {
+
+        if (i >= part->nelts) {
+            if (part->next == NULL) {
+                break;
+            }
+
+            part = part->next;
+            h = part->elts;
+            i = 0;
+        }
+
+        if (h[i].key.len != value->len) {
+            continue;
+        }
+
+        if (ngx_strncasecmp(h[i].key.data, value->data, value->len) == 0) {
+            return &h[i];
+        }
+    }
+
+    return NULL;
+}
+
+
+static u_char *
+ngx_http_log_connection_header_out(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
+{
+    if (r->keepalive) {
+        return ngx_cpymem(buf, "keep-alive", sizeof("keep-alive") - 1);
+
+    } else {
+        return ngx_cpymem(buf, "close", sizeof("close") - 1);
+    }
+}
+
+
+static u_char *
+ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
+{
+    if (r->chunked) {
+        return ngx_cpymem(buf, "chunked", sizeof("chunked") - 1);
+    }
+
+    *buf = '-';
+
+    return buf + 1;
+}
+
+
+static ngx_int_t 
+ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
+    ngx_str_t *value)
+{
+    ngx_int_t  index;
+
+    index = ngx_http_get_variable_index(cf, value);
+    if (index == NGX_ERROR) {
+        return NGX_ERROR;
+    }
+
+    op->len = 0;
+    op->getlen = ngx_http_log_variable_getlen;
+    op->run = ngx_http_log_variable;
+    op->data = index;
+
+    return NGX_OK;
+}
+
+
+static size_t
+ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data)
+{
+    ngx_http_variable_value_t  *value;
+
+    value = ngx_http_get_indexed_variable(r, data);
+
+    if (value == NULL
+        || value == NGX_HTTP_VAR_NOT_FOUND
+        || value->text.len == 0)
+    {
+        return 1;
+    }
+
+    return value->text.len;
+}
+
+
+static u_char *
+ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
+{
+    ngx_http_variable_value_t  *value;
+
+    value = ngx_http_get_indexed_variable(r, op->data);
+
+    if (value == NULL
+        || value == NGX_HTTP_VAR_NOT_FOUND
+        || value->text.len == 0)
+    {
+        *buf = '-';
+        return buf + 1;
+    }
+
+    return ngx_cpymem(buf, value->text.data, value->text.len);
+}
+
+
+static ngx_int_t
+ngx_http_log_set_formats(ngx_conf_t *cf)
+{
+    ngx_http_log_op_name_t  *op;
+
+    for (op = ngx_http_log_fmt_ops; op->name.len; op++) { /* void */ }
+    op->run = NULL;
+
+    return NGX_OK;
+}
+
+
+static void *
+ngx_http_log_create_main_conf(ngx_conf_t *cf)
+{
+    ngx_http_log_main_conf_t  *conf;
+
+    char       *rc;
+    ngx_str_t  *value;
+
+    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t));
+    if (conf == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    if (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))
+        == NGX_ERROR)
+    {
+        return NGX_CONF_ERROR;
+    }
+
+    cf->args->nelts = 0;
+
+    value = ngx_array_push(cf->args);
+    if (value == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    value->len = 0;
+    value->data = NULL;
+
+    value = ngx_array_push(cf->args);
+    if (value == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    value->len = sizeof("combined") - 1;
+    value->data = (u_char *) "combined";
+
+    value = ngx_array_push(cf->args);
+    if (value == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    *value = ngx_http_combined_fmt;
+
+    rc = ngx_http_log_set_format(cf, NULL, conf);
+    if (rc != NGX_CONF_OK) {
+        return NGX_CONF_ERROR;
+    }
+
+    return conf;
+}
+
+
+static void *
+ngx_http_log_create_loc_conf(ngx_conf_t *cf)
+{
+    ngx_http_log_loc_conf_t  *conf;
+
+    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t));
+    if (conf == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    return conf;
+}
+
+
+static char *
+ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
+{
+    ngx_http_log_loc_conf_t *prev = parent;
+    ngx_http_log_loc_conf_t *conf = child;
+
+    ngx_http_log_t            *log;
+    ngx_http_log_fmt_t        *fmt;
+    ngx_http_log_main_conf_t  *lmcf;
+
+    if (conf->logs == NULL) {
+
+        if (conf->off) {
+            return NGX_CONF_OK;
+        }
+
+        if (prev->logs) {
+            conf->logs = prev->logs;
+
+        } else {
+
+            if (prev->off) {
+                conf->off = prev->off;
+                return NGX_CONF_OK;
+            }
+
+            conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
+            if (conf->logs == NULL) {
+                return NGX_CONF_ERROR;
+            }
+
+            log = ngx_array_push(conf->logs);
+            if (log == NULL) {
+                return NGX_CONF_ERROR;
+            }
+
+            log->file = ngx_conf_open_file(cf->cycle, &http_access_log);
+            if (log->file == NULL) {
+                return NGX_CONF_ERROR;
+            }
+
+            lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
+            fmt = lmcf->formats.elts;
+
+            /* the default "combined" format */
+            log->ops = fmt[0].ops;
+        }
+    }
+
+    return NGX_CONF_OK;
+}
+
+
+static char *
+ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+    ngx_http_log_loc_conf_t *llcf = conf;
+
+    ngx_uint_t                 i;
+    ngx_str_t                 *value, name;
+    ngx_http_log_t            *log;
+    ngx_http_log_fmt_t        *fmt;
+    ngx_http_log_main_conf_t  *lmcf;
+
+    value = cf->args->elts;
+
+    if (ngx_strcmp(value[1].data, "off") == 0) {
+        llcf->off = 1;
+        return NGX_CONF_OK;
+    }
+
+    if (llcf->logs == NULL) {
+        llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
+        if (llcf->logs == NULL) {
+            return NGX_CONF_ERROR;
+        }
+    }
+
+    lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
+
+    log = ngx_array_push(llcf->logs);
+    if (log == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    log->file = ngx_conf_open_file(cf->cycle, &value[1]);
+    if (log->file == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    if (cf->args->nelts == 3) {
+        name = value[2];
+    } else {
+        name.len = sizeof("combined") - 1;
+        name.data = (u_char *) "combined";
+    }
+
+    fmt = lmcf->formats.elts;
+    for (i = 0; i < lmcf->formats.nelts; i++) {
+        if (fmt[i].name.len == name.len
+            && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
+        {
+            log->ops = fmt[i].ops;
+            return NGX_CONF_OK;
+        }
+    }
+
+    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                       "unknown log format \"%V\"", &name);
+
+    return NGX_CONF_ERROR;
+}
+
+
+static char *
+ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+    ngx_http_log_main_conf_t *lmcf = conf;
+
+    u_char                     *data, *p, *fname, ch;
+    size_t                      i, len, fname_len;
+    ngx_str_t                  *value, var, arg, *a;
+    ngx_uint_t                  s, f, bracket;
+    ngx_http_log_op_t          *op;
+    ngx_http_log_fmt_t         *fmt;
+    ngx_http_log_op_name_t     *name;
+
+    value = cf->args->elts;
+
+    fmt = lmcf->formats.elts;
+    for (f = 0; f < lmcf->formats.nelts; f++) {
+        if (fmt[f].name.len == value[1].len
+            && ngx_strcmp(fmt->name.data, value[1].data) == 0)
+        {
+            return "duplicate \"log_format\" name";
+        }
+    }
+
+    fmt = ngx_array_push(&lmcf->formats);
+    if (fmt == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    fmt->name = value[1];
+
+    fmt->ops = ngx_array_create(cf->pool, 20, sizeof(ngx_http_log_op_t));
+    if (fmt->ops == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    arg.data = NULL;
+
+    for (s = 2; s < cf->args->nelts; s++) {
+
+        i = 0;
+
+        while (i < value[s].len) {
+
+            op = ngx_array_push(fmt->ops);
+            if (op == NULL) {
+                return NGX_CONF_ERROR;
+            }
+
+            data = &value[s].data[i];
+
+            if (value[s].data[i] == '%') {
+                i++;
+
+                if (i == value[s].len) {
+                    goto invalid;
+                }
+
+                if (value[s].data[i] == '{') {
+                    i++;
+
+                    arg.data = &value[s].data[i];
+
+                    while (i < value[s].len && value[s].data[i] != '}') {
+                        i++;
+                    }
+
+                    arg.len = &value[s].data[i] - arg.data;
+
+                    if (i == value[s].len || arg.len == 0) {
+                        goto invalid;
+                    }
+
+                    i++;
+
+                } else {
+                    arg.len = 0;
+                }
+
+                fname = &value[s].data[i];
+
+                while (i < value[s].len
+                       && ((value[s].data[i] >= 'a' && value[s].data[i] <= 'z')
+                           || value[s].data[i] == '_'))
+                {
+                    i++;
+                }
+
+                fname_len = &value[s].data[i] - fname;
+
+                if (fname_len == 0) {
+                    goto invalid;
+                }
+
+                for (name = ngx_http_log_fmt_ops; name->run; name++) {
+                    if (name->name.len == 0) {
+                        name = (ngx_http_log_op_name_t *) name->run;
+                    }
+
+                    if (name->name.len == fname_len
+                        && ngx_strncmp(name->name.data, fname, fname_len) == 0)
+                    {
+                        if (name->compile == NULL) {
+                            if (arg.len) {
+                                fname[fname_len] = '\0';
+                                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                                               "\"%s\" must not have argument",
+                                               data);
+                                return NGX_CONF_ERROR;
+                            }
+
+                            op->len = name->len;
+                            op->getlen = name->getlen;
+                            op->run = name->run;
+                            op->data = 0;
+
+                            break;
+                        }
+
+                        if (arg.len == 0) {
+                            fname[fname_len] = '\0';
+                            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                                               "\"%s\" requires argument", 
+                                               data);
+                            return NGX_CONF_ERROR;
+                        }
+
+                        a = ngx_palloc(cf->pool, sizeof(ngx_str_t));
+                        if (a == NULL) {
+                            return NGX_CONF_ERROR;
+                        }
+
+                        *a = arg;
+                        if (name->compile(cf, op, a) == NGX_ERROR) {
+                            return NGX_CONF_ERROR;
+                        }
+
+                        break;
+                    }
+                }
+
+                if (name->name.len == 0) {
+                    goto invalid;
+                }
+
+            } else if (value[s].data[i] == '$') {
+
+                if (++i == value[s].len) {
+                    goto invalid;
+                }
+
+                if (value[s].data[i] == '{') {
+                    bracket = 1;
+
+                    if (++i == value[s].len) {
+                        goto invalid;
+                    }
+
+                    var.data = &value[s].data[i];
+
+                } else {
+                    bracket = 0;
+                    var.data = &value[s].data[i];
+                }
+
+                for (var.len = 0; i < value[s].len; i++, var.len++) {
+                    ch = value[s].data[i];
+
+                    if (ch == '}' && bracket) {
+                        i++;
+                        bracket = 0;
+                        break;
+                    }
+
+                    if ((ch >= 'A' && ch <= 'Z')
+                        || (ch >= 'a' && ch <= 'z')
+                        || (ch >= '0' && ch <= '9')
+                        || ch == '_')
+                    {
+                        continue;
+                    }
+
+                    break;
+                }
+
+                if (bracket) {
+                    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                                       "the closing bracket in \"%V\" "
+                                       "variable is missing", &var);
+                    return NGX_CONF_ERROR;
+                }
+
+                if (var.len == 0) {
+                    goto invalid;
+                }
+
+                if (ngx_http_log_variable_compile(cf, op, &var) != NGX_OK) {
+                    return NGX_CONF_ERROR;
+                }
+
+            } else {
+                i++;
+
+                while (i < value[s].len
+                       && value[s].data[i] != '$'
+                       && value[s].data[i] != '%')
+                {
+                    i++;
+                }
+
+                len = &value[s].data[i] - data;
+
+                if (len) {
+
+                    op->len = len;
+                    op->getlen = NULL;
+
+                    if (len <= sizeof(uintptr_t)) {
+                        op->run = ngx_http_log_copy_short;
+                        op->data = 0;
+
+                        while (len--) {
+                            op->data <<= 8;
+                            op->data |= data[len];
+                        }
+
+                    } else {
+                        op->run = ngx_http_log_copy_long;
+
+                        p = ngx_palloc(cf->pool, len);
+                        if (p == NULL) {
+                            return NGX_CONF_ERROR;
+                        }
+
+                        ngx_memcpy(p, data, len);
+                        op->data = (uintptr_t) p;
+                    }
+                }
+            }
+        }
+    }
+
+    return NGX_CONF_OK;
+
+invalid:
+
+    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%s\"", data);
+
+    return NGX_CONF_ERROR;
+}
+
+
+static ngx_int_t
+ngx_http_log_init(ngx_cycle_t *cycle)
+{
+    ngx_http_core_main_conf_t  *cmcf;
+
+    cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
+
+    cmcf->log_handler = ngx_http_log_handler;
+
+    return NGX_OK;
+}
rename from src/http/ngx_http_log_module.h
rename to src/http/modules/ngx_http_log_module.h
--- a/src/http/modules/ngx_http_ssi_filter_module.c
+++ b/src/http/modules/ngx_http_ssi_filter_module.c
@@ -229,9 +229,8 @@ ngx_module_t  ngx_http_ssi_filter_module
 };
 
 
-static ngx_int_t (*ngx_http_next_header_filter) (ngx_http_request_t *r);
-static ngx_int_t (*ngx_http_next_body_filter) (ngx_http_request_t *r,
-    ngx_chain_t *in);
+static ngx_http_output_header_filter_pt  ngx_http_next_header_filter;
+static ngx_http_output_body_filter_pt    ngx_http_next_body_filter;
 
 
 static u_char ngx_http_ssi_string[] = "<!--";
@@ -407,6 +406,7 @@ ngx_http_ssi_body_filter(ngx_http_reques
     ngx_buf_t               *b;
     ngx_chain_t             *cl;
     ngx_table_elt_t         *param;
+    ngx_connection_t        *c;
     ngx_http_ssi_ctx_t      *ctx;
     ngx_http_ssi_conf_t     *conf;
     ngx_http_ssi_param_t    *prm;
@@ -650,7 +650,15 @@ ngx_http_ssi_body_filter(ngx_http_reques
                     }
                 }
 
-                if (cmd->handler(r, ctx, params) == NGX_OK) {
+                c = r->connection;
+
+                rc = cmd->handler(r, ctx, params);
+
+                if (c->closed) {
+                    return NGX_DONE;
+                }
+
+                if (rc == NGX_OK) {
                     continue;
                 }
             }
--- a/src/http/ngx_http_copy_filter_module.c
+++ b/src/http/ngx_http_copy_filter_module.c
@@ -70,21 +70,12 @@ static ngx_http_output_body_filter_pt   
 static ngx_int_t
 ngx_http_copy_filter(ngx_http_request_t *r, ngx_chain_t *in)
 {
-    ngx_int_t                     rc;
     ngx_output_chain_ctx_t       *ctx;
     ngx_http_copy_filter_conf_t  *conf;
 
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                    "copy filter: \"%V\"", &r->uri);
 
-    if (r->connection->closed) {
-        rc = ngx_http_next_filter(r, in);
-
-        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                       "copy closed filter: %i \"%V\"", rc, &r->uri);
-        return rc;
-    }
-
     ctx = ngx_http_get_module_ctx(r, ngx_http_copy_filter_module);
 
     if (ctx == NULL) {
@@ -111,12 +102,9 @@ ngx_http_copy_filter(ngx_http_request_t 
 
     }
 
-    rc = ngx_output_chain(ctx, in);
+    /* the request pool may be already destroyed after ngx_output_chain()*/
 
-    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                   "copy filter: %i \"%V\"", rc, &r->uri);
-
-    return rc;
+    return ngx_output_chain(ctx, in);
 }
 
 
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -1172,8 +1172,7 @@ ngx_http_subrequest(ngx_http_request_t *
 
     ngx_http_handler(sr);
 
-    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                   "http subrequest \"%V\" done", uri);
+    /* the request pool may be already destroyed */
 
     return NGX_OK;
 }
@@ -1231,28 +1230,39 @@ ngx_http_internal_redirect(ngx_http_requ
 }
 
 
-#if 0       /* STUB: test the delay http handler */
-
-ngx_int_t
-ngx_http_delay_handler(ngx_http_request_t *r)
+ngx_http_cleanup_t *
+ngx_http_cleanup_add(ngx_http_request_t *r, size_t size)
 {
-    static int  on;
-
-    if (on++ == 0) {
-        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                       "http set delay");
-        ngx_add_timer(r->connection->write, 10000);
-        return NGX_AGAIN;
+    ngx_http_cleanup_t  *cln;
+
+    r = r->main;
+
+    cln = ngx_palloc(r->pool, sizeof(ngx_http_cleanup_t));
+    if (cln == NULL) {
+        return NULL;
     }
 
-    r->connection->write->timedout = 0;
-    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                   "http reset delay");
-    return NGX_DECLINED;
+    if (size) {
+        cln->data = ngx_palloc(r->pool, size);
+        if (cln->data == NULL) {
+            return NULL;
+        }
+
+    } else {
+        cln->data = NULL;
+    }
+
+    cln->handler = NULL;
+    cln->next = r->cleanup;
+
+    r->cleanup = cln;
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http cleanup add: %p", cln);
+
+    return cln;
 }
 
-#endif
-
 
 static char *
 ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
@@ -1665,7 +1675,8 @@ ngx_http_core_create_main_conf(ngx_conf_
     }
 
     if (ngx_array_init(&cmcf->servers, cf->pool, 4,
-                       sizeof(ngx_http_core_srv_conf_t *)) == NGX_ERROR)
+                       sizeof(ngx_http_core_srv_conf_t *))
+        == NGX_ERROR)
     {
         return NGX_CONF_ERROR;
     }
@@ -1767,6 +1778,10 @@ ngx_http_core_merge_srv_conf(ngx_conf_t 
         ls->port = (getuid() == 0) ? 80 : 8000;
 #endif
         ls->family = AF_INET;
+
+        ls->conf.backlog = -1;
+        ls->conf.rcvbuf = -1;
+        ls->conf.sndbuf = -1;
     }
 
     if (conf->server_names.nelts == 0) {
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -65,7 +65,7 @@ typedef struct {
     ngx_array_t                servers;         /* ngx_http_core_srv_conf_t */
 
     ngx_http_phase_t           phases[NGX_HTTP_LAST_PHASE];
-    ngx_array_t                index_handlers;
+    ngx_http_handler_pt        log_handler;
 
     ngx_hash_t                 headers_in_hash;
     ngx_hash_t                 variables_hash;
@@ -264,6 +264,8 @@ ngx_int_t ngx_http_subrequest(ngx_http_r
 ngx_int_t ngx_http_internal_redirect(ngx_http_request_t *r,
     ngx_str_t *uri, ngx_str_t *args);
 
+ngx_http_cleanup_t *ngx_http_cleanup_add(ngx_http_request_t *r, size_t size);
+
 
 typedef ngx_int_t (*ngx_http_output_header_filter_pt)(ngx_http_request_t *r);
 typedef ngx_int_t (*ngx_http_output_body_filter_pt)
--- a/src/http/ngx_http_header_filter_module.c
+++ b/src/http/ngx_http_header_filter_module.c
@@ -122,7 +122,7 @@ static ngx_str_t ngx_http_status_lines[]
 };
 
 
-ngx_http_header0_t  ngx_http_headers_out[] = {
+ngx_http_header_out_t  ngx_http_headers_out[] = {
     { ngx_string("Server"), offsetof(ngx_http_headers_out_t, server) },
     { ngx_string("Date"), offsetof(ngx_http_headers_out_t, date) },
 #if 0
deleted file mode 100644
--- a/src/http/ngx_http_log_module.c
+++ /dev/null
@@ -1,1188 +0,0 @@
-
-/*
- * Copyright (C) Igor Sysoev
- */
-
-
-#include <ngx_config.h>
-#include <ngx_core.h>
-#include <ngx_http.h>
-#include <nginx.h>
-
-
-static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-
-static size_t ngx_http_log_request_getlen(ngx_http_request_t *r,
-    uintptr_t data);
-static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-
-static ngx_int_t ngx_http_log_header_in_compile(ngx_conf_t *cf,
-    ngx_http_log_op_t *op, ngx_str_t *value);
-static size_t ngx_http_log_header_in_getlen(ngx_http_request_t *r,
-    uintptr_t data);
-static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static size_t ngx_http_log_unknown_header_in_getlen(ngx_http_request_t *r,
-    uintptr_t data);
-static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
-    u_char *buf, ngx_http_log_op_t *op);
-
-static ngx_int_t ngx_http_log_header_out_compile(ngx_conf_t *cf,
-    ngx_http_log_op_t *op, ngx_str_t *value);
-static size_t ngx_http_log_header_out_getlen(ngx_http_request_t *r,
-    uintptr_t data);
-static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-static size_t ngx_http_log_unknown_header_out_getlen(ngx_http_request_t *r,
-    uintptr_t data);
-static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r,
-    u_char *buf, ngx_http_log_op_t *op);
-
-static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
-    u_char *buf, ngx_http_log_op_t *op);
-static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
-    u_char *buf, ngx_http_log_op_t *op);
-
-static ngx_table_elt_t *ngx_http_log_unknown_header(ngx_list_t *headers,
-    ngx_str_t *value);
-
-static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf,
-    ngx_http_log_op_t *op, ngx_str_t *value);
-static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
-    uintptr_t data);
-static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op);
-
-
-static ngx_int_t ngx_http_log_set_formats(ngx_conf_t *cf);
-static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
-static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
-static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
-    void *child);
-static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
-    void *conf);
-static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
-    void *conf);
-
-
-static ngx_command_t  ngx_http_log_commands[] = {
-
-    { ngx_string("log_format"),
-      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
-      ngx_http_log_set_format,
-      NGX_HTTP_MAIN_CONF_OFFSET,
-      0,
-      NULL },
-
-    { ngx_string("access_log"),
-      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
-      ngx_http_log_set_log,
-      NGX_HTTP_LOC_CONF_OFFSET,
-      0,
-      NULL },
-
-      ngx_null_command
-};
-
-
-ngx_http_module_t  ngx_http_log_module_ctx = {
-    ngx_http_log_set_formats,              /* preconfiguration */
-    NULL,                                  /* postconfiguration */
-
-    ngx_http_log_create_main_conf,         /* create main configuration */
-    NULL,                                  /* init main configuration */
-
-    NULL,                                  /* create server configuration */
-    NULL,                                  /* merge server configuration */
-
-    ngx_http_log_create_loc_conf,          /* create location configration */
-    ngx_http_log_merge_loc_conf            /* merge location configration */
-};
-
-
-ngx_module_t  ngx_http_log_module = {
-    NGX_MODULE_V1,
-    &ngx_http_log_module_ctx,              /* module context */
-    ngx_http_log_commands,                 /* module directives */
-    NGX_HTTP_MODULE,                       /* module type */
-    NULL,                                  /* init master */
-    NULL,                                  /* init module */
-    NULL,                                  /* init process */
-    NULL,                                  /* init thread */
-    NULL,                                  /* exit thread */
-    NULL,                                  /* exit process */
-    NULL,                                  /* exit master */
-    NGX_MODULE_V1_PADDING
-};
-
-
-static ngx_str_t http_access_log = ngx_string(NGX_HTTP_LOG_PATH);
-
-
-static ngx_str_t ngx_http_combined_fmt =
-    ngx_string("%addr - - [%time] \"%request\" %status %apache_length "
-               "\"%{Referer}i\" \"%{User-Agent}i\"");
-
-
-ngx_http_log_op_name_t ngx_http_log_fmt_ops[] = {
-    { ngx_string("addr"), INET_ADDRSTRLEN - 1, NULL, NULL, ngx_http_log_addr },
-    { ngx_string("conn"), NGX_ATOMIC_T_LEN, NULL, NULL,
-                          ngx_http_log_connection },
-    { ngx_string("pipe"), 1, NULL, NULL, ngx_http_log_pipe },
-    { ngx_string("time"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
-                          NULL, NULL, ngx_http_log_time },
-    { ngx_string("msec"), NGX_TIME_T_LEN + 4, NULL, NULL, ngx_http_log_msec },
-    { ngx_string("request_time"), NGX_TIME_T_LEN, NULL, NULL,
-                          ngx_http_log_request_time },
-    { ngx_string("status"), 3, NULL, NULL, ngx_http_log_status },
-    { ngx_string("length"), NGX_OFF_T_LEN, NULL, NULL, ngx_http_log_length },
-    { ngx_string("apache_length"), NGX_OFF_T_LEN,
-                          NULL, NULL, ngx_http_log_apache_length },
-    { ngx_string("request_length"), NGX_SIZE_T_LEN,
-                          NULL, NULL, ngx_http_log_request_length },
-
-    { ngx_string("request"), 0, NULL,
-                          ngx_http_log_request_getlen,
-                          ngx_http_log_request },
-
-    { ngx_string("i"), 0, ngx_http_log_header_in_compile, NULL,
-                          ngx_http_log_header_in },
-    { ngx_string("o"), 0, ngx_http_log_header_out_compile, NULL,
-                          ngx_http_log_header_out },
-    { ngx_string("v"), 0, ngx_http_log_variable_compile, NULL,
-                          ngx_http_log_variable },
-
-    { ngx_null_string, 0, NULL, NULL, NULL }
-};
-
-
-ngx_int_t
-ngx_http_log_handler(ngx_http_request_t *r)
-{
-    ngx_uint_t                i, l;
-    u_char                   *line, *p;
-    size_t                    len;
-    ngx_http_log_t           *log;
-    ngx_http_log_op_t        *op;
-    ngx_http_log_loc_conf_t  *lcf;
-#if (NGX_WIN32)
-    u_long                    written;
-#endif
-
-    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                   "http log handler");
-
-    lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
-
-    if (lcf->off) {
-        return NGX_OK;
-    }
-
-    log = lcf->logs->elts;
-    for (l = 0; l < lcf->logs->nelts; l++) {
-
-        len = 0;
-        op = log[l].ops->elts;
-        for (i = 0; i < log[l].ops->nelts; i++) {
-            if (op[i].len == 0) {
-                len += op[i].getlen(r, op[i].data);
-
-            } else {
-                len += op[i].len;
-            }
-        }
-
-#if (NGX_WIN32)
-        len += 2;
-#else
-        len++;
-#endif
-
-        line = ngx_palloc(r->pool, len);
-        if (line == NULL) {
-            return NGX_ERROR;
-        }
-
-        p = line;
-
-        for (i = 0; i < log[l].ops->nelts; i++) {
-            p = op[i].run(r, p, &op[i]);
-        }
-
-#if (NGX_WIN32)
-        *p++ = CR; *p++ = LF;
-        WriteFile(log[l].file->fd, line, p - line, &written, NULL);
-#else
-        *p++ = LF;
-        write(log[l].file->fd, line, p - line);
-#endif
-    }
-
-    return NGX_OK;
-}
-
-
-static u_char *
-ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    size_t     len;
-    uintptr_t  data;
-
-    len = op->len;
-    data = op->data;
-
-    while (len--) {
-        *buf++ = (u_char) (data & 0xff);
-        data >>= 8;
-    }
-
-    return buf;
-}
-
-
-static u_char *
-ngx_http_log_copy_long(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    return ngx_cpymem(buf, (u_char *) op->data, op->len);
-}
-
-
-static u_char *
-ngx_http_log_addr(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
-{
-    return ngx_cpymem(buf, r->connection->addr_text.data,
-                      r->connection->addr_text.len);
-}
-
-
-static u_char *
-ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    return ngx_sprintf(buf, "%ui", r->connection->number);
-}
-
-
-static u_char *
-ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
-{
-    if (r->pipeline) {
-        *buf = 'p';
-    } else {
-        *buf = '.';
-    }
-
-    return buf + 1;
-}
-
-
-static u_char *
-ngx_http_log_time(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
-{
-    return ngx_cpymem(buf, ngx_cached_http_log_time.data,
-                      ngx_cached_http_log_time.len);
-}
-
-
-static u_char *
-ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
-{
-    ngx_time_t  *tp;
-
-    tp = ngx_timeofday();
-
-    return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
-}
-
-
-static u_char *
-ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    time_t  elapsed;
-
-    elapsed = ngx_time() - r->start_time;
-
-    return ngx_sprintf(buf, "%T", elapsed);
-}
-
-
-static size_t
-ngx_http_log_request_getlen(ngx_http_request_t *r, uintptr_t data)
-{
-    return r->request_line.len;
-}
-
-
-static u_char *
-ngx_http_log_request(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
-{
-    return ngx_cpymem(buf, r->request_line.data, r->request_line.len);
-}
-
-
-static u_char *
-ngx_http_log_status(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
-{
-    return ngx_sprintf(buf, "%ui",
-                       r->err_status ? r->err_status : r->headers_out.status);
-}
-
-
-static u_char *
-ngx_http_log_length(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
-{
-    return ngx_sprintf(buf, "%O", r->connection->sent);
-}
-
-
-static u_char *
-ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    off_t  length;
-
-    length = r->connection->sent - r->header_size;
-
-    if (length > 0) {
-        return ngx_sprintf(buf, "%O", length);
-    }
-
-    *buf = '0';
-
-    return buf + 1;
-}
-
-
-static u_char *
-ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    return ngx_sprintf(buf, "%z", r->request_length);
-}
-
-
-static ngx_int_t
-ngx_http_log_header_in_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
-    ngx_str_t *value)
-{
-    ngx_uint_t  i;
-
-    op->len = 0;
-
-    for (i = 0; ngx_http_headers_in[i].name.len != 0; i++) {
-
-        if (ngx_http_headers_in[i].name.len != value->len) {
-            continue;
-        }
-
-        /* STUB: "Cookie" speacial handling */
-        if (ngx_http_headers_in[i].offset == 0) {
-            continue;
-        }
-
-        if (ngx_strncasecmp(ngx_http_headers_in[i].name.data, value->data,
-                            value->len) == 0)
-        {
-            op->getlen = ngx_http_log_header_in_getlen;
-            op->run = ngx_http_log_header_in;
-            op->data = ngx_http_headers_in[i].offset;
-
-            return NGX_OK;
-        }
-    }
-
-    op->getlen = ngx_http_log_unknown_header_in_getlen;
-    op->run = ngx_http_log_unknown_header_in;
-    op->data = (uintptr_t) value;
-
-    return NGX_OK;
-}
-
-
-static size_t
-ngx_http_log_header_in_getlen(ngx_http_request_t *r, uintptr_t data)
-{
-    ngx_table_elt_t  *h;
-
-    h = *(ngx_table_elt_t **) ((char *) &r->headers_in + data);
-
-    if (h) {
-        return h->value.len;
-    }
-
-    return 1;
-}
-
-
-static u_char *
-ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    ngx_table_elt_t  *h;
-
-    h = *(ngx_table_elt_t **) ((char *) &r->headers_in + op->data);
-
-    if (h) {
-        return ngx_cpymem(buf, h->value.data, h->value.len);
-    }
-
-    *buf = '-';
-
-    return buf + 1;
-}
-
-
-static size_t
-ngx_http_log_unknown_header_in_getlen(ngx_http_request_t *r, uintptr_t data)
-{
-    ngx_table_elt_t  *h;
-
-    h = ngx_http_log_unknown_header(&r->headers_in.headers, (ngx_str_t *) data);
-
-    if (h) {
-        return h->value.len;
-    }
-
-    return 1;
-}
-
-
-static u_char *
-ngx_http_log_unknown_header_in(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    ngx_table_elt_t  *h;
-
-    h = ngx_http_log_unknown_header(&r->headers_in.headers,
-                                    (ngx_str_t *) op->data);
-
-    if (h) {
-        return ngx_cpymem(buf, h->value.data, h->value.len);
-    }
-
-    *buf = '-';
-
-    return buf + 1;
-}
-
-
-static ngx_int_t
-ngx_http_log_header_out_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
-    ngx_str_t *value)
-{
-    ngx_uint_t  i;
-
-    op->len = 0;
-
-    for (i = 0; ngx_http_headers_out[i].name.len != 0; i++) {
-
-        if (ngx_http_headers_out[i].name.len != value->len) {
-            continue;
-        }
-
-        if (ngx_strncasecmp(ngx_http_headers_out[i].name.data, value->data,
-                            value->len) == 0)
-        {
-            op->getlen = ngx_http_log_header_out_getlen;
-            op->run = ngx_http_log_header_out;
-            op->data = ngx_http_headers_out[i].offset;
-
-            return NGX_OK;
-        }
-    }
-
-    if (value->len == sizeof("Connection") - 1
-        && ngx_strncasecmp(value->data, "Connection", value->len) == 0)
-    {
-        op->len = sizeof("keep-alive") - 1;
-        op->getlen = NULL;
-        op->run = ngx_http_log_connection_header_out;
-        op->data = 0;
-        return NGX_OK;
-    }
-
-    if (value->len == sizeof("Transfer-Encoding") - 1
-        && ngx_strncasecmp(value->data, "Transfer-Encoding", value->len) == 0)
-    {
-        op->len = sizeof("chunked") - 1;
-        op->getlen = NULL;
-        op->run = ngx_http_log_transfer_encoding_header_out;
-        op->data = 0;
-        return NGX_OK;
-    }
-
-    op->getlen = ngx_http_log_unknown_header_out_getlen;
-    op->run = ngx_http_log_unknown_header_out;
-    op->data = (uintptr_t) value;
-
-    return NGX_OK;
-}
-
-
-static size_t
-ngx_http_log_header_out_getlen(ngx_http_request_t *r, uintptr_t data)
-{
-    ngx_table_elt_t  *h;
-
-    h = *(ngx_table_elt_t **) ((char *) &r->headers_out + data);
-
-    if (h) {
-        return h->value.len;
-    }
-
-    /*
-     * No header pointer was found.
-     * However, some headers: "Date", "Server", "Content-Length",
-     * and "Last-Modified" have a special handling in the header filter,
-     * but we do not set up their pointers in the filter,
-     * because they are too seldom needed to be logged.
-     */
-
-    if (data == offsetof(ngx_http_headers_out_t, date)) {
-        return ngx_cached_http_time.len;
-    }
-
-    if (data == offsetof(ngx_http_headers_out_t, server)) {
-        return (sizeof(NGINX_VER) - 1);
-    }
-
-    if (data == offsetof(ngx_http_headers_out_t, content_length)) {
-        if (r->headers_out.content_length_n == -1) {
-            return 1;
-        }
-
-        return NGX_OFF_T_LEN;
-    }
-
-    if (data == offsetof(ngx_http_headers_out_t, last_modified)) {
-        if (r->headers_out.last_modified_time == -1) {
-            return 1;
-        }
-
-        return sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
-    }
-
-    return 1;
-}
-
-
-static u_char *
-ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    ngx_table_elt_t  *h;
-
-    h = *(ngx_table_elt_t **) ((char *) &r->headers_out + op->data);
-
-    if (h) {
-        return ngx_cpymem(buf, h->value.data, h->value.len);
-    }
-
-    /*
-     * No header pointer was found.
-     * However, some headers: "Date", "Server", "Content-Length",
-     * and "Last-Modified" have a special handling in the header filter,
-     * but we do not set up their pointers in the filter,
-     * because they are too seldom needed to be logged.
-     */
-
-    if (op->data == offsetof(ngx_http_headers_out_t, date)) {
-        return ngx_cpymem(buf, ngx_cached_http_time.data,
-                          ngx_cached_http_time.len);
-    }
-
-    if (op->data == offsetof(ngx_http_headers_out_t, server)) {
-        return ngx_cpymem(buf, NGINX_VER, sizeof(NGINX_VER) - 1);
-    }
-
-    if (op->data == offsetof(ngx_http_headers_out_t, content_length)) {
-        if (r->headers_out.content_length_n == -1) {
-            *buf = '-';
-
-            return buf + 1;
-        }
-
-        return ngx_sprintf(buf, "%O", r->headers_out.content_length_n);
-    }
-
-    if (op->data == offsetof(ngx_http_headers_out_t, last_modified)) {
-        if (r->headers_out.last_modified_time == -1) {
-            *buf = '-';
-
-            return buf + 1;
-        }
-
-        return ngx_http_time(buf, r->headers_out.last_modified_time);
-    }
-
-    *buf = '-';
-
-    return buf + 1;
-}
-
-
-static size_t
-ngx_http_log_unknown_header_out_getlen(ngx_http_request_t *r, uintptr_t data)
-{
-    ngx_table_elt_t  *h;
-
-    h = ngx_http_log_unknown_header(&r->headers_out.headers,
-                                    (ngx_str_t *) data);
-
-    if (h) {
-        return h->value.len;
-    }
-
-    return 1;
-}
-
-
-static u_char *
-ngx_http_log_unknown_header_out(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    ngx_table_elt_t  *h;
-
-    h = ngx_http_log_unknown_header(&r->headers_out.headers,
-                                    (ngx_str_t *) op->data);
-
-    if (h) {
-        return ngx_cpymem(buf, h->value.data, h->value.len);
-    }
-
-    *buf = '-';
-
-    return buf + 1;
-}
-
-
-static ngx_table_elt_t *
-ngx_http_log_unknown_header(ngx_list_t *headers, ngx_str_t *value)
-{
-    ngx_uint_t        i;
-    ngx_list_part_t  *part;
-    ngx_table_elt_t  *h;
-
-    part = &headers->part;
-    h = part->elts;
-
-    for (i = 0; /* void */; i++) {
-
-        if (i >= part->nelts) {
-            if (part->next == NULL) {
-                break;
-            }
-
-            part = part->next;
-            h = part->elts;
-            i = 0;
-        }
-
-        if (h[i].key.len != value->len) {
-            continue;
-        }
-
-        if (ngx_strncasecmp(h[i].key.data, value->data, value->len) == 0) {
-            return &h[i];
-        }
-    }
-
-    return NULL;
-}
-
-
-static u_char *
-ngx_http_log_connection_header_out(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    if (r->keepalive) {
-        return ngx_cpymem(buf, "keep-alive", sizeof("keep-alive") - 1);
-
-    } else {
-        return ngx_cpymem(buf, "close", sizeof("close") - 1);
-    }
-}
-
-
-static u_char *
-ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r, u_char *buf,
-    ngx_http_log_op_t *op)
-{
-    if (r->chunked) {
-        return ngx_cpymem(buf, "chunked", sizeof("chunked") - 1);
-    }
-
-    *buf = '-';
-
-    return buf + 1;
-}
-
-
-static ngx_int_t 
-ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
-    ngx_str_t *value)
-{
-    ngx_int_t  index;
-
-    index = ngx_http_get_variable_index(cf, value);
-    if (index == NGX_ERROR) {
-        return NGX_ERROR;
-    }
-
-    op->len = 0;
-    op->getlen = ngx_http_log_variable_getlen;
-    op->run = ngx_http_log_variable;
-    op->data = index;
-
-    return NGX_OK;
-}
-
-
-static size_t
-ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data)
-{
-    ngx_http_variable_value_t  *value;
-
-    value = ngx_http_get_indexed_variable(r, data);
-
-    if (value == NULL
-        || value == NGX_HTTP_VAR_NOT_FOUND
-        || value->text.len == 0)
-    {
-        return 1;
-    }
-
-    return value->text.len;
-}
-
-
-static u_char *
-ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
-{
-    ngx_http_variable_value_t  *value;
-
-    value = ngx_http_get_indexed_variable(r, op->data);
-
-    if (value == NULL
-        || value == NGX_HTTP_VAR_NOT_FOUND
-        || value->text.len == 0)
-    {
-        *buf = '-';
-        return buf + 1;
-    }
-
-    return ngx_cpymem(buf, value->text.data, value->text.len);
-}
-
-
-static ngx_int_t
-ngx_http_log_set_formats(ngx_conf_t *cf)
-{
-    ngx_http_log_op_name_t  *op;
-
-    for (op = ngx_http_log_fmt_ops; op->name.len; op++) { /* void */ }
-    op->run = NULL;
-
-    return NGX_OK;
-}
-
-
-static void *
-ngx_http_log_create_main_conf(ngx_conf_t *cf)
-{
-    ngx_http_log_main_conf_t  *conf;
-
-    char       *rc;
-    ngx_str_t  *value;
-
-    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t));
-    if (conf == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    if (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))
-                                                                  == NGX_ERROR)
-    {
-        return NGX_CONF_ERROR;
-    }
-
-    cf->args->nelts = 0;
-
-    value = ngx_array_push(cf->args);
-    if (value == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    value = ngx_array_push(cf->args);
-    if (value == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    value->len = sizeof("combined") - 1;
-    value->data = (u_char *) "combined";
-
-    value = ngx_array_push(cf->args);
-    if (value == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    *value = ngx_http_combined_fmt;
-
-    rc = ngx_http_log_set_format(cf, NULL, conf);
-    if (rc != NGX_CONF_OK) {
-        return NULL;
-    }
-
-    return conf;
-}
-
-
-static void *
-ngx_http_log_create_loc_conf(ngx_conf_t *cf)
-{
-    ngx_http_log_loc_conf_t  *conf;
-
-    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t));
-    if (conf == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    return conf;
-}
-
-
-static char *
-ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
-{
-    ngx_http_log_loc_conf_t *prev = parent;
-    ngx_http_log_loc_conf_t *conf = child;
-
-    ngx_http_log_t            *log;
-    ngx_http_log_fmt_t        *fmt;
-    ngx_http_log_main_conf_t  *lmcf;
-
-    if (conf->logs == NULL) {
-
-        if (conf->off) {
-            return NGX_CONF_OK;
-        }
-
-        if (prev->logs) {
-            conf->logs = prev->logs;
-
-        } else {
-
-            if (prev->off) {
-                conf->off = prev->off;
-                return NGX_CONF_OK;
-            }
-
-            conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
-            if (conf->logs == NULL) {
-                return NGX_CONF_ERROR;
-            }
-
-            log = ngx_array_push(conf->logs);
-            if (log == NULL) {
-                return NGX_CONF_ERROR;
-            }
-
-            log->file = ngx_conf_open_file(cf->cycle, &http_access_log);
-            if (log->file == NULL) {
-                return NGX_CONF_ERROR;
-            }
-
-            lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
-            fmt = lmcf->formats.elts;
-
-            /* the default "combined" format */
-            log->ops = fmt[0].ops;
-        }
-    }
-
-    return NGX_CONF_OK;
-}
-
-
-static char *
-ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
-    ngx_http_log_loc_conf_t *llcf = conf;
-
-    ngx_uint_t                 i;
-    ngx_str_t                 *value, name;
-    ngx_http_log_t            *log;
-    ngx_http_log_fmt_t        *fmt;
-    ngx_http_log_main_conf_t  *lmcf;
-
-    value = cf->args->elts;
-
-    if (ngx_strcmp(value[1].data, "off") == 0) {
-        llcf->off = 1;
-        return NGX_CONF_OK;
-    }
-
-    if (llcf->logs == NULL) {
-        llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
-        if (llcf->logs == NULL) {
-            return NGX_CONF_ERROR;
-        }
-    }
-
-    lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
-
-    log = ngx_array_push(llcf->logs);
-    if (log == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    log->file = ngx_conf_open_file(cf->cycle, &value[1]);
-    if (log->file == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    if (cf->args->nelts == 3) {
-        name = value[2];
-    } else {
-        name.len = sizeof("combined") - 1;
-        name.data = (u_char *) "combined";
-    }
-
-    fmt = lmcf->formats.elts;
-    for (i = 0; i < lmcf->formats.nelts; i++) {
-        if (fmt[i].name.len == name.len
-            && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
-        {
-            log->ops = fmt[i].ops;
-            return NGX_CONF_OK;
-        }
-    }
-
-    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                       "unknown log format \"%V\"", &name);
-
-    return NGX_CONF_ERROR;
-}
-
-
-static char *
-ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
-    ngx_http_log_main_conf_t *lmcf = conf;
-
-    ngx_uint_t                  s, f, invalid;
-    u_char                     *data, *p, *fname;
-    size_t                      i, len, fname_len;
-    ngx_str_t                  *value, arg, *a;
-    ngx_http_log_op_t          *op;
-    ngx_http_log_fmt_t         *fmt;
-    ngx_http_log_op_name_t     *name;
-
-    value = cf->args->elts;
-
-    fmt = lmcf->formats.elts;
-    for (f = 0; f < lmcf->formats.nelts; f++) {
-        if (fmt[f].name.len == value[1].len
-            && ngx_strcmp(fmt->name.data, value[1].data) == 0)
-        {
-            return "duplicate \"log_format\" name";
-        }
-    }
-
-    fmt = ngx_array_push(&lmcf->formats);
-    if (fmt == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    fmt->name = value[1];
-
-    fmt->ops = ngx_array_create(cf->pool, 20, sizeof(ngx_http_log_op_t));
-    if (fmt->ops == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    invalid = 0;
-    data = NULL;
-    arg.data = NULL;
-
-    for (s = 2; s < cf->args->nelts && !invalid; s++) {
-
-        i = 0;
-
-        while (i < value[s].len) {
-
-            op = ngx_array_push(fmt->ops);
-            if (op == NULL) {
-                return NGX_CONF_ERROR;
-            }
-
-            data = &value[s].data[i];
-
-            if (value[s].data[i] == '%') {
-                i++;
-
-                if (i == value[s].len) {
-                    invalid = 1;
-                    break;
-                }
-
-                if (value[s].data[i] == '{') {
-                    i++;
-
-                    arg.data = &value[s].data[i];
-
-                    while (i < value[s].len && value[s].data[i] != '}') {
-                        i++;
-                    }
-
-                    arg.len = &value[s].data[i] - arg.data;
-
-                    if (i == value[s].len || arg.len == 0) {
-                        invalid = 1;
-                        break;
-                    }
-
-                    i++;
-
-                } else {
-                    arg.len = 0;
-                }
-
-                fname = &value[s].data[i];
-
-                while (i < value[s].len
-                       && ((value[s].data[i] >= 'a' && value[s].data[i] <= 'z')
-                           || value[s].data[i] == '_'))
-                {
-                    i++;
-                }
-
-                fname_len = &value[s].data[i] - fname;
-
-                if (fname_len == 0) {
-                    invalid = 1;
-                    break;
-                }
-
-                for (name = ngx_http_log_fmt_ops; name->run; name++) {
-                    if (name->name.len == 0) {
-                        name = (ngx_http_log_op_name_t *) name->run;
-                    }
-
-                    if (name->name.len == fname_len
-                        && ngx_strncmp(name->name.data, fname, fname_len) == 0)
-                    {
-                        if (name->compile == NULL) {
-                            if (arg.len) {
-                                fname[fname_len] = '\0';
-                                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                                               "\"%s\" must not have argument",
-                                               data);
-                                return NGX_CONF_ERROR;
-                            }
-
-                            op->len = name->len;
-                            op->getlen = name->getlen;
-                            op->run = name->run;
-                            op->data = 0;
-
-                            break;
-                        }
-
-                        if (arg.len == 0) {
-                            fname[fname_len] = '\0';
-                            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                                               "\"%s\" requires argument", 
-                                               data);
-                            return NGX_CONF_ERROR;
-                        }
-
-                        a = ngx_palloc(cf->pool, sizeof(ngx_str_t));
-                        if (a == NULL) {
-                            return NGX_CONF_ERROR;
-                        }
-
-                        *a = arg;
-                        if (name->compile(cf, op, a) == NGX_ERROR) {
-                            return NGX_CONF_ERROR;
-                        }
-
-                        break;
-                    }
-                }
-
-                if (name->name.len == 0) {
-                    invalid = 1;
-                    break;
-                }
-
-            } else {
-                i++;
-
-                while (i < value[s].len && value[s].data[i] != '%') {
-                    i++;
-                }
-
-                len = &value[s].data[i] - data;
-
-                if (len) {
-
-                    op->len = len;
-                    op->getlen = NULL;
-
-                    if (len <= sizeof(uintptr_t)) {
-                        op->run = ngx_http_log_copy_short;
-                        op->data = 0;
-
-                        while (len--) {
-                            op->data <<= 8;
-                            op->data |= data[len];
-                        }
-
-                    } else {
-                        op->run = ngx_http_log_copy_long;
-
-                        p = ngx_palloc(cf->pool, len);
-                        if (p == NULL) {
-                            return NGX_CONF_ERROR;
-                        }
-
-                        ngx_memcpy(p, data, len);
-                        op->data = (uintptr_t) p;
-                    }
-                }
-            }
-        }
-    }
-
-    if (invalid) {
-        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                           "invalid parameter \"%s\"", data);
-        return NGX_CONF_ERROR;
-    }
-
-    return NGX_CONF_OK;
-}
--- a/src/http/ngx_http_postpone_filter_module.c
+++ b/src/http/ngx_http_postpone_filter_module.c
@@ -56,15 +56,6 @@ ngx_http_postpone_filter(ngx_http_reques
     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                    "http postpone filter \"%V\" %p", &r->uri, in);
 
-    if (r->connection->closed) {
-
-        if (r->postponed) {
-            r->postponed = r->postponed->next;
-        }
-
-        return NGX_ERROR;
-    }
-
     if (r != r->connection->data || (r->postponed && in)) {
 
         if (r->postponed) {
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -41,6 +41,7 @@ static void ngx_http_keepalive_handler(n
 static void ngx_http_set_lingering_close(ngx_http_request_t *r);
 static void ngx_http_lingering_close_handler(ngx_event_t *ev);
 static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
+static void ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error);
 static void ngx_http_close_connection(ngx_connection_t *c);
 
 static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
@@ -156,8 +157,7 @@ ngx_http_init_connection(ngx_connection_
 
     rev = c->read;
     rev->handler = ngx_http_init_request;
-
-    /* STUB: epoll edge */ c->write->handler = ngx_http_empty_handler;
+    c->write->handler = ngx_http_empty_handler;
 
 #if (NGX_STAT_STUB)
     ngx_atomic_fetch_add(ngx_stat_reading, 1);
@@ -374,7 +374,6 @@ void ngx_http_init_request(ngx_event_t *
                       sizeof(ngx_table_elt_t)) == NGX_ERROR)
     {
         ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
-        ngx_http_close_connection(c);
         return;
     }
 
@@ -382,7 +381,6 @@ void ngx_http_init_request(ngx_event_t *
     r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
     if (r->ctx == NULL) {
         ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
-        ngx_http_close_connection(c);
         return;
     }
 
@@ -435,7 +433,6 @@ ngx_http_ssl_handshake(ngx_event_t *rev)
         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
         c->timedout = 1;
         ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
-        ngx_http_close_connection(c);
         return;
     }
 
@@ -502,7 +499,6 @@ ngx_http_ssl_handshake_handler(ngx_conne
     r = c->data;
 
     ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
-    ngx_http_close_connection(r->connection);
 
     return;
 }
@@ -530,7 +526,6 @@ ngx_http_process_request_line(ngx_event_
         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
         c->timedout = 1;
         ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
-        ngx_http_close_connection(c);
         return;
     }
 
@@ -568,7 +563,6 @@ ngx_http_process_request_line(ngx_event_
                 r->uri.data = ngx_palloc(r->pool, r->uri.len + 1);
                 if (r->uri.data == NULL) {
                     ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
-                    ngx_http_close_connection(c);
                     return;
                 }
 
@@ -655,7 +649,6 @@ ngx_http_process_request_line(ngx_event_
                                          sizeof(ngx_table_elt_t)) == NGX_ERROR)
             {
                 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
-                ngx_http_close_connection(c);
                 return;
             }
 
@@ -664,7 +657,6 @@ ngx_http_process_request_line(ngx_event_
                                        sizeof(ngx_table_elt_t *)) == NGX_ERROR)
             {
                 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
-                ngx_http_close_connection(c);
                 return;
             }
 
@@ -694,7 +686,6 @@ ngx_http_process_request_line(ngx_event_
 
             if (rv == NGX_ERROR) {
                 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
-                ngx_http_close_connection(c);
                 return;
             }
 
@@ -739,7 +730,6 @@ ngx_http_process_request_headers(ngx_eve
         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
         c->timedout = 1;
         ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
-        ngx_http_close_connection(c);
         return;
     }
 
@@ -759,7 +749,6 @@ ngx_http_process_request_headers(ngx_eve
 
                 if (rv == NGX_ERROR) {
                     ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
-                    ngx_http_close_connection(c);
                     return;
                 }
 
@@ -778,7 +767,6 @@ ngx_http_process_request_headers(ngx_eve
                                   "client sent too long header line: \"%V\"",
                                   &header);
                     ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
-                    ngx_http_close_connection(c);
                     return;
                 }
             }
@@ -812,7 +800,6 @@ ngx_http_process_request_headers(ngx_eve
             h = ngx_list_push(&r->headers_in.headers);
             if (h == NULL) {
                 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
-                ngx_http_close_connection(c);
                 return;
             }
 
@@ -895,7 +882,6 @@ ngx_http_process_request_headers(ngx_eve
                       "client sent invalid header line: \"%V\\r...\"",
                       &header);
         ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
-        ngx_http_close_connection(c);
         return;
     }
 }
@@ -932,7 +918,6 @@ ngx_http_read_request_header(ngx_http_re
 
         if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
             ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
-            ngx_http_close_connection(r->connection);
             return NGX_ERROR;
         }
 
@@ -946,7 +931,6 @@ ngx_http_read_request_header(ngx_http_re
 
     if (n == 0 || n == NGX_ERROR) {
         ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
-        ngx_http_close_connection(r->connection);
         return NGX_ERROR;
     }
 
@@ -1152,7 +1136,6 @@ ngx_http_process_cookie(ngx_http_request
     }
 
     ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
-    ngx_http_close_connection(r->connection);
 
     return NGX_ERROR;
 }
@@ -1190,7 +1173,6 @@ ngx_http_process_request_header(ngx_http
 
             if (cscf->restrict_host_names == NGX_HTTP_RESTRICT_HOST_CLOSE) {
                 ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
-                ngx_http_close_connection(r->connection);
                 return NGX_ERROR;
             }
 
@@ -1432,21 +1414,36 @@ ngx_http_finalize_request(ngx_http_reque
     ngx_http_core_loc_conf_t  *clcf;
 
     if (rc == NGX_DONE) {
-        /* r may be already destroyed when rc == NGX_DONE */
+        /* the request pool may be already destroyed */
         return;
     }
 
     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                    "http finalize request: %d, \"%V\"", rc, &r->uri);
 
-    if (r->parent
-        && (rc >= NGX_HTTP_SPECIAL_RESPONSE || rc == NGX_HTTP_NO_CONTENT))
-    {
-        ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
+    if (rc == NGX_ERROR || r->connection->closed) {
+        ngx_http_close_request(r, 0);
         return;
     }
 
-    if (r->parent || rc == NGX_AGAIN) {
+    if (rc >= NGX_HTTP_SPECIAL_RESPONSE || rc == NGX_HTTP_NO_CONTENT) {
+
+        if (r->main == r) {
+            if (r->connection->read->timer_set) {
+                ngx_del_timer(r->connection->read);
+            }
+
+            if (r->connection->write->timer_set) {
+                ngx_del_timer(r->connection->write);
+            }
+        }
+
+        ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
+
+        return;
+    }
+
+    if (r->main != r || rc == NGX_AGAIN) {
         if (ngx_http_set_write_handler(r) != NGX_OK) {
             return;
         }
@@ -1460,7 +1457,7 @@ ngx_http_finalize_request(ngx_http_reque
         return;
     }
 
-    if (r->parent) {
+    if (r->main != r) {
 
         pr = r->parent;
 
@@ -1499,34 +1496,11 @@ ngx_http_finalize_request(ngx_http_reque
         return;
     }
 
-    if (rc >= NGX_HTTP_SPECIAL_RESPONSE || rc == NGX_HTTP_NO_CONTENT) {
-
-        if (r->connection->read->timer_set) {
-            ngx_del_timer(r->connection->read);
-        }
-
-        if (r->connection->write->timer_set) {
-            ngx_del_timer(r->connection->write);
-        }
-
-        if (r->connection->closed) {
-            ngx_http_close_request(r, 0);
-            ngx_http_close_connection(r->connection);
-            return;
-        }
-
-        ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
-
+    if (rc == NGX_AGAIN) {
         return;
     }
 
-    if (rc == NGX_ERROR || r->connection->closed) {
-        ngx_http_close_request(r, 0);
-        ngx_http_close_connection(r->connection);
-        return;
-    }
-
-    if (rc == NGX_AGAIN || r->out) {
+    if (r->out) {
         (void) ngx_http_set_write_handler(r);
         return;
     }
@@ -1547,7 +1521,6 @@ ngx_http_finalize_request(ngx_http_reque
                        "kevent() reported about an closed connection");
 #endif
         ngx_http_close_request(r, 0);
-        ngx_http_close_connection(r->connection);
         return;
     }
 
@@ -1567,7 +1540,6 @@ ngx_http_finalize_request(ngx_http_reque
     }
 
     ngx_http_close_request(r, 0);
-    ngx_http_close_connection(r->connection);
 }
 
 
@@ -1581,10 +1553,6 @@ ngx_http_set_write_handler(ngx_http_requ
 
     r->write_event_handler = ngx_http_writer;
 
-    if (r->connection->closed) {
-        return NGX_OK;
-    }
-
     wev = r->connection->write;
 
     if (wev->ready && wev->delayed) {
@@ -1598,7 +1566,6 @@ ngx_http_set_write_handler(ngx_http_requ
 
     if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
         ngx_http_close_request(r, 0);
-        ngx_http_close_connection(r->connection);
         return NGX_ERROR;
     }
 
@@ -1626,14 +1593,7 @@ ngx_http_writer(ngx_http_request_t *r)
                           "client timed out");
             c->timedout = 1;
 
-#if 0
-            ngx_http_close_request(r->main, NGX_HTTP_REQUEST_TIME_OUT);
-            ngx_http_close_connection(c);
-#endif
-
-            c->closed = 1;
-            ngx_http_finalize_request(r, 0);
-
+            ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
             return;
         }
 
@@ -1645,8 +1605,7 @@ ngx_http_writer(ngx_http_request_t *r)
             ngx_add_timer(wev, clcf->send_timeout);
 
             if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
-                ngx_http_close_request(r->main, 0);
-                ngx_http_close_connection(r->connection);
+                ngx_http_close_request(r, 0);
             }
 
             return;
@@ -1660,8 +1619,7 @@ ngx_http_writer(ngx_http_request_t *r)
             clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
 
             if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
-                ngx_http_close_request(r->main, 0);
-                ngx_http_close_connection(r->connection);
+                ngx_http_close_request(r, 0);
             }
 
             return;
@@ -1672,7 +1630,7 @@ ngx_http_writer(ngx_http_request_t *r)
         rc = ngx_http_postponed_handler(r);
 
         if (rc == NGX_DONE) {
-            /* r may be already destroyed when rc == NGX_DONE */
+            /* the request pool may be already destroyed */
             return;
         }
 
@@ -1691,8 +1649,7 @@ ngx_http_writer(ngx_http_request_t *r)
         }
 
         if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
-            ngx_http_close_request(r->main, 0);
-            ngx_http_close_connection(r->connection);
+            ngx_http_close_request(r, 0);
         }
 
         return;
@@ -1722,13 +1679,18 @@ ngx_http_postponed_handler(ngx_http_requ
 
         rc = ngx_http_output_filter(r, NULL);
 
+        if (rc == NGX_DONE) {
+            /* the request pool is already destroyed */
+            return NGX_DONE;
+        }
+
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                        "http postponed output filter: %d", rc);
 
-        /*
-         * we treat NGX_ERROR as NGX_OK, because we need to complete
-         * all postponed requests
-         */
+        if (rc == NGX_ERROR) {
+            ngx_http_close_request(r, 0);
+            return NGX_DONE;
+        }
 
         pr = r->postponed;
 
@@ -1769,7 +1731,6 @@ ngx_http_block_read(ngx_http_request_t *
             == NGX_ERROR)
         {
             ngx_http_close_request(r, 0);
-            ngx_http_close_connection(r->connection);
         }
     }
 }
@@ -1832,14 +1793,12 @@ ngx_http_read_discarded_body_handler(ngx
     if (rc == NGX_AGAIN) {
         if (ngx_handle_read_event(r->connection->read, 0) == NGX_ERROR) {
             ngx_http_close_request(r, rc);
-            ngx_http_close_connection(r->connection);
             return;
         }
     }
 
     if (rc != NGX_OK) {
         ngx_http_close_request(r, rc);
-        ngx_http_close_connection(r->connection);
     }
 }
 
@@ -1932,7 +1891,6 @@ ngx_http_set_keepalive(ngx_http_request_
 
                 if (hc->free == NULL) {
                     ngx_http_close_request(r, 0);
-                    ngx_http_close_connection(c);
                     return;
                 }
             }
@@ -1951,7 +1909,8 @@ ngx_http_set_keepalive(ngx_http_request_
 
     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
 
-    ngx_http_close_request(r, 0);
+    ngx_http_request_done(r, 0);
+
     c->data = hc;
 
     ngx_add_timer(rev, clcf->keepalive_timeout);
@@ -2221,7 +2180,6 @@ ngx_http_set_lingering_close(ngx_http_re
 
     if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
         ngx_http_close_request(r, 0);
-        ngx_http_close_connection(c);
         return;
     }
 
@@ -2234,14 +2192,12 @@ ngx_http_set_lingering_close(ngx_http_re
                                                                   == NGX_ERROR)
             {
                 ngx_http_close_request(r, 0);
-                ngx_http_close_connection(c);
                 return;
             }
 
         } else if (ngx_event_flags & NGX_USE_LEVEL_EVENT) {
             if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
                 ngx_http_close_request(r, 0);
-                ngx_http_close_connection(c);
                 return;
             }
         }
@@ -2251,7 +2207,6 @@ ngx_http_set_lingering_close(ngx_http_re
         ngx_connection_error(c, ngx_socket_errno,
                              ngx_shutdown_socket_n " failed");
         ngx_http_close_request(r, 0);
-        ngx_http_close_connection(c);
         return;
     }
 
@@ -2279,14 +2234,12 @@ ngx_http_lingering_close_handler(ngx_eve
 
     if (rev->timedout) {
         ngx_http_close_request(r, 0);
-        ngx_http_close_connection(c);
         return;
     }
 
     timer = r->lingering_time - ngx_time();
     if (timer <= 0) {
         ngx_http_close_request(r, 0);
-        ngx_http_close_connection(c);
         return;
     }
 
@@ -2297,7 +2250,6 @@ ngx_http_lingering_close_handler(ngx_eve
 
         if (n == NGX_ERROR || n == 0) {
             ngx_http_close_request(r, 0);
-            ngx_http_close_connection(c);
             return;
         }
 
@@ -2305,7 +2257,6 @@ ngx_http_lingering_close_handler(ngx_eve
 
     if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
         ngx_http_close_request(r, 0);
-        ngx_http_close_connection(c);
         return;
     }
 
@@ -2362,10 +2313,31 @@ ngx_http_send_last(ngx_http_request_t *r
 void
 ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error)
 {
-    ngx_log_t                 *log;
-    struct linger              linger;
-    ngx_http_log_ctx_t        *ctx;
-    ngx_http_core_loc_conf_t  *clcf;
+    ngx_connection_t    *c;
+    ngx_http_cleanup_t  *cln;
+
+    c = r->connection;
+    r = r->main;
+
+    for (cln = r->cleanup; cln; cln = cln->next) {
+        if (cln->handler) {
+            cln->handler(cln->data);
+        }
+    }
+
+    ngx_http_request_done(r, error);
+    ngx_http_close_connection(c);
+}
+
+
+void
+ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error)
+{
+    ngx_log_t                  *log;
+    struct linger               linger;
+    ngx_http_log_ctx_t         *ctx;
+    ngx_http_core_loc_conf_t   *clcf;
+    ngx_http_core_main_conf_t  *cmcf;
 
     log = r->connection->log;
 
@@ -2392,7 +2364,11 @@ ngx_http_close_request(ngx_http_request_
         r->headers_out.status = error;
     }
 
-    ngx_http_log_handler(r);
+    cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
+
+    if (cmcf->log_handler) {
+        cmcf->log_handler(r);
+    }
 
     if (r->connection->timedout) {
         clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -125,7 +125,7 @@ typedef struct {
 typedef struct {
     ngx_str_t                         name;
     ngx_uint_t                        offset;
-} ngx_http_header0_t;
+} ngx_http_header_out_t;
 
 
 typedef struct {
@@ -238,6 +238,17 @@ typedef struct {
 } ngx_http_connection_t;
 
 
+typedef void (*ngx_http_cleanup_pt)(void *data);
+
+typedef struct ngx_http_cleanup_s  ngx_http_cleanup_t;
+
+struct ngx_http_cleanup_s {
+    ngx_http_cleanup_pt   handler;
+    void                 *data;
+    ngx_http_cleanup_t   *next;
+};
+
+
 typedef struct ngx_http_postponed_request_s  ngx_http_postponed_request_t;
 
 struct ngx_http_postponed_request_s {
@@ -325,6 +336,8 @@ struct ngx_http_request_s {
 
     ngx_http_log_handler_pt           log_handler;
 
+    ngx_http_cleanup_t               *cleanup;
+
     unsigned                          http_state:4;
 
     /* URI with "/." and on Win32 with "//" */
@@ -401,8 +414,8 @@ struct ngx_http_request_s {
 };
 
 
-extern ngx_http_header_t   ngx_http_headers_in[];
-extern ngx_http_header0_t   ngx_http_headers_out[];
+extern ngx_http_header_t       ngx_http_headers_in[];
+extern ngx_http_header_out_t   ngx_http_headers_out[];
 
 
 #endif /* _NGX_HTTP_REQUEST_H_INCLUDED_ */
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -29,6 +29,7 @@ static void ngx_http_upstream_process_bo
 static void ngx_http_upstream_dummy_handler(ngx_event_t *wev);
 static void ngx_http_upstream_next(ngx_http_request_t *r,
     ngx_http_upstream_t *u, ngx_uint_t ft_type);
+static void ngx_http_upstream_cleanup(void *data);
 static void ngx_http_upstream_finalize_request(ngx_http_request_t *r,
     ngx_http_upstream_t *u, ngx_int_t rc);
 
@@ -223,6 +224,7 @@ ngx_http_upstream_init(ngx_http_request_
 {
     ngx_time_t                *tp;
     ngx_connection_t          *c;
+    ngx_http_cleanup_t        *cln;
     ngx_http_upstream_t       *u;
     ngx_http_core_loc_conf_t  *clcf;
 
@@ -277,7 +279,8 @@ ngx_http_upstream_init(ngx_http_request_
     u->writer.pool = r->pool;
 
     if (ngx_array_init(&u->states, r->pool, u->peer.peers->number,
-                       sizeof(ngx_http_upstream_state_t)) != NGX_OK)
+                       sizeof(ngx_http_upstream_state_t))
+        != NGX_OK)
     {
         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
         return;
@@ -295,6 +298,16 @@ ngx_http_upstream_init(ngx_http_request_
 
     u->state->response_time = tp->sec * 1000 + tp->msec;
 
+    cln = ngx_http_cleanup_add(r, sizeof(void *));
+    if (cln == NULL) {
+        ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+        return;
+    }
+
+    cln->handler = ngx_http_upstream_cleanup;
+    cln->data = r;
+    u->cleanup = &cln->handler;
+
     ngx_http_upstream_connect(r, u);
 }
 
@@ -1379,8 +1392,7 @@ ngx_http_upstream_next(ngx_http_request_
         }
     }
 
-    if (r->connection->write->eof) {
-        r->connection->closed = 1;
+    if (r->connection->closed) {
         ngx_http_upstream_finalize_request(r, u,
                                            NGX_HTTP_CLIENT_CLOSED_REQUEST);
         return;
@@ -1426,6 +1438,18 @@ ngx_http_upstream_next(ngx_http_request_
 
 
 static void
+ngx_http_upstream_cleanup(void *data)
+{
+    ngx_http_request_t *r = data;
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "cleanup http upstream request: \"%V\"", &r->uri);
+
+    ngx_http_upstream_finalize_request(r, r->upstream, NGX_DONE);
+}
+
+
+static void
 ngx_http_upstream_finalize_request(ngx_http_request_t *r,
     ngx_http_upstream_t *u, ngx_int_t rc)
 {
@@ -1434,6 +1458,8 @@ ngx_http_upstream_finalize_request(ngx_h
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                    "finalize http upstream request: %i", rc);
 
+    *u->cleanup = NULL;
+
     if (u->state->response_time) {
         tp = ngx_timeofday();
         u->state->response_time = tp->sec * 1000 + tp->msec
@@ -1864,7 +1890,7 @@ ngx_http_upstream_log_error(ngx_http_req
 
     p = ngx_snprintf(buf, len,
                      ", server: %V, URL: \"%V\","
-                     " upstream: %V%V%s%V",
+                     " upstream: \"%V%V%s%V\"",
                      &r->server_name,
                      &r->unparsed_uri,
                      &u->conf->schema,
--- a/src/http/ngx_http_upstream.h
+++ b/src/http/ngx_http_upstream.h
@@ -167,6 +167,8 @@ struct ngx_http_upstream_s {
 
     ngx_str_t                       uri;
 
+    ngx_http_cleanup_pt            *cleanup;
+
     unsigned                        cachable:1;
     unsigned                        accel:1;
 
--- a/src/http/ngx_http_variables.c
+++ b/src/http/ngx_http_variables.c
@@ -97,6 +97,9 @@ static ngx_http_variable_t  ngx_http_cor
     { ngx_string("document_uri"), ngx_http_variable_request,
       offsetof(ngx_http_request_t, uri), 0, 0 },
 
+    { ngx_string("request"), ngx_http_variable_request,
+      offsetof(ngx_http_request_t, request_line), 0, 0 },
+
     { ngx_string("document_root"), ngx_http_variable_document_root, 0, 0, 0 },
 
     { ngx_string("query_string"), ngx_http_variable_request,
@@ -705,7 +708,8 @@ ngx_http_variables_add_core_vars(ngx_con
     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
 
     if (ngx_array_init(&cmcf->all_variables, cf->pool, 32,
-                       sizeof(ngx_http_variable_t)) == NGX_ERROR)
+                       sizeof(ngx_http_variable_t))
+        == NGX_ERROR)
     {
         return NGX_ERROR;
     }
--- a/src/imap/ngx_imap_handler.c
+++ b/src/imap/ngx_imap_handler.c
@@ -355,9 +355,9 @@ ngx_imap_auth_state(ngx_event_t *rev)
         switch (s->command) {
 
         case NGX_IMAP_LOGIN:
-            if (s->args.nelts == 2) {
+            arg = s->args.elts;
 
-                arg = s->args.elts;
+            if (s->args.nelts == 2 && arg[0].len) {
 
                 s->login.len = arg[0].len;
                 s->login.data = ngx_palloc(c->pool, s->login.len);