Conditionals

TrafficScript provides two primary conditional statement types, “If” and “Switch”.

The “If” Statement

TrafficScript provides “if” and “if/else” statements for conditional execution. The condition is an expression, which is evaluated.

The return value of the expression determines whether the condition is “true” or “false”:

A non-zero number or non-empty string is true.

A zero number or empty string is false.

if( <condition> ) {

<statement list>

}

or

if( <condition> ) {

<statement list>

} else {

<statement list>

}

TrafficScript evaluates the given condition. If it is true (the value is non-zero, or is a non-empty string), TrafficScript executes the statement list that follows. In the case of an “If/Else” statement, and the condition evaluates to false, TrafficScript instead executes the second statement list following “else”.

For example:

$path = http.getPath();

if( string.startsWith( $path, "/secure" ) ) {

pool.use( "secure pool" );

} else {

pool.use( "non-secure pool" );

}

The "switch" Statement

TrafficScript provides a "switch" statement for cases where one if-else expression can evaluate to a number of different values, each requiring the Traffic Manager to take a different action. For example, a Traffic Manager might serve several different domains, each with its own back-end. By checking the host value of the HTTP header, you can use the switch statement to select the correct back-end using a number of cases.

The syntax for the switch statement is:

switch( <expression>[, <function name>] ) {

case <expression list 1>:

<statement list 1>;

case <expression list 2>:

<statement list 2>;

case <expression list 3>:

<statement list 3>;

...

[default:

<statement list>;]

}

By default, the Traffic Manager executes the first case containing an expression that evaluates to be equal ("==") to the switch <expression>.

You can specify an alternative comparison function using the optional <function name> argument. This argument accepts the name of a user defined or built in function, declared without any brackets or arguments. It must accept two arguments, with the switch expression being the first argument and the current case expression being the second. If this function returns a value that evaluates to true, the Traffic Manager executes the corresponding case statement list. For example, to perform matches on regular expressions, set <function name> to the TrafficScript function "string.regexmatch".

For each case statement, you can specify either a single expression or a comma-separated list of multiple expressions to be used in the comparison. A case is executed if the evaluation function returns true for any of the case's expressions. In this context, the commas in the expression list act as the "or" logical operator.

The optional default statement must only be used as the last case and is always executed if reached.

A typical usage example:

$host = http.getHostHeader();

switch( $host ) {

case "secure.example.com", "ssl.example.com":

pool.use( "secure pool" );

case "www.example.com", "www.example.org", "www.example.net":

pool.use( "non-secure pool" );

default:

pool.use( "discard" );

}

An example using a custom evaluation function:

$host = http.getHostHeader();

switch( $host, string.startsWith ) {

case "secure", "ssl":

pool.use( "secure pool" );

case "www":

pool.use( "non-secure pool );

default:

pool.use( "discard" );

}

A switch statement is additionally affected by the following:

You can use the break statement to break out of a switch statement entirely and continue execution after the switch. This has no effect on surrounding loops.

TrafficScript does not provide an implicit fall-through. In other words, a break at the end of a case body is redundant.

You can use any variable type within a switch.

A switch has no return value.

The Traffic Manager evaluates <expression> only once and stores the result for case comparison. If <expression> is a function, it is therefore executed only once.