nginx-0.3.0-RELEASE import

    *) Change: the 10-days live time limit of worker process was
       eliminated. The limit was introduced because of millisecond timers
       overflow.
diff --git a/src/http/modules/ngx_http_autoindex_module.c b/src/http/modules/ngx_http_autoindex_module.c
index f568964..70f7974 100644
--- a/src/http/modules/ngx_http_autoindex_module.c
+++ b/src/http/modules/ngx_http_autoindex_module.c
@@ -39,13 +39,15 @@
 } ngx_http_autoindex_loc_conf_t;
 
 
-#define NGX_HTTP_AUTOINDEX_NAME_LEN  50
+#define NGX_HTTP_AUTOINDEX_PREALLOCATE  50
+
+#define NGX_HTTP_AUTOINDEX_NAME_LEN     50
 
 
 static int ngx_libc_cdecl ngx_http_autoindex_cmp_entries(const void *one,
     const void *two);
 static ngx_int_t ngx_http_autoindex_error(ngx_http_request_t *r,
-    ngx_dir_t *dir, u_char *name);
+    ngx_dir_t *dir, ngx_str_t *name);
 static ngx_int_t ngx_http_autoindex_init(ngx_cycle_t *cycle);
 static void *ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf);
 static char *ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf,
@@ -131,20 +133,19 @@
 static ngx_int_t
 ngx_http_autoindex_handler(ngx_http_request_t *r)
 {
-    u_char                         *last, scale;
+    u_char                         *last, *filename, scale;
     off_t                           length;
-    size_t                          len, copy;
+    size_t                          len, copy, allocated;
     ngx_tm_t                        tm;
-    ngx_int_t                       rc, size;
-    ngx_uint_t                      i, level;
     ngx_err_t                       err;
     ngx_buf_t                      *b;
-    ngx_chain_t                     out;
-    ngx_str_t                       dname, fname;
+    ngx_int_t                       rc, size;
+    ngx_str_t                       path;
     ngx_dir_t                       dir;
+    ngx_uint_t                      i, level;
     ngx_pool_t                     *pool;
+    ngx_chain_t                     out;
     ngx_array_t                     entries;
-    ngx_http_core_loc_conf_t       *clcf;
     ngx_http_autoindex_entry_t     *entry;
     ngx_http_autoindex_loc_conf_t  *alcf;
 
@@ -166,41 +167,21 @@
         return NGX_DECLINED;
     }
 
-    /* TODO: pool should be temporary pool */
-    pool = r->pool;
+    /* NGX_DIR_MASK_LEN is lesser than NGX_HTTP_AUTOINDEX_PREALLOCATE */
 
-    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
-
-    if (clcf->alias) {
-        dname.data = ngx_palloc(pool, clcf->root.len + r->uri.len
-                                                     + NGX_DIR_MASK_LEN + 1
-                                                     - clcf->name.len);
-        if (dname.data == NULL) {
-            return NGX_HTTP_INTERNAL_SERVER_ERROR;
-        }
-    
-        last = ngx_cpymem(dname.data, clcf->root.data, clcf->root.len);
-        last = ngx_cpystrn(last, r->uri.data + clcf->name.len,
-                           r->uri.len - clcf->name.len + 1);
-
-    } else {
-        dname.data = ngx_palloc(pool, clcf->root.len + r->uri.len
-                                                     + NGX_DIR_MASK_LEN);
-        if (dname.data == NULL) {
-            return NGX_HTTP_INTERNAL_SERVER_ERROR;
-        }
-
-        last = ngx_cpymem(dname.data, clcf->root.data, clcf->root.len);
-        last = ngx_cpystrn(last, r->uri.data, r->uri.len);
+    last = ngx_http_map_uri_to_path(r, &path, NGX_HTTP_AUTOINDEX_PREALLOCATE);
+    if (last == NULL) {
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
 
-    dname.len = last - dname.data;
+    allocated = path.len;
+    path.len = last - path.data - 1;
+    path.data[path.len] = '\0';
 
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                   "http autoindex: \"%s\"", dname.data);
+                   "http autoindex: \"%s\"", path.data);
 
