Search
Colin Walker - Bettering Applications by Network Wizardry
You are here: DevCentral > Weblogs

posted on Thursday, June 26, 2008 1:58 PM

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

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

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.

when HTTP_REQUEST {
  # Save the URI
  set uri [HTTP::uri]
}
when HTTP_RESPONSE {
  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

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

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.

when CLIENT_ACCEPTED {
  TCP::collect

when CLIENT_DATA { 
  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 } 
  } 
  TCP::release 
}

Pool Selection based on File Extension

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

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.

when HTTP_REQUEST {
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!

#Colin



Feedback

No comments posted yet.

Let Me Know What You Think


Please use the form below if you have any comments, questions, or suggestions.

Title:
 
Name:
 
Email: (so we can show your gravatar)
Website:
Comment: Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, code
 
Please add 8 and 4 and type the answer here:

Blog Stats

Posts:221
Comments:77
Stories:0
Trackbacks:0
  

Games, Gaming, etc.

  

IT News and Info

  

Misc.

  

Add to Technorati Favorites

82,243 Members in 102 Countries and Growing!

Join DevCentral Today!

About DevCentral

DevCentral has been a successful, thriving community for many years. We have always strived to bring you the best technical documentation, discussion forums, blogs, media and much more that we can.

So dive in, get familiar with DevCentral. We hope you like it, we hope it makes your job easier, and lets you get that much more power out of the community. To learn more, make sure to check out the Getting Started section. And if you have any problems, or think something could be easier to use, drop us a line to let us know.

Got It !

We've received your comment and transmitted it directly to DevCentral HQ.

Thanks for taking time to let us know what's on your mind. At DevCentral | Community Matters!

Get In Touch With Us

Have questions, suggestions or just want to get something off your chest?

Use our handy form below to Direct Connect with DevCentral Mission Control.

Send Us Feedback       or