nginx-0.1.16-RELEASE import

    *) Bugfix: if the response were transferred by chunks, then on the HEAD
       request the final chunk was issued.

    *) Bugfix: the "Connection: keep-alive" header were issued, even if the
       keepalive_timeout directive forbade the keep-alive use.

    *) Bugfix: the errors in the ngx_http_fastcgi_module caused the
       segmentation faults.

    *) Bugfix: the compressed response encrypted by SSL may not transferred
       complete.

    *) Bugfix: the TCP-specific TCP_NODELAY, TCP_NOPSUH, and TCP_CORK
       options, are not used for the unix domain sockets.

    *) Feature: the rewrite directive supports the arguments rewriting.

    *) Bugfix: the response code 400 was returned for the POST request with
       the "Content-Length: 0" header; the bug had appeared in 0.1.14.
diff --git a/src/http/modules/ngx_http_chunked_filter.c b/src/http/modules/ngx_http_chunked_filter.c
index 5134151..5145ff6 100644
--- a/src/http/modules/ngx_http_chunked_filter.c
+++ b/src/http/modules/ngx_http_chunked_filter.c
@@ -67,7 +67,7 @@
     ngx_buf_t    *b;
     ngx_chain_t   out, tail, *cl, *tl, **ll;
 
-    if (in == NULL || !r->chunked) {
+    if (in == NULL || !r->chunked || r->header_only) {
         return ngx_http_next_body_filter(r, in);
     }
 
diff --git a/src/http/modules/ngx_http_fastcgi_handler.c b/src/http/modules/ngx_http_fastcgi_handler.c
index 41efa0b..9f5016a 100644
--- a/src/http/modules/ngx_http_fastcgi_handler.c
+++ b/src/http/modules/ngx_http_fastcgi_handler.c
@@ -1385,7 +1385,7 @@
                                                ngx_buf_t *buf)
 {
     ngx_int_t                rc;
-    ngx_buf_t               *b;
+    ngx_buf_t               *b, **prev;
     ngx_str_t                line;
     ngx_chain_t             *cl;
     ngx_http_request_t      *r;
@@ -1399,6 +1399,7 @@
     f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module);
 
     b = NULL;
+    prev = &buf->shadow;
 
     f->pos = buf->pos;
     f->last = buf->last;
@@ -1510,11 +1511,14 @@
         ngx_memzero(b, sizeof(ngx_buf_t));
 
         b->pos = f->pos;
-        b->shadow = buf;
+        b->start = buf->start;
+        b->end = buf->end;
         b->tag = p->tag;
         b->temporary = 1;
         b->recycled = 1;
-        buf->shadow = b;
+
+        *prev = b;
+        prev = &b->shadow;
 
         if (!(cl = ngx_alloc_chain_link(p->pool))) {
             return NGX_ERROR;
@@ -1523,6 +1527,8 @@
         cl->buf = b;
         cl->next = NULL;
 
+        /* STUB */ b->num = buf->num;
+
         ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "input buf #%d", b->num);
 
         ngx_chain_add_link(p->in, p->last_in, cl);
@@ -1563,7 +1569,16 @@
     }
 
     if (b) {
+        b->shadow = buf;
         b->last_shadow = 1;
+
+        return NGX_OK;
+    }
+
+    /* there is no data record in the buf, add it to free chain */
+
+    if (ngx_event_pipe_add_free_buf(p, buf) != NGX_OK) {
+        return NGX_ERROR;
     }
 
     return NGX_OK;