-
-    if (ngx_open_dir(&dname, &dir) == NGX_ERROR) {
+    if (ngx_open_dir(&path, &dir) == NGX_ERROR) {
         err = ngx_errno;
 
         if (err == NGX_ENOENT
@@ -220,20 +201,25 @@
         }
 
         ngx_log_error(level, r->connection->log, err,
-                      ngx_open_dir_n " \"%s\" failed", dname.data);
+                      ngx_open_dir_n " \"%s\" failed", path.data);
 
         return rc;
     }
 
 #if (NGX_SUPPRESS_WARN)
+
     /* MSVC thinks 'entries' may be used without having been initialized */
     ngx_memzero(&entries, sizeof(ngx_array_t));
+
 #endif
 
-    if (ngx_array_init(&entries, pool, 50, sizeof(ngx_http_autoindex_entry_t))
-        == NGX_ERROR)
+    /* TODO: pool should be temporary pool */
+    pool = r->pool;
+
+    if (ngx_array_init(&entries, pool, 40, sizeof(ngx_http_autoindex_entry_t))
+        != NGX_OK)
     {
-        return ngx_http_autoindex_error(r, &dir, dname.data);
+        return ngx_http_autoindex_error(r, &dir, &path);
     }
 
     r->headers_out.status = NGX_HTTP_OK;
@@ -246,10 +232,8 @@
         return rc;
     }
 
-    fname.len = 0;
-#if (NGX_SUPPRESS_WARN)
-    fname.data = NULL;
-#endif
+    filename = path.data;
+    filename[path.len] = '/';
 
     for ( ;; ) {
         ngx_set_errno(0);
@@ -259,8 +243,8 @@
 
             if (err != NGX_ENOMOREFILES) {
                 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
-                              ngx_read_dir_n " \"%s\" failed", dname.data);
-                return ngx_http_autoindex_error(r, &dir, dname.data);
+                              ngx_read_dir_n " \"%V\" failed", &path);
+                return ngx_http_autoindex_error(r, &dir, &path);
             }
 
             break; 
@@ -277,49 +261,51 @@
 
         if (!dir.valid_info) {
 
-            if (dname.len + 1 + len + 1 > fname.len) {
-                fname.len = dname.len + 1 + len + 1 + 32;
+            /* 1 byte for '/' and 1 byte for terminating '\0' */
 
-                fname.data = ngx_palloc(pool, fname.len);
-                if (fname.data == NULL) {
-                    return ngx_http_autoindex_error(r, &dir, dname.data);
+            if (path.len + 1 + len + 1 > allocated) {
+                allocated = path.len + 1 + len + 1
+                                     + NGX_HTTP_AUTOINDEX_PREALLOCATE;
+
+                filename = ngx_palloc(pool, allocated);
+                if (filename == NULL) {
+                    return ngx_http_autoindex_error(r, &dir, &path);
                 }
 
-                last = ngx_cpystrn(fname.data, dname.data,
-                                   dname.len + 1);
+                last = ngx_cpystrn(filename, path.data, path.len + 1);
                 *last++ = '/';
             }
 
             ngx_cpystrn(last, ngx_de_name(&dir), len + 1);
 
-            if (ngx_de_info(fname.data, &dir) == NGX_FILE_ERROR) {
+            if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) {
                 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);
+                                  ngx_de_info_n " \"%s\" failed", filename);
+                    return ngx_http_autoindex_error(r, &dir, &path);
                 }
 
-                if (ngx_de_link_info(fname.data, &dir) == NGX_FILE_ERROR) {
+                if (ngx_de_link_info(filename, &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);
+                                  filename);
+                    return ngx_http_autoindex_error(r, &dir, &path);
                 }
             }
         }
 
         entry = ngx_array_push(&entries);
         if (entry == NULL) {
-            return ngx_http_autoindex_error(r, &dir, dname.data);
+            return ngx_http_autoindex_error(r, &dir, &path);
         }
 
         entry->name.len = len;        
 
         entry->name.data = ngx_palloc(pool, len + 1);
         if (entry->name.data == NULL) {
-            return ngx_http_autoindex_error(r, &dir, dname.data);
+            return ngx_http_autoindex_error(r, &dir, &path);
         }
 
         ngx_cpystrn(entry->name.data, ngx_de_name(&dir), len + 1);
