nginx-0.1.9-RELEASE import

    *) Bugfix: the proxied request was sent without arguments if the
       request contains "//", "/./", "/../" or "%XX".

    *) Bugfix: the large compressed responses may be transferred not
       completely.

    *) Bugfix: the files bigger than 2G was not transferred on Linux that
       does not support sendfile64().

    *) Bugfix: while the build configuration on Linux the
       --with-poll_module parameter was required; the bug had appeared in
       0.1.8.
diff --git a/src/core/nginx.c b/src/core/nginx.c
index 53ac666..887fc79 100644
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -112,7 +112,7 @@
 
 
 
-int main(int argc, char *const *argv, char *const *envp)
+int main(int argc, char *const *argv)
 {
     ngx_int_t         i;
     ngx_log_t        *log;
diff --git a/src/core/nginx.h b/src/core/nginx.h
index 5f2339b..d737d6b 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.8"
+#define NGINX_VER          "nginx/0.1.9"
 
 #define NGINX_VAR          "NGINX"
 #define NGX_NEWPID_EXT     ".newbin"
diff --git a/src/core/ngx_buf.c b/src/core/ngx_buf.c
index 7d37989..dedf444 100644
--- a/src/core/ngx_buf.c
+++ b/src/core/ngx_buf.c
@@ -140,7 +140,7 @@
             break;
         }
 
-#if (HAVE_WRITE_ZEROCOPY)
+#if (NGX_HAVE_WRITE_ZEROCOPY)
         if ((*busy)->buf->zerocopy_busy) {
             break;
         }
diff --git a/src/core/ngx_config.h b/src/core/ngx_config.h
index 43fd985..916b1b5 100644
--- a/src/core/ngx_config.h
+++ b/src/core/ngx_config.h
@@ -11,6 +11,13 @@
 #include <ngx_auto_headers.h>
 
 
+#if ((__GNU__ == 2) && (__GNUC_MINOR__ < 8))
+#define NGX_MAX_UINT32_VALUE  0xffffffffLL
+#else
+#define NGX_MAX_UINT32_VALUE  0xffffffff
+#endif
+
+
 #if defined __DragonFly__ && !defined __FreeBSD__
 #define __FreeBSD__        4
 #define __FreeBSD_version  480101
@@ -127,12 +134,5 @@
 #define NGX_MAXHOSTNAMELEN MAXHOSTNAMELEN
 */
 
-#if ((__GNU__ == 2) && (__GNUC_MINOR__ < 8))
-#define NGX_MAX_UINT32_VALUE  0xffffffffLL
-#else
-#define NGX_MAX_UINT32_VALUE  0xffffffff
-#endif
-
-
 
 #endif /* _NGX_CONFIG_H_INCLUDED_ */
diff --git a/src/core/ngx_connection.h b/src/core/ngx_connection.h
index e2257ac..40f19a8 100644
--- a/src/core/ngx_connection.h
+++ b/src/core/ngx_connection.h
@@ -48,7 +48,7 @@
     unsigned          nonblocking_accept:1;
     unsigned          nonblocking:1;
     unsigned          shared:1;    /* shared between threads or processes */
-#if (HAVE_DEFERRED_ACCEPT)
+#if (NGX_HAVE_DEFERRED_ACCEPT)
     unsigned          deferred_accept:1;
 #endif
 
@@ -101,7 +101,7 @@
     ngx_ssl_t          *ssl;
 #endif
 
-#if (HAVE_IOCP)
+#if (NGX_HAVE_IOCP)
     struct sockaddr    *local_sockaddr;
     socklen_t           local_socklen;
 #endif
@@ -122,7 +122,7 @@
     unsigned            tcp_nodelay:1;
     signed              tcp_nopush:2;
 
-#if (HAVE_IOCP)
+#if (NGX_HAVE_IOCP)
     unsigned            accept_context_updated:1;
 #endif
 
diff --git a/src/core/ngx_output_chain.c b/src/core/ngx_output_chain.c
index d2fbf12..1c367cc 100644
--- a/src/core/ngx_output_chain.c
+++ b/src/core/ngx_output_chain.c
@@ -9,11 +9,18 @@
 #include <ngx_event.h>
 
 
-#define NGX_NONE      1
+#if 0
+#define NGX_SENDFILE_LIMIT  4096
+#endif
+
+
+#define NGX_NONE            1
 
 
 static ngx_inline ngx_int_t
     ngx_output_chain_need_to_copy(ngx_output_chain_ctx_t *ctx, ngx_buf_t *buf);
+static ngx_int_t ngx_output_chain_add_copy(ngx_pool_t *pool,
+                                          ngx_chain_t **chain, ngx_chain_t *in);
 static ngx_int_t ngx_output_chain_copy_buf(ngx_buf_t *dst, ngx_buf_t *src,
                                            ngx_uint_t sendfile);
 
