changeset 46:9f3205d496a0 NGINX_0_1_23

nginx 0.1.23 *) Feature: the ngx_http_ssi_filter_module and the ssi, ssi_silent_errors, and ssi_min_file_chunk directives. The 'echo var="HTTP_..." default=""' and 'echo var="REMOTE_ADDR"' commands are supported. *) Feature: the %request_time log parameter. *) Feature: if the request has no the "Host" header line, then the "proxy_preserve_host" directive set this header line to the first server name of the "server_name" directive. *) Bugfix: nginx could not be built on platforms different from i386, amd64, sparc и ppc; bug appeared in 0.1.22. *) Bugfix: the ngx_http_autoindex_module now shows the information not about the symlink, but about file or directory it points to. *) Bugfix: the %apache_length parameter logged the negative length of the response header if the no response was transferred to a client.
author Igor Sysoev <http://sysoev.ru>
date Tue, 01 Mar 2005 00:00:00 +0300
parents 09c688b472a7
children 4ae32548452c
files CHANGES CHANGES.ru auto/cc/gcc auto/options src/core/nginx.h src/http/modules/ngx_http_autoindex_handler.c src/http/modules/ngx_http_gzip_filter.c src/http/modules/ngx_http_ssi_filter.c src/http/modules/proxy/ngx_http_proxy_upstream.c src/http/ngx_http_log_handler.c src/http/ngx_http_request.c src/http/ngx_http_request.h src/os/unix/ngx_atomic.h src/os/unix/ngx_files.h
diffstat 14 files changed, 1479 insertions(+), 178 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES
+++ b/CHANGES
@@ -1,16 +1,39 @@
+
+Changes with nginx 0.1.23                                        01 Mar 2005
+
+    *) Feature: the ngx_http_ssi_filter_module and the ssi, 
+       ssi_silent_errors, and ssi_min_file_chunk directives. The 'echo 
+       var="HTTP_..." default=""' and 'echo var="REMOTE_ADDR"' commands are 
+       supported.
+
+    *) Feature: the %request_time log parameter.
+
+    *) Feature: if the request has no the "Host" header line, then the 
+       proxy_preserve_host directive set this header line to the first 
+       server name of the server_name directive.
+
+    *) Bugfix: nginx could not be built on platforms different from i386, 
+       amd64, sparc и ppc; bug appeared in 0.1.22.
+
+    *) Bugfix: the ngx_http_autoindex_module now shows the information not 
+       about the symlink, but about file or directory it points to.
+
+    *) Bugfix: the %apache_length parameter logged the negative length of 
+       the response header if the no response was transferred to a client.
+
 
 Changes with nginx 0.1.22                                        22 Feb 2005
 
     *) Bugfix: the ngx_http_stub_status_module showed incorrect handled 
-       connections statictics if the proxing or FastCGI server were used.
+       connections statistics if the proxing or FastCGI server were used.
 
-    *) Bugfix: the installation pathes were incorrectly quoted on Linux and 
+    *) Bugfix: the installation paths were incorrectly quoted on Linux and 
        Solaris; bug appeared in 0.1.21.
 
 
 Changes with nginx 0.1.21                                        22 Feb 2005
 
-    *) Bugfix: the ngx_http_stub_status_module showed incorrect statictics 
+    *) Bugfix: the ngx_http_stub_status_module showed incorrect statistics 
        if "rtsig" method was used or if several worker process ran on SMP.
 
     *) Bugfix: nginx could not be built by the icc compiler on Linux or if 
@@ -21,10 +44,10 @@ Changes with nginx 0.1.21               
 
 Changes with nginx 0.1.20                                        17 Feb 2005
 
-    *) Feature: the new "script_filename" and "remote_port" parameters 
-       ofthe fastcgi_params directive.
+    *) Feature: the new "script_filename" and "remote_port" parameters of 
+       the fastcgi_params directive.
 
-    *) Bugfix: the FastCGI stderr stream was handled incorreсtly.
+    *) Bugfix: the FastCGI stderr stream was handled incorrectly.
 
 
 Changes with nginx 0.1.19                                        16 Feb 2005
@@ -123,11 +146,11 @@ Changes with nginx 0.1.15               
     *) Bugfix: the error while the connecting to the FastCGI server caused 
        segmentation fault.
 
-    *) Bugfix: the correct handling of the regular exporession, that has 
+    *) Bugfix: the correct handling of the regular expression, that has 
        different number of the captures and substitutions.
 
     *) Feature: the location, that is passed to the FastCGI server, can be 
-       regualar expression.
+       regular expression.
 
     *) Bugfix: the FastCGI's parameter REQUEST_URI is now passed with the 
        arguments and in the original state.
@@ -139,9 +162,9 @@ Changes with nginx 0.1.15               
        "Host" headers, if upstream listen on port 80; bug appeared in 
        0.1.14.
 
-    *) Bugfix: the same pathes in autoconfiguration paramters 
+    *) Bugfix: the same paths in autoconfiguration parameters 
        --http-client-body-temp-path=PATH and --http-proxy-temp-path=PATH, 
-       or --http-client-body-temp-path=PATH nad 
+       or --http-client-body-temp-path=PATH and 
        --http-fastcgi-temp-path=PATH caused segmentation fault.
 
 
@@ -231,7 +254,7 @@ Changes with nginx 0.1.11               
 
     *) Bugfix: "close() channel failed" errors.
 
-    *) Bugfix: the autodetection of the nobody and nogroup groups.
+    *) Bugfix: the autodetection of the "nobody" and "nogroup" groups.
 
     *) Bugfix: the send_lowat directive did not work on Linux.
 
@@ -287,7 +310,7 @@ Changes with nginx 0.1.7                
 
 Changes with nginx 0.1.6                                         11 Nov 2004
 
-    *) Bugfix: some location directive combiations with the regular 
+    *) Bugfix: some location directive combinations with the regular 
        expressions caused the wrong configuration choose.
 
 
--- a/CHANGES.ru
+++ b/CHANGES.ru
@@ -1,3 +1,26 @@
+
+Изменения в nginx 0.1.23                                          01.03.2005
+
+    *) Добавление: модуль ngx_http_ssi_filter_module и директивы ssi, 
+       ssi_silent_errors и ssi_min_file_chunk. Поддерживаются команды 'echo 
+       var="HTTP_..." default=""' и 'echo var="REMOTE_ADDR"'.
+
+    *) Добавление: параметр лога %request_time.
+
+    *) Добавление: если запрос пришёл без строки заголовка "Host", то 
+       директива proxy_preserve_host устанавливает в качестве этого 
+       заголовка первое имя сервера из директивы server_name.
+
+    *) Исправление: nginx не собирался на платформах, отличных от i386, 
+       amd64, sparc и ppc; ошибка появилась в 0.1.22.
+
+    *) Исправление: модуль ngx_http_autoindex_module теперь показывает 
+       информацию не о символическом линке, а о файле или каталоге, на 
+       который он указывает.
+
+    *) Исправление: если клиенту ничего не передавалось, то параметр 
+       %apache_length записывал в лог отрицательную длину заголовка ответа.
+
 
 Изменения в nginx 0.1.22                                          22.02.2005
 
--- a/auto/cc/gcc
+++ b/auto/cc/gcc
@@ -3,7 +3,7 @@
 
 
 # gcc 2.7.2.3, 2.8.1, 2.95.4,
-#     3.0.4, 3.1.1, 3.2.3, 3.3.2, 3.3.3, 3.3.4, 3.4, 3.4.2
+#     3.0.4, 3.1.1, 3.2.3, 3.3.2, 3.3.3, 3.3.4, 3.4.0, 3.4.2
 
 
 # Solaris 7's /usr/ccs/bin/as does not support "-pipe"
--- a/auto/options
+++ b/auto/options
@@ -48,7 +48,7 @@ NGX_HTTP_FASTCGI_TEMP_PATH=
 HTTP_CHARSET=YES
 HTTP_GZIP=YES
 HTTP_SSL=NO
-HTTP_SSI=NO
+HTTP_SSI=YES
 HTTP_ACCESS=YES
 HTTP_USERID=YES
 HTTP_AUTOINDEX=YES
--- a/src/core/nginx.h
+++ b/src/core/nginx.h
@@ -8,7 +8,7 @@
 #define _NGINX_H_INCLUDED_
 
 
-#define NGINX_VER          "nginx/0.1.22"
+#define NGINX_VER          "nginx/0.1.23"
 
 #define NGINX_VAR          "NGINX"
 #define NGX_NEWPID_EXT     ".newbin"
--- a/src/http/modules/ngx_http_autoindex_handler.c
+++ b/src/http/modules/ngx_http_autoindex_handler.c
@@ -241,7 +241,7 @@ static ngx_int_t ngx_http_autoindex_hand
 
         if (len == 2
             && ngx_de_name(&dir)[0] == '.'
-            && ngx_de_name(&dir)[0] == '.')
+            && ngx_de_name(&dir)[1] == '.')
         {
             continue;
         }