@@ -340,7 +326,7 @@
 
     if (ngx_close_dir(&dir) == NGX_ERROR) {
         ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
-                      ngx_close_dir_n " \"%s\" failed", dname.data);
+                      ngx_close_dir_n " \"%s\" failed", &path);
     }
 
     len = sizeof(title) - 1
@@ -586,11 +572,11 @@
 
 
 static ngx_int_t
-ngx_http_autoindex_error(ngx_http_request_t *r, ngx_dir_t *dir, u_char *name)
+ngx_http_autoindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name)
 {
     if (ngx_close_dir(dir) == NGX_ERROR) {
         ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
-                      ngx_close_dir_n " \"%s\" failed", name);
+                      ngx_close_dir_n " \"%V\" failed", name);
     }
 
     return NGX_HTTP_INTERNAL_SERVER_ERROR;
diff --git a/src/http/modules/ngx_http_index_module.c b/src/http/modules/ngx_http_index_module.c
index abab268..01b6d0c 100644
--- a/src/http/modules/ngx_http_index_module.c
+++ b/src/http/modules/ngx_http_index_module.c
@@ -24,10 +24,8 @@
 
 typedef struct {
     ngx_uint_t               current;
-    size_t                   allocated;
 
-    u_char                  *path;
-    ngx_str_t                uri;
+    ngx_str_t                path;
     ngx_str_t                index;
 
     ngx_uint_t               tested;     /* unsigned  tested:1 */
@@ -37,8 +35,6 @@
 #define NGX_HTTP_DEFAULT_INDEX   "index.html"
 
 
-static ngx_int_t ngx_http_index_alloc(ngx_http_request_t *r, size_t size,
-    ngx_http_index_ctx_t *ctx, ngx_http_core_loc_conf_t *clcf);
 static ngx_int_t ngx_http_index_test_dir(ngx_http_request_t *r,
     ngx_http_index_ctx_t *ctx);
 static ngx_int_t ngx_http_index_error(ngx_http_request_t *r,
@@ -120,11 +116,12 @@
 static ngx_int_t
 ngx_http_index_handler(ngx_http_request_t *r)
 {
-    u_char                       *name;
+    u_char                       *last;
     size_t                        len;
     ngx_fd_t                      fd;
     ngx_int_t                     rc;
     ngx_err_t                     err;
+    ngx_str_t                     uri;
     ngx_log_t                    *log;
     ngx_uint_t                    i;
     ngx_http_index_t             *index;
@@ -152,7 +149,6 @@
      * and may be called several times
      */
 
-    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
     ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);
 
     ctx = ngx_http_get_module_ctx(r, ngx_http_index_module);
@@ -184,7 +180,9 @@
             e.ip = index[i].lengths->elts;
             e.request = r;
 
-            len = 1;
+            /* 1 byte for terminating '\0' and 4 bytes is preallocation */
+
+            len = 1 + 4;
 
             while (*(uintptr_t *) e.ip) {
                 lcode = *(ngx_http_script_len_code_pt *) e.ip;
@@ -194,14 +192,21 @@
             ctx->index.len = len;
         }
 
-        if (len > ctx->allocated) {
-            if (ngx_http_index_alloc(r, len, ctx, clcf) != NGX_OK) {
-                return NGX_HTTP_INTERNAL_SERVER_ERROR;
+        if (len > ctx->path.len) {
+
+            last = ngx_http_map_uri_to_path(r, &ctx->path, len);
+            if (last == NULL) {
+                return NGX_ERROR;
             }
+
+            ctx->index.data = last;
         }
 
         if (index[i].values == NULL) {
-            ngx_memcpy(ctx->index.data, index[i].name.data, ctx->index.len);
+
+            /* index[i].name.len includes the terminating '\0' */
+
+            ngx_memcpy(ctx->index.data, index[i].name.data, index[i].name.len);
 
         } else {
             e.ip = index[i].values->elts;
@@ -220,12 +225,10 @@
             *e.pos++ = '\0';
         }
 
-        name = ctx->path;
-
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
-                       "open index \"%s\"", name);
+                       "open index \"%s\"", ctx->path.data);
 
-        fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN);
+        fd = ngx_open_file(ctx->path.data, NGX_FILE_RDONLY, NGX_FILE_OPEN);
 
         if (fd == (ngx_fd_t) NGX_AGAIN) {
             ctx->current = i;
@@ -236,7 +239,7 @@
             err = ngx_errno;
 
             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, err,
-                           ngx_open_file_n " \"%s\" failed", name);
+                           ngx_open_file_n " \"%s\" failed", ctx->path.data);
 
             if (err == NGX_ENOTDIR) {
                 return ngx_http_index_error(r, ctx, err);
@@ -260,7 +263,7 @@
             }
 
             ngx_log_error(NGX_LOG_ERR, log, err,
-                          ngx_open_file_n " \"%s\" failed", name);
+                          ngx_open_file_n " \"%s\" failed", ctx->path.data);
 
             return NGX_HTTP_INTERNAL_SERVER_ERROR;
         }
