Added ngx_filename_cmp() with "/" sorted to the left.

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.
This commit is contained in:
Maxim Dounin 2013-09-23 19:37:13 +04:00
parent d2ef70e97a
commit e11584164f
10 changed files with 51 additions and 17 deletions

View 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_int_t
ngx_atoi(u_char *line, size_t n) ngx_atoi(u_char *line, size_t n)
{ {

View 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_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_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_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_atoi(u_char *line, size_t n);
ngx_int_t ngx_atofp(u_char *line, size_t n, size_t point); ngx_int_t ngx_atofp(u_char *line, size_t n, size_t point);

View File

@ -20,6 +20,7 @@
#include <stddef.h> /* offsetof() */ #include <stddef.h> /* offsetof() */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>

View 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); 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(p, r) (u_char *) realpath((char *) p, (char *) r)
#define ngx_realpath_n "realpath()" #define ngx_realpath_n "realpath()"
#define ngx_getcwd(buf, size) (getcwd((char *) buf, size) != NULL) #define ngx_getcwd(buf, size) (getcwd((char *) buf, size) != NULL)

View File

@ -16,6 +16,7 @@
#include <stddef.h> /* offsetof() */ #include <stddef.h> /* offsetof() */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>

View File

@ -22,6 +22,7 @@
#include <stddef.h> /* offsetof() */ #include <stddef.h> /* offsetof() */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>

View File

@ -39,6 +39,7 @@
#include <stddef.h> /* offsetof() */ #include <stddef.h> /* offsetof() */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>

View File

@ -22,6 +22,7 @@
#include <stddef.h> /* offsetof() */ #include <stddef.h> /* offsetof() */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>

View 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); 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); u_char *ngx_realpath(u_char *path, u_char *resolved);
#define ngx_realpath_n "" #define ngx_realpath_n ""
#define ngx_getcwd(buf, size) GetCurrentDirectory(size, (char *) buf) #define ngx_getcwd(buf, size) GetCurrentDirectory(size, (char *) buf)

View File

@ -45,6 +45,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <ctype.h>
#include <locale.h> #include <locale.h>
#ifdef __WATCOMC__ #ifdef __WATCOMC__
@ -123,7 +124,6 @@ typedef unsigned __int32 uint32_t;
typedef __int32 int32_t; typedef __int32 int32_t;
typedef unsigned __int16 uint16_t; typedef unsigned __int16 uint16_t;
#define ngx_libc_cdecl __cdecl #define ngx_libc_cdecl __cdecl
#define _strnicmp strnicmp
#else /* __WATCOMC__ */ #else /* __WATCOMC__ */
typedef unsigned int uint32_t; typedef unsigned int uint32_t;
@ -196,6 +196,10 @@ typedef int sig_atomic_t;
#define NGX_HAVE_INHERITED_NONBLOCK 1 #define NGX_HAVE_INHERITED_NONBLOCK 1
#endif #endif
#ifndef NGX_HAVE_CASELESS_FILESYSTEM
#define NGX_HAVE_CASELESS_FILESYSTEM 1
#endif
#ifndef NGX_HAVE_WIN32_TRANSMITPACKETS #ifndef NGX_HAVE_WIN32_TRANSMITPACKETS
#define NGX_HAVE_WIN32_TRANSMITPACKETS 1 #define NGX_HAVE_WIN32_TRANSMITPACKETS 1
#define NGX_HAVE_WIN32_TRANSMITFILE 0 #define NGX_HAVE_WIN32_TRANSMITFILE 0