Local variables
Fastly VCL supports variables for storing temporary values during request processing.
Declaring a variable
Variables must be declared before they are used, usually at the beginning of a function before any statements. They can only be used in the same function where they are declared. Fastly VCL does not provide block scope. Declarations apply to an entire function's scope even if a variable is declared within a block.
Variables start with var.
and their names consist of characters in the set [A-Za-z0-9._-]
. (:
is explicitly disallowed.) The declaration syntax is:
declare local var.<name> <type>;
Variables can be assigned a value at declaration time. The syntax for this is:
declare local var.<name> <type> = <assignment-expr>;
Variable types
Variables can be of the following types:
- BOOL
- INTEGER
- FLOAT
- TIME (absolute time)
- RTIME (relative time)
- STRING
- REGEX
Variables are initialized to the zero value of the type:
0
for numeric typesfalse
for BOOLNULL
for STRING- An unsatisfisable regex for REGEX.
An unsatisfiable regex is a regex that will never match any string.
Usage
Boolean variables
Boolean assignments support boolean variables on the right-hand side as well as BOOL-returning functions, conditional expressions, and the true
and false
constants.
declare local var.boolean BOOL;
# Inline assignment as part of a declarationdeclare local var.boolean BOOL = true;
# BOOL assignment with RHS variableset var.boolean = true;set req.esi = var.boolean;set resp.http.Bool = if(req.esi, "y", "n");
# BOOL assignment with RHS functionset var.boolean = http_status_matches(resp.status, "200,304");
# BOOL assigment with RHS conditionalset var.boolean = (req.url == "/");
# not-set check, like 'if (req.http.Foo) { ... }'set var.boolean = (req.http.Foo);
Numeric variables
Numeric assignment and comparison support numeric variables (anything except STRING or BOOL) on the right-hand side, including conversion in both directions between FLOAT and INTEGER types, rounding to the nearest integer in the FLOAT to INTEGER case.
Invalid conditions or domain errors like division by zero will set "fastly.error"
.
declare local var.integer INTEGER;declare local var.float FLOAT;
# Inline assignment as part of a declarationdeclare local var.float FLOAT = 1.3;declare local var.integer INTEGER = req.bytes_read;
# Numeric assignment with RHS variable and# implicit string conversion for headerset var.integer = req.bytes_read;set var.integer -= req.body_bytes_read;set resp.http.VarInteger = var.integer;
# Numeric comparison with RHS variableset resp.http.VarIntegerOK = if(req.header_bytes_read == var.integer, "y", "n");
String variables
String assignments support string concatenation on the right-hand side.
declare local var.restarted STRING;
# Inline assignment as part of a declarationdeclare local var.s STRING = "I am a string!";
# string concatenation on RHSset var.restarted = "Request " if(req.restarts > 0, "has", "has not") " restarted.";
Time variables
Time variables support both relative and absolute times.
declare local var.time TIME;declare local var.rtime RTIME;
# Inline assignment as part of a declarationdeclare local var.rtime RTIME = req.grace;declare local var.time = std.time("Fri, 10 Jun 2016 00:02:12 GMT", now);
set req.grace = 72s;set var.rtime = req.grace;set resp.http.VarRTime = var.rtime;
set var.time = std.time("Fri, 10 Jun 2016 00:02:12 GMT", now);set var.time -= var.rtime;# implicit string conversion for headerset resp.http.VarTime = var.time;
IP Variables
Local variables support both IPv4 and IPv6:
declare local var.ipv4 IP = "192.168.0.1";declare local var.ipv6 IP = "::1";