diff options
author | Zhidao HONG <z.hong@f5.com> | 2023-05-25 00:27:55 +0800 |
---|---|---|
committer | Zhidao HONG <z.hong@f5.com> | 2023-05-25 00:27:55 +0800 |
commit | a378f6aa3136ed7ef172b71add31ee62e560df00 (patch) | |
tree | 701fc0e0d348a7f08cf37ed1d43cbef1e0e9d909 /src/nxt_var.c | |
parent | ce2405ec3dd97e8bdf8f63312e3c6ce59ef562d4 (diff) | |
download | unit-a378f6aa3136ed7ef172b71add31ee62e560df00.tar.gz unit-a378f6aa3136ed7ef172b71add31ee62e560df00.tar.bz2 |
HTTP: fixed variable caching.
When a variable is accessed in the Unit configuration, the value is cached.
This was useful prior to the URI rewrite feature, but now that the URI (more
precisely, the request target) can be rewritten, the contents of the variable
$uri (which contains the path part of the request target, and is decoded)
should not be cached anymore, or at least the cached value should be invalidated
after a URI rewrite.
Example:
{
"rewrite": "/prefix$uri",
"share": "$uri"
}
For a request line like GET /foo?bar=baz HTTP/1.1\r\n, the expected file
served in the response would be /prefix/foo, but due to the caching issue,
Unit currently serves /foo.
Diffstat (limited to 'src/nxt_var.c')
-rw-r--r-- | src/nxt_var.c | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/src/nxt_var.c b/src/nxt_var.c index e113969f..e4299a09 100644 --- a/src/nxt_var.c +++ b/src/nxt_var.c @@ -80,7 +80,7 @@ static const nxt_lvlhsh_proto_t nxt_var_cache_proto nxt_aligned(64) = { static nxt_lvlhsh_t nxt_var_hash; static uint32_t nxt_var_count; -static nxt_var_handler_t *nxt_var_index; +static nxt_var_decl_t **nxt_vars; static nxt_int_t @@ -235,8 +235,11 @@ nxt_var_cache_value(nxt_task_t *task, nxt_var_cache_t *cache, uint32_t index, { nxt_int_t ret; nxt_str_t *value; + nxt_var_decl_t *var; nxt_lvlhsh_query_t lhq; + var = nxt_vars[index >> 16]; + value = cache->spare; if (value == NULL) { @@ -248,6 +251,10 @@ nxt_var_cache_value(nxt_task_t *task, nxt_var_cache_t *cache, uint32_t index, cache->spare = value; } + if (!var->cacheable) { + goto not_cached; + } + lhq.key_hash = nxt_murmur_hash2_uint32(&index); lhq.replace = 0; lhq.key.length = sizeof(uint32_t); @@ -261,16 +268,20 @@ nxt_var_cache_value(nxt_task_t *task, nxt_var_cache_t *cache, uint32_t index, return NULL; } - if (ret == NXT_OK) { - ret = nxt_var_index[index >> 16](task, value, ctx, index & 0xffff); - if (nxt_slow_path(ret != NXT_OK)) { - return NULL; - } + if (ret == NXT_DECLINED) { + return lhq.value; + } + +not_cached: - cache->spare = NULL; + ret = var->handler(task, value, ctx, index & 0xffff); + if (nxt_slow_path(ret != NXT_OK)) { + return NULL; } - return lhq.value; + cache->spare = NULL; + + return value; } @@ -303,12 +314,11 @@ nxt_int_t nxt_var_index_init(void) { nxt_uint_t i; - nxt_var_decl_t *decl; - nxt_var_handler_t *index; + nxt_var_decl_t *decl, **vars; nxt_lvlhsh_each_t lhe; - index = nxt_memalign(64, nxt_var_count * sizeof(nxt_var_handler_t)); - if (index == NULL) { + vars = nxt_memalign(64, nxt_var_count * sizeof(nxt_var_decl_t *)); + if (vars == NULL) { return NXT_ERROR; } @@ -317,10 +327,10 @@ nxt_var_index_init(void) for (i = 0; i < nxt_var_count; i++) { decl = nxt_lvlhsh_each(&nxt_var_hash, &lhe); decl->index = i; - index[i] = decl->handler; + vars[i] = decl; } - nxt_var_index = index; + nxt_vars = vars; return NXT_OK; } |