nginx-0.0.1-2003-10-29-11:30:44 import
diff --git a/src/http/modules/ngx_http_gzip_filter.c b/src/http/modules/ngx_http_gzip_filter.c
index a32714d..e9271a9 100644
--- a/src/http/modules/ngx_http_gzip_filter.c
+++ b/src/http/modules/ngx_http_gzip_filter.c
@@ -190,18 +190,23 @@
                         sizeof(ngx_http_gzip_ctx_t), NGX_ERROR);
     ctx->request = r;
 
-    ngx_test_null(r->headers_out.content_encoding,
-                  ngx_push_table(r->headers_out.headers),
-                  NGX_ERROR);
+    if (!(r->headers_out.content_encoding =
+                   ngx_http_add_header(&r->headers_out, ngx_http_headers_out)))
+    {
+        return NGX_ERROR;
+    }
 
     r->headers_out.content_encoding->key.len = 0;
     r->headers_out.content_encoding->key.data = NULL;
-    r->headers_out.content_encoding->value.len = 4;
+    r->headers_out.content_encoding->value.len = sizeof("gzip") - 1;
     r->headers_out.content_encoding->value.data = "gzip";
 
     ctx->length = r->headers_out.content_length_n;
     r->headers_out.content_length_n = -1;
-    r->headers_out.content_length = NULL;
+    if (r->headers_out.content_length) {
+        r->headers_out.content_length->key.len = 0;
+        r->headers_out.content_length = NULL;
+    }
     r->filter |= NGX_HTTP_FILTER_NEED_IN_MEMORY;
 
     return ngx_http_next_header_filter(r);
diff --git a/src/http/modules/ngx_http_range_filter.c b/src/http/modules/ngx_http_range_filter.c
index 9233605..ff534bb 100644
--- a/src/http/modules/ngx_http_range_filter.c
+++ b/src/http/modules/ngx_http_range_filter.c
@@ -96,9 +96,12 @@
         || r->headers_in.range->value.len < 7
         || ngx_strncasecmp(r->headers_in.range->value.data, "bytes=", 6) != 0)
     {
-        ngx_test_null(r->headers_out.accept_ranges,
-                      ngx_push_table(r->headers_out.headers),
-                      NGX_ERROR);
+
+        if (!(r->headers_out.accept_ranges =
+                   ngx_http_add_header(&r->headers_out, ngx_http_headers_out)))
+        {
+            return NGX_ERROR;
+        }
 
         r->headers_out.accept_ranges->key.len = sizeof("Accept-Ranges") - 1;
         r->headers_out.accept_ranges->key.data = "Accept-Ranges";
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.c b/src/http/modules/proxy/ngx_http_proxy_handler.c
index 8483cd7..50c199c 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -475,6 +475,11 @@
 
                     /* rc == NGX_OK */
 
+                    if (c->read->ready) {
+                        /* post aio operation */
+                        ngx_http_proxy_process_upstream_status_line(c->read);
+                    }
+
                     if (ngx_handle_level_write_event(c->write) == NGX_ERROR) {
                         ngx_http_proxy_finalize_request(p,
                                                NGX_HTTP_INTERNAL_SERVER_ERROR);
@@ -742,8 +747,8 @@
                 }
             }
 
-            ngx_log_debug(c->log, "HTTP proxy header: '%s: %s'" _
-                          h->key.data _ h->value.data);
+            ngx_log_debug(c->log, "HTTP proxy header: %08X '%s: %s'" _
+                          h _ h->key.data _ h->value.data);
 
             continue;
 
@@ -791,10 +796,6 @@
     n = p->header_in->last - p->header_in->pos;
 
     if (n > 0) {
-#if 0
-        /* TODO THINK */
-        rev->ready = 0;
-#endif
         return n;
     }
 