@@ -263,9 +263,20 @@ static ngx_int_t ngx_http_autoindex_hand
             ngx_cpystrn(last, ngx_de_name(&dir), len + 1);
 
             if (ngx_de_info(fname.data, &dir) == NGX_FILE_ERROR) {
-                ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
-                              ngx_de_info_n " \"%s\" failed", fname.data);
-                return ngx_http_autoindex_error(r, &dir, dname.data);
+                err = ngx_errno;
+
+                if (err != NGX_ENOENT) {
+                    ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
+                                  ngx_de_info_n " \"%s\" failed", fname.data);
+                    return ngx_http_autoindex_error(r, &dir, dname.data);
+                }
+
+                if (ngx_de_link_info(fname.data, &dir) == NGX_FILE_ERROR) {
+                    ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
+                                  ngx_de_link_info_n " \"%s\" failed",
+                                  fname.data);
+                    return ngx_http_autoindex_error(r, &dir, dname.data);
+                }
             }
         }
 
--- a/src/http/modules/ngx_http_gzip_filter.c
+++ b/src/http/modules/ngx_http_gzip_filter.c
@@ -76,23 +76,23 @@ typedef struct {
 
 
 static ngx_int_t ngx_http_gzip_proxied(ngx_http_request_t *r,
-                                       ngx_http_gzip_conf_t *conf);
+    ngx_http_gzip_conf_t *conf);
 static void *ngx_http_gzip_filter_alloc(void *opaque, u_int items,
-                                        u_int size);
+    u_int size);
 static void ngx_http_gzip_filter_free(void *opaque, void *address);
 static void ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx);
 
 static u_char *ngx_http_gzip_log_ratio(ngx_http_request_t *r, u_char *buf,
-                                       ngx_http_log_op_t *op);
+    ngx_http_log_op_t *op);
 
 static ngx_int_t ngx_http_gzip_add_log_formats(ngx_conf_t *cf);
 
 static ngx_int_t ngx_http_gzip_filter_init(ngx_cycle_t *cycle);
 static void *ngx_http_gzip_create_conf(ngx_conf_t *cf);
 static char *ngx_http_gzip_merge_conf(ngx_conf_t *cf,
-                                      void *parent, void *child);
+    void *parent, void *child);
 static char *ngx_http_gzip_set_types(ngx_conf_t *cf, ngx_command_t *cmd,
-                                     void *conf);
+    void *conf);
 static char *ngx_http_gzip_set_window(ngx_conf_t *cf, void *post, void *data);
 static char *ngx_http_gzip_set_hash(ngx_conf_t *cf, void *post, void *data);
 
@@ -261,7 +261,8 @@ static ngx_http_output_header_filter_pt 
 static ngx_http_output_body_filter_pt    ngx_http_next_body_filter;
 
 
-static ngx_int_t ngx_http_gzip_header_filter(ngx_http_request_t *r)
+static ngx_int_t
+ngx_http_gzip_header_filter(ngx_http_request_t *r)
 {
     ngx_uint_t             i, found;
     ngx_http_gzip_ctx_t   *ctx;
@@ -357,8 +358,8 @@ static ngx_int_t ngx_http_gzip_header_fi
 }
 
 
