nginx-0.3.10-RELEASE import

    *) Change: the "valid_referers" directive and the "$invalid_referer"
       variable were moved to the new ngx_http_referer_module from the
       ngx_http_rewrite_module.

    *) Change: the "$apache_bytes_sent" variable name was changed to
       "$body_bytes_sent".

    *) Feature: the "$sent_http_..." variables.

    *) Feature: the "if" directive supports the "=" and "!=" operations.

    *) Feature: the "proxy_pass" directive supports the HTTPS protocol.

    *) Feature: the "proxy_set_body" directive.

    *) Feature: the "post_action" directive.

    *) Feature: the ngx_http_empty_gif_module.

    *) Feature: the "worker_cpu_affinity" directive for Linux.

    *) Bugfix: the "rewrite" directive did not unescape URI part in
       redirect, now it is unescaped except the %00-%25 and %7F-%FF
       characters.

    *) Bugfix: nginx could not be built by the icc 9.0 compiler.

    *) Bugfix: if the SSI was enabled for zero size static file, then the
       chunked response was encoded incorrectly.
diff --git a/src/http/modules/ngx_http_autoindex_module.c b/src/http/modules/ngx_http_autoindex_module.c
index 3623e2a..8a04807 100644
--- a/src/http/modules/ngx_http_autoindex_module.c
+++ b/src/http/modules/ngx_http_autoindex_module.c
@@ -98,7 +98,7 @@
 
 ngx_module_t  ngx_http_autoindex_module = {
     NGX_MODULE_V1,
-    &ngx_http_autoindex_module_ctx,        /* module context */ 
+    &ngx_http_autoindex_module_ctx,        /* module context */
     ngx_http_autoindex_commands,           /* module directives */
     NGX_HTTP_MODULE,                       /* module type */
     NULL,                                  /* init master */
@@ -248,7 +248,7 @@
                 return ngx_http_autoindex_error(r, &dir, &path);
             }
 
-            break; 
+            break;
         }
 
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -302,7 +302,7 @@
             return ngx_http_autoindex_error(r, &dir, &path);
         }
 
-        entry->name.len = len;        
+        entry->name.len = len;
 
         entry->name.data = ngx_palloc(pool, len + 1);
         if (entry->name.data == NULL) {
@@ -446,7 +446,7 @@
                                      sizeof("                  -") - 1);
             } else {
                 b->last = ngx_sprintf(b->last, "%19O", entry[i].size);
-            } 
+            }
 
         } else {
             if (entry[i].dir) {
@@ -460,7 +460,7 @@
                     size = (ngx_int_t) (length / (1024 * 1024 * 1024));
                     if ((length % (1024 * 1024 * 1024))
                                                 > (1024 * 1024 * 1024 / 2 - 1))
-                    { 
+                    {
                         size++;
                     }
                     scale = 'G';
diff --git a/src/http/modules/ngx_http_empty_gif_module.c b/src/http/modules/ngx_http_empty_gif_module.c
new file mode 100644
index 0000000..5c9a415
--- /dev/null
+++ b/src/http/modules/ngx_http_empty_gif_module.c
@@ -0,0 +1,173 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ */
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_http.h>
+
+
+static char *ngx_http_empty_gif(ngx_conf_t *cf, ngx_command_t *cmd,
+    void *conf);
+
+static ngx_command_t  ngx_http_empty_gif_commands[] = {
+
+    { ngx_string("empty_gif"),
+      NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
+      ngx_http_empty_gif,
+      0,
+      0,
+      NULL },
+
+      ngx_null_command
+};
+
+
+/* the minimal single pixel transparent GIF, 43 bytes */
+
+static u_char  ngx_empty_gif[] = {
+
+    'G', 'I', 'F', '8', '9', 'a',  /* header */
+
+                                   /* logical screen descriptor */
+    0x01, 0x00,                    /* logical screen width */
+    0x01, 0x00,                    /* logical screen height */
+    0x80,                          /* global 1-bit color table */
+    0x01,                          /* background color #1 */
+    0x00,                          /* no aspect ratio */
+
+                                   /* global color table */
+    0x00, 0x00, 0x00,              /* #0: black */
+    0xff, 0xff, 0xff,              /* #1: white */
+
+                                   /* graphic control extension */
+    0x21,                          /* extension introducer */
+    0xf9,                          /* graphic control label */
+    0x04,                          /* block size */
+    0x01,                          /* transparent color is given, */
+                                   /*     no disposal specified, */
+                                   /*     user input is not expected */
+    0x00, 0x00,                    /* delay time */
+    0x01,                          /* transparent color #1 */
+    0x00,                          /* block terminator */
+
+                                   /* image descriptor */
+    0x2c,                          /* image separator */
+    0x00, 0x00,                    /* image left position */
+    0x00, 0x00,                    /* image top position */
+    0x01, 0x00,                    /* image width */
+    0x01, 0x00,                    /* image height */
+    0x00,                          /* no local color table, no interlaced */
+
+                                   /* table based image data */
+    0x02,                          /* LZW minimum code size, */
+                                   /*     must be at least 2-bit */
+    0x02,                          /* block size */
+    0x4c, 0x01,                    /* compressed bytes 01_001_100, 0000000_1 */
+                                   /* 100: clear code */
+                                   /* 001: 1 */
+                                   /* 101: end of information code */
+    0x00,                          /* block terminator */
+
+    0x3B                           /* trailer */
+};
+
+
+ngx_http_module_t  ngx_http_empty_gif_module_ctx = {
+    NULL,                          /* preconfiguration */
+    NULL,                          /* postconfiguration */
+
+    NULL,                          /* create main configuration */
+    NULL,                          /* init main configuration */
+
+    NULL,                          /* create server configuration */
+    NULL,                          /* merge server configuration */
+
+    NULL,                          /* create location configuration */
+    NULL                           /* merge location configuration */
+};
+
+
+ngx_module_t  ngx_http_empty_gif_module = {
+    NGX_MODULE_V1,
+    &ngx_http_empty_gif_module_ctx, /* module context */
+    ngx_http_empty_gif_commands,   /* module directives */
+    NGX_HTTP_MODULE,               /* module type */
+    NULL,                          /* init master */
+    NULL,                          /* init module */
+    NULL,                          /* init process */
+    NULL,                          /* init thread */
+    NULL,                          /* exit thread */
+    NULL,                          /* exit process */
+    NULL,                          /* exit master */
+    NGX_MODULE_V1_PADDING
+};
+
+
+static ngx_int_t
+ngx_http_empty_gif_handler(ngx_http_request_t *r)
+{
+    ngx_int_t     rc;
+    ngx_buf_t    *b;
+    ngx_chain_t   out;
+
+    if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
+        return NGX_HTTP_NOT_ALLOWED;
+    }
+
+    rc = ngx_http_discard_body(r);
+
+    if (rc != NGX_OK && rc != NGX_AGAIN) {
+        return rc;
+    }
+
+    r->headers_out.content_type.len = sizeof("image/gif") - 1;
+    r->headers_out.content_type.data = (u_char *) "image/gif";
+
+    if (r->method == NGX_HTTP_HEAD) {
+        r->headers_out.status = NGX_HTTP_OK;
+
+        rc = ngx_http_send_header(r);
+
+        if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
+            return rc;
+        }
+    }
+
+    b = ngx_create_temp_buf(r->pool, sizeof(ngx_empty_gif));
+    if (b == NULL) {
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
+    }
+
+    out.buf = b;
+    out.next = NULL;
+
+    b->pos = ngx_empty_gif;
+    b->last = ngx_empty_gif + sizeof(ngx_empty_gif);
+    b->last_buf = 1;
+
+    r->headers_out.status = NGX_HTTP_OK;
+    r->headers_out.content_length_n = b->last - b->pos;
+    r->headers_out.last_modified_time = 23349600;
+
+    rc = ngx_http_send_header(r);
+
+    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
+        return rc;
+    }
+
+    return ngx_http_output_filter(r, &out);;
+}
+
+
+static char *
+ngx_http_empty_gif(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+    ngx_http_core_loc_conf_t  *clcf;
+
+    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
+    clcf->handler = ngx_http_empty_gif_handler;
+
+    return NGX_CONF_OK;
+}
diff --git a/src/http/modules/ngx_http_fastcgi_module.c b/src/http/modules/ngx_http_fastcgi_module.c
index 160aacb..5d2c153 100644
--- a/src/http/modules/ngx_http_fastcgi_module.c
+++ b/src/http/modules/ngx_http_fastcgi_module.c
@@ -556,9 +556,9 @@
 
             if (i >= part->nelts) {
                 if (part->next == NULL) {
-                    break; 
+                    break;
                 }
-    
+
                 part = part->next;
                 header = part->elts;
                 i = 0;
@@ -570,7 +570,7 @@
                 *b->last++ = (u_char) ((len >> 16) & 0xff);
                 *b->last++ = (u_char) ((len >> 8) & 0xff);
                 *b->last++ = (u_char) (len & 0xff);
-    
+
             } else {
                 *b->last++ = (u_char) len;
             }
@@ -876,7 +876,7 @@
                     f->length = 0;
                     f->state = ngx_http_fastcgi_st_padding;
 