@@ -26,17 +33,20 @@
 
     if (ctx->in == NULL && ctx->busy == 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
-        */
+        /*
+         * 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);
         }
 
         if (in->next == NULL
+#if (NGX_SENDFILE_LIMIT)
+            && !(in->buf->in_file && in->buf->file_last > NGX_SENDFILE_LIMIT)
+#endif
             && (!ngx_output_chain_need_to_copy(ctx, in->buf)))
         {
             return ctx->output_filter(ctx->filter_ctx, in);
@@ -46,7 +56,7 @@
     /* add the incoming buf to the chain ctx->in */
 
     if (in) {
-        if (ngx_chain_add_copy(ctx->pool, &ctx->in, in) == NGX_ERROR) {
+        if (ngx_output_chain_add_copy(ctx->pool, &ctx->in, in) == NGX_ERROR) {
             return NGX_ERROR;
         }
     }
@@ -191,11 +201,23 @@
 static ngx_inline ngx_int_t
     ngx_output_chain_need_to_copy(ngx_output_chain_ctx_t *ctx, ngx_buf_t *buf)
 {
+    ngx_uint_t  sendfile;
+
     if (ngx_buf_special(buf)) {
         return 0;
     }
 
-    if (!ctx->sendfile) {
+    sendfile = ctx->sendfile;
+
+#if (NGX_SENDFILE_LIMIT)
+
+    if (buf->in_file && buf->file_pos >= NGX_SENDFILE_LIMIT) {
+        sendfile = 0;
+    }
+
+#endif
+
+    if (!sendfile) {
 
         if (!ngx_buf_in_memory(buf)) {
             return 1;
@@ -216,6 +238,71 @@
 }
 
 
+static ngx_int_t ngx_output_chain_add_copy(ngx_pool_t *pool,
+                                           ngx_chain_t **chain, ngx_chain_t *in)
+{
+    ngx_chain_t  *cl, **ll;
+#if (NGX_SENDFILE_LIMIT)
+    ngx_buf_t    *b, *buf;
+#endif
+
+    ll = chain;
+
+    for (cl = *chain; cl; cl = cl->next) {
+        ll = &cl->next;
+    }
+
+    while (in) {
+
+        if (!(cl = ngx_alloc_chain_link(pool))) {
+            return NGX_ERROR;
+        }
+
+#if (NGX_SENDFILE_LIMIT)
+
+        buf = in->buf;
+
+        if (buf->in_file
+            && buf->file_pos < NGX_SENDFILE_LIMIT
+            && buf->file_last > NGX_SENDFILE_LIMIT)
+        {
+            if (!(b = ngx_calloc_buf(pool))) {
+                return NGX_ERROR;
+            }
+
+            ngx_memcpy(b, buf, sizeof(ngx_buf_t));
+
+            if (ngx_buf_in_memory(buf)) {
+                buf->pos += (ssize_t) (NGX_SENDFILE_LIMIT - buf->file_pos);
+                b->last = buf->pos;
+            }
+
+            buf->file_pos = NGX_SENDFILE_LIMIT;
+            b->file_last = NGX_SENDFILE_LIMIT;
+
+            cl->buf = b;
+
+        } else {
+            cl->buf = buf;
+            in = in->next;
+        }
+
+#else
+        cl->buf = in->buf;
+        in = in->next;
+
+#endif
+
+        *ll = cl;
+        ll = &cl->next;
+    }
+
+    *ll = NULL;
+
+    return NGX_OK;
+}
+
+
 static ngx_int_t ngx_output_chain_copy_buf(ngx_buf_t *dst, ngx_buf_t *src,
                                            ngx_uint_t sendfile)
 {
@@ -228,6 +315,14 @@
         size = dst->end - dst->pos;
     }
 
+#if (NGX_SENDFILE_LIMIT)
+
+    if (src->in_file && src->file_pos >= NGX_SENDFILE_LIMIT) {
+        sendfile = 0;
+    }
+
+#endif
+
     if (ngx_buf_in_memory(src)) {
         ngx_memcpy(dst->pos, src->pos, size);
         src->pos += size;
diff --git a/src/core/ngx_times.c b/src/core/ngx_times.c
index f06d644..5ca235c 100644
--- a/src/core/ngx_times.c
+++ b/src/core/ngx_times.c
@@ -162,12 +162,12 @@
     ngx_cached_http_time.data = p;
 
 
-#if (HAVE_GETTIMEZONE)
+#if (NGX_HAVE_GETTIMEZONE)
 
     ngx_gmtoff = ngx_gettimezone();
     ngx_gmtime(s + ngx_gmtoff * 60, &tm);
 
-#elif (HAVE_GMTOFF)
+#elif (NGX_HAVE_GMTOFF)
 
     ngx_localtime(&tm);
     ngx_gmtoff = tm.ngx_tm_gmtoff / 60;
diff --git a/src/event/modules/ngx_aio_module.c b/src/event/modules/ngx_aio_module.c
index 398c571..bdd31cf 100644
--- a/src/event/modules/ngx_aio_module.c
+++ b/src/event/modules/ngx_aio_module.c
@@ -9,7 +9,7 @@
 #include <ngx_event.h>
 #include <ngx_aio.h>
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 #include <ngx_kqueue_module.h>
 #endif
 
@@ -27,7 +27,7 @@
     ngx_aio_read_chain,
     ngx_aio_write,
     ngx_aio_write_chain,
-    NGX_HAVE_ZEROCOPY
+    0
 };
 
 
@@ -64,7 +64,7 @@
 
 
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 
 static int ngx_aio_init(ngx_cycle_t *cycle)
 {
@@ -152,7 +152,7 @@
     return ngx_kqueue_module_ctx.actions.process_events(cycle);
 }
 
-#endif /* HAVE_KQUEUE */
+#endif /* NGX_HAVE_KQUEUE */
 
 
 #if 0
diff --git a/src/event/modules/ngx_devpoll_module.c b/src/event/modules/ngx_devpoll_module.c
index 663e2bf..1a48cec 100644
--- a/src/event/modules/ngx_devpoll_module.c
+++ b/src/event/modules/ngx_devpoll_module.c
@@ -9,7 +9,7 @@
 #include <ngx_event.h>
 
 
-#if (TEST_BUILD_DEVPOLL)
+#if (NGX_TEST_BUILD_DEVPOLL)
 
 /* Solaris declarations */
 
diff --git a/src/event/modules/ngx_epoll_module.c b/src/event/modules/ngx_epoll_module.c
index 8096932..a2eeab0 100644
--- a/src/event/modules/ngx_epoll_module.c
+++ b/src/event/modules/ngx_epoll_module.c
@@ -9,7 +9,7 @@
 #include <ngx_event.h>
 
 
-#if (TEST_BUILD_EPOLL)
+#if (NGX_TEST_BUILD_EPOLL)
 
 /* epoll declarations */
 
@@ -168,7 +168,7 @@
 
     ngx_event_actions = ngx_epoll_module_ctx.actions;
 
-#if (HAVE_CLEAR_EVENT)
+#if (NGX_HAVE_CLEAR_EVENT)
     ngx_event_flags = NGX_USE_CLEAR_EVENT
 #else
     ngx_event_flags = NGX_USE_LEVEL_EVENT
diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c
index e0fae6f..49c22a9 100644
--- a/src/event/modules/ngx_kqueue_module.c
+++ b/src/event/modules/ngx_kqueue_module.c
@@ -189,7 +189,7 @@
     ngx_event_actions = ngx_kqueue_module_ctx.actions;
 
     ngx_event_flags = NGX_USE_ONESHOT_EVENT
-#if (HAVE_CLEAR_EVENT)
+#if (NGX_HAVE_CLEAR_EVENT)
                      |NGX_USE_CLEAR_EVENT
 #else
                      |NGX_USE_LEVEL_EVENT
diff --git a/src/event/modules/ngx_rtsig_module.c b/src/event/modules/ngx_rtsig_module.c
index 3c0ea95..d017f05 100644
--- a/src/event/modules/ngx_rtsig_module.c
+++ b/src/event/modules/ngx_rtsig_module.c
@@ -9,7 +9,7 @@
 #include <ngx_event.h>
 
 
-#if (TEST_BUILD_RTSIG)
+#if (NGX_TEST_BUILD_RTSIG)
 
 #define F_SETSIG       10
 #define SIGRTMIN       33
@@ -217,7 +217,7 @@
         return NGX_ERROR;
     }
 
-#if (HAVE_ONESIGFD)
+#if (NGX_HAVE_ONESIGFD)
     if (fcntl(c->fd, F_SETAUXFL, O_ONESIGFD) == -1) {
         ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
                       "fcntl(F_SETAUXFL) failed");
diff --git a/src/event/modules/ngx_select_module.c b/src/event/modules/ngx_select_module.c
index 14e6f81..0554ee9 100644
--- a/src/event/modules/ngx_select_module.c
+++ b/src/event/modules/ngx_select_module.c
@@ -258,7 +258,7 @@
     ngx_connection_t         *c;
     ngx_epoch_msec_t          delta;
     struct timeval            tv, *tp;
-#if (HAVE_SELECT_CHANGE_TIMEOUT)
+#if (NGX_HAVE_SELECT_CHANGE_TIMEOUT)
     static ngx_epoch_msec_t   deltas = 0;
 #endif
 
@@ -362,7 +362,7 @@
         err = 0;
     }
 
-#if (HAVE_SELECT_CHANGE_TIMEOUT)
+#if (NGX_HAVE_SELECT_CHANGE_TIMEOUT)
 
     if (timer != NGX_TIMER_INFINITE) {
         delta = timer - (tv.tv_sec * 1000 + tv.tv_usec / 1000);
@@ -403,7 +403,7 @@
         }
     }
 
-#else /* !(HAVE_SELECT_CHANGE_TIMEOUT) */
+#else /* !(NGX_HAVE_SELECT_CHANGE_TIMEOUT) */
 
     ngx_gettimeofday(&tv);
     ngx_time_update(tv.tv_sec);
@@ -427,7 +427,7 @@
         }
     }
 
-#endif /* HAVE_SELECT_CHANGE_TIMEOUT */
+#endif /* NGX_HAVE_SELECT_CHANGE_TIMEOUT */
 
     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                    "select ready %d", ready);
diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c
index b3bf530..bd451bc 100644
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -371,7 +371,7 @@
 
         rev->accept = 1;
 
-#if (HAVE_DEFERRED_ACCEPT)
+#if (NGX_HAVE_DEFERRED_ACCEPT)
         rev->deferred_accept = s[i].deferred_accept;
 #endif
 
@@ -719,7 +719,7 @@
     rtsig = 0;
     fd = 0;
 
-#if (HAVE_EPOLL) && !(TEST_BUILD_EPOLL)
+#if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
 
     fd = epoll_create(100);
 
@@ -735,7 +735,7 @@
 
 #endif
 