-static ngx_int_t ngx_http_gzip_proxied(ngx_http_request_t *r,
-                                       ngx_http_gzip_conf_t *conf)
+static ngx_int_t
+ngx_http_gzip_proxied(ngx_http_request_t *r, ngx_http_gzip_conf_t *conf)
 {
     time_t  date, expires;
 
@@ -437,8 +438,8 @@ static ngx_int_t ngx_http_gzip_proxied(n
 }
 
 
-static ngx_int_t ngx_http_gzip_body_filter(ngx_http_request_t *r,
-                                           ngx_chain_t *in)
+static ngx_int_t
+ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
 {
     int                    rc, wbits, memlevel;
     ngx_int_t              last;
@@ -825,7 +826,8 @@ static ngx_int_t ngx_http_gzip_body_filt
 }
 
 
-static void *ngx_http_gzip_filter_alloc(void *opaque, u_int items, u_int size)
+static void *
+ngx_http_gzip_filter_alloc(void *opaque, u_int items, u_int size)
 {
     ngx_http_gzip_ctx_t *ctx = opaque;
 
@@ -866,7 +868,8 @@ static void *ngx_http_gzip_filter_alloc(
 }
 
 
-static void ngx_http_gzip_filter_free(void *opaque, void *address)
+static void
+ngx_http_gzip_filter_free(void *opaque, void *address)
 {
 #if 0
     ngx_http_gzip_ctx_t *ctx = opaque;
@@ -877,8 +880,9 @@ static void ngx_http_gzip_filter_free(vo
 }
 
 
-static u_char *ngx_http_gzip_log_ratio(ngx_http_request_t *r, u_char *buf,
-                                       ngx_http_log_op_t *op)
+static u_char *
+ngx_http_gzip_log_ratio(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
 {
     ngx_uint_t            zint, zfrac;
     ngx_http_gzip_ctx_t  *ctx;
@@ -909,7 +913,8 @@ static u_char *ngx_http_gzip_log_ratio(n
 }
 
 
-static void ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx)
+static void
+ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx)
 {
     deflateEnd(&ctx->zstream);
 
@@ -926,7 +931,8 @@ static void ngx_http_gzip_error(ngx_http
 }
 
 
-static ngx_int_t ngx_http_gzip_add_log_formats(ngx_conf_t *cf)
+static ngx_int_t
+ngx_http_gzip_add_log_formats(ngx_conf_t *cf)
 {
     ngx_http_log_op_name_t  *op;
 
@@ -945,7 +951,8 @@ static ngx_int_t ngx_http_gzip_add_log_f
 }
 
 
-static ngx_int_t ngx_http_gzip_filter_init(ngx_cycle_t *cycle)
+static ngx_int_t
+ngx_http_gzip_filter_init(ngx_cycle_t *cycle)
 {
     ngx_http_next_header_filter = ngx_http_top_header_filter;
     ngx_http_top_header_filter = ngx_http_gzip_header_filter;
@@ -957,7 +964,8 @@ static ngx_int_t ngx_http_gzip_filter_in
 }
 
 
-static void *ngx_http_gzip_create_conf(ngx_conf_t *cf)
+static void *
+ngx_http_gzip_create_conf(ngx_conf_t *cf)
 {
     ngx_http_gzip_conf_t  *conf;
 
@@ -987,8 +995,8 @@ static void *ngx_http_gzip_create_conf(n
 }
 
 
-static char *ngx_http_gzip_merge_conf(ngx_conf_t *cf,
-                                      void *parent, void *child)
+static char *
+ngx_http_gzip_merge_conf(ngx_conf_t *cf, void *parent, void *child)
 {
     ngx_http_gzip_conf_t *prev = parent;
     ngx_http_gzip_conf_t *conf = child;
@@ -1000,10 +1008,9 @@ static char *ngx_http_gzip_merge_conf(ng
     ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 4, ngx_pagesize);
 
     ngx_conf_merge_unsigned_value(conf->http_version, prev->http_version,
-                                  NGX_HTTP_VERSION_11);
+                              NGX_HTTP_VERSION_11);
     ngx_conf_merge_bitmask_value(conf->proxied, prev->proxied,
-                                 (NGX_CONF_BITMASK_SET
-                                  |NGX_HTTP_GZIP_PROXIED_OFF));
+                              (NGX_CONF_BITMASK_SET|NGX_HTTP_GZIP_PROXIED_OFF));
 
     ngx_conf_merge_value(conf->level, prev->level, 1);
     ngx_conf_merge_size_value(conf->wbits, prev->wbits, MAX_WBITS);
@@ -1037,8 +1044,8 @@ static char *ngx_http_gzip_merge_conf(ng
 }
 
 
-static char *ngx_http_gzip_set_types(ngx_conf_t *cf, ngx_command_t *cmd,
-                                     void *conf)
+static char *
+ngx_http_gzip_set_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
     ngx_http_gzip_conf_t *gcf = conf;
 
@@ -1047,7 +1054,7 @@ static char *ngx_http_gzip_set_types(ngx
     ngx_http_gzip_type_t  *type;
 
     if (gcf->types == NULL) {
-        gcf->types = ngx_array_create(cf->pool, 5,
+        gcf->types = ngx_array_create(cf->pool, 4,
                                        sizeof(ngx_http_gzip_type_t));
         if (gcf->types == NULL) {
             return NGX_CONF_ERROR;
@@ -1087,7 +1094,8 @@ static char *ngx_http_gzip_set_types(ngx
 }
 
 
-static char *ngx_http_gzip_set_window(ngx_conf_t *cf, void *post, void *data)
+static char *
+ngx_http_gzip_set_window(ngx_conf_t *cf, void *post, void *data)
 {
     int *np = data;
 
@@ -1110,7 +1118,8 @@ static char *ngx_http_gzip_set_window(ng
 }
 
 
-static char *ngx_http_gzip_set_hash(ngx_conf_t *cf, void *post, void *data)
+static char *
+ngx_http_gzip_set_hash(ngx_conf_t *cf, void *post, void *data)
 {
     int *np = data;
 
new file mode 100644
--- /dev/null
+++ b/src/http/modules/ngx_http_ssi_filter.c
@@ -0,0 +1,1179 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_http.h>
+
+#define NGX_HTTP_SSI_MAX_PARAMS   16
+
+#define NGX_HTTP_SSI_COMMAND_LEN  31
+#define NGX_HTTP_SSI_PARAM_LEN    31
+#define NGX_HTTP_SSI_PARAMS_N     4
+
+#define NGX_HTTP_SSI_ERROR        1
+
+
+typedef struct {
+    ngx_flag_t        enable;
+    ngx_flag_t        silent_errors;
+
+    size_t            min_file_chunk;
+    size_t            value_len;
+} ngx_http_ssi_conf_t;
+
+
+typedef struct {
+    ngx_buf_t         *buf;
+
+    u_char            *pos;
+    u_char            *copy_start;
+    u_char            *copy_end;
+
+    ngx_str_t          command;
+    ngx_array_t        params;
+    ngx_table_elt_t   *param;
+    ngx_table_elt_t    params_array[NGX_HTTP_SSI_PARAMS_N];
+
+    ngx_chain_t       *in;
+    ngx_chain_t       *out;
+    ngx_chain_t      **last_out;
+
+    ngx_uint_t         state;
+    ngx_uint_t         saved_state;
+    size_t             saved;
+    size_t             looked;
+
+    size_t             value_len;
+} ngx_http_ssi_ctx_t;
+
+
+typedef ngx_int_t (*ngx_http_ssi_command_pt) (ngx_http_request_t *r,
+    ngx_http_ssi_ctx_t *ctx, ngx_str_t **);
+
+
+typedef struct {
+    ngx_str_t                 name;
+    ngx_uint_t                index;
+
+    ngx_uint_t                mandatory;
+} ngx_http_ssi_param_t;
+
+
+typedef struct {
+    ngx_str_t                 name;
+    ngx_http_ssi_command_pt   handler;
+    ngx_http_ssi_param_t     *params;
+
+    ngx_uint_t                flush;    /* unsigned  flush:1; */
+} ngx_http_ssi_command_t;
+
+
+typedef enum {
+    ssi_start_state = 0,
+    ssi_tag_state,
+    ssi_comment0_state,
+    ssi_comment1_state,
+    ssi_sharp_state,
+    ssi_precommand_state,
+    ssi_command_state,
+    ssi_preparam_state,
+    ssi_param_state,
+    ssi_preequal_state,
+    ssi_prevalue_state,
+    ssi_double_quoted_value_state,
+    ssi_quoted_value_state,
+    ssi_quoted_symbol_state,
+    ssi_postparam_state,
+    ssi_comment_end0_state,
+    ssi_comment_end1_state,
+    ssi_error_state,
+    ssi_error_end0_state,
+    ssi_error_end1_state
+} ngx_http_ssi_state_e;
+
+
+static ngx_int_t ngx_http_ssi_error(ngx_http_request_t *r,
+    ngx_http_ssi_ctx_t *ctx);
+static ngx_int_t ngx_http_ssi_parse(ngx_http_request_t *r,
+    ngx_http_ssi_ctx_t *ctx);
+
+static ngx_int_t ngx_http_ssi_echo(ngx_http_request_t *r,
+    ngx_http_ssi_ctx_t *ctx, ngx_str_t **params);
+
+static void *ngx_http_ssi_create_conf(ngx_conf_t *cf);
+static char *ngx_http_ssi_merge_conf(ngx_conf_t *cf,
+    void *parent, void *child);
+static ngx_int_t ngx_http_ssi_filter_init(ngx_cycle_t *cycle);
+
+
+static ngx_command_t  ngx_http_ssi_filter_commands[] = {
+
+    { ngx_string("ssi"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_ssi_conf_t, enable),
+      NULL },
+
+    { ngx_string("ssi_silent_errors"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_ssi_conf_t, silent_errors),
+      NULL },
+
+    { ngx_string("ssi_min_file_chunk"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_size_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_ssi_conf_t, min_file_chunk),
+      NULL },
+
+      ngx_null_command
+};
+
+
+    
+static ngx_http_module_t  ngx_http_ssi_filter_module_ctx = {
+    NULL,                                  /* pre conf */
+
+    NULL,                                  /* create main configuration */
+    NULL,                                  /* init main configuration */
+
+    NULL,                                  /* create server configuration */
+    NULL,                                  /* merge server configuration */
+
+    ngx_http_ssi_create_conf,              /* create location configuration */
+    ngx_http_ssi_merge_conf                /* merge location configuration */
+};  
+
+
+ngx_module_t  ngx_http_ssi_filter_module = {
+    NGX_MODULE,
+    &ngx_http_ssi_filter_module_ctx,       /* module context */
+    ngx_http_ssi_filter_commands,          /* module directives */
+    NGX_HTTP_MODULE,                       /* module type */
+    ngx_http_ssi_filter_init,              /* init module */
+    NULL                                   /* init child */
+};
+
+
+static ngx_int_t (*ngx_http_next_header_filter) (ngx_http_request_t *r);
+static ngx_int_t (*ngx_http_next_body_filter) (ngx_http_request_t *r,
+    ngx_chain_t *in);
+
+
+static u_char ngx_http_ssi_string[] = "<!--";
+static u_char ngx_http_ssi_error_string[] =
+                          "[an error occurred while processing the directive]";
+
+static ngx_str_t ngx_http_ssi_none = ngx_string("(none)");
+
+
+#define  NGX_HTTP_SSI_ECHO_VAR      0
+#define  NGX_HTTP_SSI_ECHO_DEFAULT  1
+
+static ngx_http_ssi_param_t  ngx_http_ssi_echo_params[] = {
+    { ngx_string("var"), NGX_HTTP_SSI_ECHO_VAR, 1 },
+    { ngx_string("default"), NGX_HTTP_SSI_ECHO_DEFAULT, 0 },
+    { ngx_null_string, 0, 0 }
+};
+
+
+static ngx_http_ssi_command_t  ngx_http_ssi_commands[] = {
+    { ngx_string("echo"), ngx_http_ssi_echo, ngx_http_ssi_echo_params, 0 },
+    { ngx_null_string, NULL, NULL, 0 }
+};
+
+
+static ngx_int_t
+ngx_http_ssi_header_filter(ngx_http_request_t *r)
+{
+    ngx_http_ssi_ctx_t   *ctx;
+    ngx_http_ssi_conf_t  *conf;
+
+    conf = ngx_http_get_module_loc_conf(r, ngx_http_ssi_filter_module);
+
+    if (!conf->enable) {
+        return ngx_http_next_header_filter(r);
+    }
+
+    /* TODO: "text/html" -> custom types */
+
+    if (r->headers_out.content_type
+        && ngx_strncasecmp(r->headers_out.content_type->value.data,
+                                                          "text/html", 5) != 0)
+    {
+        return ngx_http_next_header_filter(r);
+    }
+
+
+    if (!(ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_ssi_ctx_t)))) {
+        return NGX_ERROR;
+    }
+
+    ngx_http_set_ctx(r, ctx, ngx_http_ssi_filter_module);
+
+
+    ctx->value_len = conf->value_len;
+    ctx->last_out = &ctx->out;
+
+    ctx->params.elts = ctx->params_array;
+    ctx->params.size = sizeof(ngx_table_elt_t);
+    ctx->params.nalloc = NGX_HTTP_SSI_PARAMS_N;
+    ctx->params.pool = r->pool;
+
+    r->headers_out.content_length_n = -1;
+    if (r->headers_out.content_length) {
+        r->headers_out.content_length->key.len = 0;
+        r->headers_out.content_length = NULL;
+    }
+
+    r->headers_out.last_modified_time = -1;
+    if (r->headers_out.last_modified) {
+        r->headers_out.last_modified->key.len = 0;
+        r->headers_out.last_modified = NULL;
+    }
+
+    r->filter_need_in_memory = 1;
+    r->filter_ssi_need_in_memory = 1;
+
+    return ngx_http_next_header_filter(r);
+}
+
+
+static ngx_int_t
+ngx_http_ssi_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
+{
+    ngx_int_t                rc;
+    ngx_uint_t               i;
+    ngx_buf_t               *b;
+    ngx_chain_t             *cl;
+    ngx_table_elt_t         *param;
+    ngx_http_ssi_ctx_t      *ctx;
+    ngx_http_ssi_conf_t     *conf;
+    ngx_http_ssi_param_t    *prm;
+    ngx_http_ssi_command_t  *cmd;
+    ngx_str_t               *params[NGX_HTTP_SSI_MAX_PARAMS];
+
+    ctx = ngx_http_get_module_ctx(r, ngx_http_ssi_filter_module);
+
+    if (ctx == NULL || (in == NULL && ctx->in == NULL)) {
+        return ngx_http_next_body_filter(r, in);
+    }
+
+    /* add the incoming chain to the chain ctx->in */
+
+    if (in) {
+        if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) {
+            return NGX_ERROR;
+        }
+    }
+
+    conf = ngx_http_get_module_loc_conf(r, ngx_http_ssi_filter_module);
+
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http ssi filter");
+
+    b = NULL;
+
+    while (ctx->in) {
+
+        ctx->buf = ctx->in->buf;
+        ctx->in = ctx->in->next;
+        ctx->pos = ctx->buf->pos;
+
+        if (ctx->state == ssi_start_state) {
+            ctx->copy_start = ctx->pos;
+            ctx->copy_end = ctx->pos;
+        }
+
+        while (ctx->pos < ctx->buf->last) {
+
+            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                           "saved: %d state: %d", ctx->saved, ctx->state);
+
+            rc = ngx_http_ssi_parse(r, ctx);
+
+            ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                           "parse: %d, looked: %d %p-%p",
+                           rc, ctx->looked, ctx->copy_start, ctx->copy_end);
+
+            if (rc == NGX_ERROR) {
+                return rc;
+            }
+
+            if (ctx->copy_start != ctx->copy_end) {
+
+                ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                               "saved: %d", ctx->saved);
+
+                if (ctx->saved) {
+                    if (!(b = ngx_calloc_buf(r->pool))) {
+                        return NGX_ERROR;
+                    }
+
+                    b->memory = 1;
+                    b->pos = ngx_http_ssi_string;
+                    b->last = ngx_http_ssi_string + ctx->saved;
+
+                    if (!(cl = ngx_alloc_chain_link(r->pool))) {
+                        return NGX_ERROR;
+                    }
+
+                    cl->buf = b;
+                    *ctx->last_out = cl;
+                    ctx->last_out = &cl->next;
+
+                    ctx->saved = 0;
+                }
+
+                if (!(b = ngx_calloc_buf(r->pool))) {
+                    return NGX_ERROR;
+                }
+
+                ngx_memcpy(b, ctx->buf, sizeof(ngx_buf_t));
+
+                b->last_buf = 0;
+                b->pos = ctx->copy_start;
+                b->last = ctx->copy_end;
+
+                if (b->in_file) {
+
+                    if (conf->min_file_chunk < (size_t) (b->last - b->pos)) {
+                        b->file_last = b->file_pos + (b->last - b->start);
+                        b->file_pos += b->pos - b->start;
+
+                    } else {
+                        b->in_file = 0;
+                    }
+                }
+
+                if (!(cl = ngx_alloc_chain_link(r->pool))) {
+                    return NGX_ERROR;
+                }
+
+                cl->buf = b;
+                cl->next = NULL;
+                *ctx->last_out = cl;
+                ctx->last_out = &cl->next;
+            }
+
+            if (ctx->state == ssi_start_state) {
+                ctx->copy_start = ctx->pos;
+                ctx->copy_end = ctx->pos;
+
+            } else {
+                ctx->copy_start = NULL;
+                ctx->copy_end = NULL;
+            }
+
+            if (rc == NGX_AGAIN) {
+                continue;
+            }
+
+
+            if (rc == NGX_OK) {
+
+                for (cmd = ngx_http_ssi_commands; cmd->handler; cmd++) {
+                    if (cmd->name.len == 0) {
+                        cmd = (ngx_http_ssi_command_t *) cmd->handler;
+                    }
+
+                    if (cmd->name.len != ctx->command.len
+                        || ngx_strncmp(cmd->name.data, ctx->command.data,
+                                       ctx->command.len) != 0)
+                    {
+                        continue;
+                    }
+
+                    break;
+                }
+
+                if (cmd->name.len == 0) {
+                    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                                  "invalid SSI command: \"%V\"", &ctx->command);
+                    goto ssi_error;
+                }
+
+                ngx_memzero(params,
+                            NGX_HTTP_SSI_MAX_PARAMS * sizeof(ngx_str_t *));
+
+                param = ctx->params.elts;
+
+
+                for (i = 0; i < ctx->params.nelts; i++) {
+
+                    for (prm = cmd->params; prm->name.len; prm++) {
+
+                        if (param[i].key.len != prm->name.len
+                            || ngx_strncmp(param[i].key.data, prm->name.data,
+                                           prm->name.len) != 0)
+                        {
+                            continue;
+                        }
+
+                        if (params[prm->index]) {
+                            ngx_log_error(NGX_LOG_ERR,
+                                          r->connection->log, 0,
+                                          "duplicate \"%V\" parameter "
+                                          "in \"%V\" SSI command",
+                                          &param[i].key, &ctx->command);
+
+                            goto ssi_error;
+                        }
+
+                        params[prm->index] = &param[i].value;
+
+                        break;
+                    }
+
+                    if (prm->name.len == 0) {
+                        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                                      "invalid parameter name: \"%V\" "
+                                      "in \"%V\" SSI command",
+                                      &param[i].key, &ctx->command);
+
+                        goto ssi_error;
+                    }
+                }
+
+                for (prm = cmd->params; prm->name.len; prm++) {
+                    if (prm->mandatory && params[prm->index] == 0) {
+                        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                                      "mandatory \"%V\" parameter is absent "
+                                      "in \"%V\" SSI command",
+                                      &prm->name, &ctx->command);
+
+                        goto ssi_error;
+                    }
+                }
+
+                if (cmd->handler(r, ctx, params) == NGX_OK) {
+                    continue;
+                }
+            }
+
+
+            /* rc == NGX_HTTP_SSI_ERROR */
+
+ssi_error:
+
+            if (conf->silent_errors) {
+                continue;
+            }
+
+            if (!(b = ngx_calloc_buf(r->pool))) {
+                return NGX_ERROR;
+            }
+
+            b->memory = 1;
+            b->pos = ngx_http_ssi_error_string;
+            b->last = ngx_http_ssi_error_string
+                      + sizeof(ngx_http_ssi_error_string) - 1;
+
+            if (!(cl = ngx_alloc_chain_link(r->pool))) {
+                return NGX_ERROR;
+            }
+
+            cl->buf = b;
+            cl->next = NULL;
+            *ctx->last_out = cl;
+            ctx->last_out = &cl->next;
+
+            continue;
+        }
+
+        ctx->buf->pos = ctx->buf->last;
+
+        if (b && ctx->buf->last_buf) {
+            b->last_buf = 1;
+        }
+
+        ctx->saved = ctx->looked;
+    }
+
+    if (ctx->out == NULL) {
+        return NGX_OK;
+    }
+
+    rc = ngx_http_next_body_filter(r, ctx->out);
+
+    ctx->out = NULL;
+    ctx->last_out = &ctx->out;
+
+    return rc;
+}
+
+
+static ngx_int_t
+ngx_http_ssi_parse(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx)
+{
+    u_char                *p, *last, *copy_end, ch;
+    size_t                 looked;
+    ngx_http_ssi_state_e   state;
+
+    state = ctx->state;
+    looked = ctx->looked;
+    last = ctx->buf->last;
+    copy_end = ctx->copy_end;
+
+    for (p = ctx->pos; p < last; p++) {
+
+        ch = *p;
+
+        if (state == ssi_start_state) {
+
+            /* the tight loop */
+
+            for ( /* void */ ; p < last; ch = *(++p)) {
+                if (ch != '<') {
+                    continue;
+                }
+
+                copy_end = p;
+                looked = 1;
+                state = ssi_tag_state;
+
+                goto tag_started;
+            }
+
+            ctx->pos = p;
+            ctx->looked = looked;
+            ctx->copy_end = p;
+
+            if (ctx->copy_start == NULL) {
+                ctx->copy_start = ctx->buf->pos;
+            }
+
+            return NGX_AGAIN;
+
+tag_started:
+            continue;
+        }
+
+        switch (state) {
+
+        case ssi_start_state:
+            break;
+
+        case ssi_tag_state:
+            switch (ch) {
+            case '!':
+                looked = 2;
+                state = ssi_comment0_state;
+                break;
+
+            case '<':
+                copy_end = p;
+                break;
+
+            default:
+                copy_end = p;
+                looked = 0;
+                state = ssi_start_state;
+                break;
+            }
+
+            break;
+
+        case ssi_comment0_state:
+            switch (ch) {
+            case '-':
+                looked = 3;
+                state = ssi_comment1_state;
+                break;
+
+            case '<':
+                copy_end = p;
+                looked = 1;
+                state = ssi_tag_state;
+                break;
+
+            default:
+                copy_end = p;
+                looked = 0;
+                state = ssi_start_state;
+                break;
+            }
+
+            break;
+
+        case ssi_comment1_state:
+            switch (ch) {
+            case '-':
+                looked = 4;
+                state = ssi_sharp_state;
+                break;
+
+            case '<':
+                copy_end = p;
+                looked = 1;
+                state = ssi_tag_state;
+                break;
+
+            default:
+                copy_end = p;
+                looked = 0;
+                state = ssi_start_state;
+                break;
+            }
+
+            break;
+
+        case ssi_sharp_state:
+            switch (ch) {
+            case '#':
+                if (ctx->copy_start) {
+                    ctx->saved = 0;
+                }
+                looked = 0;
+                state = ssi_precommand_state;
+                break;
+
+            case '<':
+                copy_end = p;
+                looked = 1;
+                state = ssi_tag_state;
+                break;
+
+            default:
+                copy_end = p;
+                looked = 0;
+                state = ssi_start_state;
+                break;
+            }
+
+            break;
+
+        case ssi_precommand_state:
+            switch (ch) {
+            case ' ':
+            case CR:
+            case LF:
+            case '\t':
+                break;
+
+            default:
+                ctx->command.len = 1;
+                ctx->command.data = ngx_palloc(r->pool,
+                                               NGX_HTTP_SSI_COMMAND_LEN + 1);
+                if (ctx->command.data == NULL) {
+                    return NGX_ERROR;
+                }
+
+                ctx->command.data[0] = ch;
+                ctx->params.nelts = 0;
+                state = ssi_command_state;
+                break;
+            }
+
+            break;
+
+        case ssi_command_state:
+            switch (ch) {
+            case ' ':
+            case CR:
+            case LF:
+            case '\t':
+                state = ssi_preparam_state;
+                break;
+
+            case '-':
+                state = ssi_comment_end0_state;
+                break;
+
+            default:
+                ctx->command.data[ctx->command.len++] = ch;
+
+                if (ctx->command.len == NGX_HTTP_SSI_COMMAND_LEN) {
+                    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                                  "the \"%V\" SSI command is too long",
+                                  &ctx->command);
+
+                    state = ssi_error_state;
+                    break;
+                }
+            }
+
+            break;
+
+        case ssi_preparam_state:
+            switch (ch) {
+            case ' ':
+            case CR:
+            case LF:
+            case '\t':
+                break;
+
+            case '-':
+                state = ssi_comment_end0_state;
+                break;
+
+            default:
+                if (!(ctx->param = ngx_array_push(&ctx->params))) {
+                    return NGX_ERROR;
+                }
+
+                ctx->param->key.len = 1;
+                ctx->param->key.data = ngx_palloc(r->pool,
+                                                  NGX_HTTP_SSI_PARAM_LEN + 1);
+                if (ctx->param->key.data == NULL) {
+                    return NGX_ERROR;
+                }
+
+                ctx->param->key.data[0] = ch;
+
+                ctx->param->value.len = 0;
+                ctx->param->value.data = ngx_palloc(r->pool,
+                                                    ctx->value_len + 1);
+                if (ctx->param->value.data == NULL) {
+                    return NGX_ERROR;
+                }
+
+                state = ssi_param_state;
+                break;
+            }
+
+            break;
+
+        case ssi_param_state:
+            switch (ch) {
+            case ' ':
+            case CR:
+            case LF:
+            case '\t':
+                state = ssi_preequal_state;
+                break;
+
+            case '=':
+                state = ssi_prevalue_state;
+                break;
+
+            case '-':
+                state = ssi_error_end0_state;
+
+                ctx->param->key.data[ctx->param->key.len++] = ch;
+                ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                              "invalid \"%V\" parameter in \"%V\" SSI command",
+                              &ctx->param->key, &ctx->command);
+                break;
+
+            default:
+                ctx->param->key.data[ctx->param->key.len++] = ch;
+
+                if (ctx->param->key.len == NGX_HTTP_SSI_PARAM_LEN) {
+                    state = ssi_error_state;
+                    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                                  "too long \"%V\" parameter in "
+                                  "\"%V\" SSI command",
+                                  &ctx->param->key, &ctx->command);
+                    break;
+                }
+            }
+
+            break;
+
+        case ssi_preequal_state:
+            switch (ch) {
+            case ' ':
+            case CR:
+            case LF:
+            case '\t':
+                break;
+
+            case '=':
+                state = ssi_prevalue_state;
+                break;
+
+            default:
+                if (ch == '-') {
+                    state = ssi_error_end0_state;
+                } else {
+                    state = ssi_error_state;
+                }
+
+                ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                              "unexpected \"%c\" symbol after \"%V\" "
+                              "parameter in \"%V\" SSI command",
+                              ch, &ctx->param->key, &ctx->command);
+                break;
+            }
+
+            break;
+
+        case ssi_prevalue_state:
+            switch (ch) {
+            case ' ':
+            case CR:
+            case LF:
+            case '\t':
+                break;
+
+            case '"':
+                state = ssi_double_quoted_value_state;
+                break;
+
+            case '\'':
+                state = ssi_quoted_value_state;
+                break;
+
+            default:
+                if (ch == '-') {
+                    state = ssi_error_end0_state;
+                } else {
+                    state = ssi_error_state;
+                }
+
+                ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                              "unexpected \"%c\" symbol before value of "
+                              "\"%V\" parameter in \"%V\" SSI command",
+                              ch, &ctx->param->key, &ctx->command);
+                break;
+            }
+
+            break;
+
+        case ssi_double_quoted_value_state:
+            switch (ch) {
+            case '\\':
+                ctx->saved_state = ssi_double_quoted_value_state;
+                state = ssi_quoted_symbol_state;
+                break;
+
+            case '"':
+                state = ssi_postparam_state;
+                break;
+
+            default:
+                ctx->param->value.data[ctx->param->value.len++] = ch;
+
+                if (ctx->param->value.len == ctx->value_len) {
+                    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                                  "too long \"%V\" value of \"%V\" parameter "
+                                  "in \"%V\" SSI command",
+                                  &ctx->param->value, &ctx->param->key,
+                                  &ctx->command);
+                    state = ssi_error_state;
+                    break;
+                }
+            }
+
+            break;
+
+        case ssi_quoted_value_state:
+            switch (ch) {
+            case '\\':
+                ctx->saved_state = ssi_quoted_value_state;
+                state = ssi_quoted_symbol_state;
+                break;
+
+            case '\'':
+                state = ssi_postparam_state;
+                break;
+
+            default:
+                ctx->param->value.data[ctx->param->value.len++] = ch;
+
+                if (ctx->param->value.len == ctx->value_len) {
+                    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                                  "too long \"%V\" value of \"%V\" parameter "
+                                  "in \"%V\" SSI command",
+                                  &ctx->param->value, &ctx->param->key,
+                                  &ctx->command);
+                    state = ssi_error_state;
+                    break;
+                }
+            }
+
+            break;
+
+        case ssi_quoted_symbol_state:
+            ctx->param->value.data[ctx->param->value.len++] = ch;
+
+            if (ctx->param->value.len == ctx->value_len) {
+                if (ctx->param->value.len == ctx->value_len) {
+                    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                                  "too long \"%V\" value of \"%V\" parameter "
+                                  "in \"%V\" SSI command",
+                                  &ctx->param->value, &ctx->param->key,
+                                  &ctx->command);
+                    state = ssi_error_state;
+                    break;
+                }
+            }
+
+            state = ctx->saved_state;
+            break;
+
+        case ssi_postparam_state:
+            switch (ch) {
+            case ' ':
+            case CR:
+            case LF:
+            case '\t':
+                state = ssi_preparam_state;
+                break;
+
+            case '-':
+                state = ssi_comment_end0_state;
+                break;
+
+            default:
+                ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                              "unexpected \"%c\" symbol after \"%V\" value "
+                              "of \"%V\" parameter in \"%V\" SSI command",
+                              ch, &ctx->param->value, &ctx->param->key,
+                              &ctx->command);
+                state = ssi_error_state;
+                break;
+            }
+
+            break;
+
+        case ssi_comment_end0_state:
+            switch (ch) {
+            case '-':
+                state = ssi_comment_end1_state;
+                break;
+
+            default:
+                ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                              "unexpected \"%c\" symbol in \"%V\" SSI command",
+                              ch, &ctx->command);
+                state = ssi_error_state;
+                break;
+            }
+
+            break;
+
+        case ssi_comment_end1_state:
+            switch (ch) {
+            case '>':
+                ctx->state = ssi_start_state;
+                ctx->pos = p + 1;
+                ctx->looked = looked;
+                ctx->copy_end = copy_end;
+
+                if (ctx->copy_start == NULL && copy_end) {
+                    ctx->copy_start = ctx->buf->pos;
+                }
+
+                return NGX_OK;
+
+            default:
+                ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                              "unexpected \"%c\" symbol in \"%V\" SSI command",
+                              ch, &ctx->command);
+                state = ssi_error_state;
+                break;
+            }
+
+            break;
+
+        case ssi_error_state:
+            switch (ch) {
+            case '-':
+                state = ssi_error_end0_state;
+                break;
+
+            default:
+                break;
+            }
+
+            break;
+
+        case ssi_error_end0_state:
+            switch (ch) {
+            case '-':
+                state = ssi_error_end1_state;
+                break;
+
+            default:
+                state = ssi_error_state;
+                break;
+            }
+
+            break;
+
+        case ssi_error_end1_state:
+            switch (ch) {
+            case '>':
+                ctx->state = ssi_start_state;
+                ctx->pos = p + 1;
+                ctx->looked = looked;
+                ctx->copy_end = copy_end;
+
+                if (ctx->copy_start == NULL && copy_end) {
+                    ctx->copy_start = ctx->buf->pos;
+                }
+
+                return NGX_HTTP_SSI_ERROR;
+
+            default:
+                state = ssi_error_state;
+                break;
+            }
+
+            break;
+        }
+    }
+
+    ctx->state = state;
+    ctx->pos = p;
+    ctx->looked = looked;
+
+    ctx->copy_end = (state == ssi_start_state) ? p : copy_end;
+
+    if (ctx->copy_start == NULL && ctx->copy_end) {
+        ctx->copy_start = ctx->buf->pos;
+    }
+
+    return NGX_AGAIN;
+}
+
+
+static ngx_int_t
+ngx_http_ssi_echo(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx,
+    ngx_str_t **params)
+{
+    u_char            ch;
+    ngx_uint_t        i, n;
+    ngx_buf_t        *b;
+    ngx_str_t        *var, *value;
+    ngx_chain_t      *cl;
+    ngx_list_part_t  *part;
+    ngx_table_elt_t  *header;
+
+    var = params[NGX_HTTP_SSI_ECHO_VAR];
+    value = NULL;
+
+    if (var->len > 5 && ngx_strncmp(var->data, "HTTP_", 5) == 0) {
+
+        part = &r->headers_in.headers.part;
+        header = part->elts;
+
+        for (i = 0; /* void */ ; i++) {
+
+            if (i >= part->nelts) {
+                if (part->next == NULL) {
+                    break;
+                }
+
+                part = part->next;
+                header = part->elts;
+                i = 0; 
+            }
+
+            for (n = 0; n + 5 < var->len && n < header[i].key.len; n++)
+            {
+                ch = header[i].key.data[n];
+
+                if (ch >= 'a' && ch <= 'z') {
+                    ch &= ~0x20;
+
+                } else if (ch == '-') {
+                    ch = '_';
+                }
+
+                if (var->data[n + 5] != ch) {
+                    break;
+                }
+            }
+
+            if (n + 5 == var->len) {
+                value = &header[i].value;
+                break;
+            }
+        }
+
+    } else if (var->len == sizeof("REMOTE_ADDR") - 1
+               && ngx_strncmp(var->data, "REMOTE_ADDR",
+                              sizeof("REMOTE_ADDR") - 1) == 0)
+    {
+        value = &r->connection->addr_text;
+    }
+
+
+    if (value == NULL) {
+        value = params[NGX_HTTP_SSI_ECHO_DEFAULT];
+    }
+
+    if (value == NULL) {
+        value = &ngx_http_ssi_none;
+
+    } else if (value->len == 0) {
+        return NGX_OK;
+    }
+
+    if (!(b = ngx_calloc_buf(r->pool))) {
+        return NGX_HTTP_SSI_ERROR;
+    }
+
+    if (!(cl = ngx_alloc_chain_link(r->pool))) {
+        return NGX_HTTP_SSI_ERROR;
+    }
+
+    b->memory = 1;
+    b->pos = value->data;
+    b->last = value->data + value->len;
+
+    cl->buf = b;
+    cl->next = NULL;
+    *ctx->last_out = cl;
+    ctx->last_out = &cl->next;
+
+    return NGX_OK;
+}
+
+
+static void *
+ngx_http_ssi_create_conf(ngx_conf_t *cf)
+{
+    ngx_http_ssi_conf_t  *conf;
+
+    if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ssi_conf_t)))) {
+        return NGX_CONF_ERROR;
+    }
+
+    conf->enable = NGX_CONF_UNSET;
+    conf->silent_errors = NGX_CONF_UNSET;
+
+    conf->min_file_chunk = NGX_CONF_UNSET_SIZE;
+    conf->value_len = NGX_CONF_UNSET_SIZE;
+
+    return conf;
+}
+
+
+static char *
+ngx_http_ssi_merge_conf(ngx_conf_t *cf, void *parent, void *child)
+{
+    ngx_http_ssi_conf_t *prev = parent;
+    ngx_http_ssi_conf_t *conf = child;
+
+    ngx_conf_merge_value(conf->enable, prev->enable, 0);
+    ngx_conf_merge_value(conf->silent_errors, prev->silent_errors, 0);
+
+    ngx_conf_merge_size_value(conf->min_file_chunk, prev->min_file_chunk, 1024);
+    ngx_conf_merge_size_value(conf->value_len, prev->value_len, 256);
+
+    return NGX_CONF_OK;
+}
+
+
+static ngx_int_t
+ngx_http_ssi_filter_init(ngx_cycle_t *cycle)
+{
+    ngx_http_next_header_filter = ngx_http_top_header_filter;
+    ngx_http_top_header_filter = ngx_http_ssi_header_filter;
+
+    ngx_http_next_body_filter = ngx_http_top_body_filter;
+    ngx_http_top_body_filter = ngx_http_ssi_body_filter;
+
+    return NGX_OK;
+}
--- a/src/http/modules/proxy/ngx_http_proxy_upstream.c
+++ b/src/http/modules/proxy/ngx_http_proxy_upstream.c
@@ -145,10 +145,18 @@ static ngx_chain_t *ngx_http_proxy_creat
     }
 
 
