# HG changeset patch # User Igor Sysoev # Date 1171479079 0 # Node ID db7c468c447d3a3236f311e728f8d809e1cb846c # Parent 2c7fed12fab7dbc412866f44105adf0f0876d7ef ngx_strcasecmp()/ngx_strncasecmp() diff --git a/src/core/ngx_conf_file.c b/src/core/ngx_conf_file.c --- 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 { diff --git a/src/core/ngx_inet.c b/src/core/ngx_inet.c --- 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) diff --git a/src/core/ngx_string.c b/src/core/ngx_string.c --- 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) { diff --git a/src/core/ngx_string.h b/src/core/ngx_string.h --- 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); diff --git a/src/http/modules/ngx_http_addition_filter_module.c b/src/http/modules/ngx_http_addition_filter_module.c --- 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); diff --git a/src/http/modules/ngx_http_charset_filter_module.c b/src/http/modules/ngx_http_charset_filter_module.c --- 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 { diff --git a/src/http/modules/ngx_http_headers_filter_module.c b/src/http/modules/ngx_http_headers_filter_module.c --- 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; } diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c --- 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) diff --git a/src/http/modules/ngx_http_range_filter_module.c b/src/http/modules/ngx_http_range_filter_module.c --- 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) { diff --git a/src/http/modules/ngx_http_referer_module.c b/src/http/modules/ngx_http_referer_module.c --- 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; diff --git a/src/http/modules/ngx_http_ssi_filter_module.c b/src/http/modules/ngx_http_ssi_filter_module.c --- 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", diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c --- 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 } }; diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c --- 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; diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c --- a/src/http/ngx_http_upstream.c +++ b/src/http/ngx_http_upstream.c @@ -2313,7 +2313,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; } diff --git a/src/imap/ngx_imap_auth_http_module.c b/src/imap/ngx_imap_auth_http_module.c --- 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); diff --git a/src/imap/ngx_imap_handler.c b/src/imap/ngx_imap_handler.c --- 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;