comparison src/http/modules/ngx_http_map_module.c @ 3789:19c931be8b98

allow expressions in the first "map" parameter
author Igor Sysoev <igor@sysoev.ru>
date Thu, 25 Nov 2010 15:22:43 +0000
parents ac33852faaac
children d8300807c4f3
comparison
equal deleted inserted replaced
3788:b50daa26aa22 3789:19c931be8b98
25 } ngx_http_map_conf_ctx_t; 25 } ngx_http_map_conf_ctx_t;
26 26
27 27
28 typedef struct { 28 typedef struct {
29 ngx_hash_combined_t hash; 29 ngx_hash_combined_t hash;
30 ngx_int_t index; 30 ngx_http_complex_value_t value;
31 ngx_http_variable_value_t *default_value; 31 ngx_http_variable_value_t *default_value;
32 ngx_uint_t hostnames; /* unsigned hostnames:1 */ 32 ngx_uint_t hostnames; /* unsigned hostnames:1 */
33 } ngx_http_map_ctx_t; 33 } ngx_http_map_ctx_t;
34 34
35 35
103 uintptr_t data) 103 uintptr_t data)
104 { 104 {
105 ngx_http_map_ctx_t *map = (ngx_http_map_ctx_t *) data; 105 ngx_http_map_ctx_t *map = (ngx_http_map_ctx_t *) data;
106 106
107 size_t len; 107 size_t len;
108 u_char *name; 108 ngx_str_t val;
109 ngx_uint_t key; 109 ngx_uint_t key;
110 ngx_http_variable_value_t *vv, *value; 110 ngx_http_variable_value_t *value;
111 111
112 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 112 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
113 "http map started"); 113 "http map started");
114 114
115 vv = ngx_http_get_flushed_variable(r, map->index); 115 if (ngx_http_complex_value(r, &map->value, &val) != NGX_OK) {
116 116 return NGX_ERROR;
117 if (vv == NULL || vv->not_found) { 117 }
118 *v = *map->default_value; 118
119 return NGX_OK; 119 len = val.len;
120 } 120
121 121 if (len && map->hostnames && val.data[len - 1] == '.') {
122 len = vv->len;
123
124 if (len && map->hostnames && vv->data[len - 1] == '.') {
125 len--; 122 len--;
126 } 123 }
127 124
128 if (len == 0) { 125 if (len == 0) {
129 *v = *map->default_value; 126 *v = *map->default_value;
130 return NGX_OK; 127 return NGX_OK;
131 } 128 }
132 129
133 name = ngx_pnalloc(r->pool, len); 130 key = ngx_hash_strlow(val.data, val.data, len);
134 if (name == NULL) { 131
135 return NGX_ERROR; 132 value = ngx_hash_find_combined(&map->hash, key, val.data, len);
136 }
137
138 key = ngx_hash_strlow(name, vv->data, len);
139
140 value = ngx_hash_find_combined(&map->hash, key, name, len);
141 133
142 if (value) { 134 if (value) {
143 *v = *value; 135 *v = *value;
144 136
145 } else { 137 } else {
146 *v = *map->default_value; 138 *v = *map->default_value;
147 } 139 }
148 140
149 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 141 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
150 "http map: \"%v\" \"%v\"", vv, v); 142 "http map: \"%v\" \"%v\"", &val, v);
151 143
152 return NGX_OK; 144 return NGX_OK;
153 } 145 }
154 146
155 147
173 static char * 165 static char *
174 ngx_http_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 166 ngx_http_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
175 { 167 {
176 ngx_http_map_conf_t *mcf = conf; 168 ngx_http_map_conf_t *mcf = conf;
177 169
178 char *rv; 170 char *rv;
179 ngx_str_t *value, name; 171 ngx_str_t *value, name;
180 ngx_conf_t save; 172 ngx_conf_t save;
181 ngx_pool_t *pool; 173 ngx_pool_t *pool;
182 ngx_hash_init_t hash; 174 ngx_hash_init_t hash;
183 ngx_http_map_ctx_t *map; 175 ngx_http_map_ctx_t *map;
184 ngx_http_variable_t *var; 176 ngx_http_variable_t *var;
185 ngx_http_map_conf_ctx_t ctx; 177 ngx_http_map_conf_ctx_t ctx;
178 ngx_http_compile_complex_value_t ccv;
186 179
187 if (mcf->hash_max_size == NGX_CONF_UNSET_UINT) { 180 if (mcf->hash_max_size == NGX_CONF_UNSET_UINT) {
188 mcf->hash_max_size = 2048; 181 mcf->hash_max_size = 2048;
189 } 182 }
190 183
201 return NGX_CONF_ERROR; 194 return NGX_CONF_ERROR;
202 } 195 }
203 196
204 value = cf->args->elts; 197 value = cf->args->elts;
205 198
206 name = value[1]; 199 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
207 name.len--; 200
208 name.data++; 201 ccv.cf = cf;
209 202 ccv.value = &value[1];
210 map->index = ngx_http_get_variable_index(cf, &name); 203 ccv.complex_value = &map->value;
211 204
212 if (map->index == NGX_ERROR) { 205 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
213 return NGX_CONF_ERROR; 206 return NGX_CONF_ERROR;
214 } 207 }
215 208
216 name = value[2]; 209 name = value[2];
217 name.len--; 210 name.len--;