diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-02-12 12:38:20 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-02-12 12:38:20 +0200 |
commit | c36a2210ed6cd53c5c65418fa9c20ac22dec8c59 (patch) | |
tree | 95cbf77e8d0851e5ea2b9adaba7ad12c6bf0e1c4 | |
parent | b771edaa218bee8a8365cc8e4ee9af4005ad61b2 (diff) | |
download | vmod-variable-c36a2210ed6cd53c5c65418fa9c20ac22dec8c59.tar.gz vmod-variable-c36a2210ed6cd53c5c65418fa9c20ac22dec8c59.tar.bz2 |
Implement global string variables
* src/variable.c (vmod_global_set)
(vmod_global_get): New functions.
* src/variable.vcc (global_set)
(global_get): New protos.
* tests/gstring.at: New file.
* tests/Makefile.am (TESTSUITE_AT): Add gstring.at
* tests/testsuite.at (AT_VCL): Remove
(AT_VARNISHTEST): Rewrite. All uses changed.
Include gstring.at
* tests/atlocal.in (at_vcl_backend): Remove.
* tests/batchset.at: Use new AT_VARNISHTEST.
* tests/clear.at: Likewise.
* tests/complex.at: Likewise.
* tests/defined.at: Likewise.
* tests/duration.at: Likewise.
* tests/int.at: Likewise.
* tests/real.at: Likewise.
* tests/string.at: Likewise.
* tests/type_of.at: Likewise.
* tests/undef.at: Likewise.
-rw-r--r-- | src/variable.c | 45 | ||||
-rw-r--r-- | src/variable.vcc | 4 | ||||
-rw-r--r-- | tests/Makefile.am | 1 | ||||
-rw-r--r-- | tests/atlocal.in | 43 | ||||
-rw-r--r-- | tests/batchset.at | 29 | ||||
-rw-r--r-- | tests/clear.at | 68 | ||||
-rw-r--r-- | tests/complex.at | 62 | ||||
-rw-r--r-- | tests/defined.at | 24 | ||||
-rw-r--r-- | tests/duration.at | 26 | ||||
-rw-r--r-- | tests/gstring.at | 46 | ||||
-rw-r--r-- | tests/int.at | 26 | ||||
-rw-r--r-- | tests/real.at | 26 | ||||
-rw-r--r-- | tests/string.at | 28 | ||||
-rw-r--r-- | tests/testsuite.at | 38 | ||||
-rw-r--r-- | tests/type_of.at | 33 | ||||
-rw-r--r-- | tests/undef.at | 32 |
16 files changed, 292 insertions, 239 deletions
diff --git a/src/variable.c b/src/variable.c index c8ab8e6..6403c1a 100644 --- a/src/variable.c +++ b/src/variable.c @@ -316,2 +316,47 @@ static pthread_mutex_t symtab_mtx = PTHREAD_MUTEX_INITIALIZER; +static struct symtab *global_symtab; +static pthread_mutex_t global_mtx = PTHREAD_MUTEX_INITIALIZER; + +VCL_VOID +vmod_global_set(VARIABLE_CTX ctx, VCL_STRING name, VCL_STRING value) +{ + struct variable *var; + int inst; + + AZ(pthread_mutex_lock(&global_mtx)); + if (!global_symtab) + global_symtab = symtab_create(); + if (!value) + symtab_remove(global_symtab, name); + else { + inst = 1; + var = symtab_lookup_or_install(global_symtab, name, &inst); + if (!inst && var->type == variable_string) + free(var->v.s); + var->type = variable_string; + var->v.s = strdup(value); + AN(var->v.s); + } + AZ(pthread_mutex_unlock(&global_mtx)); +} + +VCL_STRING +vmod_global_get(VARIABLE_CTX ctx, VCL_STRING name) +{ + char *s; + struct variable *var; + + AZ(pthread_mutex_lock(&global_mtx)); + if (!global_symtab) + global_symtab = symtab_create(); + var = symtab_lookup_or_install(global_symtab, name, NULL); + if (var && var->type == variable_string) { + s = WS_Copy(ctx->ws, var->v.s, -1); + AN(s); + } else + s = NULL; + AZ(pthread_mutex_unlock(&global_mtx)); + return s; +} + static struct symtab * diff --git a/src/variable.vcc b/src/variable.vcc index a87c204..b8eb8e2 100644 --- a/src/variable.vcc +++ b/src/variable.vcc @@ -20,4 +20,4 @@ $Function VOID set(STRING, STRING) $Function STRING get(STRING) -#$Function VOID global_set(STRING, STRING) -#$Function STRING global_get(STRING) +$Function VOID global_set(STRING, STRING) +$Function STRING global_get(STRING) $Function VOID set_int(STRING, INT) diff --git a/tests/Makefile.am b/tests/Makefile.am index 2e4a53b..1a81bea 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -48,2 +48,3 @@ TESTSUITE_AT = \ duration.at\ + gstring.at\ int.at\ diff --git a/tests/atlocal.in b/tests/atlocal.in index e833179..6cae626 100644 --- a/tests/atlocal.in +++ b/tests/atlocal.in @@ -10,44 +10 @@ VARNISHVERSION=@VARNISHVERSION@ -at_vcl_backend() { - case $VARNISHVERSION in - 3) cat <<EOT -server s1 { - rxreq - txresp -} -start - -varnish v1 -vcl+backend { - import std; - import variable from "$abs_top_builddir/src/.libs/libvmod_variable.so"; - - sub vcl_recv { - } - - sub vcl_deliver { - } -} -start -EOT - ;; - 4) cat <<EOT -server s1 { - rxreq - txresp -} -start - -varnish v1 -vcl+backend { - import std; - import variable from "$abs_top_builddir/src/.libs/libvmod_variable.so"; - - sub vcl_recv { - $1 - } - - sub vcl_deliver { - $2 - } -} -start -EOT - ;; - *) echo >&2 "unsupported varnish version: $VARNISHVERSION" -esac -} diff --git a/tests/batchset.at b/tests/batchset.at index d782191..be07cae 100644 --- a/tests/batchset.at +++ b/tests/batchset.at @@ -1,2 +1,2 @@ -# This file is part of vmod-variable +# This file is part of vmod-variable -*- autotest -*- # Copyright (C) 2015 Sergey Poznyakoff @@ -19,16 +19,15 @@ AT_KEYWORDS(batchset) -AT_CHECK([ -AT_VCL([variable.batchset("y=i,x=s", "^/(\d+)/(.+)", req.url); -], -[set resp.http.X-X = variable.get("x"); - set resp.http.X-Y = variable.get_int("y");], -[txreq -url /10/test - rxresp - expect resp.http.X-X == "test" - expect resp.http.X-Y == "10" -]) -AT_VARNISHTEST -], -[0], -[OK +AT_VARNISHTEST([ +sub vcl_recv { + variable.batchset("y=i,x=s", "^/(\d+)/(.+)", req.url); +} +sub vcl_deliver { + set resp.http.X-X = variable.get("x"); + set resp.http.X-Y = variable.get_int("y"); +} +],[ +txreq -url /10/test +rxresp +expect resp.http.X-X == "test" +expect resp.http.X-Y == "10" ]) diff --git a/tests/clear.at b/tests/clear.at index 53ac20f..a3c602f 100644 --- a/tests/clear.at +++ b/tests/clear.at @@ -19,37 +19,35 @@ AT_KEYWORDS(clear) -AT_CHECK([ -AT_VCL([ -variable.set("foo", req.http.Host); -variable.set("bar","two"); -variable.set_int("ten", 10); -variable.set_int("five",5); -variable.set_real("one-half", 0.5); -variable.set_real("one-fourth",0.25); -variable.set_duration("minute", 1m); -variable.set_duration("hour",1h); -], -[variable.clear(); - set resp.http.X-Foo = variable.get("foo"); - set resp.http.X-Bar = variable.get("bar"); - set resp.http.X-Five = variable.get_int("five"); - set resp.http.X-Ten = variable.get_int("ten"); - set resp.http.X-One-Fourth = variable.get_real("one-fourth"); - set resp.http.X-One-Half = variable.get_real("one-half"); - set resp.http.X-Minute = variable.get_duration("minute"); - set resp.http.X-Hour = variable.get_duration("hour");], -[txreq -url / -hdr "Host:en.example.net" - rxresp - expect resp.http.X-Foo == "" - expect resp.http.X-Bar == "" - expect resp.http.X-Five == "0" - expect resp.http.X-Ten == "0" - expect resp.http.X-One-Half == "0.000" - expect resp.http.X-One-Fourth == "0.000" - expect resp.http.X-Minute == "0.000" - expect resp.http.X-Hour == "0.000" -]) -AT_VARNISHTEST -], -[0], -[OK +AT_VARNISHTEST([ +sub vcl_recv { + variable.set("foo", req.http.Host); + variable.set("bar","two"); + variable.set_int("ten", 10); + variable.set_int("five",5); + variable.set_real("one-half", 0.5); + variable.set_real("one-fourth",0.25); + variable.set_duration("minute", 1m); + variable.set_duration("hour",1h); +} +sub vcl_deliver { + variable.clear(); + set resp.http.X-Foo = variable.get("foo"); + set resp.http.X-Bar = variable.get("bar"); + set resp.http.X-Five = variable.get_int("five"); + set resp.http.X-Ten = variable.get_int("ten"); + set resp.http.X-One-Fourth = variable.get_real("one-fourth"); + set resp.http.X-One-Half = variable.get_real("one-half"); + set resp.http.X-Minute = variable.get_duration("minute"); + set resp.http.X-Hour = variable.get_duration("hour"); +} +],[ +txreq -url / -hdr "Host:en.example.net" +rxresp +expect resp.http.X-Foo == "" +expect resp.http.X-Bar == "" +expect resp.http.X-Five == "0" +expect resp.http.X-Ten == "0" +expect resp.http.X-One-Half == "0.000" +expect resp.http.X-One-Fourth == "0.000" +expect resp.http.X-Minute == "0.000" +expect resp.http.X-Hour == "0.000" ]) diff --git a/tests/complex.at b/tests/complex.at index dada9a1..77a7903 100644 --- a/tests/complex.at +++ b/tests/complex.at @@ -19,36 +19,34 @@ AT_KEYWORDS(complex) -AT_CHECK([ -AT_VCL([ -variable.set("foo", req.http.Host); -variable.set("bar","two"); -variable.set_int("ten", 10); -variable.set_int("five",5); -variable.set_real("one-half", 0.5); -variable.set_real("one-fourth",0.25); -variable.set_duration("minute", 1m); -variable.set_duration("hour",1h); +AT_VARNISHTEST([ +sub vcl_recv { + variable.set("foo", req.http.Host); + variable.set("bar","two"); + variable.set_int("ten", 10); + variable.set_int("five",5); + variable.set_real("one-half", 0.5); + variable.set_real("one-fourth",0.25); + variable.set_duration("minute", 1m); + variable.set_duration("hour",1h); +} +sub vcl_deliver { + set resp.http.X-Foo = variable.get("foo"); + set resp.http.X-Bar = variable.get("bar"); + set resp.http.X-Five = variable.get_int("five"); + set resp.http.X-Ten = variable.get_int("ten"); + set resp.http.X-One-Fourth = variable.get_real("one-fourth"); + set resp.http.X-One-Half = variable.get_real("one-half"); + set resp.http.X-Minute = variable.get_duration("minute"); + set resp.http.X-Hour = variable.get_duration("hour"); +} ], -[set resp.http.X-Foo = variable.get("foo"); - set resp.http.X-Bar = variable.get("bar"); - set resp.http.X-Five = variable.get_int("five"); - set resp.http.X-Ten = variable.get_int("ten"); - set resp.http.X-One-Fourth = variable.get_real("one-fourth"); - set resp.http.X-One-Half = variable.get_real("one-half"); - set resp.http.X-Minute = variable.get_duration("minute"); - set resp.http.X-Hour = variable.get_duration("hour");], [txreq -url / -hdr "Host:en.example.net" - rxresp - expect resp.http.X-Foo == "en.example.net" - expect resp.http.X-Bar == "two" - expect resp.http.X-Five == "5" - expect resp.http.X-Ten == "10" - expect resp.http.X-One-Half == "0.500" - expect resp.http.X-One-Fourth == "0.250" - expect resp.http.X-Minute == "60.000" - expect resp.http.X-Hour == "3600.000" -]) -AT_VARNISHTEST -], -[0], -[OK +rxresp +expect resp.http.X-Foo == "en.example.net" +expect resp.http.X-Bar == "two" +expect resp.http.X-Five == "5" +expect resp.http.X-Ten == "10" +expect resp.http.X-One-Half == "0.500" +expect resp.http.X-One-Fourth == "0.250" +expect resp.http.X-Minute == "60.000" +expect resp.http.X-Hour == "3600.000" ]) diff --git a/tests/defined.at b/tests/defined.at index 4c54a9d..c2d50f2 100644 --- a/tests/defined.at +++ b/tests/defined.at @@ -19,15 +19,15 @@ AT_KEYWORDS(defined) -AT_CHECK([ -AT_VCL([variable.set("string", "test");], -[set resp.http.X-String = variable.defined("string"); - set resp.http.X-Foo = variable.defined("foo");], -[txreq -url / - rxresp - expect resp.http.X-String == "1" - expect resp.http.X-Foo == "0" -]) -AT_VARNISHTEST +AT_VARNISHTEST([ +sub vcl_recv { + variable.set("string", "test"); +} +sub vcl_deliver { + set resp.http.X-String = variable.defined("string"); + set resp.http.X-Foo = variable.defined("foo"); +} ], -[0], -[OK +[txreq -url / +rxresp +expect resp.http.X-String == "1" +expect resp.http.X-Foo == "0" ]) diff --git a/tests/duration.at b/tests/duration.at index c866ef3..5368145 100644 --- a/tests/duration.at +++ b/tests/duration.at @@ -19,16 +19,16 @@ AT_KEYWORDS(duration) -AT_CHECK([ -AT_VCL([variable.set_duration("minute", 1m); -variable.set_duration("hour",1h);], -[set resp.http.X-Minute = variable.get_duration("minute"); - set resp.http.X-Hour = variable.get_duration("hour");], -[txreq -url / - rxresp - expect resp.http.X-Minute == "60.000" - expect resp.http.X-Hour == "3600.000" -]) -AT_VARNISHTEST +AT_VARNISHTEST([ +sub vcl_recv { + variable.set_duration("minute", 1m); + variable.set_duration("hour",1h); +} +sub vcl_deliver { + set resp.http.X-Minute = variable.get_duration("minute"); + set resp.http.X-Hour = variable.get_duration("hour"); +} ], -[0], -[OK +[txreq -url / +rxresp +expect resp.http.X-Minute == "60.000" +expect resp.http.X-Hour == "3600.000" ]) diff --git a/tests/gstring.at b/tests/gstring.at new file mode 100644 index 0000000..8a93dfe --- /dev/null +++ b/tests/gstring.at @@ -0,0 +1,46 @@ +# This file is part of vmod-variable -*- autotest -*- +# Copyright (C) 2015 Sergey Poznyakoff +# +# Vmod-variable is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# Vmod-variable is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with vmod-variable. If not, see <http://www.gnu.org/licenses/>. + +AT_SETUP(global string) +AT_KEYWORDS(global string gstring) + +AT_VARNISHTEST([ +sub vcl_recv { + if (req.url == "/") { + variable.global_set("host", req.http.Host); + } +} + +sub vcl_deliver { + set resp.http.X-Host = variable.global_get("host"); +} +],[ +txreq -url / -hdr "Host:en.example.net" +rxresp +expect resp.http.X-Host == "en.example.net" +txreq -url /test +rxresp +expect resp.http.X-Host == "en.example.net" +],[ +rxreq +txresp +rxreq +txresp +]) +AT_CLEANUP + + + diff --git a/tests/int.at b/tests/int.at index fc1140a..7b193cf 100644 --- a/tests/int.at +++ b/tests/int.at @@ -19,16 +19,16 @@ AT_KEYWORDS(int) -AT_CHECK([ -AT_VCL([variable.set_int("ten", 10); -variable.set_int("five",5);], -[set resp.http.X-Five = variable.get_int("five"); - set resp.http.X-Ten = variable.get_int("ten");], -[txreq -url / - rxresp - expect resp.http.X-Five == "5" - expect resp.http.X-Ten == "10" -]) -AT_VARNISHTEST +AT_VARNISHTEST([ +sub vcl_recv { + variable.set_int("ten", 10); + variable.set_int("five",5); +} +sub vcl_deliver { + set resp.http.X-Five = variable.get_int("five"); + set resp.http.X-Ten = variable.get_int("ten"); +} ], -[0], -[OK +[txreq -url / +rxresp +expect resp.http.X-Five == "5" +expect resp.http.X-Ten == "10" ]) diff --git a/tests/real.at b/tests/real.at index f6faa94..3500480 100644 --- a/tests/real.at +++ b/tests/real.at @@ -19,16 +19,16 @@ AT_KEYWORDS(real) -AT_CHECK([ -AT_VCL([variable.set_real("one-half", 0.5); -variable.set_real("one-fourth",0.25);], -[set resp.http.X-One-Fourth = variable.get_real("one-fourth"); - set resp.http.X-One-Half = variable.get_real("one-half");], +AT_VARNISHTEST([ +sub vcl_recv { + variable.set_real("one-half", 0.5); + variable.set_real("one-fourth",0.25); +} +sub vcl_deliver { + set resp.http.X-One-Fourth = variable.get_real("one-fourth"); + set resp.http.X-One-Half = variable.get_real("one-half"); +} +], [txreq -url / - rxresp - expect resp.http.X-One-Half == "0.500" - expect resp.http.X-One-Fourth == "0.250" -]) -AT_VARNISHTEST -], -[0], -[OK +rxresp +expect resp.http.X-One-Half == "0.500" +expect resp.http.X-One-Fourth == "0.250" ]) diff --git a/tests/string.at b/tests/string.at index 9bc4651..698ca4a 100644 --- a/tests/string.at +++ b/tests/string.at @@ -19,16 +19,16 @@ AT_KEYWORDS(string) -AT_CHECK([ -AT_VCL([variable.set("foo", req.http.Host); -variable.set("bar","two");], -[set resp.http.X-Foo = variable.get("foo"); - set resp.http.X-Bar = variable.get("bar");], -[txreq -url / -hdr "Host:en.example.net" - rxresp - expect resp.http.X-Foo == "en.example.net" - expect resp.http.X-Bar == "two" -]) -AT_VARNISHTEST -], -[0], -[OK +AT_VARNISHTEST([ +sub vcl_recv { + variable.set("foo", req.http.Host); + variable.set("bar","two"); +} +sub vcl_deliver { + set resp.http.X-Foo = variable.get("foo"); + set resp.http.X-Bar = variable.get("bar"); +} +],[ +txreq -url / -hdr "Host:en.example.net" +rxresp +expect resp.http.X-Foo == "en.example.net" +expect resp.http.X-Bar == "two" ]) diff --git a/tests/testsuite.at b/tests/testsuite.at index a022697..7312433 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -20,16 +20,27 @@ m4_define([AT_SKIP_TEST],[exit 77]) -m4_define([AT_VCL],[ - at_vcl_backend "m4_bpatsubst([$1],[[\"\$]],\\\&)" "m4_bpatsubst([$2],[[\"\$]],\\\&)" > test.vtc - cat >> test.vtc <<EOT -client c1 { - $3 -} - -client c1 -run -EOT -]) - -m4_define([AT_VARNISHTEST], -[$VARNISHTEST test.vtc | sed 's/^#.*TEST test.vtc passed.*/OK/']) +dnl AT_VARNISHTEST(VCL, CLT[, SRV]) +m4_define([AT_VARNISHTEST],[ +AT_CHECK([ +cat > test.vtc <<EOT +server s1 { +m4_if($3,,[dnl + rxreq + txresp],[$3]) +} -start + +varnish v1 -vcl+backend { + import std; + import variable from "$abs_top_builddir/src/.libs/libvmod_variable.so"; +$1 +} -start +client c1 { +$2 +} -run +EOT +$VARNISHTEST test.vtc | sed 's/^#.*TEST test.vtc passed.*/OK/' +], +[0], +[OK +])]) @@ -46,2 +57,3 @@ m4_include([undef.at]) m4_include([batchset.at]) +m4_include([gstring.at]) diff --git a/tests/type_of.at b/tests/type_of.at index 2aeda17..8d8b7e8 100644 --- a/tests/type_of.at +++ b/tests/type_of.at @@ -19,19 +19,18 @@ AT_KEYWORDS(type_of) -AT_CHECK([ -AT_VCL([variable.set("s", "test"); -variable.set_int("i", 1); -], -[set resp.http.X-S = variable.type_of("s"); - set resp.http.X-I = variable.type_of("i"); - set resp.http.X-Y = variable.type_of("y");], -[txreq -url / - rxresp - expect resp.http.X-S == "STRING" - expect resp.http.X-I == "INT" - expect resp.http.X-Y == "UNSET" -]) -AT_VARNISHTEST -], -[0], -[OK +AT_VARNISHTEST([ +sub vcl_recv { + variable.set("s", "test"); + variable.set_int("i", 1); +} +sub vcl_deliver { + set resp.http.X-S = variable.type_of("s"); + set resp.http.X-I = variable.type_of("i"); + set resp.http.X-Y = variable.type_of("y"); +} +],[ +txreq -url / +rxresp +expect resp.http.X-S == "STRING" +expect resp.http.X-I == "INT" +expect resp.http.X-Y == "UNSET" ]) diff --git a/tests/undef.at b/tests/undef.at index fa0aaf8..65165d1 100644 --- a/tests/undef.at +++ b/tests/undef.at @@ -19,19 +19,17 @@ AT_KEYWORDS(undef) -AT_CHECK([ -AT_VCL([variable.set("s", "test"); -variable.set("t", "foo"); -variable.undef("s"); -], -[ - set resp.http.X-S = variable.defined("s"); - set resp.http.X-T = variable.defined("t");], -[txreq -url / - rxresp - expect resp.http.X-S == "0" - expect resp.http.X-T == "1" -]) -AT_VARNISHTEST -], -[0], -[OK +AT_VARNISHTEST([ +sub vcl_recv { + variable.set("s", "test"); + variable.set("t", "foo"); + variable.undef("s"); +} +sub vcl_deliver { + set resp.http.X-S = variable.defined("s"); + set resp.http.X-T = variable.defined("t"); +} +],[ +txreq -url / +rxresp +expect resp.http.X-S == "0" +expect resp.http.X-T == "1" ]) |