nginx-0.1.2-RELEASE import

    *) Feature: the --user=USER, --group=GROUP, and --with-ld-opt=OPTIONS
       options in configure.

    *) Feature: the server_name directive supports *.domain.tld.

    *) Bugfix: the portability improvements.

    *) Bugfix: if configuration file was set in command line, the
       reconfiguration was impossible; the bug had appeared in 0.1.1.

    *) Bugfix: proxy module may get caught in an endless loop when sendfile
       is not used.

    *) Bugfix: with sendfile the response was not recoded according to the
       charset module directives; the bug had appeared in 0.1.1.

    *) Bugfix: very seldom bug in the kqueue processing.

    *) Bugfix: the gzip module compressed the proxied responses that was
       already compressed.
diff --git a/src/core/nginx.c b/src/core/nginx.c
index 9d91080..ab85b23 100644
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -104,7 +104,7 @@
     ngx_cycle_t      *cycle, init_cycle;
     ngx_core_conf_t  *ccf;
 
-#if defined __FreeBSD__
+#if (NGX_FREEBSD)
     ngx_debug_init();
 #endif
 
@@ -112,7 +112,7 @@
 
     ngx_time_init();
 
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
     ngx_regex_init();
 #endif
 
@@ -136,11 +136,11 @@
         return 1;
     }
 
-    if (ngx_getopt(&init_cycle, argc, argv) == NGX_ERROR) {
+    if (ngx_save_argv(&init_cycle, argc, argv) == NGX_ERROR) {
         return 1;
     }
 
-    if (ngx_save_argv(&init_cycle, argc, argv) == NGX_ERROR) {
+    if (ngx_getopt(&init_cycle, argc, ngx_argv) == NGX_ERROR) {
         return 1;
     }
 
@@ -175,7 +175,7 @@
     if (ngx_test_config) {
         ngx_log_error(NGX_LOG_INFO, log, 0,
                       "the configuration file %s was tested successfully",
-                      init_cycle.conf_file.data);
+                      cycle->conf_file.data);
         return 0;
     }
 
@@ -387,7 +387,7 @@
 
     ngx_argc = argc;
 
-#if __FreeBSD__
+#if (NGX_FREEBSD)
 
     ngx_argv = (char **) argv;
 
@@ -462,28 +462,26 @@
 
 #if !(WIN32)
 
-#if 0
     if (ccf->user == (uid_t) NGX_CONF_UNSET) {
 
-        pwd = getpwnam("nobody");
+        pwd = getpwnam(NGX_USER);
         if (pwd == NULL) {
             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
-                          "getpwnam(\"nobody\") failed");
+                          "getpwnam(\"" NGX_USER "\") failed");
             return NGX_CONF_ERROR;
         }
 
         ccf->user = pwd->pw_uid;
 
-        grp = getgrnam("nobody");
+        grp = getgrnam(NGX_GROUP);
         if (grp == NULL) {
             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
-                          "getgrnam(\"nobody\") failed");
+                          "getgrnam(\"" NGX_GROUP "\") failed");
             return NGX_CONF_ERROR;
         }
 
         ccf->group = grp->gr_gid;
     }
-#endif
 
     if (ccf->pid.len == 0) {
         ccf->pid.len = sizeof(NGX_PID_PATH) - 1;
@@ -522,6 +520,7 @@
 
     ngx_core_conf_t  *ccf = conf;
 
+    char             *group;
     struct passwd    *pwd;
     struct group     *grp;
     ngx_str_t        *value;
@@ -530,6 +529,14 @@
         return "is duplicate";
     }
 
+    if (geteuid() != 0) {
+        ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
+                           "the \"user\" directive makes sense only "
+                           "if the master process runs "
+                           "with super-user privileges, ignored");
+        return NGX_CONF_OK;
+    }
+
     value = (ngx_str_t *) cf->args->elts;
 
     pwd = getpwnam((const char *) value[1].data);
@@ -541,14 +548,12 @@
 
     ccf->user = pwd->pw_uid;
 
-    if (cf->args->nelts == 2) {
-        return NGX_CONF_OK;
-    }
+    group = (char *) ((cf->args->nelts == 2) ? value[1].data : value[2].data);
 
-    grp = getgrnam((const char *) value[2].data);
+    grp = getgrnam(group);
     if (grp == NULL) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
-                           "getgrnam(\"%s\") failed", value[2].data);
+                           "getgrnam(\"%s\") failed", group);
         return NGX_CONF_ERROR;
     }
 
diff --git a/src/core/nginx.h b/src/core/nginx.h
index 82545e5..bac01fc 100644
--- a/src/core/nginx.h
+++ b/src/core/nginx.h
@@ -8,7 +8,7 @@
 #define _NGINX_H_INCLUDED_
 
 
-#define NGINX_VER          "nginx/0.1.1"
+#define NGINX_VER          "nginx/0.1.2"
 
 #define NGINX_VAR          "NGINX"
 #define NGX_NEWPID_EXT     ".newbin"
diff --git a/src/core/ngx_buf.c b/src/core/ngx_buf.c
index a536c04..7d37989 100644
--- a/src/core/ngx_buf.c
+++ b/src/core/ngx_buf.c
@@ -126,12 +126,11 @@
         *busy = *out;
 
     } else {
-        for (tl = *busy; /* void */ ; tl = tl->next) {
-            if (tl->next == NULL) {
-                tl->next = *out;
-                break;
-            }
+        for (tl = *busy; tl->next; tl = tl->next) {
+            /* void */;
         }
+
+        tl->next = *out;
     }
 
     *out = NULL;
diff --git a/src/core/ngx_buf.h b/src/core/ngx_buf.h
index 3e014d1..e2d0719 100644
--- a/src/core/ngx_buf.h
+++ b/src/core/ngx_buf.h
@@ -22,7 +22,6 @@
     off_t            file_pos;
     off_t            file_last;
 
-    int              type;
     u_char          *start;         /* start of buffer */
     u_char          *end;           /* end of buffer */
     ngx_buf_tag_t    tag;
diff --git a/src/core/ngx_conf_file.c b/src/core/ngx_conf_file.c
index 90a09d2..fb629a4 100644
--- a/src/core/ngx_conf_file.c
+++ b/src/core/ngx_conf_file.c
@@ -579,8 +579,18 @@
 
     name->len = cycle->root.len + old.len;
 
-    if (!(name->data = ngx_palloc(cycle->pool, name->len + 1))) {
-        return  NGX_ERROR;
+    if (cycle->connections) {
+        if (!(name->data = ngx_palloc(cycle->pool, name->len + 1))) {
+            return  NGX_ERROR;
+        }
+
+    } else {
+
+        /* the init_cycle */
+
+        if (!(name->data = ngx_alloc(name->len + 1, cycle->log))) {
+            return  NGX_ERROR;
+        }
     }
 
     p = ngx_cpymem(name->data, cycle->root.data, cycle->root.len),
diff --git a/src/core/ngx_config.h b/src/core/ngx_config.h
index 7e3e682..3590fae 100644
--- a/src/core/ngx_config.h
+++ b/src/core/ngx_config.h
@@ -8,30 +8,32 @@
 #define _NGX_CONFIG_H_INCLUDED_
 
 
+#include <ngx_auto_headers.h>
+
+
 #if defined __DragonFly__ && !defined __FreeBSD__
 #define __FreeBSD__        4
 #define __FreeBSD_version  480101
 #endif
 
 
-#if defined __FreeBSD__
+#if (NGX_FREEBSD)
 #include <ngx_freebsd_config.h>
 
 
-#elif defined __linux__
+#elif (NGX_LINUX)
 #include <ngx_linux_config.h>
 
 
-       /* Solaris */
-#elif defined sun && (defined __svr4__ || defined __SVR4)
+#elif (NGX_SOLARIS)
 #include <ngx_solaris_config.h>
 
 
-#elif defined _WIN32
+#elif (NGX_WIN32)
 #include <ngx_win32_config.h>
 
 
-#else /* posix */
+#else /* POSIX */
 #include <ngx_posix_config.h>
 
 #endif
@@ -89,8 +91,10 @@
 #define NGX_INT64_LEN      sizeof("-9223372036854775808") - 1
 #define NGX_OFF_T_LEN      sizeof("-9223372036854775808") - 1
 
+#define NGX_MAX_INT_LEN    (sizeof("-9223372036854775808") - 1)
 
-#if (SOLARIS)
+
+#if (NGX_SOLARIS)
 
 /* TODO: auto_conf */
 #define NGX_ALIGN       (_MAX_ALIGNMENT - 1)         /* platform word */
diff --git a/src/core/ngx_connection.h b/src/core/ngx_connection.h
index 57c7a33..8e2ba39 100644
--- a/src/core/ngx_connection.h
+++ b/src/core/ngx_connection.h
@@ -40,7 +40,7 @@
     time_t            post_accept_timeout;     /* should be here because
                                                   of the deferred accept */
 
-    unsigned          new:1;
+    unsigned          open:1;
     unsigned          remain:1;
     unsigned          ignore:1;
 
diff --git a/src/core/ngx_core.h b/src/core/ngx_core.h
index 02a4a17..79fb009 100644
--- a/src/core/ngx_core.h
+++ b/src/core/ngx_core.h
@@ -54,7 +54,7 @@
 #include <ngx_file.h>
 #include <ngx_files.h>
 #include <ngx_crc.h>
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
 #include <ngx_regex.h>
 #endif
 #include <ngx_rbtree.h>
diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c
index 38f4ad5..a1402f1 100644
--- a/src/core/ngx_cycle.c
+++ b/src/core/ngx_cycle.c
@@ -158,6 +158,9 @@
     conf.module_type = NGX_CORE_MODULE;
     conf.cmd_type = NGX_MAIN_CONF;
 
+#if 0
+    log->log_level = NGX_LOG_DEBUG_ALL;
+#endif
 
     if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
         ngx_destroy_pool(pool);
@@ -223,9 +226,6 @@
                                        NGX_FILE_RDWR,
                                        NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND);
 
-#if 0
-            log->log_level = NGX_LOG_DEBUG_ALL;
-#endif
             ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
                            "log: %0X %d \"%s\"",
                            &file[i], file[i].fd, file[i].name.data);
@@ -310,14 +310,14 @@
                 }
 
                 if (nls[n].fd == -1) {
-                    nls[n].new = 1;
+                    nls[n].open = 1;
                 }
             }
 
         } else {
             ls = cycle->listening.elts;
             for (i = 0; i < cycle->listening.nelts; i++) {
-                ls[i].new = 1;
+                ls[i].open = 1;
             }
         }
 
@@ -366,7 +366,7 @@
 
         ls = cycle->listening.elts;
         for (i = 0; i < cycle->listening.nelts; i++) {
-            if (ls[i].fd == -1 || !ls[i].new) {
+            if (ls[i].fd == -1 || !ls[i].open) {
                 continue;
             }
 
diff --git a/src/core/ngx_log.c b/src/core/ngx_log.c
index 9cf1d56..f1c0e97 100644
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -57,7 +57,7 @@
 };
 
 
-#if (HAVE_VARIADIC_MACROS)
+#if (NGX_HAVE_VARIADIC_MACROS)
 void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
                         const char *fmt, ...)
 #else
@@ -67,7 +67,7 @@
 {
     char      errstr[MAX_ERROR_STR];
     size_t    len, max;
-#if (HAVE_VARIADIC_MACROS)
+#if (NGX_HAVE_VARIADIC_MACROS)
     va_list   args;
 #endif
 
@@ -97,7 +97,7 @@
                             "*%u ", *(u_int *) log->data);
     }
 
-#if (HAVE_VARIADIC_MACROS)
+#if (NGX_HAVE_VARIADIC_MACROS)
 
     va_start(args, fmt);
     len += ngx_vsnprintf(errstr + len, max - len, fmt, args);
@@ -187,7 +187,7 @@
 }
 
 
-#if !(HAVE_VARIADIC_MACROS)
+#if !(NGX_HAVE_VARIADIC_MACROS)
 
 void ngx_log_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
                    const char *fmt, ...)
@@ -253,7 +253,7 @@
 #endif
 
     ngx_log.file = &ngx_stderr;
-    ngx_log.log_level = NGX_LOG_ERR;
+    ngx_log.log_level = NGX_LOG_NOTICE;
 
     return &ngx_log;
 }
diff --git a/src/core/ngx_log.h b/src/core/ngx_log.h
index e82995e..5ddd8fc 100644
--- a/src/core/ngx_log.h
+++ b/src/core/ngx_log.h
@@ -55,9 +55,9 @@
 
 /*********************************/
 
-#if (HAVE_GCC_VARIADIC_MACROS)
+#if (NGX_HAVE_GCC_VARIADIC_MACROS)
 