-                } else { 
+                } else {
                     line.len = u->header_in.last - u->header_in.pos;
                     f->length -= u->header_in.last - u->header_in.pos;
                     u->header_in.pos = u->header_in.last;
@@ -1145,7 +1145,7 @@
                     f->length = 0;
                     f->state = ngx_http_fastcgi_st_padding;
 
-                } else { 
+                } else {
                     line.len = f->last - f->pos;
                     f->length -= f->last - f->pos;
                     f->pos = f->last;
@@ -1447,7 +1447,7 @@
     conf->upstream.header_buffer_size = NGX_CONF_UNSET_SIZE;
 
     conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
-    conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE; 
+    conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
     conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
 
     conf->upstream.max_fails = NGX_CONF_UNSET_UINT;
@@ -1497,7 +1497,7 @@
     ngx_conf_merge_size_value(conf->upstream.send_lowat,
                               prev->upstream.send_lowat, 0);
 
-    ngx_conf_merge_size_value(conf->upstream.header_buffer_size, 
+    ngx_conf_merge_size_value(conf->upstream.header_buffer_size,
                               prev->upstream.header_buffer_size,
                               (size_t) ngx_pagesize);
 
@@ -1693,7 +1693,7 @@
             if (copy == NULL) {
                 return NGX_CONF_ERROR;
             }
-    
+
             copy->code = ngx_http_script_copy_code;
             copy->len = src[i].key.len + src[i].value.len;
 
@@ -1722,7 +1722,7 @@
             if (copy == NULL) {
                 return NGX_CONF_ERROR;
             }
-    
+
             copy->code = ngx_http_script_copy_code;
             copy->len = src[i].key.len;
 
@@ -1777,7 +1777,7 @@
     u_char                       *p;
     ngx_http_fastcgi_loc_conf_t  *flcf;
 
-    v->valid = 1; 
+    v->valid = 1;
     v->no_cachable = 0;
     v->not_found = 0;
 
@@ -1850,7 +1850,7 @@
 
         inet_upstream.name = value[1];
         inet_upstream.url = value[1];
-    
+
         lcf->peers = ngx_inet_upstream_parse(cf, &inet_upstream);
         if (lcf->peers == NULL) {
             return NGX_CONF_ERROR;
diff --git a/src/http/modules/ngx_http_geo_module.c b/src/http/modules/ngx_http_geo_module.c
index a3638e4..8231de1 100644
--- a/src/http/modules/ngx_http_geo_module.c
+++ b/src/http/modules/ngx_http_geo_module.c
@@ -64,10 +64,6 @@
 };
 
 
-static ngx_http_variable_value_t  ngx_http_geo_null_value =
-    ngx_http_variable("");
-
-
 /* AF_INET only */
 
 static ngx_int_t
@@ -171,7 +167,8 @@
     }
 
     if (ngx_radix32tree_insert(tree, 0, 0,
-                            (uintptr_t) &ngx_http_geo_null_value) == NGX_ERROR)
+                               (uintptr_t) &ngx_http_variable_null_value)
+        == NGX_ERROR)
     {
         return NGX_CONF_ERROR;
     }
diff --git a/src/http/modules/ngx_http_gzip_filter_module.c b/src/http/modules/ngx_http_gzip_filter_module.c
index adf7005..66d5935 100644
--- a/src/http/modules/ngx_http_gzip_filter_module.c
+++ b/src/http/modules/ngx_http_gzip_filter_module.c
@@ -301,7 +301,7 @@
     type = conf->types->elts;
     for (i = 0; i < conf->types->nelts; i++) {
         if (r->headers_out.content_type.len >= type[i].len
-            && ngx_strncasecmp(r->headers_out.content_type.data, 
+            && ngx_strncasecmp(r->headers_out.content_type.data,
                                type[i].data, type[i].len) == 0)
         {
             goto found;
@@ -358,15 +358,12 @@
     r->headers_out.content_encoding->value.data = (u_char *) "gzip";
 
     ctx->length = r->headers_out.content_length_n;
-    r->headers_out.content_length_n = -1;
-
-    if (r->headers_out.content_length) {
-        r->headers_out.content_length->hash = 0;
-        r->headers_out.content_length = NULL;
-    }
 
     r->main_filter_need_in_memory = 1;
 
+    ngx_http_clear_content_length(r);
+    ngx_http_clear_accept_ranges(r);
+
     return ngx_http_next_header_filter(r);
 }
 
@@ -993,7 +990,7 @@
     ngx_uint_t            zint, zfrac;
     ngx_http_gzip_ctx_t  *ctx;
 
-    v->valid = 1; 
+    v->valid = 1;
     v->no_cachable = 0;
     v->not_found = 0;
 
@@ -1096,7 +1093,7 @@
     ngx_conf_merge_size_value(conf->wbits, prev->wbits, MAX_WBITS);
     ngx_conf_merge_size_value(conf->memlevel, prev->memlevel,
                               MAX_MEM_LEVEL - 1);
-    ngx_conf_merge_value(conf->min_length, prev->min_length, 0);
+    ngx_conf_merge_value(conf->min_length, prev->min_length, 20);
     ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0);
 
     if (conf->types == NULL) {
diff --git a/src/http/modules/ngx_http_headers_filter_module.c b/src/http/modules/ngx_http_headers_filter_module.c
index da9061b..86c1a80 100644
--- a/src/http/modules/ngx_http_headers_filter_module.c
+++ b/src/http/modules/ngx_http_headers_filter_module.c
@@ -262,7 +262,7 @@
 
 static void *
 ngx_http_headers_create_conf(ngx_conf_t *cf)
-{   
+{
     ngx_http_headers_conf_t  *conf;
 
     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_headers_conf_t));
@@ -350,7 +350,7 @@
     if (hcf->expires == NGX_ERROR) {
         return "invalid value";
     }
-    
+
     if (hcf->expires == NGX_PARSE_LARGE_TIME) {
         return "value must be less than 68 years";
     }
diff --git a/src/http/modules/ngx_http_index_module.c b/src/http/modules/ngx_http_index_module.c
index 4847cb7..e6e45a1 100644
--- a/src/http/modules/ngx_http_index_module.c
+++ b/src/http/modules/ngx_http_index_module.c
@@ -356,7 +356,7 @@
     if (err == NGX_EACCES) {
         ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
                       "\"%s\" is forbidden", ctx->path.data);
-    
+
         return NGX_HTTP_FORBIDDEN;
     }
 
diff --git a/src/http/modules/ngx_http_log_module.c b/src/http/modules/ngx_http_log_module.c
index e832070..3bcf73a 100644
--- a/src/http/modules/ngx_http_log_module.c
+++ b/src/http/modules/ngx_http_log_module.c
@@ -55,7 +55,7 @@
     ngx_http_log_op_t *op);
 static u_char *ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
     ngx_http_log_op_t *op);
-static u_char *ngx_http_log_apache_bytes_sent(ngx_http_request_t *r,
+static u_char *ngx_http_log_body_bytes_sent(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);
@@ -173,7 +173,7 @@
 
 static ngx_str_t  ngx_http_combined_fmt =
     ngx_string("$remote_addr - $remote_user [$time_gmt] "
-               "\"$request\" $status $apache_bytes_sent "
+               "\"$request\" $status $body_bytes_sent "
                "\"$http_referer\" \"$http_user_agent\"");
 
 
@@ -186,8 +186,10 @@
     { ngx_string("request_time"), NGX_TIME_T_LEN, ngx_http_log_request_time },
     { ngx_string("status"), 3, ngx_http_log_status },
     { ngx_string("bytes_sent"), NGX_OFF_T_LEN, ngx_http_log_bytes_sent },
+    { ngx_string("body_bytes_sent"), NGX_OFF_T_LEN,
+                          ngx_http_log_body_bytes_sent },
     { ngx_string("apache_bytes_sent"), NGX_OFF_T_LEN,
-                          ngx_http_log_apache_bytes_sent },
+                          ngx_http_log_body_bytes_sent },
     { ngx_string("request_length"), NGX_SIZE_T_LEN,
                           ngx_http_log_request_length },
 
@@ -209,7 +211,7 @@
     { ngx_string("length"), NGX_OFF_T_LEN,
                           NULL, NULL, ngx_http_log_bytes_sent },
     { ngx_string("apache_length"), NGX_OFF_T_LEN,
-                          NULL, NULL, ngx_http_log_apache_bytes_sent },
+                          NULL, NULL, ngx_http_log_body_bytes_sent },
     { ngx_string("request_length"), NGX_SIZE_T_LEN,
                           NULL, NULL, ngx_http_log_request_length },
 
@@ -429,7 +431,7 @@
 
 
 static u_char *
-ngx_http_log_apache_bytes_sent(ngx_http_request_t *r, u_char *buf,
+ngx_http_log_body_bytes_sent(ngx_http_request_t *r, u_char *buf,
     ngx_http_log_op_t *op)
 {
     off_t  length;
@@ -811,7 +813,7 @@
 }
 
 
-static ngx_int_t 
+static ngx_int_t
 ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
     ngx_str_t *value)
 {
@@ -1220,7 +1222,7 @@
                         if (arg_len == 0) {
                             fname[fname_len] = '\0';
                             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                                               "\"%s\" requires argument", 
+                                               "\"%s\" requires argument",
                                                data);
                             return NGX_CONF_ERROR;
                         }
@@ -1298,6 +1300,12 @@
                     goto invalid;
                 }
 