diff --git a/src/http/modules/ngx_http_gzip_filter.c b/src/http/modules/ngx_http_gzip_filter.c
index 0cd1d91..cda9ca3 100644
--- a/src/http/modules/ngx_http_gzip_filter.c
+++ b/src/http/modules/ngx_http_gzip_filter.c
@@ -471,8 +471,8 @@
 
         /*
          * We preallocate a memory for zlib in one buffer (200K-400K), this
-         * dicreases a number of malloc() and free() calls and also probably
-         * dicreases a number of syscalls (sbrk() and so on).
+         * decreases a number of malloc() and free() calls and also probably
+         * decreases a number of syscalls (sbrk() and so on).
          * Besides we free this memory as soon as the gzipping will complete
          * and do not wait while a whole response will be sent to a client.
          *
diff --git a/src/http/modules/ngx_http_rewrite_handler.c b/src/http/modules/ngx_http_rewrite_handler.c
index 002edb8..636331b 100644
--- a/src/http/modules/ngx_http_rewrite_handler.c
+++ b/src/http/modules/ngx_http_rewrite_handler.c
@@ -9,9 +9,10 @@
 #include <ngx_http.h>
 
 
-#define NGX_HTTP_REWRITE_COPY_MATCH  0
-#define NGX_HTTP_REWRITE_COPY_SHORT  1
-#define NGX_HTTP_REWRITE_COPY_LONG   2
+#define NGX_HTTP_REWRITE_COPY_CAPTURE  0
+#define NGX_HTTP_REWRITE_COPY_SHORT    1
+#define NGX_HTTP_REWRITE_COPY_LONG     2
+#define NGX_HTTP_REWRITE_START_ARGS    3
 
 
 typedef struct {
@@ -119,7 +120,7 @@
     uintptr_t                     data;
     ngx_int_t                     rc;
     ngx_uint_t                    i, m, n;
-    ngx_str_t                     uri;
+    ngx_str_t                     uri, args;
     ngx_http_rewrite_op_t        *op;
     ngx_http_rewrite_rule_t      *rule;
     ngx_http_rewrite_srv_conf_t  *scf;
@@ -176,13 +177,14 @@
         uri.len = rule[i].size;
 
         for (n = 1; n < (ngx_uint_t) rc; n++) {
-           uri.len += captures[2 * n + 1] - captures[2 * n];
+            uri.len += captures[2 * n + 1] - captures[2 * n];
         }
 
         if (!(uri.data = ngx_palloc(r->pool, uri.len))) {
             return NGX_HTTP_INTERNAL_SERVER_ERROR;
         }
 
+        args.data = NULL;
         p = uri.data;
 
         op = rule[i].ops.elts;
@@ -198,22 +200,34 @@
             } else if (op[n].op == NGX_HTTP_REWRITE_COPY_LONG) {
                 p = ngx_cpymem(p, (void *) op[n].data, op[n].len);
 
-            } else { /* NGX_HTTP_REWRITE_COPY_MATCH */
+            } else if (op[n].op == NGX_HTTP_REWRITE_START_ARGS) {
+                args.data = p;
+
+            } else { /* NGX_HTTP_REWRITE_COPY_CAPTURE */
                 m = 2 * op[n].data;
                 p = ngx_cpymem(p, &r->uri.data[captures[m]],
                                captures[m + 1] - captures[m]);
             }
         }
 
-        uri.len = p - uri.data;
+        if (args.data) {
+            uri.len = args.data - uri.data;
+            args.len = p - args.data;
 
-        if (scf->log) {
-            ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
-                          "rewritten uri: \"%V\"", &uri);
+            r->args = args;
+
+        } else {
+            uri.len = p - uri.data;
+            args.len = 0;
         }
 
         r->uri = uri;
 
+        if (scf->log) {
+            ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
+                          "rewritten uri: \"%V\", args: \"%V\"", &uri, &args);
+        }
+
         if (ngx_http_set_exten(r) != NGX_OK) {
             return NGX_HTTP_INTERNAL_SERVER_ERROR;
         }
@@ -359,7 +373,9 @@
             return NGX_CONF_ERROR;
         }
 