-#define HAVE_VARIADIC_MACROS  1
+#define NGX_HAVE_VARIADIC_MACROS  1
 
 #define ngx_log_error(level, log, args...) \
         if (log->log_level >= level) ngx_log_error_core(level, log, args)
@@ -67,9 +67,9 @@
 
 /*********************************/
 
-#elif (HAVE_C99_VARIADIC_MACROS)
+#elif (NGX_HAVE_C99_VARIADIC_MACROS)
 
-#define HAVE_VARIADIC_MACROS  1
+#define NGX_HAVE_VARIADIC_MACROS  1
 
 #define ngx_log_error(level, log, ...) \
         if (log->log_level >= level) ngx_log_error_core(level, log, __VA_ARGS__)
@@ -81,7 +81,7 @@
 
 #else /* NO VARIADIC MACROS */
 
-#define HAVE_VARIADIC_MACROS  0
+#define NGX_HAVE_VARIADIC_MACROS  0
 
 void ngx_log_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
                    const char *fmt, ...);
@@ -98,7 +98,7 @@
 
 #if (NGX_DEBUG)
 
-#if (HAVE_VARIADIC_MACROS)
+#if (NGX_HAVE_VARIADIC_MACROS)
 
 #define ngx_log_debug0(level, log, err, fmt) \
     if (log->log_level & level) \
diff --git a/src/core/ngx_output_chain.c b/src/core/ngx_output_chain.c
index 74e38e1..23b7846 100644
--- a/src/core/ngx_output_chain.c
+++ b/src/core/ngx_output_chain.c
@@ -24,13 +24,13 @@
     size_t        size, bsize;
     ngx_chain_t  *cl, *out, **last_out;
 
-    /*
-     * the short path for the case when the ctx->in chain is empty
-     * and the incoming chain is empty too or it has the single buf
-     * that does not require the copy
-     */
+    if (ctx->in == NULL && ctx->busy == NULL) {
 
-    if (ctx->in == NULL) {
+       /*
+        * the short path for the case when the ctx->in and ctx->busy chains
+        * are empty, the incoming chain is empty too or has the single buf
+        * that does not require the copy
+        */
 
         if (in == NULL) {
             return ctx->output_filter(ctx->filter_ctx, in);
@@ -192,6 +192,7 @@
     }
 
     if (!ctx->sendfile) {
+
         if (!ngx_buf_in_memory(buf)) {
             return 1;
         }
@@ -228,12 +229,19 @@
         src->pos += size;
         dst->last += size;
 
-        if (src->in_file && sendfile) {
-            dst->in_file = 1;
-            dst->file = src->file;
-            dst->file_pos = src->file_pos;
+        if (src->in_file) {
+
+            if (sendfile) {
+                dst->in_file = 1;
+                dst->file = src->file;
+                dst->file_pos = src->file_pos;
+                dst->file_last = src->file_pos + size;
+
+            } else {
+                dst->in_file = 0;
+            }
+
             src->file_pos += size;
-            dst->file_last = src->file_pos;
 
         } else {
             dst->in_file = 0;
@@ -271,14 +279,14 @@
             dst->in_file = 1;
             dst->file = src->file;
             dst->file_pos = src->file_pos;
-            src->file_pos += size;
-            dst->file_last = src->file_pos;
+            dst->file_last = src->file_pos + n;
 
         } else {
             dst->in_file = 0;
-            src->file_pos += n;
         }
 
+        src->file_pos += n;
+
         if (src->last_buf && src->file_pos == src->file_last) {
             dst->last_buf = 1;
         }
diff --git a/src/core/ngx_string.c b/src/core/ngx_string.c
index 32a4079..8e4e09a 100644
--- a/src/core/ngx_string.c
+++ b/src/core/ngx_string.c
@@ -28,6 +28,238 @@
 }
 
 
+/*
+ * supported formats:
+ *    %[0][width]O     off_t
+ *    %[0][width]T     time_t
+ *    %[0][width]S     ssize_t
+ *    %[0][width]uS    size_t
+ *    %[0][width]uxS   size_t in hex
+ *    %[0][width]l     long
+ *    %[0][width]d     int
+ *    %[0][width]i     ngx_int_t
+ *    %[0][width]ui    ngx_uint_t
+ *    %[0][width]uxi   ngx_uint_t in hex
+ *    %s               null-terminated string
+ *    %%               %
+ *
+ */
+
+u_char *ngx_sprintf(u_char *buf, char *fmt, ...)
+{
+    u_char        *p, c, temp[NGX_MAX_INT_LEN];
+    int            d;
+    long           l;
+    off_t          offset;
+    size_t         size, len;
+    ssize_t        ssize;
+    time_t         sec;
+    va_list        arg;
+    ngx_int_t      i;
+    ngx_uint_t     ui, zero, width, sign, hexadecimal;
+    static u_char  hex[] = "0123456789abcdef";
+
+    va_start(arg, fmt);
+
+    while (*fmt) {
+        if (*fmt == '%') {
+
+            zero = (*++fmt == '0') ? 1 : 0;
+            width = 0;
+            sign = 1;
+            hexadecimal = 0;
+
+            p = temp + NGX_MAX_INT_LEN;
+
+            while (*fmt >= '0' && *fmt <= '9') {
+                width = width * 10 + *fmt++ - '0';
+            }
+
+
+            for ( ;; ) {
+                switch (*fmt) {
+
+                case 'u':
+                    sign = 0;
+                    fmt++;
+                    continue;
+
+                case 'x':
+                    hexadecimal = 1;
+                    fmt++;
+                    continue;
+
+                default:
+                    break;
+                }
+
+                break;
+            }
+
+
+            switch (*fmt) {
+
+            case 'O':
+                offset = va_arg(arg, off_t);
+
+                if (offset < 0) {
+                    *buf++ = '-';
+                    offset = -offset;
+                }
+
+                do {
+                    *--p = (u_char) (offset % 10 + '0');
+                } while (offset /= 10);
+
+                break;
+
+            case 'T':
+                sec = va_arg(arg, time_t);
+
+                if (sec < 0) {
+                    *buf++ = '-';
+                    sec = -sec;
+                }
+
+                do {
+                    *--p = (u_char) (sec % 10 + '0');
+                } while (sec /= 10);
+
+                break;
+
+            case 'S':
+                if (sign) {
+                    ssize = va_arg(arg, ssize_t);
+
+                    if (ssize < 0) {
+                        *buf++ = '-';
+                        size = (size_t) -ssize;
+
+                    } else {
+                        size = (size_t) ssize;
+                    }
+
+                } else {
+                    size = va_arg(arg, size_t);
+                }
+
+                if (hexadecimal) {
+                    do {
+                        *--p = hex[size & 0xf];
+                    } while (size >>= 4);
+
+                } else {
+                    do {
+                        *--p = (u_char) (size % 10 + '0');
+                    } while (size /= 10);
+                }
+
+                break;
+
+            case 'l':
+                l = va_arg(arg, long);
+
+                if (l < 0) {
+                    *buf++ = '-';
+                    l = -l;
+                }
+
+                do {
+                    *--p = (u_char) (l % 10 + '0');
+                } while (l /= 10);
+
+                break;
+
+            case 'd':
+                d = va_arg(arg, int);
+
+                if (d < 0) {
+                    *buf++ = '-';
+                    d = -d;
+                }
+
+                do {
+                    *--p = (u_char) (d % 10 + '0');
+                } while (d /= 10);
+
+                break;
+
+            case 'i':
+                if (sign) {
+                    i = va_arg(arg, ngx_int_t);
+
+                    if (i < 0) {
+                        *buf++ = '-';
+                        ui = (ngx_uint_t) -i;
+
+                    } else {
+                        ui = (ngx_uint_t) i;
+                    }
+
+                } else {
+                    ui = va_arg(arg, ngx_uint_t);
+                }
+
+                if (hexadecimal) {
+                    do {
+                        *--p = hex[ui & 0xf];
+                    } while (ui >>= 4);
+
+                } else {
+                    do {
+                        *--p = (u_char) (ui % 10 + '0');
+                    } while (ui /= 10);
+                }
+
+                break;
+
+            case 's':
+                p = va_arg(arg, u_char *);
+
+                while (*p) {
+                    *buf++ = *p++;
+                }
+                fmt++;
+
+                continue;
+
+            case '%':
+                *buf++ = '%';
+                fmt++;
+
+                continue;
+
+            default:
+                *buf++ = *fmt++;
+
+                continue;
+            }
+
+            len = (temp + NGX_MAX_INT_LEN) - p;
+
+            c = (u_char) (zero ? '0' : ' ');
+
+            while (len++ < width) {
+                *buf++ = c;
+            }
+
+            buf = ngx_cpymem(buf, p, ((temp + NGX_MAX_INT_LEN) - p));
+
+            fmt++;
+
+        } else {
+            *buf++ = *fmt++;
+        }
+    }
+
+    va_end(arg);
+
+    *buf = '\0';
+
+    return buf;
+}
+
+
 ngx_int_t ngx_rstrncmp(u_char *s1, u_char *s2, size_t n)
 {
     if (n == 0) {
@@ -50,6 +282,40 @@
 }
 
 
+ngx_int_t ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n)
+{
+    u_char  c1, c2;
+
+    if (n == 0) {
+        return 0;
+    }
+
+    n--;
+
+    for ( ;; ) {
+        c1 = s1[n];
+        if (c1 >= 'a' && c1 <= 'z') {
+            c1 -= 'a' - 'A';
+        }
+
+        c2 = s2[n];
+        if (c2 >= 'a' && c2 <= 'z') {
+            c2 -= 'a' - 'A';
+        }
+
+        if (c1 != c2) {
+            return c1 - c2;
+        }
+
+        if (n == 0) {
+            return 0;
+        }
+
+        n--;
+    }
+}
+
+
 ngx_int_t ngx_atoi(u_char *line, size_t n)
 {
     ngx_int_t  value;
diff --git a/src/core/ngx_string.h b/src/core/ngx_string.h
index e8e69c0..d7caaaf 100644
--- a/src/core/ngx_string.h
+++ b/src/core/ngx_string.h
@@ -70,7 +70,10 @@
 #define ngx_memcmp                memcmp
 
 u_char *ngx_cpystrn(u_char *dst, u_char *src, size_t n);
+u_char *ngx_sprintf(u_char *buf, char *fmt, ...);
+
 ngx_int_t ngx_rstrncmp(u_char *s1, u_char *s2, size_t n);
+ngx_int_t ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n);
 
 ngx_int_t ngx_atoi(u_char *line, size_t n);
 ngx_int_t ngx_hextoi(u_char *line, size_t n);
diff --git a/src/core/ngx_times.c b/src/core/ngx_times.c
index 2ca31ac..40dbbdb 100644
--- a/src/core/ngx_times.c
+++ b/src/core/ngx_times.c
@@ -212,12 +212,25 @@
 }
 
 
+u_char *ngx_http_time(u_char *buf, time_t t)
+#if 0
 size_t ngx_http_time(u_char *buf, time_t t)
+#endif
 {
     ngx_tm_t  tm;
 
     ngx_gmtime(t, &tm);
 
+    return ngx_sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d GMT",
+                       week[tm.ngx_tm_wday],
+                       tm.ngx_tm_mday,
+                       months[tm.ngx_tm_mon - 1],
+                       tm.ngx_tm_year,
+                       tm.ngx_tm_hour,
+                       tm.ngx_tm_min,
+                       tm.ngx_tm_sec);
+
+#if 0
     return ngx_snprintf((char *) buf, sizeof("Mon, 28 Sep 1970 06:00:00 GMT"),
                                       "%s, %02d %s %4d %02d:%02d:%02d GMT",
                                       week[tm.ngx_tm_wday],
@@ -227,10 +240,14 @@
                                       tm.ngx_tm_hour,
                                       tm.ngx_tm_min,
                                       tm.ngx_tm_sec);
+#endif
 }
 
 
+u_char *ngx_http_cookie_time(u_char *buf, time_t t)
+#if 0
 size_t ngx_http_cookie_time(u_char *buf, time_t t)
+#endif
 {
     ngx_tm_t  tm;
 
@@ -241,6 +258,20 @@
      * 2-digit years more than "37"
      */
 
+    return ngx_sprintf(buf,
+                       (tm.ngx_tm_year > 2037) ?
+                                         "%s, %02d-%s-%d %02d:%02d:%02d GMT":
+                                         "%s, %02d-%s-%02d %02d:%02d:%02d GMT",
+                       week[tm.ngx_tm_wday],
+                       tm.ngx_tm_mday,
+                       months[tm.ngx_tm_mon - 1],
+                       (tm.ngx_tm_year > 2037) ? tm.ngx_tm_year:
+                                                 tm.ngx_tm_year % 100,
+                       tm.ngx_tm_hour,
+                       tm.ngx_tm_min,
+                       tm.ngx_tm_sec);
+
+#if 0
     if (tm.ngx_tm_year > 2037) {
         return ngx_snprintf((char *) buf,
                                       sizeof("Mon, 28-Sep-1970 06:00:00 GMT"),
@@ -264,6 +295,7 @@
                                       tm.ngx_tm_min,
                                       tm.ngx_tm_sec);
     }
+#endif
 }
 
 
diff --git a/src/core/ngx_times.h b/src/core/ngx_times.h
index e1d1515..5eabac5 100644
--- a/src/core/ngx_times.h
+++ b/src/core/ngx_times.h
@@ -14,8 +14,8 @@
 
 void ngx_time_init();
 void ngx_time_update(time_t s);
-size_t ngx_http_time(u_char *buf, time_t t);
-size_t ngx_http_cookie_time(u_char *buf, time_t t);
+u_char *ngx_http_time(u_char *buf, time_t t);
+u_char *ngx_http_cookie_time(u_char *buf, time_t t);
 void ngx_gmtime(time_t t, ngx_tm_t *tp);
 
 #if (NGX_THREADS)
diff --git a/src/event/modules/ngx_epoll_module.c b/src/event/modules/ngx_epoll_module.c
index 4067fd8..45791e9 100644
--- a/src/event/modules/ngx_epoll_module.c
+++ b/src/event/modules/ngx_epoll_module.c
@@ -173,7 +173,7 @@
 #else
     ngx_event_flags = NGX_USE_LEVEL_EVENT
 #endif
-                      |NGX_HAVE_GREEDY_EVENT
+                      |NGX_USE_GREEDY_EVENT
                       |NGX_USE_EPOLL_EVENT;
 
     return NGX_OK;
diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c
index db5b756..e6bebb5 100644
--- a/src/event/modules/ngx_kqueue_module.c
+++ b/src/event/modules/ngx_kqueue_module.c
@@ -194,10 +194,10 @@
 #else
                      |NGX_USE_LEVEL_EVENT
 #endif
-#if (HAVE_LOWAT_EVENT)
-                     |NGX_HAVE_LOWAT_EVENT
+#if (NGX_HAVE_LOWAT_EVENT)
+                     |NGX_USE_LOWAT_EVENT
 #endif
-                     |NGX_HAVE_KQUEUE_EVENT;
+                     |NGX_USE_KQUEUE_EVENT;
 
     return NGX_OK;
 }
