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 | |||
@@ -311,12 +311,57 @@ symtab_free(struct symtab *st) | |||
311 | 311 | ||
312 | 312 | ||
313 | static struct symtab **symtabv; | 313 | static struct symtab **symtabv; |
314 | static size_t symtabc; | 314 | static size_t symtabc; |
315 | static pthread_mutex_t symtab_mtx = PTHREAD_MUTEX_INITIALIZER; | 315 | static pthread_mutex_t symtab_mtx = PTHREAD_MUTEX_INITIALIZER; |
316 | 316 | ||
317 | static struct symtab *global_symtab; | ||
318 | static pthread_mutex_t global_mtx = PTHREAD_MUTEX_INITIALIZER; | ||
319 | |||
320 | VCL_VOID | ||
321 | vmod_global_set(VARIABLE_CTX ctx, VCL_STRING name, VCL_STRING value) | ||
322 | { | ||
323 | struct variable *var; | ||
324 | int inst; | ||
325 | |||
326 | AZ(pthread_mutex_lock(&global_mtx)); | ||
327 | if (!global_symtab) | ||
328 | global_symtab = symtab_create(); | ||
329 | if (!value) | ||
330 | symtab_remove(global_symtab, name); | ||
331 | else { | ||
332 | inst = 1; | ||
333 | var = symtab_lookup_or_install(global_symtab, name, &inst); | ||
334 | if (!inst && var->type == variable_string) | ||
335 | free(var->v.s); | ||
336 | var->type = variable_string; | ||
337 | var->v.s = strdup(value); | ||
338 | AN(var->v.s); | ||
339 | } | ||
340 | AZ(pthread_mutex_unlock(&global_mtx)); | ||
341 | } | ||
342 | |||
343 | VCL_STRING | ||
344 | vmod_global_get(VARIABLE_CTX ctx, VCL_STRING name) | ||
345 | { | ||
346 | char *s; | ||
347 | struct variable *var; | ||
348 | |||
349 | AZ(pthread_mutex_lock(&global_mtx)); | ||
350 | if (!global_symtab) | ||
351 | global_symtab = symtab_create(); | ||
352 | var = symtab_lookup_or_install(global_symtab, name, NULL); | ||
353 | if (var && var->type == variable_string) { | ||
354 | s = WS_Copy(ctx->ws, var->v.s, -1); | ||
355 | AN(s); | ||
356 | } else | ||
357 | s = NULL; | ||
358 | AZ(pthread_mutex_unlock(&global_mtx)); | ||
359 | return s; | ||
360 | } | ||
361 | |||
317 | static struct symtab * | 362 | static struct symtab * |
318 | get_symtab(VARIABLE_CTX ctx) | 363 | get_symtab(VARIABLE_CTX ctx) |
319 | { | 364 | { |
320 | struct symtab *st; | 365 | struct symtab *st; |
321 | int fd = ctx->req->sp->fd; | 366 | int fd = ctx->req->sp->fd; |
322 | AZ(pthread_mutex_lock(&symtab_mtx)); | 367 | AZ(pthread_mutex_lock(&symtab_mtx)); |
diff --git a/src/variable.vcc b/src/variable.vcc index a87c204..b8eb8e2 100644 --- a/src/variable.vcc +++ b/src/variable.vcc | |||
@@ -15,14 +15,14 @@ | |||
15 | # along with vmod-variable. If not, see <http://www.gnu.org/licenses/>. | 15 | # along with vmod-variable. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 | ||
17 | $Module variable 3 Variable Support for Varnish | 17 | $Module variable 3 Variable Support for Varnish |
18 | 18 | ||
19 | $Function VOID set(STRING, STRING) | 19 | $Function VOID set(STRING, STRING) |
20 | $Function STRING get(STRING) | 20 | $Function STRING get(STRING) |
21 | #$Function VOID global_set(STRING, STRING) | 21 | $Function VOID global_set(STRING, STRING) |
22 | #$Function STRING global_get(STRING) | 22 | $Function STRING global_get(STRING) |
23 | $Function VOID set_int(STRING, INT) | 23 | $Function VOID set_int(STRING, INT) |
24 | $Function INT get_int(STRING) | 24 | $Function INT get_int(STRING) |
25 | $Function VOID set_string(STRING, STRING) | 25 | $Function VOID set_string(STRING, STRING) |
26 | $Function STRING get_string(STRING) | 26 | $Function STRING get_string(STRING) |
27 | $Function VOID set_real(STRING, REAL) | 27 | $Function VOID set_real(STRING, REAL) |
28 | $Function REAL get_real(STRING) | 28 | $Function REAL get_real(STRING) |
diff --git a/tests/Makefile.am b/tests/Makefile.am index 2e4a53b..1a81bea 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am | |||
@@ -43,12 +43,13 @@ TESTSUITE_AT = \ | |||
43 | testsuite.at\ | 43 | testsuite.at\ |
44 | batchset.at\ | 44 | batchset.at\ |
45 | complex.at\ | 45 | complex.at\ |
46 | clear.at\ | 46 | clear.at\ |
47 | defined.at\ | 47 | defined.at\ |
48 | duration.at\ | 48 | duration.at\ |
49 | gstring.at\ | ||
49 | int.at\ | 50 | int.at\ |
50 | real.at\ | 51 | real.at\ |
51 | string.at\ | 52 | string.at\ |
52 | type_of.at\ | 53 | type_of.at\ |
53 | undef.at | 54 | undef.at |
54 | 55 | ||
diff --git a/tests/atlocal.in b/tests/atlocal.in index e833179..6cae626 100644 --- a/tests/atlocal.in +++ b/tests/atlocal.in | |||
@@ -5,49 +5,6 @@ | |||
5 | PATH=@abs_builddir@:@abs_top_builddir@/src:@abs_top_srcdir@/build-aux:$top_srcdir:$srcdir:$PATH | 5 | PATH=@abs_builddir@:@abs_top_builddir@/src:@abs_top_srcdir@/build-aux:$top_srcdir:$srcdir:$PATH |
6 | INITFILE=@abs_builddir@/.TESTINIT | 6 | INITFILE=@abs_builddir@/.TESTINIT |
7 | FAILFILE=@abs_builddir@/FAILURE | 7 | FAILFILE=@abs_builddir@/FAILURE |
8 | VARNISHTEST="@VARNISHSRC@/bin/varnishtest/varnishtest -Dvarnishd=@VARNISHSRC@/bin/varnishd/varnishd" | 8 | VARNISHTEST="@VARNISHSRC@/bin/varnishtest/varnishtest -Dvarnishd=@VARNISHSRC@/bin/varnishd/varnishd" |
9 | VARNISHVERSION=@VARNISHVERSION@ | 9 | VARNISHVERSION=@VARNISHVERSION@ |
10 | 10 | ||
11 | at_vcl_backend() { | ||
12 | case $VARNISHVERSION in | ||
13 | 3) cat <<EOT | ||
14 | server s1 { | ||
15 | rxreq | ||
16 | txresp | ||
17 | } -start | ||
18 | |||
19 | varnish v1 -vcl+backend { | ||
20 | import std; | ||
21 | import variable from "$abs_top_builddir/src/.libs/libvmod_variable.so"; | ||
22 | |||
23 | sub vcl_recv { | ||
24 | } | ||
25 | |||
26 | sub vcl_deliver { | ||
27 | } | ||
28 | } -start | ||
29 | EOT | ||
30 | ;; | ||
31 | 4) cat <<EOT | ||
32 | server s1 { | ||
33 | rxreq | ||
34 | txresp | ||
35 | } -start | ||
36 | |||
37 | varnish v1 -vcl+backend { | ||
38 | import std; | ||
39 | import variable from "$abs_top_builddir/src/.libs/libvmod_variable.so"; | ||
40 | |||
41 | sub vcl_recv { | ||
42 | $1 | ||
43 | } | ||
44 | |||
45 | sub vcl_deliver { | ||
46 | $2 | ||
47 | } | ||
48 | } -start | ||
49 | EOT | ||
50 | ;; | ||
51 | *) echo >&2 "unsupported varnish version: $VARNISHVERSION" | ||
52 | esac | ||
53 | } | ||
diff --git a/tests/batchset.at b/tests/batchset.at index d782191..be07cae 100644 --- a/tests/batchset.at +++ b/tests/batchset.at | |||
@@ -1,7 +1,7 @@ | |||
1 | # This file is part of vmod-variable | 1 | # This file is part of vmod-variable -*- autotest -*- |
2 | # Copyright (C) 2015 Sergey Poznyakoff | 2 | # Copyright (C) 2015 Sergey Poznyakoff |
3 | # | 3 | # |
4 | # Vmod-variable is free software; you can redistribute it and/or modify | 4 | # Vmod-variable is free software; you can redistribute it and/or modify |
5 | # it under the terms of the GNU General Public License as published by | 5 | # it under the terms of the GNU General Public License as published by |
6 | # the Free Software Foundation; either version 3, or (at your option) | 6 | # the Free Software Foundation; either version 3, or (at your option) |
7 | # any later version. | 7 | # any later version. |
@@ -14,25 +14,24 @@ | |||
14 | # You should have received a copy of the GNU General Public License | 14 | # You should have received a copy of the GNU General Public License |
15 | # along with vmod-variable. If not, see <http://www.gnu.org/licenses/>. | 15 | # along with vmod-variable. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 | ||
17 | AT_SETUP(batchset) | 17 | AT_SETUP(batchset) |
18 | AT_KEYWORDS(batchset) | 18 | AT_KEYWORDS(batchset) |
19 | 19 | ||
20 | AT_CHECK([ | 20 | AT_VARNISHTEST([ |
21 | AT_VCL([variable.batchset("y=i,x=s", "^/(\d+)/(.+)", req.url); | 21 | sub vcl_recv { |
22 | ], | 22 | variable.batchset("y=i,x=s", "^/(\d+)/(.+)", req.url); |
23 | [set resp.http.X-X = variable.get("x"); | 23 | } |
24 | set resp.http.X-Y = variable.get_int("y");], | 24 | sub vcl_deliver { |
25 | [txreq -url /10/test | 25 | set resp.http.X-X = variable.get("x"); |
26 | rxresp | 26 | set resp.http.X-Y = variable.get_int("y"); |
27 | expect resp.http.X-X == "test" | 27 | } |
28 | expect resp.http.X-Y == "10" | 28 | ],[ |
29 | ]) | 29 | txreq -url /10/test |
30 | AT_VARNISHTEST | 30 | rxresp |
31 | ], | 31 | expect resp.http.X-X == "test" |
32 | [0], | 32 | expect resp.http.X-Y == "10" |
33 | [OK | ||