@@ -272,22 +275,31 @@
         }
 
         cln->fd = fd;
-        cln->name = name;
+        cln->name = ctx->path.data;
         cln->log = r->pool->log;
 
         if (ngx_pool_cleanup_add(r->pool, ngx_pool_cleanup_file, cln) == NULL) {
             return NGX_HTTP_INTERNAL_SERVER_ERROR;
         }
 
+        clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
 
-        if (clcf->alias) {
-            name = ngx_cpymem(ctx->uri.data, r->uri.data, r->uri.len);
-            ngx_memcpy(name, ctx->index.data, ctx->index.len - 1);
+        uri.len = r->uri.len + ctx->index.len - 1;
+
+        if (!clcf->alias) {
+            uri.data = ctx->path.data + clcf->root.len;
+
+        } else {
+            uri.data = ngx_palloc(r->pool, uri.len);
+            if (uri.data == NULL) {
+                return NGX_HTTP_INTERNAL_SERVER_ERROR;
+            }
+
+            last = ngx_cpymem(uri.data, r->uri.data, r->uri.len);
+            ngx_memcpy(last, ctx->index.data, ctx->index.len - 1);
         }
 
-        ctx->uri.len = r->uri.len + ctx->index.len - 1;
-
-        return ngx_http_internal_redirect(r, &ctx->uri, &r->args);
+        return ngx_http_internal_redirect(r, &uri, &r->args);
     }
 
     return NGX_DECLINED;
@@ -295,45 +307,6 @@
 
 
 static ngx_int_t
-ngx_http_index_alloc(ngx_http_request_t *r, size_t size,
-    ngx_http_index_ctx_t *ctx, ngx_http_core_loc_conf_t *clcf)
-{
-    ctx->allocated = size;
-
-    if (!clcf->alias) {
-        ctx->path = ngx_palloc(r->pool, clcf->root.len + r->uri.len + size);
-        if (ctx->path == NULL) {
-            return NGX_ERROR;
-        }
-
-        ctx->uri.data = ngx_cpymem(ctx->path, clcf->root.data, clcf->root.len);
-
-        ctx->index.data = ngx_cpymem(ctx->uri.data, r->uri.data, r->uri.len);
-
-    } else {
-        ctx->path = ngx_palloc(r->pool,
-                          clcf->root.len + r->uri.len - clcf->name.len + size);
-        if (ctx->path == NULL) {
-            return NGX_ERROR;
-        }
-
-        ctx->uri.data = ngx_palloc(r->pool, r->uri.len + size);
-        if (ctx->uri.data == NULL) {
-            return NGX_ERROR;
-        }
-
-        ngx_memcpy(ctx->path, clcf->root.data, clcf->root.len);
-
-        ctx->index.data = ngx_cpymem(ctx->path + clcf->root.len,
-                                     r->uri.data + clcf->name.len,
-                                     r->uri.len - clcf->name.len);
-    }
-
-    return NGX_OK;
-}
-
-
-static ngx_int_t
 ngx_http_index_test_dir(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx)
 {
     ngx_err_t        err;
@@ -342,9 +315,9 @@
     *(ctx->index.data - 1) = '\0';
 
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                   "http index check dir: \"%s\"", ctx->path);
+                   "http index check dir: \"%s\"", ctx->path.data);
 