+                if (ngx_strncmp(var.data, "apache_bytes_sent", 17) == 0) {
+                    ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
+                        "use \"$body_bytes_sent\" instead of "
+                        "\"$apache_bytes_sent\"");
+                }
+
                 for (v = ngx_http_log_vars; v->name.len; v++) {
 
                     if (v->name.len == var.len
diff --git a/src/http/modules/ngx_http_not_modified_filter_module.c b/src/http/modules/ngx_http_not_modified_filter_module.c
index 1e2db62..4a6bb5c 100644
--- a/src/http/modules/ngx_http_not_modified_filter_module.c
+++ b/src/http/modules/ngx_http_not_modified_filter_module.c
@@ -61,7 +61,7 @@
 
     ims = ngx_http_parse_time(r->headers_in.if_modified_since->value.data,
                               r->headers_in.if_modified_since->value.len);
-    
+
     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                    "http ims:%d lm:%d", ims, r->headers_out.last_modified_time);
 
@@ -72,11 +72,8 @@
     if (ims != NGX_ERROR && ims == r->headers_out.last_modified_time) {
         r->headers_out.status = NGX_HTTP_NOT_MODIFIED;
         r->headers_out.content_type.len = 0;
-        r->headers_out.content_length_n = -1;
-        r->headers_out.content_length = NULL;
-#if 0
-        r->headers_out.accept_ranges->hash = 0;
-#endif
+        ngx_http_clear_content_length(r);
+        ngx_http_clear_accept_ranges(r);
     }
 
     return ngx_http_next_header_filter(r);
diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c
index 26f0831..5712ea0 100644
--- a/src/http/modules/ngx_http_proxy_module.c
+++ b/src/http/modules/ngx_http_proxy_module.c
@@ -1,5 +1,5 @@
 
-/*  
+/*
  * Copyright (C) Igor Sysoev
  */
 
@@ -38,6 +38,8 @@
     ngx_peers_t                 *peers;
 
     ngx_array_t                 *flushes;
+    ngx_array_t                 *body_set_len;
+    ngx_array_t                 *body_set;
     ngx_array_t                 *headers_set_len;
     ngx_array_t                 *headers_set;
     ngx_hash_t                  *headers_set_hash;
@@ -47,6 +49,8 @@
 
     ngx_array_t                 *redirects;
 
+    ngx_str_t                    body_source;
+
     ngx_str_t                    method;
     ngx_str_t                    host_header;
     ngx_str_t                    port_text;
@@ -60,6 +64,8 @@
     ngx_uint_t                   status_count;
     u_char                      *status_start;
     u_char                      *status_end;
+
+    size_t                       internal_body_length;
 } ngx_http_proxy_ctx_t;
 
 
@@ -83,6 +89,9 @@
 static ngx_int_t
     ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t
+    ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r,
     ngx_table_elt_t *h, size_t prefix);
 
@@ -164,6 +173,13 @@
       offsetof(ngx_http_proxy_loc_conf_t, headers_source),
       NULL },
 
+    { ngx_string("proxy_set_body"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_str_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_proxy_loc_conf_t, body_source),
+      NULL },
+
     { ngx_string("proxy_method"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
       ngx_conf_set_str_slot,
@@ -336,24 +352,27 @@
     { ngx_string("proxy_add_via"), NULL, 0, 0, 0 },
 #endif
 
+    { ngx_string("proxy_internal_body_length"),
+      ngx_http_proxy_internal_body_length_variable, 0, 0, 0 },
+
     { ngx_null_string, NULL, 0, 0, 0 }
 };
 
 
 static ngx_int_t
 ngx_http_proxy_handler(ngx_http_request_t *r)
-{   
+{
     ngx_int_t                   rc;
     ngx_http_upstream_t        *u;
     ngx_http_proxy_loc_conf_t  *plcf;
-    
+
     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
-    
+
     u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t));
     if (u == NULL) {
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
-    
+
     u->peer.log = r->connection->log;
     u->peer.log_error = NGX_ERROR_ERR;
     u->peer.peers = plcf->peers;
@@ -379,7 +398,7 @@
     u->pipe.input_filter = ngx_event_pipe_copy_input_filter;
 
     u->accel = 1;
-    
+
     r->upstream = u;
 
     rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
@@ -395,7 +414,7 @@
 static ngx_int_t
 ngx_http_proxy_create_request(ngx_http_request_t *r)
 {
-    size_t                        len, loc_len;
+    size_t                        len, loc_len, body_len;
     ngx_uint_t                    i, key;
     uintptr_t                     escape;
     ngx_buf_t                    *b;
@@ -404,6 +423,7 @@
     ngx_list_part_t              *part;
     ngx_table_elt_t              *header;
     ngx_http_upstream_t          *u;
+    ngx_http_proxy_ctx_t         *p;
     ngx_http_script_code_pt       code;
     ngx_http_script_engine_t      e, le;
     ngx_http_proxy_loc_conf_t    *plcf;
@@ -413,6 +433,13 @@
 
     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
 
+    p = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t));
+    if (p == NULL) {
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
+    }
+
+    ngx_http_set_ctx(r, p, ngx_http_proxy_module);
+
     len = sizeof(ngx_http_proxy_version) - 1 + sizeof(CRLF) - 1;
 
     if (u->method.len) {
@@ -446,10 +473,23 @@
         len += r->uri.len - loc_len + escape + sizeof("?") - 1 + r->args.len;
     }
 
-    ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
-
     ngx_http_script_flush_no_cachable_variables(r, plcf->flushes);
 
+    if (plcf->body_set_len) {
+        le.ip = plcf->body_set_len->elts;
+        le.request = r;
+        le.flushed = 1;
+        body_len = 0;
+
+        while (*(uintptr_t *) le.ip) {
+            lcode = *(ngx_http_script_len_code_pt *) le.ip;
+            body_len += lcode(&le);
+        }
+
+        p->internal_body_length = body_len;
+        len += body_len;
+    }
+
     le.ip = plcf->headers_set_len->elts;
     le.request = r;
     le.flushed = 1;
@@ -478,7 +518,7 @@
 
                 part = part->next;
                 header = part->elts;
-                i = 0; 
+                i = 0;
             }
 
             key = header[i].hash % plcf->headers_set_hash->hash_size;
@@ -527,7 +567,7 @@
                            r->uri.len - loc_len, NGX_ESCAPE_URI);
             b->last += r->uri.len - loc_len + escape;
 
-        } else { 
+        } else {
             b->last = ngx_copy(b->last, r->uri.data + loc_len,
                                r->uri.len - loc_len);
         }
@@ -543,6 +583,7 @@
     b->last = ngx_cpymem(b->last, ngx_http_proxy_version,
                          sizeof(ngx_http_proxy_version) - 1);
 
+    ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
 
     e.ip = plcf->headers_set->elts;
     e.pos = b->last;
@@ -574,7 +615,7 @@
         while (*(uintptr_t *) e.ip) {
             code = *(ngx_http_script_code_pt *) e.ip;
             code((ngx_http_script_engine_t *) &e);
-        } 
+        }
         e.ip += sizeof(uintptr_t);
     }
 
@@ -584,7 +625,7 @@
     if (plcf->upstream.pass_request_headers) {
         part = &r->headers_in.headers.part;
         header = part->elts;
-    
+
         for (i = 0; /* void */; i++) {
 
             if (i >= part->nelts) {
@@ -594,7 +635,7 @@
 
                 part = part->next;
                 header = part->elts;
-                i = 0; 
+                i = 0;
             }
 
             key = header[i].hash % plcf->headers_set_hash->hash_size;
@@ -620,9 +661,22 @@
         }
     }
 
+
     /* add "\r\n" at the header end */
     *b->last++ = CR; *b->last++ = LF;
 
+    if (plcf->body_set) {
+        e.ip = plcf->body_set->elts;
+        e.pos = b->last;
+
+        while (*(uintptr_t *) e.ip) {
+            code = *(ngx_http_script_code_pt *) e.ip;
+            code((ngx_http_script_engine_t *) &e);
+        }
+    }
+
+    b->last = e.pos;
+
 #if (NGX_DEBUG)
     {
     ngx_str_t  s;
@@ -634,7 +688,7 @@
     }
 #endif
 
-    if (plcf->upstream.pass_request_body) {
+    if (plcf->body_set == NULL && plcf->upstream.pass_request_body) {
 
         body = u->request_bufs;
         u->request_bufs = cl;
@@ -700,12 +754,7 @@
     p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
 
     if (p == NULL) {
-        p = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t));
-        if (p == NULL) {
-            return NGX_HTTP_INTERNAL_SERVER_ERROR;
-        }
-
-        ngx_http_set_ctx(r, p, ngx_http_proxy_module);
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
 
     rc = ngx_http_proxy_parse_status_line(r, p);
@@ -718,7 +767,7 @@
 
     if (rc == NGX_HTTP_PROXY_PARSE_NO_HEADER) {
         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
-                      "upstream sent no valid HTTP/1.0 header"); 
+                      "upstream sent no valid HTTP/1.0 header");
 
         if (u->accel) {
             return NGX_HTTP_UPSTREAM_INVALID_HEADER;
@@ -726,7 +775,7 @@
 
         r->http_version = NGX_HTTP_VERSION_9;
         p->status = NGX_HTTP_OK;
-    
+
         return NGX_OK;
     }
 
@@ -773,7 +822,7 @@
         sw_space_after_status,
         sw_status_text,
         sw_almost_done
-    } state; 
+    } state;
 
     u = r->upstream;
 
@@ -787,7 +836,7 @@
         /* "HTTP/" */
         case sw_start:
             switch (ch) {
-            case 'H': 
+            case 'H':
                 state = sw_H;
                 break;
             default:
@@ -795,9 +844,9 @@
             }
             break;
 
