changeset 286:5bef04fc3fd5 NGINX_0_5_13

nginx 0.5.13 *) Feature: the COPY and MOVE methods. *) Bugfix: the ngx_http_realip_module set garbage for requests passed via keep-alive connection. *) Bugfix: nginx did not work on big-endian 64-bit Linux. Thanks to Andrei Nigmatulin. *) Bugfix: now when IMAP/POP3 proxy receives too long command it closes the connection right away, but not after timeout. *) Bugfix: if the "epoll" method was used and a client closed a connection prematurely, then nginx closed the connection after a send timeout only. *) Bugfix: nginx could not be built on platforms different from i386, amd64, sparc and ppc; bug appeared in 0.5.8.
author Igor Sysoev <http://sysoev.ru>
date Mon, 19 Feb 2007 00:00:00 +0300
parents 0e505c8b6528
children eaccdcfdfe41
files CHANGES CHANGES.ru src/core/nginx.h src/core/ngx_conf_file.c src/core/ngx_inet.c src/core/ngx_shmtx.h src/core/ngx_string.c src/core/ngx_string.h src/http/modules/ngx_http_addition_filter_module.c src/http/modules/ngx_http_charset_filter_module.c src/http/modules/ngx_http_dav_module.c src/http/modules/ngx_http_headers_filter_module.c src/http/modules/ngx_http_proxy_module.c src/http/modules/ngx_http_range_filter_module.c src/http/modules/ngx_http_realip_module.c src/http/modules/ngx_http_referer_module.c src/http/modules/ngx_http_ssi_filter_module.c src/http/modules/perl/nginx.pm src/http/ngx_http_core_module.c src/http/ngx_http_request.c src/http/ngx_http_request_body.c src/http/ngx_http_upstream.c src/http/ngx_http_variables.c src/imap/ngx_imap_auth_http_module.c src/imap/ngx_imap_handler.c src/os/unix/ngx_socket.c
diffstat 26 files changed, 990 insertions(+), 252 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES
+++ b/CHANGES
@@ -1,4 +1,25 @@
 
+Changes with nginx 0.5.13                                        19 Feb 2007
+
+    *) Feature: the COPY and MOVE methods.
+
+    *) Bugfix: the ngx_http_realip_module set garbage for requests passed 
+       via keep-alive connection.
+
+    *) Bugfix: nginx did not work on big-endian 64-bit Linux.
+       Thanks to Andrei Nigmatulin.
+
+    *) Bugfix: now when IMAP/POP3 proxy receives too long command it closes 
+       the connection right away, but not after timeout.
+
+    *) Bugfix: if the "epoll" method was used and a client closed a 
+       connection prematurely, then nginx closed the connection after a 
+       send timeout only.
+
+    *) Bugfix: nginx could not be built on platforms different from i386, 
+       amd64, sparc и ppc; bug appeared in 0.5.8.
+
+
 Changes with nginx 0.5.12                                        12 Feb 2007
 
     *) Bugfix: nginx could not be built on platforms different from i386, 
--- a/CHANGES.ru
+++ b/CHANGES.ru
@@ -1,4 +1,25 @@
 
+Изменения в nginx 0.5.13                                          19.02.2007
+
+    *) Добавление: методы COPY и MOVE.
+
+    *) Исправление: модуль ngx_http_realip_module устанавливал мусор для 
+       запросов, переданных по keep-alive соединению.
+
+    *) Исправление: nginx не работал на 64-битном big-endian Linux.
+       Спасибо Андрею Нигматулину.
+
+    *) Исправление: при получении слишком длинной команды IMAP/POP3-прокси 
+       теперь сразу закрывает соединение, а не по таймауту.
+
+    *) Исправление: если при использовании метода epoll клиент закрывал 
+       преждевременно соединение со своей стороны, то nginx закрывал это 
+       соединение только по истечении таймаута на передачу.
+
+    *) Исправление: nginx не собирался на платформах, отличных от i386, 
+       amd64, sparc и ppc; ошибка появилась в 0.5.8.
+
+
 Изменения в nginx 0.5.12                                          12.02.2007
 
     *) Исправление: nginx не собирался на платформах, отличных от i386, 
--- a/src/core/nginx.h
+++ b/src/core/nginx.h
@@ -8,7 +8,7 @@
 #define _NGINX_H_INCLUDED_
 
 
-#define NGINX_VERSION      "0.5.12"
+#define NGINX_VERSION      "0.5.13"
 #define NGINX_VER          "nginx/" NGINX_VERSION
 
 #define NGINX_VAR          "NGINX"