-#if (HAVE_RTSIG)
+#if (NGX_HAVE_RTSIG)
 
     if (module == NULL) {
         connections = DEFAULT_CONNECTIONS;
@@ -745,21 +745,21 @@
 
 #endif
 
-#if (HAVE_DEVPOLL)
+#if (NGX_HAVE_DEVPOLL)
 
     connections = DEFAULT_CONNECTIONS;
     module = &ngx_devpoll_module;
 
 #endif
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 
     connections = DEFAULT_CONNECTIONS;
     module = &ngx_kqueue_module;
 
 #endif
 
-#if (HAVE_SELECT)
+#if (NGX_HAVE_SELECT)
 
     if (module == NULL) {
 
diff --git a/src/event/ngx_event.h b/src/event/ngx_event.h
index 44728e3..d156a63 100644
--- a/src/event/ngx_event.h
+++ b/src/event/ngx_event.h
@@ -15,7 +15,7 @@
 #define NGX_INVALID_INDEX  0xd0d0d0d0
 
 
-#if (HAVE_IOCP)
+#if (NGX_HAVE_IOCP)
 
 typedef struct {
     WSAOVERLAPPED    ovlp;
@@ -86,7 +86,7 @@
     unsigned         accept_context_updated:1;
 #endif
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
     unsigned         kq_vnode:1;
 
     /* the pending errno reported by kqueue */
@@ -107,7 +107,7 @@
      *   accept:     1 if accept many, 0 otherwise
      */
 
-#if (HAVE_KQUEUE) || (HAVE_IOCP)
+#if (NGX_HAVE_KQUEUE) || (NGX_HAVE_IOCP)
     int              available;
 #else
     unsigned         available:1;
@@ -117,9 +117,9 @@
     ngx_event_handler_pt  event_handler;
 
 
-#if (HAVE_AIO)
+#if (NGX_HAVE_AIO)
 
-#if (HAVE_IOCP)
+#if (NGX_HAVE_IOCP)
     ngx_event_ovlp_t ovlp;
 #else
     struct aiocb     aiocb;
@@ -154,12 +154,12 @@
     unsigned         posted_timedout:1;
     unsigned         posted_eof:1;
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
     /* the pending errno reported by kqueue */
     int              posted_errno;
 #endif
 
-#if (HAVE_KQUEUE) || (HAVE_IOCP)
+#if (NGX_HAVE_KQUEUE) || (NGX_HAVE_IOCP)
     int              posted_available;
 #else
     unsigned         posted_available:1;
@@ -272,7 +272,7 @@
 
 /*
  * Need to add socket or handle only once - i/o completion port.
- * It also requires HAVE_AIO and NGX_USE_AIO_EVENT to be set.
+ * It also requires NGX_HAVE_AIO and NGX_USE_AIO_EVENT to be set.
  */
 #define NGX_USE_IOCP_EVENT       0x00000200
 
@@ -296,7 +296,7 @@
 #define NGX_VNODE_EVENT    0
 
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 
 #define NGX_READ_EVENT     EVFILT_READ
 #define NGX_WRITE_EVENT    EVFILT_WRITE
@@ -325,7 +325,7 @@
 #define NGX_DISABLE_EVENT  EV_DISABLE
 
 
-#elif (HAVE_DEVPOLL)
+#elif (NGX_HAVE_DEVPOLL)
 
 #define NGX_READ_EVENT     POLLIN
 #define NGX_WRITE_EVENT    POLLOUT
@@ -334,7 +334,7 @@
 #define NGX_ONESHOT_EVENT  1
 
 
-#elif (HAVE_EPOLL)
+#elif (NGX_HAVE_EPOLL)
 
 #define NGX_READ_EVENT     EPOLLIN
 #define NGX_WRITE_EVENT    EPOLLOUT
@@ -347,7 +347,7 @@
 #endif
 
 
-#elif (HAVE_POLL)
+#elif (NGX_HAVE_POLL)
 
 #define NGX_READ_EVENT     POLLIN
 #define NGX_WRITE_EVENT    POLLOUT
@@ -364,10 +364,10 @@
 #define NGX_LEVEL_EVENT    0
 #define NGX_ONESHOT_EVENT  1
 
-#endif /* HAVE_KQUEUE */
+#endif /* NGX_HAVE_KQUEUE */
 
 
-#if (HAVE_IOCP)
+#if (NGX_HAVE_IOCP)
 #define NGX_IOCP_ACCEPT      0
 #define NGX_IOCP_IO          1
 #define NGX_IOCP_CONNECT     2
diff --git a/src/event/ngx_event_openssl.c b/src/event/ngx_event_openssl.c
index b5c5b72..294f3b4 100644
--- a/src/event/ngx_event_openssl.c
+++ b/src/event/ngx_event_openssl.c
@@ -9,7 +9,7 @@
 #include <ngx_event.h>
 
 
-static ngx_int_t ngx_ssl_write(ngx_connection_t *c, u_char *data, size_t size);
+static ssize_t ngx_ssl_write(ngx_connection_t *c, u_char *data, size_t size);
 
 
 ngx_int_t ngx_ssl_init(ngx_log_t *log)
@@ -58,7 +58,7 @@
 }
 
 
-ngx_int_t ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size)
+ssize_t ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size)
 {
     int         n, sslerr;
     ngx_err_t   err;
@@ -239,7 +239,7 @@
 }
 
 
-static ngx_int_t ngx_ssl_write(ngx_connection_t *c, u_char *data, size_t size)
+static ssize_t ngx_ssl_write(ngx_connection_t *c, u_char *data, size_t size)
 {
     int         n, sslerr;
     ngx_err_t   err;
diff --git a/src/event/ngx_event_openssl.h b/src/event/ngx_event_openssl.h
index 69d8d12..dbdb5f7 100644
--- a/src/event/ngx_event_openssl.h
+++ b/src/event/ngx_event_openssl.h
@@ -41,7 +41,7 @@
 
 #define ngx_ssl_handshake(c)     NGX_OK
 
-ngx_int_t ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size);
+ssize_t ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size);
 ngx_chain_t *ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in,
                                 off_t limit);
 ngx_int_t ngx_ssl_shutdown(ngx_connection_t *c);
