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.

With the announcement of the BIG-IP LTM VE this week, the iRules world just got a little more interesting.  Between the new commands and features that have come out in the past several months, and now the ability to build and test iRules from your laptop, without having to connect to the internet/network at all, the possibilities just keep expanding.  I’m excited to see what kinds of things people come up with and the scenarios I hear about with VEs running all sorts of wacky iRules that people didn’t dare try out on their systems before.  Amidst the amazing news of the BIG-IP LTM VE release and the ensuing rush of content, comments and questions surrounding it, the iRules world moves along just as it does every week. As such, I’ve got some more cool examples of iRules goodness exemplifying the brevity I’ve come to seek out. 

 

Strip HTTP Server

Looking for a simple way to remove all headers containing a given string? Perhaps you don’t want to use the sanitize command because you’re leaving in more headers than you’re taking out, but you want to remove some specific headers?  Well here you go. A simple little loop that’ll go through all your headers and remove those that match whatever criteria you lay out. In this case it’s removing all headers that begin with x-.

 when HTTP_RESPONSE { 

# Remove all instances of the Server header
HTTP::header remove Server

# Remove all headers starting with x-
foreach header_name [HTTP::header names] {

if {[string match -nocase x-* $header_name]}{

HTTP::header remove $header_name
}
}
}

 

Change Content Disposition

I’m pretty certain I’ve posted a way to do this here before, or maybe that was a tech tip..they all blur together.  This is a very quick, elegant way to do so however, so I wanted to share (even if it’s sharing the same concept again). 

 when HTTP_REQUEST {  
set querystring URI::query
}

when HTTP_RESPONSE {
if { $querystring contains "attachment=1" } {
HTTP::header replace Content-Disposition [string map -nocase "inline attachment" [HTTP::header Content-Disposition]]
}

}

 

Particularly Tricky Path Parsing

Someone was looking to parse the first two letters of the HTTP path, and came up with a very neat way of doing just that.  They’re performing structured matches based on character sets in a glob style match with switch. Very inventive and it looks quite efficient & functional.  Nicely played.

 when HTTP_REQUEST { 
# Parse the first then second character in the path
switch -glob [string tolower [HTTP::path]] {
"/a[a-l]*" {
pool reward-uat5.123.com_AA2AL
}
"/a[m-z]*" -
"/b[a-l]*" {
pool reward-uat5.123.com_AM2BL
}
"/b[m-z]*" -
"/c[a-d]*" {
pool reward-uat5.123.com_BM2CD
}
default {
# Requested URI was a leading forward slash only
pool reward-uat5.123.com_AA2AL
}
}
}
There’s your 20LoL for the week. Thanks for keeping up with the awesome examples guys. See you next week for three more.
#Colin