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