The TrafficScript Language
By using the TrafficScript language, you can write tailored traffic management rules to inspect, manage and route requests and responses within your Traffic Manager deployment.
TrafficScript rules can manage connections in any TCP-based or UDP-based protocol. The Traffic Manager executes designated rules whenever a new connection or network request is received, whenever it receives a response from a back-end server node, or at the completion of a transaction. TrafficScript rules can inspect the incoming and outgoing data in the connection, and other aspects such as the remote client address. They can also connect to external TCP or HTTP services on demand.
The TrafficScript rules can then modify the request or response (for example, rewriting the URL or headers in an HTTP request), set session persistence parameters, or decide how to route the request to the most appropriate pool. This makes it possible to control precisely how traffic is managed, using rules designed to meet to your specific hosting requirements.
This manual introduces the TrafficScript language, provides details of syntax and usage, and then includes a full reference guide to the available function set for version 21.4.
TrafficScript rules are stored in the Rules Catalog and applied to your Virtual Servers.
TrafficScript rules can use regular expressions. Before using regular expressions, read and understand the security considerations in the “Administration System Security” chapter of the Pulse Secure Virtual Traffic Manager: User’s Guide.
Use TrafficScript to perform any of these traffic management tasks:
•Inspect incoming or outgoing traffic and rewrite it fully or in part as desired.
•Restrict a Web site to a certain range of IP addresses.
•Apply selective management to elements such as Web spiders.
•Enable or disable functions for a given request or response (such as compression)
•Retry requests that generate errors a maximum number of times.
•Future-proof your services against any change in the back-end components of the system.
•Work around broken links and content on your Web site.
TrafficScript Examples
The following example TrafficScript rule can be used with HTTP requests. It handles the request as follows:
•Requests for “www.example.co.uk” are rewritten to “www.example.com”.
•Requests for .jsp pages are routed to a set of application servers (the pool named “JSPServers”).
•Requests for URLs beginning “/secure” are only allowed during office hours.
# Rewrite host header if necessary
if( http.getHeader( "Host" ) == "www.example.co.uk" ) {
http.setHeader( "Host", "www.example.com" );
}
$path = http.getPath();
# Give .jsp requests to the ‘JSPServers’ pool
if( string.endsWith( $path, ".jsp" ) ) {
pool.use( "JSPServers" );
}
# Deny access to /secure outside office hours
if( string.startsWith( $path, "/secure" ) ) {
if( sys.time.hour() < 9 || sys.time.hour() >= 18 ){
connection.discard();
}
}
The next example rule can be used with HTTP responses. It processes the response as follows:
•If the HTTP status code is 404 or 5xx, retry the request a maximum of 3 times.
•If the response contains references to www.example.co.uk, rewrite it by changing these references to www.example.com.
$code = http.getResponseCode();
if( $code == 404 || $code >= 500 ) {
if( request.getRetries() < 3 ) {
# Avoid the current node when we retry,
# if possible
request.avoidNode( connection.getNode() );
request.retry();
} else {
http.sendResponse( "312 Redirect", "text/plain",
"", "Location: /" );
}
}
# We're only going to process text/html responses, so
# break out of the rule if the response is of a
# different type...
if( http.getResponseHeader(
"Content-Type" ) != "text/html" ) break;
$response = http.getResponseBody();
if( string.contains( $response,"www.example.co.uk" ) ) {
$response = string.regexsub( $response,
"www.example.co.uk", "www.example.com", "g" );
http.setResponseBody( $reponse );
}