@@ -245,6 +245,8 @@
         return NGX_ERROR;
     }
 
+#if 1
+
     if (nchanges > 0
         && ev->index < (u_int) nchanges
         && ((uintptr_t) change_list[ev->index].udata & (uintptr_t) ~1)
@@ -262,7 +264,8 @@
                            ngx_event_ident(ev->data), event);
 
             if (ev->index < (u_int) --nchanges) {
-                e = (ngx_event_t *) change_list[nchanges].udata;
+                e = (ngx_event_t *)
+                    ((uintptr_t) change_list[nchanges].udata & (uintptr_t) ~1);
                 change_list[ev->index] = change_list[nchanges];
                 e->index = ev->index;
             }
@@ -282,6 +285,8 @@
         return NGX_ERROR;
     }
 
+#endif
+
     rc = ngx_kqueue_set_event(ev, event, EV_ADD|EV_ENABLE|flags);
 
     ngx_mutex_unlock(list_mutex);
@@ -302,6 +307,8 @@
         return NGX_ERROR;
     }
 
+#if 1
+
     if (nchanges > 0
         && ev->index < (u_int) nchanges
         && ((uintptr_t) change_list[ev->index].udata & (uintptr_t) ~1)
@@ -314,7 +321,8 @@
         /* if the event is still not passed to a kernel we will not pass it */
 
         if (ev->index < (u_int) --nchanges) {
-            e = (ngx_event_t *) change_list[nchanges].udata;
+            e = (ngx_event_t *)
+                    ((uintptr_t) change_list[nchanges].udata & (uintptr_t) ~1);
             change_list[ev->index] = change_list[nchanges];
             e->index = ev->index;
         }
@@ -324,6 +332,8 @@
         return NGX_OK;
     }
 
+#endif
+
     /*
      * when the file descriptor is closed the kqueue automatically deletes
      * its filters so we do not need to delete explicity the event
@@ -393,7 +403,7 @@
         kev->data = 0;
 
     } else {
-#if (HAVE_LOWAT_EVENT)
+#if (NGX_HAVE_LOWAT_EVENT)
         if (flags & NGX_LOWAT_EVENT) {
             kev->fflags = NOTE_LOWAT;
             kev->data = ev->available;
diff --git a/src/event/modules/ngx_rtsig_module.c b/src/event/modules/ngx_rtsig_module.c
index 72c5b7c..4f2f480 100644
--- a/src/event/modules/ngx_rtsig_module.c
+++ b/src/event/modules/ngx_rtsig_module.c
@@ -158,7 +158,7 @@
 
     ngx_event_actions = ngx_rtsig_module_ctx.actions;
 
-    ngx_event_flags = NGX_USE_RTSIG_EVENT|NGX_HAVE_GREEDY_EVENT;
+    ngx_event_flags = NGX_USE_RTSIG_EVENT|NGX_USE_GREEDY_EVENT;
 
     return NGX_OK;
 }
diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c
index 4718dcd..f006c6b 100644
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -465,9 +465,9 @@
 {
     int  sndlowat;
 
-#if (HAVE_LOWAT_EVENT)
+#if (NGX_HAVE_LOWAT_EVENT)
 
-    if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+    if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
         c->write->available = lowat;
         return NGX_OK;
     }
diff --git a/src/event/ngx_event.h b/src/event/ngx_event.h
index c173adc..f341a14 100644
--- a/src/event/ngx_event.h
+++ b/src/event/ngx_event.h
@@ -240,19 +240,19 @@
  * The event filter has kqueue features - the eof flag, errno,
  * available data, etc.
  */
-#define NGX_HAVE_KQUEUE_EVENT    0x00000008
+#define NGX_USE_KQUEUE_EVENT    0x00000008
 
 /*
  * The event filter supports low water mark - kqueue's NOTE_LOWAT.
  * kqueue in FreeBSD 4.1-4.2 has no NOTE_LOWAT so we need a separate flag.
  */
-#define NGX_HAVE_LOWAT_EVENT     0x00000010
+#define NGX_USE_LOWAT_EVENT     0x00000010
 
 /*
  * The event filter requires to do i/o operation until EAGAIN -
  * epoll, rt signals.
  */
-#define NGX_HAVE_GREEDY_EVENT    0x00000020
+#define NGX_USE_GREEDY_EVENT    0x00000020
 
 /*
  * The event filter is epoll,
@@ -571,7 +571,7 @@
     ngx_connection_t  *c;
 
     if (lowat) {
-        c = wev->data;
+        c = (ngx_connection_t *) wev->data;
 
         if (ngx_send_lowat(c, lowat) == NGX_ERROR) {
             return NGX_ERROR;
diff --git a/src/event/ngx_event_accept.c b/src/event/ngx_event_accept.c
index 5a5e0e1..9ae5ccd 100644
--- a/src/event/ngx_event_accept.c
+++ b/src/event/ngx_event_accept.c
@@ -39,7 +39,7 @@
     if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
         ev->available = 1;
 
-    } else if (!(ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)) {
+    } else if (!(ngx_event_flags & NGX_USE_KQUEUE_EVENT)) {
         ev->available = ecf->multi_accept;
     }
 
@@ -118,7 +118,7 @@
                           ls->listening->addr_text.data);
 
             if (err == NGX_ECONNABORTED) {
-                if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+                if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
                     ev->available--;
                 }
 
@@ -342,7 +342,7 @@
 
         ls->listening->handler(c);
 
-        if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+        if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
             ev->available--;
         }
 
diff --git a/src/event/ngx_event_connect.c b/src/event/ngx_event_connect.c
index c1e6c19..b41eca7 100644
--- a/src/event/ngx_event_connect.c
+++ b/src/event/ngx_event_connect.c
@@ -296,6 +296,9 @@
 
     if (ngx_event_flags & NGX_USE_AIO_EVENT) {
 
+        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, pc->log, ngx_socket_errno,
+                       "connect(): %d", rc);
+
         /* aio, iocp */
 
         if (ngx_blocking(s) == -1) {
@@ -311,8 +314,7 @@
         }
 
         /*
-         * aio allows to post operation on non-connected socket
-         * at least in FreeBSD.
+         * FreeBSD aio allows to post operation on non-connected socket.
          * NT does not support it.
          * 
          * TODO: check in Win32, etc. As workaround we can use NGX_ONESHOT_EVENT
diff --git a/src/event/ngx_event_openssl.c b/src/event/ngx_event_openssl.c
index 151cc42..7a7038d 100644
--- a/src/event/ngx_event_openssl.c
+++ b/src/event/ngx_event_openssl.c
@@ -86,6 +86,7 @@
     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
 
     if (sslerr == SSL_ERROR_WANT_READ) {
+        c->read->ready = 0;
         return NGX_AGAIN;
     }
 
diff --git a/src/http/modules/ngx_http_charset_filter.c b/src/http/modules/ngx_http_charset_filter.c
index d22a86b..f2e85e5 100644
--- a/src/http/modules/ngx_http_charset_filter.c
+++ b/src/http/modules/ngx_http_charset_filter.c
@@ -12,7 +12,7 @@
 typedef struct {
     char       **tables;
     ngx_str_t    name;
-    unsigned     server;
+    ngx_uint_t   server;  /* unsigned     server:1; */
 } ngx_http_charset_t;
 
 
@@ -45,7 +45,7 @@
 } ngx_http_charset_ctx_t;
 
 
-static void ngx_charset_recode(ngx_buf_t *b, char *table);
+static ngx_uint_t ngx_charset_recode(ngx_buf_t *b, char *table);
 
 static char *ngx_charset_map_block(ngx_conf_t *cf, ngx_command_t *cmd,
                                    void *conf);
@@ -232,14 +232,31 @@
 }
 
 
-static void ngx_charset_recode(ngx_buf_t *b, char *table)
+static ngx_uint_t ngx_charset_recode(ngx_buf_t *b, char *table)
 {
-    u_char  *p, c;
+    u_char      *p;
+    ngx_uint_t   change;
+
+    change = 0;
 
     for (p = b->pos; p < b->last; p++) {
-        c = *p;
-        *p = table[c];
+        if (*p != table[*p]) {
+            change = 1;
+            break;
+        }
     }
+
+    if (change) {
+
+        while (p < b->last) {
+            *p = table[*p];
+            p++;
+        }
+
+        b->in_file = 0;
+    }
+
+    return change;
 }
 
 
@@ -419,7 +436,9 @@
         return NGX_ERROR;
     }
 
+    c->tables = NULL;
     c->name = *name;
+    c->server = 0;
 
     return i;
 }
diff --git a/src/http/modules/ngx_http_chunked_filter.c b/src/http/modules/ngx_http_chunked_filter.c
index 2112461..7881248 100644
--- a/src/http/modules/ngx_http_chunked_filter.c
+++ b/src/http/modules/ngx_http_chunked_filter.c
@@ -63,7 +63,7 @@
                                               ngx_chain_t *in)
 {
     u_char       *chunk;
-    size_t        size, len;
+    size_t        size;
     ngx_buf_t    *b;
     ngx_chain_t   out, tail, *cl, *tl, **ll;
 
@@ -98,6 +98,20 @@
     }
 
     if (size) {
+        if (!(b = ngx_calloc_buf(r->pool))) {
+            return NGX_ERROR;
+        }
+
+        if (!(chunk = ngx_palloc(r->pool, 11))) {
+            return NGX_ERROR;
+        }
+
+        b->temporary = 1;
+        b->pos = chunk;
+        b->last = ngx_sprintf(chunk, "%uxS" CRLF, size);
+
+        out.buf = b;
+#if 0
         ngx_test_null(chunk, ngx_palloc(r->pool, 11), NGX_ERROR);
         len = ngx_snprintf((char *) chunk, 11, SIZE_T_X_FMT CRLF, size);
 
@@ -107,6 +121,7 @@
         b->last = chunk + len;
 
         out.buf = b;
+#endif
     }
 
     if (cl->buf->last_buf) {
diff --git a/src/http/modules/ngx_http_gzip_filter.c b/src/http/modules/ngx_http_gzip_filter.c
index 4089ffb..bb3a1f0 100644
--- a/src/http/modules/ngx_http_gzip_filter.c
+++ b/src/http/modules/ngx_http_gzip_filter.c
@@ -837,26 +837,30 @@
         return buf + 1;
     }
 
