20 Lines or Less #30
What could you do with your code in 20 Lines or Less? That's the question I ask (almost) every week for the devcentral community, and every week I go looking to find cool new examples that show just how flexible and powerful iRules can be without getting in over your head.
Well we made it to 30 editions of the 20LoL. Soon we’ll break 100 iRule examples that are under 21 lines of code each. Pretty neat stuff, if you ask me. This week is the hoolio show, it seems. The guy is just a monster in the forums, what can I say? I sure am glad he’s on our side. I’ve got three examples that I randomly pulled from the forums because I thought they were cool. Only later did I realize that he had penned them all. So big thanks yet again to Aaron and all his hard work to better the community.
Pre-loaded searches based on host name
http://devcentral.f5.com/s/Default.aspx?tabid=53&forumid=5&postid=86016&view=topic
This cool little example is a neat spin on a simple HTTP redirect. The basic idea is to redirect to a given search site and set the search parameter to be the original host name of the request. So I could request bobschickenshack.com and be redirected to a search for bobschickenshack on the search page of my choosing. Very cool idea, and darn easy to implement.
when HTTP_REQUEST {
# Rewrite the host header to www.yahoo.com and the
# uri to /search?q=$host where $host is the originally requested hostname
HTTP::header replace "www.yahoo.com"
HTTP::uri "/search?q=[HTTP::host]"
}
More fun with nested switch
http://devcentral.f5.com/s/Default.aspx?tabid=53&forumid=5&postid=85807&view=topic
I know we’ve covered switch before, but this is yet another good use of it and I really like the idea of selecting snatpools based on which server the request is going to end up going to. I trimmed this one down a little but only by removing a few of the possible snatpool options, all logic is the same, even though it’s just an excerpt of the overall solution provided.
when LB_SELECTED {
switch [LB::server addr] {
222.35.42.126 {
switch [IP::client_addr] {
192.168.3.11 { snatpool snat_crt_test2 }
default { snatpool snat_crt_pool }
}
}
221.218.248.155 {
switch [IP::client_addr] {
192.168.3.11 { snatpool snat_uni_test2 }
default { snatpool snat_uni_pool }
}
}
default { snat automap }
}
}
Updating referrers
http://devcentral.f5.com/s/Default.aspx?tabid=53&forumid=5&postid=85807&view=topic
Hoolio does a good job of not only pointing out the inherent problem with trying to replace referrer headers with hostnames from requests, but giving an option that works as desired even if it’s a little bit different direction than the OP was headed. This is a good example of in-line string replacement with string map, too, which is an often under used command that’s worth a look.
when HTTP_REQUEST {
log local0. "[IP::client_addr]:[TCP::client_port]: New [HTTP::method] request to [HTTP::host][HTTP::uri]\
with Referer [HTTP::header Referer]"
if {[HTTP::header exists "MyHeader"]} {
log local0. "[IP::client_addr]:[TCP::client_port]: Updating Referer to\
[string map -nocase {http:// https://} [HTTP::header Referer]"
HTTP::header replace Referer "[string map -nocase {http:// https://} [HTTP::header Referer]"
}
}
when HTTP_REQUEST priority 501 {
log local0. "[IP::client_addr]:[TCP::client_port] (501): Current Referer [HTTP::header Referer]"
}
There you have it, 3 more iRules to show off just how much you can do in only 20 lines of code. Next time we’ll break past the 100 examples mark. See ya then.
#Colin