vADC Forum

Occasional Contributor
Posts: 5
Registered: ‎11-29-2012
Accepted Solution

Traffic script as function



Currently I am trying to make reverse proxying with stingray. It is working fine, but sometimes I need to make content rewritting. My question is: Is it possible to call a rule into another rule as a function?




I have the following rule ( request rules ):


$url = http.getpath();
if( string.startsWith($url,"/shinken/")){
$newUrl = string.replace($url,"/shinken/","/");



The Stingray returns the right result wihtout css, images... because path is wrong that is why I need to make rewriting. I am able to make rewriting and I make three rules: html, css, js rewriting


I have this rule ( response ):


$cookie = http.getcookie("Stingray");

if( !string.startsWith( http.getResponseHeader("Content-Type"),"text/html")){


}else {

if( $cookie == "shinken" ){

$body = http.getResponseBody();

$line = string.regexsub($body,"href=['\"](+)['\"]","href=\"/shinken$1\"","g");

$line = string.regexsub($line,"src=['\"](+)['\"]","src=\"/shinken$1\"","g");

$line = string.regexsub($line,"action=['\"](+)['\"]","action=\"/shinken$1\"","g");






It is working fine but what I want is to use this rule for other entries which need the rewriting.


Do you have a solution?


Best regards,


Frequent Contributor
Posts: 321
Registered: ‎11-29-2012

Re: Traffic script as function

Take a look at the 'Libraries' capability in TrafficScript.  This lets you centralize common code in a 'library' (just a standard TrafficScript rules) and then reference that code from other rules.

The TrafficScript manual describes Libraries in detail.  In brief:

Create a rule to be used as a library - the rule is named 'debug':

# per-connection debugging to the event log

sub on() { "mydebug", 1 );


sub off() { "mydebug", 0 );


# only logs the msg if debug is on for this connection

sub log( $msg ) {

  if( "mydebug" ) ) { $msg );



Now import and use that rule within another rule:

# For demo purposes (so that the map looks good), fake the source addres

# of this connection

# Here's where we pull in another rule ('library')

import debug;

# Comment out the following line to disable debugging


$countries = [ "US", "CA", "GB", "FR", "ES", "AU", "NZ", "DE", "CH", "IT", "GR", "RU" ];

while ( 1 ) {

  $a = random( 254 ) + 1;

  $b = random( 254 ) + 1;

  $c = random( 254 ) + 1;

  $d = random( 254 ) + 1;

  $ip = $a . '.' . $b . '.' . $c . '.' . $d;

  debug.log( "Trying IP address: " . $ip );

# now verify that the generated IP is valid

  if( array.contains( $countries, geo.getCountryCode( $ip ) ) ) break;


debug.log( "Picked " . $ip . " in " . geo.getCountry( $ip ) );

request.setRemoteIP( $ip );

# not strictly necessary...;

Hopefully, this illustrates how to place functions in a separate rule ('library') and then invoke them from a rule that is associated with a virtual server.



Join the Community

Get quick and easy access to valuable resource designed to help you manage your Brocade Network.