-        case sw_H: 
+        case sw_H:
             switch (ch) {
-            case 'T': 
+            case 'T':
                 state = sw_HT;
                 break;
             default:
@@ -807,7 +856,7 @@
 
         case sw_HT:
             switch (ch) {
-            case 'T': 
+            case 'T':
                 state = sw_HTT;
                 break;
             default:
@@ -817,7 +866,7 @@
 
         case sw_HTT:
             switch (ch) {
-            case 'P': 
+            case 'P':
                 state = sw_HTTP;
                 break;
             default:
@@ -827,7 +876,7 @@
 
         case sw_HTTP:
             switch (ch) {
-            case '/': 
+            case '/':
                 state = sw_first_major_digit;
                 break;
             default:
@@ -897,7 +946,7 @@
          /* space or end of line */
          case sw_space_after_status:
             switch (ch) {
-            case ' ': 
+            case ' ':
                 state = sw_status_text;
                 break;
             case '.':                    /* IIS may send 403.1, 403.2, etc */
@@ -1044,14 +1093,14 @@
 {
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                    "abort http proxy request");
-    
+
     return;
 }
 
 
 static void
 ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
-{   
+{
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                    "finalize http proxy request");
 
@@ -1068,7 +1117,7 @@
     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
 
     v->len = plcf->host_header.len;
-    v->valid = 1; 
+    v->valid = 1;
     v->no_cachable = 0;
     v->not_found = 0;
     v->data = plcf->host_header.data;
@@ -1086,7 +1135,7 @@
     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
 
     v->len = plcf->port_text.len;
-    v->valid = 1; 
+    v->valid = 1;
     v->no_cachable = 0;
     v->not_found = 0;
     v->data = plcf->port_text.data;
@@ -1097,12 +1146,11 @@
 
 static ngx_int_t
 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
-    
     ngx_http_variable_value_t *v, uintptr_t data)
 {
     u_char  *p;
 
-    v->valid = 1; 
+    v->valid = 1;
     v->no_cachable = 0;
     v->not_found = 0;
 
@@ -1134,6 +1182,35 @@
 
 
 static ngx_int_t
+ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data)
+{
+    ngx_http_proxy_ctx_t  *p;
+
+    p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
+
+    if (p == NULL) {
+        v->not_found = 1;
+        return NGX_OK;
+    }
+
+    v->valid = 1;
+    v->no_cachable = 0;
+    v->not_found = 0;
+
+    v->data = ngx_palloc(r->connection->pool, NGX_SIZE_T_LEN);
+
+    if (v->data == NULL) {
+        return NGX_ERROR;
+    }
+
+    v->len = ngx_sprintf(v->data, "%uz", p->internal_body_length) - v->data;
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
 ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h,
     size_t prefix)
 {
@@ -1294,9 +1371,12 @@
      *     conf->headers_set_len = NULL;
      *     conf->headers_set = NULL;
      *     conf->headers_set_hash = NULL;
+     *     conf->body_set_len = NULL;
+     *     conf->body_set = NULL;
+     *     conf->body_source = { 0, NULL };
      *     conf->rewrite_locations = NULL;
      */
-    
+
     conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
     conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
     conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
@@ -1305,7 +1385,7 @@
     conf->upstream.header_buffer_size = NGX_CONF_UNSET_SIZE;
 
     conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
-    conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;  
+    conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
     conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
 
     conf->upstream.max_fails = NGX_CONF_UNSET_UINT;
@@ -1366,14 +1446,14 @@
                               8, ngx_pagesize);
 
     if (conf->upstream.bufs.num < 2) {
-        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                            "there must be at least 2 \"proxy_buffers\"");
         return NGX_CONF_ERROR;
     }
-    
+
 
     size = conf->upstream.header_buffer_size;
-    if (size < conf->upstream.bufs.size) { 
+    if (size < conf->upstream.bufs.size) {
         size = conf->upstream.bufs.size;
     }
 
@@ -1388,7 +1468,7 @@
         conf->upstream.busy_buffers_size =
                                          conf->upstream.busy_buffers_size_conf;
     }
-    
+
     if (conf->upstream.busy_buffers_size < size) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
              "\"proxy_busy_buffers_size\" must be equal or bigger than "
@@ -1419,7 +1499,7 @@
         conf->upstream.temp_file_write_size =
                                       conf->upstream.temp_file_write_size_conf;
     }
-    
+
     if (conf->upstream.temp_file_write_size < size) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
              "\"proxy_temp_file_write_size\" must be equal or bigger than "
@@ -1534,6 +1614,50 @@
         conf->peers = prev->peers;
     }
 
+
+    if (conf->body_source.data == NULL) {
+        conf->body_source = prev->body_source;
+        conf->body_set_len = prev->body_set_len;
+        conf->body_set = prev->body_set;
+    }
+
+    if (conf->body_set_len == 0) {
+
+        ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
+
+        sc.cf = cf;
+        sc.source = &conf->body_source;
+        sc.flushes = &conf->flushes;
+        sc.lengths = &conf->body_set_len;
+        sc.values = &conf->body_set;
+        sc.complete_lengths = 1;
+        sc.complete_values = 1;
+
+        if (ngx_http_script_compile(&sc) != NGX_OK) {
+            return NGX_CONF_ERROR;
+        }
+
+        if (conf->headers_source == NULL) {
+            conf->headers_source = ngx_array_create(cf->pool, 4,
+                                                    sizeof(ngx_table_elt_t));
+            if (conf->headers_source == NULL) {
+                return NGX_CONF_ERROR;
+            }
+        }
+
+        s = ngx_array_push(conf->headers_source);
+        if (s == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+        s->hash = 0;
+        s->key.len = sizeof("Content-Length") - 1;
+        s->key.data = (u_char *) "Content-Length";
+        s->value.len = sizeof("$proxy_internal_body_length") - 1;
+        s->value.data = (u_char *) "$proxy_internal_body_length";
+    }
+
+
     if (conf->headers_source == NULL) {
         conf->flushes = prev->flushes;
         conf->headers_set_len = prev->headers_set_len;
@@ -1595,6 +1719,8 @@
         continue;
     }
 
+
+    src = conf->headers_source->elts;
     for (i = 0; i < conf->headers_source->nelts; i++) {
 
         name = ngx_array_push(conf->headers_names);
@@ -1766,9 +1892,13 @@
 {
     ngx_http_proxy_loc_conf_t *plcf = conf;
 
+    size_t                       add;
     ngx_str_t                   *value, *url;
     ngx_inet_upstream_t          inet_upstream;
     ngx_http_core_loc_conf_t    *clcf;
+#if (NGX_HTTP_SSL)
+    ngx_pool_cleanup_t          *cln;
+#endif
 #if (NGX_HAVE_UNIX_DOMAIN)
     ngx_unix_domain_upstream_t   unix_upstream;
 #endif
@@ -1781,20 +1911,57 @@
 
     url = &value[1];
 
-    if (ngx_strncasecmp(url->data, "http://", 7) != 0) {
+    if (ngx_strncasecmp(url->data, "http://", 7) == 0) {
+        add = 7;
+
+    } else if (ngx_strncasecmp(url->data, "https://", 8) == 0) {
+
+#if (NGX_HTTP_SSL)
+
+        add = 8;
+
+        plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
+        if (plcf->upstream.ssl == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+        plcf->upstream.ssl->log = cf->log;
+
+        if (ngx_ssl_create(plcf->upstream.ssl,
+                           NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1)
+            != NGX_OK)
+        {
+            return NGX_CONF_ERROR;
+        }
+
+        cln = ngx_pool_cleanup_add(cf->pool, 0);
+        if (cln == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+        cln->handler = ngx_ssl_cleanup_ctx;
+        cln->data = plcf->upstream.ssl;
+
+#else
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "https protocol requires SSL support");
+        return NGX_CONF_ERROR;
+#endif
+
+    } else {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
         return NGX_CONF_ERROR;
     }
 
-    if (ngx_strncasecmp(url->data + 7, "unix:", 5) == 0) {
+    if (ngx_strncasecmp(url->data + add, "unix:", 5) == 0) {
 
 #if (NGX_HAVE_UNIX_DOMAIN)
 
         ngx_memzero(&unix_upstream, sizeof(ngx_unix_domain_upstream_t));
 
         unix_upstream.name = *url;
-        unix_upstream.url.len = url->len - 7;
-        unix_upstream.url.data = url->data + 7;
+        unix_upstream.url.len = url->len - add;
+        unix_upstream.url.data = url->data + add;
         unix_upstream.uri_part = 1;
 
         plcf->peers = ngx_unix_upstream_parse(cf, &unix_upstream);
@@ -1818,8 +1985,8 @@
         ngx_memzero(&inet_upstream, sizeof(ngx_inet_upstream_t));
 
         inet_upstream.name = *url;
-        inet_upstream.url.len = url->len - 7;
-        inet_upstream.url.data = url->data + 7;
+        inet_upstream.url.len = url->len - add;
+        inet_upstream.url.data = url->data + add;
         inet_upstream.default_port_value = 80;
         inet_upstream.uri_part = 1;
 
diff --git a/src/http/modules/ngx_http_range_filter_module.c b/src/http/modules/ngx_http_range_filter_module.c
index c8b30b8..05e2c93 100644
--- a/src/http/modules/ngx_http_range_filter_module.c
+++ b/src/http/modules/ngx_http_range_filter_module.c
@@ -300,11 +300,7 @@
                                                r->headers_out.content_length_n)
                                    - content_range->value.data;
 
-        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;
-        }
+        ngx_http_clear_content_length(r);
 
         return rc;
     }
@@ -350,7 +346,7 @@
     if (ctx == NULL) {
         return NGX_ERROR;
     }
-    
+
     ngx_http_set_ctx(r, ctx, ngx_http_range_body_filter_module);
 
 
diff --git a/src/http/modules/ngx_http_referer_module.c b/src/http/modules/ngx_http_referer_module.c
new file mode 100644
index 0000000..a3d3d46
--- /dev/null
+++ b/src/http/modules/ngx_http_referer_module.c
@@ -0,0 +1,309 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_http.h>
+
+
+typedef struct {
+    ngx_str_t     name;
+    ngx_uint_t    wildcard;
+} ngx_http_referer_t;
+
+typedef struct {
+    ngx_array_t  *referers;     /* ngx_http_referer_t */
+
+    ngx_flag_t    no_referer;
+    ngx_flag_t    blocked_referer;
+} ngx_http_referer_conf_t;
+
+
+static void * ngx_http_referer_create_conf(ngx_conf_t *cf);
+static char * ngx_http_referer_merge_conf(ngx_conf_t *cf, void *parent,
+    void *child);
+static char *ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd,
+    void *conf);
+
+
+static ngx_command_t  ngx_http_referer_commands[] = {
+
+    { ngx_string("valid_referers"),
+      NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
+      ngx_http_valid_referers,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      0,
+      NULL },
+
+      ngx_null_command
+};
+
+
+static ngx_http_module_t  ngx_http_referer_module_ctx = {
+    NULL,                                  /* preconfiguration */
+    NULL,                                  /* postconfiguration */
+
+    NULL,                                  /* create main configuration */
+    NULL,                                  /* init main configuration */
+
+    NULL,                                  /* create server configuration */
+    NULL,                                  /* merge server configuration */
+
+    ngx_http_referer_create_conf,          /* create location configuration */
+    ngx_http_referer_merge_conf            /* merge location configuration */
+};
+
+
+ngx_module_t  ngx_http_referer_module = {
+    NGX_MODULE_V1,
+    &ngx_http_referer_module_ctx,          /* module context */
+    ngx_http_referer_commands,             /* module directives */
+    NGX_HTTP_MODULE,                       /* module type */
+    NULL,                                  /* init master */
+    NULL,                                  /* init module */
+    NULL,                                  /* init process */
+    NULL,                                  /* init thread */
+    NULL,                                  /* exit thread */
+    NULL,                                  /* exit process */
+    NULL,                                  /* exit master */
+    NGX_MODULE_V1_PADDING
+};
+
+
+static ngx_int_t
+ngx_http_referer_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
+     uintptr_t data)
+{
+    u_char                  *ref;
+    size_t                   len;
+    ngx_uint_t               i, n;
+    ngx_http_referer_t       *refs;
+    ngx_http_referer_conf_t  *cf;
+
+    cf = ngx_http_get_module_loc_conf(r, ngx_http_referer_module);
+
+    if (cf->referers == NULL) {
+        *v = ngx_http_variable_null_value;
+        return NGX_OK;
+    }
+
+    if (r->headers_in.referer == NULL) {
+        if (cf->no_referer) {
+            *v = ngx_http_variable_null_value;
+            return NGX_OK;
+
+        } else {
+            *v = ngx_http_variable_true_value;
+            return NGX_OK;
+        }
+    }
+
+    len = r->headers_in.referer->value.len;
+    ref = r->headers_in.referer->value.data;
+
+    if (len < sizeof("http://i.ru") - 1
+        || (ngx_strncasecmp(ref, "http://", 7) != 0))
+    {
+        if (cf->blocked_referer) {
+            *v = ngx_http_variable_null_value;
+            return NGX_OK;
+
+        } else {
+            *v = ngx_http_variable_true_value;
+            return NGX_OK;
+        }
+    }
+
+    len -= 7;
+    ref += 7;
+
+    refs = cf->referers->elts;
+    for (i = 0; i < cf->referers->nelts; i++ ){
+
+        if (refs[i].name.len > len) {
+            continue;
+        }
+
+        if (refs[i].wildcard) {
+            for (n = 0; n < len; n++) {
+                if (ref[n] == '/' || ref[n] == ':') {
+                    break;
+                }
+
+                if (ref[n] != '.') {
+                    continue;
+                }
+
+                if (ngx_strncmp(&ref[n], refs[i].name.data,
+                                refs[i].name.len) == 0)
+                {
+                    *v = ngx_http_variable_null_value;
+                    return NGX_OK;
+                }
+            }
+
+        } else {
+            if (ngx_strncasecmp(refs[i].name.data, ref, refs[i].name.len) == 0)
+            {
+                *v = ngx_http_variable_null_value;
+                return NGX_OK;
+            }
+        }
+    }
+
+    *v = ngx_http_variable_true_value;
+
+    return NGX_OK;
+}
+
+
+static void *
+ngx_http_referer_create_conf(ngx_conf_t *cf)
+{
+    ngx_http_referer_conf_t  *conf;
+
+    conf = ngx_palloc(cf->pool, sizeof(ngx_http_referer_conf_t));
+    if (conf == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    conf->referers = NULL;
+    conf->no_referer = NGX_CONF_UNSET;
+    conf->blocked_referer = NGX_CONF_UNSET;
+
+    return conf;
+}
+
+
+static char *
+ngx_http_referer_merge_conf(ngx_conf_t *cf, void *parent, void *child)
+{
+    ngx_http_referer_conf_t *prev = parent;
+    ngx_http_referer_conf_t *conf = child;
+
+    if (conf->referers == NULL) {
+        conf->referers = prev->referers;
+        ngx_conf_merge_value(conf->no_referer, prev->no_referer, 0);
+        ngx_conf_merge_value(conf->blocked_referer, prev->blocked_referer, 0);
+    }
+
+    if (conf->no_referer == NGX_CONF_UNSET) {
+        conf->no_referer = 0;
+    }
+
+    if (conf->blocked_referer == NGX_CONF_UNSET) {
+        conf->blocked_referer = 0;
+    }
+
+    return NGX_CONF_OK;
+}
+
+
+static char *
+ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+    ngx_http_referer_conf_t  *lcf = conf;
+
+    ngx_uint_t                 i, server_names;
+    ngx_str_t                 *value, name;
+    ngx_http_referer_t        *ref;
+    ngx_http_variable_t       *var;
+    ngx_http_server_name_t    *sn;
+    ngx_http_core_srv_conf_t  *cscf;
+
+    name.len = sizeof("invalid_referer") - 1;
+    name.data = (u_char *) "invalid_referer";
+
+    var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGABLE);
+    if (var == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    var->handler = ngx_http_referer_variable;
+
+    cscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_core_module);
+
+    if (lcf->referers == NULL) {
+        lcf->referers = ngx_array_create(cf->pool,
+                                    cf->args->nelts + cscf->server_names.nelts,
+                                    sizeof(ngx_http_referer_t));
+        if (lcf->referers == NULL) {
+            return NGX_CONF_ERROR;
+        }
+    }
+
+    value = cf->args->elts;
+    server_names = 0;
+
+    for (i = 1; i < cf->args->nelts; i++) {
+        if (value[i].len == 0) {
+            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                               "invalid referer \"%V\"", &value[i]);
+            return NGX_CONF_ERROR;
+        }
+
+        if (ngx_strcmp(value[i].data, "none") == 0) {
+            lcf->no_referer = 1;
+            continue;
+        }
+
+        if (ngx_strcmp(value[i].data, "blocked") == 0) {
+            lcf->blocked_referer = 1;
+            continue;
+        }
+
+        if (ngx_strcmp(value[i].data, "server_names") == 0) {
+            server_names = 1;
+            continue;
+        }
+
+        ref = ngx_array_push(lcf->referers);
+        if (ref == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+        if (value[i].data[0] != '*') {
+            ref->name = value[i];
+            ref->wildcard = 0;
+            continue;
+        }
+
+
+        if (value[i].data[1] != '.') {
+            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                               "invalid wildcard referer \"%V\"", &value[i]);
+            return NGX_CONF_ERROR;
+        }
+
+        ref->name.len = value[i].len - 1;
+        ref->name.data = value[i].data + 1;
+        ref->wildcard = 1;
+    }
+
+    if (!server_names) {
+        return NGX_CONF_OK;
+    }
+
+    sn = cscf->server_names.elts;
+    for (i = 0; i < cscf->server_names.nelts; i++) {
+        ref = ngx_array_push(lcf->referers);
+        if (ref == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+        ref->name.len = sn[i].name.len + 1;
+        ref->name.data = ngx_palloc(cf->pool, ref->name.len);
+        if (ref->name.data == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+        ngx_memcpy(ref->name.data, sn[i].name.data, sn[i].name.len);
+        ref->name.data[sn[i].name.len] = '/';
+        ref->wildcard = sn[i].wildcard;
+    }
+
+    return NGX_CONF_OK;
+}
diff --git a/src/http/modules/ngx_http_rewrite_module.c b/src/http/modules/ngx_http_rewrite_module.c
index 6e64f6a..2c895c5 100644
--- a/src/http/modules/ngx_http_rewrite_module.c
+++ b/src/http/modules/ngx_http_rewrite_module.c
@@ -10,22 +10,12 @@
 
 
 typedef struct {
-    ngx_str_t                   name;
-    ngx_uint_t                  wildcard;
-} ngx_http_rewrite_referer_t;
+    ngx_array_t  *codes;        /* uintptr_t */
 
+    ngx_uint_t    captures;
+    ngx_uint_t    stack_size;
 
-typedef struct {
-    ngx_array_t                *codes;        /* uintptr_t */
-    ngx_array_t                *referers;     /* ngx_http_rewrite_referer_t */
-
-    ngx_uint_t                  captures;
-    ngx_uint_t                  stack_size;
-
-    ngx_flag_t                  log;
-
-    ngx_flag_t                  no_referer;
-    ngx_flag_t                  blocked_referer;
+    ngx_flag_t    log;
 } ngx_http_rewrite_loc_conf_t;
 
 
@@ -44,10 +34,10 @@
     ngx_http_rewrite_loc_conf_t *lcf);
 static char *ngx_http_rewrite_variable(ngx_conf_t *cf,
     ngx_http_rewrite_loc_conf_t *lcf, ngx_str_t *value);
-static char *ngx_http_rewrite_valid_referers(ngx_conf_t *cf,
-    ngx_command_t *cmd, void *conf);
 static char *ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
+static char * ngx_http_rewrite_value(ngx_conf_t *cf,
+    ngx_http_rewrite_loc_conf_t *lcf, ngx_str_t *value);
 
 
 static ngx_command_t  ngx_http_rewrite_commands[] = {
@@ -83,12 +73,14 @@
       0,
       NULL },
 
+#if 0
     { ngx_string("valid_referers"),
       NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
       ngx_http_rewrite_valid_referers,
       NGX_HTTP_LOC_CONF_OFFSET,
       0,
       NULL },
+#endif
 
     { ngx_string("set"),
       NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
@@ -127,7 +119,7 @@
 
 ngx_module_t  ngx_http_rewrite_module = {
     NGX_MODULE_V1,
-    &ngx_http_rewrite_module_ctx,          /* module context */ 
+    &ngx_http_rewrite_module_ctx,          /* module context */
     ngx_http_rewrite_commands,             /* module directives */
     NGX_HTTP_MODULE,                       /* module type */
     NULL,                                  /* init master */
@@ -141,10 +133,6 @@
 };
 
 
-static ngx_http_variable_value_t  ngx_http_rewrite_null_value =
-    ngx_http_variable("");
-
-
 static ngx_int_t
 ngx_http_rewrite_handler(ngx_http_request_t *r)
 {
@@ -194,121 +182,6 @@
 }
 
 
-static void
-ngx_http_rewrite_invalid_referer_code(ngx_http_script_engine_t *e)
-{
-    u_char                       *ref;
-    size_t                        len;
-    ngx_uint_t                    i, n;
-    ngx_http_request_t           *r;
-    ngx_http_rewrite_referer_t   *refs;
-    ngx_http_rewrite_loc_conf_t  *cf;
-
-    r = e->request;
-
-    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                   "http rewrite invalid referer");
-
-    cf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module);
-
-    e->ip += sizeof(uintptr_t);
-
-    if (cf->referers == NULL) {
-        e->sp->data = (u_char *) "";
-        e->sp->len = 0;
-        e->sp++;
-
-        return;
-    }
-
-    if (r->headers_in.referer == NULL) {
-        if (cf->no_referer) {
-            e->sp->data = (u_char *) "";
-            e->sp->len = 0;
-            e->sp++;
-
-            return;
-
-        } else {
-            e->sp->data = (u_char *) "1";
-            e->sp->len = 1;
-            e->sp++;
-
-            return;
-        }
-    }
-
-    len = r->headers_in.referer->value.len;
-    ref = r->headers_in.referer->value.data;
-
-    if (len < sizeof("http://i.ru") - 1
-        || (ngx_strncasecmp(ref, "http://", 7) != 0))
-    {
-        if (cf->blocked_referer) {
-            e->sp->data = (u_char *) "";
-            e->sp->len = 0;
-            e->sp++;
-
-            return;
-
-        } else {
-            e->sp->data = (u_char *) "1";
-            e->sp->len = 1;
-            e->sp++;
-
-            return;
-        }
-    }
-
-    len -= 7;
-    ref += 7;
-
-    refs = cf->referers->elts;
-    for (i = 0; i < cf->referers->nelts; i++ ){
-
-        if (refs[i].name.len > len) {
-            continue;
-        }
-
-        if (refs[i].wildcard) {
-            for (n = 0; n < len; n++) {
-                if (ref[n] == '/' || ref[n] == ':') {
-                    break;
-                }
-
-                if (ref[n] != '.') {
-                    continue;
-                }
-
-                if (ngx_strncmp(&ref[n], refs[i].name.data,
-                                refs[i].name.len) == 0)
-                {
-                    e->sp->data = (u_char *) "";
-                    e->sp->len = 0;
-                    e->sp++;
-
-                    return;
-                }
-            }
-
-        } else {
-            if (ngx_strncasecmp(refs[i].name.data, ref, refs[i].name.len) == 0)
-            {
-                e->sp->data = (u_char *) "";
-                e->sp->len = 0;
-                e->sp++;
-
-                return;
-            }
-        }
-    }
-
-    e->sp->data = (u_char *) "1";
-    e->sp->len = 1;
-    e->sp++;
-}
-
-
 static ngx_int_t
 ngx_http_rewrite_var(ngx_http_request_t *r, ngx_http_variable_value_t *v,
     uintptr_t data)
@@ -329,7 +202,7 @@
     ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
                   "using uninitialized \"%V\" variable", &var[data].name);
 