-#if 0
-    return buf + ngx_snprintf((char *) buf, NGX_INT32_LEN + 4, "%.2f",
-                              (float) ctx->zin / ctx->zout);
-#endif
-
     /* we prefer do not use the FPU */
 
     zint = (ngx_uint_t) (ctx->zin / ctx->zout);
     zfrac = (ngx_uint_t) ((ctx->zin * 100 / ctx->zout) % 100);
 
-    if ((ctx->zin * 1000 / ctx->zout) %10 > 4) {
-        if (++zfrac > 99) {
+    if ((ctx->zin * 1000 / ctx->zout) % 10 > 4) {
+
+        /* the rounding, e.g., 2.125 to 2.13 */
+
+        zfrac++;
+
+        if (zfrac > 99) {
             zint++;
             zfrac = 0;
         }
     }
 
+    return ngx_sprintf(buf, "%ui.%02ui", zint, zfrac);
+
+#if 0
     return buf + ngx_snprintf((char *) buf, NGX_INT32_LEN + 4,
                               "%" NGX_UINT_T_FMT ".%02" NGX_UINT_T_FMT,
                               zint, zfrac);
+#endif
 }
 
 
diff --git a/src/http/modules/ngx_http_headers_filter.c b/src/http/modules/ngx_http_headers_filter.c
index f7fe52c..faf8192 100644
--- a/src/http/modules/ngx_http_headers_filter.c
+++ b/src/http/modules/ngx_http_headers_filter.c
@@ -134,10 +134,16 @@
                         return NGX_ERROR;
                     }
 
+                    cc->value.len = ngx_sprintf(cc->value.data, "max-age=%T",
+                                                conf->expires)
+                                    - cc->value.data;
+
+#if 0
                     cc->value.len = ngx_snprintf((char *) cc->value.data,
                                                sizeof("max-age=") + TIME_T_LEN,
                                                "max-age=" TIME_T_FMT,
                                                conf->expires);
+#endif
                 }
             }
         }
diff --git a/src/http/modules/ngx_http_range_filter.c b/src/http/modules/ngx_http_range_filter.c
index a08e25f..082fd3c 100644
--- a/src/http/modules/ngx_http_range_filter.c
+++ b/src/http/modules/ngx_http_range_filter.c
@@ -264,9 +264,14 @@
         }
 
         r->headers_out.content_range->value.len =
+                ngx_sprintf(r->headers_out.content_range->value.data,
+                            "bytes */%O", r->headers_out.content_length_n)
+                - r->headers_out.content_range->value.data;
+#if 0
                 ngx_snprintf((char *) r->headers_out.content_range->value.data,
                              8 + 20 + 1, "bytes */" OFF_T_FMT,
                              r->headers_out.content_length_n);
+#endif
 
         r->headers_out.content_length_n = -1;
         if (r->headers_out.content_length) {
@@ -297,12 +302,20 @@
             /* "Content-Range: bytes SSSS-EEEE/TTTT" header */
 
             r->headers_out.content_range->value.len =
+                         ngx_sprintf(r->headers_out.content_range->value.data,
+                                     "bytes %O-%O/%O",
+                                     range->start, range->end - 1,
+                                     r->headers_out.content_length_n)
+                         - r->headers_out.content_range->value.data;
+
+#if 0
                    ngx_snprintf((char *)
                                 r->headers_out.content_range->value.data,
                                 6 + 20 + 1 + 20 + 1 + 20 + 1,
                                 "bytes " OFF_T_FMT "-" OFF_T_FMT "/" OFF_T_FMT,
                                 range->start, range->end - 1,
                                 r->headers_out.content_length_n);
+#endif
 
             r->headers_out.content_length_n = range->end - range->start;
 
@@ -343,6 +356,15 @@
 
             if (r->headers_out.charset.len) {
                 ctx->boundary_header.len =
+                          ngx_sprintf(ctx->boundary_header.data,
+                                      CRLF "--%010ui" CRLF
+                                      "Content-Type: %s; charset=%s" CRLF
+                                      "Content-Range: bytes ",
+                                      boundary,
+                                      r->headers_out.content_type->value.data,
+                                      r->headers_out.charset.data)
+                          - ctx->boundary_header.data;
+#if 0
                          ngx_snprintf((char *) ctx->boundary_header.data, len,
                                       CRLF "--%010" NGX_UINT_T_FMT CRLF
                                       "Content-Type: %s; charset=%s" CRLF
@@ -350,17 +372,29 @@
                                       boundary,
                                       r->headers_out.content_type->value.data,
                                       r->headers_out.charset.data);
+#endif
 
                 r->headers_out.charset.len = 0;
 
             } else {
                 ctx->boundary_header.len =
+                          ngx_sprintf(ctx->boundary_header.data,
+                                      CRLF "--%010ui" CRLF
+                                      "Content-Type: %s" CRLF
+                                      "Content-Range: bytes ",
+                                      boundary,
+                                      r->headers_out.content_type->value.data)
+                          - ctx->boundary_header.data;
+
+#if 0
                          ngx_snprintf((char *) ctx->boundary_header.data, len,
                                       CRLF "--%010" NGX_UINT_T_FMT CRLF
                                       "Content-Type: %s" CRLF
                                       "Content-Range: bytes ",
                                       boundary,
                                       r->headers_out.content_type->value.data);
+
+#endif
             }
 
             ngx_test_null(r->headers_out.content_type->value.data,
@@ -370,12 +404,18 @@
             /* "Content-Type: multipart/byteranges; boundary=0123456789" */
 
             r->headers_out.content_type->value.len =
+                       ngx_sprintf(r->headers_out.content_type->value.data,
+                                   "multipart/byteranges; boundary=%010ui",
+                                   boundary)
+                       - r->headers_out.content_type->value.data;
+#if 0
                       ngx_snprintf((char *)
                                    r->headers_out.content_type->value.data,
                                    31 + 10 + 1,
                                    "multipart/byteranges; boundary=%010"
                                    NGX_UINT_T_FMT,
                                    boundary);
+#endif
 
             /* the size of the last boundary CRLF "--0123456789--" CRLF */
             len = 4 + 10 + 4;
@@ -389,11 +429,18 @@
                 /* the size of the range: "SSSS-EEEE/TTTT" CRLF CRLF */
 
                 range[i].content_range.len =
+                                 ngx_sprintf(range[i].content_range.data,
+                                             "%O-%O/%O" CRLF CRLF,
+                                             range[i].start, range[i].end - 1,
+                                             r->headers_out.content_length_n)
+                                 - range[i].content_range.data;
+#if 0
                   ngx_snprintf((char *) range[i].content_range.data,
                                20 + 1 + 20 + 1 + 20 + 5,
                                OFF_T_FMT "-" OFF_T_FMT "/" OFF_T_FMT CRLF CRLF,
                                range[i].start, range[i].end - 1,
                                r->headers_out.content_length_n);
+#endif
 
                 len += ctx->boundary_header.len + range[i].content_range.len
                                     + (size_t) (range[i].end - range[i].start);
diff --git a/src/http/modules/ngx_http_status_handler.c b/src/http/modules/ngx_http_status_handler.c
index 6206ac3..357afff 100644
--- a/src/http/modules/ngx_http_status_handler.c
+++ b/src/http/modules/ngx_http_status_handler.c
@@ -159,6 +159,9 @@
                   + 1 + (r->server_name ? cmcf->max_server_name_len : 1)
                   + 2;                                /* "\r\n" */
 
+            /* BUG: cmcf->max_server_name_len and "*.domain.tld" */
+
+
             if (r->request_line.len) {
                 len += 1 + 1 + r->request_line.len + 1;
             }
diff --git a/src/http/modules/ngx_http_userid_filter.c b/src/http/modules/ngx_http_userid_filter.c
index 5f8e452..bafdea8 100644
--- a/src/http/modules/ngx_http_userid_filter.c
+++ b/src/http/modules/ngx_http_userid_filter.c
@@ -367,7 +367,7 @@
 
     } else if (conf->expires) {
         p = ngx_cpymem(p, expires, sizeof("; expires=") - 1);
-        p += ngx_http_cookie_time(p, ngx_time() + conf->expires);
+        p = ngx_http_cookie_time(p, ngx_time() + conf->expires);
     }
 
     if (conf->domain.len > 1) {
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.c b/src/http/modules/proxy/ngx_http_proxy_handler.c
index f0794f3..5d61167 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -289,6 +289,8 @@
                          offsetof(ngx_http_proxy_headers_in_t, content_type) },
     { ngx_string("Content-Length"),
                        offsetof(ngx_http_proxy_headers_in_t, content_length) },
+    { ngx_string("Content-Encoding"),
+                     offsetof(ngx_http_proxy_headers_in_t, content_encoding) },
     { ngx_string("Last-Modified"),
                         offsetof(ngx_http_proxy_headers_in_t, last_modified) },
     { ngx_string("Location"),
@@ -400,7 +402,7 @@
 
 #if (HAVE_KQUEUE)
 
-    if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+    if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
 
         if (!ev->pending_eof) {
             return;
@@ -1310,10 +1312,10 @@
 
 static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
 {
-#if __FreeBSD__
-
     ssize_t *np = data;
 
+#if (NGX_FREEBSD)
+
     if (*np >= ngx_freebsd_net_inet_tcp_sendspace) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                            "\"proxy_send_lowat\" must be less than %d "
@@ -1323,15 +1325,12 @@
         return NGX_CONF_ERROR;
     }
 
+#elif !(HAVE_SO_SNDLOWAT)
 
-#else
-
-#if 0
     ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
                        "\"proxy_send_lowat\" is not supported, ignored");
 
     *np = 0;
-#endif
 
 #endif
 
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.h b/src/http/modules/proxy/ngx_http_proxy_handler.h
index 728259c..3e721ba 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.h
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.h
@@ -130,6 +130,7 @@
     ngx_table_elt_t                 *connection;
     ngx_table_elt_t                 *content_type;
     ngx_table_elt_t                 *content_length;
+    ngx_table_elt_t                 *content_encoding;
     ngx_table_elt_t                 *last_modified;
     ngx_table_elt_t                 *location;
     ngx_table_elt_t                 *accept_ranges;
diff --git a/src/http/modules/proxy/ngx_http_proxy_header.c b/src/http/modules/proxy/ngx_http_proxy_header.c
index 07722fc..cd5deeb 100644
--- a/src/http/modules/proxy/ngx_http_proxy_header.c
+++ b/src/http/modules/proxy/ngx_http_proxy_header.c
@@ -113,6 +113,11 @@
             continue;
         }
 
+        if (&h[i] == headers_in->content_encoding) {
+            r->headers_out.content_encoding = ho;
+            continue;
+        }
+
         if (&h[i] == headers_in->last_modified) {
             r->headers_out.last_modified = ho;
             /* TODO: update r->headers_out.last_modified_time */
diff --git a/src/http/modules/proxy/ngx_http_proxy_upstream.c b/src/http/modules/proxy/ngx_http_proxy_upstream.c
index be5d69a..88479da 100644
--- a/src/http/modules/proxy/ngx_http_proxy_upstream.c
+++ b/src/http/modules/proxy/ngx_http_proxy_upstream.c
@@ -692,12 +692,14 @@
 
     /* rc == NGX_OK */
 
-#if 1 /* test only, see below about "post aio operation" */
+#if 0 /* test only, see below about "post aio operation" */
 
     if (c->read->ready) {
         /* post aio operation */
         ngx_http_proxy_process_upstream_status_line(c->read);
+#if 0
         return;
+#endif
     }
 
 #endif
@@ -718,7 +720,7 @@
 
 #if (HAVE_KQUEUE)
 
-    if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)
+    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT)
         && !p->request_sent
         && c->write->pending_eof)
     {
@@ -776,7 +778,7 @@
 
     ngx_add_timer(c->read, p->lcf->read_timeout);
 
-#if 0
+#if 1
     if (c->read->ready) {
 
         /* post aio operation */
diff --git a/src/http/ngx_http.c b/src/http/ngx_http.c
index a37ffc6..90bbbe2 100644
--- a/src/http/ngx_http.c
+++ b/src/http/ngx_http.c
@@ -11,6 +11,14 @@
 
 
 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
+static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
+                                      ngx_http_in_port_t *in_port,
+                                      ngx_http_listen_t *lscf,
+                                      ngx_http_core_srv_conf_t *cscf);
+static ngx_int_t ngx_http_add_names(ngx_conf_t *cf,
+                                    ngx_http_in_addr_t *in_addr,
+                                    ngx_http_core_srv_conf_t *cscf);
+
 static char *ngx_http_merge_locations(ngx_conf_t *cf,
                                       ngx_array_t *locations,
                                       void **loc_conf,
@@ -79,6 +87,11 @@
     ngx_iocp_conf_t             *iocpcf;
 #endif
 
+#if (NGX_SUPPRESS_WARN)
+    /* MSVC thinks 'in_ports' may be used without having been initialized */
+    ngx_memzero(&in_ports, sizeof(ngx_array_t));
+#endif
+
     /* the main http context */
     ngx_test_null(ctx,
                   ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)),
@@ -274,18 +287,23 @@
 
 
     /*
-     * create the lists of the ports, the addresses and the server names
-     * to allow quickly find the server core module configuration at run-time
+     * create the lists of ports, addresses and server names
+     * to quickly find the server core module configuration at run-time
      */
 
-    ngx_init_array(in_ports, cf->pool, 10, sizeof(ngx_http_in_port_t),
-                   NGX_CONF_ERROR);
+    if (ngx_array_init(&in_ports, cf->pool, 10, sizeof(ngx_http_in_port_t))
+                                                                  == NGX_ERROR)
+    {
+        return NGX_CONF_ERROR;
+    }
 
     /* "server" directives */
+
     cscfp = cmcf->servers.elts;
     for (s = 0; s < cmcf->servers.nelts; s++) {
 
         /* "listen" directives */
+
         lscf = cscfp[s]->listen.elts;
         for (l = 0; l < cscfp[s]->listen.nelts; l++) {
 
@@ -308,38 +326,26 @@
 
                         if (lscf[l].addr == in_addr[a].addr) {
 
-                            /* the address is already bound to this port */
+                            /* the address is already in the address list */
 
-                            /* "server_name" directives */
-                            s_name = cscfp[s]->server_names.elts;
-                            for (n = 0; n < cscfp[s]->server_names.nelts; n++) {
-
-                                /*
-                                 * add the server name and server core module
-                                 * configuration to the address:port
-                                 */
-
-                                /* TODO: duplicate names can be checked here */
-
-                                ngx_test_null(name,
-                                              ngx_push_array(&in_addr[a].names),
-                                              NGX_CONF_ERROR);
-
-                                name->name = s_name[n].name;
-                                name->core_srv_conf = s_name[n].core_srv_conf;
+                            if (ngx_http_add_names(cf, &in_addr[a], cscfp[s])
+                                                                  == NGX_ERROR)
+                            {
+                                return NGX_CONF_ERROR;
                             }
 
                             /*
-                             * check duplicate "default" server that
-                             * serves this address:port
+                             * check the duplicate "default" server 
+                             * for this address:port
                              */
 
                             if (lscf[l].default_server) {
+
                                 if (in_addr[a].default_server) {
                                     ngx_log_error(NGX_LOG_ERR, cf->log, 0,
-                                           "duplicate default server in %s:%d",
-                                           lscf[l].file_name.data,
-                                           lscf[l].line);
+                                        "the duplicate default server in %s:%d",
+                                        lscf[l].file_name.data,
+                                        lscf[l].line);
 
                                     return NGX_CONF_ERROR;
                                 }
@@ -354,31 +360,31 @@
 
                         } else if (in_addr[a].addr == INADDR_ANY) {
 
-                            /*
-                             * "*:port" must be the last resort so move it
-                             * to the end of the address list and add
-                             * the new address at its place
-                             */
+                            /* the INADDR_ANY is always the last address */
 
-                            ngx_test_null(inaddr,
-                                          ngx_push_array(&in_port[p].addrs),
-                                          NGX_CONF_ERROR);
+                            if (!(inaddr = ngx_array_push(&in_port[p].addrs))) {
+                                return NGX_CONF_ERROR;
+                            }
+
+                            /*
+                             * the INADDR_ANY must be the last resort
+                             * so we move it to the end of the address list
+                             * and put the new address in its place
+                             */
 
                             ngx_memcpy(inaddr, &in_addr[a],
                                        sizeof(ngx_http_in_addr_t));
 
                             in_addr[a].addr = lscf[l].addr;
+                            in_addr[a].names.elts = NULL;
                             in_addr[a].default_server = lscf[l].default_server;
                             in_addr[a].core_srv_conf = cscfp[s];
 
-                            /*
-                             * create the empty list of the server names that
-                             * can be served on this address:port
-                             */
-
-                            ngx_init_array(inaddr->names, cf->pool, 10,
-                                           sizeof(ngx_http_server_name_t),
-                                           NGX_CONF_ERROR);
+                            if (ngx_http_add_names(cf, &in_addr[a], cscfp[s])
+                                                                  == NGX_ERROR)
+                            {
+                                return NGX_CONF_ERROR;
+                            }
 
                             addr_found = 1;
 
@@ -393,22 +399,11 @@
                          * bound to this port
                          */
 
-                        ngx_test_null(inaddr,
-                                      ngx_push_array(&in_port[p].addrs),
-                                      NGX_CONF_ERROR);
-
-                        inaddr->addr = lscf[l].addr;
-                        inaddr->default_server = lscf[l].default_server;
-                        inaddr->core_srv_conf = cscfp[s];
-
-                        /*
-                         * create the empty list of the server names that
-                         * can be served on this address:port
-                         */
-
-                        ngx_init_array(inaddr->names, cf->pool, 10,
-                                       sizeof(ngx_http_server_name_t),
-                                       NGX_CONF_ERROR);
+                        if (ngx_http_add_address(cf, &in_port[p], &lscf[l],
+                                                        cscfp[s]) == NGX_ERROR)
+                        {
+                            return NGX_CONF_ERROR;
+                        }
                     }
                 }
             }
@@ -417,54 +412,42 @@
 
                 /* add the port to the in_port list */
 
-                ngx_test_null(in_port,
-                              ngx_push_array(&in_ports),
-                              NGX_CONF_ERROR);
+                if (!(in_port = ngx_array_push(&in_ports))) {
+                    return NGX_CONF_ERROR;
+                }
 
                 in_port->port = lscf[l].port;
+                in_port->addrs.elts = NULL;
 
-                ngx_test_null(in_port->port_text.data, ngx_palloc(cf->pool, 7),
-                              NGX_CONF_ERROR);
-                in_port->port_text.len = ngx_snprintf((char *)
-                                                      in_port->port_text.data,
-                                                      7, ":%d",
-                                                      in_port->port);
+                if (!(in_port->port_text.data = ngx_palloc(cf->pool, 7))) {
+                    return NGX_CONF_ERROR;
+                }
 
-                /* create list of the addresses that bound to this port ... */
+                in_port->port_text.len = ngx_sprintf(in_port->port_text.data,
+                                                     ":%d", in_port->port)
+                                         - in_port->port_text.data;
 
-                ngx_init_array(in_port->addrs, cf->pool, 10,
-                               sizeof(ngx_http_in_addr_t),
-                               NGX_CONF_ERROR);
-
-                ngx_test_null(inaddr, ngx_push_array(&in_port->addrs),
-                              NGX_CONF_ERROR);
-
-                /* ... and add the address to this list */
-
-                inaddr->addr = lscf[l].addr;
-                inaddr->default_server = lscf[l].default_server;
-                inaddr->core_srv_conf = cscfp[s];
-
-                /*
-                 * create the empty list of the server names that
-                 * can be served on this address:port
-                 */
-
-                ngx_init_array(inaddr->names, cf->pool, 10,
-                               sizeof(ngx_http_server_name_t),
-                               NGX_CONF_ERROR);
+                if (ngx_http_add_address(cf, in_port, &lscf[l], cscfp[s])
+                                                                  == NGX_ERROR)
+                {
+                    return NGX_CONF_ERROR;
+                }
             }
         }
     }
 
