nginx-0.0.3-2004-04-13-19:08:48 import
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.c b/src/http/modules/proxy/ngx_http_proxy_handler.c
index 017d7c8..0fd89c1 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -425,7 +425,9 @@
 
     n = recv(c->fd, buf, 1, MSG_PEEK);
 
-    if (ev->write && n >= 0) {
+    err = ngx_socket_errno;
+
+    if (ev->write && (n >= 0 || err == NGX_EAGAIN)) {
         return;
     }
 
@@ -443,7 +445,6 @@
     ev->eof = 1;
 
     if (n == -1) {
-        err = ngx_socket_errno;
         if (err == NGX_EAGAIN) {
             return;
         }
@@ -634,11 +635,11 @@
         ngx_del_conn(c, NGX_CLOSE_EVENT);
 
     } else {
-        if (c->read->active) {
+        if (c->read->active || c->read->posted) {
             ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT);
         }
 
-        if (c->write->active) {
+        if (c->write->active || c->read->posted) {
             ngx_del_event(c->write, NGX_WRITE_EVENT, NGX_CLOSE_EVENT);
         }
     }
diff --git a/src/http/modules/proxy/ngx_http_proxy_upstream.c b/src/http/modules/proxy/ngx_http_proxy_upstream.c
index d279759..760d8d6 100644
--- a/src/http/modules/proxy/ngx_http_proxy_upstream.c
+++ b/src/http/modules/proxy/ngx_http_proxy_upstream.c
@@ -327,19 +327,20 @@
 
     r->connection->read->event_handler = ngx_http_proxy_check_broken_connection;
 
-    if ((ngx_event_flags & (NGX_USE_CLEAR_EVENT|NGX_HAVE_KQUEUE_EVENT))
-        && !r->connection->write->active)
-    {
+    if (ngx_event_flags & (NGX_USE_CLEAR_EVENT|NGX_HAVE_KQUEUE_EVENT)) {
+
         /* kqueue allows to detect when client closes prematurely connection */
 
         r->connection->write->event_handler =
                                         ngx_http_proxy_check_broken_connection;
 
-        if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT,
+        if (!r->connection->write->active) {
+            if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT,
                                                 NGX_CLEAR_EVENT) == NGX_ERROR)
-        {
-            ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
-            return;
+            {
+                ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+                return;
+            }
         }
     }
 
@@ -560,6 +561,9 @@
 
     rc = ngx_event_connect_peer(&p->upstream->peer);
 
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, p->request->connection->log, 0,
+                   "http proxy connect: %d", rc);
+
     if (rc == NGX_ERROR) {
         ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
         return;
@@ -573,14 +577,13 @@
         return;
     }
 
-    p->upstream->peer.connection->data = p;
-    p->upstream->peer.connection->write->event_handler =
-                                           ngx_http_proxy_send_request_handler;
-    p->upstream->peer.connection->read->event_handler =
-                                   ngx_http_proxy_process_upstream_status_line;
-
     r = p->request;
     c = p->upstream->peer.connection;
+
+    c->data = p;
+    c->write->event_handler = ngx_http_proxy_send_request_handler;
+    c->read->event_handler = ngx_http_proxy_process_upstream_status_line;
+
     c->pool = r->pool;
     c->read->log = c->write->log = c->log = r->connection->log;
 
@@ -613,6 +616,11 @@
 
     if (rc == NGX_AGAIN) {
         ngx_add_timer(c->write, p->lcf->connect_timeout);
+
+        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                       "http proxy connect handler: " PTR_FMT,
+                       c->write->event_handler);
+
         return;
     }
 
@@ -638,6 +646,9 @@
 
     c = p->upstream->peer.connection;
 
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                   "http proxy send request");
+
 #if (HAVE_KQUEUE)
 
     if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)
@@ -715,8 +726,7 @@
     }
 #endif
 
-    p->upstream->peer.connection->write->event_handler =
-                                                  ngx_http_proxy_dummy_handler;
+    c->write->event_handler = ngx_http_proxy_dummy_handler;
 
     if (ngx_handle_level_write_event(c->write) == NGX_ERROR) {
         ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
@@ -733,6 +743,9 @@
     c = wev->data;
     p = c->data;
 
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
+                   "http proxy send request handler");
+
     if (wev->timedout) {
         p->action = "sending request to upstream";
         ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
diff --git a/src/http/ngx_http_headers.c b/src/http/ngx_http_headers.c
index 57413af..8c925be 100644
--- a/src/http/ngx_http_headers.c
+++ b/src/http/ngx_http_headers.c
@@ -22,6 +22,7 @@
 #if (NGX_HTTP_GZIP)
     { ngx_string("Accept-Encoding"),
                            offsetof(ngx_http_headers_in_t, accept_encoding) },
+    { ngx_string("Via"), offsetof(ngx_http_headers_in_t, via) },
 #endif
 
     { ngx_string("Authorization"),
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index 9233f89..976475e 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -97,8 +97,7 @@
                 return;
             }
 
-            rev->next = (ngx_event_t *) ngx_posted_events;
-            ngx_posted_events = rev; 
+            ngx_post_event(rev); 
 
             ngx_mutex_unlock(ngx_posted_events_mutex);
             return;
@@ -1613,11 +1612,11 @@
         ngx_del_conn(c, NGX_CLOSE_EVENT);
 
     } else {
-        if (c->read->active || c->read->disabled) {
+        if (c->read->active || c->read->posted || c->read->disabled) {
             ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT);
         }
 
-        if (c->write->active || c->write->disabled) {
+        if (c->write->active || c->write->posted || c->write->disabled) {
             ngx_del_event(c->write, NGX_WRITE_EVENT, NGX_CLOSE_EVENT);
         }
     }
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h
index 5e127e8..215dedf 100644
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -100,6 +100,7 @@
 
 #if (NGX_HTTP_GZIP)
     ngx_table_elt_t  *accept_encoding;
+    ngx_table_elt_t  *via;
 #endif
 
     ngx_table_elt_t  *authorization;