-        for (i = 0; i < value[2].len; /* void */) {
+        i = 0;
+
+        while (i < value[2].len) {
 
             if (!(op = ngx_push_array(&rule->ops))) {
                 return NGX_CONF_ERROR;
@@ -372,7 +388,8 @@
                 && value[2].data[i + 1] >= '1'
                 && value[2].data[i + 1] <= '9')
             {
-                op->op = NGX_HTTP_REWRITE_COPY_MATCH; 
+                op->op = NGX_HTTP_REWRITE_COPY_CAPTURE; 
+                op->len = 0;
                 op->data = value[2].data[++i] - '0';
 
                 if (rule->ncaptures < op->data) {
@@ -381,39 +398,53 @@
 
                 i++;
 
-            } else {
+                continue;
+            }
+
+            if (value[2].data[i] == '?') {
+                op->op = NGX_HTTP_REWRITE_START_ARGS; 
+                op->len = 0;
+                op->data = 0;
+
                 i++;
 
-                while (i < value[2].len && value[2].data[i] != '$') {
-                    i++;
-                }
+                continue;
+            }
 
-                len = &value[2].data[i] - data;
-                rule->size += len;
+            i++;
 
-                if (len) {
+            while (i < value[2].len
+                   && value[2].data[i] != '$'
+                   && value[2].data[i] != '?')
+            {
+                i++;
+            }
 
-                    op->len = len;
+            len = &value[2].data[i] - data;
+            rule->size += len;
 
-                    if (len <= sizeof(uintptr_t)) {
-                        op->op = NGX_HTTP_REWRITE_COPY_SHORT; 
-                        op->data = 0;
+            if (len) {
 
-                        while (len--) {
-                            op->data <<= 8;
-                            op->data |= data[len];
-                        }
+                op->len = len;
 
-                    } else {
-                        op->op = NGX_HTTP_REWRITE_COPY_LONG;
+                if (len <= sizeof(uintptr_t)) {
+                    op->op = NGX_HTTP_REWRITE_COPY_SHORT; 
+                    op->data = 0;
 
-                        if (!(p = ngx_palloc(cf->pool, len))) {
-                            return NGX_CONF_ERROR;
-                        }
-
-                        ngx_memcpy(p, data, len);
-                        op->data = (uintptr_t) p;
+                    while (len--) {
+                        op->data <<= 8;
+                        op->data |= data[len];
                     }
+
+                } else {
+                    op->op = NGX_HTTP_REWRITE_COPY_LONG;
+
+                    if (!(p = ngx_palloc(cf->pool, len))) {
+                        return NGX_CONF_ERROR;
+                    }
+
+                    ngx_memcpy(p, data, len);
+                    op->data = (uintptr_t) p;
                 }
             }
         }
diff --git a/src/http/modules/ngx_http_static_handler.c b/src/http/modules/ngx_http_static_handler.c
index 4cc0de1..fa2bcd5 100644
--- a/src/http/modules/ngx_http_static_handler.c
+++ b/src/http/modules/ngx_http_static_handler.c
@@ -76,7 +76,6 @@
     ngx_chain_t                  out;
     ngx_file_info_t              fi;
     ngx_http_cleanup_t          *file_cleanup, *redirect_cleanup;
-    ngx_http_log_ctx_t          *ctx;
     ngx_http_core_loc_conf_t    *clcf;
     ngx_http_static_loc_conf_t  *slcf;
 #if (NGX_HTTP_CACHE)
@@ -476,8 +475,7 @@
 
 #endif
 
-    ctx = log->data;
-    ctx->action = "sending response to client";
+    log->action = "sending response to client";
 
     file_cleanup->data.file.fd = fd;
     file_cleanup->data.file.name = name.data;
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.c b/src/http/modules/proxy/ngx_http_proxy_handler.c
index 3e9383e..ea3a0ca 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -778,17 +778,17 @@
 }
 
 
-u_char *ngx_http_proxy_log_error(void *data, u_char *buf, size_t len)
+u_char *ngx_http_proxy_log_error(ngx_log_t *log, u_char *buf, size_t len)
 {
-    ngx_http_proxy_log_ctx_t *ctx = data;
-
     u_char                          *p;
     ngx_int_t                        escape;
     ngx_str_t                        uri;
     ngx_http_request_t              *r;
     ngx_peer_connection_t           *peer;
+    ngx_http_proxy_log_ctx_t        *ctx;
     ngx_http_proxy_upstream_conf_t  *uc;
 
+    ctx = log->data;
     r = ctx->proxy->request;
     uc = ctx->proxy->lcf->upstream;
     peer = &ctx->proxy->upstream->peer;
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.h b/src/http/modules/proxy/ngx_http_proxy_handler.h
index 915ca62..4dd97c2 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.h
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.h
@@ -251,7 +251,7 @@
 void ngx_http_proxy_busy_lock_handler(ngx_event_t *rev);
 void ngx_http_proxy_upstream_busy_lock(ngx_http_proxy_ctx_t *p);
 
-u_char *ngx_http_proxy_log_error(void *data, u_char *buf, size_t len);
+u_char *ngx_http_proxy_log_error(ngx_log_t *log, u_char *buf, size_t len);
 void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc);
 void ngx_http_proxy_close_connection(ngx_http_proxy_ctx_t *p);
 
diff --git a/src/http/ngx_http.h b/src/http/ngx_http.h
index 9425e3b..a21c670 100644
--- a/src/http/ngx_http.h
+++ b/src/http/ngx_http.h
@@ -36,15 +36,6 @@
 
 
 struct ngx_http_log_ctx_s {
-    ngx_uint_t         connection;
-
-    /*
-     * we declare "action" as "char *" because the actions are usually
-     * the static strings and in the "u_char *" case we have to override
-     * all the time their types
-     */
-
-    char                *action;
     ngx_str_t           *client;
     ngx_http_request_t  *request;
 };
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index f2f69f4..bf455d8 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -351,13 +351,10 @@
 
 void ngx_http_handler(ngx_http_request_t *r)
 {
-    ngx_http_log_ctx_t  *ctx;
+    r->connection->log->action = NULL;
 
     r->connection->unexpected_eof = 0;
 
-    ctx = r->connection->log->data;
-    ctx->action = NULL;
-
     switch (r->headers_in.connection_type) {
     case 0:
         if (r->http_version > NGX_HTTP_VERSION_10) {
@@ -541,6 +538,10 @@
         r->connection->sendfile = 0;
     }
 
+    if (r->keepalive && clcf->keepalive_timeout == 0) {
+        r->keepalive = 0;
+    }
+
     if (!clcf->tcp_nopush) {
         /* disable TCP_NOPUSH/TCP_CORK use */
         r->connection->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index 8110312..209c547 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -35,7 +35,7 @@
 
 static void ngx_http_client_error(ngx_http_request_t *r,
                                   int client_error, int error);
-static u_char *ngx_http_log_error(void *data, u_char *buf, size_t len);
+static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
 
 
 /* NGX_HTTP_PARSE_... errors */
@@ -111,12 +111,14 @@
         return;
     }
 
-    ctx->connection = c->number;
     ctx->client = &c->addr_text;
-    ctx->action = "reading client request line";
     ctx->request = NULL;
-    c->log->data = ctx;
+
+    c->log->connection = c->number;
     c->log->handler = ngx_http_log_error;
+    c->log->data = ctx;
+    c->log->action = "reading client request line";
+
     c->log_error = NGX_ERROR_INFO;
 
     rev = c->read;
@@ -634,7 +636,7 @@
                 return;
             }
 
-            ctx->action = "reading client request headers";
+            c->log->action = "reading client request headers";
 
             rev->event_handler = ngx_http_process_request_headers;
             ngx_http_process_request_headers(rev);
@@ -1099,7 +1101,7 @@
         }
     }
 
-    if (r->method == NGX_HTTP_POST && r->headers_in.content_length_n <= 0) {
+    if (r->method == NGX_HTTP_POST && r->headers_in.content_length_n == -1) {
         return NGX_HTTP_PARSE_POST_WO_CL_HEADER;
     }
 
@@ -1608,7 +1610,6 @@
     ngx_event_t               *rev, *wev;
     ngx_connection_t          *c;
     ngx_http_connection_t     *hc;
-    ngx_http_log_ctx_t        *ctx;
     ngx_http_core_srv_conf_t  *cscf;
     ngx_http_core_loc_conf_t  *clcf;
 
@@ -1617,8 +1618,7 @@
 
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
 
-    ctx = c->log->data;
-    ctx->action = "closing request";
+    c->log->action = "closing request";
 
     hc = r->http_connection;
     b = r->header_in;
@@ -1682,7 +1682,7 @@
         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
 
         hc->pipeline = 1;
-        ctx->action = "reading client pipelined request line";
+        c->log->action = "reading client pipelined request line";
         ngx_http_init_request(rev);
         return;
     }
@@ -1760,7 +1760,7 @@
         }
     }
 
