Modify URI among other things

Hello Everyone,

Here’s what I am trying to do:

I have a datagroup called payment_resp_1

When I receive a request I want to match it to the URIs in the datagroup, modify it, and then forward it to a specific member in a pool.

Here is the contents of the datagroup:

/paymentresponse1.jsf

/paymenterror1.jsf

/paymentsuccess1.jsf

So if the HTTP request comes in as http://192.168.1.2:434/paymentresponse1.jsf I would like to modify it to /CSU/paymentresponse.jsf and forward it to a specific member in the pool, say for example 10.48.52.58.

and if it came in as http://192.168.1.2:434/paymenterror1.jsf I would like to modify it to /CSU/paymenterror.jsf and forward it the same way.

Here is an iRule that I wrote to start off. Can someone please take a look at it and let me know if I am in the right track and if possible supply a better solution.

when HTTP_REQUEST {

if {[matchclass [HTTP::uri] equals $::payment_resp_1]} {

HTTP::uri “/CSU/paymentresponse.jsf”

pool payment_resp member 10.48.52.58 8080

}

}

Thanks in advance

Which LTM version are you running? Do you want to have an old URI, new URI and pool for each datagroup element?

Aaron

I am running v9. I am not completely sure about what you are asking.

The old URI will determine what the new URI will be. So if the old is /paymentresponse1.jsf the new one would be /CSU/paymentresponse.jsf. If it is /paymenterror1.jsf the new one will be /CSU/paymenterror.jsf

I wrote this iRule but I think there should be a more efficient way of doing it. This rule doesnt use a datagroup. I wanted to use a datagroup because I plan on creating a rule that will include other members in a pool and including different possible URIs.

In the future there could be a PaymentResponse2.jsf and PaymentResponse3.jsf…if it’s PaymentResponse2.jsf it’ll go to a different host and if it’s PaymentResponse3.jsf it’ll go to another host.

The way I wanted to set up the datagroups was to group all the URIs with a 1 in them together, the ones with a 2 together, and the ones with a 3 together.

when HTTP_REQUEST {

set uri [HTTP::uri]

if { [HTTP::uri] equals “/PaymentResponse1.jsf”}{

HTTP::uri “/CSU/PaymentResponse.jsf”

pool payment_resp member 10.48.52.58 8080

log local0. “Received req1 response for $uri”

}

elseif { [HTTP::uri] equals “/PaymentError1.jsf”}{

HTTP::uri “/CSU/PaymentError.jsf”

pool payment_resp member 10.48.52.58 8080

log local0. “Received req1 response for $uri”

}

elseif { [HTTP::uri] equals “/PaymentDeclined1.jsf”}{

HTTP::uri “/CSU/PaymentDecline.jsf”

pool payment_resp member 10.48.52.58 8080

log local0. “Received req1 response for $uri”

}

elseif { [HTTP::uri] equals “/PaymentSuccess1.jsf”}{

HTTP::uri “/CSU/PaymentSuccess.jsf”

pool payment_resp member 10.48.52.58 8080

log local0. “Received req1 response for $uri”

}

elseif { [HTTP::uri] equals “/PaymentTimeOut1.jsf”}{

HTTP::uri “/CSU/PaymentTimeOut.jsf”

pool payment_resp member 10.48.52.58 8080

log local0. “Received req1 response for $uri”

}

}

For 9.x you could create a string datagroup which has the following fields:

original_uri new_uri node_ip node_port

/PaymentResponse1.jsf /CSU/PaymentResponse.jsf 10.48.52.58 8080

You could use the findclass command to look up the requested URI against the datagroup. If you find a matching line using [lindex $line 0] to get the new URI, [lindex $line 1] to get the IP and [lindex $line 1] to get the port.

Here’s an untested example:

when HTTP_REQUEST {

   set line [findclass [HTTP::path] uri_class]
   if {$line ne ""}{
      HTTP::uri [lindex $line 0]
      node [lindex $line 1] [lindex $line 2]
   }
}

Aaron

Or if you are using the same node address and port for all the entries in the datagroup, you could change leave that out of the datagroup:

original_uri new_uri datagroup

/PaymentResponse1.jsf /CSU/PaymentResponse.jsf

when HTTP_REQUEST {

   set uri [findclass [HTTP::path] uri_class]
   if {$line ne ""}{
      HTTP::uri $uri
      node 10.48.52.58 8080
   }
}

If you’re on 9.4.3 or lower you’ll want to add a $:: prefix to the datagroup name in the iRule.

Aaron

Can you please explain the following line?

if {$line ne ‘’‘’}

In his original example he was doing :

set line [findclass [HTTP::path] uri_class]

So the if $line ne “” makes more sense. In the second example you’d need to change it to “if {$uri ne “”} {”

All he’s doing is making sure that the URI or path in question was found in the class and didn’t return a null result.

Colin

So it should look like this?

when HTTP_REQUEST {

set uri [findclass [HTTP::path] uri_class]

if {$uri ne “”}{

HTTP::uri $uri

node 10.48.52.58 8080

}

}

So it should look like this?

when HTTP_REQUEST {

set uri [findclass [HTTP::path] uri_class]

if {$uri ne “”}{

HTTP::uri $uri

node 10.48.52.58 8080

}

}

Yep, that looks right to me.

Colin

I’m actually a little confused by that rule. I don’t see where it looks at the requested URI and changes it to the new URI. By looking at the rule above, it looks like it’s saying to match the requested URI against the data group and if it sees a match, then change the uri back to $uri…or maybe im just not getting it. Can you help me understand it?

Well, after looking again it looks like the rule needs just a little updating. Try this:

when HTTP_REQUEST {

   set uri [findclass [HTTP::path] uri_class " "]
   if {$line ne ""}{
      HTTP::uri $uri
      node 10.48.52.58 8080
   }
}

With a class where each line looks like:

“original_uri new_uri”

Okay, now for the explanation. What this is doing is looking through the class to find the current HTTP::path (the portion of the URI before the query string). Once it finds the current path in the data group, it will retrieve whatever comes after the separator (the " " in this case) and return that as the value of the findclass.

So, that means that in the above case, assuming [HTTP::path] were “original_uri”, the findclass command would return “new_uri”. Then the HTTP::uri $uri command sets the uri to be equal to $uri, which we just established was “new_uri”.

Basically, you’re creating key->value pairs within your data group. The key is whatever is before the space in each string. This should be the original URI that you want to search for. The value comes after the space, and that is whatever you want the original URI to be re-written to. The iRule then automagically goes through and does just that, then sends the connection on to the specified node.

Make sense?

Colin

Yes I think I understand now. Thank you very much for the explanation.

So when I go to enter a string in the datagroup using the GUI, I will enter it as

/paymentresponse1.jsf /csu/paymentresonse.jsf

/paymenterror1.jsf /csu/paymenterror.jsf

/paymentdeclined1.jsf /csu/paymenterror.jsf

each line being set up as original_uri new_uri?

Exactly. :slight_smile:

Colin

Sweet!

Can I change the last line to this:

when HTTP_REQUEST {

set uri [findclass [HTTP::path] uri_class " "]

if {$line ne “”}{

HTTP::uri $uri

pool payment_resp member 10.48.52.58 8080

}

}

Of course! That should work just fine.

Colin

Thank you for all of your help. I will test this and hope everything works. :slight_smile:

Sure thing, let us know if you run into any trouble, or have any other questions.

Colin

I am using version 9.3.1. Would I still be able to use the findclass command, or should I use matchclass?