changeset 64:34d647deb1da

nginx-0.0.1-2003-03-04-09:33:48 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 04 Mar 2003 06:33:48 +0000
parents 36d2c25cc9bb
children 4222c496acb3
files src/core/ngx_conf_file.c src/core/ngx_conf_file.h src/core/ngx_config.h src/event/modules/ngx_kqueue_module.c src/event/ngx_event.c src/event/ngx_event.h src/event/ngx_event_accept.c src/http/ngx_http.c src/http/ngx_http.h src/http/ngx_http_core_module.c src/http/ngx_http_event.c src/http/ngx_http_header_filter.c
diffstat 12 files changed, 318 insertions(+), 82 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/ngx_conf_file.c
+++ b/src/core/ngx_conf_file.c
@@ -108,7 +108,13 @@ ngx_log_debug(cf->log, "command '%s'" _ 
 #endif
 
                     if (!(cmd->type & NGX_CONF_ANY)
-                        && !(cmd->type & argument_number[cf->args->nelts - 1]))
+                        && ((cmd->type & NGX_CONF_FLAG && cf->args->nelts != 2)
+                            || (!(cmd->type & NGX_CONF_FLAG)
+                                && !(cmd->type
+                                      & argument_number[cf->args->nelts - 1])
+                               )
+                           )
+                       )
                     {
                         ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
                                       "invalid number arguments in "
@@ -143,7 +149,11 @@ ngx_log_debug(cf->log, "rv: %d" _ rv);
 
                     } else {
                         ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                                     "%s", rv);
+                                     "%s %s in %s:%d",
+                                     name->data, rv,
+                                     cf->conf_file->file.name.data,
+                                     cf->conf_file->line);
+
                         return NGX_CONF_ERROR;
                     }
                 }
@@ -383,6 +393,29 @@ ngx_log_debug(cf->log, "FOUND %d:'%s'" _
 }
 
 
+char *ngx_conf_set_flag_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
+{
+    int         flag;
+    ngx_str_t  *value;
+
+    value = (ngx_str_t *) cf->args->elts;
+
+    if (ngx_strcasecmp(value[1].data, "on") == 0) {
+        flag = 1;
+
+    } else if (ngx_strcasecmp(value[1].data, "off") == 0) {
+        flag = 0;
+
+    } else {
+        return "must be \"on\" or \"off\"";
+    }
+
+    *(int *) (conf + cmd->offset) = flag;
+
+    return NGX_CONF_OK;
+}
+
+
 char *ngx_conf_set_str_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
 {
     ngx_str_t  *field, *value;
@@ -427,7 +460,12 @@ char *ngx_conf_set_time_slot(ngx_conf_t 
         return "value must be greater or equal to zero";
     }
 
-    *(int *) (conf + cmd->offset) = size;
+    *(int *) (conf + cmd->offset) = size * 1000;
 
     return NGX_CONF_OK;
 }
+
+char *ngx_conf_unsupported(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
+{
+    return "unsupported on this platform";
+}
--- a/src/core/ngx_conf_file.h
+++ b/src/core/ngx_conf_file.h
@@ -18,6 +18,7 @@
 #define NGX_CONF_ARGS_NUMBER 0x00ffff
 #define NGX_CONF_ANY         0x010000
 #define NGX_CONF_BLOCK       0x020000
+#define NGX_CONF_FLAG        0x040000
 
 
 #define NGX_CONF_UNSET       -1
@@ -89,10 +90,13 @@ struct ngx_conf_s {
     }
 
 
+#define addressof(addr)  ((int) &addr)
+
 
 char *ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename);
 
 
+char *ngx_conf_set_flag_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
 char *ngx_conf_set_str_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
 char *ngx_conf_set_size_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
 char *ngx_conf_set_time_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
--- a/src/core/ngx_config.h
+++ b/src/core/ngx_config.h
@@ -189,6 +189,16 @@
 #include <sys/event.h>
 #endif
 
