1VMOD_VAR(3)                                                        VMOD_VAR(3)
2
3
4

NAME

6       vmod_var - Variable support for Varnish VCL
7

SYNOPSIS

9          import var [as name] [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, real numbers, durations, IP addresses
48       and backends. There are methods to 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. Global variables only support
52       strings.
53
54       The remaining functions have PRIV_TASK lifespan and are local to a sin‐
55       gle request or backend request.
56
57       Example:
58
59          vcl 4.0;
60          import var;
61
62          backend default { .host = "192.0.2.11"; .port = "8080"; }
63
64          sub vcl_recv {
65              # Set and get some values.
66              var.set("foo", "bar");
67              set req.http.x-foo = var.get("foo");
68
69              var.set_int("ten", 10);
70              var.set_int("five", 5);
71              set req.http.twenty = var.get_int("ten") + var.get_int("five") + 5;
72
73              # VCL will use the first token to decide final data type. Headers are strings.
74              # set req.http.X-lifetime = var.get_int("ten") + " seconds"; #  Won't work.
75              set req.http.X-lifetime = "" + var.get_int("ten") + " seconds";  # Works!
76
77              var.set_duration("timedelta", 1m);  # 60s
78              set req.http.d1 = var.get_duration("timedelta");
79
80              var.set_ip("endpoint", client.ip);
81              set req.http.x-client = var.get_ip("endpoint");
82
83              # Unset all non-global variables.
84              var.clear();
85
86              # Demonstrate use of global variables as state flags.
87              if (req.url ~ "/close$") {
88                  var.global_set("open", "no");
89              }
90              else if (req.url ~ "/open$") {
91                  var.global_set("open", "yes");
92              }
93
94              if (var.global_get("open") != "yes") {
95                  return (synth(200, "We are currently closed, sorry!"));
96              }
97          }
98
99   VOID set(STRING key, STRING value)
100       Set key to value.
101
102   STRING get(STRING)
103       Get  key  with data type STRING. If stored key is not a STRING an empty
104       string is returned.
105
106   VOID global_set(STRING, STRING)
107   STRING global_get(STRING)
108   VOID set_int(STRING key, INT value)
109       Set key to value.
110
111   INT get_int(STRING key)
112       Get key with data type INT. If stored key is not an INT  zero  will  be
113       returned.
114
115   VOID set_string(STRING key, STRING value)
116       Identical to set().
117
118   STRING get_string(STRING key)
119       Identical to get().
120
121   VOID set_real(STRING key, REAL value)
122       Set key to value.
123
124   REAL get_real(STRING key)
125       Get  key  with data type REAL. If stored key is not a REAL zero will be
126       returned.
127
128   VOID set_duration(STRING key, DURATION value)
129       Set key to value.
130
131   DURATION get_duration(STRING key)
132       Get key with data type DURATION. If stored key is not a  DURATION  zero
133       will be returned.
134
135   VOID set_ip(STRING key, IP value)
136       Set key to value.
137
138   IP get_ip(STRING key)
139       Get  key with data type IP. If stored key is not an IP null will be re‐
140       turned.
141
142   VOID set_backend(STRING key, BACKEND value)
143       Set key to value.
144
145   BACKEND get_backend(STRING key)
146       Get key with data type BACKEND. If stored key is not  a  BACKEND,  null
147       will be returned.
148
149   VOID clear()
150       Clear all non-global variables.
151
152
153
154
155                                                                   VMOD_VAR(3)
Impressum