-    /* optimize the lists of the ports, the addresses and the server names */
+
+    /* optimize the lists of ports, addresses and server names */
 
     /* AF_INET only */
 
     in_port = in_ports.elts;
     for (p = 0; p < in_ports.nelts; p++) {
 
-        /* check whether the all server names point to the same server */
+        /*
+         * check whether all name-based servers have the same configuraiton
+         * as the default server, or some servers restrict the host names
+         */
 
         in_addr = in_port[p].addrs.elts;
         for (a = 0; a < in_port[p].addrs.nelts; a++) {
@@ -473,15 +456,19 @@
 
             name = in_addr[a].names.elts;
             for (n = 0; n < in_addr[a].names.nelts; n++) {
-                if (in_addr[a].core_srv_conf != name[n].core_srv_conf) {
+                if (in_addr[a].core_srv_conf != name[n].core_srv_conf
+                    || name[n].core_srv_conf->restrict_host_names
+                                                 != NGX_HTTP_RESTRICT_HOST_OFF)
+                {
                     virtual_names = 1;
                     break;
                 }
             }
 
             /*
-             * if the all server names point to the same server
-             * then we do not need to check them at run-time
+             * if all name-based servers have the same configuration
+             * as the default server, and no servers restrict the host names
+             * then we do not need to check them at run-time at all
              */
 
             if (!virtual_names) {
@@ -588,30 +575,117 @@
     }
 
 #if (NGX_DEBUG)
+    {
+    u_char      address[20];
+    ngx_uint_t  p, a, n;
+
     in_port = in_ports.elts;
     for (p = 0; p < in_ports.nelts; p++) {
         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
                       "port: %d %08x", in_port[p].port, &in_port[p]);
         in_addr = in_port[p].addrs.elts;
         for (a = 0; a < in_port[p].addrs.nelts; a++) {
-            u_char ip[20];
-            ngx_inet_ntop(AF_INET, &in_addr[a].addr, ip, 20);
-            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
-                           "%s %08x", ip, in_addr[a].core_srv_conf);
+            ngx_inet_ntop(AF_INET, &in_addr[a].addr, address, 20);
+            ngx_log_debug3(NGX_LOG_DEBUG_HTTP, cf->log, 0,
+                           "%s:%d %08x",
+                           address, in_port[p].port, in_addr[a].core_srv_conf);
             s_name = in_addr[a].names.elts;
             for (n = 0; n < in_addr[a].names.nelts; n++) {
-                 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
-                                "%s %08x", s_name[n].name.data,
+                 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, cf->log, 0,
+                                "%s:%d %s %08x",
+                                address, in_port[p].port, s_name[n].name.data,
                                 s_name[n].core_srv_conf);
             }
         }
     }
+    }
 #endif
 
     return NGX_CONF_OK;
 }
 
 
+/*
+ * add the server address, the server names and the server core module
+ * configurations to the port (in_port)
+ */
+
+static ngx_int_t ngx_http_add_address(ngx_conf_t *cf,
+                                      ngx_http_in_port_t *in_port,
+                                      ngx_http_listen_t *lscf,
+                                      ngx_http_core_srv_conf_t *cscf)
+{
+    ngx_http_in_addr_t  *in_addr;
+
+    if (in_port->addrs.elts == NULL) {
+        if (ngx_array_init(&in_port->addrs, cf->pool, 10,
+                                      sizeof(ngx_http_in_addr_t)) == NGX_ERROR)
+        {
+            return NGX_ERROR;
+        }
+    }
+
+    if (!(in_addr = ngx_array_push(&in_port->addrs))) {
+        return NGX_ERROR;
+    }
+
+    in_addr->addr = lscf->addr;
+    in_addr->names.elts = NULL;
+    in_addr->default_server = lscf->default_server;
+    in_addr->core_srv_conf = cscf;
+
+#if (NGX_DEBUG)
+    {
+    u_char text[20];
+    ngx_inet_ntop(AF_INET, &in_addr->addr, text, 20);
+    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0, "address: %s:%d",
+                   text, in_port->port);
+    }
+#endif
+
+    return ngx_http_add_names(cf, in_addr, cscf);
+}
+
+
+/*
+ * add the server names and the server core module
+ * configurations to the address:port (in_addr)
+ */
+
+static ngx_int_t ngx_http_add_names(ngx_conf_t *cf,
+                                    ngx_http_in_addr_t *in_addr,
+                                    ngx_http_core_srv_conf_t *cscf)
+{
+    ngx_uint_t               i;
+    ngx_http_server_name_t  *server_names, *name;
+
+    if (in_addr->names.elts == NULL) {
+        if (ngx_array_init(&in_addr->names, cf->pool, 10,
+                                  sizeof(ngx_http_server_name_t)) == NGX_ERROR)
+        {
+            return NGX_ERROR;
+        }
+    }
+
+    server_names = cscf->server_names.elts;
+    for (i = 0; i < cscf->server_names.nelts; i++) {
+
+        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
+                       "name: %s", server_names[i].name.data);
+
+        /* TODO: duplicate names can be checked here */
+
+        if (!(name = ngx_array_push(&in_addr->names))) {
+            return NGX_ERROR;
+        }
+
+        *name = server_names[i];
+    }
+
+    return NGX_OK;
+}
+
+
 static char *ngx_http_merge_locations(ngx_conf_t *cf,
                                       ngx_array_t *locations,
                                       void **loc_conf,
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 58021e8..8544e25 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -36,7 +36,7 @@
                                 void *dummy);
 static char *ngx_types_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 static char *ngx_set_type(ngx_conf_t *cf, ngx_command_t *dummy, void *conf);
