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.

With another three examples of cool iRules, this week's 20 Lines or Less shows even more things you can do in less than 21 lines of code. I still haven't heard much from you guys as to the kinds of things you want to see, so make sure to get those requests in.  I can build all sorts of neat iRules if you just let me know what would be helpful or interesting.  Otherwise I might just make iRules that make iRules. Scary.

This week we've got a couple forum examples and a contribution to the codeshare. Here's your epic, 10th edition of the 20LoL:

HTTP Headers in the HTTP Response


Dealing with HTTP headers is one of the most common tasks we see in iRules. One of the things that I've seen floating about the forums and elsewhere lately is the question of how to access that information in the Response portion of the HTTP transaction. Some people have had a problem with this, as many of those headers no longer exist (like, say, the host). It's a simple solution though, as you can see below...just use a variable to get you there.

  # Save the URI
  set uri [HTTP::uri]
  if {([HTTP::header Cache-Control] eq "private, max-age=3600") and ($uri ends_with “.html”)} {
    HTTP::header replace Cache-Control "public, max-age=3600"

Persistence equality in RDP sessions


This example solves an issue with mixing Linux and Windows based RDP sessions across a persistence enabled virtual Apparently there's an issue with trying to persist based off of the user string as some clients include user@local.host and others just include the username. That's a bit of an issue.  iRules to the rescue, as always.


  TCP::collect 25 
  binary scan [TCP::payload] x11a* msrdp 
  if { [string equal -nocase -length 17 $msrdp "cookie: mstshash="] } { 
    set msrdp [string range $msrdp 17 end] 
    set len [string first "\n" $msrdp] 
    if { $len == -1 } { TCP::collect } 
    if { $msrdp contains "@" } { 
      if { $len > 5 } { 
        incr len -1 
        persist uie [getfield $msrdp "@" 1] 10800
    } else { persist uie $msrdp 10800 } 

Pool Selection based on File Extension


Taking a page from the codeshare, this iRule lets you build a correlation of file extensions and pools that serve those particular file types. This can be quite handy when dealing with large scale image servers, media systems, and especially systems that do things like dynamically generate watermarks on images and the like. Take a peek.

switch -glob [HTTP::path] {
"*.jpg" -
"*.gif" -
"*.png" { pool image_pool }
"*.pdf" { pool pdf_pool }
default { pool web_pool }

There you have it; three more examples in less than 60 lines of code. I hope you're still finding this series helpful. As always, feel free to drop me a line for feedback or suggestions. Thanks!