-
Notifications
You must be signed in to change notification settings - Fork 86
/
vmod_var.vcc
126 lines (76 loc) · 3.34 KB
/
vmod_var.vcc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
$Module var 3 "Variable support for Varnish VCL"
This VMOD implements basic variable support in VCL.
It supports strings, integers, real numbers, durations, IP addresses
and backends. There are methods to get and set each data type.
Global variables have a lifespan that extends across requests and
VCLs, for as long as the vmod is loaded. Global variables only support
strings.
The remaining functions have PRIV_TASK lifespan and are local to a single
request or backend request.
.. vcl-start
Example::
vcl 4.0;
import var;
backend default { .host = "192.0.2.11"; .port = "8080"; }
sub vcl_recv {
# Set and get some values.
var.set("foo", "bar");
set req.http.x-foo = var.get("foo");
var.set_int("ten", 10);
var.set_int("five", 5);
set req.http.twenty = var.get_int("ten") + var.get_int("five") + 5;
# VCL will use the first token to decide final data type. Headers are strings.
# set req.http.X-lifetime = var.get_int("ten") + " seconds"; # Won't work.
set req.http.X-lifetime = "" + var.get_int("ten") + " seconds"; # Works!
var.set_duration("timedelta", 1m); # 60s
set req.http.d1 = var.get_duration("timedelta");
var.set_ip("endpoint", client.ip);
set req.http.x-client = var.get_ip("endpoint");
# Unset all non-global variables.
var.clear();
# Demonstrate use of global variables as state flags.
if (req.url ~ "/close$") {
var.global_set("open", "no");
}
else if (req.url ~ "/open$") {
var.global_set("open", "yes");
}
if (var.global_get("open") != "yes") {
return (synth(200, "We are currently closed, sorry!"));
}
}
.. vcl-end
$ABI vrt
$Function VOID set(PRIV_TASK, STRING key, STRING value)
Set `key` to `value`.
$Function STRING get(PRIV_TASK, STRING )
Get `key` with data type STRING. If stored `key` is not a STRING a NULL string is returned.
$Function VOID global_set(STRING, STRING)
$Function STRING global_get(STRING)
$Function VOID set_int(PRIV_TASK, STRING key, INT value)
Set `key` to `value`.
$Function INT get_int(PRIV_TASK, STRING key)
Get `key` with data type INT. If stored `key` is not an INT zero will be returned.
$Function VOID set_string(PRIV_TASK, STRING key, STRING value)
Identical to set().
$Function STRING get_string(PRIV_TASK, STRING key)
Identical to get().
$Function VOID set_real(PRIV_TASK, STRING key, REAL value)
Set `key` to `value`.
$Function REAL get_real(PRIV_TASK, STRING key)
Get `key` with data type REAL. If stored `key` is not a REAL zero will be returned.
$Function VOID set_duration(PRIV_TASK, STRING key, DURATION value)
Set `key` to `value`.
$Function DURATION get_duration(PRIV_TASK, STRING key)
Get `key` with data type DURATION. If stored `key` is not a DURATION zero will be returned.
$Function VOID set_ip(PRIV_TASK, STRING key, IP value)
Set `key` to `value`.
$Function IP get_ip(PRIV_TASK, STRING key)
Get `key` with data type IP. If stored `key` is not an IP null will be returned.
$Function VOID set_backend(PRIV_TASK, STRING key, BACKEND value)
Set `key` to `value`.
$Function BACKEND get_backend(PRIV_TASK, STRING key)
Get `key` with data type BACKEND. If stored `key` is not a BACKEND,
null will be returned.
$Function VOID clear(PRIV_TASK)
Clear all non-global variables.