Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
Showing results for
Search instead for
Did you mean:
HowTo: TrafficScript variables and types
Variables in TrafficScript can contain data of various types: integer, floating point (double), string, array or hash. TrafficScript automatically casts (converts) basic values and variables into the correct type when you evaluate an expression or call a function:
Cast to Integer
Cast to double
Cast to String
$a = 14;
$a = 3.25;
$a = 3.75;
$a = "abcde";
$a = "3.25";
$a = "3.75";
$a = "14str";
$a = "3.2.7";
Strings and doubles are rounded up or down to the nearest integer value when they are cast to integers:
$int = 10;
$double = 2.71828;
string.len( $int ); # casts to string, returns 2
string.len( $double ); # casts to string, returns 7
# Set $string to "10, 2.71828"
$string = $int . ", " . $double;
# Convert $string to a number, and add 4:
$r = $string + 4; # $r is 14
In a mixed-type operation such as a compare, where all types are allowed:
If either operand is a double, both operands will be converted to doubles, else:
If either operand is an integer, both operand will be converted to integers, else:
Both operands are strings, in which case a string operation is used.
Casting to or from arrays and hashes is not permitted. For example, if an array is used where a string value is required, a warning message will be printed to the Event Log. The warning will contain the line number at which the problem occurred so it can be easily located and corrected. A conversion of this type will have a result of 0 and the rule will continue to execute.
Adding the clause import strict; to the rule will cause the rule to be terminated should any type casting errors occur.
...will print the following log line, but will still add the "X-Test" header to the request and send it to the server:
WARNING: Rule <Rule Name>: Line 3: Cannot convert array to string value
The following rule attempts to authorize users by checking the value of the 'X-Username' header. There is a bug in the rule though - the author forgot to look up the username in the $authorized hash. If this rule was controlling access to a secure part of the website then it could be a security risk to continue processing the rule with an error. To protect against such an occurrence the import strict; clause is used:
When this rule is executed, it will reach the line containing the error and then abort because it is using strict error checking. The connection to the client will be closed and the client will see a 500 error message. The following log messages will also be printed:
WARNING: Rule aborted during execution - Strict error checking enabled
WARNING: Rule <Rule Name>: Line 8: Cannot convert hash to integer value