-    ctx->action = "keepalive";
+    c->log->action = "keepalive";
 
     if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
         if (ngx_tcp_push(c->fd) == NGX_ERROR) {
@@ -1776,8 +1776,10 @@
         tcp_nodelay = 1;
     }
 
-    if (tcp_nodelay && clcf->tcp_nodelay && !c->tcp_nodelay) {
-
+    if (tcp_nodelay
+        && clcf->tcp_nodelay
+        && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET)
+    {
         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
 
         if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
@@ -1789,7 +1791,7 @@
             return;
         }
 
-        c->tcp_nodelay = 1;
+        c->tcp_nodelay = NGX_TCP_NODELAY_SET;
     }
 
 #if 0
@@ -1809,7 +1811,6 @@
     ssize_t                 n;
     ngx_buf_t              *b;
     ngx_connection_t       *c;
-    ngx_http_log_ctx_t     *ctx;
     ngx_http_connection_t  *hc;
 
     c = rev->data;
@@ -1821,16 +1822,14 @@
         return;
     }
 
-    ctx = (ngx_http_log_ctx_t *) rev->log->data;
-
 #if (NGX_HAVE_KQUEUE)
 
     if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
         if (rev->pending_eof) {
-            rev->log->handler = NULL;
+            c->log->handler = NULL;
             ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
                           "kevent() reported that client %V closed "
-                          "keepalive connection", ctx->client);
+                          "keepalive connection", &c->addr_text);
 #if (NGX_HTTP_SSL)
             if (c->ssl) {
                 c->ssl->no_send_shut = 1;
@@ -1889,18 +1888,19 @@
         return;
     }
 
