How to streamline an irule?
January 9, 2015 3:02 PM   Subscribe

My organization has an F5 appliance that is doing some fairly heavy web redirecting. These redirects are in the form of an irule, which is written in TCL. It works fine. But, if you can believe it, we are nearing the 65,520 character limit for this irule, and I am only enough of a TCL coder to get the thing to this point, barely, but not enough to make it efficient.

The irule looks like:
if { [string tolower [HTTP::uri]] equals ("/this1") } { HTTP::redirect "/that1" }
if { [string tolower [HTTP::uri]] equals ("/this2") } { HTTP::redirect "/that2" }
if { [string tolower [HTTP::uri]] equals ("/this3") } { HTTP::redirect "/that3" }
if { [string tolower [HTTP::uri]] equals ("/this4") } { HTTP::redirect "/that4" }
if { [string tolower [HTTP::uri]] equals ("/this5") } { HTTP::redirect "/that5" }
if { [string tolower [HTTP::uri]] equals ("/this6") } { HTTP::redirect "/that6" }
if { [string tolower [HTTP::uri]] equals ("/this7") } { HTTP::redirect "/that7" }
It does what it needs to do. But I see that list of several hundred...
[string tolower [HTTP::uri]]
...all in a row and it seems I should be able to say:
aa = [string tolower [HTTP::uri]]
once, and then I could use a whole bunch of
if { aa equals ("/this1") } { HTTP::redirect "/that1" }"
and save myself about 10,000 characters. Right?

Can I do that with TCL? Can you see any other ways I could make this thing slimmer?

Note: the real URLS are effectively random - not sequential, and not predictable replacements like my fake example.
posted by anonymous to Computers & Internet (7 answers total) 1 user marked this as a favorite
I would put the URLs in an array and have one if-block that does a lookup:
array set urls {
  "/this1" "/that1"
  "/this2" "/that2"
  "/this3" "/that3"
  "/this4" "/that4" 

set source [string tolower [HTTP::uri]]
if {[info exists urls($source)]} { HTTP::redirect $urls($source) }
My TCL is super rusty and I don't have any way to test this. There are probably some bugs/typos but I think the logic of the example is basically sound.
posted by indyz at 3:32 PM on January 9, 2015

Just so, but F5's also have lookup tables. What you need to do if you haven't already is hie thee to their devcentral site where people are really helpful.
posted by idb at 3:54 PM on January 9, 2015 [1 favorite]

What idb says. We've got a bunch of LTMs where I work, and DevCentral is the place you need to take this question.
posted by Kreiger at 4:02 PM on January 9, 2015

I find that someone's always asked the question before. I popped over there and found this:

but I think you need to create a data group (same thing as a class).
posted by idb at 4:12 PM on January 9, 2015

btw, although some of the articles talk about external files for data groups, I think there's a web ui for creating data groups too. Too lazy to log onto one of ours right now to check.

see here's another sample redirect iRule that does the same thing:
posted by idb at 4:14 PM on January 9, 2015

Also, get rid of the unneeded spaces. I don't know TCL, but if it's like similar languages, spaces are unnecessary in many places. In your example, the spaces before and after braces and brackets are probably unneeded:

if{[string tolower[HTTP::uri]]equals("/this4")}{HTTP::redirect "/that4"}

That removes 9 spaces - it's gone from 80 characters to 71 reducing it more than 10%.
posted by ShooBoo at 5:04 PM on January 9, 2015

A close relative of mine is an expert in this and he says you do, as idb says, need a data group (which will be gargantuan but which doesn't have a character limit).
posted by gingerest at 3:37 AM on January 10, 2015

« Older NYC shelter for undocumented immigrant women?   |   What can I wear under a sheer blouse that won't... Newer »
This thread is closed to new comments.