20 Lines or Less #22
What could you do with your code in 20 Lines or Less? That's the question I ask (almost) 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.
Wow! It's been a while since I was able to pull the cycles away from other things to get to the 20LoL. Many apologies to any of you out there that have missed it. Things here have been amazingly busy (in a good way) and I just haven't made the time. There are so many cool iRules to cover, I'll have plenty of fodder in weeks to come, so hopefully things will be more regular. This week we'll cover three cool, short iRules, as always, but this week we have a special request for the 20LoL that I'll be including just for DevCentral's own Joe Pruitt.
IE8 Compatibility
http://devcentral.f5.com/s/Default.aspx?tabid=53&forumid=5&tpage=1&view=topic&postid=56407#56534
Here's a cool iRule that Joe requested get tossed into the mix for the 20LoL, and I'm more than happy to oblige. This cool rule inspects an incoming user's request to see if they're using IE8. If so, it inserts the necessary headers to force them into IE7 compatibility mode. This can be handy for now since not everything works perfectly with IE8 just yet.
when HTTP_REQUEST {
# Check if User-Agent header is IE 8
if { [HTTP::header User-Agent] contains "MSIE 8" } {
set IE8Resp 1
} else {
set IE8Resp 0
}
}when HTTP_RESPONSE {
if { $IE8Resp } {
log "[IP::client_addr]:[TCP::client_port] inserting header"
HTTP::header insert "X-UA-Compatible" "IE=EmulateIE7"
}
}
Redirecting from Java Strings
http://devcentral.f5.com/s/Default.aspx?tabid=53&forumid=5&postid=56263&view=topic
This request in the forums was to expand the scope of an originally straight-forward HTTP redirect rule to do a little bit more inspection, and route people to different places based on the results. It's great to see people not only using but modifying and expanding their iRules as time goes on. Good stuff.
when HTTP_REQUEST {
if { [HTTP::path] equals "/" }{
if { not ([string tolower [HTTP::query]] starts_with "user=soviet") }{
if { [HTTP::header "User-Agent"] contains "soviet" }{
HTTP::redirect "http://soviet.com/"
}
}
}
}
Display log messages to your browser
The original version of this iRule comes straight from the iRules CodeShare in the Wiki, linked to above. It's a cool idea of being able to display the messages your request is generating from the server directly to a browser, rather than forcing a developer or admin to repeatedly view the log file for updates while testing / coding. This would be used with some controls in place and in moderation of course, but still a very cool idea. It took a little tinkering to get it under 20 lines, but the functionality should be the same, and we all know I love "squeezing" code. ;)
]*>} [HTTP::payload] BodyLocationwhen HTTP_REQUEST {
append ::webLogMsg "HTTP URI: [HTTP::uri]"
}
when HTTP_RESPONSE {
if {([IP::client_addr] equals "x.x.x.x") && ([info exists ::webLogMsg]) } {
if {[HTTP::header "Content-Length"] > 2000} {
HTTP::collect 2000
} else {
HTTP::collect [HTTP::header "Content-Length"]
}
}
}
when HTTP_RESPONSE_DATA {
regexp -indices {(?i)
HTTP::payload replace [lindex $BodyLocation 1] 1 [concat {>
"Client Port: [TCP::client_port]
$::webLogMsg" {
unset ::webLogMsg
HTTP::release
}
Thanks for reading the 20LoL this week. Hopefully you'll be back for more awesome iRules examples in less than 21 lines.
#Colin
Listening to: Tool - ÆNIMA - Forty Six and 2