]> git.kaiwu.me - nginx.git/commitdiff
Added ngx_filename_cmp() with "/" sorted to the left.
authorMaxim Dounin <mdounin@mdounin.ru>
Mon, 23 Sep 2013 15:37:13 +0000 (19:37 +0400)
committerMaxim Dounin <mdounin@mdounin.ru>
Mon, 23 Sep 2013 15:37:13 +0000 (19:37 +0400)
This patch fixes incorrect handling of auto redirect in configurations
like:

    location /0  { }
    location /a- { }
    location /a/ { proxy_pass ... }

With previously used sorting, this resulted in the following locations
tree (as "-" is less than "/"):

        "/a-"
    "/0"    "/a/"

and a request to "/a" didn't match "/a/" with auto_redirect, as it
didn't traverse relevant tree node during lookup (it tested "/a-",
then "/0", and then falled back to null location).

To preserve locale use for non-ASCII characters on case-insensetive
systems, libc's tolower() used.

src/core/ngx_string.c
src/core/ngx_string.h
src/os/unix/ngx_darwin_config.h
src/os/unix/ngx_files.h
src/os/unix/ngx_freebsd_config.h
src/os/unix/ngx_linux_config.h
src/os/unix/ngx_posix_config.h
src/os/unix/ngx_solaris_config.h
src/os/win32/ngx_files.h
src/os/win32/ngx_win32_config.h

index d3ff36855fb17a0fbc4b2da36a7da5e7b2b37dfc..fe6b1ee7136249ba27385e67fd27408fc7805694 100644 (file)
@@ -852,6 +852,46 @@ ngx_dns_strcmp(u_char *s1, u_char *s2)
 }
 
 
+ngx_int_t
+ngx_filename_cmp(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++;
+
+#if (NGX_HAVE_CASELESS_FILESYSTEM)
+        c1 = tolower(c1);
+        c2 = tolower(c2);
+#endif
+
+        if (c1 == c2) {
+
+            if (c1) {
+                n--;
+                continue;
+            }
+
+            return 0;
+        }
+
+        /* we need '/' to be the lowest character */
+
+        if (c1 == 0 || c2 == 0) {
+            return c1 - c2;
+        }
+
+        c1 = (c1 == '/') ? 0 : c1;
+        c2 = (c2 == '/') ? 0 : c2;
+
+        return c1 - c2;
+    }
+
+    return 0;
+}
+
+
 ngx_int_t
 ngx_atoi(u_char *line, size_t n)
 {
index 92d246ebcff417a878b9c1cc39d3c30250dfef0a..99bdbda6fa0db719102f01f8fffa0a8a460c7c10 100644 (file)
@@ -167,6 +167,7 @@ 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);
 ngx_int_t ngx_dns_strcmp(u_char *s1, u_char *s2);
+ngx_int_t ngx_filename_cmp(u_char *s1, u_char *s2, size_t n);
 
 ngx_int_t ngx_atoi(u_char *line, size_t n);
 ngx_int_t ngx_atofp(u_char *line, size_t n, size_t point);
index 149778c29ad440976c57e1f95d3f633e7d8ef1a0..7ac86c73e82a2e8af5d5aeb6ead37226df729914 100644 (file)
@@ -20,6 +20,7 @@
 #include <stddef.h>             /* offsetof() */
 #include <stdio.h>
 #include <stdlib.h>
+#include <ctype.h>
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
index df759df52f987ff9a00fec31f995f01701be36b8..4b20165004d4c1449e16771fb89ca01a67d03ae6 100644 (file)
@@ -192,17 +192,6 @@ ngx_int_t ngx_create_file_mapping(ngx_file_mapping_t *fm);
 void ngx_close_file_mapping(ngx_file_mapping_t *fm);
 
 
-#if (NGX_HAVE_CASELESS_FILESYSTEM)
-
-#define ngx_filename_cmp(s1, s2, n)  strncasecmp((char *) s1, (char *) s2, n)
-
-#else
-
-#define ngx_filename_cmp         ngx_memcmp
-
-#endif
-
-
 #define ngx_realpath(p, r)       (u_char *) realpath((char *) p, (char *) r)
 #define ngx_realpath_n           "realpath()"
 #define ngx_getcwd(buf, size)    (getcwd((char *) buf, size) != NULL)
index 248e7a731012bdf3e901f1626e392a00a8a354f7..92b2928c8b54cb4d38c1ef4ef622260cf0d97ee4 100644 (file)
@@ -16,6 +16,7 @@
 #include <stddef.h>             /* offsetof() */
 #include <stdio.h>
 #include <stdlib.h>
+#include <ctype.h>
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
index 8467a97fec95675884a75d28bb3a59f95eba44f1..72594bac0ea8b84ef3ba9bac41da4585435de193 100644 (file)
@@ -22,6 +22,7 @@
 #include <stddef.h>             /* offsetof() */
 #include <stdio.h>
 #include <stdlib.h>
+#include <ctype.h>
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
index 4cf90cc9897f70aff78d7ded89befdfd59616b81..d725659dfaccb0c82872e3467b819890db78c0dd 100644 (file)
@@ -39,6 +39,7 @@
 #include <stddef.h>             /* offsetof() */
 #include <stdio.h>
 #include <stdlib.h>
+#include <ctype.h>
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
index e664ba826c003802d59f18f37614c8ff23811c0e..ddfd984577ea0bacdb9cc870566554c67b25ca2d 100644 (file)
@@ -22,6 +22,7 @@
 #include <stddef.h>             /* offsetof() */
 #include <stdio.h>
 #include <stdlib.h>
+#include <ctype.h>
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
index a4624738f9f05056531b9af493fd4a1c30bdd590..08e5dc67e3f22853f2efea143e6db1437b95fa7b 100644 (file)
@@ -172,11 +172,6 @@ ngx_int_t ngx_create_file_mapping(ngx_file_mapping_t *fm);
 void ngx_close_file_mapping(ngx_file_mapping_t *fm);
 
 
-#define NGX_HAVE_CASELESS_FILESYSTEM  1
-
-#define ngx_filename_cmp(s1, s2, n) _strnicmp((char *) s1, (char *) s2, n)
-
-
 u_char *ngx_realpath(u_char *path, u_char *resolved);
 #define ngx_realpath_n              ""
 #define ngx_getcwd(buf, size)       GetCurrentDirectory(size, (char *) buf)
index 67105f1af5c20abe2f1f6c282bcea043bbc2e23f..fcbb308455e5a13e31d43d187f88aee1a7dbe457 100644 (file)
@@ -45,6 +45,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <ctype.h>
 #include <locale.h>
 
 #ifdef __WATCOMC__
@@ -123,7 +124,6 @@ typedef unsigned __int32    uint32_t;
 typedef __int32             int32_t;
 typedef unsigned __int16    uint16_t;
 #define ngx_libc_cdecl      __cdecl
-#define _strnicmp           strnicmp
 
 #else /* __WATCOMC__ */
 typedef unsigned int        uint32_t;
@@ -196,6 +196,10 @@ typedef int                 sig_atomic_t;
 #define NGX_HAVE_INHERITED_NONBLOCK  1
 #endif
 
+#ifndef NGX_HAVE_CASELESS_FILESYSTEM
+#define NGX_HAVE_CASELESS_FILESYSTEM  1
+#endif
+
 #ifndef NGX_HAVE_WIN32_TRANSMITPACKETS
 #define NGX_HAVE_WIN32_TRANSMITPACKETS  1
 #define NGX_HAVE_WIN32_TRANSMITFILE     0