-    if (ngx_file_info(ctx->path, &fi) == -1) {
+    if (ngx_file_info(ctx->path.data, &fi) == -1) {
 
         err = ngx_errno;
 
@@ -354,7 +327,7 @@
         }
 
         ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
-                      ngx_file_info_n " \"%s\" failed", ctx->path);
+                      ngx_file_info_n " \"%s\" failed", ctx->path.data);
 
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
@@ -366,7 +339,7 @@
     }
 
     ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
-                  "\"%s\" is not a directory", ctx->path);
+                  "\"%s\" is not a directory", ctx->path.data);
 
     return NGX_HTTP_INTERNAL_SERVER_ERROR;
 }
@@ -378,13 +351,13 @@
 {
     if (err == NGX_EACCES) {
         ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
-                      "\"%s\" is forbidden", ctx->path);
+                      "\"%s\" is forbidden", ctx->path.data);
     
         return NGX_HTTP_FORBIDDEN;
     }
 
     ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
-                  "\"%s\" is not found", ctx->path);
+                  "\"%s\" is not found", ctx->path.data);
 
     return NGX_HTTP_NOT_FOUND;
 }
@@ -494,14 +467,15 @@
         n = ngx_http_script_variables_count(&value[i]);
 
         if (n == 0) {
-            index->name.len++;
-
             if (ilcf->max_index_len != 0
                 && ilcf->max_index_len < index->name.len)
             {
                 ilcf->max_index_len = index->name.len;
             }
 
+            /* include the terminating '\0' to the length to use ngx_memcpy() */
+            index->name.len++;
+
             continue;
         }
 