-    if (p->lcf->preserve_host && r->headers_in.host) {
-        len += sizeof(host_header) - 1
-            + r->headers_in.host_name_len + sizeof(":") - 1 + uc->port_text.len
-            + sizeof(CRLF) - 1;
+    if (p->lcf->preserve_host) {
+        if (r->headers_in.host) {
+            len += sizeof(host_header) - 1
+                + r->headers_in.host_name_len + sizeof(":") - 1
+                + uc->port_text.len + sizeof(CRLF) - 1;
+
+        } else {
+            len += sizeof(host_header) - 1
+                + r->server_name.len + sizeof(":") - 1
+                + uc->port_text.len + sizeof(CRLF) - 1;
+        }
+
     } else {
         len += sizeof(host_header) - 1 + uc->host_header.len
             + sizeof(CRLF) - 1;
@@ -278,9 +286,14 @@ static ngx_chain_t *ngx_http_proxy_creat
 
     b->last = ngx_cpymem(b->last, host_header, sizeof(host_header) - 1);
 
-    if (p->lcf->preserve_host && r->headers_in.host) {
-        b->last = ngx_cpymem(b->last, r->headers_in.host->value.data,
-                             r->headers_in.host_name_len);
+    if (p->lcf->preserve_host) {
+        if (r->headers_in.host) {
+            b->last = ngx_cpymem(b->last, r->headers_in.host->value.data,
+                                 r->headers_in.host_name_len);
+        } else {
+            b->last = ngx_cpymem(b->last, r->server_name.data,
+                                 r->server_name.len);
+        }
 
         if (!uc->default_port) {
             *b->last++ = ':';
--- a/src/http/ngx_http_log_handler.c
+++ b/src/http/ngx_http_log_handler.c
@@ -11,93 +11,91 @@
 
 
 static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf,
-                                 ngx_http_log_op_t *op);
+    ngx_http_log_op_t *op);
 static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
-                                       ngx_http_log_op_t *op);
+    ngx_http_log_op_t *op);
 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
-                                 ngx_http_log_op_t *op);
+    ngx_http_log_op_t *op);
 static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