-    *v = ngx_http_rewrite_null_value;
+    *v = ngx_http_variable_null_value;
 
     return NGX_OK;
 }
@@ -347,8 +220,6 @@
 
     conf->stack_size = NGX_CONF_UNSET_UINT;
     conf->log = NGX_CONF_UNSET;
-    conf->no_referer = NGX_CONF_UNSET;
-    conf->blocked_referer = NGX_CONF_UNSET;
 
     return conf;
 }
@@ -365,20 +236,6 @@
     ngx_conf_merge_value(conf->log, prev->log, 0);
     ngx_conf_merge_unsigned_value(conf->stack_size, prev->stack_size, 10);
 
-    if (conf->referers == NULL) {
-        conf->referers = prev->referers;
-        ngx_conf_merge_value(conf->no_referer, prev->no_referer, 0);
-        ngx_conf_merge_value(conf->blocked_referer, prev->blocked_referer, 0);
-    }
-
-    if (conf->no_referer == NGX_CONF_UNSET) {
-        conf->no_referer = 0;
-    }
-
-    if (conf->blocked_referer == NGX_CONF_UNSET) {
-        conf->blocked_referer = 0;
-    }
-
     if (conf->codes == NULL) {
         return NGX_CONF_OK;
     }
@@ -400,7 +257,7 @@
 
 static ngx_int_t
 ngx_http_rewrite_init(ngx_cycle_t *cycle)