--- a/src/core/ngx_conf_file.c
+++ b/src/core/ngx_conf_file.c
@@ -883,10 +883,10 @@ ngx_conf_set_flag_slot(ngx_conf_t *cf, n
 
     value = cf->args->elts;
 
-    if (ngx_strcasecmp(value[1].data, "on") == 0) {
+    if (ngx_strcasecmp(value[1].data, (u_char *) "on") == 0) {
         *fp = 1;
 
-    } else if (ngx_strcasecmp(value[1].data, "off") == 0) {
+    } else if (ngx_strcasecmp(value[1].data, (u_char *) "off") == 0) {
         *fp = 0;
 
     } else {
--- a/src/core/ngx_inet.c
+++ b/src/core/ngx_inet.c
@@ -233,7 +233,7 @@ ngx_parse_url(ngx_conf_t *cf, ngx_url_t 
     len = u->url.len;
     p = u->url.data;
 
-    if (ngx_strncasecmp(p, "unix:", 5) == 0) {
+    if (ngx_strncasecmp(p, (u_char *) "unix:", 5) == 0) {
 
 #if (NGX_HAVE_UNIX_DOMAIN)
 
--- a/src/core/ngx_shmtx.h
+++ b/src/core/ngx_shmtx.h
@@ -58,6 +58,8 @@ ngx_shmtx_trylock(ngx_shmtx_t *mtx)
     }
 
     ngx_log_abort(err, ngx_trylock_fd_n " failed");
+
+    return 0;
 }
 
 
--- a/src/core/ngx_string.c
+++ b/src/core/ngx_string.c
@@ -428,6 +428,68 @@ ngx_vsnprintf(u_char *buf, size_t max, c
 }
 
 
+/*
+ * We use ngx_strcasecmp()/ngx_strncasecmp() for 7-bit ASCII string only,
+ * and implement our own ngx_strcasecmp()/ngx_strncasecmp()
+ * to avoid libc locale overhead.  Besides, we use the ngx_uint_t's
+ * instead of the u_char's, because they are slightly faster.
+ */
+
+ngx_int_t
+ngx_strcasecmp(u_char *s1, u_char *s2)
+{
+    ngx_uint_t  c1, c2;
+
+    for ( ;; ) {
+        c1 = (ngx_uint_t) *s1++;
+        c2 = (ngx_uint_t) *s2++;
+
+        c1  = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
+        c2  = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
+
+        if (c1 == c2) {
+
+            if (c1) {
+                continue;
+            }
+
+            return 0;
+        }
+
+        return c1 - c2;
+    }
+}
+
+
+ngx_int_t
+ngx_strncasecmp(u_char *s1, u_char *s2, size_t n)
+{
+    ngx_uint_t  c1, c2;
+
+    while (n) {
+        c1 = (ngx_uint_t) *s1++;
+        c2 = (ngx_uint_t) *s2++;
+
+        c1  = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
+        c2  = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
+
+        if (c1 == c2) {
+
+            if (c1) {
+                n--;
+                continue;
+            }
+
+            return 0;
+        }
+
+        return c1 - c2;
+    }
+
+    return 0;
+}
+
+
 ngx_int_t
 ngx_rstrncmp(u_char *s1, u_char *s2, size_t n)
 {
--- a/src/core/ngx_string.h
+++ b/src/core/ngx_string.h
@@ -24,29 +24,12 @@ typedef struct {
 } ngx_keyval_t;
 
 
-#define ngx_string(str)  { sizeof(str) - 1, (u_char *) str }
-#define ngx_null_string  { 0, NULL }
-
-
-#define ngx_tolower(c)     (u_char) ((c >= 'A' && c <= 'Z') ? (c | 0x20) : c)
-#define ngx_toupper(c)     (u_char) ((c >= 'a' && c <= 'z') ? (c & ~0x20) : c)
+#define ngx_string(str)     { sizeof(str) - 1, (u_char *) str }
+#define ngx_null_string     { 0, NULL }
 
 
-#if (NGX_WIN32)
-
-#define ngx_strncasecmp(s1, s2, n)                                           \
-                           strnicmp((const char *) s1, (const char *) s2, n)
-#define ngx_strcasecmp(s1, s2)                                               \
-                           stricmp((const char *) s1, (const char *) s2)
-
-#else
-
-#define ngx_strncasecmp(s1, s2, n)                                           \
-                           strncasecmp((const char *) s1, (const char *) s2, n)
-#define ngx_strcasecmp(s1, s2)                                               \
-                           strcasecmp((const char *) s1, (const char *) s2)
-
-#endif
+#define ngx_tolower(c)      (u_char) ((c >= 'A' && c <= 'Z') ? (c | 0x20) : c)
+#define ngx_toupper(c)      (u_char) ((c >= 'a' && c <= 'z') ? (c & ~0x20) : c)
 
 
 #define ngx_strncmp(s1, s2, n)  strncmp((const char *) s1, (const char *) s2, n)
@@ -128,6 +111,9 @@ u_char * ngx_cdecl ngx_sprintf(u_char *b
 u_char * ngx_cdecl ngx_snprintf(u_char *buf, size_t max, const char *fmt, ...);
 u_char *ngx_vsnprintf(u_char *buf, size_t max, const char *fmt, va_list args);
 
+ngx_int_t ngx_strcasecmp(u_char *s1, u_char *s2);
+ngx_int_t ngx_strncasecmp(u_char *s1, u_char *s2, size_t n);
+
 ngx_int_t ngx_rstrncmp(u_char *s1, u_char *s2, size_t n);
 ngx_int_t ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n);
 ngx_int_t ngx_memn2cmp(u_char *s1, u_char *s2, size_t n1, size_t n2);
--- a/src/http/modules/ngx_http_addition_filter_module.c
+++ b/src/http/modules/ngx_http_addition_filter_module.c
@@ -100,8 +100,8 @@ ngx_http_addition_header_filter(ngx_http
         return ngx_http_next_header_filter(r);
     }
 
-    if (ngx_strncasecmp(r->headers_out.content_type.data, "text/html",
-                        sizeof("text/html") - 1)
+    if (ngx_strncasecmp(r->headers_out.content_type.data,
+                        (u_char *) "text/html", sizeof("text/html") - 1)
         != 0)
     {
         return ngx_http_next_header_filter(r);
--- a/src/http/modules/ngx_http_charset_filter_module.c
+++ b/src/http/modules/ngx_http_charset_filter_module.c
@@ -239,8 +239,10 @@ ngx_http_charset_header_filter(ngx_http_
             } else {
                 ct = r->headers_out.content_type.data;
 
-                if (ngx_strncasecmp(ct, "text/", 5) != 0
-                    && ngx_strncasecmp(ct, "application/x-javascript", 24) != 0)
+                if (ngx_strncasecmp(ct, (u_char *) "text/", 5) != 0
+                    && ngx_strncasecmp(ct,
+                                      (u_char *) "application/x-javascript", 24)
+                       != 0)
                 {
                     return ngx_http_next_header_filter(r);
                 }
@@ -1118,7 +1120,7 @@ ngx_http_charset_map_block(ngx_conf_t *c
     table->src = src;
     table->dst = dst;
 
-    if (ngx_strcasecmp(value[2].data, "utf-8") == 0) {
+    if (ngx_strcasecmp(value[2].data, (u_char *) "utf-8") == 0) {
         table->src2dst = ngx_pcalloc(cf->pool, 256 * NGX_UTF_LEN);
         if (table->src2dst == NULL) {
             return NGX_CONF_ERROR;
@@ -1372,7 +1374,7 @@ ngx_http_add_charset(ngx_array_t *charse
     c->name = *name;
     c->length = 0;
 
-    if (ngx_strcasecmp(name->data, "utf-8") == 0) {
+    if (ngx_strcasecmp(name->data, (u_char *) "utf-8") == 0) {
         c->utf8 = 1;
 
     } else {
--- a/src/http/modules/ngx_http_dav_module.c
+++ b/src/http/modules/ngx_http_dav_module.c
@@ -9,7 +9,15 @@
 #include <ngx_http.h>
 
 
-#define NGX_HTTP_DAV_OFF   2
+#define NGX_HTTP_DAV_COPY_BLOCK      65536
+
+#define NGX_HTTP_DAV_OFF             2
+
+
+#define NGX_HTTP_DAV_NO_DEPTH        -3
+#define NGX_HTTP_DAV_INVALID_DEPTH   -2
+#define NGX_HTTP_DAV_INFINITY_DEPTH  -1
+
 
 typedef struct {
     ngx_uint_t  methods;
@@ -18,13 +26,35 @@ typedef struct {
 } ngx_http_dav_loc_conf_t;
 
 
+typedef struct {
+    ngx_str_t   path;
+    size_t      len;
+} ngx_http_dav_copy_ctx_t;
+
+
 static ngx_int_t ngx_http_dav_handler(ngx_http_request_t *r);
+
+static void ngx_http_dav_put_handler(ngx_http_request_t *r);
+
+static ngx_int_t ngx_http_dav_delete_handler(ngx_http_request_t *r);
 static ngx_int_t ngx_http_dav_no_init(void *ctx, void *prev);
 static ngx_int_t ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path);
 static ngx_int_t ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path);
 static ngx_int_t ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path);
-static void ngx_http_dav_put_handler(ngx_http_request_t *r);
-static ngx_int_t ngx_http_dav_error(ngx_http_request_t *, ngx_err_t err,
+
+static ngx_int_t ngx_http_dav_mkcol_handler(ngx_http_request_t *r,
+    ngx_http_dav_loc_conf_t *dlcf);
+
+static ngx_int_t ngx_http_dav_copy_move_handler(ngx_http_request_t *r);
+static ngx_int_t ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path);
+static ngx_int_t ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx,
+    ngx_str_t *path);
+static ngx_int_t ngx_http_dav_copy_file(ngx_tree_ctx_t *ctx, ngx_str_t *path);
+
+static ngx_int_t ngx_http_dav_delete_path(ngx_http_request_t *r,
+    ngx_str_t *path, ngx_uint_t dir);
+static ngx_int_t ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt);
+static ngx_int_t ngx_http_dav_error(ngx_log_t *log, ngx_err_t err,
     ngx_int_t not_found, char *failed, u_char *path);
 static ngx_int_t ngx_http_dav_location(ngx_http_request_t *r, u_char *path);
 static char *ngx_http_dav_access(ngx_conf_t *cf, ngx_command_t *cmd,
@@ -40,6 +70,8 @@ static ngx_conf_bitmask_t  ngx_http_dav_
     { ngx_string("put"), NGX_HTTP_PUT },
     { ngx_string("delete"), NGX_HTTP_DELETE },
     { ngx_string("mkcol"), NGX_HTTP_MKCOL },
+    { ngx_string("copy"), NGX_HTTP_COPY },
+    { ngx_string("move"), NGX_HTTP_MOVE },
     { ngx_null_string, 0 }
 };
 
@@ -105,13 +137,7 @@ ngx_module_t  ngx_http_dav_module = {
 static ngx_int_t
 ngx_http_dav_handler(ngx_http_request_t *r)
 {
-    char                     *failed;
-    size_t                    root;
     ngx_int_t                 rc;
-    ngx_str_t                 path;
-    ngx_tree_ctx_t            tree;
-    ngx_file_info_t           fi;
-    ngx_table_elt_t          *depth;
     ngx_http_dav_loc_conf_t  *dlcf;
 
     /* TODO: Win32 */
@@ -149,165 +175,25 @@ ngx_http_dav_handler(ngx_http_request_t 
 
     case NGX_HTTP_DELETE:
 
-        if (r->headers_in.content_length_n > 0) {
-            return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
-        }
-
-        rc = ngx_http_discard_body(r);
-
-        if (rc != NGX_OK && rc != NGX_AGAIN) {
-            return rc;
-        }
-
-        ngx_http_map_uri_to_path(r, &path, &root, 0);
-
-        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                       "http delete filename: \"%s\"", path.data);
-
-        if (ngx_file_info(path.data, &fi) != -1) {
-
-            if (ngx_is_dir(&fi)) {
-
-                if (r->uri.data[r->uri.len - 1] != '/') {
-                    return NGX_HTTP_BAD_REQUEST;
-                }
-
-                depth = r->headers_in.depth;
-
-                if (depth
-                    && (depth->value.len != sizeof("infinity") - 1
-                        || ngx_strcmp(depth->value.data, "infinity") != 0))
-                {
-                    return NGX_HTTP_BAD_REQUEST;
-                }
-
-                path.len -= 2;
-
-                tree.init_handler = ngx_http_dav_no_init;
-                tree.file_handler = ngx_http_dav_delete_file;
-                tree.pre_tree_handler = ngx_http_dav_noop;
-                tree.post_tree_handler = ngx_http_dav_delete_dir;
-                tree.spec_handler = ngx_http_dav_delete_file;
-                tree.data = NULL;
-                tree.alloc = 0;
-                tree.log = r->connection->log;
-
-                if (ngx_walk_tree(&tree, &path) == NGX_OK) {
-
-                    if (ngx_delete_dir(path.data) != NGX_FILE_ERROR) {
-                        return NGX_HTTP_NO_CONTENT;
-                    }
-
-                }
-
-                failed = ngx_delete_dir_n;
-
-            } else {
-
-                if (r->uri.data[r->uri.len - 1] == '/') {
-                    return NGX_HTTP_BAD_REQUEST;
-                }
-
-                depth = r->headers_in.depth;
-
-                if (depth
-                    && depth->value.len == 1
-                    && depth->value.data[0] == '1')
-                {
-                    return NGX_HTTP_BAD_REQUEST;
-                }
-
-                if (ngx_delete_file(path.data) != NGX_FILE_ERROR) {
-                    return NGX_HTTP_NO_CONTENT;
-                }
-
-                failed = ngx_delete_file_n;
-            }
-
-        } else {
-            failed = ngx_file_info_n;
-        }
-
-        return ngx_http_dav_error(r, ngx_errno, NGX_HTTP_NOT_FOUND, failed,
-                                  path.data);
+        return ngx_http_dav_delete_handler(r);
 
     case NGX_HTTP_MKCOL:
 
-        if (r->uri.data[r->uri.len - 1] != '/') {
-            return NGX_DECLINED;
-        }
-
-        if (r->headers_in.content_length_n > 0) {
-            return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
-        }
+        return ngx_http_dav_mkcol_handler(r, dlcf);
 
-        rc = ngx_http_discard_body(r);
-
-        if (rc != NGX_OK && rc != NGX_AGAIN) {
-            return rc;
-        }
-
-        ngx_http_map_uri_to_path(r, &path, &root, 0);
+    case NGX_HTTP_COPY:
 
-        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                       "http mkcol path: \"%s\"", path.data);
+        return ngx_http_dav_copy_move_handler(r);
 
-        if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access))
-            != NGX_FILE_ERROR)
-        {
-            if (ngx_http_dav_location(r, path.data) != NGX_OK) {
-                return NGX_HTTP_INTERNAL_SERVER_ERROR;
-            }
+    case NGX_HTTP_MOVE:
 
-            return NGX_HTTP_CREATED;
-        }
-
-        return ngx_http_dav_error(r, ngx_errno, NGX_HTTP_CONFLICT,
-                                  ngx_create_dir_n, path.data);
+        return ngx_http_dav_copy_move_handler(r);
     }
 
     return NGX_DECLINED;
 }
 
 
-static ngx_int_t
-ngx_http_dav_no_init(void *ctx, void *prev)
-{
-    return NGX_OK;
-}
-
-
-static ngx_int_t
-ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path)
-{
-    return NGX_OK;
-}
-
-
-static ngx_int_t
-ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path)
-{
-    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
-                   "http delete dir: \"%s\"", path->data);
-
-    ngx_delete_dir(path->data);
-
-    return NGX_OK;
-}
-
-
-static ngx_int_t
-ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
-{
-    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
-                   "http delete file: \"%s\"", path->data);
-
-    ngx_delete_file(path->data);
-
-    return NGX_OK;
-}
-
-
 static void
 ngx_http_dav_put_handler(ngx_http_request_t *r)
 {
@@ -317,7 +203,7 @@ ngx_http_dav_put_handler(ngx_http_reques
     time_t                    date;
     ngx_err_t                 err;
     ngx_str_t                *temp, path;
-    ngx_uint_t                status;
+    ngx_uint_t                status, not_found;
     ngx_file_info_t           fi;
     ngx_http_dav_loc_conf_t  *dlcf;
 
@@ -357,6 +243,7 @@ ngx_http_dav_put_handler(ngx_http_reques
         == NGX_FILE_ERROR)
     {
         err = ngx_errno;
+        not_found = NGX_HTTP_INTERNAL_SERVER_ERROR;
         failed = ngx_change_file_access_n;
         name = temp->data;
 
@@ -375,6 +262,7 @@ ngx_http_dav_put_handler(ngx_http_reques
                 != NGX_OK)
             {
                 err = ngx_errno;
+                not_found = NGX_HTTP_INTERNAL_SERVER_ERROR;
                 failed = ngx_set_file_time_n;
                 name = temp->data;
 
@@ -383,6 +271,7 @@ ngx_http_dav_put_handler(ngx_http_reques
         }
     }
 
+    not_found = NGX_HTTP_CONFLICT;
     failed = ngx_rename_file_n;
     name = path.data;
 
@@ -431,7 +320,8 @@ failed:
     }
 
     ngx_http_finalize_request(r,
-                  ngx_http_dav_error(r, err, NGX_HTTP_CONFLICT, failed, name));
+                              ngx_http_dav_error(r->connection->log, err,
+                                                 not_found, failed, name));
 
     return;
 
@@ -455,7 +345,700 @@ ok:
 
 
 static ngx_int_t
-ngx_http_dav_error(ngx_http_request_t *r, ngx_err_t err, ngx_int_t not_found,
+ngx_http_dav_delete_handler(ngx_http_request_t *r)
+{
+    size_t           root;
+    ngx_int_t        rc, depth;
+    ngx_uint_t       dir;
+    ngx_str_t        path;
+    ngx_file_info_t  fi;
+
+    if (r->headers_in.content_length_n > 0) {
+        return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
+    }
+
+    rc = ngx_http_discard_body(r);
+
+    if (rc != NGX_OK && rc != NGX_AGAIN) {
+        return rc;
+    }
+
+    ngx_http_map_uri_to_path(r, &path, &root, 0);
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http delete filename: \"%s\"", path.data);
+
+    if (ngx_file_info(path.data, &fi) == -1) {
+        return ngx_http_dav_error(r->connection->log, ngx_errno,
+                                  NGX_HTTP_NOT_FOUND, ngx_file_info_n,
+                                  path.data);
+    }
+
+    if (ngx_is_dir(&fi)) {
+
+        if (r->uri.data[r->uri.len - 1] != '/') {
+            /* TODO: 301 */
+            return NGX_HTTP_BAD_REQUEST;
+        }
+
+        depth = ngx_http_dav_depth(r, NGX_HTTP_DAV_INFINITY_DEPTH);
+
+        if (depth != NGX_HTTP_DAV_INFINITY_DEPTH) {
+            return NGX_HTTP_BAD_REQUEST;
+        }
+
+        path.len -= 2;  /* omit "/\0" */
+
+        dir = 1;
+
+    } else {
+
+        depth = ngx_http_dav_depth(r, 0);
+
+        if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) {
+            return NGX_HTTP_BAD_REQUEST;
+        }
+
+        dir = 0;
+    }
+
+    rc = ngx_http_dav_delete_path(r, &path, dir);
+
+    if (rc == NGX_OK) {
+        return NGX_HTTP_NO_CONTENT;
+    }
+
+    return rc;
+}
+
+
+static ngx_int_t
+ngx_http_dav_no_init(void *ctx, void *prev)
+{
+    return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_dav_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path)
+{
+    return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_dav_delete_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path)
+{
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
+                   "http delete dir: \"%s\"", path->data);
+
+    if (ngx_delete_dir(path->data) == NGX_FILE_ERROR) {
+
+        /* TODO: add to 207 */
+
+        (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_dir_n,
+                                  path->data);
+    }
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_dav_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
+{
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
+                   "http delete file: \"%s\"", path->data);
+
+    if (ngx_delete_file(path->data) == NGX_FILE_ERROR) {
+
+        /* TODO: add to 207 */
+
+        (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_delete_file_n,
+                                  path->data);
+    }
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_dav_mkcol_handler(ngx_http_request_t *r, ngx_http_dav_loc_conf_t *dlcf)
+{
+    size_t     root;
+    ngx_int_t  rc;
+    ngx_str_t  path;
+
+    if (r->headers_in.content_length_n > 0) {
+        return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
+    }
+
+    rc = ngx_http_discard_body(r);
+
+    if (rc != NGX_OK && rc != NGX_AGAIN) {
+        return rc;
+    }
+
+    ngx_http_map_uri_to_path(r, &path, &root, 0);
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http mkcol path: \"%s\"", path.data);
+
+    if (ngx_create_dir(path.data, ngx_dir_access(dlcf->access))
+        != NGX_FILE_ERROR)
+    {
+        if (ngx_http_dav_location(r, path.data) != NGX_OK) {
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
+        }
+
+        return NGX_HTTP_CREATED;
+    }
+
+    return ngx_http_dav_error(r->connection->log, ngx_errno,
+                              NGX_HTTP_BAD_REQUEST, ngx_create_dir_n,
+                              path.data);
+}
+
+
+static ngx_int_t
+ngx_http_dav_copy_move_handler(ngx_http_request_t *r)
+{
+    u_char                   *p, *host, *last, ch;
+    size_t                    root;
+    ngx_err_t                 err;
+    ngx_int_t                 rc, depth;
+    ngx_uint_t                overwrite, slash;
+    ngx_str_t                 path, uri;
+    ngx_tree_ctx_t            tree;
+    ngx_file_info_t           fi;
+    ngx_table_elt_t          *dest, *over;
+    ngx_http_dav_copy_ctx_t   copy;
+
+    if (r->headers_in.content_length_n > 0) {
+        return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
+    }
+
+    dest = r->headers_in.destination;
+
+    if (dest == NULL) {
+        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                      "client sent no \"Destination\" header");
+        return NGX_HTTP_BAD_REQUEST;
+    }
+
+    if (dest->value.len < sizeof("http://") - 1 + r->server_name.len + 1) {
+        goto invalid_destination;
+    }
+
+#if (NGX_HTTP_SSL)
+
+    if (r->connection->ssl) {
+        if (ngx_strncmp(dest->value.data, "https://", sizeof("https://") - 1)
+            != 0)
+        {
+            goto invalid_destination;
+        }
+
+        host = dest->value.data + sizeof("https://") - 1;
+
+    } else
+#endif
+    {
+        if (ngx_strncmp(dest->value.data, "http://", sizeof("http://") - 1)
+            != 0)
+        {
+            goto invalid_destination;
+        }
+
+        host = dest->value.data + sizeof("http://") - 1;
+    }
+
+    if (ngx_strncmp(host, r->server_name.data, r->server_name.len) != 0) {
+        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                      "Destination URI \"%V\" is handled by "
+                      "different repository than the source URI",
+                      &dest->value);
+
+        return NGX_HTTP_BAD_REQUEST;
+    }
+
+    last = dest->value.data + dest->value.len;
+
+    for (p = host + r->server_name.len; p < last; p++) {
+        if (*p == '/') {
+            goto destination_done;
+        }
+    }
+
+invalid_destination:
+
+    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                  "client sent invalid \"Destination\" header: \"%V\"",
+                  &dest->value);
+
+    return NGX_HTTP_BAD_REQUEST;
+
+destination_done:
+
+    depth = ngx_http_dav_depth(r, 0);
+
+    if (depth != 0 && depth != NGX_HTTP_DAV_INFINITY_DEPTH) {
+        return NGX_HTTP_BAD_REQUEST;
+    }
+ 
+    over = r->headers_in.overwrite;
+
+    if (over) {
+        if (over->value.len == 1) {
+            ch = over->value.data[0];
+
+            if (ch == 'T' || ch == 't') {
+                overwrite = 1;
+                goto overwrite_done;
+            }
+
+            if (ch == 'F' || ch == 'f') {
+                overwrite = 0;
+                goto overwrite_done;
+            }
+
+        }
+
+        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                      "client sent invalid \"Overwrite\" header: \"%V\"",
+                      &over->value);
+
+        return NGX_HTTP_BAD_REQUEST;
+    }
+
+    overwrite = 1;
+
+overwrite_done:
+
+    rc = ngx_http_discard_body(r);
+
+    if (rc != NGX_OK && rc != NGX_AGAIN) {
+        return rc;
+    }
+
+    ngx_http_map_uri_to_path(r, &path, &root, 0);
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http copy from: \"%s\"", path.data);
+
+    uri = r->uri;
+
+    r->uri.len = last - p;
+    r->uri.data = p;
+
+    ngx_http_map_uri_to_path(r, &copy.path, &root, 0);
+
+    r->uri = uri;
+
+    copy.path.len--;  /* omit "\0" */
+
+    if (copy.path.data[copy.path.len - 1] == '/') {
+        slash = 1;
+        copy.path.len--;
+        copy.path.data[copy.path.len] = '\0';
+
+    } else {
+        slash = 0;
+    }
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http copy to: \"%s\"", copy.path.data);
+
+    if (ngx_file_info(copy.path.data, &fi) == -1) {
+        err = ngx_errno;
+
+        if (err != NGX_ENOENT) {
+            return ngx_http_dav_error(r->connection->log, err,
+                                      NGX_HTTP_NOT_FOUND, ngx_file_info_n,
+                                      copy.path.data);
+        }
+
+        /* destination does not exist */
+
+    } else {
+
+        /* destination exists */
+
+        if (ngx_is_dir(&fi) && !slash) {
+            return NGX_HTTP_CONFLICT;
+        }
+
+        if (!overwrite) {
+            return NGX_HTTP_PRECONDITION_FAILED;
+        }
+
+        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                       "http delete: \"%s\"", copy.path.data);
+
+        rc = ngx_http_dav_delete_path(r, &copy.path, ngx_is_dir(&fi));
+
+        if (rc != NGX_OK) {
+            return rc;
+        }
+    }
+
+    if (ngx_file_info(path.data, &fi) == -1) {
+        return ngx_http_dav_error(r->connection->log, ngx_errno,
+                                  NGX_HTTP_NOT_FOUND, ngx_file_info_n,
+                                  path.data);
+    }
+
+
+    if (ngx_is_dir(&fi)) {
+
+        if (r->uri.data[r->uri.len - 1] != '/') {
+            /* TODO: 301 */
+            return NGX_HTTP_BAD_REQUEST;
+        }
+
+        path.len -= 2;  /* omit "/\0" */
+
+        if (r->method == NGX_HTTP_MOVE) {
+            if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) {
+                return NGX_HTTP_CREATED;
+            }
+        }
+
+        if (ngx_create_dir(copy.path.data, ngx_file_access(&fi))
+            == NGX_FILE_ERROR)
+        {
+            return ngx_http_dav_error(r->connection->log, ngx_errno,
+                                      NGX_HTTP_NOT_FOUND,
+                                      ngx_create_dir_n, copy.path.data);
+        }
+
+        copy.len = path.len;
+
+        tree.init_handler = ngx_http_dav_no_init;
+        tree.file_handler = ngx_http_dav_copy_file;
+        tree.pre_tree_handler = ngx_http_dav_copy_dir;
+        tree.post_tree_handler = ngx_http_dav_copy_dir_time;
+        tree.spec_handler = ngx_http_dav_noop;
+        tree.data = &copy;
+        tree.alloc = 0;
+        tree.log = r->connection->log;
+
+        if (ngx_walk_tree(&tree, &path) == NGX_OK) {
+
+            if (r->method == NGX_HTTP_MOVE) {
+                rc = ngx_http_dav_delete_path(r, &path, 1);
+
+                if (rc != NGX_OK) {
+                    return rc;
+                }
+            }
+
+            return NGX_HTTP_CREATED;
+        }
+
+    } else {
+
+        if (dest->value.data[dest->value.len - 1] == '/') {
+            return NGX_HTTP_BAD_REQUEST;
+        }
+
+        if (r->method == NGX_HTTP_MOVE) {
+            if (ngx_rename_file(path.data, copy.path.data) != NGX_FILE_ERROR) {
+                return NGX_HTTP_NO_CONTENT;
+            }
+        }
+
+        tree.data = &copy;
+        tree.log = r->connection->log;
+
+        if (ngx_http_dav_copy_file(&tree, &path) != NGX_FILE_ERROR) {
+
+            if (r->method == NGX_HTTP_MOVE) {
+                rc = ngx_http_dav_delete_path(r, &path, 0);
+
+                if (rc != NGX_OK) {
+                    return rc;
+                }
+            }
+
+            return NGX_HTTP_NO_CONTENT;
+        }
+    }
+
+    return NGX_HTTP_INTERNAL_SERVER_ERROR;
+}
+
+
+static ngx_int_t
+ngx_http_dav_copy_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path)
+{
+    u_char                   *p, *dir;
+    size_t                    len;
+    ngx_http_dav_copy_ctx_t  *copy;
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
+                   "http copy dir: \"%s\"", path->data);
+
+    copy = ctx->data;
+
+    len = copy->path.len + path->len;
+
+    dir = ngx_alloc(len + 1, ctx->log);
+    if (dir == NULL) {
+        return NGX_ABORT;
+    }
+
+    p = ngx_cpymem(dir, copy->path.data, copy->path.len);
+    (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1);
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
+                   "http copy dir to: \"%s\"", dir);
+
+    if (ngx_create_dir(dir, ngx_dir_access(ctx->access)) == NGX_FILE_ERROR) {
+        (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_create_dir_n,
+                                  dir);
+    }
+
+    ngx_free(dir);
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_dav_copy_dir_time(ngx_tree_ctx_t *ctx, ngx_str_t *path)
+{
+    u_char                   *p, *dir;
+    size_t                    len;
+    ngx_http_dav_copy_ctx_t  *copy;
+#if (WIN32)
+    ngx_fd_t                  fd;
+#endif
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
+                   "http copy dir time: \"%s\"", path->data);
+
+    copy = ctx->data;
+
+    len = copy->path.len + path->len;
+
+    dir = ngx_alloc(len + 1, ctx->log);
+    if (dir == NULL) {
+        return NGX_ABORT;
+    }
+
+    p = ngx_cpymem(dir, copy->path.data, copy->path.len);
+    (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1);
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
+                   "http copy dir time to: \"%s\"", dir);
+
+#if (WIN32)
+
+    fd = ngx_open_file(dir, NGX_FILE_RDWR, NGX_FILE_OPEN, 0);
+
+    if (fd == NGX_INVALID_FILE) {
+        (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n, dir);
+        goto failed;
+    }
+
+    if (ngx_set_file_time(NULL, fd, ctx->mtime) != NGX_OK) {
+        ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
+                      ngx_set_file_time_n " \"%s\" failed", dir);
+    }
+
+    if (ngx_close_file(fd) == NGX_FILE_ERROR) {
+        ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
+                      ngx_close_file_n " \"%s\" failed", dir);
+    }
+
+failed:
+
+#else
+
+    if (ngx_set_file_time(dir, 0, ctx->mtime) != NGX_OK) {
+        ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
+                      ngx_set_file_time_n " \"%s\" failed", dir);
+    }
+
+#endif
+
+    ngx_free(dir);
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_dav_copy_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
+{
+    u_char                   *p, *file;
+    size_t                    len;
+    off_t                     size;
+    ssize_t                   n;
+    ngx_fd_t                  fd, copy_fd;
+    ngx_http_dav_copy_ctx_t  *copy;
+    u_char                    buf[NGX_HTTP_DAV_COPY_BLOCK];
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
+                   "http copy file: \"%s\"", path->data);
+
+    copy = ctx->data;
+
+    len = copy->path.len + path->len;
+
+    file = ngx_alloc(len + 1, ctx->log);
+    if (file == NULL) {
+        return NGX_ABORT;
+    }
+
+    p = ngx_cpymem(file, copy->path.data, copy->path.len);
+    (void) ngx_cpystrn(p, path->data + copy->len, path->len - copy->len + 1);
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
+                   "http copy file to: \"%s\"", file);
+
+    fd = ngx_open_file(path->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
+
+    if (fd == NGX_INVALID_FILE) {
+        (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n,
+                                  path->data);
+        goto failed;
+    }
+
+    copy_fd = ngx_open_file(file, NGX_FILE_WRONLY, NGX_FILE_CREATE_OR_OPEN,
+                            ctx->access);
+
+    if (copy_fd == NGX_INVALID_FILE) {
+        (void) ngx_http_dav_error(ctx->log, ngx_errno, 0, ngx_open_file_n,
+                                  file);
+        goto copy_failed;
+    }
+
+    for (size = ctx->size; size > 0; size -= n) {
+
+        n = ngx_read_fd(fd, buf, NGX_HTTP_DAV_COPY_BLOCK);
+
+        if (n == NGX_FILE_ERROR) {
+            ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
+                          ngx_read_fd_n " \"%s\" failed", path->data);
+            break;
+        }
+
+        if (ngx_write_fd(copy_fd, buf, n) == NGX_FILE_ERROR) {
+            ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
+                          ngx_write_fd_n " \"%s\" failed", file);
+        }
+    }
+
+    if (ngx_set_file_time(file, copy_fd, ctx->mtime) != NGX_OK) {
+        ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
+                      ngx_set_file_time_n " \"%s\" failed", file);
+    }
+
+    if (ngx_close_file(copy_fd) == NGX_FILE_ERROR) {
+        ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
+                      ngx_close_file_n " \"%s\" failed", file);
+    }
+
+copy_failed:
+
+    if (ngx_close_file(fd) == NGX_FILE_ERROR) {
+        ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
+                      ngx_close_file_n " \"%s\" failed", path->data);
+    }
+
+failed:
+
+    ngx_free(file);
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_dav_delete_path(ngx_http_request_t *r, ngx_str_t *path, ngx_uint_t dir)
+{
+    char            *failed;
+    ngx_tree_ctx_t   tree;
+
+    if (dir) {
+
+        tree.init_handler = ngx_http_dav_no_init;
+        tree.file_handler = ngx_http_dav_delete_file;
+        tree.pre_tree_handler = ngx_http_dav_noop;
+        tree.post_tree_handler = ngx_http_dav_delete_dir;
+        tree.spec_handler = ngx_http_dav_delete_file;
+        tree.data = NULL;
+        tree.alloc = 0;
+        tree.log = r->connection->log;
+
+        /* todo: 207 */
+
+        if (ngx_walk_tree(&tree, path) != NGX_OK) {
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
+        }
+
+        if (ngx_delete_dir(path->data) != NGX_FILE_ERROR) {
+            return NGX_OK;
+        }
+
+        failed = ngx_delete_dir_n;
+
+    } else {
+
+        if (ngx_delete_file(path->data) != NGX_FILE_ERROR) {
+            return NGX_OK;
+        }
+
+        failed = ngx_delete_file_n;
+    }
+
+    return ngx_http_dav_error(r->connection->log, ngx_errno,
+                              NGX_HTTP_NOT_FOUND, failed, path->data);
+}
+
+
+static ngx_int_t
+ngx_http_dav_depth(ngx_http_request_t *r, ngx_int_t dflt)
+{
+    ngx_table_elt_t  *depth;
+ 
+    depth = r->headers_in.depth;
+
+    if (depth == NULL) {
+        return dflt;
+    }
+
+    if (depth->value.len == 1) {
+
+        if (depth->value.data[0] == '0') {
+            return 0;
+        }
+
+        if (depth->value.data[0] == '1') {
+            return 1;
+        }
+
+    } else {
+
+        if (depth->value.len == sizeof("infinity") - 1
+            && ngx_strcmp(depth->value.data, "infinity") == 0)
+        {
+            return NGX_HTTP_DAV_INFINITY_DEPTH;
+        }
+    }
+
+    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                  "client sent invalid \"Depth\" header: \"%V\"",
+                  &depth->value);
+
+    return NGX_HTTP_DAV_INVALID_DEPTH;
+}
+
+
+static ngx_int_t
+ngx_http_dav_error(ngx_log_t *log, ngx_err_t err, ngx_int_t not_found,
     char *failed, u_char *path)
 {
     ngx_int_t   rc;
@@ -482,8 +1065,7 @@ ngx_http_dav_error(ngx_http_request_t *r
         rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
 
-    ngx_log_error(level, r->connection->log, err,
-                  "%s \"%s\" failed", failed, path);
+    ngx_log_error(level, log, err, "%s \"%s\" failed", failed, path);
 
     return rc;
 }
--- a/src/http/modules/ngx_http_headers_filter_module.c
+++ b/src/http/modules/ngx_http_headers_filter_module.c
@@ -411,7 +411,7 @@ ngx_http_headers_add(ngx_conf_t *cf, ngx
 
     value = cf->args->elts;
 
-    if (ngx_strcasecmp(value[1].data, "cache-control") == 0) {
+    if (ngx_strcasecmp(value[1].data, (u_char *) "cache-control") == 0) {
         hcf->cache_control = value[2];
         return NGX_CONF_OK;
     }
--- a/src/http/modules/ngx_http_proxy_module.c
+++ b/src/http/modules/ngx_http_proxy_module.c
@@ -2111,11 +2111,11 @@ ngx_http_proxy_pass(ngx_conf_t *cf, ngx_
 
     url = &value[1];
 
-    if (ngx_strncasecmp(url->data, "http://", 7) == 0) {
+    if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) {
         add = 7;
         port = 80;
 
-    } else if (ngx_strncasecmp(url->data, "https://", 8) == 0) {
+    } else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) {
 
 #if (NGX_HTTP_SSL)
 
--- a/src/http/modules/ngx_http_range_filter_module.c
+++ b/src/http/modules/ngx_http_range_filter_module.c
@@ -152,7 +152,9 @@ ngx_http_range_header_filter(ngx_http_re
 
     if (r->headers_in.range == NULL
         || r->headers_in.range->value.len < 7
-        || ngx_strncasecmp(r->headers_in.range->value.data, "bytes=", 6) != 0)
+        || ngx_strncasecmp(r->headers_in.range->value.data,
+                           (u_char *) "bytes=", 6)
+           != 0)
     {
         r->headers_out.accept_ranges = ngx_list_push(&r->headers_out.headers);
         if (r->headers_out.accept_ranges == NULL) {
--- a/src/http/modules/ngx_http_realip_module.c
+++ b/src/http/modules/ngx_http_realip_module.c
@@ -97,6 +97,7 @@ ngx_http_realip_handler(ngx_http_request
 {
     u_char                      *ip, *p;
     size_t                       len;
+    in_addr_t                    addr;
     ngx_uint_t                   i;
     struct sockaddr_in          *sin;
     ngx_http_realip_from_t      *from;
@@ -128,16 +129,19 @@ ngx_http_realip_handler(ngx_http_request
         len = r->headers_in.x_forwarded_for->value.len;
         ip = r->headers_in.x_forwarded_for->value.data;
 
-        for (p = ip + len; p > ip; p--) {
+        for (p = ip + len - 1; p > ip; p--) {
             if (*p == ' ' || *p == ',') {
-               p++;
-               len -= p - ip;
-               ip = p;
-               break;
+                p++;
+                len -= p - ip;
+                ip = p;
+                break;
             }
         }
     }
 
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "realip: \"%s\"", ip);
+
     /* AF_INET only */
 
     sin = (struct sockaddr_in *) r->connection->sockaddr;
@@ -151,12 +155,25 @@ ngx_http_realip_handler(ngx_http_request
 
         if ((sin->sin_addr.s_addr & from[i].mask) == from[i].addr) {
 
-            r->connection->addr_text.len = len;
-            r->connection->addr_text.data = ip;
+            r->realip_set = 1;
+
+            addr = inet_addr((char *) ip);
+
+            if (addr == INADDR_NONE) {
+                return NGX_DECLINED;
+            }
 
-            sin->sin_addr.s_addr = inet_addr((char *) ip);
+            p = ngx_palloc(r->connection->pool, len);
+            if (p == NULL) {
+                return NGX_HTTP_INTERNAL_SERVER_ERROR;
+            }
 
-            r->realip_set = 1;
+            ngx_memcpy(p, ip, len);
+
+            sin->sin_addr.s_addr = addr;
+
+            r->connection->addr_text.len = len;
+            r->connection->addr_text.data = p;
 
             return NGX_DECLINED;
         }
--- a/src/http/modules/ngx_http_referer_module.c
+++ b/src/http/modules/ngx_http_referer_module.c
@@ -106,7 +106,7 @@ ngx_http_referer_variable(ngx_http_reque
     ref = r->headers_in.referer->value.data;
 
     if (len < sizeof("http://i.ru") - 1
-        || (ngx_strncasecmp(ref, "http://", 7) != 0))
+        || (ngx_strncasecmp(ref, (u_char *) "http://", 7) != 0))
     {
         if (rlcf->blocked_referer) {
             goto valid;
--- a/src/http/modules/ngx_http_ssi_filter_module.c
+++ b/src/http/modules/ngx_http_ssi_filter_module.c
@@ -1890,10 +1890,13 @@ ngx_http_ssi_include(ngx_http_request_t 
             return NGX_HTTP_SSI_ERROR;
         }
 
-        if (wait->len == 2 && ngx_strncasecmp(wait->data, "no", 2) == 0) {
+        if (wait->len == 2
+            && ngx_strncasecmp(wait->data, (u_char *) "no", 2) == 0)
+        {
             wait = NULL;
 
-        } else if (wait->len != 3 || ngx_strncasecmp(wait->data, "yes", 3) != 0)
+        } else if (wait->len != 3
+                   || ngx_strncasecmp(wait->data, (u_char *) "yes", 3) != 0)
         {
             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                           "invalid value \"%V\" in the \"wait\" parameter",
--- a/src/http/modules/perl/nginx.pm
+++ b/src/http/modules/perl/nginx.pm
@@ -47,7 +47,7 @@ our @EXPORT = qw(
     HTTP_INSUFFICIENT_STORAGE
 );
 
-our $VERSION = '0.5.12';
+our $VERSION = '0.5.13';
 
 require XSLoader;
 XSLoader::load('nginx', $VERSION);
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -12,7 +12,7 @@
 
 
 typedef struct {
-    char      *name;
+    u_char    *name;
     uint32_t   method;
 } ngx_http_method_name_t;
 
@@ -1247,7 +1247,9 @@ ngx_http_auth_basic_user(ngx_http_reques
     encoded = r->headers_in.authorization->value;
 
     if (encoded.len < sizeof("Basic ") - 1
-        || ngx_strncasecmp(encoded.data, "Basic ", sizeof("Basic ") - 1) != 0)
+        || ngx_strncasecmp(encoded.data, (u_char *) "Basic ",
+                           sizeof("Basic ") - 1)
+           != 0)
     {
         r->headers_in.user.data = (u_char *) "";
         return NGX_DECLINED;
@@ -2681,19 +2683,19 @@ ngx_http_core_root(ngx_conf_t *cf, ngx_c
 
 
 static ngx_http_method_name_t  ngx_methods_names[] = {
-   { "GET",       (uint32_t) ~NGX_HTTP_GET },
-   { "HEAD",      (uint32_t) ~NGX_HTTP_HEAD },
-   { "POST",      (uint32_t) ~NGX_HTTP_POST },
-   { "PUT",       (uint32_t) ~NGX_HTTP_PUT },
-   { "DELETE",    (uint32_t) ~NGX_HTTP_DELETE },
-   { "MKCOL",     (uint32_t) ~NGX_HTTP_MKCOL },
-   { "COPY",      (uint32_t) ~NGX_HTTP_COPY },
-   { "MOVE",      (uint32_t) ~NGX_HTTP_MOVE },
-   { "OPTIONS",   (uint32_t) ~NGX_HTTP_OPTIONS },
-   { "PROPFIND" , (uint32_t) ~NGX_HTTP_PROPFIND },
-   { "PROPPATCH", (uint32_t) ~NGX_HTTP_PROPPATCH },
-   { "LOCK",      (uint32_t) ~NGX_HTTP_LOCK },
-   { "UNLOCK",    (uint32_t) ~NGX_HTTP_UNLOCK },
+   { (u_char *) "GET",       (uint32_t) ~NGX_HTTP_GET },
+   { (u_char *) "HEAD",      (uint32_t) ~NGX_HTTP_HEAD },
+   { (u_char *) "POST",      (uint32_t) ~NGX_HTTP_POST },
+   { (u_char *) "PUT",       (uint32_t) ~NGX_HTTP_PUT },
+   { (u_char *) "DELETE",    (uint32_t) ~NGX_HTTP_DELETE },
+   { (u_char *) "MKCOL",     (uint32_t) ~NGX_HTTP_MKCOL },
+   { (u_char *) "COPY",      (uint32_t) ~NGX_HTTP_COPY },
+   { (u_char *) "MOVE",      (uint32_t) ~NGX_HTTP_MOVE },
+   { (u_char *) "OPTIONS",   (uint32_t) ~NGX_HTTP_OPTIONS },
+   { (u_char *) "PROPFIND" , (uint32_t) ~NGX_HTTP_PROPFIND },
+   { (u_char *) "PROPPATCH", (uint32_t) ~NGX_HTTP_PROPPATCH },
+   { (u_char *) "LOCK",      (uint32_t) ~NGX_HTTP_LOCK },
+   { (u_char *) "UNLOCK",    (uint32_t) ~NGX_HTTP_UNLOCK },
    { NULL, 0 }
 };
 
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -1285,14 +1285,16 @@ ngx_http_process_request_header(ngx_http
 
     if (r->headers_in.connection) {
         if (r->headers_in.connection->value.len == 5
-            && ngx_strcasecmp(r->headers_in.connection->value.data, "close")
+            && ngx_strcasecmp(r->headers_in.connection->value.data,
+                              (u_char *) "close")
                == 0)
         {
             r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
 
         } else if (r->headers_in.connection->value.len == 10
                    && ngx_strcasecmp(r->headers_in.connection->value.data,
-                                     "keep-alive") == 0)
+                                     (u_char *) "keep-alive")
+                      == 0)
         {
             r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
 
@@ -1651,6 +1653,7 @@ ngx_http_set_write_handler(ngx_http_requ
 
     r->http_state = NGX_HTTP_WRITING_REQUEST_STATE;
 
+    r->read_event_handler = ngx_http_block_read;
     r->write_event_handler = ngx_http_writer;
 
     wev = r->connection->write;
@@ -1831,9 +1834,7 @@ closed:
     ngx_log_error(NGX_LOG_INFO, c->log, err,
                   "client closed prematurely connection");
 
-    ngx_http_close_request(r, 0);
-
-    return;
+    ngx_http_finalize_request(r, 0);
 }
 
 
--- a/src/http/ngx_http_request_body.c
+++ b/src/http/ngx_http_request_body.c
@@ -317,7 +317,7 @@ ngx_http_do_read_client_request_body(ngx
         }
 
         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
-                       "http client request body rest %uz", rb->rest);
+                       "http client request body rest %O", rb->rest);
 
         if (rb->rest == 0) {
             break;
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -433,6 +433,9 @@ ngx_http_upstream_check_broken_connectio
 
     err = ngx_socket_errno;
 
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ev->log, err,
+                   "http upstream recv(): %d", n);
+
     /*
      * we do not need to disable the write event because
      * that event has NGX_USE_CLEAR_EVENT type
@@ -2313,7 +2316,7 @@ ngx_http_upstream_copy_content_type(ngx_
 
         while (*++p == ' ') { /* void */ }
 
-        if (ngx_strncasecmp(p, "charset=", 8) != 0) {
+        if (ngx_strncasecmp(p, (u_char *) "charset=", 8) != 0) {
             continue;
         }
 
--- a/src/http/ngx_http_variables.c
+++ b/src/http/ngx_http_variables.c
@@ -101,7 +101,7 @@ static ngx_http_variable_t  ngx_http_cor
       offsetof(ngx_http_request_t, headers_in.via), 0, 0 },
 #endif
 
-#if (NGX_HTTP_PROXY)
+#if (NGX_HTTP_PROXY || NGX_HTTP_REALIP)
     { ngx_string("http_x_forwarded_for"), NULL, ngx_http_variable_header,
       offsetof(ngx_http_request_t, headers_in.x_forwarded_for), 0, 0 },
 #endif
--- a/src/imap/ngx_imap_auth_http_module.c
+++ b/src/imap/ngx_imap_auth_http_module.c
@@ -477,8 +477,10 @@ ngx_imap_auth_http_process_headers(ngx_i
             len = ctx->header_name_end - ctx->header_name_start;
 
             if (len == sizeof("Auth-Status") - 1
-                && ngx_strncasecmp(ctx->header_name_start, "Auth-Status",
-                                   sizeof("Auth-Status") - 1) == 0)
+                && ngx_strncasecmp(ctx->header_name_start,
+                                   (u_char *) "Auth-Status",
+                                   sizeof("Auth-Status") - 1)
+                   == 0)
             {
                 len = ctx->header_end - ctx->header_start;
 
@@ -539,8 +541,10 @@ ngx_imap_auth_http_process_headers(ngx_i
             }
 
             if (len == sizeof("Auth-Server") - 1
-                && ngx_strncasecmp(ctx->header_name_start, "Auth-Server",
-                                   sizeof("Auth-Server") - 1) == 0)
+                && ngx_strncasecmp(ctx->header_name_start,
+                                   (u_char *) "Auth-Server",
+                                   sizeof("Auth-Server") - 1)
+                    == 0)
             {
                 ctx->addr.len = ctx->header_end - ctx->header_start;
                 ctx->addr.data = ctx->header_start;
@@ -549,8 +553,10 @@ ngx_imap_auth_http_process_headers(ngx_i
             }
 
             if (len == sizeof("Auth-Port") - 1
-                && ngx_strncasecmp(ctx->header_name_start, "Auth-Port",
-                                   sizeof("Auth-Port") - 1) == 0)
+                && ngx_strncasecmp(ctx->header_name_start,
+                                   (u_char *) "Auth-Port",
+                                   sizeof("Auth-Port") - 1)
+                   == 0)
             {
                 ctx->port.len = ctx->header_end - ctx->header_start;
                 ctx->port.data = ctx->header_start;
@@ -559,8 +565,10 @@ ngx_imap_auth_http_process_headers(ngx_i
             }
 
             if (len == sizeof("Auth-User") - 1
-                && ngx_strncasecmp(ctx->header_name_start, "Auth-User",
-                                   sizeof("Auth-User") - 1) == 0)
+                && ngx_strncasecmp(ctx->header_name_start,
+                                   (u_char *) "Auth-User",
+                                   sizeof("Auth-User") - 1)
+                   == 0)
             {
                 s->login.len = ctx->header_end - ctx->header_start;
 
@@ -578,8 +586,10 @@ ngx_imap_auth_http_process_headers(ngx_i
             }
 
             if (len == sizeof("Auth-Pass") - 1
-                && ngx_strncasecmp(ctx->header_name_start, "Auth-Pass",
-                                   sizeof("Auth-Pass") - 1) == 0)
+                && ngx_strncasecmp(ctx->header_name_start,
+                                   (u_char *) "Auth-Pass",
+                                   sizeof("Auth-Pass") - 1)
+                   == 0)
             {
                 s->passwd.len = ctx->header_end - ctx->header_start;
 
@@ -597,8 +607,10 @@ ngx_imap_auth_http_process_headers(ngx_i
             }
 
             if (len == sizeof("Auth-Wait") - 1
-                && ngx_strncasecmp(ctx->header_name_start, "Auth-Wait",
-                                   sizeof("Auth-Wait") - 1) == 0)
+                && ngx_strncasecmp(ctx->header_name_start,
+                                   (u_char *) "Auth-Wait",
+                                   sizeof("Auth-Wait") - 1)
+                   == 0)
             {
                 n = ngx_atoi(ctx->header_start,
                              ctx->header_end - ctx->header_start);
--- a/src/imap/ngx_imap_handler.c
+++ b/src/imap/ngx_imap_handler.c
@@ -802,7 +802,9 @@ ngx_pop3_auth_state(ngx_event_t *rev)
 
                 if (arg[0].len == 5) {
 
-                    if (ngx_strncasecmp(arg[0].data, "LOGIN", 5) == 0) {
+                    if (ngx_strncasecmp(arg[0].data, (u_char *) "LOGIN", 5)
+                        == 0)
+                    {
 
                         if (s->args.nelts != 1) {
                             rc = NGX_IMAP_PARSE_INVALID_COMMAND;
@@ -816,7 +818,10 @@ ngx_pop3_auth_state(ngx_event_t *rev)
 
                         break;
 
-                    } else if (ngx_strncasecmp(arg[0].data, "PLAIN", 5) == 0) {
+                    } else if (ngx_strncasecmp(arg[0].data, (u_char *) "PLAIN",
+                                               5)
+                               == 0)
+                    {
 
                         if (s->args.nelts == 1) {
                             s->imap_state = ngx_pop3_auth_plain;
@@ -856,7 +861,9 @@ ngx_pop3_auth_state(ngx_event_t *rev)
                     }
 
                 } else if (arg[0].len == 8
-                           && ngx_strncasecmp(arg[0].data, "CRAM-MD5", 8) == 0)
+                           && ngx_strncasecmp(arg[0].data,
+                                              (u_char *) "CRAM-MD5", 8)
+                              == 0)
                 {
                     s->imap_state = ngx_pop3_auth_cram_md5;
 
@@ -1205,6 +1212,7 @@ ngx_imap_read_command(ngx_imap_session_t
 {
     ssize_t    n;
     ngx_int_t  rc;
+    ngx_str_t  l;
 
     n = s->connection->recv(s->connection, s->buffer->last,
                             s->buffer->end - s->buffer->last);
@@ -1233,10 +1241,24 @@ ngx_imap_read_command(ngx_imap_session_t
         rc = ngx_imap_parse_command(s);
     }
 
-    if (rc == NGX_AGAIN
-        || rc == NGX_IMAP_NEXT
-        || rc == NGX_IMAP_PARSE_INVALID_COMMAND)
-    {
+    if (rc == NGX_AGAIN) {
+
+        if (s->buffer->last < s->buffer->end) {
+            return rc;
+        }
+
+        l.len = s->buffer->last - s->buffer->start;
+        l.data = s->buffer->start;
+
+        ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
+                      "client sent too long command \"%V\"", &l);
+
+        s->quit = 1;
+
+        return NGX_IMAP_PARSE_INVALID_COMMAND;
+    }
+
+    if (rc == NGX_IMAP_NEXT || rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
         return rc;
     }
 
--- a/src/os/unix/ngx_socket.c
+++ b/src/os/unix/ngx_socket.c
@@ -25,7 +25,7 @@
 int
 ngx_nonblocking(ngx_socket_t s)
 {
-    u_long  nb;
+    int  nb;
 
     nb = 1;
 
@@ -36,7 +36,7 @@ ngx_nonblocking(ngx_socket_t s)
 int
 ngx_blocking(ngx_socket_t s)
 {
-    u_long  nb;
+    int  nb;
 
     nb = 0;