@@ -804,7 +805,7 @@
     if (n == NGX_AGAIN) {
         ngx_add_timer(rev, p->lcf->read_timeout);
 
-        if (ngx_handle_read_event(rev) == NGX_ERROR) {
+        if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
             ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
             return NGX_ERROR;
         }
@@ -829,10 +830,11 @@
 
 static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p)
 {
-    int                  rc, i;
-    ngx_table_elt_t     *ch, *ph;
-    ngx_event_pipe_t    *ep;
-    ngx_http_request_t  *r;
+    int                        rc, i;
+    ngx_table_elt_t           *ch, *h;
+    ngx_event_pipe_t          *ep;
+    ngx_http_request_t        *r;
+    ngx_http_core_loc_conf_t  *clcf;
 
     r = p->request;
 
@@ -843,26 +845,26 @@
 
     /* copy an upstream header to r->headers_out */
 
-    ph = (ngx_table_elt_t *) p->headers_in.headers->elts;
+    h = p->headers_in.headers->elts;
     for (i = 0; i < p->headers_in.headers->nelts; i++) {
 
-        if (&ph[i] == p->headers_in.connection) {
+        if (&h[i] == p->headers_in.connection) {
             continue;
         }
 
         if (p->accel) {
-            if (&ph[i] == p->headers_in.date
-                || &ph[i] == p->headers_in.accept_ranges) {
+            if (&h[i] == p->headers_in.date
+                || &h[i] == p->headers_in.accept_ranges) {
                 continue;
             }
 
-            if (&ph[i] == p->headers_in.server && !p->lcf->pass_server) {
+            if (&h[i] == p->headers_in.server && !p->lcf->pass_server) {
                 continue;
             }
         }
 
-        if (&ph[i] == p->headers_in.content_type) {
-            r->headers_out.content_type = &ph[i];
+        if (&h[i] == p->headers_in.content_type) {
+            r->headers_out.content_type = &h[i];
             r->headers_out.content_type->key.len = 0;
             continue;
         }
@@ -873,7 +875,7 @@
             return;
         }
 
-        *ch = ph[i];
+        *ch = h[i];
 
         /*
          * ngx_http_header_filter() output the following headers
@@ -883,17 +885,17 @@
          *     r->headers_out.content_length
          */
 
-        if (&ph[i] == p->headers_in.server) {
+        if (&h[i] == p->headers_in.server) {
             r->headers_out.server = ch;
             continue;
         }
 
-        if (&ph[i] == p->headers_in.date) {
+        if (&h[i] == p->headers_in.date) {
             r->headers_out.date = ch;
             continue;
         }
 
-        if (&ph[i] == p->headers_in.content_length) {
+        if (&h[i] == p->headers_in.content_length) {
 
             r->headers_out.content_length_n =
                              ngx_atoi(p->headers_in.content_length->value.data,
@@ -977,6 +979,12 @@
         r->sendfile = 1;
     }
 
+    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
+    ep->read_timeout = p->lcf->read_timeout;
+    ep->send_timeout = clcf->send_timeout;
+    ep->send_lowat = clcf->send_lowat;
+
     p->event_pipe = ep;
 
     p->upstream.connection->read->event_handler = ngx_http_proxy_process_body;
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.h b/src/http/modules/proxy/ngx_http_proxy_handler.h
index f371cbc..601a8f6 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.h
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.h
@@ -44,6 +44,8 @@
 
 
 typedef struct {
+    ngx_table_t      *headers;   /* it must be first field */
+
     ngx_table_elt_t  *date;
     ngx_table_elt_t  *server;
     ngx_table_elt_t  *connection;
@@ -53,8 +55,6 @@
     ngx_table_elt_t  *accept_ranges;
 
     off_t             content_length_n;
-
-    ngx_table_t      *headers;
 } ngx_http_proxy_headers_in_t;
 
 
diff --git a/src/http/ngx_http.h b/src/http/ngx_http.h
index 0bb04c2..90a47f4 100644
--- a/src/http/ngx_http.h
+++ b/src/http/ngx_http.h
@@ -60,6 +60,9 @@
 
 time_t ngx_http_parse_time(char *value, size_t len);
 size_t ngx_http_get_time(char *buf, time_t t);
+ngx_table_elt_t *ngx_http_add_header(void *header,
+                                     ngx_http_header_t *http_headers);
+
 
 
 int ngx_http_discard_body(ngx_http_request_t *r);
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 56e99c9..3725fb9 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -4,7 +4,9 @@
 #include <ngx_event.h>
 #include <ngx_http.h>
 #include <nginx.h>
-
+#if __FreeBSD__
+#include <ngx_freebsd_init.h>
+#endif
 
 
 static void ngx_http_phase_event_handler(ngx_event_t *rev);
@@ -31,6 +33,10 @@
                                  void *conf);
 static char *ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 
+static char *ngx_http_lowat_check(ngx_conf_t *cf, void *post, void *data);
+
+static ngx_conf_post_t  ngx_http_lowat_post = { ngx_http_lowat_check } ;
+
 
 static ngx_command_t  ngx_http_core_commands[] = {
 
@@ -151,6 +157,13 @@
      offsetof(ngx_http_core_loc_conf_t, send_timeout),
      NULL},
 
+    {ngx_string("send_lowat"),
+     NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+     ngx_conf_set_size_slot,
+     NGX_HTTP_LOC_CONF_OFFSET,
+     offsetof(ngx_http_core_loc_conf_t, send_lowat),
+     &ngx_http_lowat_post},
+
     {ngx_string("keepalive_timeout"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_msec_slot,
@@ -506,6 +519,49 @@
 #endif
 
 
+ngx_table_elt_t *ngx_http_add_header(void *header,
+                                     ngx_http_header_t *http_headers)
+{
+    int               i, j;
+    char             *prev;
+    ngx_table_t      *headers;
+    ngx_table_elt_t  *h, *new;
+
+    headers = *(ngx_table_t **) header;
+
+    prev = headers->elts;
+
+    if (!(new = ngx_push_table(headers))) {
+        return NULL;
+    }
+
+    if (prev == headers->elts) {
+        return new;
+    }
+
+    h = headers->elts;
+    for (i = 0; i < headers->nelts; i++) {
+        if (h[i].key.len == 0) {
+            continue;
+        }
+
+        for (j = 0; http_headers[j].name.len != 0; j++) {
+            if (http_headers[j].name.len != h[i].key.len) {
+                continue;
+            }
+
+            if (ngx_strcasecmp(http_headers[j].name.data, h[i].key.data) == 0) {
+                *((ngx_table_elt_t **)
+                      ((char *) &header + http_headers[j].offset)) = &h[i];
+                break;
+            }
+        }
+    }
+
+    return new;
+}
+
+
 static int ngx_http_core_init(ngx_cycle_t *cycle)
 {
 #if 0
@@ -852,6 +908,7 @@
     lcf->client_body_timeout = NGX_CONF_UNSET;
     lcf->sendfile = NGX_CONF_UNSET;
     lcf->send_timeout = NGX_CONF_UNSET;
+    lcf->send_lowat = NGX_CONF_UNSET;
     lcf->discarded_buffer_size = NGX_CONF_UNSET;
     lcf->keepalive_timeout = NGX_CONF_UNSET;
     lcf->lingering_time = NGX_CONF_UNSET;
@@ -925,6 +982,7 @@
                               prev->client_body_timeout, 10000);
     ngx_conf_merge_value(conf->sendfile, prev->sendfile, 0);
     ngx_conf_merge_msec_value(conf->send_timeout, prev->send_timeout, 10000);
+    ngx_conf_merge_size_value(conf->send_lowat, prev->send_lowat, 0);
     ngx_conf_merge_size_value(conf->discarded_buffer_size,
                               prev->discarded_buffer_size, 1500);
     ngx_conf_merge_msec_value(conf->keepalive_timeout,
@@ -1063,3 +1121,29 @@
 
     return NGX_CONF_OK;
 }
+
+
+static char *ngx_http_lowat_check(ngx_conf_t *cf, void *post, void *data)
+{
+    int *np = data;
+
+#if (HAVE_LOWAT_EVENT)
+
+    if (*np >= ngx_freebsd_net_inet_tcp_sendspace) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "\"send_lowat\" must be less than %d "
+                           "(sysctl net.inet.tcp.sendspace)",
+                           ngx_freebsd_net_inet_tcp_sendspace);
+
+        return NGX_CONF_ERROR;
+    }
+
+#else
+
+    ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
+                       "\"send_lowat\" is not supported, ignored");
+
+#endif
+
+    return NGX_CONF_OK;
+}
diff --git a/src/http/ngx_http_header_filter.c b/src/http/ngx_http_header_filter.c
index e8cd5b8..e121d00 100644
--- a/src/http/ngx_http_header_filter.c
+++ b/src/http/ngx_http_header_filter.c
@@ -102,9 +102,8 @@
         r->header_only = 1;
     }
 
-    /* 9 is for "HTTP/1.x ", 2 is for trailing "\r\n"
-       and 2 is for end of header */
-    len = 9 + 2 + 2;
+    /* 2 is for trailing "\r\n" and 2 is for "\r\n" in the end of header */
+    len = sizeof("HTTP/1.x ") - 1 + 2 + 2;
 
     /* status line */
     if (r->headers_out.status_line.len) {
@@ -150,29 +149,29 @@
         len += r->headers_out.date->key.len
                + r->headers_out.date->value.len + 2;
     } else {
-        /* "Date: ... \r\n" */
-        len += 37;
+        len += sizeof("Date: Mon, 28 Sep 1970 00:00:00 GMT" CRLF) - 1;
     }
 
     if (r->headers_out.content_range && r->headers_out.content_range->value.len)
     {
-        len += 15 + r->headers_out.content_range->value.len + 2;
+        len += sizeof("Content-Range: ") - 1
+               + r->headers_out.content_range->value.len + 2;
     }
 
     if (r->headers_out.content_length == NULL) {
         if (r->headers_out.content_length_n >= 0) {
-            /* "Content-Length: ... \r\n", 2^64 is 20 characters */
-            len += 48;
+                                            /* 2^64 */
+            len += sizeof("Content-Length: 18446744073709551616" CRLF) - 1;
         }
     }
 
     if (r->headers_out.content_type && r->headers_out.content_type->value.len) {
         r->headers_out.content_type->key.len = 0;
-        len += 14 + r->headers_out.content_type->value.len + 2;
+        len += sizeof("Content-Type: ") - 1
+               + r->headers_out.content_type->value.len + 2;
 
         if (r->headers_out.charset.len) {
-            /* "; charset= ... " */
-            len += 10 + r->headers_out.charset.len;
+            len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
         }
     }
 
@@ -187,9 +186,8 @@
         && r->headers_out.location->value.data[0] == '/')
     {
         r->headers_out.location->key.len = 0;
-        /* "Location: http:// ... \r\n" */
-        len += 17 + r->server_name->len
-               + r->headers_out.location->value.len + 2;
+        len += sizeof("Location: http://") - 1,
+               + r->server_name->len + r->headers_out.location->value.len + 2;
 
         if (r->port != 80) {
             len += r->port_name->len;
@@ -201,24 +199,20 @@
                + r->headers_out.last_modified->value.len + 2;
 
     } else if (r->headers_out.last_modified_time != -1) {
-        /* "Last-Modified: ... \r\n" */
-        len += 46;
+        len += sizeof("Last-Modified: Mon, 28 Sep 1970 00:00:00 GMT" CRLF) - 1;
     }
 
     if (r->chunked) {
-        /* "Transfer-Encoding: chunked\r\n" */
-        len += 28;
+        len += sizeof("Transfer-Encoding: chunked" CRLF) - 1;
     }
 
     if (r->keepalive) {
-        /* "Connection: keep-alive\r\n" */
-        len += 24;
+        len += sizeof("Connection: keep-alive" CRLF) - 1;
     } else {
-        /* "Connection: close\r\n" */
-        len += 19;
+        len += sizeof("Connection: closed" CRLF) - 1;
     }
 
-    header = (ngx_table_elt_t *) r->headers_out.headers->elts;
+    header = r->headers_out.headers->elts;
     for (i = 0; i < r->headers_out.headers->nelts; i++) {
         if (header[i].key.len == 0) {
             continue;
@@ -230,7 +224,7 @@
     ngx_test_null(h, ngx_create_temp_hunk(r->pool, len, 0, 64), NGX_ERROR);
 
     /* "HTTP/1.x " */
-    h->last = ngx_cpymem(h->last, "HTTP/1.1 ", 9);
+    h->last = ngx_cpymem(h->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1);
 
     /* status line */
     if (r->headers_out.status_line.len) {
@@ -248,7 +242,7 @@
     }
 
     if (!(r->headers_out.date && r->headers_out.date->key.len)) {
-        h->last = ngx_cpymem(h->last, "Date: ", 6);
+        h->last = ngx_cpymem(h->last, "Date: ", sizeof("Date: ") - 1);
         h->last += ngx_http_get_time(h->last, time(NULL));
         *(h->last++) = CR; *(h->last++) = LF;
     }
@@ -256,7 +250,8 @@
 
     if (r->headers_out.content_range && r->headers_out.content_range->value.len)
     {
-        h->last = ngx_cpymem(h->last, "Content-Range: ", 15);
+        h->last = ngx_cpymem(h->last, "Content-Range: ",
+                             sizeof("Content-Range: ") - 1);
         h->last = ngx_cpymem(h->last, r->headers_out.content_range->value.data,
                              r->headers_out.content_range->value.len);
         *(h->last++) = CR; *(h->last++) = LF;
@@ -265,19 +260,22 @@
     if (r->headers_out.content_length == NULL) {
         /* 2^64 is 20 characters  */
         if (r->headers_out.content_length_n >= 0) {
-            h->last += ngx_snprintf(h->last, 49,
-                                    "Content-Length: " OFF_FMT CRLF,
-                                    r->headers_out.content_length_n);
+            h->last += ngx_snprintf(h->last,
+                            sizeof("Content-Length: 18446744073709551616" CRLF),
+                            "Content-Length: " OFF_FMT CRLF,
+                            r->headers_out.content_length_n);
         }
     }
 
     if (r->headers_out.content_type && r->headers_out.content_type->value.len) {
-        h->last = ngx_cpymem(h->last, "Content-Type: ", 14);
+        h->last = ngx_cpymem(h->last, "Content-Type: ",
+                             sizeof("Content-Type: ") - 1);
         h->last = ngx_cpymem(h->last, r->headers_out.content_type->value.data,
                              r->headers_out.content_type->value.len);
 
         if (r->headers_out.charset.len) {
-            h->last = ngx_cpymem(h->last, "; charset=", 10);
+            h->last = ngx_cpymem(h->last, "; charset=",
+                                 sizeof("; charset=") - 1);
             h->last = ngx_cpymem(h->last, r->headers_out.charset.data,
                                  r->headers_out.charset.len);
         }
@@ -288,7 +286,8 @@
     if (r->headers_out.content_encoding
         && r->headers_out.content_encoding->value.len)
     {
-        h->last = ngx_cpymem(h->last, "Content-Encoding: ", 18);
+        h->last = ngx_cpymem(h->last, "Content-Encoding: ",
+                             sizeof("Content-Encoding: ") - 1);
         h->last = ngx_cpymem(h->last,
                              r->headers_out.content_encoding->value.data,
                              r->headers_out.content_encoding->value.len);
@@ -300,7 +299,8 @@
         && r->headers_out.location->value.len
         && r->headers_out.location->value.data[0] == '/')
     {
-        h->last = ngx_cpymem(h->last, "Location: http://", 17);
+        h->last = ngx_cpymem(h->last, "Location: http://",
+                             sizeof("Location: http://") - 1);
         h->last = ngx_cpymem(h->last, r->server_name->data,
                              r->server_name->len);
         if (r->port != 80) {
@@ -317,21 +317,25 @@
     if (!(r->headers_out.last_modified && r->headers_out.last_modified->key.len)
         && r->headers_out.last_modified_time != -1)
     {
-        h->last = ngx_cpymem(h->last, "Last-Modified: ", 15);
+        h->last = ngx_cpymem(h->last, "Last-Modified: ",
+                             sizeof("Last-Modified: ") - 1);
         h->last += ngx_http_get_time(h->last,
-                                         r->headers_out.last_modified_time);
+                                            r->headers_out.last_modified_time);
         *(h->last++) = CR; *(h->last++) = LF;
     }
 
     if (r->chunked) {
-        h->last = ngx_cpymem(h->last, "Transfer-Encoding: chunked" CRLF, 28);
+        h->last = ngx_cpymem(h->last, "Transfer-Encoding: chunked" CRLF,
+                             sizeof("Transfer-Encoding: chunked" CRLF) - 1);
     }
 
     if (r->keepalive) {
-        h->last = ngx_cpymem(h->last, "Connection: keep-alive" CRLF, 24);
+        h->last = ngx_cpymem(h->last, "Connection: keep-alive" CRLF,
+                             sizeof("Connection: keep-alive" CRLF) - 1);
 
     } else {
-        h->last = ngx_cpymem(h->last, "Connection: close" CRLF, 19);
+        h->last = ngx_cpymem(h->last, "Connection: close" CRLF,
+                             sizeof("Connection: close" CRLF) - 1);
     }
 
     for (i = 0; i < r->headers_out.headers->nelts; i++) {
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index e3a4952..87a0799 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -44,7 +44,7 @@
 
 
 
-static ngx_http_header_t headers_in[] = {
+ngx_http_header_t ngx_http_headers_in[] = {
     { ngx_string("Host"), offsetof(ngx_http_headers_in_t, host) },
     { ngx_string("Connection"), offsetof(ngx_http_headers_in_t, connection) },
     { ngx_string("If-Modified-Since"),
@@ -66,6 +66,27 @@
 };
 
 
+ngx_http_header_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) },
+    { ngx_string("Content-Type"),
+                             offsetof(ngx_http_headers_out_t, content_type) },
+    { ngx_string("Content-Length"),
+                           offsetof(ngx_http_headers_out_t, content_length) },
+    { ngx_string("Content-Encoding"),
+                         offsetof(ngx_http_headers_out_t, content_encoding) },
+
+    /* Location */
+
+    { ngx_string("Last-Modified"),
+                            offsetof(ngx_http_headers_out_t, last_modified) },
+    { ngx_string("Accept-Ranges"),
+                            offsetof(ngx_http_headers_out_t, accept_ranges) },
+
+    { ngx_null_string, 0 }
+};
+
+
 static void ngx_http_dummy(ngx_event_t *wev)
 {
     return;
@@ -106,20 +127,14 @@
     rev->event_handler = ngx_http_init_request;
 
     if (rev->ready) {
-        /* deferred accept */
+        /* deferred accept, aio, iocp, epoll */
         ngx_http_init_request(rev);
         return;
     }
 
     ngx_add_timer(rev, c->listening->post_accept_timeout);
 
-    if (ngx_event_flags & (NGX_USE_AIO_EVENT|NGX_USE_EDGE_EVENT)) {
-        /* aio, iocp, epoll */
-        ngx_http_init_request(rev);
-        return;
-    }
-
-    if (ngx_handle_read_event(rev) == NGX_ERROR) {
+    if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
         ngx_http_close_connection(c);
         return;
     }
@@ -256,7 +271,7 @@
         return;
     }
 
-    r->headers_out.headers = ngx_create_table(r->pool, 10);
+    r->headers_out.headers = ngx_create_table(r->pool, 1);
     if (r->headers_out.headers == NULL) {
         ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
         ngx_http_close_connection(c);
@@ -451,7 +466,7 @@
         lctx = c->log->data;
         lctx->action = "reading client request headers";
         lctx->url = r->unparsed_uri.data;
-        r->headers_in.headers = ngx_create_table(r->pool, 10);
+        r->headers_in.headers = ngx_create_table(r->pool, 1);
 
         if (cscf->large_client_header
             && r->header_in->pos == r->header_in->last)
@@ -560,8 +575,8 @@
 
             /* a header line has been parsed successfully */
 
-            h = ngx_push_table(r->headers_in.headers);
-            if (h == NULL) {
+            if (!(h = ngx_http_add_header(&r->headers_in, ngx_http_headers_in)))
+            {
                 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
                 ngx_http_close_connection(c);
                 return;
@@ -593,14 +608,16 @@
                 h->value.data[h->value.len] = '\0';
             }
 
-            for (i = 0; headers_in[i].name.len != 0; i++) {
-                if (headers_in[i].name.len != h->key.len) {
+            for (i = 0; ngx_http_headers_in[i].name.len != 0; i++) {
+                if (ngx_http_headers_in[i].name.len != h->key.len) {
                     continue;
                 }
 
-                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;
+                if (ngx_strcasecmp(ngx_http_headers_in[i].name.data,
+                                   h->key.data) == 0)
+                {
+                    *((ngx_table_elt_t **) ((char *) &r->headers_in
+                                         + ngx_http_headers_in[i].offset)) = h;
                     break;
                 }
             }
@@ -692,10 +709,6 @@
     n = r->header_in->last - r->header_in->pos;
 
     if (n > 0) {
-#if 0
-        /* TODO: THINK - AIO ??? */
-        rev->ready = 0;
-#endif
         return n;
     }
 
@@ -709,7 +722,7 @@
             r->header_timeout_set = 1;
         }
 
-        if (ngx_handle_read_event(rev) == NGX_ERROR) {
+        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;
@@ -1157,7 +1170,7 @@
         c->tcp_nopush = 0;
     }
 
-    if (rev->ready || (ngx_event_flags & NGX_USE_AIO_EVENT)) {
+    if (rev->ready) {
         ngx_http_keepalive_handler(rev);
     }
 }
@@ -1256,7 +1269,7 @@
         return;
     }
 
-    if (rev->ready || (ngx_event_flags & NGX_USE_AIO_EVENT)) {
+    if (rev->ready) {
         ngx_http_lingering_close_handler(rev);
     }
 }
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h
index a05b97c..5c7ffb7 100644
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -68,6 +68,8 @@
 
 
 typedef struct {
+    ngx_table_t      *headers;   /* it must be first field */
+
     ngx_table_elt_t  *host;
     ngx_table_elt_t  *connection;
     ngx_table_elt_t  *if_modified_since;
@@ -83,8 +85,6 @@
     ssize_t           content_length_n;
     size_t            connection_type;
     ssize_t           keep_alive_n;
-
-    ngx_table_t      *headers;
 } ngx_http_headers_in_t;
 
 
@@ -108,6 +108,8 @@
 
 
 typedef struct {
+    ngx_table_t      *headers;   /* it must be first field */
+
     int               status;
     ngx_str_t         status_line;
 
@@ -124,8 +126,6 @@
     ngx_str_t         charset;
     ngx_array_t       ranges;
 
-    ngx_table_t      *headers;
-
     off_t             content_length_n;
     char             *etag;
     time_t            date_time;
@@ -231,4 +231,9 @@
 };
 
 
+extern ngx_http_header_t ngx_http_headers_in[];
+extern ngx_http_header_t ngx_http_headers_out[];
+
+
+
 #endif /* _NGX_HTTP_REQUEST_H_INCLUDED_ */
diff --git a/src/http/ngx_http_request_body.c b/src/http/ngx_http_request_body.c
index 1c9cd97..19730e7 100644
--- a/src/http/ngx_http_request_body.c
+++ b/src/http/ngx_http_request_body.c
@@ -112,7 +112,7 @@
         clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
         ngx_add_timer(rev, clcf->client_body_timeout);
 
-        if (ngx_handle_read_event(rev) == NGX_ERROR) {
+        if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
             ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
         }
 
diff --git a/src/http/ngx_http_special_response.c b/src/http/ngx_http_special_response.c
index c1a37f1..8f9d8ce 100644
--- a/src/http/ngx_http_special_response.c
+++ b/src/http/ngx_http_special_response.c
@@ -218,9 +218,11 @@
                                           + sizeof(error_tail) - 1
                                           + sizeof(msie_stub) - 1;
 
-        ngx_test_null(r->headers_out.content_type,
-                      ngx_push_table(r->headers_out.headers),
-                      NGX_HTTP_INTERNAL_SERVER_ERROR);
+        if (!(r->headers_out.content_type =
+                   ngx_http_add_header(&r->headers_out, ngx_http_headers_out)))
+        {
+            return NGX_ERROR;
+        }
 
         r->headers_out.content_type->key.len = 12;
         r->headers_out.content_type->key.data = "Content-Type";
@@ -245,7 +247,9 @@
     out = NULL;
     ll = NULL;
 
-    ngx_test_null(h, ngx_calloc_hunk(r->pool), NGX_ERROR);
+    if (!(h = ngx_calloc_hunk(r->pool))) {
+        return NGX_ERROR;
+    }
     h->type = NGX_HUNK_MEMORY|NGX_HUNK_IN_MEMORY;
     h->pos = error_pages[err].data;
     h->last = error_pages[err].data + error_pages[err].len;
@@ -254,7 +258,9 @@
     ngx_chain_add_link(out, ll, cl);
 
 
-    ngx_test_null(h, ngx_calloc_hunk(r->pool), NGX_ERROR);
+    if (!(h = ngx_calloc_hunk(r->pool))) {
+        return NGX_ERROR;
+    }
     h->type = NGX_HUNK_MEMORY|NGX_HUNK_IN_MEMORY;
     h->pos = error_tail;
     h->last = error_tail + sizeof(error_tail) - 1;
@@ -270,7 +276,9 @@
         && error != NGX_HTTP_REQUEST_URI_TOO_LARGE
        )
     {
-        ngx_test_null(h, ngx_calloc_hunk(r->pool), NGX_ERROR);
+        if (!(h = ngx_calloc_hunk(r->pool))) {
+            return NGX_ERROR;
+        }
         h->type = NGX_HUNK_MEMORY|NGX_HUNK_IN_MEMORY;
         h->pos = msie_stub;
         h->last = msie_stub + sizeof(msie_stub) - 1;