changeset 2136:7a7c9e9024dd

ngx_hash_strlow()
author Igor Sysoev <igor@sysoev.ru>
date Mon, 04 Aug 2008 10:18:36 +0000
parents 8c6521eedf84
children 76d5af541412
files src/core/ngx_hash.c src/core/ngx_hash.h src/http/modules/ngx_http_map_module.c src/http/modules/ngx_http_ssi_filter_module.c src/http/modules/perl/nginx.xs src/http/ngx_http_core_module.c src/http/ngx_http_request.c
diffstat 7 files changed, 40 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/ngx_hash.c
+++ b/src/core/ngx_hash.c
@@ -620,6 +620,24 @@ ngx_hash_key_lc(u_char *data, size_t len
 }
 
 
+ngx_uint_t
+ngx_hash_strlow(u_char *dst, u_char *src, size_t n)
+{
+    ngx_uint_t  key;
+
+    key = 0;
+
+    while (n--) {
+        *dst = ngx_tolower(*src);
+        key = ngx_hash(key, *dst);
+        dst++;
+        src++;
+    }
+
+    return key;
+}
+
+
 ngx_int_t
 ngx_hash_keys_array_init(ngx_hash_keys_arrays_t *ha, ngx_uint_t type)
 {
@@ -794,12 +812,7 @@ wildcard:
 
     /* wildcard hash */
 
-    k = 0;
-
-    for (i = skip; i < last; i++) {
-        key->data[i] = ngx_tolower(key->data[i]);
-        k = ngx_hash(k, key->data[i]);
-    }
+    k = ngx_hash_strlow(&key->data[skip], &key->data[skip], last - skip);
 
     k %= ha->hsize;
 
--- a/src/core/ngx_hash.h
+++ b/src/core/ngx_hash.h
@@ -110,6 +110,8 @@ ngx_int_t ngx_hash_wildcard_init(ngx_has
 #define ngx_hash(key, c)   ((ngx_uint_t) key * 31 + c)
 ngx_uint_t ngx_hash_key(u_char *data, size_t len);
 ngx_uint_t ngx_hash_key_lc(u_char *data, size_t len);
+ngx_uint_t ngx_hash_strlow(u_char *dst, u_char *src, size_t n);
+
 
 ngx_int_t ngx_hash_keys_array_init(ngx_hash_keys_arrays_t *ha, ngx_uint_t type);
 ngx_int_t ngx_hash_add_key(ngx_hash_keys_arrays_t *ha, ngx_str_t *key,
--- a/src/http/modules/ngx_http_map_module.c
+++ b/src/http/modules/ngx_http_map_module.c
@@ -106,7 +106,7 @@ ngx_http_map_variable(ngx_http_request_t
 
     size_t                      len;
     u_char                     *name;
-    ngx_uint_t                  key, i;
+    ngx_uint_t                  key;
     ngx_http_variable_value_t  *vv, *value;
 
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -135,11 +135,7 @@ ngx_http_map_variable(ngx_http_request_t
         return NGX_ERROR;
     }
 
-    key = 0;
-    for (i = 0; i < len; i++) {
-        name[i] = ngx_tolower(vv->data[i]);
-        key = ngx_hash(key, name[i]);
-    }
+    key = ngx_hash_strlow(name, vv->data, len);
 
     value = ngx_hash_find_combined(&map->hash, key, name, len);
 
--- a/src/http/modules/ngx_http_ssi_filter_module.c
+++ b/src/http/modules/ngx_http_ssi_filter_module.c
@@ -1605,7 +1605,7 @@ ngx_http_ssi_evaluate_string(ngx_http_re
     size_t                     *size, len, prefix, part_len;
     ngx_str_t                   var, *val;
     ngx_int_t                   key;
-    ngx_uint_t                  i, j, n, bracket, quoted;
+    ngx_uint_t                  i, n, bracket, quoted;
     ngx_array_t                 lengths, values;
     ngx_http_variable_value_t  *vv;
 
@@ -1731,12 +1731,7 @@ ngx_http_ssi_evaluate_string(ngx_http_re
                 goto invalid_variable;
             }
 
-            key = 0;
-
-            for (j = 0; j < var.len; j++) {
-                var.data[j] = ngx_tolower(var.data[j]);
-                key = ngx_hash(key, var.data[j]);
-            }
+            key = ngx_hash_strlow(var.data, var.data, var.len);
 
             val = ngx_http_ssi_get_variable(r, &var, key);
 
@@ -2025,12 +2020,7 @@ ngx_http_ssi_include(ngx_http_request_t 
     }
 
     if (set) {
-        key = 0;
-
-        for (i = 0; i < set->len; i++) {
-            set->data[i] = ngx_tolower(set->data[i]);
-            key = ngx_hash(key, set->data[i]);
-        }
+        key = ngx_hash_strlow(set->data, set->data, set->len);
 
         psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
         if (psr == NULL) {
@@ -2141,7 +2131,6 @@ ngx_http_ssi_echo(ngx_http_request_t *r,
     u_char                     *p;
     uintptr_t                   len;
     ngx_int_t                   key;
-    ngx_uint_t                  i;
     ngx_buf_t                  *b;
     ngx_str_t                  *var, *value, *enc, text;
     ngx_chain_t                *cl;
@@ -2152,12 +2141,7 @@ ngx_http_ssi_echo(ngx_http_request_t *r,
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                    "ssi echo \"%V\"", var);
 
-    key = 0;
-
-    for (i = 0; i < var->len; i++) {
-        var->data[i] = ngx_tolower(var->data[i]);
-        key = ngx_hash(key, var->data[i]);
-    }
+    key = ngx_hash_strlow(var->data, var->data, var->len);
 
     value = ngx_http_ssi_get_variable(r, var, key);
 
@@ -2310,7 +2294,6 @@ ngx_http_ssi_set(ngx_http_request_t *r, 
     ngx_str_t **params)
 {
     ngx_int_t            key, rc;
-    ngx_uint_t           i;
     ngx_str_t           *name, *value, *vv;
     ngx_http_ssi_var_t  *var;
     ngx_http_ssi_ctx_t  *mctx;
@@ -2337,12 +2320,7 @@ ngx_http_ssi_set(ngx_http_request_t *r, 
         return rc;
     }
 
-    key = 0;
-
-    for (i = 0; i < name->len; i++) {
-        name->data[i] = ngx_tolower(name->data[i]);
-        key = ngx_hash(key, name->data[i]);
-    }
+    key = ngx_hash_strlow(name->data, name->data, name->len);
 
     vv = ngx_http_ssi_get_variable(r, name, key);
 
--- a/src/http/modules/perl/nginx.xs
+++ b/src/http/modules/perl/nginx.xs
@@ -247,11 +247,7 @@ header_in(r, key)
         XSRETURN_UNDEF;
     }
 
-    hash = 0;
-    for (i = 0; i < len; i++) {
-        lowcase_key[i] = ngx_tolower(p[i]);
-        hash = ngx_hash(hash, lowcase_key[i]);
-    }
+    hash = ngx_hash_strlow(lowcase_key, p, len);
 
     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
 
@@ -833,11 +829,7 @@ variable(r, name, value = NULL)
         XSRETURN_UNDEF;
     }
 
-    hash = 0;
-    for (i = 0; i < len; i++) {
-        lowcase[i] = ngx_tolower(p[i]);
-        hash = ngx_hash(hash, lowcase[i]);
-    }
+    hash = ngx_hash_strlow(lowcase, p, len);
 
     var.len = len;
     var.data = lowcase;
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -1305,7 +1305,7 @@ ngx_http_core_send_continue(ngx_http_req
 ngx_int_t
 ngx_http_set_content_type(ngx_http_request_t *r)
 {
-    u_char                     c, *p, *exten;
+    u_char                     c, *exten;
     ngx_str_t                 *type;
     ngx_uint_t                 i, hash;
     ngx_http_core_loc_conf_t  *clcf;
@@ -1325,19 +1325,12 @@ ngx_http_set_content_type(ngx_http_reque
 
             if (c >= 'A' && c <= 'Z') {
 
-                p = ngx_pnalloc(r->pool, r->exten.len);
-                if (p == NULL) {
+                exten = ngx_pnalloc(r->pool, r->exten.len);
+                if (exten == NULL) {
                     return NGX_HTTP_INTERNAL_SERVER_ERROR;
                 }
 
-                hash = 0;
-                exten = p;
-
-                for (i = 0; i < r->exten.len; i++) {
-                    c = ngx_tolower(r->exten.data[i]);
-                    hash = ngx_hash(hash, c);
-                    *p++ = c;
-                }
+                hash = ngx_hash_strlow(exten, r->exten.data, r->exten.len);
 
                 r->exten.data = exten;
 
@@ -2316,7 +2309,7 @@ ngx_http_core_type(ngx_conf_t *cf, ngx_c
     ngx_http_core_loc_conf_t *lcf = conf;
 
     ngx_str_t       *value, *content_type, *old, file;
-    ngx_uint_t       i, n;
+    ngx_uint_t       i, n, hash;
     ngx_hash_key_t  *type;
 
     value = cf->args->elts;
@@ -2342,9 +2335,7 @@ ngx_http_core_type(ngx_conf_t *cf, ngx_c
 
     for (i = 1; i < cf->args->nelts; i++) {
 
-        for (n = 0; n < value[i].len; n++) {
-            value[i].data[n] = ngx_tolower(value[i].data[n]);
-        }
+        hash = ngx_hash_strlow(value[i].data, value[i].data, value[i].len);
 
         type = lcf->types->elts;
         for (n = 0; n < lcf->types->nelts; n++) {
@@ -2368,7 +2359,7 @@ ngx_http_core_type(ngx_conf_t *cf, ngx_c
         }
 
         type->key = value[i];
-        type->key_hash = ngx_hash_key(value[i].data, value[i].len);
+        type->key_hash = hash;
         type->value = content_type;
     }
 
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -1549,8 +1549,8 @@ ngx_http_validate_host(u_char *host, siz
 static ngx_int_t
 ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
 {
-    u_char                    *server, ch;
-    ngx_uint_t                 i, hash;
+    u_char                    *server;
+    ngx_uint_t                 hash;
     ngx_http_core_loc_conf_t  *clcf;
     ngx_http_core_srv_conf_t  *cscf;
     u_char                     buf[32];
@@ -1569,16 +1569,7 @@ ngx_http_find_virtual_server(ngx_http_re
         }
     }
 
-    hash = 0;
-
-    for (i = 0; i < len; i++) {
-        ch = host[i];
-
-        ch = ngx_tolower(ch);
-        server[i] = ch;
-
-        hash = ngx_hash(hash, ch);
-    }
+    hash = ngx_hash_strlow(server, host, len);
 
     cscf = ngx_hash_find_combined(&r->virtual_names->names, hash, server, len);