-                                 ngx_http_log_op_t *op);
+    ngx_http_log_op_t *op);
 static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
-                                 ngx_http_log_op_t *op);
+    ngx_http_log_op_t *op);
+static u_char *ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op);
 static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
-                                   ngx_http_log_op_t *op);
+    ngx_http_log_op_t *op);
 static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
-                                   ngx_http_log_op_t *op);
+    ngx_http_log_op_t *op);
 static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
-                                          ngx_http_log_op_t *op);
+    ngx_http_log_op_t *op);
 static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
-                                           ngx_http_log_op_t *op);
+    ngx_http_log_op_t *op);
 
 static size_t ngx_http_log_request_getlen(ngx_http_request_t *r,
-                                          uintptr_t data);
+    uintptr_t data);
 static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
-                                    ngx_http_log_op_t *op);
+    ngx_http_log_op_t *op);
 
 static ngx_int_t ngx_http_log_header_in_compile(ngx_http_log_op_t *op,
-                                                ngx_str_t *value);
+    ngx_str_t *value);
 static size_t ngx_http_log_header_in_getlen(ngx_http_request_t *r,
-                                            uintptr_t data);
+    uintptr_t data);
 static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
-                                      ngx_http_log_op_t *op);
+    ngx_http_log_op_t *op);
 static size_t ngx_http_log_unknown_header_in_getlen(ngx_http_request_t *r,
-                                                    uintptr_t data);
+    uintptr_t data);
 static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