diff --git a/src/event/ngx_event_pipe.c b/src/event/ngx_event_pipe.c
index a2edc71..3ca8a0c 100644
--- a/src/event/ngx_event_pipe.c
+++ b/src/event/ngx_event_pipe.c
@@ -132,7 +132,7 @@
                 p->upstream_eof = 1;
                 p->read = 1;
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
                 if (p->upstream->read->kq_errno) {
                     p->upstream->read->error = 1;
                     p->upstream_error = 1;
diff --git a/src/event/ngx_event_posted.c b/src/event/ngx_event_posted.c
index aed07fa..a99a434 100644
--- a/src/event/ngx_event_posted.c
+++ b/src/event/ngx_event_posted.c
@@ -120,7 +120,7 @@
             ev->ready |= ev->posted_ready;
             ev->timedout |= ev->posted_timedout;
             ev->pending_eof |= ev->posted_eof;
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
             ev->kq_errno |= ev->posted_errno;
 #endif
             if (ev->posted_available) {
@@ -130,7 +130,7 @@
             ev->posted_ready = 0;
             ev->posted_timedout = 0;
             ev->posted_eof = 0;
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
             ev->posted_errno = 0;
 #endif
             ev->posted_available = 0;
diff --git a/src/http/modules/ngx_http_gzip_filter.c b/src/http/modules/ngx_http_gzip_filter.c
index f8980af..b093d58 100644
--- a/src/http/modules/ngx_http_gzip_filter.c
+++ b/src/http/modules/ngx_http_gzip_filter.c
@@ -237,14 +237,14 @@
 
 static u_char  gzheader[10] = { 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 };
 
-#if (HAVE_LITTLE_ENDIAN)
+#if (NGX_HAVE_LITTLE_ENDIAN)
 
 struct gztrailer {
     uint32_t  crc32;
     uint32_t  zlen;
 };
 
-#else /* HAVE_BIG_ENDIAN */
+#else /* NGX_HAVE_BIG_ENDIAN */
 
 struct gztrailer {
     u_char  crc32[4];
@@ -437,7 +437,8 @@
 static ngx_int_t ngx_http_gzip_body_filter(ngx_http_request_t *r,
                                            ngx_chain_t *in)
 {
-    int                    rc, wbits, memlevel, last;
+    int                    rc, wbits, memlevel;
+    ngx_int_t              last;
     struct gztrailer      *trailer;
     ngx_buf_t             *b;
     ngx_chain_t           *cl;
@@ -469,7 +470,7 @@
         /*
          * We preallocate a memory for zlib in one buffer (200K-400K), this
          * dicreases a number of malloc() and free() calls and also probably
-         * dicreases a number of syscalls (sbrk() or so).
+         * dicreases a number of syscalls (sbrk() and so on).
          * Besides we free this memory as soon as the gzipping will complete
          * and do not wait while a whole response will be sent to a client.
          *
@@ -512,8 +513,19 @@
         }
         cl->buf = b;
         cl->next = NULL;
-        ctx->out = cl;
-        ctx->last_out = &cl->next;
+
+        /*
+         * We pass the gzheader to the next filter now to avoid its linking
+         * to the ctx->busy chain.  zlib does not usually output the compressed
+         * data in the initial iterations, so the gzheader that was linked
+         * to the ctx->busy chain would be flushed by ngx_http_write_filter().
+         */
+
+        if (ngx_http_next_body_filter(r, cl) == NGX_ERROR) {
+            return ngx_http_gzip_error(ctx);
+        }
+
+        ctx->last_out = &ctx->out;
 
         ctx->crc32 = crc32(0L, Z_NULL, 0);
         ctx->flush = Z_NO_FLUSH;
@@ -727,7 +739,7 @@
                     b->last += 8;
                 }
 
-#if (HAVE_LITTLE_ENDIAN)
+#if (NGX_HAVE_LITTLE_ENDIAN)
                 trailer->crc32 = ctx->crc32;
                 trailer->zlen = ctx->zin;
 #else
@@ -763,7 +775,7 @@
             }
         }
 
-        if (last == NGX_AGAIN) {
+        if (last == NGX_AGAIN && !ctx->done) {
             return NGX_AGAIN;
         }
 
@@ -881,7 +893,9 @@
 {
     deflateEnd(&ctx->zstream);
 
-    ngx_pfree(ctx->request->pool, ctx->preallocated);
+    if (ctx->preallocated) {
+        ngx_pfree(ctx->request->pool, ctx->preallocated);
+    }
 
     ctx->zstream.avail_in = 0;
     ctx->zstream.avail_out = 0;
diff --git a/src/http/modules/ngx_http_index_handler.c b/src/http/modules/ngx_http_index_handler.c
index 013f62b..3386953 100644
--- a/src/http/modules/ngx_http_index_handler.c
+++ b/src/http/modules/ngx_http_index_handler.c
@@ -10,19 +10,19 @@
 
 
 typedef struct {
-    ngx_array_t             indices;
-    size_t                  max_index_len;
-    ngx_http_cache_hash_t  *index_cache;
+    ngx_array_t              indices;
+    size_t                   max_index_len;
+    ngx_http_cache_hash_t   *index_cache;
 } ngx_http_index_loc_conf_t;
 
 
 typedef struct {
-    ngx_uint_t         index;
-    u_char            *last;
-    ngx_str_t          path;
-    ngx_str_t          redirect;
-    ngx_http_cache_t  *cache;
-    unsigned           tested:1;
+    ngx_uint_t               index;
+    u_char                  *last;
+    ngx_str_t                path;
+    ngx_str_t                redirect;
+    ngx_http_cache_entry_t  *cache;
+    ngx_uint_t               tested; /* unsigned  tested:1 */
 } ngx_http_index_ctx_t;
 
 
diff --git a/src/http/modules/proxy/ngx_http_proxy_cache.c b/src/http/modules/proxy/ngx_http_proxy_cache.c
index f0b56f5..86b8424 100644
--- a/src/http/modules/proxy/ngx_http_proxy_cache.c
+++ b/src/http/modules/proxy/ngx_http_proxy_cache.c
@@ -297,7 +297,7 @@
 
     if (rc == NGX_AGAIN) {
 
-        if ((ngx_event_flags & (NGX_USE_CLEAR_EVENT|NGX_HAVE_KQUEUE_EVENT))
+        if ((ngx_event_flags & (NGX_USE_CLEAR_EVENT|NGX_USE_KQUEUE_EVENT))
             && !p->request->connection->write->active)
         {
             /*
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.c b/src/http/modules/proxy/ngx_http_proxy_handler.c
index c0a8cf3..425a1a6 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -414,7 +414,7 @@
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ev->log, 0,
                    "http proxy check client, write event:%d", ev->write);
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 
     if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
 
@@ -563,9 +563,9 @@
      */
 
 #if 0
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 
-    if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) && rev->kq_eof) {
+    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && rev->kq_eof) {
         ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
 
         ngx_del_timer(rev);
@@ -1400,7 +1400,7 @@
         return NGX_CONF_ERROR;
     }
 
-#elif !(HAVE_SO_SNDLOWAT)
+#elif !(NGX_HAVE_SO_SNDLOWAT)
 
     ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
                        "\"proxy_send_lowat\" is not supported, ignored");
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.h b/src/http/modules/proxy/ngx_http_proxy_handler.h
index 094ee3b..8ab88ad 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.h
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.h
@@ -142,7 +142,7 @@
 
 
 typedef struct {
-    ngx_http_cache_ctx_t             ctx;
+    ngx_http_cache_t                 ctx;
     ngx_uint_t                       status;
     ngx_str_t                        status_line;
 
diff --git a/src/http/modules/proxy/ngx_http_proxy_upstream.c b/src/http/modules/proxy/ngx_http_proxy_upstream.c
index 0852a42..23d40c4 100644
--- a/src/http/modules/proxy/ngx_http_proxy_upstream.c
+++ b/src/http/modules/proxy/ngx_http_proxy_upstream.c
@@ -782,7 +782,7 @@
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
                    "http proxy send request");
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 
     if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT)
         && !p->request_sent
@@ -1287,8 +1287,8 @@
         header->length = r->headers_out.content_length_n;
         p->cache->ctx.length = r->headers_out.content_length_n;
 
-        header->key_len = p->cache->ctx.key.len;
-        ngx_memcpy(&header->key, p->cache->ctx.key.data, header->key_len);
+        header->key_len = p->cache->ctx.key0.len;
+        ngx_memcpy(&header->key, p->cache->ctx.key0.data, header->key_len);
         header->key[header->key_len] = LF;
     }
 
diff --git a/src/http/ngx_http_cache.c b/src/http/ngx_http_cache.c
index 22572a5..abdeae9 100644
--- a/src/http/ngx_http_cache.c
+++ b/src/http/ngx_http_cache.c
@@ -63,7 +63,7 @@
 
             c[i].refs++;
 
-            if ((!(c[i].notify && (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)))
+            if ((!(c[i].notify && (ngx_event_flags & NGX_USE_KQUEUE_EVENT)))
                 && (ngx_cached_time - c[i].updated >= hash->update))
             {
                 c[i].expired = 1;
diff --git a/src/http/ngx_http_cache.h b/src/http/ngx_http_cache.h
index 40f4852..aa66902 100644
--- a/src/http/ngx_http_cache.h
+++ b/src/http/ngx_http_cache.h
@@ -14,11 +14,12 @@
 
 
 /*
- * The 7 uses before an allocation.
+ * The 3 bits allows the 7 uses before the cache entry allocation.
  * We can use maximum 7 bits, i.e up to the 127 uses.
  */
 #define NGX_HTTP_CACHE_LAZY_ALLOCATION_BITS  3
 
+
 typedef struct {
     uint32_t         crc;
     ngx_str_t        key;
@@ -45,7 +46,7 @@
         off_t        size;
         ngx_str_t    value;
     } data;
-} ngx_http_cache_t;
+} ngx_http_cache_entry_t;
 
 
 typedef struct {
@@ -62,7 +63,7 @@
 #define NGX_HTTP_CACHE_NELTS  4
 
 typedef struct {
-    ngx_http_cache_t         *elts;
+    ngx_http_cache_entry_t   *elts;
     size_t                    hash;
     size_t                    nelts;
     time_t                    life;
@@ -76,9 +77,9 @@
 
 typedef struct {
     ngx_http_cache_hash_t    *hash;
-    ngx_http_cache_t         *cache;
+    ngx_http_cache_entry_t   *cache;
     ngx_file_t                file;
-    ngx_str_t                 key;
+    ngx_array_t               key;
     uint32_t                  crc;
     u_char                    md5[16];
     ngx_path_t               *path;
@@ -90,7 +91,10 @@
     ssize_t                   header_size;
     size_t                    file_start;
     ngx_log_t                *log;
-} ngx_http_cache_ctx_t;
+
+    /* STUB */
+    ngx_str_t                 key0;
+} ngx_http_cache_t;
 
 
 
@@ -99,6 +103,8 @@
 #define NGX_HTTP_CACHE_THE_SAME  3
 
 
+#if 0
+
 ngx_http_cache_t *ngx_http_cache_get(ngx_http_cache_hash_t *cache,
                                      ngx_http_cleanup_t *cleanup,
                                      ngx_str_t *key, uint32_t *crc);
@@ -127,5 +133,7 @@
 
 char *ngx_http_set_cache_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 
+#endif
+
 
 #endif /* _NGX_HTTP_CACHE_H_INCLUDED_ */
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index c764b2a..c4cb8b9 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -1828,7 +1828,7 @@
         return NGX_CONF_ERROR;
     }
 
-#elif !(HAVE_SO_SNDLOWAT)
+#elif !(NGX_HAVE_SO_SNDLOWAT)
 
     ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
                        "\"send_lowat\" is not supported, ignored");