diff --git a/src/http/modules/ngx_http_static_module.c b/src/http/modules/ngx_http_static_module.c
index afdbf78..c5d5af8 100644
--- a/src/http/modules/ngx_http_static_module.c
+++ b/src/http/modules/ngx_http_static_module.c
@@ -72,18 +72,18 @@
 static ngx_int_t
 ngx_http_static_handler(ngx_http_request_t *r)
 {
-    u_char                      *last;
-    ngx_fd_t                     fd;
-    ngx_int_t                    rc;
-    ngx_uint_t                   level;
-    ngx_str_t                    name, location;
-    ngx_err_t                    err;
-    ngx_log_t                   *log;
-    ngx_buf_t                   *b;
-    ngx_chain_t                  out;
-    ngx_file_info_t              fi;
-    ngx_pool_cleanup_file_t     *cln;
-    ngx_http_core_loc_conf_t    *clcf;
+    u_char                    *last, *location;
+    ngx_fd_t                   fd;
+    ngx_int_t                  rc;
+    ngx_uint_t                 level;
+    ngx_str_t                  path;
+    ngx_err_t                  err;
+    ngx_log_t                 *log;
+    ngx_buf_t                 *b;
+    ngx_chain_t                out;
+    ngx_file_info_t            fi;
+    ngx_pool_cleanup_file_t   *cln;
+    ngx_http_core_loc_conf_t  *clcf;
 
     if (r->uri.data[r->uri.len - 1] == '/') {
         return NGX_DECLINED;
@@ -106,99 +106,20 @@
 
     log = r->connection->log;
 
-    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
-
     /*
-     * make a file name, reserve 2 bytes for a trailing '/'
-     * in a possible redirect and for the last '\0'
+     * ngx_http_map_uri_to_path() allocates memory for terminating '\0'
+     * so we do not need to reserve memory for '/' for possible redirect
      */
 
-    if (!clcf->alias) {
-        name.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len + 2);
-        if (name.data == NULL) {
-            return NGX_HTTP_INTERNAL_SERVER_ERROR;
-        }
-
-        location.data = ngx_cpymem(name.data, clcf->root.data, clcf->root.len);
-        last = ngx_cpystrn(location.data, r->uri.data, r->uri.len + 1);
-
-        name.len = last - name.data;
-        location.len = last - location.data + 1;
-
-    } else {
-        name.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len + 2
-                                        - clcf->name.len);
-        if (name.data == NULL) {
-            return NGX_HTTP_INTERNAL_SERVER_ERROR;
-        }
-
-        last = ngx_cpymem(name.data, clcf->root.data, clcf->root.len);
-        last = ngx_cpystrn(last, r->uri.data + clcf->name.len,
-                           r->uri.len + 1 - clcf->name.len);
-
-        name.len = last - name.data;
-
-        location.data = ngx_palloc(r->pool, r->uri.len + 2);
-        if (location.data == NULL) {
-            return NGX_HTTP_INTERNAL_SERVER_ERROR;
-        }
-
-        last = ngx_cpystrn(location.data, r->uri.data, r->uri.len + 1);
-
-        location.len = last - location.data + 1;
+    last = ngx_http_map_uri_to_path(r, &path, 0);
+    if (last == NULL) {
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
 
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
-                   "http filename: \"%s\"", name.data);
+                   "http filename: \"%s\"", path.data);
 
-    /* open file */
-
-#if (NGX_WIN9X)
-
-    if (ngx_win32_version < NGX_WIN_NT) {
-
-        /*
-         * there is no way to open a file or a directory in Win9X with
-         * one syscall because Win9X has no FILE_FLAG_BACKUP_SEMANTICS flag
-         * so we need to check its type before the opening
-         */
-
-        if (ngx_file_info(name.data, &fi) == NGX_FILE_ERROR) {
-            err = ngx_errno;
-            ngx_log_error(NGX_LOG_ERR, log, err,
-                          ngx_file_info_n " \"%s\" failed", name.data);
-
-            if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
-                return NGX_HTTP_NOT_FOUND;
-
-            } else if (err == NGX_EACCES) {
-                return NGX_HTTP_FORBIDDEN;
-
-            } else {
-                return NGX_HTTP_INTERNAL_SERVER_ERROR;
-            }
-        }
-
-        if (ngx_is_dir(&fi)) {
-            r->headers_out.location = ngx_http_add_header(&r->headers_out,
-                                                          ngx_http_headers_out);
-            if (r->headers_out.location == NULL) {
-                return NGX_HTTP_INTERNAL_SERVER_ERROR;
-            }
-
-            *last++ = '/';
-            *last = '\0';
-            r->headers_out.location->value.len = last - location;
-            r->headers_out.location->value.data = location;
-
-            return NGX_HTTP_MOVED_PERMANENTLY;
-        }
-    }
-
-#endif
-
-
-    fd = ngx_open_file(name.data, NGX_FILE_RDONLY, NGX_FILE_OPEN);
+    fd = ngx_open_file(path.data, NGX_FILE_RDONLY, NGX_FILE_OPEN);
 
     if (fd == NGX_INVALID_FILE) {
         err = ngx_errno;
@@ -219,9 +140,11 @@
             rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
         }
 
+        clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
         if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
             ngx_log_error(level, log, err,
-                          ngx_open_file_n " \"%s\" failed", name.data);
+                          ngx_open_file_n " \"%s\" failed", path.data);
         }
 
         return rc;
@@ -231,11 +154,11 @@
 
     if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
         ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