-static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
+static char *ngx_http_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 static char *ngx_set_server_name(ngx_conf_t *cf, ngx_command_t *cmd,
                                  void *conf);
 static char *ngx_set_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
@@ -126,9 +126,9 @@
 #if 0
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
 #else
-      NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
+      NGX_HTTP_SRV_CONF|NGX_CONF_TAKE12,
 #endif
-      ngx_set_listen,
+      ngx_http_listen,
       NGX_HTTP_SRV_CONF_OFFSET,
       0,
       NULL },
@@ -576,7 +576,7 @@
     clcfp = locations->elts;
     for (i = 0; i < locations->nelts; i++) {
 
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
         if (clcfp[i]->regex) {
             break;
         }
@@ -638,7 +638,7 @@
         }
     }
 
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
 
     /* regex matches */
 
@@ -673,7 +673,7 @@
         return NGX_HTTP_LOCATION_REGEX;
     }
 
-#endif /* HAVE_PCRE */
+#endif /* NGX_PCRE */
 
     return NGX_OK;
 }
@@ -991,7 +991,7 @@
     first = *(ngx_http_core_loc_conf_t **) one;
     second = *(ngx_http_core_loc_conf_t **) two;
 
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
 
     if (first->regex && !second->regex) {
         /* shift the regex matches to the end */
@@ -1026,7 +1026,7 @@
     ngx_http_conf_ctx_t       *ctx, *pctx;
     ngx_http_core_srv_conf_t  *cscf;
     ngx_http_core_loc_conf_t  *clcf, *pclcf, **clcfp;
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
     ngx_str_t                  err;
     u_char                     errstr[NGX_MAX_CONF_ERRSTR];
 #endif
@@ -1076,7 +1076,7 @@
                        && value[1].data[0] == '~'
                        && value[1].data[1] == '*'))
         {
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
             err.len = NGX_MAX_CONF_ERRSTR;
             err.data = errstr;
 
@@ -1129,7 +1129,7 @@
             return NGX_CONF_ERROR;
         }
 
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
         if (clcf->regex == NULL
             && ngx_strncmp(clcf->name.data, pclcf->name.data, pclcf->name.len)
                                                                          != 0)
@@ -1323,12 +1323,8 @@
 
         n->name.len = ngx_strlen(n->name.data);
         n->core_srv_conf = conf;
+        n->wildcard = 0;
 
-#if 0
-        ctx = (ngx_http_conf_ctx_t *)
-                                    cf->cycle->conf_ctx[ngx_http_module.index];
-        cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
-#endif
         cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
 
         if (cmcf->max_server_name_len < n->name.len) {
@@ -1512,7 +1508,7 @@
 }
 
 
-static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+static char *ngx_http_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
     ngx_http_core_srv_conf_t *scf = conf;
 
@@ -1607,13 +1603,8 @@
     ngx_http_server_name_t     *sn;
     ngx_http_core_main_conf_t  *cmcf;
 
-    /* TODO: several names */
     /* TODO: warn about duplicate 'server_name' directives */
 
-#if 0
-    ctx = (ngx_http_conf_ctx_t *) cf->cycle->conf_ctx[ngx_http_module.index];
-    cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
-#endif
     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
 
     value = cf->args->elts;
@@ -1627,12 +1618,23 @@
             return NGX_CONF_ERROR;
         }
 
-        ngx_test_null(sn, ngx_push_array(&scf->server_names), NGX_CONF_ERROR);
+        if (!(sn = ngx_array_push(&scf->server_names))) {
+            return NGX_CONF_ERROR;
+        }
 
         sn->name.len = value[i].len;
         sn->name.data = value[i].data;
         sn->core_srv_conf = scf;
 
+        if (sn->name.data[0] == '*') {
+            sn->name.len--;
+            sn->name.data++;
+            sn->wildcard = 1;
+
+        } else {
+            sn->wildcard = 0;
+        }
+
         if (cmcf->max_server_name_len < sn->name.len) {
             cmcf->max_server_name_len = sn->name.len;
         }
@@ -1806,7 +1808,7 @@
 {
     ssize_t *np = data;
 
-#if __FreeBSD__
+#if (NGX_FREEBSD)
 
     if (*np >= ngx_freebsd_net_inet_tcp_sendspace) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
diff --git a/src/http/ngx_http_core_module.h b/src/http/ngx_http_core_module.h
index 787f8b8..9a17130 100644
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -96,13 +96,15 @@
     ngx_http_core_srv_conf_t  *core_srv_conf;  /* default server conf
                                                   for this address:port */
 
-    unsigned                   default_server:1;
+    ngx_uint_t                 default_server; /* unsigned  default_server:1; */
 } ngx_http_in_addr_t;
 
 
 typedef struct {
     ngx_str_t                  name;
     ngx_http_core_srv_conf_t  *core_srv_conf; /* virtual name server conf */
+
+    ngx_uint_t                 wildcard;  /*unsigned  wildcard:1; */
 } ngx_http_server_name_t;
 
 
@@ -135,7 +137,7 @@
 struct ngx_http_core_loc_conf_s {
     ngx_str_t     name;          /* location name */
 
-#if (HAVE_PCRE)
+#if (NGX_PCRE)
     ngx_regex_t  *regex;
 #endif
 
diff --git a/src/http/ngx_http_header_filter.c b/src/http/ngx_http_header_filter.c
index 5b7f3eb..a083b45 100644
--- a/src/http/ngx_http_header_filter.c
+++ b/src/http/ngx_http_header_filter.c
@@ -317,10 +317,15 @@
 
     if (r->headers_out.content_length == NULL) {
         if (r->headers_out.content_length_n >= 0) {
+            b->last = ngx_sprintf(b->last, "Content-Length: %O" CRLF,
+                                  r->headers_out.content_length_n);
+
+#if 0
             b->last += ngx_snprintf((char *) b->last,
                                 sizeof("Content-Length: ") + NGX_OFF_T_LEN + 2,
                                 "Content-Length: " OFF_T_FMT CRLF,
                                 r->headers_out.content_length_n);
+#endif
         }
     }
 
@@ -372,7 +377,7 @@
     {
         b->last = ngx_cpymem(b->last, "Last-Modified: ",
                              sizeof("Last-Modified: ") - 1);
-        b->last += ngx_http_time(b->last, r->headers_out.last_modified_time);
+        b->last = ngx_http_time(b->last, r->headers_out.last_modified_time);
 
         *(b->last++) = CR; *(b->last++) = LF;
     }
@@ -389,10 +394,14 @@
         if (clcf->keepalive_header
             && (r->headers_in.gecko || r->headers_in.konqueror))
         {
+            b->last = ngx_sprintf(b->last, "Keep-Alive: timeout=%T" CRLF,
+                                  clcf->keepalive_header);
+#if 0
             b->last += ngx_snprintf((char *) b->last,
                             sizeof("Keep-Alive: timeout=") + TIME_T_LEN + 2,
                             "Keep-Alive: timeout=" TIME_T_FMT CRLF,
                             clcf->keepalive_header);
+#endif
         }
 
     } else {
diff --git a/src/http/ngx_http_log_handler.c b/src/http/ngx_http_log_handler.c
index 51166cf..9a1389d 100644
--- a/src/http/ngx_http_log_handler.c
+++ b/src/http/ngx_http_log_handler.c
@@ -210,9 +210,13 @@
 static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
                                      uintptr_t data)
 {
+    return ngx_sprintf(buf, "%ui", r->connection->number);
+
+#if 0
     return buf + ngx_snprintf((char *) buf, NGX_INT_T_LEN + 1,
                               "%" NGX_UINT_T_FMT,
                               r->connection->number);
+#endif
 }
 
 
@@ -244,8 +248,12 @@
 
     ngx_gettimeofday(&tv);
 
+    return ngx_sprintf(buf, "%l.%03l", tv.tv_sec, tv.tv_usec / 1000);
+
+#if 0
     return buf + ngx_snprintf((char *) buf, TIME_T_LEN + 5, "%ld.%03ld",
                               tv.tv_sec, tv.tv_usec / 1000);
+#endif
 }
 
 
@@ -264,24 +272,36 @@
 static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
                                    uintptr_t data)
 {
+    return ngx_sprintf(buf, "%ui",
+                       r->err_status ? r->err_status : r->headers_out.status);
+
+#if 0
     return buf + ngx_snprintf((char *) buf, 4, "%" NGX_UINT_T_FMT,
                         r->err_status ? r->err_status : r->headers_out.status);
+#endif
 }
 
 
 static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
                                    uintptr_t data)
 {
+    return ngx_sprintf(buf, "%O", r->connection->sent);
+
+#if 0
     return buf + ngx_snprintf((char *) buf, NGX_OFF_T_LEN + 1, OFF_T_FMT,
                               r->connection->sent);
+#endif
 }
 
 
 static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
                                           uintptr_t data)
 {
+    return ngx_sprintf(buf, "%O", r->connection->sent - r->header_size);
+#if 0
     return buf + ngx_snprintf((char *) buf, NGX_OFF_T_LEN + 1, OFF_T_FMT,
                               r->connection->sent - r->header_size);
+#endif
 }
 
 
@@ -467,8 +487,7 @@
                     return (u_char *)
                                    sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
                 }