diff --git a/src/http/ngx_http_file_cache.c b/src/http/ngx_http_file_cache.c
index 8f920d2..1b258c9 100644
--- a/src/http/ngx_http_file_cache.c
+++ b/src/http/ngx_http_file_cache.c
@@ -9,19 +9,20 @@
 #include <ngx_http.h>
 
 
-#if (HAVE_OPENSSL_MD5_H)
+#if (NGX_HAVE_OPENSSL_MD5_H)
 #include <openssl/md5.h>
 #else
 #include <md5.h>
 #endif
 
-#if (HAVE_OPENSSL_MD5)
+#if (NGX_OPENSSL_MD5)
 #define  MD5Init    MD5_Init
 #define  MD5Update  MD5_Update
 #define  MD5Final   MD5_Final
 #endif
 
 
+#if 0
 
 int ngx_http_cache_get_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx)
 {
@@ -237,3 +238,5 @@
 
     return NGX_OK;
 }
+
+#endif
diff --git a/src/http/ngx_http_parse.c b/src/http/ngx_http_parse.c
index bfe5efb..a4e7d80 100644
--- a/src/http/ngx_http_parse.c
+++ b/src/http/ngx_http_parse.c
@@ -686,7 +686,7 @@
 
     ch = *p++;
 
-    while (p < r->uri_start + r->uri.len + 1 && r->args_start == NULL) {
+    while (p < r->uri_end && r->args_start == NULL) {
 
         /*
          * we use "ch = *p++" inside the cycle but this operation is safe
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index a2cf1a0..b6013fc 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -532,6 +532,7 @@
                 r->uri.len = r->uri_end - r->uri_start;
             }
 
+
             if (r->complex_uri || r->quoted_uri) {
 
                 if (!(r->uri.data = ngx_palloc(r->pool, r->uri.len + 1))) {
@@ -557,6 +558,7 @@
                 r->uri.data = r->uri_start;
             }
 
+
             r->unparsed_uri.len = r->uri_end - r->uri_start;
             r->unparsed_uri.data = r->uri_start;
 
@@ -1751,7 +1753,7 @@
 
     ctx = (ngx_http_log_ctx_t *) rev->log->data;
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 
     if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
         if (rev->pending_eof) {
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h
index 31b1187..141daa1 100644
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -220,7 +220,7 @@
 
         struct {
             ngx_http_cache_hash_t   *hash;
-            ngx_http_cache_t        *cache;
+            ngx_http_cache_entry_t  *cache;
         } cache;
     } data;
 
@@ -254,7 +254,7 @@
     void                    **srv_conf;
     void                    **loc_conf;
 
-    ngx_http_cache_t         *cache;
+    ngx_http_cache_entry_t   *cache;
 
     ngx_file_t                file;
 
diff --git a/src/os/unix/ngx_aio_read.c b/src/os/unix/ngx_aio_read.c
index d011054..2a66eb8 100644
--- a/src/os/unix/ngx_aio_read.c
+++ b/src/os/unix/ngx_aio_read.c
@@ -9,7 +9,7 @@
 #include <ngx_event.h>
 #include <ngx_aio.h>
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 #include <ngx_kqueue_module.h>
 #endif
 
@@ -46,7 +46,7 @@
         rev->aiocb.aio_buf = buf;
         rev->aiocb.aio_nbytes = size;
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
         rev->aiocb.aio_sigevent.sigev_notify_kqueue = ngx_kqueue;
         rev->aiocb.aio_sigevent.sigev_notify = SIGEV_KEVENT;
         rev->aiocb.aio_sigevent.sigev_value.sigval_ptr = rev;
diff --git a/src/os/unix/ngx_aio_write.c b/src/os/unix/ngx_aio_write.c
index f102ba3..1d8c1aa 100644
--- a/src/os/unix/ngx_aio_write.c
+++ b/src/os/unix/ngx_aio_write.c
@@ -9,7 +9,7 @@
 #include <ngx_event.h>
 #include <ngx_aio.h>
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 #include <ngx_kqueue_module.h>
 #endif
 
@@ -43,7 +43,7 @@
         wev->aiocb.aio_buf = buf;
         wev->aiocb.aio_nbytes = size;
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
         wev->aiocb.aio_sigevent.sigev_notify_kqueue = ngx_kqueue;
         wev->aiocb.aio_sigevent.sigev_notify = SIGEV_KEVENT;
         wev->aiocb.aio_sigevent.sigev_value.sigval_ptr = wev;
diff --git a/src/os/unix/ngx_alloc.c b/src/os/unix/ngx_alloc.c
index 7ec4f87..258a10f 100644
--- a/src/os/unix/ngx_alloc.c
+++ b/src/os/unix/ngx_alloc.c
@@ -40,7 +40,7 @@
 }
 
 
-#if (HAVE_POSIX_MEMALIGN)
+#if (NGX_HAVE_POSIX_MEMALIGN)
 
 void *ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
 {
@@ -58,7 +58,7 @@
     return p;
 }
 
-#elif (HAVE_MEMALIGN)
+#elif (NGX_HAVE_MEMALIGN)
 
 void *ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
 {
diff --git a/src/os/unix/ngx_alloc.h b/src/os/unix/ngx_alloc.h
index 0084c2c..66562a1 100644
--- a/src/os/unix/ngx_alloc.h
+++ b/src/os/unix/ngx_alloc.h
@@ -25,7 +25,7 @@
  * allocations bigger than page size at the page boundary.
  */
 
-#if (HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN)
+#if (NGX_HAVE_POSIX_MEMALIGN || NGX_HAVE_MEMALIGN)
 
 void *ngx_memalign(size_t alignment, size_t size, ngx_log_t *log);
 
diff --git a/src/os/unix/ngx_channel.c b/src/os/unix/ngx_channel.c
index 0bfbd0d..b1b36c5 100644
--- a/src/os/unix/ngx_channel.c
+++ b/src/os/unix/ngx_channel.c
@@ -17,7 +17,7 @@
     struct iovec        iov[1];
     struct msghdr       msg;
 
-#if (HAVE_MSGHDR_MSG_CONTROL)
+#if (NGX_HAVE_MSGHDR_MSG_CONTROL)
 
     union {
         struct cmsghdr  cm;
@@ -83,7 +83,7 @@
     struct iovec        iov[1];
     struct msghdr       msg;
 
-#if (HAVE_MSGHDR_MSG_CONTROL)
+#if (NGX_HAVE_MSGHDR_MSG_CONTROL)
     union {
         struct cmsghdr  cm;
         char            space[CMSG_SPACE(sizeof(int))];
@@ -100,7 +100,7 @@
     msg.msg_iov = iov;
     msg.msg_iovlen = 1;
 
-#if (HAVE_MSGHDR_MSG_CONTROL)
+#if (NGX_HAVE_MSGHDR_MSG_CONTROL)
     msg.msg_control = (caddr_t) &cmsg;
     msg.msg_controllen = sizeof(cmsg);
 #else
@@ -131,7 +131,7 @@
         return NGX_ERROR;
     }
 
-#if (HAVE_MSGHDR_MSG_CONTROL)
+#if (NGX_HAVE_MSGHDR_MSG_CONTROL)
 
     if (ch->command == NGX_CMD_OPEN_CHANNEL) {
 
diff --git a/src/os/unix/ngx_errno.c b/src/os/unix/ngx_errno.c
index ced0eaf..607b361 100644
--- a/src/os/unix/ngx_errno.c
+++ b/src/os/unix/ngx_errno.c
@@ -8,7 +8,7 @@
 #include <ngx_core.h>
 
 
-#if (NGX_STRERROR_R)
+#if (NGX_HAVE_STRERROR_R)
 
 u_char *ngx_strerror_r(int err, u_char *errstr, size_t size)
 {
@@ -28,7 +28,7 @@
     return errstr;
 }
 
-#elif (NGX_GNU_STRERROR_R)
+#elif (NGX_HAVE_GNU_STRERROR_R)
 
 /* Linux strerror_r() */
 
diff --git a/src/os/unix/ngx_errno.h b/src/os/unix/ngx_errno.h
index e87ec5f..c1c5801 100644
--- a/src/os/unix/ngx_errno.h
+++ b/src/os/unix/ngx_errno.h
@@ -46,7 +46,7 @@
 #define ngx_set_socket_errno(err)  errno = err
 
 
-#if (HAVE_STRERROR_R || HAVE_GNU_STRERROR_R)
+#if (NGX_HAVE_STRERROR_R || NGX_HAVE_GNU_STRERROR_R)
 
 u_char *ngx_strerror_r(int err, u_char *errstr, size_t size);
 
diff --git a/src/os/unix/ngx_files.c b/src/os/unix/ngx_files.c
index 52aa9b6..e67b2ca 100644
--- a/src/os/unix/ngx_files.c
+++ b/src/os/unix/ngx_files.c
@@ -15,7 +15,7 @@
     ngx_log_debug4(NGX_LOG_DEBUG_CORE, file->log, 0,
                    "read: %d, %p, %uz, %O", file->fd, buf, size, offset);
 
-#if (NGX_PREAD)
+#if (NGX_HAVE_PREAD)
 
     n = pread(file->fd, buf, size, offset);
 
@@ -60,7 +60,7 @@
     ngx_log_debug4(NGX_LOG_DEBUG_CORE, file->log, 0,
                    "write: %d, %p, %uz, %O", file->fd, buf, size, offset);
 
-#if (NGX_PWRITE)
+#if (NGX_HAVE_PWRITE)
 
     n = pwrite(file->fd, buf, size, offset);
 
diff --git a/src/os/unix/ngx_freebsd_config.h b/src/os/unix/ngx_freebsd_config.h
index 7f3b286..35bf1f5 100644
--- a/src/os/unix/ngx_freebsd_config.h
+++ b/src/os/unix/ngx_freebsd_config.h
@@ -60,23 +60,23 @@
 #include <ngx_auto_config.h>
 
 
-#if (HAVE_POLL)
+#if (NGX_HAVE_POLL)
 #include <poll.h>
 #endif
 
 
-#if (HAVE_AIO)
+#if (NGX_HAVE_AIO)
 #include <aio.h>
 #endif
 
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 #include <sys/event.h>
 #endif
 
 
-#if defined SO_ACCEPTFILTER && !defined HAVE_DEFERRED_ACCEPT
-#define HAVE_DEFERRED_ACCEPT  1
+#if defined SO_ACCEPTFILTER && !defined NGX_HAVE_DEFERRED_ACCEPT
+#define NGX_HAVE_DEFERRED_ACCEPT  1
 #endif
 
 
@@ -91,8 +91,8 @@
 #endif
 
 
-#ifndef HAVE_INHERITED_NONBLOCK
-#define HAVE_INHERITED_NONBLOCK  1
+#ifndef NGX_HAVE_INHERITED_NONBLOCK
+#define NGX_HAVE_INHERITED_NONBLOCK  1
 #endif
 
 
diff --git a/src/os/unix/ngx_freebsd_init.c b/src/os/unix/ngx_freebsd_init.c
index faf8e9f..b76dc0c 100644
--- a/src/os/unix/ngx_freebsd_init.c
+++ b/src/os/unix/ngx_freebsd_init.c
@@ -30,7 +30,7 @@
     ngx_unix_recv,
     ngx_readv_chain,
     ngx_unix_send,
-#if (HAVE_SENDFILE)
+#if (NGX_HAVE_SENDFILE)
     ngx_freebsd_sendfile_chain,
     NGX_IO_SENDFILE
 #else
@@ -128,7 +128,7 @@
     version = ngx_freebsd_kern_osreldate;
 
 
-#if (HAVE_SENDFILE)
+#if (NGX_HAVE_SENDFILE)
 
     /*
      * The determination of the sendfile() "nbytes bug" is complex enough.
@@ -161,7 +161,7 @@
 
 #endif
 
-#endif /* HAVE_SENDFILE */
+#endif /* NGX_HAVE_SENDFILE */
 
 
     if ((version < 500000 && version >= 440003) || version >= 500017) {
diff --git a/src/os/unix/ngx_freebsd_rfork_thread.c b/src/os/unix/ngx_freebsd_rfork_thread.c
index b64325b..5dfd468 100644
--- a/src/os/unix/ngx_freebsd_rfork_thread.c
+++ b/src/os/unix/ngx_freebsd_rfork_thread.c
@@ -381,7 +381,7 @@
                 return NGX_AGAIN;
             }
 
-            if (ngx_freebsd_hw_ncpu > 1 && tries++ < 1000) {
+            if (ngx_ncpu > 1 && tries++ < 1000) {
 
                 /* the spinlock is used only on the SMP system */
 
@@ -581,7 +581,7 @@
     }
 
     cv->signo = NGX_CV_SIGNAL;
-    cv->tid = 0;
+    cv->tid = -1;
     cv->log = log;
     cv->kq = -1;
 
@@ -640,6 +640,8 @@
             ngx_log_error(NGX_LOG_ALERT, cv->log, ngx_errno, "kevent() failed");
             return NGX_ERROR;
         }
+
+        cv->tid = ngx_thread_self();
     }
 
     if (ngx_mutex_unlock(m) == NGX_ERROR) {
@@ -714,6 +716,10 @@
                    "cv %p to signal %P %d",
                    cv, cv->tid, cv->signo);
 
+    if (cv->tid == -1) {
+        return NGX_OK;
+    }
+
     if (kill(cv->tid, cv->signo) == -1) {
 
         err = ngx_errno;
diff --git a/src/os/unix/ngx_freebsd_sendfile_chain.c b/src/os/unix/ngx_freebsd_sendfile_chain.c
index 192a1a1..f86e1c0 100644
--- a/src/os/unix/ngx_freebsd_sendfile_chain.c
+++ b/src/os/unix/ngx_freebsd_sendfile_chain.c
@@ -55,7 +55,7 @@
         return in;
     }
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 
     if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
         ngx_log_error(NGX_LOG_INFO, c->log, wev->kq_errno,
diff --git a/src/os/unix/ngx_linux_config.h b/src/os/unix/ngx_linux_config.h
index 57ee505..620b59d 100644
--- a/src/os/unix/ngx_linux_config.h
+++ b/src/os/unix/ngx_linux_config.h
@@ -56,35 +56,37 @@
 #include <sys/prctl.h>
 #endif
 
+
 #if (NGX_HAVE_SENDFILE64)
 #include <sys/sendfile.h>
 #else
 extern ssize_t sendfile(int s, int fd, int32_t *offset, size_t size);
+#define NGX_SENDFILE_LIMIT  (NGX_MAX_UINT32_VALUE + 1)
 #endif
 
 
-#if (HAVE_POLL)
+#if (NGX_HAVE_POLL)
 #include <poll.h>
 #endif
 
 
-#if (HAVE_EPOLL)
+#if (NGX_HAVE_EPOLL)
 #include <sys/epoll.h>
-#endif /* HAVE_EPOLL */
-
-
-#if defined TCP_DEFER_ACCEPT && !defined HAVE_DEFERRED_ACCEPT
-#define HAVE_DEFERRED_ACCEPT  1
 #endif
 
 
-#ifndef HAVE_INHERITED_NONBLOCK
-#define HAVE_INHERITED_NONBLOCK  0
+#if defined TCP_DEFER_ACCEPT && !defined NGX_HAVE_DEFERRED_ACCEPT
+#define NGX_HAVE_DEFERRED_ACCEPT  1
 #endif
 
 
-#ifndef HAVE_SELECT_CHANGE_TIMEOUT
-#define HAVE_SELECT_CHANGE_TIMEOUT   1
+#ifndef NGX_HAVE_INHERITED_NONBLOCK
+#define NGX_HAVE_INHERITED_NONBLOCK  0
+#endif
+
+
+#ifndef NGX_HAVE_SELECT_CHANGE_TIMEOUT
+#define NGX_HAVE_SELECT_CHANGE_TIMEOUT   1
 #endif
 
 #ifndef NGX_SETPROCTITLE_USES_ENV
diff --git a/src/os/unix/ngx_linux_init.c b/src/os/unix/ngx_linux_init.c
index 397d342..9c1c847 100644
--- a/src/os/unix/ngx_linux_init.c
+++ b/src/os/unix/ngx_linux_init.c
@@ -18,7 +18,7 @@
     ngx_unix_recv,
     ngx_readv_chain,
     ngx_unix_send,
-#if (HAVE_SENDFILE)
+#if (NGX_HAVE_SENDFILE)
     ngx_linux_sendfile_chain,
     NGX_IO_SENDFILE
 #else
diff --git a/src/os/unix/ngx_linux_sendfile_chain.c b/src/os/unix/ngx_linux_sendfile_chain.c
index ad40f55..bcf9d4c 100644
--- a/src/os/unix/ngx_linux_sendfile_chain.c
+++ b/src/os/unix/ngx_linux_sendfile_chain.c
@@ -13,7 +13,7 @@
  * On Linux up to 2.4.21 sendfile() (syscall #187) works with 32-bit
  * offsets only and the including <sys/sendfile.h> breaks the compiling
  * if off_t is 64 bit wide.  So we use own sendfile() definition where offset
- * parameter is int32_t and use sendfile() with the file parts below 2G.
+ * parameter is int32_t and use sendfile() for the file parts below 2G only.
  *
  * Linux 2.4.21 has a new sendfile64() syscall #239.
  */
diff --git a/src/os/unix/ngx_os.h b/src/os/unix/ngx_os.h
index dc9f7a8..ee91df3 100644
--- a/src/os/unix/ngx_os.h
+++ b/src/os/unix/ngx_os.h
@@ -15,18 +15,6 @@
 #define NGX_IO_SENDFILE    1
 #define NGX_IO_ZEROCOPY    2
 
-#if (HAVE_SENDFILE)
-#define NGX_HAVE_SENDFILE  NGX_IO_SENDFILE
-#else
-#define NGX_HAVE_SENDFILE  0
-#endif
-
-#if (HAVE_ZEROCOPY)
-#define NGX_HAVE_ZEROCOPY  NGX_IO_ZEROCOPY
-#else
-#define NGX_HAVE_ZEROCOPY  0
-#endif
-
 
 typedef ssize_t (*ngx_recv_pt)(ngx_connection_t *c, u_char *buf, size_t size);
 typedef ssize_t (*ngx_recv_chain_pt)(ngx_connection_t *c, ngx_chain_t *in);
diff --git a/src/os/unix/ngx_posix_config.h b/src/os/unix/ngx_posix_config.h
index b187236..f9c91a4 100644
--- a/src/os/unix/ngx_posix_config.h
+++ b/src/os/unix/ngx_posix_config.h
@@ -70,17 +70,17 @@
 #include <ngx_auto_config.h>
 
 
-#if (HAVE_POLL)
+#if (NGX_HAVE_POLL)
 #include <poll.h>
 #endif
 
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 #include <sys/event.h>
 #endif
 
 
-#if (HAVE_DEVPOLL)
+#if (NGX_HAVE_DEVPOLL)
 #include <sys/ioctl.h>
 #include <sys/devpoll.h>
 #endif
diff --git a/src/os/unix/ngx_posix_init.c b/src/os/unix/ngx_posix_init.c
index 19d65a4..56be0ce 100644
--- a/src/os/unix/ngx_posix_init.c
+++ b/src/os/unix/ngx_posix_init.c
@@ -121,7 +121,7 @@
 
     ngx_max_sockets = rlmt.rlim_cur;
 
-#if (HAVE_INHERITED_NONBLOCK)
+#if (NGX_HAVE_INHERITED_NONBLOCK)
     ngx_inherited_nonblocking = 1;
 #else
     ngx_inherited_nonblocking = 0;
diff --git a/src/os/unix/ngx_process_cycle.c b/src/os/unix/ngx_process_cycle.c
index 37b2ef1..3d1c51b 100644
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -607,6 +607,10 @@
 
 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
 {
+    ngx_int_t          n;
+    ngx_err_t          err;
+    ngx_core_conf_t   *ccf;
+
     ngx_worker_process_init(cycle);
 
     ngx_setproctitle("worker process");
@@ -618,6 +622,8 @@
         exit(2);
     }
 
+    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
+
     if (ngx_threads_n) {
         if (ngx_init_threads(ngx_threads_n,
                                    ccf->thread_stack_size, cycle) == NGX_ERROR)
@@ -914,10 +920,9 @@
 
         for (i = 0; i < ngx_threads_n; i++) {
             if (ngx_threads[i].state < NGX_THREAD_EXIT) {
-                ngx_cond_signal(ngx_threads[i].cv);
-
-                if (ngx_threads[i].cv->tid == (ngx_tid_t) -1) {
+                if (ngx_cond_signal(ngx_threads[i].cv) == NGX_ERROR) {
                     ngx_threads[i].state = NGX_THREAD_DONE;
+
                 } else {
                     live = 1;
                 }
@@ -955,8 +960,6 @@
     ngx_core_tls_t   *tls;
     ngx_cycle_t      *cycle;
 
-    thr->cv->tid = ngx_thread_self();
-
     cycle = (ngx_cycle_t *) ngx_cycle;
 
     sigemptyset(&set);
diff --git a/src/os/unix/ngx_readv_chain.c b/src/os/unix/ngx_readv_chain.c
index 2db998a..7c57b7a 100644
--- a/src/os/unix/ngx_readv_chain.c
+++ b/src/os/unix/ngx_readv_chain.c
@@ -9,7 +9,7 @@
 #include <ngx_event.h>
 
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 
 ssize_t ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain)
 {
@@ -136,7 +136,7 @@
     return n;
 }
 
-#else /* ! NAVE_KQUEUE */
+#else /* ! NGX_HAVE_KQUEUE */
 
 ssize_t ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain)
 {
@@ -216,4 +216,4 @@
     return n;
 }
 
-#endif /* NAVE_KQUEUE */
+#endif /* NGX_HAVE_KQUEUE */
diff --git a/src/os/unix/ngx_recv.c b/src/os/unix/ngx_recv.c
index d58ca9e..369be66 100644
--- a/src/os/unix/ngx_recv.c
+++ b/src/os/unix/ngx_recv.c
@@ -9,7 +9,7 @@
 #include <ngx_event.h>
 
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 
 ssize_t ngx_unix_recv(ngx_connection_t *c, u_char *buf, size_t size)
 {
@@ -115,7 +115,7 @@
     return n;
 }
 
-#else /* ! NAVE_KQUEUE */
+#else /* ! NGX_HAVE_KQUEUE */
 
 ssize_t ngx_unix_recv(ngx_connection_t *c, u_char *buf, size_t size)
 {
@@ -170,4 +170,4 @@
     return n;
 }
 
-#endif /* NAVE_KQUEUE */
+#endif /* NGX_HAVE_KQUEUE */
diff --git a/src/os/unix/ngx_send.c b/src/os/unix/ngx_send.c
index ff501c3..ecd1362 100644
--- a/src/os/unix/ngx_send.c
+++ b/src/os/unix/ngx_send.c
@@ -17,7 +17,7 @@
 
     wev = c->write;
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 
     if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
         ngx_log_error(NGX_LOG_INFO, c->log, wev->kq_errno,
diff --git a/src/os/unix/ngx_shared.c b/src/os/unix/ngx_shared.c
index 0edc8cb..80b5f60 100644
--- a/src/os/unix/ngx_shared.c
+++ b/src/os/unix/ngx_shared.c
@@ -8,7 +8,7 @@
 #include <ngx_core.h>
 
 
-#if (HAVE_MAP_ANON)
+#if (NGX_HAVE_MAP_ANON)
 
 void *ngx_create_shared_memory(size_t size, ngx_log_t *log)
 {
@@ -25,7 +25,7 @@
     return p;
 }
 
-#elif (HAVE_MAP_DEVZERO)
+#elif (NGX_HAVE_MAP_DEVZERO)
 
 void *ngx_create_shared_memory(size_t size, ngx_log_t *log)
 {
@@ -56,7 +56,7 @@
     return p;
 }
 
-#elif (HAVE_SYSVSHM)
+#elif (NGX_HAVE_SYSVSHM)
 
 #include <sys/ipc.h>
 #include <sys/shm.h>
diff --git a/src/os/unix/ngx_socket.c b/src/os/unix/ngx_socket.c
index da936b2..523f1f4 100644
--- a/src/os/unix/ngx_socket.c
+++ b/src/os/unix/ngx_socket.c
@@ -20,7 +20,7 @@
  */
 
 
-#if (HAVE_FIONBIO)
+#if (NGX_HAVE_FIONBIO)
 
 int ngx_nonblocking(ngx_socket_t s)
 {
diff --git a/src/os/unix/ngx_socket.h b/src/os/unix/ngx_socket.h
index 4dcf924..b381967 100644
--- a/src/os/unix/ngx_socket.h
+++ b/src/os/unix/ngx_socket.h
@@ -19,7 +19,7 @@
 #define ngx_socket_n        "socket()"
 
 
-#if (HAVE_FIONBIO)
+#if (NGX_HAVE_FIONBIO)
 
 int ngx_nonblocking(ngx_socket_t s);
 int ngx_blocking(ngx_socket_t s);
diff --git a/src/os/unix/ngx_solaris_config.h b/src/os/unix/ngx_solaris_config.h
index 8f18417..452850c 100644
--- a/src/os/unix/ngx_solaris_config.h
+++ b/src/os/unix/ngx_solaris_config.h
@@ -49,35 +49,35 @@
 #include <ngx_auto_config.h>
 
 
-#if (HAVE_POLL)
+#if (NGX_HAVE_POLL)
 #include <poll.h>
 #endif
 
 
-#if (HAVE_SENDFILE)
+#if (NGX_HAVE_SENDFILE)
 #include <sys/sendfile.h>
 #endif
 
 
-#if (HAVE_AIO)
+#if (NGX_HAVE_AIO)
 #include <aio.h>
 #endif
 
 
-#if (HAVE_DEVPOLL)
+#if (NGX_HAVE_DEVPOLL)
 #include <sys/ioctl.h>
 #include <sys/devpoll.h>
 #endif
 
 
-#ifndef HAVE_INHERITED_NONBLOCK
-#define HAVE_INHERITED_NONBLOCK  1
+#ifndef NGX_HAVE_INHERITED_NONBLOCK
+#define NGX_HAVE_INHERITED_NONBLOCK  1
 #endif
 
 
-#ifndef HAVE_SO_SNDLOWAT
+#ifndef NGX_HAVE_SO_SNDLOWAT
 /* setsockopt(SO_SNDLOWAT) returns error "Option not supported by protocol" */
-#define HAVE_SO_SNDLOWAT         0
+#define NGX_HAVE_SO_SNDLOWAT         0
 #endif
 
 
diff --git a/src/os/unix/ngx_solaris_init.c b/src/os/unix/ngx_solaris_init.c
index c671197..03072ed 100644
--- a/src/os/unix/ngx_solaris_init.c
+++ b/src/os/unix/ngx_solaris_init.c
@@ -17,7 +17,7 @@
     ngx_unix_recv,
     ngx_readv_chain,
     ngx_unix_send,
-#if (HAVE_SENDFILE)
+#if (NGX_HAVE_SENDFILE)
     ngx_solaris_sendfilev_chain,
     NGX_IO_SENDFILE
 #else
diff --git a/src/os/unix/ngx_thread.h b/src/os/unix/ngx_thread.h
index ac7cfad..e630b35 100644
--- a/src/os/unix/ngx_thread.h
+++ b/src/os/unix/ngx_thread.h
@@ -53,7 +53,6 @@
 
 typedef struct {
     pthread_cond_t    cond;
-    ngx_tid_t         tid;
     ngx_log_t        *log;
 } ngx_cond_t;
 
diff --git a/src/os/unix/ngx_time.c b/src/os/unix/ngx_time.c
index c438950..20ec464 100644
--- a/src/os/unix/ngx_time.c
+++ b/src/os/unix/ngx_time.c
@@ -10,7 +10,7 @@
 
 void ngx_localtime(ngx_tm_t *tm)
 {
-#if (HAVE_LOCALTIME_R)
+#if (NGX_HAVE_LOCALTIME_R)
     time_t     now;
 
     now = ngx_time();
diff --git a/src/os/unix/ngx_time.h b/src/os/unix/ngx_time.h
index a6acf59..f257325 100644
--- a/src/os/unix/ngx_time.h
+++ b/src/os/unix/ngx_time.h
@@ -36,7 +36,7 @@
 #define ngx_tm_wday_t  int
 
 
-#if (HAVE_GMTOFF)
+#if (NGX_HAVE_GMTOFF)
 #define ngx_tm_gmtoff  tm_gmtoff
 #define ngx_tm_zone    tm_zone
 #endif
diff --git a/src/os/unix/ngx_writev_chain.c b/src/os/unix/ngx_writev_chain.c
index e389322..2af6469 100644
--- a/src/os/unix/ngx_writev_chain.c
+++ b/src/os/unix/ngx_writev_chain.c
@@ -30,7 +30,7 @@
         return in;
     }
 
-#if (HAVE_KQUEUE)
+#if (NGX_HAVE_KQUEUE)
 
     if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
         ngx_log_error(NGX_LOG_INFO, c->log, wev->kq_errno,
diff --git a/src/os/win32/ngx_os.h b/src/os/win32/ngx_os.h
index c27f8a8..6d720fa 100644
--- a/src/os/win32/ngx_os.h
+++ b/src/os/win32/ngx_os.h
@@ -15,18 +15,6 @@
 #define NGX_IO_SENDFILE    1
 #define NGX_IO_ZEROCOPY    2
 
-#if (HAVE_SENDFILE)
-#define NGX_HAVE_SENDFILE  NGX_IO_SENDFILE
-#else
-#define NGX_HAVE_SENDFILE  0
-#endif
-
-#if (HAVE_ZEROCOPY)
-#define NGX_HAVE_ZEROCOPY  NGX_IO_ZEROCOPY
-#else
-#define NGX_HAVE_ZEROCOPY  0
-#endif
-
 
 typedef ssize_t (*ngx_recv_pt)(ngx_connection_t *c, u_char *buf, size_t size);
 typedef ssize_t (*ngx_recv_chain_pt)(ngx_connection_t *c, ngx_chain_t *in);
diff --git a/src/os/win32/ngx_time.h b/src/os/win32/ngx_time.h
index 665976d..4cb9841 100644
--- a/src/os/win32/ngx_time.h
+++ b/src/os/win32/ngx_time.h
@@ -39,7 +39,7 @@
 
 #define ngx_msleep       Sleep
 
-#define HAVE_GETTIMEZONE  1
+#define NGX_HAVE_GETTIMEZONE  1
 
 ngx_int_t ngx_gettimezone(void);
 void ngx_gettimeofday(struct timeval *tp);
diff --git a/src/os/win32/ngx_win32_config.h b/src/os/win32/ngx_win32_config.h
index e7d3086..c7703c2 100644
--- a/src/os/win32/ngx_win32_config.h
+++ b/src/os/win32/ngx_win32_config.h
@@ -115,37 +115,31 @@
 
 
 #define TIME_T_LEN        sizeof("-2147483648") - 1
+#define OFF_T_MAX_VALUE   9223372036854775807
+#define NGX_HAVE_LITTLE_ENDIAN  1
+
+#define NGX_THREADS       1
 
 
 #define NGX_WIN_NT        200000
 
 
-#define NGX_THREADS       1
-
-
-#ifndef HAVE_INHERITED_NONBLOCK
-#define HAVE_INHERITED_NONBLOCK  1
+#ifndef NGX_HAVE_INHERITED_NONBLOCK
+#define NGX_HAVE_INHERITED_NONBLOCK  1
 #endif
 
-#ifndef HAVE_WIN32_TRANSMITPACKETS
-#define HAVE_WIN32_TRANSMITPACKETS  1
-#define HAVE_WIN32_TRANSMITFILE     0
+#ifndef NGX_HAVE_WIN32_TRANSMITPACKETS
+#define NGX_HAVE_WIN32_TRANSMITPACKETS  1
+#define NGX_HAVE_WIN32_TRANSMITFILE     0
 #endif
 
-#ifndef HAVE_WIN32_TRANSMITFILE
-#define HAVE_WIN32_TRANSMITFILE  1
+#ifndef NGX_HAVE_WIN32_TRANSMITFILE
+#define NGX_HAVE_WIN32_TRANSMITFILE  1
 #endif
 
-#if (HAVE_WIN32_TRANSMITPACKETS) || (HAVE_WIN32_TRANSMITFILE)
-#define HAVE_SENDFILE  1
+#if (NGX_HAVE_WIN32_TRANSMITPACKETS) || (NGX_HAVE_WIN32_TRANSMITFILE)
+#define NGX_HAVE_SENDFILE  1
 #endif
 
 
-#define OFF_T_MAX_VALUE  9223372036854775807
-
-
-/* STUB */
-#define HAVE_LITTLE_ENDIAN  1
-
-
 #endif /* _NGX_WIN32_CONFIG_H_INCLUDED_ */