-    rev->log->handler = NULL;
+    c->log->handler = NULL;
 
     if (n == 0) {
         ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,
-                      "client %V closed keepalive connection", ctx->client);
+                      "client %V closed keepalive connection", &c->addr_text);
         ngx_http_close_connection(c);
         return;
     }
 
     b->last += n;
-    rev->log->handler = ngx_http_log_error;
-    ctx->action = "reading client request line";
+
+    c->log->handler = ngx_http_log_error;
+    c->log->action = "reading client request line";
 
     ngx_http_init_request(rev);
 }
@@ -2302,16 +2302,17 @@
 }
 
 
-static u_char *ngx_http_log_error(void *data, u_char *buf, size_t len)
+static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len)
 {
-    ngx_http_log_ctx_t *ctx = data;
-
-    u_char *p;
+    u_char              *p;
+    ngx_http_log_ctx_t  *ctx;
 
     p = buf;
 
-    if (ctx->action) {
-        p = ngx_snprintf(p, len, " while %s", ctx->action);
+    ctx = log->data;
+
+    if (log->action) {
+        p = ngx_snprintf(p, len, " while %s", log->action);
         len -= p - buf;
     }
 
diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
index 2e5459d..dae8269 100644
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -117,8 +117,8 @@
     }
 
     u->peer.log = r->connection->log;
-    u->saved_ctx = r->connection->log->data;
-    u->saved_handler = r->connection->log->handler;
+    u->saved_log_ctx = r->connection->log->data;
+    u->saved_log_handler = r->connection->log->handler;
     r->connection->log->data = u->log_ctx;
     r->connection->log->handler = u->log_handler;
 
@@ -160,6 +160,14 @@
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ev->log, 0,
                    "http upstream check client, write event:%d", ev->write);
 
+    c = ev->data;
+    r = c->data;
+    u = r->upstream;
+
+    if (u->peer.connection == NULL) {
+        return;
+    }
+
 #if (NGX_HAVE_KQUEUE)
 
     if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
@@ -174,10 +182,6 @@
             ev->error = 1;
         }
 
-        c = ev->data;
-        r = c->data;
-        u = r->upstream;
-
         if (!u->cachable && u->peer.connection) {
             ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,
                           "kevent() reported that client closed "
@@ -203,8 +207,6 @@
 
 #endif
 
-    c = ev->data;
-
     n = recv(c->fd, buf, 1, MSG_PEEK);
 
     err = ngx_socket_errno;
@@ -218,9 +220,6 @@
         return;
     }
 