-                return buf + ngx_http_time(buf,
-                                           r->headers_out.last_modified_time);
+                return ngx_http_time(buf, r->headers_out.last_modified_time);
             }
 
             if (buf) {
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index 6069847..74173d8 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -1091,27 +1091,47 @@
 
         name = r->virtual_names->elts;
         for (i = 0; i < r->virtual_names->nelts; i++) {
-            if (r->headers_in.host_name_len != name[i].name.len) {
-                continue;
-            }
 
-            if (ngx_strncasecmp(r->headers_in.host->value.data,
-                                name[i].name.data,
-                                r->headers_in.host_name_len) == 0)
-            {
-                r->srv_conf = name[i].core_srv_conf->ctx->srv_conf;
-                r->loc_conf = name[i].core_srv_conf->ctx->loc_conf;
-                r->server_name = &name[i].name;
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                           "server name: %s", name[i].name.data);
 
-                clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
-                r->connection->log->file = clcf->err_log->file;
-                if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION))
-                {
-                    r->connection->log->log_level = clcf->err_log->log_level;
+            if (name[i].wildcard) {
+                if (r->headers_in.host_name_len <= name[i].name.len) {
+                    continue;
                 }
 
-                break;
+                if (ngx_rstrncasecmp(r->headers_in.host->value.data,
+                                     name[i].name.data,
+                                     name[i].name.len) == 0)
+                {
+                    continue;
+                }
+
+            } else {
+                if (r->headers_in.host_name_len != name[i].name.len) {
+                    continue;
+                }
+
+                if (ngx_strncasecmp(r->headers_in.host->value.data,
+                                    name[i].name.data,
+                                    name[i].name.len) != 0)
+                {
+                    continue;
+                }
             }
+
+            r->srv_conf = name[i].core_srv_conf->ctx->srv_conf;
+            r->loc_conf = name[i].core_srv_conf->ctx->loc_conf;
+            r->server_name = &name[i].name;
+
+            clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+            r->connection->log->file = clcf->err_log->file;
+
+            if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
+                r->connection->log->log_level = clcf->err_log->log_level;
+            }
+
+            break;
         }
 
         if (i == r->virtual_names->nelts) {
@@ -1562,7 +1582,13 @@
 
         if (b != c->buffer) {
 
-            /* move the large header buffers to the free list */
+            /*
+             * If the large header buffers were allocated while the previous
+             * request processing then we do not use c->buffer for
+             * the pipelined request (see ngx_http_init_request()).
+             * 
+             * Now we would move the large header buffers to the free list.
+             */
 
             cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
 
@@ -1614,6 +1640,14 @@
 
     hc->pipeline = 0;
 
+    /*
+     * To keep a memory footprint as small as possible for an idle
+     * keepalive connection we try to free the ngx_http_request_t and
+     * c->buffer's memory if they were allocated outside the c->pool.
+     * The large header buffers are always allocated outside the c->pool and
+     * are freed too.
+     */
+
     if (ngx_pfree(c->pool, r) == NGX_OK) {
         hc->request = NULL;
     }
@@ -1621,6 +1655,12 @@
     b = c->buffer;
 
     if (ngx_pfree(c->pool, b->start) == NGX_OK) {
+
+        /*
+         * the special note for ngx_http_keepalive_handler() that
+         * c->buffer's memory was freed
+         */
+
         b->pos = NULL;
 
     } else {
@@ -1655,7 +1695,7 @@
     rev->event_handler = ngx_http_keepalive_handler;
 
     if (wev->active) {
-        if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+        if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
             if (ngx_del_event(wev, NGX_WRITE_EVENT, NGX_DISABLE_EVENT)
                                                                   == NGX_ERROR)
             {
@@ -1702,7 +1742,7 @@
     }
 
 #if 0
-    /* if "keepalive_buffers off" then we need some other place */
+    /* if ngx_http_request_t was freed then we need some other place */
     r->http_state = NGX_HTTP_KEEPALIVE_STATE;
 #endif
 
@@ -1734,7 +1774,7 @@
 
 #if (HAVE_KQUEUE)
 
-    if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+    if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
         if (rev->pending_eof) {
             ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
                           "kevent() reported that client %s closed "
@@ -1751,6 +1791,13 @@
     size = b->end - b->start;
 
     if (b->pos == NULL) {
+
+        /*
+         * The c->buffer's memory was freed by ngx_http_set_keepalive().
+         * However, the c->buffer->start and c->buffer->end were not changed
+         * to keep the buffer size.
+         */
+
         if (!(b->pos = ngx_palloc(c->pool, size))) {
             ngx_http_close_connection(c);
             return;
@@ -1824,7 +1871,7 @@
     wev->event_handler = ngx_http_empty_handler;
 
     if (wev->active) {
-        if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+        if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
             if (ngx_del_event(wev, NGX_WRITE_EVENT, NGX_DISABLE_EVENT)
                                                                   == NGX_ERROR)
             {
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h
index 022b8a8..8a9c8f2 100644
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -38,11 +38,11 @@
 #define NGX_HTTP_PARSE_HEADER_ERROR        14
 #define NGX_HTTP_PARSE_INVALID_HEADER      14
 #define NGX_HTTP_PARSE_TOO_LONG_HEADER     15
-#define NGX_HTTP_PARSE_NO_HOST_HEADER      17
-#define NGX_HTTP_PARSE_INVALID_CL_HEADER   18
-#define NGX_HTTP_PARSE_POST_WO_CL_HEADER   19
-#define NGX_HTTP_PARSE_HTTP_TO_HTTPS       20
-#define NGX_HTTP_PARSE_INVALID_HOST        21
+#define NGX_HTTP_PARSE_NO_HOST_HEADER      16
+#define NGX_HTTP_PARSE_INVALID_CL_HEADER   17
+#define NGX_HTTP_PARSE_POST_WO_CL_HEADER   18
+#define NGX_HTTP_PARSE_HTTP_TO_HTTPS       19
+#define NGX_HTTP_PARSE_INVALID_HOST        20
 
 
 #define NGX_HTTP_OK                        200
diff --git a/src/http/ngx_http_write_filter.c b/src/http/ngx_http_write_filter.c
index a8c6894..37e0f3f 100644
--- a/src/http/ngx_http_write_filter.c
+++ b/src/http/ngx_http_write_filter.c
@@ -125,6 +125,7 @@
         if (!last) {
             ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
                           "the http output chain is empty");
+            return NGX_ERROR;
         }
         return NGX_OK;
     }
diff --git a/src/os/unix/ngx_files.c b/src/os/unix/ngx_files.c
index 8603ab2..f2ba3a4 100644
--- a/src/os/unix/ngx_files.c
+++ b/src/os/unix/ngx_files.c
@@ -210,59 +210,3 @@
 
     return NGX_OK;
 }
-
-
-#if 0
-
-ssize_t ngx_read_file(ngx_file_t *file, char *buf, size_t size, off_t offset)
-{
-    if (!file->read->ready) {
-
-        ngx_memzero(&file->iocb, sizeof(iocb));
-        file->iocb.aio_fildes = file->fd;
-        file->iocb.aio_buf = buf;
-        file->iocb.aio_nbytes = size;
-        file->iocb.aio_offset = offset;
-#if (USE_AIO_KQUEUE)
-        file->iocb.aio_sigevent.sigev_notify = SIGEV_KEVENT;
-        file->iocb.aio_sigevent.sigev_notify_kqueue = tid->kq;
-        file->iocb.aio_sigevent.sigev_value = (union sigval) file;
-#endif
-#if (USE_AIO_SIGNAL)
-        file->iocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
-        file->iocb.aio_sigevent.sigev_signo = NGX_SIGAIO;
-#ifndef __FreeBSD__
-        file->iocb.aio_sigevent.sigev_value.sival_ptr = file;
-#endif
-#endif
-
-        if (aio_read(&file->iocb) == -1) {
-            ngx_log_error(NGX_LOG_ERR, file->log, ngx_errno,
-                          "aio_read() failed");
-            return NGX_ERROR;
-
-        n = aio_error(&file->iocb);
-        if (n == EINPROGRESS)
-            return NGX_AGAIN;
-
-        if (n == -1) {
-            ngx_log_error(NGX_LOG_ERR, file->log, ngx_errno,
-                          "aio_read() failed");
-            return NGX_ERROR;
-        }
-    }
-
-    ngx_assert(file->iocb.aio_buf == buf), return NGX_ERROR,
-               "ngx_aio_read_file: another buffer is passed");
-
-    n = aio_return(&file->iocb);
-    if (n == -1) {
-        ngx_log_error(NGX_LOG_ERR, file->log, ngx_errno,
-                      "aio_read() failed");
-        return NGX_ERROR;
-    }
-
-    return n;
-}
-
-#endif
diff --git a/src/os/unix/ngx_files.h b/src/os/unix/ngx_files.h
index 3ba1670..06c22f5 100644
--- a/src/os/unix/ngx_files.h
+++ b/src/os/unix/ngx_files.h
@@ -99,7 +99,7 @@
 
 
 #define ngx_de_name(dir)         (dir)->de->d_name
-#ifdef __FreeBSD__
+#if (NGX_FREEBSD)
 #define ngx_de_namelen(dir)      (dir)->de->d_namlen
 #else
 #define ngx_de_namelen(dir)      ngx_strlen((dir)->de->d_name)
diff --git a/src/os/unix/ngx_freebsd_config.h b/src/os/unix/ngx_freebsd_config.h
index 7961a3b..4840a99 100644
--- a/src/os/unix/ngx_freebsd_config.h
+++ b/src/os/unix/ngx_freebsd_config.h
@@ -22,8 +22,8 @@
 #include <grp.h>
 #include <dirent.h>
 
-#include <sys/uio.h>
 #include <sys/filio.h>          /* FIONBIO */
+#include <sys/uio.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 
@@ -41,12 +41,11 @@
 #include <libutil.h>            /* setproctitle() before 4.1 */
 #include <osreldate.h>
 #include <sys/sysctl.h>
+#include <sys/param.h>          /* ALIGN() */
 
 
 #if __FreeBSD_version < 400017
 
-#include <sys/param.h>          /* ALIGN() */
-
 /* FreeBSD 3.x has no CMSG_SPACE() at all and has the broken CMSG_DATA() */
 
 #undef  CMSG_SPACE
diff --git a/src/os/unix/ngx_freebsd_sendfile_chain.c b/src/os/unix/ngx_freebsd_sendfile_chain.c
index d29debf..8303c06 100644
--- a/src/os/unix/ngx_freebsd_sendfile_chain.c
+++ b/src/os/unix/ngx_freebsd_sendfile_chain.c
@@ -57,7 +57,7 @@
 
 #if (HAVE_KQUEUE)
 
-    if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) && wev->pending_eof) {
+    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
         ngx_log_error(NGX_LOG_INFO, c->log, wev->kq_errno,
                       "kevent() reported about an closed connection");
 
@@ -131,7 +131,6 @@
             send += size;
         }
 
-        /* get the file buf */
 
         if (cl && cl->buf->in_file && send < limit) {
             file = cl->buf;
@@ -164,17 +163,18 @@
                      && fprev == cl->buf->file_pos);
         }
 
+
         if (file) {
+
             /* create the tailer iovec and coalesce the neighbouring bufs */
 
             prev = NULL;
             iov = NULL;
 
-            for (/* void */;
-                 cl && header.nelts < IOV_MAX && send < limit;
-                 cl = cl->next)
-            {
+            while (cl && header.nelts < IOV_MAX && send < limit) {
+
                 if (ngx_buf_special(cl->buf)) {
+                    cl = cl->next;
                     continue;
                 }
 
@@ -202,6 +202,7 @@
 
                 prev = cl->buf->pos + size;
                 send += size;
+                cl = cl->next;
             }
         }
 
@@ -210,7 +211,6 @@
             if (ngx_freebsd_use_tcp_nopush
                 && c->tcp_nopush == NGX_TCP_NOPUSH_UNSET)
             {
-
                 if (ngx_tcp_nopush(c->fd) == NGX_ERROR) {
                     err = ngx_errno;
 
@@ -275,6 +275,20 @@
                 }
             }
 
+            if (rc == 0 && sent == 0) {
+
+                /*
+                 * rc and sent are equals to zero when someone has truncated
+                 * the file, so the offset became beyond the end of the file
+                 */
+
+                ngx_log_error(NGX_LOG_ALERT, c->log, 0,
+                              "sendfile() reported that \"%s\" was truncated",
+                              file->file->name.data);
+
+                return NGX_CHAIN_ERROR;
+            }
+
             ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0,
                            "sendfile: %d, @" OFF_T_FMT " " OFF_T_FMT ":%d",
                            rc, file->file_pos, sent, fsize + hsize);
diff --git a/src/os/unix/ngx_linux_config.h b/src/os/unix/ngx_linux_config.h
index 11427d3..9a0fee9 100644
--- a/src/os/unix/ngx_linux_config.h
+++ b/src/os/unix/ngx_linux_config.h
@@ -8,11 +8,11 @@
 #define _NGX_LINUX_CONFIG_H_INCLUDED_
 
 
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE             /* pread(), pwrite(), gethostname() */
+#endif
 
 #define _FILE_OFFSET_BITS  64
-#define _LARGEFILE_SOURCE
-
 
 #include <sys/types.h>
 #include <sys/time.h>
@@ -51,11 +51,11 @@
 #include <ngx_auto_config.h>
 
 
-#if (HAVE_PRCTL)
+#if (NGX_HAVE_SYS_PRCTL_H)
 #include <sys/prctl.h>
 #endif
 
-#if (HAVE_SENDFILE64)
+#if (NGX_HAVE_SENDFILE64)
 #include <sys/sendfile.h>
 #else
 extern ssize_t sendfile(int s, int fd, int32_t *offset, size_t size);
diff --git a/src/os/unix/ngx_linux_sendfile_chain.c b/src/os/unix/ngx_linux_sendfile_chain.c
index 38a2a96..4de7ac2 100644
--- a/src/os/unix/ngx_linux_sendfile_chain.c
+++ b/src/os/unix/ngx_linux_sendfile_chain.c
@@ -37,7 +37,7 @@
     ngx_event_t     *wev;
     ngx_chain_t     *cl;
     struct iovec    *iov, headers[NGX_HEADERS];
-#if (HAVE_SENDFILE64)
+#if (NGX_HAVE_SENDFILE64)
     off_t            offset;
 #else
     int32_t          offset;
@@ -167,7 +167,7 @@
         }
 
         if (file) {
-#if (HAVE_SENDFILE64)
+#if (NGX_HAVE_SENDFILE64)
             offset = file->file_pos;
 #else
             offset = (int32_t) file->file_pos;
diff --git a/src/os/unix/ngx_os.h b/src/os/unix/ngx_os.h
index b28a73f..dc9f7a8 100644
--- a/src/os/unix/ngx_os.h
+++ b/src/os/unix/ngx_os.h
@@ -66,17 +66,15 @@
 
 #define ngx_stderr_fileno  STDERR_FILENO
 
-#ifdef __FreeBSD__
+#if (NGX_FREEBSD)
 #include <ngx_freebsd.h>
-#endif
 
 
-#ifdef __linux__
+#elif (NGX_LINUX)
 #include <ngx_linux.h>
-#endif
 
 
-#ifdef SOLARIS
+#elif (NGX_SOLARIS)
 #include <ngx_solaris.h>
 #endif
 
diff --git a/src/os/unix/ngx_posix_config.h b/src/os/unix/ngx_posix_config.h
index bf0f5d8..bac02f7 100644
--- a/src/os/unix/ngx_posix_config.h
+++ b/src/os/unix/ngx_posix_config.h
@@ -8,9 +8,20 @@
 #define _NGX_POSIX_CONFIG_H_INCLUDED_
 
 
+#if 0
+#define _XOPEN_SOURCE
+#define _XOPEN_SOURCE_EXTENDED  1
+#endif
+
+
 #include <sys/types.h>
 #include <sys/time.h>
+#if (NGX_HAVE_UNISTD_H)
 #include <unistd.h>
+#endif
+#if (NGX_HAVE_INTTYPES_H)
+#include <inttypes.h>
+#endif
 #include <stdarg.h>
 #include <stddef.h>             /* offsetof() */
 #include <stdio.h>
@@ -22,8 +33,12 @@
 #include <grp.h>
 #include <dirent.h>
 
-#include <sys/uio.h>
+#if (NGX_HAVE_SYS_FILIO_H)
 #include <sys/filio.h>          /* FIONBIO */
+#endif
+#include <sys/ioctl.h>          /* FIONBIO */
+
+#include <sys/uio.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 
@@ -34,9 +49,19 @@
 
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <netinet/tcp.h>        /* TCP_NODELAY */
 #include <arpa/inet.h>
 #include <netdb.h>
 
+#if (NGX_HAVE_LIMITS_H)
+#include <limits.h>             /* IOV_MAX */
+#endif
+
+#ifndef IOV_MAX
+#define IOV_MAX   16
+#endif
+
+
 #include <ngx_auto_config.h>
 
 
@@ -53,6 +78,32 @@
 #endif
 
 
+#if (HAVE_KQUEUE)
+#include <sys/event.h>
+#endif
+
+
+#if (HAVE_DEVPOLL)
+#include <sys/ioctl.h>
+#include <sys/devpoll.h>
+#endif
+
+
+#if (__FreeBSD__) && (__FreeBSD_version < 400017)
+
+#include <sys/param.h>          /* ALIGN() */
+
+/* FreeBSD 3.x has no CMSG_SPACE() at all and has the broken CMSG_DATA() */
+
+#undef  CMSG_SPACE
+#define CMSG_SPACE(l)       (ALIGN(sizeof(struct cmsghdr)) + ALIGN(l))
+
+#undef  CMSG_DATA
+#define CMSG_DATA(cmsg)     ((u_char *)(cmsg) + ALIGN(sizeof(struct cmsghdr)))
+
+#endif
+
+
 #define ngx_setproctitle(title)
 
 
diff --git a/src/os/unix/ngx_posix_init.c b/src/os/unix/ngx_posix_init.c
index 134c276..1c7b1b6 100644
--- a/src/os/unix/ngx_posix_init.c
+++ b/src/os/unix/ngx_posix_init.c
@@ -33,6 +33,12 @@
 }
 
 
+void ngx_os_status(ngx_log_t *log)
+{
+    ngx_posix_status(log);
+}
+
+
 #endif
 
 
diff --git a/src/os/unix/ngx_process.c b/src/os/unix/ngx_process.c
index aff242c..4f7ed64 100644
--- a/src/os/unix/ngx_process.c
+++ b/src/os/unix/ngx_process.c
@@ -236,7 +236,7 @@
                 return;
             }
 
-#if (SOLARIS)
+#if (NGX_SOLARIS)
 
             /*
              * Solaris always calls the signal handler for each exited process
diff --git a/src/os/unix/ngx_process.h b/src/os/unix/ngx_process.h
index 0cea302..9cb0700 100644
--- a/src/os/unix/ngx_process.h
+++ b/src/os/unix/ngx_process.h
@@ -54,7 +54,13 @@
 ngx_pid_t ngx_execute(ngx_cycle_t *cycle, ngx_exec_ctx_t *ctx);
 void ngx_process_get_status(void);
 
+
+#if (NGX_HAVE_SCHED_YIELD)
 #define ngx_sched_yield()  sched_yield()
+#else
+#define ngx_sched_yield()  usleep(1)
+#endif
+
 
 extern int            ngx_argc;
 extern char         **ngx_argv;
diff --git a/src/os/unix/ngx_process_cycle.c b/src/os/unix/ngx_process_cycle.c
index c43a7eb..1da4915 100644
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -175,11 +175,14 @@
 
         if (ngx_timer) {
             ngx_timer = 0;
-            ngx_start_worker_processes(cycle, ccf->worker_processes,
-                                       NGX_PROCESS_JUST_RESPAWN);
-            live = 1;
-            ngx_signal_worker_processes(cycle,
+
+            if (!ngx_noaccepting) {
+                ngx_start_worker_processes(cycle, ccf->worker_processes,
+                                           NGX_PROCESS_JUST_RESPAWN);
+                live = 1;
+                ngx_signal_worker_processes(cycle,
                                         ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
+            }
         }
 
         if (ngx_reconfigure) {
@@ -578,16 +581,14 @@
 
     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 
-    if (ccf->group != (gid_t) NGX_CONF_UNSET) {
+    if (geteuid() == 0) {
         if (setgid(ccf->group) == -1) {
             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
                           "setgid(%d) failed", ccf->group);
             /* fatal */
             exit(2);
         }
