Contribute Open Script

[BRCD_OS_101] URL Switching

by Derek_Kang on ‎01-06-2012 02:08 PM (9 Views)

Area:

Content Inspection

Traffic:

HTTP

Description:

            This script will analyze the URL in incoming HTTP traffic. If URL matches one of the patterns, it will be sent to the corresponding group. If the URL does not match any pattern, a reset will be sent to client. Finally, if the URL matches a pattern, and a server-group is selected, but all real servers are down in the group, an error with code 500 will be sent to the client.

Performance Estimate

            TBD

Required Backend configuration

            No special configuration is required

Events and API Used

Events

API

SERVER_SELECTION_FAILURE

OS_HTTP_REQUEST::version

OS_SLB::reply(string)

HTTP_REQUEST

OS_HTTP_REQUEST::url

OS_SLB::forward("group")

Script source code

###########################################################

# Purpose: Check URL and forward to different server groups

# URL with

# - hello10.html forwarded to group 10

# - hello20.html forwarded to group 20

# - hello30.html forwarded to group 30

# - If no match, send reset to Client

use OS_SLB;

use OS_HTTP_REQUEST;

#############################################

#

# HTTP REQUEST EVENTS

#

#############################################

sub HTTP_REQUEST {

       # Get the URL from the request

       my $client_url = OS_HTTP_REQUEST::url;

       print "\n\nClient Request has this URL - $client_url\n\n";

       # Group selection based on the URL in the request

       if ($client_url =~ m/hello10.html$/) {

              print "Matched hello10.html";

              OS_SLB::forward("10");

       } elsif ($client_url =~ m/hello20.html$/) {

              print "Matched hello20.html";

              OS_SLB::forward("20");

       } elsif ($client_url =~ m/hello30.html$/) {

              print "Matched hello30.html";

              OS_SLB::forward("30");

       } else {

              print "Did not match predefined URL. Send reset to client";

              OS_SLB::reset_client();

       }

}

sub SERVER_SELECTION_FAILURE {

       #print "*** Selected group Servers not available. Send Error Code 500";

       my $req_vers = OS_HTTP_REQUEST::version;

       my $err_code = "HTTP/$req_vers 500 Server Error\r\nConnection: Close\r\nContent-Length: 0\r\n\r\n";

       OS_SLB::reply($err_code);

}

Comments
by aaron.hackney on ‎10-11-2012 07:51 AM

OS_SLB::reply_error($err_code);

The method reply_error appears to have been removed from the code base? This throws an error: Undefined subroutine &OS_SLB::reply_error


Downloading a new version of the API guide from the 12.4a downloads section, this appears to have been changed to OS_SLB::reply (Page 114 ADX OpenScript API Guide - Supporting Brocade ServerIron ADX version 12.4.00a).

by Derek_Kang on ‎10-16-2012 01:47 PM

Thanks for pointing it out! I'll correct it.