-    r = c->data;
-    u = r->upstream;
-
     if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && ev->active) {
         if (ngx_del_event(ev, NGX_READ_EVENT, 0) == NGX_ERROR) {
             ngx_http_upstream_finalize_request(r, u,
@@ -242,8 +241,7 @@
 
         ev->error = 1;
 
-    } else {
-        /* n == 0 */
+    } else { /* n == 0 */
         err = 0;
     }
 
@@ -272,10 +270,8 @@
 {
     ngx_int_t            rc;
     ngx_connection_t    *c;
-    ngx_http_log_ctx_t  *ctx;
 
-    ctx = r->connection->log->data;
-    ctx->action = "connecting to upstream";
+    r->connection->log->action = "connecting to upstream";
 
     r->connection->single_connection = 0;
 
@@ -413,9 +409,8 @@
 static void ngx_http_upstream_send_request(ngx_http_request_t *r,
                                            ngx_http_upstream_t *u)
 {
-    int                  rc;
-    ngx_connection_t    *c;
-    ngx_http_log_ctx_t  *ctx;
+    int                rc;
+    ngx_connection_t  *c;
     
     c = u->peer.connection;
 
@@ -437,8 +432,7 @@
 
 #endif
 
-    ctx = c->log->data;
-    ctx->action = "sending request to upstream";
+    c->log->action = "sending request to upstream";
 
     rc = ngx_output_chain(&u->output,
                           u->request_sent ? NULL : r->request_body->bufs);
@@ -515,7 +509,6 @@
 {
     ngx_connection_t     *c;
     ngx_http_request_t   *r;
-    ngx_http_log_ctx_t   *ctx;
     ngx_http_upstream_t  *u;
 
     c = wev->data;
@@ -526,8 +519,7 @@
                    "http upstream send request handler");
 
     if (wev->timedout) {
-        ctx = c->log->data;
-        ctx->action = "sending request to upstream";
+        c->log->action = "sending request to upstream";
         ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);
         return;
     }
@@ -548,7 +540,6 @@
     ngx_int_t             rc;
     ngx_connection_t     *c;
     ngx_http_request_t   *r;
-    ngx_http_log_ctx_t   *ctx;
     ngx_http_upstream_t  *u;
 
     c = rev->data;
@@ -556,10 +547,9 @@
     u = r->upstream;
 
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
-                   "http upstream process handler");
+                   "http upstream process header");
 
-    ctx = c->log->data;
-    ctx->action = "reading response header from upstream";
+    c->log->action = "reading response header from upstream";
 
     if (rev->timedout) {
         ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);
@@ -576,7 +566,7 @@
 
         u->header_in.pos = u->header_in.start;
         u->header_in.last = u->header_in.start;
-        u->header_in.end = u->header_in.last + u->conf->header_buffer_size;
+        u->header_in.end = u->header_in.start + u->conf->header_buffer_size;
         u->header_in.temporary = 1;
 
         u->header_in.tag = u->output.tag;
@@ -815,7 +805,6 @@
 {
     ngx_connection_t     *c;
     ngx_http_request_t   *r;
-    ngx_http_log_ctx_t   *ctx;
     ngx_http_upstream_t  *u;
     ngx_event_pipe_t     *p;
 
@@ -823,17 +812,15 @@
     r = c->data;
     u = r->upstream;
 
-    ctx = ev->log->data;
-
     if (ev->write) {
-        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
-                       "http proxy process downstream");
-        ctx->action = "sending to client";
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                       "http upstream process downstream");
+        c->log->action = "sending to client";
     
     } else {
-        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
-                       "http proxy process upstream");
-        ctx->action = "reading upstream body";
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                       "http upstream process upstream");
+        c->log->action = "reading upstream";
     }
     
     p = &u->pipe;
@@ -882,8 +869,8 @@
 #endif
 
         if (p->upstream_done || p->upstream_eof || p->upstream_error) {
-            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ev->log, 0,
-                           "http proxy upstream exit: %p", p->out);
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                           "http upstream exit: %p", p->out);
 #if 0
             ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
 #endif
@@ -893,8 +880,8 @@
     }
 
     if (p->downstream_error) {
-        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
-                       "http proxy downstream error");
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                       "http upstream downstream error");
 
         if (!u->cachable && u->peer.connection) {
             ngx_http_upstream_finalize_request(r, u, 0);
@@ -976,7 +963,7 @@
 
             if (u->stale && (u->conf->use_stale & ft_type)) {
                 ngx_http_upstream_finalize_request(r, u,
-                                       ngx_http_proxy_send_cached_response(r));
+                                       ngx_http_send_cached_response(r));
                 return;
             }
 
@@ -1010,8 +997,8 @@
                                                ngx_http_upstream_t *u,
                                                ngx_int_t rc)
 {
-    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                   "finalize http upstream request");
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "finalize http upstream request: %i", rc);
 
     u->finalize_request(r, rc);
 
@@ -1023,15 +1010,17 @@
         ngx_close_connection(u->peer.connection);
     }
 
+    u->peer.connection = NULL;
+
     if (u->header_sent
         && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE))
     {
         rc = 0;
     }
 
-    if (u->saved_ctx) {
-        r->connection->log->data = u->saved_ctx;
-        r->connection->log->handler = u->saved_handler;
+    if (u->saved_log_ctx) {
+        r->connection->log->data = u->saved_log_ctx;
+        r->connection->log->handler = u->saved_log_handler;
     }
 
     if (u->pipe.temp_file) {
@@ -1043,7 +1032,7 @@
 #if 0
     if (u->cache) {
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                       "http proxy cache fd: %d",
+                       "http upstream cache fd: %d",
                        u->cache->ctx.file.fd);
     }
 #endif
@@ -1057,6 +1046,8 @@
 #endif
     }
 
