nginx-0.0.1-2003-10-21-11:47:21 import
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.c b/src/http/modules/proxy/ngx_http_proxy_handler.c
index 87d472c..16a9ed4 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -742,6 +742,12 @@
 
     ep->preread_size = p->header_in->last - p->header_in->pos;
 
+    /*
+     * event_proxy would do p->header_in->last += ep->preread_size
+     * as these bytes were read.
+     */
+    p->header_in->last = p->header_in->pos;
+
     /* STUB */ ep->cachable = 0;
 
     p->event_proxy = ep;
@@ -806,7 +812,7 @@
         }
 
         if (ep->upstream_done || ep->upstream_eof || ep->upstream_error) {
-            ngx_http_proxy_close_connection(c);
+            ngx_http_proxy_close_connection(p->upstream.connection);
             p->upstream.connection = NULL;
         }
     }
@@ -819,7 +825,7 @@
 
     if (ep->downstream_error) {
         if (!p->cachable && p->upstream.connection) {
-            ngx_http_proxy_close_connection(c);
+            ngx_http_proxy_close_connection(p->upstream.connection);
             p->upstream.connection = NULL;
         }
  
@@ -1052,7 +1058,9 @@
         p->upstream.connection = NULL;
     }
 
-    if (p->header_sent) {
+    if (p->header_sent
+        && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE))
+    {
         rc = 0;
     }
 
@@ -1152,7 +1160,12 @@
     conf->bufs.num = 10;
     conf->bufs.size = 4096;
     conf->max_busy_len = 8192 + 4096;
+
+
+    /* CHECK in _init conf->max_temp_size >= conf->bufs.size !!! */
     conf->max_temp_file_size = 4096 * 6;
+
+
     conf->temp_file_write_size = 4096 * 2;
 
     ngx_test_null(conf->temp_path, ngx_pcalloc(cf->pool, sizeof(ngx_path_t)),
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 1eee572..4e3b43c 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -207,13 +207,31 @@
     lcx = r->connection->log->data;
     lcx->action = NULL;
 
+    /* STUB */
     r->keepalive = 1;
+    if (r->headers_in.connection) {
+        if (r->headers_in.connection->value.len == 5
+            && ngx_strcasecmp(r->headers_in.connection->value.data, "close")
+                                                                          == 0)
+        {
+            r->keepalive = 0;
+        }
+    }
+
+#if 0
+    /* TEST STUB */ r->keepalive = 0;
+#endif
 
     if (r->headers_in.content_length_n > 0) {
         r->lingering_close = 1;
+
+    } else {
+        r->lingering_close = 0;
     }
 
+#if 0
     /* TEST STUB */ r->lingering_close = 1;
+#endif
 
     r->connection->write->event_handler = ngx_http_phase_event_handler;
 
@@ -258,6 +276,11 @@
         {
             rc = h[r->phase_handler](r);
 
+            if (rc == NGX_DONE) {
+                return;
+            }
+
+            /* TODO THINK: is it dupliate NGX_DONE ??? */
             if (r->closed) {
                 return;
             }
@@ -435,7 +458,7 @@
 
     ngx_http_handler(r);
 
-    return NGX_OK;
+    return NGX_DONE;
 }
 
 
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index fb5bf2c..a33c8c6 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -583,6 +583,7 @@
                 if (ngx_strcasecmp(headers_in[i].name.data, h->key.data) == 0) {
                     *((ngx_table_elt_t **)
                         ((char *) &r->headers_in + headers_in[i].offset)) = h;
+                    break;
                 }
             }