-                                              u_char *buf,
-                                              ngx_http_log_op_t *op);
+    u_char *buf, ngx_http_log_op_t *op);
 
 static ngx_int_t ngx_http_log_header_out_compile(ngx_http_log_op_t *op,
-                                                 ngx_str_t *value);
+    ngx_str_t *value);
 static size_t ngx_http_log_header_out_getlen(ngx_http_request_t *r,
-                                             uintptr_t data);
+    uintptr_t data);
 static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
-                                       ngx_http_log_op_t *op);
+    ngx_http_log_op_t *op);
 static size_t ngx_http_log_unknown_header_out_getlen(ngx_http_request_t *r,
-                                                     uintptr_t data);
+    uintptr_t data);
 static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r,
-                                               u_char *buf,
-                                               ngx_http_log_op_t *op);
+    u_char *buf, ngx_http_log_op_t *op);
 
 static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
-                                                  u_char *buf,
-                                                  ngx_http_log_op_t *op);
+    u_char *buf, ngx_http_log_op_t *op);
 static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
-                                                         u_char *buf,
-                                                         ngx_http_log_op_t *op);
+    u_char *buf, ngx_http_log_op_t *op);
 
 static ngx_table_elt_t *ngx_http_log_unknown_header(ngx_list_t *headers,
-                                                    ngx_str_t *value);
+    ngx_str_t *value);
 
 static ngx_int_t ngx_http_log_set_formats(ngx_conf_t *cf);
 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
