nginx-0.0.1-2003-02-06-20:21:13 import
diff --git a/src/http/modules/ngx_http_event_proxy_handler.c b/src/http/modules/ngx_http_event_proxy_handler.c
index c4594e6..195e4b3 100644
--- a/src/http/modules/ngx_http_event_proxy_handler.c
+++ b/src/http/modules/ngx_http_event_proxy_handler.c
@@ -6,6 +6,7 @@
 #include <ngx_hunk.h>
 #include <ngx_event_write.h>
 #include <ngx_http.h>
+#include <ngx_http_output_filter.h>
 #include <ngx_http_event_proxy_handler.h>
 
 ngx_http_module_t  ngx_http_proxy_module_ctx;
@@ -30,6 +31,9 @@
 static char conn_close[] = "Connection: close" CRLF;
 
 
+/* AF_INET only */
+
+
 int ngx_http_proxy_handler(ngx_http_request_t *r)
 {
     struct sockaddr_in     addr;
@@ -56,7 +60,7 @@
     addr.sin_addr.s_addr = inet_addr("127.0.0.1");
     addr.sin_port = htons(9000);
 
-    ngx_http_proxy_connect(r, &addr, "connecting to 127.0.0.1:9000");
+    return ngx_http_proxy_connect(r, &addr, "connecting to 127.0.0.1:9000");
 }
 
 
