ngx_hash_strlow()
diff --git a/src/core/ngx_hash.c b/src/core/ngx_hash.c
index c39395c..2954266 100644
--- a/src/core/ngx_hash.c
+++ b/src/core/ngx_hash.c
@@ -620,6 +620,24 @@
}
+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 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;
diff --git a/src/core/ngx_hash.h b/src/core/ngx_hash.h
index 95e0404..76e3c77 100644
--- a/src/core/ngx_hash.h
+++ b/src/core/ngx_hash.h
@@ -110,6 +110,8 @@
#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,
diff --git a/src/http/modules/ngx_http_map_module.c b/src/http/modules/ngx_http_map_module.c
index cedc508..c2459f2 100644
--- a/src/http/modules/ngx_http_map_module.c
+++ b/src/http/modules/ngx_http_map_module.c
@@ -106,7 +106,7 @@
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 @@
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);
diff --git a/src/http/modules/ngx_http_ssi_filter_module.c b/src/http/modules/ngx_http_ssi_filter_module.c
index d8fc8f3..5de41f3 100644
--- a/src/http/modules/ngx_http_ssi_filter_module.c
+++ b/src/http/modules/ngx_http_ssi_filter_module.c
@@ -1605,7 +1605,7 @@
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 @@
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 @@
}
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 @@
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_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_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 @@
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);
diff --git a/src/http/modules/perl/nginx.xs b/src/http/modules/perl/nginx.xs
index 6e7ad77..fbc7f59 100644
--- a/src/http/modules/perl/nginx.xs
+++ b/src/http/modules/perl/nginx.xs
@@ -247,11 +247,7 @@
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 @@
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;
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 468b3e1..7ac92f6 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -1305,7 +1305,7 @@
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 @@
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_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 @@
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 @@
}
type->key = value[i];
- type->key_hash = ngx_hash_key(value[i].data, value[i].len);
+ type->key_hash = hash;
type->value = content_type;
}
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index ec36f50..2a0ed7a 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -1549,8 +1549,8 @@
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 @@
}
}
- 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);