+    r->connection->log->action = "sending to client";
+
     if (rc == 0 && r->main == NULL) {
         rc = ngx_http_send_last(r);
     }
@@ -1111,24 +1102,24 @@
 }
 
 
-u_char *ngx_http_upstream_log_error(void *data, u_char *buf, size_t len)
+u_char *ngx_http_upstream_log_error(ngx_log_t *log, u_char *buf, size_t len)
 {
-    ngx_http_log_ctx_t *ctx = data;
-    
     u_char                 *p;
     ngx_int_t               escape;
     ngx_str_t               uri;
+    ngx_http_log_ctx_t     *ctx;
     ngx_http_request_t     *r;
     ngx_http_upstream_t    *u;
     ngx_peer_connection_t  *peer;
 
+    ctx = log->data;
     r = ctx->request;
     u = r->upstream;
     peer = &u->peer;
 
     p = ngx_snprintf(buf, len,
                      " while %s, client: %V, URL: %V, upstream: %V%V%s%V",
-                     ctx->action,
+                     log->action,
                      &r->connection->addr_text,
                      &r->unparsed_uri,
                      &u->schema,
diff --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h
index 6614211..79ba2a1 100644
--- a/src/http/ngx_http_upstream.h
+++ b/src/http/ngx_http_upstream.h
@@ -92,8 +92,8 @@
 
     ngx_http_log_ctx_t         *log_ctx;
     ngx_log_handler_pt          log_handler;
-    ngx_http_log_ctx_t         *saved_ctx;
-    ngx_log_handler_pt          saved_handler;
+    ngx_http_log_ctx_t         *saved_log_ctx;
+    ngx_log_handler_pt          saved_log_handler;
 
     /* used to parse an upstream HTTP header */
     ngx_uint_t                  status;
@@ -113,7 +113,7 @@
 
 
 void ngx_http_upstream_init(ngx_http_request_t *r);
-u_char *ngx_http_upstream_log_error(void *data, u_char *buf, size_t len);
+u_char *ngx_http_upstream_log_error(ngx_log_t *log, u_char *buf, size_t len);
 
 
 extern char *ngx_http_upstream_header_errors[];
diff --git a/src/http/ngx_http_write_filter.c b/src/http/ngx_http_write_filter.c
index 632c6bf..d4de4a1 100644
--- a/src/http/ngx_http_write_filter.c
+++ b/src/http/ngx_http_write_filter.c
@@ -81,7 +81,18 @@
 #if 1
         if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
             ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
-                              "zero size buf in writer");
+                          "zero size buf in writer "
+                          "t:%d r:%d f:%d %p %p-%p %p %O-%O",
+                          cl->buf->temporary,
+                          cl->buf->recycled,
+                          cl->buf->in_file,
+                          cl->buf->start,
+                          cl->buf->pos,
+                          cl->buf->last,
+                          cl->buf->file,
+                          cl->buf->file_pos,
+                          cl->buf->file_last);
+
             ngx_debug_point();
         }
 #endif
@@ -120,7 +131,18 @@
 #if 1
         if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
             ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
-                              "zero size buf in writer");
+                          "zero size buf in writer "
+                          "t:%d r:%d f:%d %p %p-%p %p %O-%O",
+                          cl->buf->temporary,
+                          cl->buf->recycled,
+                          cl->buf->in_file,
+                          cl->buf->start,
+                          cl->buf->pos,
+                          cl->buf->last,
+                          cl->buf->file,
+                          cl->buf->file_pos,
+                          cl->buf->file_last);
+
             ngx_debug_point();
         }
 #endif
@@ -161,15 +183,16 @@
     }
 
     if (size == 0 && !c->buffered) {
-        if (!last) {
-            ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
-                          "the http output chain is empty");
-
-            ngx_debug_point();
-
-            return NGX_ERROR;
+        if (last) {
+            return NGX_OK;
         }
-        return NGX_OK;
+
+        ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
+                      "the http output chain is empty");
+
+        ngx_debug_point();
+
+        return NGX_ERROR;
     }
 
     sent = c->sent;
@@ -192,7 +215,7 @@
 
     ctx->out = chain;
 
-    if (chain || c->buffered) {
+    if (chain || (last && c->buffered)) {
         return NGX_AGAIN;
     }