@@ -151,7 +155,7 @@
 
     s = ngx_socket(AF_INET, SOCK_STREAM, IPPROTO_IP, 0);
     if (s == -1) {
-        ngx_log_error(NGX_LOG_ERR, c->log, ngx_socket_errno,
+        ngx_log_error(NGX_LOG_ALERT, c->log, ngx_socket_errno,
                       ngx_socket_n " failed");
         return NGX_ERROR;
     }
@@ -164,7 +168,7 @@
                           "setsockopt(SO_RCVBUF) failed");
 
             if (ngx_close_socket(s) == -1) {
-                ngx_log_error(NGX_LOG_ERR, c->log, ngx_socket_errno,
+                ngx_log_error(NGX_LOG_ALERT, c->log, ngx_socket_errno,
                               ngx_close_socket_n " failed");
             }
 
@@ -178,7 +182,7 @@
                       ngx_nonblocking_n " failed");
 
         if (ngx_close_socket(s) == -1) {
-            ngx_log_error(NGX_LOG_ERR, c->log, ngx_socket_errno,
+            ngx_log_error(NGX_LOG_ALERT, c->log, ngx_socket_errno,
                           ngx_close_socket_n " failed");
         }
 
@@ -190,10 +194,10 @@
     if (rc == -1) {
         err = ngx_socket_errno;
         if (err != NGX_EINPROGRESS) {
-            ngx_log_error(NGX_LOG_ERR, c->log, err, "connect() failed");
+            ngx_log_error(NGX_LOG_CRIT, c->log, err, "connect() failed");
 
             if (ngx_close_socket(s) == -1) {
-                ngx_log_error(NGX_LOG_ERR, c->log, ngx_socket_errno,
+                ngx_log_error(NGX_LOG_ALERT, c->log, ngx_socket_errno,
                               ngx_close_socket_n " failed");
             }
 
@@ -271,7 +275,7 @@
 
 static int ngx_http_proxy_read_response_header(ngx_event_t *ev)
 {
-    int  n, rc;
+    int                    n;
     ngx_hunk_t           **ph;
     ngx_connection_t      *c;
     ngx_http_request_t    *r;
@@ -298,13 +302,12 @@
                       ngx_palloc(r->pool, sizeof(ngx_http_proxy_headers_in_t)),
                       NGX_ERROR);
 
-        ngx_test_null(p->hunks,
-                      ngx_create_array(r->pool,
-                                       /* STUB */ 10 /**/,
-                                       sizeof(ngx_hunk_t *)),
-                      NGX_ERROR);
+        ngx_init_array(p->hunks, r->pool,
+                       /* STUB */ 10 /**/,
+                       sizeof(ngx_hunk_t *),
+                       NGX_ERROR);
 
-        ngx_test_null(ph, ngx_push_array(p->hunks), NGX_ERROR);
+        ngx_test_null(ph, ngx_push_array(&p->hunks), NGX_ERROR);
         *ph = p->header_in;
 
         p->state_handler = ngx_http_proxy_process_status_line;
@@ -387,51 +390,64 @@
     /* STUB */ return NGX_ERROR;
 }
 
+#if 0
 static int ngx_http_proxy_process_response_header(ngx_http_request_t *r,
                                                   ngx_http_proxy_ctx_t *p)
 {
+    return NGX_OK;
 }
+#endif
 
 static int ngx_http_proxy_read_response_body(ngx_event_t *ev)
 {
-    int     n;
-    size_t  left;
+    int                    n;
+    char                  *buf;
+    size_t                 left, size;
     ngx_hunk_t            *h, **ph;
     ngx_connection_t      *c;
     ngx_http_request_t    *r;
     ngx_http_proxy_ctx_t  *p;
 
-    if (ev->timedout)
+    if (ev->timedout) {
         return NGX_ERROR;
+    }
 
     c = (ngx_connection_t *) ev->data;
     r = (ngx_http_request_t *) c->data;
     p = (ngx_http_proxy_ctx_t *)
                          ngx_http_get_module_ctx(r, ngx_http_proxy_module_ctx);
 
-    left = 0;
-
-    if (p->hunks->nelts > 0) {
-        h = ((ngx_hunk_t **) p->hunks->elts)[p->hunks->nelts - 1];
+    if (p->hunks.nelts > 0) {
+        h = ((ngx_hunk_t **) p->hunks.elts)[p->hunks.nelts - 1];
         left = h->end - h->last.mem;
+
+    } else {
+        h = NULL;
+        left = 0;
     }
 
     do {
 
-#if (HAVE_KQUEUE)
-#if !(USE_KQUEUE)
+#if (USE_KQUEUE)
+
+        /* do not allocate new block if there is EOF */
+        if (ev->eof && ev->available == 0) {
+            left = 1;
+        }
+
+#elif (HAVE_KQUEUE)
+
         if (ngx_event_type == NGX_KQUEUE_EVENT) {
-#endif
             /* do not allocate new block if there is EOF */
             if (ev->eof && ev->available == 0) {
                 left = 1;
             }
-#if !(USE_KQUEUE)
         }
+
 #endif
-#endif
+
         if (left == 0) {
-            ngx_test_null(ph, ngx_push_array(p->hunks), NGX_ERROR);
+            ngx_test_null(ph, ngx_push_array(&p->hunks), NGX_ERROR);
             ngx_test_null(h,
                           ngx_create_temp_hunk(r->pool,
                                                /* STUB */ 4096 /**/, 0, 0),
@@ -441,7 +457,16 @@
             *ph = h;
         }
 
-        n = ngx_event_recv(c, h->last.mem, h->end - h->last.mem);
+        if (h != NULL) {
+            buf = h->last.mem;
+            size = h->end - h->last.mem;
+
+        } else {
+            buf = (char *) &buf;
+            size = 0;
+        }
+
+        n = ngx_event_recv(c, buf, size);
 
         ngx_log_debug(c->log, "READ:%d" _ n);
 
@@ -492,14 +517,14 @@
                          ngx_http_get_module_ctx(r, ngx_http_proxy_module_ctx);
 
     do {
-        h = ((ngx_hunk_t **) p->hunks->elts)[p->hunk_n];
+        h = ((ngx_hunk_t **) p->hunks.elts)[p->hunk_n];
 
         rc = ngx_http_output_filter(r, h);
         if (rc != NGX_OK) {
             return rc;
         }
 
-        if (p->hunk_n >= p->hunks->nelts) {
+        if (p->hunk_n >= p->hunks.nelts) {
             break;
         }
 
@@ -534,8 +559,10 @@
     while (p < ctx->header_in->last.mem && state < sw_done) {
         ch = *p++;
 
+#if 0
 fprintf(stderr, "state: %d, pos: %x, end: %x, char: '%c', status: %d\n",
-       state, p, ctx->header_in->last.mem, ch, ctx->status);
+        state, p, ctx->header_in->last.mem, ch, ctx->status);
+#endif
 
         switch (state) {
 
diff --git a/src/http/modules/ngx_http_event_proxy_handler.h b/src/http/modules/ngx_http_event_proxy_handler.h
index 596a193..b5b480a 100644
--- a/src/http/modules/ngx_http_event_proxy_handler.h
+++ b/src/http/modules/ngx_http_event_proxy_handler.h
@@ -20,7 +20,7 @@
     ngx_chain_t  *out;
 
     int           last_hunk;
-    ngx_array_t  *hunks;
+    ngx_array_t   hunks;
 
     int           hunk_n;
 
diff --git a/src/http/modules/ngx_http_index_handler.c b/src/http/modules/ngx_http_index_handler.c
index d3b4901..ebbeac9 100644
--- a/src/http/modules/ngx_http_index_handler.c
+++ b/src/http/modules/ngx_http_index_handler.c
@@ -244,7 +244,9 @@
 /* STUB */
 static char *ngx_http_index_merge_conf(ngx_pool_t *p, void *parent, void *child)
 {
+#if 0
     ngx_http_index_conf_t *prev = (ngx_http_index_conf_t *) parent;
+#endif
     ngx_http_index_conf_t *conf = (ngx_http_index_conf_t *) child;
     ngx_str_t  *index;
 
diff --git a/src/http/modules/ngx_http_static_handler.c b/src/http/modules/ngx_http_static_handler.c
index c55f36a..fea2ee0 100644
--- a/src/http/modules/ngx_http_static_handler.c
+++ b/src/http/modules/ngx_http_static_handler.c
@@ -1,9 +1,11 @@
 
 #include <ngx_config.h>
+
 #include <ngx_string.h>
 #include <ngx_file.h>
 #include <ngx_hunk.h>
 #include <ngx_http.h>
+#include <ngx_http_output_filter.h>
 
 ngx_http_module_t  ngx_http_static_module;
 
diff --git a/src/http/ngx_http.c b/src/http/ngx_http.c
index 5396fa2..4675325 100644
--- a/src/http/ngx_http.c
+++ b/src/http/ngx_http.c
@@ -10,6 +10,7 @@
 #include <ngx_http_core_module.h>
 
 
+static void ngx_http_init_filters(ngx_pool_t *pool, ngx_module_t **modules);
 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy);
 
 
@@ -59,6 +60,39 @@
 };
 
 
+
+static void ngx_http_init_filters(ngx_pool_t *pool, ngx_module_t **modules)
+{
+    int  i;
+    ngx_http_module_t  *module;
+    int (*ohf)(ngx_http_request_t *r);
+    int (*obf)(ngx_http_request_t *r, ngx_chain_t *ch);
+
+    ohf = NULL;
+    obf = NULL;
+
+    for (i = 0; modules[i]; i++) {
+        if (modules[i]->type != NGX_HTTP_MODULE_TYPE) {
+            continue;
+        }
+
+        module = (ngx_http_module_t *) modules[i]->ctx;
+
+        if (module->output_header_filter) {
+            module->next_output_header_filter = ohf;
+            ohf = module->output_header_filter;
+        }
+
+        if (module->output_body_filter) {
+            module->next_output_body_filter = obf;
+            obf = module->output_body_filter;
+        }
+    }
+
+    ngx_http_top_header_filter = ohf;
+}
+
+
 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy)
 {
     int                         i, s, l, p, a, n, start;
@@ -442,9 +476,13 @@
     ls->family = AF_INET;
     ls->type = SOCK_STREAM;
     ls->protocol = IPPROTO_IP;
+
 #if (NGX_OVERLAPPED)
     ls->flags = WSA_FLAG_OVERLAPPED;
+#else
+    ls->nonblocking = 1;
 #endif
+
     ls->sockaddr = (struct sockaddr *) &addr;
     ls->socklen = sizeof(struct sockaddr_in);
     ls->addr = offsetof(struct sockaddr_in, sin_addr);
@@ -452,7 +490,6 @@
     ls->addr_text.data = addr_text;
     ls->backlog = -1;
     ls->post_accept_timeout = 10000;
-    ls->nonblocking = 1;
 
     ls->handler = ngx_http_init_connection;
     ls->server = &ngx_http_server;
diff --git a/src/http/ngx_http.h b/src/http/ngx_http.h
index ca2d5c1..2966576 100644
--- a/src/http/ngx_http.h
+++ b/src/http/ngx_http.h
@@ -67,14 +67,14 @@
 
 
 typedef struct {
-    int    len;
-    char  *data;
-    int    offset;
+    size_t  len;
+    char   *data;
+    int     offset;
 } ngx_http_header_t;
 
 
 typedef struct {
-    int               host_name_len;
+    size_t            host_name_len;
 
     ngx_table_elt_t  *host;
     ngx_table_elt_t  *connection;
@@ -240,12 +240,22 @@
 /**/
 
 int ngx_http_init_connection(ngx_connection_t *c);
+int ngx_read_http_request_line(ngx_http_request_t *r);
+int ngx_read_http_header_line(ngx_http_request_t *r, ngx_hunk_t *h);
+int ngx_http_handler(ngx_http_request_t *r);
+
+
+int ngx_http_send_header(ngx_http_request_t *r);
+int ngx_http_special_response(ngx_http_request_t *r, int error);
+
+
+time_t ngx_http_parse_time(char *value, size_t len);
+size_t ngx_http_get_time(char *buf, time_t t);
 
 
 int ngx_http_discard_body(ngx_http_request_t *r);
 
 
-int ngx_http_special_response(ngx_http_request_t *r, int error);
 
 
 extern int  ngx_max_module;
@@ -269,5 +279,9 @@
 extern ngx_http_module_t  *ngx_http_modules[];
 
 
+/* STUB */
+int ngx_http_log_handler(ngx_http_request_t *r);
+/**/
+
 
 #endif /* _NGX_HTTP_H_INCLUDED_ */
diff --git a/src/http/ngx_http_config.h b/src/http/ngx_http_config.h
index 27f8c65..6793208 100644
--- a/src/http/ngx_http_config.h
+++ b/src/http/ngx_http_config.h
@@ -32,4 +32,4 @@
 extern void **ngx_loc_conf;
 
 
-#endif _NGX_HTTP_CONFIG_H_INCLUDED_
+#endif /* _NGX_HTTP_CONFIG_H_INCLUDED_ */
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 1742b7c..2f33b57 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -10,13 +10,9 @@
 #include <ngx_http_config.h>
 #include <ngx_http_core_module.h>
 
-#if 0
-#include <ngx_http_write_filter.h>
+/* STUB for r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; */
 #include <ngx_http_output_filter.h>
-#endif
 
-/* STUB */
-#include <ngx_http_output_filter.h>
 int ngx_http_static_handler(ngx_http_request_t *r);
 int ngx_http_proxy_handler(ngx_http_request_t *r);
 /**/
@@ -104,8 +100,13 @@
     ngx_http_server_name_t  *name;
 
     r->connection->unexpected_eof = 0;
-    r->lingering_close = 1;
-    r->keepalive = 1;
+
+    r->lingering_close = 0;
+    r->keepalive = 0;
+
+#if 0
+    r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY;
+#endif
 
 ngx_log_debug(r->connection->log, "servers: %0x" _ r->connection->servers);
 
@@ -163,12 +164,6 @@
 ngx_log_debug(r->connection->log, "srv_conf: %0x" _ r->srv_conf);
 ngx_log_debug(r->connection->log, "loc_conf: %0x" _ r->loc_conf);
 
-
-#if 1
-    r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY;
-#endif
-
-
     /* run translation phase */
     for (i = 0; ngx_modules[i]; i++) {
         if (ngx_modules[i]->type != NGX_HTTP_MODULE_TYPE) {
@@ -496,8 +491,13 @@
 
     ngx_log_debug(r->connection->log, "http close");
 
-    ngx_del_timer(r->connection->read);
-    ngx_del_timer(r->connection->write);
+    if (r->connection->read->timer_set) {
+        ngx_del_timer(r->connection->read);
+    }
+
+    if (r->connection->write->timer_set) {
+        ngx_del_timer(r->connection->write);
+    }
 
     return NGX_DONE;
 }
@@ -669,73 +669,6 @@
 }
 
 
-int ngx_http_config_modules(ngx_pool_t *pool, ngx_module_t **modules)
-{
-    int i;
-    ngx_http_module_t  *module;
-
-    for (i = 0; modules[i]; i++) {
-        if (modules[i]->type != NGX_HTTP_MODULE_TYPE) {
-            continue;
-        }
-
-        module = (ngx_http_module_t *) modules[i]->ctx;
-        module->index = i;
-    }
-
-    ngx_http_max_module = i;
-
-#if 0
-    ngx_test_null(ngx_srv_conf,
-                  ngx_pcalloc(pool, sizeof(void *) * ngx_http_max_module),
-                  NGX_ERROR);
-    ngx_test_null(ngx_loc_conf,
-                  ngx_pcalloc(pool, sizeof(void *) * ngx_http_max_module),
-                  NGX_ERROR);
-
-    for (i = 0; modules[i]; i++) {
-        if (modules[i]->create_srv_conf)
-            ngx_srv_conf[i] = modules[i]->create_srv_conf(pool);
-
-        if (modules[i]->create_loc_conf)
-            ngx_loc_conf[i] = modules[i]->create_loc_conf(pool);
-    }
-#endif
-}
-
-
-void ngx_http_init_filters(ngx_pool_t *pool, ngx_module_t **modules)
-{
-    int  i;
-    ngx_http_module_t  *module;
-    int (*ohf)(ngx_http_request_t *r);
-    int (*obf)(ngx_http_request_t *r, ngx_chain_t *ch);
-
-    ohf = NULL;
-    obf = NULL;
-
-    for (i = 0; modules[i]; i++) {
-        if (modules[i]->type != NGX_HTTP_MODULE_TYPE) {
-            continue;
-        }
-
-        module = (ngx_http_module_t *) modules[i]->ctx;
-
-        if (module->output_header_filter) {
-            module->next_output_header_filter = ohf;
-            ohf = module->output_header_filter;
-        }
-
-        if (module->output_body_filter) {
-            module->next_output_body_filter = obf;
-            obf = module->output_body_filter;
-        }
-    }
-
-    ngx_http_top_header_filter = ohf;
-}
-
-
 static void *ngx_http_core_create_srv_conf(ngx_pool_t *pool)
 {
     ngx_http_core_srv_conf_t *scf, **cf;
diff --git a/src/http/ngx_http_core_module.h b/src/http/ngx_http_core_module.h
index c4d15ca..c53c815 100644
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -8,7 +8,7 @@
 
 
 typedef struct {
-    int        addr;
+    u_int32_t  addr;
     int        port;
     int        family;
     int        flags;             /* 'default' */
@@ -85,6 +85,7 @@
 
 int ngx_http_core_translate_handler(ngx_http_request_t *r);
 
+int ngx_http_internal_redirect(ngx_http_request_t *r, ngx_str_t uri);
 int ngx_http_error(ngx_http_request_t *r, int error);
 int ngx_http_close_request(ngx_http_request_t *r);
 
diff --git a/src/http/ngx_http_event.c b/src/http/ngx_http_event.c
index 6f7134a..ea5b322 100644
--- a/src/http/ngx_http_event.c
+++ b/src/http/ngx_http_event.c
@@ -15,10 +15,9 @@
 #include <ngx_http.h>
 #include <ngx_http_config.h>
 #include <ngx_http_core_module.h>
+#include <ngx_http_output_filter.h>
 
 
-int ngx_http_init_connection(ngx_connection_t *c);
-
 static int ngx_http_init_request(ngx_event_t *ev);
 static int ngx_http_process_request_header(ngx_event_t *ev);
 
@@ -89,9 +88,11 @@
     c->log->handler = ngx_http_log_error;
 
 #if (HAVE_DEFERRED_ACCEPT)
+
     if (ev->ready) {
         return ngx_http_init_request(ev);
     }
+
 #endif
 
     ngx_add_timer(ev, c->post_accept_timeout);
@@ -103,26 +104,34 @@
 
 #else
 
-#if (HAVE_CLEAR_EVENT)
+#if (HAVE_CLEAR_EVENT) /* kqueue */
+
     if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) {
         return ngx_add_event(ev, NGX_READ_EVENT, NGX_CLEAR_EVENT);
     }
+
 #endif
 
-#if (HAVE_EDGE_EVENT)
+#if (HAVE_EDGE_EVENT) /* epoll */
+
     if (ngx_event_flags & NGX_HAVE_EDGE_EVENT) {
         if (ngx_add_event(ev, NGX_READ_EVENT, NGX_EDGE_EVENT) == NGX_ERROR) {
             return NGX_ERROR;
         }
         return ngx_http_init_request(ev);
     }
+
 #endif
 
-#if (HAVE_AIO_EVENT)
+#if (HAVE_AIO_EVENT) /* aio, iocp */
+
     if (ngx_event_flags & NGX_HAVE_AIO_EVENT) {
         return ngx_http_init_request(ev);
     }
-#endif
+
+#endif /* HAVE_AIO_EVENT */
+
+    /* select, poll, /dev/poll */
 
     return ngx_add_event(ev, NGX_READ_EVENT, NGX_LEVEL_EVENT);
 
@@ -182,7 +191,7 @@
 
 static int ngx_http_process_request_header(ngx_event_t *ev)
 {
-    int  n, rc;
+    int                  n, rc;
     ngx_connection_t    *c;
     ngx_http_request_t  *r;
 
@@ -191,55 +200,64 @@
 
     ngx_log_debug(ev->log, "http process request");
 
-    if (r->header_read) {
-        r->header_read = 0;
-        ngx_log_debug(ev->log, "http preread %d" _
-                      r->header_in->last.mem - r->header_in->pos.mem);
-
-    } else {
-        n = ngx_event_recv(c, r->header_in->last.mem,
-                           r->header_in->end - r->header_in->last.mem);
-
-        if (n == NGX_AGAIN) {
-            if (!r->header_timeout_set) {
-
-                if (ev->timer_set) {
-                    ngx_del_timer(ev);
-                } else {
-                    ev->timer_set = 1;
-                }
-
-                ngx_add_timer(ev, ngx_http_client_header_timeout);
-                r->header_timeout_set = 1;
-            }
-            return NGX_AGAIN;
-        }
-
-        if (n == NGX_ERROR)
-            return ngx_http_close_request(r);
-
-        ngx_log_debug(ev->log, "http read %d" _ n);
-
-        if (n == 0) {
-            ngx_log_error(NGX_LOG_INFO, c->log, 0,
-                          "client has prematurely closed connection");
-            return ngx_http_close_request(r);
-        }
-
-        r->header_in->last.mem += n;
-    }
-
-    /* state_handlers are called in following order:
-        ngx_http_process_request_line(r)
-        ngx_http_process_request_headers(r) */
-
+#if (HAVE_AIO_EVENT)
     do {
-        rc = (r->state_handler)(r);
+#endif
 
-        if (rc == NGX_ERROR)
-            return rc;
+        if (r->header_read) {
+            r->header_read = 0;
+            ngx_log_debug(ev->log, "http preread %d" _
+                          r->header_in->last.mem - r->header_in->pos.mem);
 
-    } while (rc == NGX_AGAIN && r->header_in->pos.mem < r->header_in->last.mem);
+        } else {
+            n = ngx_event_recv(c, r->header_in->last.mem,
+                               r->header_in->end - r->header_in->last.mem);
+
+            if (n == NGX_AGAIN) {
+                if (!r->header_timeout_set) {
+
+                    if (ev->timer_set) {
+                        ngx_del_timer(ev);
+                    } else {
+                        ev->timer_set = 1;
+                    }
+
+                    ngx_add_timer(ev, ngx_http_client_header_timeout);
+                    r->header_timeout_set = 1;
+                }
+                return NGX_AGAIN;
+            }
+
+            if (n == NGX_ERROR)
+                return ngx_http_close_request(r);
+
+            ngx_log_debug(ev->log, "http read %d" _ n);
+
+            if (n == 0) {
+                ngx_log_error(NGX_LOG_INFO, c->log, 0,
+                              "client has prematurely closed connection");
+                return ngx_http_close_request(r);
+            }
+
+            r->header_in->last.mem += n;
+        }
+
+        /* state_handlers are called in following order:
+            ngx_http_process_request_line(r)
+            ngx_http_process_request_headers(r) */
+
+        do {
+            rc = (r->state_handler)(r);
+
+            if (rc == NGX_ERROR)
+                return rc;
+
+        } while (rc == NGX_AGAIN
+                 && r->header_in->pos.mem < r->header_in->last.mem);
+
+#if (HAVE_AIO_EVENT) /* aio, iocp */
+    } while (rc == NGX_AGAIN && ngx_event_flags & NGX_HAVE_AIO_EVENT);
+#endif
 
     if (rc == NGX_OK) {
         /* HTTP header done */
@@ -345,7 +363,8 @@
 
 static int ngx_http_process_request_headers(ngx_http_request_t *r)
 {
-    int                  rc, len;
+    int                  rc;
+    size_t               len;
     ngx_http_log_ctx_t  *ctx;
 
     for ( ;; ) {
@@ -432,7 +451,7 @@
 
 static int ngx_http_event_request_handler(ngx_http_request_t *r)
 {
-    int           rc;
+    int           rc, event;
     ngx_msec_t    timeout;
     ngx_event_t  *rev, *wev;
 
@@ -453,20 +472,11 @@
     if (rc == NGX_WAITING)
         return rc;
 
-    /* handler has done its work but transfer is not completed */
+    /* handler has done its work but transfer is still not completed */
     if (rc == NGX_AGAIN) {
-#if (HAVE_CLEAR_EVENT)
-        if (ngx_add_event(wev, NGX_WRITE_EVENT,
-                          NGX_CLEAR_EVENT) == NGX_ERROR) {
-#else
-        if (ngx_add_event(wev, NGX_WRITE_EVENT,
-                          NGX_ONESHOT_EVENT) == NGX_ERROR) {
-#endif
-            return ngx_http_close_request(r);
-        }
 
         if (r->connection->sent > 0) {
-            ngx_log_debug(r->connection->log, "sent: " QD_FMT _
+            ngx_log_debug(r->connection->log, "sent: " OFF_FMT _
                           r->connection->sent);
             timeout = (ngx_msec_t) (r->connection->sent * 10);
             ngx_log_debug(r->connection->log, "timeout: %d" _ timeout);
@@ -477,7 +487,67 @@
         }
 
         wev->event_handler = ngx_http_writer;
+
+#if (USE_KQUEUE)
+
+        if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_CLEAR_EVENT) == NGX_ERROR) {
+            return ngx_http_close_request(r);
+        }
+
         return rc;
+
+#else
+
+#if (HAVE_AIO_EVENT) /* aio, iocp */
+
+        if (ngx_event_flags & NGX_HAVE_AIO_EVENT) {
+            return rc;
+        }
+
+#endif
+
+#if (HAVE_CLEAR_EVENT) /* kqueue */
+
+        if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) {
+            event = NGX_CLEAR_EVENT;
+
+        } else {
+            event = NGX_ONESHOT_EVENT;
+        }
+
+#elif (HAVE_EDGE_EVENT) /* epoll */
+
+        if (ngx_event_flags & NGX_HAVE_EDGE_EVENT) {
+            event = NGX_EDGE_EVENT;
+
+        } else {
+            event = NGX_ONESHOT_EVENT;
+        }
+
+#elif (HAVE_DEVPOLL_EVENT) /* /dev/poll */
+
+        if (ngx_event_flags & NGX_HAVE_LEVEL_EVENT) {
+            event = NGX_LEVEL_EVENT;
+
+        } else {
+            event = NGX_ONESHOT_EVENT;
+        }
+
+#else /* select, poll */
+
+        event = NGX_ONESHOT_EVENT;
+
+#endif
+
+        if (ngx_add_event(wev, NGX_WRITE_EVENT, event) == NGX_ERROR) {
+            return ngx_http_close_request(r);
+        }
+
+        return rc;
+
+
+#endif /* USE_KQUEUE */
+
     }
 
     if (rc == NGX_ERROR) {
@@ -501,10 +571,13 @@
 
     /* keepalive */
 
-    ngx_http_close_request(r);
     r->connection->buffer->pos.mem = r->connection->buffer->last.mem
                                                = r->connection->buffer->start;
     rev->event_handler = ngx_http_keepalive_handler;
+
+    ngx_http_close_request(r);
+
+    return NGX_OK;
 }
 
 
@@ -532,7 +605,7 @@
 
             timeout = (ngx_msec_t) (c->sent * conf->send_timeout);
 
-            ngx_log_debug(ev->log, "sent: " QD_FMT _ c->sent);
+            ngx_log_debug(ev->log, "sent: " OFF_FMT _ c->sent);
             ngx_log_debug(ev->log, "timeout: %d" _ timeout);
 
             if (ev->timer_set) {
@@ -544,9 +617,11 @@
             ngx_add_timer(ev, timeout);
         }
 
+        /* TODO: /dev/poll, epoll, aio_write */
+
         if (ev->oneshot)
-            if (ngx_add_event(ev, NGX_WRITE_EVENT,
-                              NGX_ONESHOT_EVENT) == NGX_ERROR) {
+            if (ngx_add_event(ev, NGX_WRITE_EVENT, NGX_ONESHOT_EVENT)
+                                                                == NGX_ERROR) {
             return ngx_http_close_request(r);
         }
 
@@ -571,9 +646,12 @@
 
     /* keepalive */
 
-    ngx_http_close_request(r);
     c->buffer->pos.mem = c->buffer->last.mem = c->buffer->start;
     c->read->event_handler = ngx_http_keepalive_handler;
+
+    ngx_http_close_request(r);
+
+    return NGX_OK;
 }
 
 
@@ -581,8 +659,37 @@
 {
     ngx_log_debug(ev->log, "http read blocked");
 
+    /* aio does not call this handler */
+
+#if (USE_KQUEUE)
+
+    return NGX_OK;
+
+#else
+
+#if (HAVE_CLEAR_EVENT) /* kqueue */
+
+    if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) {
+        return NGX_OK;
+    }
+
+#endif
+
+#if (HAVE_EDGE_EVENT) /* epoll */
+
+    if (ngx_event_flags & NGX_HAVE_EDGE_EVENT) {
+        return NGX_OK;
+    }
+
+#endif
+
+    /* select, poll, /dev/poll */
+
     ev->blocked = 1;
+
     return ngx_del_event(ev, NGX_READ_EVENT, 0);
+
+#endif /* USE_KQUEUE */
 }
 
 
@@ -599,8 +706,11 @@
         ev->timer_set = 0;
     }
 
-    if (r->client_content_length)
+    if (r->client_content_length) {
         ev->event_handler = ngx_http_read_discarded_body;
+        /* if blocked - read */
+        /* else add timer */
+    }
 
     return NGX_OK;
 }
@@ -752,7 +862,7 @@
                      ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx);
 
     if (r->discarded_buffer == NULL) {
-        if (r->header_in->end - r->header_in->last.mem
+        if ((size_t)(r->header_in->end - r->header_in->last.mem)
                                                >= lcf->discarded_buffer_size) {
             r->discarded_buffer = r->header_in->last.mem;
 
@@ -789,8 +899,6 @@
 
 static int ngx_http_close_connection(ngx_event_t *ev)
 {
-    ngx_connection_t *c = (ngx_connection_t *) ev->data;
-
     return ngx_event_close_connection(ev);
 }
 
diff --git a/src/http/ngx_http_output_filter.c b/src/http/ngx_http_output_filter.c
index 62756c8..93a7d61 100644
--- a/src/http/ngx_http_output_filter.c
+++ b/src/http/ngx_http_output_filter.c
@@ -60,6 +60,7 @@
 int ngx_http_output_filter(ngx_http_request_t *r, ngx_hunk_t *hunk)
 {
     int                             rc, once;
+    u_int                           flags;
     size_t                          size;
     ngx_chain_t                    *ce;
     ngx_http_output_filter_ctx_t   *ctx;
@@ -272,11 +273,10 @@
 
 static int ngx_http_output_filter_copy_hunk(ngx_hunk_t *dst, ngx_hunk_t *src)
 {
-    size_t   size;
-    ssize_t  n;
+    ssize_t  n, size;
 
     size = src->last.mem - src->pos.mem;
-    if (size > dst->end - dst->pos.mem) {
+    if (size > (dst->end - dst->pos.mem)) {
         size = dst->end - dst->pos.mem;
     }
 
@@ -337,7 +337,7 @@
     ngx_http_output_filter_conf_t *conf =
                                        (ngx_http_output_filter_conf_t *) child;
 
-    ngx_conf_merge(conf->hunk_size, prev->hunk_size, 32768);
+    ngx_conf_size_merge(conf->hunk_size, prev->hunk_size, 32768);
 
     return NULL;
 }
diff --git a/src/http/ngx_http_parse.c b/src/http/ngx_http_parse.c
index f6d8409..a741f09 100644
--- a/src/http/ngx_http_parse.c
+++ b/src/http/ngx_http_parse.c
@@ -304,11 +304,6 @@
                 return NGX_HTTP_PARSE_INVALID_REQUEST;
             }
             break;
-
-#if (NGX_SUPPRESS_WARN)
-        case sw_done:
-            break;
-#endif
         }
     }
 
@@ -495,14 +490,6 @@
                 return NGX_HTTP_PARSE_INVALID_HEADER;
             }
             break;
-
-#if (NGX_SUPPRESS_WARN)
-        case sw_done:
-            break;
-
-        case sw_header_done:
-            break;
-#endif
         }
     }
 
diff --git a/src/http/ngx_http_special_response.c b/src/http/ngx_http_special_response.c
index 4e5e3b2..0e7ccba 100644
--- a/src/http/ngx_http_special_response.c
+++ b/src/http/ngx_http_special_response.c
@@ -5,6 +5,8 @@
 #include <ngx_core.h>
 #include <ngx_string.h>
 #include <ngx_http.h>
+#include <ngx_http_output_filter.h>
+
 
 static char error_tail[] =
 "<hr><center>" NGINX_VER "</center>" CRLF
@@ -12,6 +14,7 @@
 "</html>" CRLF
 ;
 
+
 static char error_400_page[] =
 "<html>" CRLF
 "<head><title>400 Bad Request</title></head>" CRLF
@@ -19,6 +22,7 @@
 "<center><h1>400 Bad Request</h1></center>" CRLF
 ;
 
+
 static char error_403_page[] =
 "<html>" CRLF
 "<head><title>403 Forbidden</title></head>" CRLF
@@ -26,6 +30,7 @@
 "<center><h1>403 Forbidden</h1></center>" CRLF
 ;
 
+
 static char error_404_page[] =
 "<html>" CRLF
 "<head><title>404 Not Found</title></head>" CRLF
@@ -33,6 +38,7 @@
 "<center><h1>404 Not Found</h1></center>" CRLF
 ;
 
+
 static char error_500_page[] =
 "<html>" CRLF
 "<head><title>500 Internal Server Error</title></head>" CRLF
@@ -56,9 +62,10 @@
     { sizeof(error_500_page) - 1, error_500_page }
 };
 
+
 int ngx_http_special_response(ngx_http_request_t *r, int error)
 {
-    int  rc, err, len;
+    int          err, len;
     ngx_hunk_t  *message, *tail;
 
     len = 0;
@@ -80,26 +87,29 @@
         r->headers_out.content_length = error_pages[err].len
                                         + len + sizeof(error_tail);
 
-    ngx_http_send_header(r);
+    if (ngx_http_send_header(r) == NGX_ERROR) {
+        return NGX_ERROR;
+    }
 
-    if (error_pages[err].len == 0)
+    if (error_pages[err].len == 0) {
         return NGX_OK;
+    }
 
-    ngx_test_null(message, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)),
-                  NGX_HTTP_INTERNAL_SERVER_ERROR);
+    ngx_test_null(message, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)), NGX_ERROR);
 
     message->type = NGX_HUNK_MEMORY;
     message->pos.mem = error_pages[err].data;
     message->last.mem = error_pages[err].data + error_pages[err].len;
 
-    rc = ngx_http_output_filter(r, message);
+    if (ngx_http_output_filter(r, message) == NGX_ERROR) {
+        return NGX_ERROR;
+    }
 
-    ngx_test_null(tail, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)),
-                  NGX_HTTP_INTERNAL_SERVER_ERROR);
+    ngx_test_null(tail, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)), NGX_ERROR);
 
     tail->type = NGX_HUNK_MEMORY|NGX_HUNK_LAST;
     tail->pos.mem = error_tail;
     tail->last.mem = error_tail + sizeof(error_tail);
 
-    rc = ngx_http_output_filter(r, tail);
+    return ngx_http_output_filter(r, tail);
 }
diff --git a/src/http/ngx_http_write_filter.c b/src/http/ngx_http_write_filter.c
index 2453934..087ea5b 100644
--- a/src/http/ngx_http_write_filter.c
+++ b/src/http/ngx_http_write_filter.c
@@ -171,7 +171,7 @@
     ngx_http_write_filter_conf_t *conf =
                                        (ngx_http_write_filter_conf_t *) child;
 
-    ngx_conf_merge(conf->buffer_output, prev->buffer_output, 1460);
+    ngx_conf_size_merge(conf->buffer_output, prev->buffer_output, 1460);
 
     return NULL;
 }