-{   
+{
     ngx_http_handler_pt        *h;
     ngx_http_core_main_conf_t  *cmcf;
 
@@ -421,14 +278,14 @@
     *h = ngx_http_rewrite_handler;
 
     return NGX_OK;
-}   
+}
 
 
 static char *
 ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-    ngx_http_rewrite_loc_conf_t *lcf = conf;
-    
+    ngx_http_rewrite_loc_conf_t  *lcf = conf;
+
     ngx_str_t                         *value, err;
     ngx_int_t                          n;
     ngx_uint_t                         last;
@@ -592,7 +449,7 @@
 static char *
 ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-    ngx_http_rewrite_loc_conf_t *lcf = conf;
+    ngx_http_rewrite_loc_conf_t  *lcf = conf;
 
     ngx_str_t                      *value;
     ngx_http_script_return_code_t  *ret;
@@ -639,7 +496,7 @@
 static char *
 ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-    ngx_http_rewrite_loc_conf_t *lcf = conf;
+    ngx_http_rewrite_loc_conf_t  *lcf = conf;
 
     void                         *mconf;
     char                         *rv;
@@ -659,7 +516,7 @@
 
     pctx = cf->ctx;
     ctx->main_conf = pctx->main_conf;
-    ctx->srv_conf = pctx->srv_conf; 
+    ctx->srv_conf = pctx->srv_conf;
 
     ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
     if (ctx->loc_conf == NULL) {
@@ -672,7 +529,7 @@
         }
 
         module = ngx_modules[i]->ctx;
-    
+
         if (module->create_loc_conf) {
 
             mconf = module->create_loc_conf(cf);
@@ -774,6 +631,7 @@
 {
     ngx_str_t                     *value, err;
     ngx_uint_t                     cur, last, n;
+    ngx_http_script_code_pt       *code;
     ngx_http_script_regex_code_t  *regex;
     u_char                         errstr[NGX_MAX_CONF_ERRSTR];
 
@@ -817,7 +675,7 @@
             return NGX_CONF_ERROR;
         }
 
-        if (ngx_http_rewrite_variable(cf, lcf, &value[cur])!= NGX_CONF_OK) {
+        if (ngx_http_rewrite_variable(cf, lcf, &value[cur]) != NGX_CONF_OK) {
             return NGX_CONF_ERROR;
         }
 
@@ -827,51 +685,85 @@
 
         cur++;
 
-        if ((value[cur].len == 1 && value[cur].data[0] != '~')
-            || (value[cur].len == 2
-                && value[cur].data[0] != '~' && value[cur].data[1] != '*'))
-        {
-            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                               "unexpected \"%V\" in condition", &value[cur]);
-            return NGX_CONF_ERROR;
-        }
+        if (value[cur].len == 1 && value[cur].data[0] == '=') {
 
-        regex = ngx_http_script_start_code(cf->pool, &lcf->codes,
-                                         sizeof(ngx_http_script_regex_code_t));
-        if (regex == NULL) {
-            return NGX_CONF_ERROR;
-        }
-
-        ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t));
-
-        err.len = NGX_MAX_CONF_ERRSTR;
-        err.data = errstr;
-
-        regex->regex = ngx_regex_compile(&value[last],
-                                (value[cur].len == 2) ? NGX_REGEX_CASELESS : 0,
-                                cf->pool, &err);
-
-        if (regex->regex == NULL) {
-            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data);
-            return NGX_CONF_ERROR;
-        }
-
-        regex->code = ngx_http_script_regex_start_code;
-        regex->next = sizeof(ngx_http_script_regex_code_t);
-        regex->test = 1;
-        regex->name = value[last];
-
-        n = ngx_regex_capture_count(regex->regex);
-
-        if (n) {
-            regex->ncaptures = (n + 1) * 3;
-
-            if (lcf->captures < regex->ncaptures) {
-                lcf->captures = regex->ncaptures;
+            if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
+                return NGX_CONF_ERROR;
             }
+
+            code = ngx_http_script_start_code(cf->pool, &lcf->codes,
+                                              sizeof(uintptr_t));
+            if (code == NULL) {
+                return NGX_CONF_ERROR;
+            }
+
+            *code = ngx_http_script_equal_code;
+
+            return NGX_CONF_OK;
         }
 