-                      ngx_fd_info_n " \"%s\" failed", name.data);
+                      ngx_fd_info_n " \"%s\" failed", path.data);
 
         if (ngx_close_file(fd) == NGX_FILE_ERROR) {
             ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
-                          ngx_close_file_n " \"%s\" failed", name.data);
+                          ngx_close_file_n " \"%s\" failed", path.data);
         }
 
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
@@ -247,23 +170,37 @@
 
         if (ngx_close_file(fd) == NGX_FILE_ERROR) {
             ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
-                          ngx_close_file_n " \"%s\" failed", name.data);
+                          ngx_close_file_n " \"%s\" failed", path.data);
         }
 
-        *last++ = '/';
-        *last = '\0';
-
         r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t));
         if (r->headers_out.location == NULL) {
             return NGX_HTTP_INTERNAL_SERVER_ERROR;
         }
 
+        clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
+        if (!clcf->alias) {
+            location = path.data + clcf->root.len;
+
+        } else {
+            location = ngx_palloc(r->pool, r->uri.len + 1);
+            if (location == NULL) {
+                return NGX_HTTP_INTERNAL_SERVER_ERROR;
+            }
+
+            last = ngx_cpymem(location, r->uri.data, r->uri.len);
+        }
+
+        *last = '/';
+
         /*
          * we do not need to set the r->headers_out.location->hash and
          * r->headers_out.location->key fields
          */
 
-        r->headers_out.location->value = location;
+        r->headers_out.location->value.len = r->uri.len + 1;
+        r->headers_out.location->value.data = location;
 
         return NGX_HTTP_MOVED_PERMANENTLY;
     }
@@ -272,11 +209,11 @@
 
     if (!ngx_is_file(&fi)) {
         ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
-                      "\"%s\" is not a regular file", name.data);
+                      "\"%s\" is not a regular file", path.data);
 
         if (ngx_close_file(fd) == NGX_FILE_ERROR) {
             ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
-                          ngx_close_file_n " \"%s\" failed", name.data);
+                          ngx_close_file_n " \"%s\" failed", path.data);
         }
 
         return NGX_HTTP_NOT_FOUND;
@@ -292,7 +229,7 @@
     }
 
     cln->fd = fd;
