20 Lines or Less # 14

What could you do with your code in 20 Lines or Less? That's the question I ask every week, 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.

This week we've got three more fun examples of iRules goodness for you, thanks to the awesome community driving the forums and the CodeShare.  As always the goal is to show off some of the things that you can do with iRules in less than 21 lines of code. Let's dig in:

Distribute Email by Source IP

http://devcentral.f5.com/s/wiki/default.aspx/iRules/DistributeEmailBySourceIP.html

From the wiki comes this handy showcase of pool switching based on IP address. Putting this to good use by distributing info to different mail pools is an even cooler idea, which is just  what this iRule does.

when CLIENT_ACCEPTED {   if { [IP::addr [IP::remote_addr] equals 10.2.0.0/255.255.0.0 ] }  {     log local0. "Node IP address is: [IP::remote_addr] and sent to SMTP_clients_from_10.2"     pool smtp_clients_from_10.2   } else {     log local0. "Node IP address is: [IP::remote_addr] and sent to SMTP_clients_from_elsewhere"     pool SMTP_clients_from_elsewhere   }
}

 

String Based HTTPS Redirect

http://devcentral.f5.com/s/Default.aspx?tabid=53&forumid=5&postid=26517&view=topic

Yet another cool twist on the simple HTTP redirect iRules we've talked about before, this one uses a Location header in the response as well as a class of URIs to pick from. Even though it's a variation on a theme, it's cool to see the ways people are doing things, and remind us all that we have LOTS of options when it comes to iRuling.

 when HTTP_REQUEST {  
switch -glob [string tolower [HTTP::uri]] {
"*alumni/giving/gift/" -
"*alumni/giving/pledge/" -
"*alumni/directory/search.aspx" -
"*alumni/directory/update.aspx" {
# don't do anything...
}
default {
HTTP::respond 301 Location "http://[getfield [HTTP::host] : 1][HTTP::uri]"
}
}
}

 

HTTP Routing without HTTP Profile

http://devcentral.f5.com/s/Default.aspx?tabid=53&forumid=5&postid=26569&view=topic

This one is super cool (assuming it works..note that it's untested, but I like it anyway!). The issue here was that the user needed to inspect some HTTP information in a session without applying an HTTP profile to the Virtual Server. That means no HTTP:: commands. Oh no! Never fear, TCP::collect to the rescue!

 

 when CLIENT_ACCEPTED { 
TCP::collect
}
when CLIENT_DATA {
set idx [string first " HTTP/1." [TCP::payload]]
if { $idx < 0 } {
if { [TCP::payload length] > 2048 } {
log local0. "ERROR! Could not find HTTP request in 2K! dropping..."
reject
} else {
# Not enough data yet; collect more
TCP::collect
}
return
}
set request [string tolower [TCP::payload $idx]]
log local0. "Got request: $request"
if { $request contains " /XXXXX" } {
log local0. "Sending to Pool_XXXXX"
pool Pool_XXXXX
} else {
log local0. "Sending to Pool_WWWWW"
pool Pool_WWWWW
}
}
Hot off the presses, that's this week's 20LoL. Tune in again next week for even more iRule endeavors.
#Colin
Published Aug 29, 2008
Version 1.0

Was this article helpful?

No CommentsBe the first to comment