-        return NGX_CONF_OK;
+        if (value[cur].len == 2
+            && value[cur].data[0] == '!' && value[cur].data[1] == '=')
+        {
+            if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
+                return NGX_CONF_ERROR;
+            }
+
+            code = ngx_http_script_start_code(cf->pool, &lcf->codes,
+                                              sizeof(uintptr_t));
+            if (code == NULL) {
+                return NGX_CONF_ERROR;
+            }
+
+            *code = ngx_http_script_not_equal_code;
+            return NGX_CONF_OK;
+        }
+
+        if ((value[cur].len == 1 && value[cur].data[0] == '~')
+            || (value[cur].len == 2
+                && value[cur].data[0] == '~' && value[cur].data[1] == '*'))
+        {
+            regex = ngx_http_script_start_code(cf->pool, &lcf->codes,
+                                         sizeof(ngx_http_script_regex_code_t));
+            if (regex == NULL) {
+                return NGX_CONF_ERROR;
+            }
+
+            ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t));
+
+            err.len = NGX_MAX_CONF_ERRSTR;
+            err.data = errstr;
+
+            regex->regex = ngx_regex_compile(&value[last],
+                                 (value[cur].len == 2) ? NGX_REGEX_CASELESS : 0,
+                                 cf->pool, &err);
+
+            if (regex->regex == NULL) {
+                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data);
+                return NGX_CONF_ERROR;
+            }
+
+            regex->code = ngx_http_script_regex_start_code;
+            regex->next = sizeof(ngx_http_script_regex_code_t);
+            regex->test = 1;
+            regex->name = value[last];
+
+            n = ngx_regex_capture_count(regex->regex);
+
+            if (n) {
+                regex->ncaptures = (n + 1) * 3;
+
+                if (lcf->captures < regex->ncaptures) {
+                    lcf->captures = regex->ncaptures;
+                }
+            }
+
+            return NGX_CONF_OK;
+        }
+
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "unexpected \"%V\" in condition", &value[cur]);
+        return NGX_CONF_ERROR;
     }
 
     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
@@ -886,135 +778,25 @@
     ngx_str_t *value)
 {
     ngx_int_t                    index;
-    ngx_http_script_code_pt     *code;
     ngx_http_script_var_code_t  *var_code;
 
     value->len--;
     value->data++;
 
-    if (value->len == sizeof("invalid_referer") - 1
-        && ngx_strncmp(value->data, "invalid_referer",
-                       sizeof("invalid_referer") - 1) == 0)
-    {
-        code = ngx_http_script_start_code(cf->pool, &lcf->codes,
-                                          sizeof(ngx_http_script_code_pt));
-        if (code == NULL) {
-            return NGX_CONF_ERROR;
-        }
+    index = ngx_http_get_variable_index(cf, value);
 
-        *code = ngx_http_rewrite_invalid_referer_code;
-
-    } else {
-        index = ngx_http_get_variable_index(cf, value);
-
-        if (index == NGX_ERROR) {
-            return NGX_CONF_ERROR;
-        }
-
-        var_code = ngx_http_script_start_code(cf->pool, &lcf->codes,
-                                           sizeof(ngx_http_script_var_code_t));
-        if (var_code == NULL) {
-            return NGX_CONF_ERROR;
-        }
-
-        var_code->code = ngx_http_script_var_code;
-        var_code->index = index;
+    if (index == NGX_ERROR) {
+        return NGX_CONF_ERROR;
     }
 
-    return NGX_CONF_OK;
-}
-
-
-static char *
-ngx_http_rewrite_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
-    ngx_http_rewrite_loc_conf_t *lcf = conf;
-
-    ngx_uint_t                   i, server_names;
-    ngx_str_t                   *value;
-    ngx_http_server_name_t      *sn;
-    ngx_http_core_srv_conf_t    *cscf;
-    ngx_http_rewrite_referer_t  *ref;
-
-    cscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_core_module);
-
-    if (lcf->referers == NULL) {
-        lcf->referers = ngx_array_create(cf->pool,
-                                    cf->args->nelts + cscf->server_names.nelts,
-                                    sizeof(ngx_http_rewrite_referer_t));
-        if (lcf->referers == NULL) {
-            return NGX_CONF_ERROR;
-        }
+    var_code = ngx_http_script_start_code(cf->pool, &lcf->codes,
+                                          sizeof(ngx_http_script_var_code_t));
+    if (var_code == NULL) {
+        return NGX_CONF_ERROR;
     }
 
-    value = cf->args->elts;
-    server_names = 0;
-
-    for (i = 1; i < cf->args->nelts; i++) {
-        if (value[i].len == 0) {
-            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                               "invalid referer \"%V\"", &value[i]);
-            return NGX_CONF_ERROR;
-        }
-
-        if (ngx_strcmp(value[i].data, "none") == 0) {
-            lcf->no_referer = 1;
-            continue;
-        }
-
-        if (ngx_strcmp(value[i].data, "blocked") == 0) {
-            lcf->blocked_referer = 1;
-            continue;
-        }
-
-        if (ngx_strcmp(value[i].data, "server_names") == 0) {
-            server_names = 1;
-            continue;
-        }
-
-        ref = ngx_array_push(lcf->referers);
-        if (ref == NULL) {
-            return NGX_CONF_ERROR;
-        }
-
-        if (value[i].data[0] != '*') {
-            ref->name = value[i];
-            ref->wildcard = 0;
-            continue;
-        }
-
-        if (value[i].data[1] != '.') {
-            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                               "invalid wildcard referer \"%V\"", &value[i]);
-            return NGX_CONF_ERROR;
-        }
-
-        ref->name.len = value[i].len - 1;
-        ref->name.data = value[i].data + 1;
-        ref->wildcard = 1;
-    }
-
-    if (!server_names) {
-        return NGX_CONF_OK;
-    }
-
-    sn = cscf->server_names.elts;
-    for (i = 0; i < cscf->server_names.nelts; i++) {
-        ref = ngx_array_push(lcf->referers);
-        if (ref == NULL) {
-            return NGX_CONF_ERROR;
-        }
-
-        ref->name.len = sn[i].name.len + 1;
-        ref->name.data = ngx_palloc(cf->pool, ref->name.len);
-        if (ref->name.data == NULL) {
-            return NGX_CONF_ERROR;
-        }
-
-        ngx_memcpy(ref->name.data, sn[i].name.data, sn[i].name.len);
-        ref->name.data[sn[i].name.len] = '/';
-        ref->wildcard = sn[i].wildcard;
-    }
+    var_code->code = ngx_http_script_var_code;
+    var_code->index = index;
 
     return NGX_CONF_OK;
 }
@@ -1023,15 +805,12 @@
 static char *
 ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-    ngx_http_rewrite_loc_conf_t *lcf = conf;
+    ngx_http_rewrite_loc_conf_t  *lcf = conf;
 
-    ngx_int_t                              n, index;
-    ngx_str_t                             *value;
-    ngx_http_variable_t                   *v;
-    ngx_http_script_compile_t              sc;
-    ngx_http_script_var_code_t            *var;
-    ngx_http_script_value_code_t          *val;
-    ngx_http_script_complex_value_code_t  *complex;
+    ngx_int_t                     index;
+    ngx_str_t                    *value;
+    ngx_http_variable_t          *v;
+    ngx_http_script_var_code_t   *var;
 
     value = cf->args->elts;
 
@@ -1059,48 +838,8 @@
         v->data = index;
     }
 