-    cln->name = name.data;
+    cln->name = path.data;
     cln->log = r->pool->log;
 
     if (ngx_pool_cleanup_add(r->pool, ngx_pool_cleanup_file, cln) == NULL) {
@@ -349,7 +286,7 @@
     b->file_last = ngx_file_size(&fi);
 
     b->file->fd = fd;
-    b->file->name = name;
+    b->file->name = path;
     b->file->log = log;
 
     out.buf = b;
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 0643a87..4c6cfc0 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -911,37 +911,6 @@
 
 
 ngx_int_t
-ngx_http_send_header(ngx_http_request_t *r)
-{
-    if (r->err_status) {
-        r->headers_out.status = r->err_status;
-        r->headers_out.status_line.len = 0;
-    }
-
-    return ngx_http_top_header_filter(r);
-}
-
-
-ngx_int_t
-ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *in)
-{
-    ngx_int_t  rc;
-
-    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                   "http output filter \"%V\"", &r->uri);
-
-    rc = ngx_http_top_body_filter(r, in);
-
-    if (rc == NGX_ERROR) {
-        /* NGX_ERROR may be returned by any filter */
-        r->connection->closed = 1;
-    }
-
-    return rc;
-}
-
-
-ngx_int_t
 ngx_http_set_exten(ngx_http_request_t *r)
 {
     ngx_int_t  i;
@@ -977,6 +946,63 @@
 
 
 ngx_int_t
+ngx_http_send_header(ngx_http_request_t *r)
+{
+    if (r->err_status) {
+        r->headers_out.status = r->err_status;
+        r->headers_out.status_line.len = 0;
+    }
+
+    return ngx_http_top_header_filter(r);
+}
+
+
+ngx_int_t
+ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *in)
+{
+    ngx_int_t  rc;
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http output filter \"%V\"", &r->uri);
+
+    rc = ngx_http_top_body_filter(r, in);
+
+    if (rc == NGX_ERROR) {
+        /* NGX_ERROR may be returned by any filter */
+        r->connection->closed = 1;
+    }
+
+    return rc;
+}
+
+
+u_char *
+ngx_http_map_uri_to_path(ngx_http_request_t *r, ngx_str_t *path,
+    size_t reserved)
+{
+    u_char                    *last;
+    size_t                     alias;
+    ngx_http_core_loc_conf_t  *clcf;
+
+    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
+    alias = clcf->alias ? clcf->name.len : 0;
+
+    path->len = clcf->root.len + r->uri.len - alias + 1 + reserved;
+
+    path->data = ngx_palloc(r->pool, path->len);
+    if (path->data == NULL) {
+        return NULL;
+    }
+
+    last = ngx_cpymem(path->data, clcf->root.data, clcf->root.len);
+    last = ngx_cpystrn(last, r->uri.data + alias, r->uri.len - alias + 1);
+
+    return last;
+}
+
+
+ngx_int_t
 ngx_http_auth_basic_user(ngx_http_request_t *r)
 {
     ngx_str_t   auth, encoded;
diff --git a/src/http/ngx_http_core_module.h b/src/http/ngx_http_core_module.h
index d175f2e..4de8007 100644
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -253,6 +253,8 @@
 
 ngx_int_t ngx_http_set_content_type(ngx_http_request_t *r);
 ngx_int_t ngx_http_set_exten(ngx_http_request_t *r);
+u_char *ngx_http_map_uri_to_path(ngx_http_request_t *r, ngx_str_t *name,
+    size_t reserved);
 ngx_int_t ngx_http_auth_basic_user(ngx_http_request_t *r);
 
 ngx_int_t ngx_http_subrequest(ngx_http_request_t *r,
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index 704d373..19bc239 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -2234,7 +2234,7 @@
     rev = c->read;
     rev->handler = ngx_http_lingering_close_handler;
 
-    r->lingering_time = ngx_time() + clcf->lingering_time / 1000;
+    r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
     ngx_add_timer(rev, clcf->lingering_timeout);
 
     if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
diff --git a/src/http/ngx_http_variables.c b/src/http/ngx_http_variables.c
index 214015f..7b0d231 100644
--- a/src/http/ngx_http_variables.c
+++ b/src/http/ngx_http_variables.c
@@ -619,8 +619,6 @@
 static ngx_http_variable_value_t *
 ngx_http_variable_request_filename(ngx_http_request_t *r, uintptr_t data)
 {
-    u_char                     *p;
-    ngx_http_core_loc_conf_t   *clcf;
     ngx_http_variable_value_t  *vv;
 
     vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t));
@@ -630,30 +628,14 @@
 
     vv->value = 0;
 
-    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
-
-    if (!clcf->alias) {
-        vv->text.len = clcf->root.len + r->uri.len;
-        vv->text.data = ngx_palloc(r->pool, vv->text.len);
-        if (vv->text.data == NULL) {
-            return NULL;
-        }
-
-        p = ngx_cpymem(vv->text.data, clcf->root.data, clcf->root.len);
-        ngx_memcpy(p, r->uri.data, r->uri.len + 1);
-
-    } else {
-        vv->text.len = clcf->root.len + r->uri.len + 2 - clcf->name.len;
-        vv->text.data = ngx_palloc(r->pool, vv->text.len);
-        if (vv->text.data == NULL) {
-            return NULL;
-        }
-
-        p = ngx_cpymem(vv->text.data, clcf->root.data, clcf->root.len);
-        ngx_memcpy(p, r->uri.data + clcf->name.len,
-                   r->uri.len + 1 - clcf->name.len);
+    if (ngx_http_map_uri_to_path(r, &vv->text, 0) == NULL) {
+        return NULL;
     }
 
+    /* ngx_http_map_uri_to_path() allocates memory for terminating '\0' */
+
+    vv->text.len--;
+
     return vv;
 }