-                                         void *child);
+    void *child);
 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
-                                  void *conf);
+    void *conf);
 static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
-                                     void *conf);
+    void *conf);
 static ngx_int_t ngx_http_log_parse_format(ngx_conf_t *cf, ngx_array_t *ops,
-                                           ngx_str_t *line);
+    ngx_str_t *line);
 
 
 static ngx_command_t  ngx_http_log_commands[] = {
 
-    {ngx_string("log_format"),
-     NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
-     ngx_http_log_set_format,
-     NGX_HTTP_MAIN_CONF_OFFSET,
-     0,
-     NULL},
+    { ngx_string("log_format"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
+      ngx_http_log_set_format,
+      NGX_HTTP_MAIN_CONF_OFFSET,
+      0,
+      NULL },
 
-    {ngx_string("access_log"),
-     NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
-     ngx_http_log_set_log,
-     NGX_HTTP_LOC_CONF_OFFSET,
-     0,
-     NULL},
+    { ngx_string("access_log"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
+      ngx_http_log_set_log,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      0,
+      NULL },
 
-    ngx_null_command
+      ngx_null_command
 };
 
 
@@ -141,6 +139,8 @@ ngx_http_log_op_name_t ngx_http_log_fmt_
     { ngx_string("time"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
                           NULL, NULL, ngx_http_log_time },
     { ngx_string("msec"), NGX_TIME_T_LEN + 4, NULL, NULL, ngx_http_log_msec },
+    { ngx_string("request_time"), NGX_TIME_T_LEN, NULL, NULL,
+                          ngx_http_log_request_time },
     { ngx_string("status"), 3, NULL, NULL, ngx_http_log_status },
     { ngx_string("length"), NGX_OFF_T_LEN, NULL, NULL, ngx_http_log_length },
     { ngx_string("apache_length"), NGX_OFF_T_LEN,
@@ -164,7 +164,8 @@ ngx_http_log_op_name_t ngx_http_log_fmt_
 };
 
 
-ngx_int_t ngx_http_log_handler(ngx_http_request_t *r)
+ngx_int_t
+ngx_http_log_handler(ngx_http_request_t *r)
 {
     ngx_uint_t                i, l;
     u_char                   *line, *p;
@@ -228,8 +229,9 @@ ngx_int_t ngx_http_log_handler(ngx_http_
 }
 
 
-static u_char *ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
-                                       ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
 {
     size_t     len;
     uintptr_t  data;
@@ -246,30 +248,32 @@ static u_char *ngx_http_log_copy_short(n
 }
 
 
-static u_char *ngx_http_log_copy_long(ngx_http_request_t *r, u_char *buf,
-                                      ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_copy_long(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
 {
     return ngx_cpymem(buf, (u_char *) op->data, op->len);
 }
 
 
-static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf,
-                                 ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_addr(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
 {
     return ngx_cpymem(buf, r->connection->addr_text.data,
                       r->connection->addr_text.len);
 }
 
 
-static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
-                                       ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
 {
     return ngx_sprintf(buf, "%ui", r->connection->number);
 }
 
 
-static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
-                                 ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
 {
     if (r->pipeline) {
         *buf = 'p';
@@ -281,16 +285,16 @@ static u_char *ngx_http_log_pipe(ngx_htt
 }
 
 
-static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
-                                 ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_time(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
 {
     return ngx_cpymem(buf, ngx_cached_http_log_time.data,
                       ngx_cached_http_log_time.len);
 }
 
 
-static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
-                                 ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
 {
     struct timeval  tv;
 
@@ -300,51 +304,75 @@ static u_char *ngx_http_log_msec(ngx_htt
 }
 
 
-static size_t ngx_http_log_request_getlen(ngx_http_request_t *r,
-                                          uintptr_t data)
+static u_char *
+ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
+{
+    time_t  elapsed;
+
+    elapsed = ngx_time() - r->start_time;
+
+    return ngx_sprintf(buf, "%T", elapsed);
+}
+
+
+static size_t
+ngx_http_log_request_getlen(ngx_http_request_t *r, uintptr_t data)
 {
     return r->request_line.len;
 }
 
 
-static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
-                                    ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_request(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
 {
     return ngx_cpymem(buf, r->request_line.data, r->request_line.len);
 }
 
 
-static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
-                                   ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_status(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
 {
     return ngx_sprintf(buf, "%ui",
                        r->err_status ? r->err_status : r->headers_out.status);
 }
 
 
-static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf,
-                                   ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_length(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
 {
     return ngx_sprintf(buf, "%O", r->connection->sent);
 }
 
 
-static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
-                                          ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
 {
-    return ngx_sprintf(buf, "%O", r->connection->sent - r->header_size);
+    off_t  length;
+
+    length = r->connection->sent - r->header_size;
+
+    if (length > 0) {
+        return ngx_sprintf(buf, "%O", length);
+    }
+
+    *buf = '0';
+
+    return buf + 1;
 }
 
 
-static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
-                                           ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
 {
     return ngx_sprintf(buf, "%z", r->request_length);
 }
 
 
-static ngx_int_t ngx_http_log_header_in_compile(ngx_http_log_op_t *op,
-                                                ngx_str_t *value)
+static ngx_int_t
+ngx_http_log_header_in_compile(ngx_http_log_op_t *op, ngx_str_t *value)
 {
     ngx_uint_t  i;
 
@@ -375,8 +403,8 @@ static ngx_int_t ngx_http_log_header_in_
 }
 
 
-static size_t ngx_http_log_header_in_getlen(ngx_http_request_t *r,
-                                            uintptr_t data)
+static size_t
+ngx_http_log_header_in_getlen(ngx_http_request_t *r, uintptr_t data)
 {
     ngx_table_elt_t  *h;
 
@@ -390,8 +418,9 @@ static size_t ngx_http_log_header_in_get
 }
 
 
-static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
-                                      ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
 {
     ngx_table_elt_t  *h;
 
@@ -407,8 +436,8 @@ static u_char *ngx_http_log_header_in(ng
 }
 
 
-static size_t ngx_http_log_unknown_header_in_getlen(ngx_http_request_t *r,
-                                                    uintptr_t data)
+static size_t
+ngx_http_log_unknown_header_in_getlen(ngx_http_request_t *r, uintptr_t data)
 {
     ngx_table_elt_t  *h;
 
@@ -422,9 +451,9 @@ static size_t ngx_http_log_unknown_heade
 }
 
 
-static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
-                                              u_char *buf,
-                                              ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_unknown_header_in(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
 {
     ngx_table_elt_t  *h;
 
@@ -441,8 +470,8 @@ static u_char *ngx_http_log_unknown_head
 }
 
 
-static ngx_int_t ngx_http_log_header_out_compile(ngx_http_log_op_t *op,
-                                                 ngx_str_t *value)
+static ngx_int_t
+ngx_http_log_header_out_compile(ngx_http_log_op_t *op, ngx_str_t *value)
 {
     ngx_uint_t  i;
 
@@ -455,7 +484,7 @@ static ngx_int_t ngx_http_log_header_out
         }
 
         if (ngx_strncasecmp(ngx_http_headers_out[i].name.data, value->data,
-                                                              value->len) == 0)
+                            value->len) == 0)
         {
             op->getlen = ngx_http_log_header_out_getlen;
             op->run = ngx_http_log_header_out;
@@ -493,8 +522,8 @@ static ngx_int_t ngx_http_log_header_out
 }
 
 
-static size_t ngx_http_log_header_out_getlen(ngx_http_request_t *r,
-                                             uintptr_t data)
+static size_t
+ngx_http_log_header_out_getlen(ngx_http_request_t *r, uintptr_t data)
 {
     ngx_table_elt_t  *h;
 
@@ -540,8 +569,9 @@ static size_t ngx_http_log_header_out_ge
 }
 
 
-static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
-                                       ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
 {
     ngx_table_elt_t  *h;
 
@@ -594,8 +624,8 @@ static u_char *ngx_http_log_header_out(n
 }
 
 
-static size_t ngx_http_log_unknown_header_out_getlen(ngx_http_request_t *r,
-                                                     uintptr_t data)
+static size_t
+ngx_http_log_unknown_header_out_getlen(ngx_http_request_t *r, uintptr_t data)
 {
     ngx_table_elt_t  *h;
 
@@ -610,9 +640,9 @@ static size_t ngx_http_log_unknown_heade
 }
 
 
-static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r,
-                                               u_char *buf,
-                                               ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_unknown_header_out(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
 {
     ngx_table_elt_t  *h;
 
@@ -629,8 +659,8 @@ static u_char *ngx_http_log_unknown_head
 }
 
 
-static ngx_table_elt_t *ngx_http_log_unknown_header(ngx_list_t *headers,
-                                                    ngx_str_t *value)
+static ngx_table_elt_t *
+ngx_http_log_unknown_header(ngx_list_t *headers, ngx_str_t *value)
 {
     ngx_uint_t        i;
     ngx_list_part_t  *part;
@@ -664,9 +694,9 @@ static ngx_table_elt_t *ngx_http_log_unk
 }
 
 
-static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
-                                                  u_char *buf,
-                                                  ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_connection_header_out(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
 {
     if (r->keepalive) {
         return ngx_cpymem(buf, "keep-alive", sizeof("keep-alive") - 1);
@@ -677,9 +707,9 @@ static u_char *ngx_http_log_connection_h
 }
 
 
-static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
-                                                         u_char *buf,
-                                                         ngx_http_log_op_t *op)
+static u_char *
+ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r, u_char *buf,
+    ngx_http_log_op_t *op)
 {
     if (r->chunked) {
         return ngx_cpymem(buf, "chunked", sizeof("chunked") - 1);
@@ -691,7 +721,8 @@ static u_char *ngx_http_log_transfer_enc
 }
 
 
-static ngx_int_t ngx_http_log_set_formats(ngx_conf_t *cf)
+static ngx_int_t
+ngx_http_log_set_formats(ngx_conf_t *cf)
 {
     ngx_http_log_op_name_t  *op;
 
@@ -702,7 +733,8 @@ static ngx_int_t ngx_http_log_set_format
 }
 
 
-static void *ngx_http_log_create_main_conf(ngx_conf_t *cf)
+static void *
+ngx_http_log_create_main_conf(ngx_conf_t *cf)
 {
     ngx_http_log_main_conf_t  *conf;
 
@@ -713,23 +745,26 @@ static void *ngx_http_log_create_main_co
         return NGX_CONF_ERROR;
     }
 
-    ngx_init_array(conf->formats, cf->pool, 5, sizeof(ngx_http_log_fmt_t),
-                  NGX_CONF_ERROR);
+    if (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))
+                                                                  == NGX_ERROR)
+    {
+        return NGX_CONF_ERROR;
+    }
 
     cf->args->nelts = 0;
 
-    if (!(value = ngx_push_array(cf->args))) {
+    if (!(value = ngx_array_push(cf->args))) {
         return NGX_CONF_ERROR;
     }
 
-    if (!(value = ngx_push_array(cf->args))) {
+    if (!(value = ngx_array_push(cf->args))) {
         return NGX_CONF_ERROR;
     }
 
     value->len = sizeof("combined") - 1;
     value->data = (u_char *) "combined";
 
-    if (!(value = ngx_push_array(cf->args))) {
+    if (!(value = ngx_array_push(cf->args))) {
         return NGX_CONF_ERROR;
     }
 
@@ -744,7 +779,8 @@ static void *ngx_http_log_create_main_co
 }
 
 
-static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf)
+static void *
+ngx_http_log_create_loc_conf(ngx_conf_t *cf)
 {
     ngx_http_log_loc_conf_t  *conf;
 
@@ -756,8 +792,8 @@ static void *ngx_http_log_create_loc_con
 }
 
 
-static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
-                                         void *child)
+static char *
+ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
 {
     ngx_http_log_loc_conf_t *prev = parent;
     ngx_http_log_loc_conf_t *conf = child;
@@ -808,8 +844,8 @@ static char *ngx_http_log_merge_loc_conf
 }
 
 
-static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
-                                  void *conf)
+static char *
+ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
     ngx_http_log_loc_conf_t *llcf = conf;
 
@@ -867,8 +903,8 @@ static char *ngx_http_log_set_log(ngx_co
 }
 
 
-static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
-                                     void *conf)
+static char *
+ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
     ngx_http_log_main_conf_t *lmcf = conf;
 
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -422,6 +422,8 @@ static void ngx_http_init_request(ngx_ev
     c->single_connection = 1;
     r->connection = c;
 
+    r->start_time = ngx_time();
+
     r->file.fd = NGX_INVALID_FILE;
 
     r->headers_in.content_length_n = -1;
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -275,7 +275,8 @@ struct ngx_http_request_s {
 
     ngx_http_request_body_t  *request_body;
 
-    time_t               lingering_time;
+    time_t                    lingering_time;
+    time_t                    start_time;
 
     ngx_uint_t           method;
     ngx_uint_t           http_version;
--- a/src/os/unix/ngx_atomic.h
+++ b/src/os/unix/ngx_atomic.h
@@ -388,6 +388,7 @@ ngx_atomic_cmp_set(ngx_atomic_t *lock, n
 typedef int32_t  ngx_atomic_int_t;
 typedef uint32_t  ngx_atomic_uint_t;
 typedef volatile ngx_atomic_uint_t  ngx_atomic_t;
+#define NGX_ATOMIC_T_LEN  sizeof("-2147483648") - 1
 
 #define ngx_atomic_inc(x)  ++(*(x))
 #define ngx_atomic_dec(x)  --(*(x))
--- a/src/os/unix/ngx_files.h
+++ b/src/os/unix/ngx_files.h
@@ -18,7 +18,7 @@
 
 
 #define ngx_open_file(name, access, create)                                 \
-                                 open((const char *) name, access|create, 0644)
+    open((const char *) name, access|create, 0644)
 #define ngx_open_file_n          "open()"
 
 #define NGX_FILE_RDONLY          O_RDONLY
@@ -46,10 +46,10 @@ ssize_t ngx_read_file(ngx_file_t *file, 
 
 
 ssize_t ngx_write_file(ngx_file_t *file, u_char *buf, size_t size,
-                       off_t offset);
+    off_t offset);
 
 ssize_t ngx_write_chain_to_file(ngx_file_t *file, ngx_chain_t *ce,
-                                off_t offset, ngx_pool_t *pool);
+    off_t offset, ngx_pool_t *pool);
 
 
 #define ngx_rename_file          rename
@@ -86,7 +86,7 @@ ngx_int_t ngx_open_dir(ngx_str_t *name, 
 
 
 #define ngx_read_dir(d)                                                      \
-                           (((d)->de = readdir((d)->dir)) ? NGX_OK : NGX_ERROR)
+    (((d)->de = readdir((d)->dir)) ? NGX_OK : NGX_ERROR)
 #define ngx_read_dir_n           "readdir()"
 
 
@@ -104,10 +104,13 @@ ngx_int_t ngx_open_dir(ngx_str_t *name, 
 #else
 #define ngx_de_namelen(dir)      ngx_strlen((dir)->de->d_name)
 #endif
-#define ngx_de_info(name, dir)   lstat((const char *) name, &(dir)->info)
+#define ngx_de_info(name, dir)   stat((const char *) name, &(dir)->info)
 #define ngx_de_info_n            "stat()"
+#define ngx_de_link_info(name, dir)  lstat((const char *) name, &(dir)->info)
+#define ngx_de_link_info_n       "lstat()"
 #define ngx_de_is_dir(dir)       (S_ISDIR((dir)->info.st_mode))
 #define ngx_de_is_file(dir)      (S_ISREG((dir)->info.st_mode))
+#define ngx_de_is_link(dir)      (S_ISLNK((dir)->info.st_mode))
 #define ngx_de_size(dir)         (dir)->info.st_size
 #define ngx_de_mtime(dir)        (dir)->info.st_mtime