-    n = ngx_http_script_variables_count(&value[2]);
-
-    if (n == 0) {
-        val = ngx_http_script_start_code(cf->pool, &lcf->codes,
-                                         sizeof(ngx_http_script_value_code_t));
-        if (val == NULL) {
-            return NGX_CONF_ERROR;
-        }
-
-        n = ngx_atoi(value[2].data, value[2].len);
-
-        if (n == NGX_ERROR) {
-            n = 0;
-        }
-
-        val->code = ngx_http_script_value_code;
-        val->value = (uintptr_t) n;
-        val->text_len = (uintptr_t) value[2].len;
-        val->text_data = (uintptr_t) value[2].data;
-
-    } else {
-        complex = ngx_http_script_start_code(cf->pool, &lcf->codes,
-                                 sizeof(ngx_http_script_complex_value_code_t));
-        if (complex == NULL) {
-            return NGX_CONF_ERROR;
-        }
-
-        complex->code = ngx_http_script_complex_value_code;
-        complex->lengths = NULL;
-
-        ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
-
-        sc.cf = cf; 
-        sc.source = &value[2];
-        sc.lengths = &complex->lengths;
-        sc.values = &lcf->codes;
-        sc.variables = n;
-        sc.complete_lengths = 1;
-
-        if (ngx_http_script_compile(&sc) != NGX_OK) {
-            return NGX_CONF_ERROR;
-        }
+    if (ngx_http_rewrite_value(cf, lcf, &value[2]) != NGX_CONF_OK) {
+        return NGX_CONF_ERROR;
     }
 
     var = ngx_http_script_start_code(cf->pool, &lcf->codes,
@@ -1114,3 +853,61 @@
 
     return NGX_CONF_OK;
 }
+
+
+static char *
+ngx_http_rewrite_value(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf,
+    ngx_str_t *value)
+{
+    ngx_int_t                              n;
+    ngx_http_script_compile_t              sc;
+    ngx_http_script_value_code_t          *val;
+    ngx_http_script_complex_value_code_t  *complex;
+
+    n = ngx_http_script_variables_count(value);
+
+    if (n == 0) {
+        val = ngx_http_script_start_code(cf->pool, &lcf->codes,
+                                         sizeof(ngx_http_script_value_code_t));
+        if (val == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+        n = ngx_atoi(value->data, value->len);
+
+        if (n == NGX_ERROR) {
+            n = 0;
+        }
+
+        val->code = ngx_http_script_value_code;
+        val->value = (uintptr_t) n;
+        val->text_len = (uintptr_t) value->len;
+        val->text_data = (uintptr_t) value->data;
+
+        return NGX_CONF_OK;
+    }
+
+    complex = ngx_http_script_start_code(cf->pool, &lcf->codes,
+                                 sizeof(ngx_http_script_complex_value_code_t));
+    if (complex == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    complex->code = ngx_http_script_complex_value_code;
+    complex->lengths = NULL;
+
+    ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
+
+    sc.cf = cf;
+    sc.source = value;
+    sc.lengths = &complex->lengths;
+    sc.values = &lcf->codes;
+    sc.variables = n;
+    sc.complete_lengths = 1;
+
+    if (ngx_http_script_compile(&sc) != NGX_OK) {
+        return NGX_CONF_ERROR;
+    }
+
+    return NGX_CONF_OK;
+}
diff --git a/src/http/modules/ngx_http_ssi_filter_module.c b/src/http/modules/ngx_http_ssi_filter_module.c
index 86700ec..6f4ac45 100644
--- a/src/http/modules/ngx_http_ssi_filter_module.c
+++ b/src/http/modules/ngx_http_ssi_filter_module.c
@@ -197,7 +197,7 @@
 };
 
 
-    
+
 static ngx_http_module_t  ngx_http_ssi_filter_module_ctx = {
     ngx_http_ssi_add_variables,            /* preconfiguration */
     NULL,                                  /* postconfiguration */
@@ -210,7 +210,7 @@
 
     ngx_http_ssi_create_conf,              /* create location configuration */
     ngx_http_ssi_merge_conf                /* merge location configuration */
-};  
+};
 
 
 ngx_module_t  ngx_http_ssi_filter_module = {
@@ -331,7 +331,8 @@
     conf = ngx_http_get_module_loc_conf(r, ngx_http_ssi_filter_module);
 
     if (!conf->enable
-        || r->headers_out.content_type.len == 0)
+        || r->headers_out.content_type.len == 0
+        || r->headers_out.content_length_n == 0)
     {
         return ngx_http_next_header_filter(r);
     }
@@ -381,17 +382,8 @@
     r->filter_need_in_memory = 1;
 
     if (r->main == r) {
-        r->headers_out.content_length_n = -1;
-        if (r->headers_out.content_length) {
-            r->headers_out.content_length->hash = 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->hash = 0;
-            r->headers_out.last_modified = NULL;
-        }
+        ngx_http_clear_content_length(r);
+        ngx_http_clear_last_modified(r);
     }
 
     return ngx_http_next_header_filter(r);
@@ -1485,7 +1477,7 @@
                 return NGX_ERROR;
             }
 
-            if (var.len == 0) { 
+            if (var.len == 0) {
                 goto invalid_variable;
             }
 
@@ -1983,7 +1975,7 @@
     struct tm            tm;
     char                 buf[NGX_HTTP_SSI_DATE_LEN];
 
-    v->valid = 1; 
+    v->valid = 1;
     v->no_cachable = 0;
     v->not_found = 0;
 
@@ -2092,7 +2084,7 @@
         var->data = v->data;
     }
 
-    return NGX_OK; 
+    return NGX_OK;
 }
 
 
diff --git a/src/http/modules/ngx_http_static_module.c b/src/http/modules/ngx_http_static_module.c
index ab7ac9c..74cd010 100644
--- a/src/http/modules/ngx_http_static_module.c
+++ b/src/http/modules/ngx_http_static_module.c
@@ -44,13 +44,13 @@
 
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
-    
+
     NULL,                                  /* create server configuration */
     NULL,                                  /* merge server configuration */
-    
+
     ngx_http_static_create_loc_conf,       /* create location configuration */
     ngx_http_static_merge_loc_conf         /* merge location configuration */
-};  
+};
 
 
 ngx_module_t  ngx_http_static_module = {
@@ -240,51 +240,37 @@
     r->headers_out.content_length_n = ngx_file_size(&fi);
     r->headers_out.last_modified_time = ngx_file_mtime(&fi);
 
-    if (r->headers_out.content_length_n == 0) {
-        r->header_only = 1;
-    }
-
     if (ngx_http_set_content_type(r) != NGX_OK) {
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
 
-#if (NGX_SUPPRESS_WARN)
-    b = NULL;
-#endif
+    /* we need to allocate all before the header would be sent */
 
-    if (!r->header_only) {
-        /* we need to allocate all before the header would be sent */
-
-        b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
-        if (b == NULL) {
-            return NGX_HTTP_INTERNAL_SERVER_ERROR;
-        }
-
-        b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
-        if (b->file == NULL) {
-            return NGX_HTTP_INTERNAL_SERVER_ERROR;
-        }
-
-        r->filter_allow_ranges = 1;
+    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
+    if (b == NULL) {
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
 
+    b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
+    if (b->file == NULL) {
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
+    }
+
+    r->filter_allow_ranges = 1;
+
     rc = ngx_http_send_header(r);
 
     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
         return rc;
     }
 
-    b->in_file = 1;
-
-    if (r->main == r) {
-        b->last_buf = 1;
-    }
-
-    b->last_in_chain = 1;
-
     b->file_pos = 0;
     b->file_last = ngx_file_size(&fi);
 
+    b->in_file = b->file_last ? 1: 0;
+    b->last_buf = (r->main == r) ? 1: 0;
+    b->last_in_chain = 1;
+
     b->file->fd = fd;
     b->file->name = path;
     b->file->log = log;
@@ -333,7 +319,7 @@
     ngx_http_core_main_conf_t  *cmcf;
 
     cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
-    
+
     h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
     if (h == NULL) {
         return NGX_ERROR;
diff --git a/src/http/modules/ngx_http_status_module.c b/src/http/modules/ngx_http_status_module.c
index ce49ae4..cf4f33f 100644
--- a/src/http/modules/ngx_http_status_module.c
+++ b/src/http/modules/ngx_http_status_module.c
@@ -78,7 +78,7 @@
     r->headers_out.content_type = ngx_list_push(&r->headers_out.headers);
     if (r->headers_out.content_type == NULL) {
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
-    } 
+    }
 
     r->headers_out.content_type->key.len = 0;
     r->headers_out.content_type->key.data = NULL;
@@ -281,7 +281,7 @@
         if (ctx->head) {
             *ll = cl;
 
-        } else { 
+        } else {
             ctx->head = cl;
         }
 
diff --git a/src/http/modules/ngx_http_stub_status_module.c b/src/http/modules/ngx_http_stub_status_module.c
index ea55c1b..7cdb3c1 100644
--- a/src/http/modules/ngx_http_stub_status_module.c
+++ b/src/http/modules/ngx_http_stub_status_module.c
@@ -1,4 +1,9 @@
 
+/*
+ * Copyright (C) Igor Sysoev
+ */
+
+
 #include <ngx_config.h>
 #include <ngx_core.h>
 #include <ngx_http.h>
@@ -6,7 +11,7 @@
 
 static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd,
                                  void *conf);
-    
+
 static ngx_command_t  ngx_http_status_commands[] = {
 
     { ngx_string("stub_status"),
@@ -17,20 +22,20 @@
       NULL },
 
       ngx_null_command
-};  
+};
 
 
-    
+
 ngx_http_module_t  ngx_http_stub_status_module_ctx = {
     NULL,                                  /* preconfiguration */
     NULL,                                  /* postconfiguration */
 
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
-    
+
     NULL,                                  /* create server configuration */
     NULL,                                  /* merge server configuration */
-    
+
     NULL,                                  /* create location configuration */
     NULL                                   /* merge location configuration */
 };
@@ -59,7 +64,7 @@
     ngx_buf_t         *b;
     ngx_chain_t        out;
     ngx_atomic_int_t   ap, hn, ac, rq, rd, wr;
-    
+
     if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
         return NGX_HTTP_NOT_ALLOWED;
     }
diff --git a/src/http/modules/ngx_http_userid_filter_module.c b/src/http/modules/ngx_http_userid_filter_module.c
index 94a709b..c77dca2 100644
--- a/src/http/modules/ngx_http_userid_filter_module.c
+++ b/src/http/modules/ngx_http_userid_filter_module.c
@@ -594,7 +594,7 @@
     ngx_http_userid_ctx_t   *ctx;
     ngx_http_userid_conf_t  *conf;
 
-    v->valid = 1; 
+    v->valid = 1;
     v->no_cachable = 0;
     v->not_found = 0;
 
@@ -634,7 +634,7 @@
 
 static void *
 ngx_http_userid_create_conf(ngx_conf_t *cf)
-{   
+{
     ngx_http_userid_conf_t  *conf;
 
     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_userid_conf_t));
@@ -661,7 +661,7 @@
     conf->mark = (u_char) '\xFF';
 
     return conf;
-}   
+}
 
 
 static char *