static in_addr_t ngx_http_qqwry_real_addr(ngx_http_request_t *r, ngx_http_qqwry_ctx_t *qqwry) { struct sockaddr_in *sin; ngx_http_variable_value_t *v; if (qqwry->index == -1) { ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http qqwry started: %V", &r->connection->addr_text); if (r->connection->sockaddr->sa_family != AF_INET) { return 0; } sin = (struct sockaddr_in *) r->connection->sockaddr; return ntohl(sin->sin_addr.s_addr); } v = ngx_http_get_flushed_variable(r, qqwry->index); if (v == NULL || v->not_found) { ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http qqwry not found"); return 0; } ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http qqwry started: %v", v); return ntohl(ngx_inet_addr(v->data, v->len)); }
ngx_http_variable_value_t * ngx_http_get_variable(ngx_http_request_t *r, ngx_str_t *name, ngx_uint_t key) { ngx_http_variable_t *v; ngx_http_variable_value_t *vv; ngx_http_core_main_conf_t *cmcf; cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); //先根据哈希表查找变量是否存在,如果存在,则根据其保存在哈希表里面的index获取变量的内容。或者调用get_handler v = ngx_hash_find(&cmcf->variables_hash, key, name->data, name->len); if (v) { if (v->flags & NGX_HTTP_VAR_INDEXED) { return ngx_http_get_flushed_variable(r, v->index); } else { vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t)); if (vv && v->get_handler(r, vv, v->data) == NGX_OK) { return vv; } return NULL; } } //如果不存在variables里面,则看一下是否是预定义的变量。 vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t)); if (vv == NULL) { return NULL; } if (ngx_strncmp(name->data, "http_", 5) == 0) { if (ngx_http_variable_unknown_header_in(r, vv, (uintptr_t) name) == NGX_OK){ return vv; } return NULL; } if (ngx_strncmp(name->data, "sent_http_", 10) == 0) { if (ngx_http_variable_unknown_header_out(r, vv, (uintptr_t) name) == NGX_OK) { return vv; } return NULL; } if (ngx_strncmp(name->data, "upstream_http_", 14) == 0) { if (ngx_http_upstream_header_variable(r, vv, (uintptr_t) name) == NGX_OK) { return vv; } return NULL; } if (ngx_strncmp(name->data, "cookie_", 7) == 0) { if (ngx_http_variable_cookie(r, vv, (uintptr_t) name) == NGX_OK) { return vv; } return NULL; } if (ngx_strncmp(name->data, "arg_", 4) == 0) { if (ngx_http_variable_argument(r, vv, (uintptr_t) name) == NGX_OK) { return vv; } return NULL; } vv->not_found = 1; return vv; }
static ngx_int_t ngx_http_map_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { ngx_http_map_ctx_t *map = (ngx_http_map_ctx_t *) data; size_t len; u_char *name; ngx_uint_t key, i; ngx_http_variable_value_t *vv, *value; ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http map started"); vv = ngx_http_get_flushed_variable(r, map->index); if (vv == NULL || vv->not_found) { *v = *map->default_value; return NGX_OK; } len = vv->len; if (len && map->hostnames && vv->data[len - 1] == '.') { len--; } if (len == 0) { *v = *map->default_value; return NGX_OK; } name = ngx_palloc(r->pool, len); if (name == NULL) { return NGX_ERROR; } key = 0; for (i = 0; i < len; i++) { name[i] = ngx_tolower(vv->data[i]); key = ngx_hash(key, name[i]); } value = ngx_hash_find_combined(&map->hash, key, name, len); if (value) { *v = *value; } else { *v = *map->default_value; } ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http map: \"%v\" \"%v\"", vv, v); return NGX_OK; }
static ngx_int_t ngx_http_map_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { ngx_http_map_ctx_t *map = (ngx_http_map_ctx_t *) data; size_t len; ngx_str_t val; ngx_uint_t key; ngx_http_variable_value_t *value; ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http map started"); if (ngx_http_complex_value(r, &map->value, &val) != NGX_OK) { return NGX_ERROR; } len = val.len; if (len && map->hostnames && val.data[len - 1] == '.') { len--; } key = ngx_hash_strlow(val.data, val.data, len); value = ngx_http_map_find(r, &map->map, key, val.data, len, &val); if (value == NULL) { value = map->default_value; } if (!value->valid) { value = ngx_http_get_flushed_variable(r, (ngx_uint_t) value->data); if (value == NULL || value->not_found) { value = &ngx_http_variable_null_value; } } *v = *value; ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http map: \"%v\" \"%v\"", &val, v); return NGX_OK; }
static ngx_int_t ngx_http_reqstat_check_enable(ngx_http_request_t *r, ngx_http_reqstat_conf_t **rlcf, ngx_http_reqstat_store_t **store) { ngx_http_reqstat_conf_t *rcf; ngx_http_reqstat_store_t *s; ngx_http_variable_value_t *v; rcf = ngx_http_get_module_main_conf(r, ngx_http_reqstat_module); if (r->variables[rcf->index].valid) { v = ngx_http_get_flushed_variable(r, rcf->index); if (v->data[0] == '0') { return NGX_DECLINED; } s = (ngx_http_reqstat_store_t *) (*((uintptr_t *) &v->data[1])); rcf = s->conf; } else { rcf = ngx_http_get_module_loc_conf(r, ngx_http_reqstat_module); s = ngx_http_get_module_ctx(r, ngx_http_reqstat_module); if (s == NULL) { s = ngx_http_reqstat_create_store(r, rcf); if (s == NULL) { return NGX_ERROR; } } if (s->bypass) { return NGX_DECLINED; } } *rlcf = rcf; *store = s; return NGX_OK; }