-    }
 
-    if (ccf->user != (uid_t) NGX_CONF_UNSET) {
         if (setuid(ccf->user) == -1) {
             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
                           "setuid(%d) failed", ccf->user);
@@ -596,7 +597,7 @@
         }
     }
 
-#if (HAVE_PR_SET_DUMPABLE)
+#if (NGX_HAVE_PR_SET_DUMPABLE)
 
     /* allow coredump after setuid() in Linux 2.4.x */
 
diff --git a/src/os/unix/ngx_readv_chain.c b/src/os/unix/ngx_readv_chain.c
index a529b3d..2db998a 100644
--- a/src/os/unix/ngx_readv_chain.c
+++ b/src/os/unix/ngx_readv_chain.c
@@ -22,7 +22,7 @@
 
     rev = c->read; 
 
-    if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+    if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
         ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
                        "readv: eof:%d, avail:%d, err:%d",
                        rev->pending_eof, rev->available, rev->kq_errno);
@@ -81,7 +81,7 @@
         n = readv(c->fd, (struct iovec *) io.elts, io.nelts);
 
         if (n >= 0) {
-            if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+            if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
                 rev->available -= n;
 
                 /*
@@ -186,7 +186,7 @@
 
         } else if (n > 0) {
 
-            if (n < size && !(ngx_event_flags & NGX_HAVE_GREEDY_EVENT)) {
+            if (n < size && !(ngx_event_flags & NGX_USE_GREEDY_EVENT)) {
                 rev->ready = 0;
             }
 
diff --git a/src/os/unix/ngx_recv.c b/src/os/unix/ngx_recv.c
index 6e5b8c0..d58ca9e 100644
--- a/src/os/unix/ngx_recv.c
+++ b/src/os/unix/ngx_recv.c
@@ -19,7 +19,7 @@
 
     rev = c->read;
 
-    if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+    if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
         ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
                        "recv: eof:%d, avail:%d, err:%d",
                        rev->pending_eof, rev->available, rev->kq_errno);
@@ -60,7 +60,7 @@
                        "recv: fd:%d %d of %d", c->fd, n, size);
 
         if (n >= 0) {
-            if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
+            if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
                 rev->available -= n;
 
                 /*
@@ -139,7 +139,7 @@
         } else if (n > 0) {
 
             if ((size_t) n < size
-                && !(ngx_event_flags & NGX_HAVE_GREEDY_EVENT))
+                && !(ngx_event_flags & NGX_USE_GREEDY_EVENT))
             {
                 rev->ready = 0;
             }
diff --git a/src/os/unix/ngx_send.c b/src/os/unix/ngx_send.c
index f09dadb..ff501c3 100644
--- a/src/os/unix/ngx_send.c
+++ b/src/os/unix/ngx_send.c
@@ -19,7 +19,7 @@
 
 #if (HAVE_KQUEUE)
 
-    if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) && wev->pending_eof) {
+    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
         ngx_log_error(NGX_LOG_INFO, c->log, wev->kq_errno,
                       "kevent() reported about an closed connection");
 
diff --git a/src/os/unix/ngx_setproctitle.c b/src/os/unix/ngx_setproctitle.c
index 6ac8707..2e41108 100644
--- a/src/os/unix/ngx_setproctitle.c
+++ b/src/os/unix/ngx_setproctitle.c
@@ -76,7 +76,7 @@
 {
     u_char     *p;
 
-#if (SOLARIS)
+#if (NGX_SOLARIS)
 
     ngx_int_t   i;
     size_t      size;
@@ -90,7 +90,7 @@
 
     p = ngx_cpystrn(p, (u_char *) title, ngx_os_argv_last - (char *) p);
 
-#if (SOLARIS)
+#if (NGX_SOLARIS)
 
     size = 0;
 
diff --git a/src/os/unix/ngx_socket.c b/src/os/unix/ngx_socket.c
index 3e188bd..da936b2 100644
--- a/src/os/unix/ngx_socket.c
+++ b/src/os/unix/ngx_socket.c
@@ -44,7 +44,7 @@
 #endif
 
 
-#ifdef __FreeBSD__
+#if (NGX_FREEBSD)
 
 int ngx_tcp_nopush(ngx_socket_t s)
 {
@@ -67,7 +67,7 @@
                       (const void *) &tcp_nopush, sizeof(int));
 }
 
-#elif __linux__
+#elif (NGX_LINUX)
 
 int ngx_tcp_nopush(ngx_socket_t s)
 {
diff --git a/src/os/unix/ngx_socket.h b/src/os/unix/ngx_socket.h
index 67d0d41..34449d0 100644
--- a/src/os/unix/ngx_socket.h
+++ b/src/os/unix/ngx_socket.h
@@ -37,7 +37,7 @@
 int ngx_tcp_nopush(ngx_socket_t s);
 int ngx_tcp_push(ngx_socket_t s);
 
-#ifdef __linux__
+#if (NGX_LINUX)
 
 #define ngx_tcp_nopush_n   "setsockopt(TCP_CORK)"
 #define ngx_tcp_push_n     "setsockopt(!TCP_CORK)"
diff --git a/src/os/unix/ngx_solaris_config.h b/src/os/unix/ngx_solaris_config.h
index 96c119c..5d94cdc 100644
--- a/src/os/unix/ngx_solaris_config.h
+++ b/src/os/unix/ngx_solaris_config.h
@@ -8,8 +8,6 @@
 #define _NGX_SOLARIS_CONFIG_H_INCLUDED_
 
 
-#define SOLARIS  1
-
 #define _REENTRANT
 
 #define _FILE_OFFSET_BITS  64   /* must be before <sys/types.h> */
@@ -28,8 +26,8 @@
 #include <grp.h>
 #include <dirent.h>
 
-#include <sys/uio.h>
 #include <sys/filio.h>          /* FIONBIO */
+#include <sys/uio.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 
diff --git a/src/os/unix/ngx_thread.h b/src/os/unix/ngx_thread.h
index 7b5dc23..16e216e 100644
--- a/src/os/unix/ngx_thread.h
+++ b/src/os/unix/ngx_thread.h
@@ -28,7 +28,7 @@
 #define ngx_thread_self()            pthread_self()
 #define ngx_log_tid                  (int) ngx_thread_self()
 
-#if defined(__FreeBSD__) && !defined(NGX_LINUXTHREADS)
+#if (NGX_FREEBSD) && !(NGX_LINUXTHREADS)
 #define TID_T_FMT                    PTR_FMT
 #else
 #define TID_T_FMT                    "%d"
diff --git a/src/os/unix/ngx_time.h b/src/os/unix/ngx_time.h
index 793e32e..0ead0d8 100644
--- a/src/os/unix/ngx_time.h
+++ b/src/os/unix/ngx_time.h
@@ -42,8 +42,14 @@
 #endif
 
 
-#if (SOLARIS)
+#if (NGX_SOLARIS)
+
 #define ngx_timezone(isdst) (- (isdst ? altzone : timezone) / 60)
+
+#else
+
+#define ngx_timezone(isdst) (- (isdst ? timezone + 3600 : timezone) / 60)
+
 #endif
 
 
diff --git a/src/os/unix/ngx_writev_chain.c b/src/os/unix/ngx_writev_chain.c
index a3595ff..e57b593 100644
--- a/src/os/unix/ngx_writev_chain.c
+++ b/src/os/unix/ngx_writev_chain.c
@@ -32,7 +32,7 @@
 
 #if (HAVE_KQUEUE)
 
-    if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) && wev->pending_eof) {
+    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
         ngx_log_error(NGX_LOG_INFO, c->log, wev->kq_errno,
                       "kevent() reported about an closed connection");
 
diff --git a/src/os/win32/ngx_win32_config.h b/src/os/win32/ngx_win32_config.h
index baba3da..a6c336d 100644
--- a/src/os/win32/ngx_win32_config.h
+++ b/src/os/win32/ngx_win32_config.h
@@ -30,7 +30,12 @@
 #include <stdlib.h>
 #include <stdarg.h>
 
+
 #ifdef _MSC_VER
+
+/* the end of the precompiled headers */
+#pragma hdrstop
+
 #pragma warning(default:4201)