+/* kqueue's NOTE_LOWAT */
+#if (__FreeBSD__ == 4 && __FreeBSD_version >= 430000) \
+    || __FreeBSD_version >= 500018
+
+#ifndef HAVE_LOWAT_EVENT
+#define HAVE_LOWAT_EVENT  1
+#endif
+
+#endif
+
 #endif /* FreeBSD kqueue */
 
 
--- a/src/event/modules/ngx_kqueue_module.c
+++ b/src/event/modules/ngx_kqueue_module.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2002 Igor Sysoev, http://sysoev.ru
+ * Copyright (C) 2002-2003 Igor Sysoev, http://sysoev.ru
  */
 
 
@@ -77,6 +77,9 @@ int ngx_kqueue_init(int max_connections,
 #if (HAVE_CLEAR_EVENT)
                      |NGX_HAVE_CLEAR_EVENT
 #endif
+#if (HAVE_LOWAT_EVENT)
+                     |NGX_HAVE_LOWAT_EVENT
+#endif
                      |NGX_HAVE_KQUEUE_EVENT;
 
     ngx_write_chain_proc = ngx_freebsd_write_chain;
@@ -89,6 +92,14 @@ int ngx_kqueue_init(int max_connections,
 }
 
 
+void ngx_kqueue_done(ngx_log_t *log)
+{
+    if (close(kq) == -1) {
+        ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "kqueue close() failed");
+    }
+}
+
+
 int ngx_kqueue_add_event(ngx_event_t *ev, int event, u_int flags)
 {
     ev->active = 1;
@@ -173,9 +184,25 @@ int ngx_kqueue_set_event(ngx_event_t *ev
     change_list[nchanges].ident = c->fd;
     change_list[nchanges].filter = filter;
     change_list[nchanges].flags = flags;
+    change_list[nchanges].udata = ev;
+
+#if (HAVE_LOWAT_EVENT)
+
+    if ((flags & EV_ADD) && ev->lowat > 0) {
+        change_list[nchanges].fflags = NOTE_LOWAT;
+        change_list[nchanges].data = ev->lowat;
+
+    } else {
+        change_list[nchanges].fflags = 0;
+        change_list[nchanges].data = 0;
+    }
+
+#else
+
     change_list[nchanges].fflags = 0;
     change_list[nchanges].data = 0;
-    change_list[nchanges].udata = ev;
+
+#endif
 
     ev->index = nchanges;
 
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -153,6 +153,8 @@ void ngx_pre_thread(ngx_array_t *ls, ngx
         if (ngx_event_flags & NGX_HAVE_IOCP_EVENT) {
             ev->event_handler = &ngx_event_acceptex;
 
+            /* LOOK: we call ngx_iocp_add_event() also
+               in ngx_event_post_acceptex() */
             if (ngx_iocp_add_event(ev) == NGX_ERROR) {
                 return NGX_ERROR;
             }
--- a/src/event/ngx_event.h
+++ b/src/event/ngx_event.h
@@ -10,6 +10,8 @@
 #include <ngx_alloc.h>
 #include <ngx_array.h>
 
+/* STUB */
+#define NGX_LOWAT   10000
 
 #define NGX_INVALID_INDEX  0x80000000
 
@@ -81,6 +83,10 @@ struct ngx_event_s {
     int              error;
 #endif
 
+#if (HAVE_LOWAT_EVENT) /* kqueue's NOTE_LOWAT */
+    int              lowat;
+#endif
+
 
 #if (HAVE_AIO)
 
@@ -151,18 +157,23 @@ typedef struct {
 /* Event filter has kqueue features - eof flag, errno, available data, etc */
 #define NGX_HAVE_KQUEUE_EVENT   8
 
-/* Event filter notifies only changes (edgesi) but not initial level - epoll */
-#define NGX_HAVE_EDGE_EVENT     16
+/* Event filter supports low water mark - kqueue's NOTE_LOWAT,
+   early kqueue implementations have no NOTE_LOWAT so we need separate flag */
+#define NGX_HAVE_LOWAT_EVENT    16
+
+/* Event filter notifies only changes (edges) but not initial level - epoll */
+#define NGX_HAVE_EDGE_EVENT     32
 
 /* No need to add or delete event filters - rt signals */
-#define NGX_HAVE_SIGIO_EVENT    32
+#define NGX_HAVE_SIGIO_EVENT    64
 
 /* No need to add or delete event filters - overlapped, aio_read, aioread */
-#define NGX_HAVE_AIO_EVENT      64
+#define NGX_HAVE_AIO_EVENT      128
 
 /* Need to add socket or halde only once - i/o completion port.
    It also requires to set HAVE_AIO_EVENT and NGX_HAVE_AIO_EVENT */
-#define NGX_HAVE_IOCP_EVENT     128
+#define NGX_HAVE_IOCP_EVENT     256
+
 
 /* Event filter is deleted before closing file. Has no meaning
    for select, poll, epoll.
--- a/src/event/ngx_event_accept.c
+++ b/src/event/ngx_event_accept.c
@@ -148,6 +148,16 @@ int ngx_event_accept(ngx_event_t *ev)
         }
 #endif
 
+#if (HAVE_EDGE_EVENT) /* epoll */
+
+        if (ngx_event_flags & NGX_HAVE_EDGE_EVENT) {
+            if (ngx_edge_add_event(ev) == NGX_ERROR) {
+                return NGX_OK;
+            }
+        }
+
+#endif
+
         ls->handler(c);
 
 #if (USE_KQUEUE)
--- a/src/http/ngx_http.c
+++ b/src/http/ngx_http.c
@@ -18,11 +18,14 @@ int  ngx_http_max_module;
 
 ngx_array_t  ngx_http_servers;   /* array of ngx_http_core_srv_conf_t */ 
 
-int  ngx_http_post_accept_timeout = 10000;
+int  ngx_http_post_accept_timeout = 30000;
 int  ngx_http_connection_pool_size = 16384;
 int  ngx_http_request_pool_size = 16384;
-int  ngx_http_client_header_timeout = 20000;
+int  ngx_http_client_header_timeout = 60000;
 int  ngx_http_client_header_buffer_size = 1024;
+int  ngx_http_large_client_header = 1;
+
+int  ngx_http_url_in_error_log = 1;
 
 /* STUB: per location */
 int  ngx_http_lingering_timeout = 5000;
--- a/src/http/ngx_http.h
+++ b/src/http/ngx_http.h
@@ -15,7 +15,9 @@
 /* STUB */
 #include <ngx_event_timer.h>
 
+#define NGX_HTTP_VERSION_9           9
 #define NGX_HTTP_VERSION_10       1000
+#define NGX_HTTP_VERSION_11       1001
 
 #define NGX_HTTP_GET   1
 #define NGX_HTTP_HEAD  2
@@ -31,6 +33,8 @@
 #define NGX_HTTP_PARSE_TOO_LONG_URI     12
 #define NGX_HTTP_PARSE_INVALID_HEAD     13
 #define NGX_HTTP_PARSE_INVALID_HEADER   14
+#define NGX_HTTP_PARSE_TOO_LONG_HEADER  15
+#define NGX_HTTP_PARSE_NO_HOST_HEADER   16
 
 
 #define NGX_HTTP_OK                     200
@@ -270,11 +274,13 @@ extern int  ngx_http_connection_pool_siz
 extern int  ngx_http_request_pool_size;
 extern int  ngx_http_client_header_timeout;
 extern int  ngx_http_client_header_buffer_size;
+extern int  ngx_http_large_client_header;
 extern int  ngx_http_discarded_buffer_size;
 
 extern int  ngx_http_lingering_timeout;
 extern int  ngx_http_lingering_time;
 
+extern int  ngx_http_url_in_error_log;
 
 extern ngx_array_t  ngx_http_index_handlers;
 
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -36,6 +36,42 @@ static ngx_command_t  ngx_http_core_comm
      0,
      0},
 
+    {ngx_string("post_accept_timeout"),
+     NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
+     ngx_conf_set_time_slot,
+     0,
+     addressof(ngx_http_post_accept_timeout)},
+
+    {ngx_string("connection_pool_size"),
+     NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
+     ngx_conf_set_size_slot,
+     0,
+     addressof(ngx_http_connection_pool_size)},
+
+    {ngx_string("request_pool_size"),
+     NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
+     ngx_conf_set_size_slot,
+     0,
+     addressof(ngx_http_request_pool_size)},
+
+    {ngx_string("client_header_timeout"),
+     NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
+     ngx_conf_set_time_slot,
+     0,
+     addressof(ngx_http_client_header_timeout)},
+
+    {ngx_string("client_header_buffer_size"),
+     NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
+     ngx_conf_set_size_slot,
+     0,
+     addressof(ngx_http_client_header_buffer_size)},
+
+    {ngx_string("large_client_header"),
+     NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
+     ngx_conf_set_flag_slot,
+     0,
+     addressof(ngx_http_large_client_header)},
+
     {ngx_string("location"),
      NGX_HTTP_SRV_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1,
      ngx_location_block,
@@ -468,6 +504,8 @@ int ngx_http_error(ngx_http_request_t *r
 
 int ngx_http_close_request(ngx_http_request_t *r)
 {
+    ngx_http_log_ctx_t  *ctx;
+
     ngx_log_debug(r->connection->log, "CLOSE#: %d" _ r->file.fd);
 
     ngx_http_log_handler(r);
@@ -487,9 +525,14 @@ int ngx_http_close_request(ngx_http_requ
         ngx_http_log_request(r);
 */
 
+    ctx = (ngx_http_log_ctx_t *) r->connection->log->data;
+
+    /* ctx->url was allocated from r->pool */
+    ctx->url = NULL;
+
     ngx_destroy_pool(r->pool);
 
-    ngx_log_debug(r->connection->log, "http close");
+    ngx_log_debug(r->connection->log, "http closed");
 
     if (r->connection->read->timer_set) {
         ngx_del_timer(r->connection->read);
--- a/src/http/ngx_http_event.c
+++ b/src/http/ngx_http_event.c
@@ -44,10 +44,15 @@ static char *header_errors[] = {
     "client %s sent invalid method",
     "client %s sent invalid request",
     "client %s sent too long URI",
-    "client %s sent HEAD method in HTTP/0.9 request"
+    "client %s sent HEAD method in HTTP/0.9 request",
+
+    "client %s sent invalid header, URL: %s",
+    "client %s sent too long header line, URL: %s",
+    "client %s sent HTTP/1.1 request without \"Host\" header, URL: %s"
 };
 
 
+
 static ngx_http_header_t headers_in[] = {
     { 4, "Host", offsetof(ngx_http_headers_in_t, host) },
     { 10, "Connection", offsetof(ngx_http_headers_in_t, connection) },
@@ -113,21 +118,9 @@ int ngx_http_init_connection(ngx_connect
 
 #endif
 
-/* THINK: should ngx_edge_add_event() be moved to accept part ? */
-#if (HAVE_EDGE_EVENT) /* epoll */
+#if (HAVE_EDGE_EVENT) /* epoll */ || (HAVE_AIO_EVENT) /* aio, iocp */
 
-    if (ngx_event_flags & NGX_HAVE_EDGE_EVENT) {
-        if (ngx_edge_add_event(ev) == NGX_ERROR) {
-            return NGX_ERROR;
-        }
-        return ngx_http_init_request(ev);
-    }
-
-#endif
-
-#if (HAVE_AIO_EVENT) /* aio, iocp */
-
-    if (ngx_event_flags & NGX_HAVE_AIO_EVENT) {
+    if (ngx_event_flags & (NGX_HAVE_EDGE_EVENT|NGX_HAVE_AIO_EVENT)) {
         return ngx_http_init_request(ev);
     }
 
@@ -193,9 +186,10 @@ static int ngx_http_init_request(ngx_eve
 
 static int ngx_http_process_request_header(ngx_event_t *ev)
 {
-    int                  n, rc;
+    int                  n, rc, offset;
     ngx_connection_t    *c;
     ngx_http_request_t  *r;
+    ngx_http_log_ctx_t  *ctx;
 
     c = (ngx_connection_t *) ev->data;
     r = (ngx_http_request_t *) c->data;
@@ -249,6 +243,7 @@ static int ngx_http_process_request_head
             ngx_http_process_request_headers(r) */
 
         do {
+            /* state_handlers return NGX_OK when whole header done */
             rc = (r->state_handler)(r);
 
             if (rc == NGX_ERROR)
@@ -257,6 +252,38 @@ static int ngx_http_process_request_head
         } while (rc == NGX_AGAIN
                  && r->header_in->pos.mem < r->header_in->last.mem);
 
+        /* if large client header is supported then
+            we need to compact r->header_in hunk */
+
+        if (ngx_http_large_client_header
+            && rc == NGX_AGAIN
+            && r->header_in->last.mem == r->header_in->end
+            && r->header_in->pos.mem == r->header_in->last.mem)
+        {
+            offset = r->header_name_start - r->header_in->start;
+
+            if (offset == 0) {
+                ctx = r->connection->log->data;
+                r->connection->log->handler = NULL;
+                ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                              "client %s sent too long header line, URL: %s",
+                              ctx->client, ctx->url);
+                r->connection->log->handler = ngx_http_log_error;
+
+                return ngx_http_error(r, NGX_HTTP_BAD_REQUEST);
+            }
+
+            ngx_memcpy(r->header_in->start, r->header_name_start,
+                       r->header_in->last.mem - r->header_name_start);
+
+            r->header_name_start = r->header_in->start;
+            r->header_in->last.mem -= offset;
+            r->header_in->pos.mem -= offset;
+            r->header_name_end -= offset;
+            r->header_start -= offset;
+            r->header_end -= offset;
+        }
+
 #if (HAVE_AIO_EVENT) /* aio, iocp */
     } while (rc == NGX_AGAIN && ngx_event_flags & NGX_HAVE_AIO_EVENT);
 #endif
@@ -301,19 +328,30 @@ static int ngx_http_process_request_line
     c = r->connection;
 
     if (rc == NGX_OK) {
+        /* copy URI */
         r->uri.len = (r->args_start ? r->args_start - 1 : r->uri_end)
                                                                 - r->uri_start;
         ngx_test_null(r->uri.data, ngx_palloc(r->pool, r->uri.len + 1),
                       ngx_http_close_request(r));
         ngx_cpystrn(r->uri.data, r->uri_start, r->uri.len + 1);
 
+        /* if large client headers is supported then
+           we need to copy request line */
+
         r->request_line.len = r->request_end - r->header_in->start;
-        ngx_test_null(r->request_line.data,
-                      ngx_palloc(r->pool, r->request_line.len + 1),
-                      ngx_http_close_request(r));
-        ngx_cpystrn(r->request_line.data, r->header_in->start,
-                    r->request_line.len + 1);
+        if (ngx_http_large_client_header) {
+            ngx_test_null(r->request_line.data,
+                          ngx_palloc(r->pool, r->request_line.len + 1),
+                          ngx_http_close_request(r));
+            ngx_cpystrn(r->request_line.data, r->header_in->start,
+                        r->request_line.len + 1);
 
+        } else {
+            r->request_line.data = r->header_in->start;
+            r->request_line.data[r->request_line.len] = '\0';
+        }
+
+        /* copy URI extention if it exists */
         if (r->uri_ext) {
             r->exten.len = (r->args_start ? r->args_start - 1 : r->uri_end)
                                                                   - r->uri_ext;
@@ -329,21 +367,26 @@ static int ngx_http_process_request_line
                       r->uri.data _ r->exten.data);
 #endif
 
-        if (r->http_version == 9)
+        ctx = r->connection->log->data;
+        if (ngx_http_url_in_error_log) {
+            ngx_test_null(ctx->url,
+                          ngx_palloc(r->pool, r->uri_end - r->uri_start + 1),
+                          ngx_http_close_request(r));
+            ngx_cpystrn(ctx->url, r->uri_start, r->uri_end - r->uri_start + 1);
+        }
+
+        if (r->http_version == NGX_HTTP_VERSION_9)
             return NGX_OK;
 
-        /* TODO: check too long URI - no space for header, compact buffer */
-
         r->headers_in.headers = ngx_create_table(r->pool, 10);
 
         r->state_handler = ngx_http_process_request_headers;
-        ctx = r->connection->log->data;
         ctx->action = "reading client request headers";
 
         return NGX_AGAIN;
     }
 
-    if (r->header_in->last.mem >= r->header_in->end) {
+    if (r->header_in->last.mem == r->header_in->end) {
         rc = NGX_HTTP_PARSE_TOO_LONG_URI;
 
     } else if (rc == NGX_AGAIN) {
@@ -372,11 +415,9 @@ static int ngx_http_process_request_head
     for ( ;; ) {
         rc = ngx_read_http_header_line(r, r->header_in);
 
-        /* TODO: check too long header, compact buffer */
-
         if (rc == NGX_OK) { /* header line is ready */
             if (ngx_http_process_request_header_line(r) == NGX_ERROR) {
-                return ngx_http_error(r, NGX_HTTP_BAD_REQUEST);
+                return ngx_http_error(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
             }
 
             return NGX_AGAIN;
@@ -391,28 +432,33 @@ static int ngx_http_process_request_head
                      }
                  }
                  r->headers_in.host_name_len = len;
+                 return NGX_OK;
 
             } else {
-                 if (r->http_version > NGX_HTTP_VERSION_10) {
-                     return ngx_http_error(r, NGX_HTTP_BAD_REQUEST);
+                 if (r->http_version < NGX_HTTP_VERSION_11) {
+                     r->headers_in.host_name_len = 0;
+                     return NGX_OK;
                  }
-                 r->headers_in.host_name_len = 0;
             }
 
-            return NGX_OK;
+            rc = NGX_HTTP_PARSE_NO_HOST_HEADER;
+
+        } else if (!ngx_http_large_client_header
+                   && r->header_in->last.mem == r->header_in->end) {
+            rc = NGX_HTTP_PARSE_TOO_LONG_HEADER;
 
         } else if (rc == NGX_AGAIN) {
             return NGX_AGAIN;
+        }
 
-        } else if (rc == NGX_HTTP_PARSE_INVALID_HEADER) {
-            ctx = r->connection->log->data;
-            r->connection->log->handler = NULL;
-            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
-                          "client %s sent invalid header", ctx->client);
-            r->connection->log->handler = ngx_http_log_error;
+        ctx = r->connection->log->data;
+        r->connection->log->handler = NULL;
+        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                      header_errors[rc - NGX_HTTP_PARSE_INVALID_METHOD],
+                      ctx->client, ctx->url);
+        r->connection->log->handler = ngx_http_log_error;
 
-            return ngx_http_error(r, NGX_HTTP_BAD_REQUEST);
-        }
+        return ngx_http_error(r, NGX_HTTP_BAD_REQUEST);
     }
 }
 
@@ -424,14 +470,26 @@ static int ngx_http_process_request_head
 
     ngx_test_null(h, ngx_push_table(r->headers_in.headers), NGX_ERROR);
 
+    /* if large client headers is supported then
+       we need to copy header name and value */
+
     h->key.len = r->header_name_end - r->header_name_start;
-    ngx_test_null(h->key.data, ngx_palloc(r->pool, h->key.len + 1), NGX_ERROR);
-    ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1);
+    h->value.len = r->header_end - r->header_start;
 
-    h->value.len = r->header_end - r->header_start;
-    ngx_test_null(h->value.data, ngx_palloc(r->pool, h->value.len + 1),
-                  NGX_ERROR);
-    ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1);
+    if (ngx_http_large_client_header) {
+        ngx_test_null(h->key.data, ngx_palloc(r->pool, h->key.len + 1),
+                      NGX_ERROR);
+        ngx_test_null(h->value.data, ngx_palloc(r->pool, h->value.len + 1),
+                      NGX_ERROR);
+        ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1);
+        ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1);
+
+    } else {
+        h->key.data = r->header_name_start;
+        h->key.data[h->key.len] = '\0';
+        h->value.data = r->header_start;
+        h->value.data[h->value.len] = '\0';
+    }
 
     for (i = 0; headers_in[i].len != 0; i++) {
         if (headers_in[i].len != h->key.len) {
@@ -453,9 +511,10 @@ static int ngx_http_process_request_head
 
 static int ngx_http_event_request_handler(ngx_http_request_t *r)
 {
-    int           rc, event;
-    ngx_msec_t    timeout;
-    ngx_event_t  *rev, *wev;
+    int                  rc, event;
+    ngx_msec_t           timeout;
+    ngx_event_t         *rev, *wev;
+    ngx_http_log_ctx_t  *ctx;
 
     rev = r->connection->read;
     wev = r->connection->write;
@@ -468,6 +527,9 @@ static int ngx_http_event_request_handle
     r->state_handler = NULL;
     rev->event_handler = ngx_http_block_read;
 
+    ctx = r->connection->log->data;
+    ctx->action = "processing client request";
+
     rc = ngx_http_handler(r);
 
     /* handler is still busy */
@@ -477,6 +539,7 @@ static int ngx_http_event_request_handle
     /* handler has done its work but transfer is still not completed */
     if (rc == NGX_AGAIN) {
 
+        /* STUB: timeouts should be reworked */
         if (r->connection->sent > 0) {
             ngx_log_debug(r->connection->log, "sent: " OFF_FMT _
                           r->connection->sent);
@@ -492,6 +555,10 @@ static int ngx_http_event_request_handle
 
 #if (USE_KQUEUE)
 
+#if (HAVE_LOWAT_EVENT) /* kqueue's NOTE_LOWAT */
+        wev->lowat = /* STUB */ NGX_LOWAT;
+#endif
+
         if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_CLEAR_EVENT) == NGX_ERROR) {
             return ngx_http_close_request(r);
         }
@@ -508,6 +575,14 @@ static int ngx_http_event_request_handle
 
 #endif
 
+#if (HAVE_LOWAT_EVENT) /* kqueue's NOTE_LOWAT */
+
+        if (ngx_event_flags & NGX_HAVE_LOWAT_EVENT) {
+            wev->lowat = /* STUB */ NGX_LOWAT;
+        }
+
+#endif
+
 #if (HAVE_CLEAR_EVENT) /* kqueue */
 
         if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) {
@@ -546,7 +621,7 @@ static int ngx_http_event_request_handle
 
     /* rc == NGX_OK */
 
-    if (!r->keepalive) {
+    if (r->keepalive == 0) {
         if (r->lingering_close) {
             return ngx_http_set_lingering_close(r);
 
@@ -578,6 +653,7 @@ static int ngx_http_writer(ngx_event_t *
 
     if (rc == NGX_AGAIN) {
 
+        /* STUB: timeouts should be reworked */
         if (c->sent > 0) {
             conf = (ngx_http_core_loc_conf_t *)
                         ngx_http_get_module_loc_conf(r->main ? r->main : r,
@@ -607,7 +683,7 @@ static int ngx_http_writer(ngx_event_t *
 
     ngx_log_debug(ev->log, "http writer done");
 
-    if (!r->keepalive) {
+    if (r->keepalive == 0) {
         if (r->lingering_close) {
             return ngx_http_set_lingering_close(r);
 
@@ -731,6 +807,7 @@ static int ngx_http_read_discarded_body(
 static int ngx_http_set_keepalive(ngx_http_request_t *r)
 {
     ngx_connection_t    *c;
+    ngx_http_log_ctx_t  *ctx;
 
     c = (ngx_connection_t *) r->connection;
 
@@ -743,8 +820,13 @@ static int ngx_http_set_keepalive(ngx_ht
         }
     }
 
+    ctx = (ngx_http_log_ctx_t *) c->log->data;
+    ctx->action = "closing request";
+
     ngx_http_close_request(r);
 
+    ctx->action = "keepalive";
+
 #if (HAVE_AIO_EVENT) /* aio, iocp */
     if (ngx_event_flags & NGX_HAVE_AIO_EVENT) {
         return ngx_http_keepalive_handler(c->read);
@@ -768,6 +850,9 @@ static int ngx_http_keepalive_handler(ng
     if (ev->timedout)
         return NGX_DONE;
 
+    /* TODO: MSIE closes keepalive connection with ECONNRESET
+             so we need to handle here this error
+             1) in INFO (not ERR) level, 2) with time elapsed */
     n = ngx_event_recv(c, c->buffer->last.mem,
                        c->buffer->end - c->buffer->last.mem);
 
@@ -815,14 +900,11 @@ static int ngx_http_set_lingering_close(
     ngx_add_timer(ev, lcf->lingering_timeout);
 
     if (ev->blocked) {
-        if (ngx_add_event(ev, NGX_READ_EVENT,
-#if (HAVE_CLEAR_EVENT)
-                          NGX_CLEAR_EVENT) == NGX_ERROR)
-#else
-                          NGX_ONESHOT_EVENT) == NGX_ERROR)
-#endif
-        {
-            return ngx_http_close_request(r);
+        if (ngx_event_flags & NGX_HAVE_LEVEL_EVENT) {
+            if (ngx_add_event(ev, NGX_READ_EVENT, NGX_LEVEL_EVENT)
+                                                                == NGX_ERROR) {
+                return ngx_http_close_request(r);
+            }
         }
     }
 
@@ -838,7 +920,7 @@ static int ngx_http_set_lingering_close(
     }
 #endif
 
-#if (HAVE_CLEAR_EVENT) || (HAVE_EDGE_EVENT) /* kqueue, epoll */
+#if (HAVE_CLEAR_EVENT) /* kqueue */ || (HAVE_EDGE_EVENT) /* epoll */
     if (ngx_event_flags & (NGX_HAVE_CLEAR_EVENT|NGX_HAVE_EDGE_EVENT)) {
         return NGX_OK;
     }
--- a/src/http/ngx_http_header_filter.c
+++ b/src/http/ngx_http_header_filter.c
@@ -163,10 +163,10 @@ static int ngx_http_header_filter(ngx_ht
         len += 46;
     }
 
-    if (r->keepalive) {
+    if (r->keepalive == 0) {
+        len += 19;
+    } else {
         len += 24;
-    } else {
-        len += 19;
     }
 
     header = (ngx_table_elt_t *) r->headers_out.headers->elts;
@@ -238,13 +238,13 @@ static int ngx_http_header_filter(ngx_ht
         *(h->last.mem++) = CR; *(h->last.mem++) = LF;
     }
 
-    if (r->keepalive) {
+    if (r->keepalive == 0) {
+        ngx_memcpy(h->last.mem, "Connection: close" CRLF, 19);
+        h->last.mem += 19;
+
+    } else {
         ngx_memcpy(h->last.mem, "Connection: keep-alive" CRLF, 24);
         h->last.mem += 24;
-
-    } else {
-        ngx_memcpy(h->last.mem, "Connection: close" CRLF, 19);
-        h->last.mem += 19;
     }
 
     for (i = 0; i < r->headers_out.headers->nelts; i++) {