20 Lines or Less #16

What could you do with your code in 20 Lines or Less? That's the question (almost) 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 I bring you some more groovy examples of the power that iRule can bring to the table in just a few short lines of code. We'll touch on Protocol specific header insertion, port re-writing, and configuring partial gets properly for PDF reading.  Many thanks to the awesome forum and CodeShare contributors that keep on adding these great examples.

 

Disable Partial Get Requests for Ram Cache

https://devcentral.f5.com/s/wiki/iRules.Disable_Partial_Get_Requests_For_Ram_Cache.ashx

Partial Get requests are normally a good thing, but in some cases, as with PDFs, it can hinder the way things operate and throw a wrench in the works.  This tiny little iRule can make sure that your cached PDFs aren't subject to this behavior.

when CACHE_RESPONSE {
  if { [HTTP::uri] ends_with ".pdf" } {
    HTTP::header remove "Accept-Ranges"
  }
}

 

Changing HTTP Header Host and Port

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

It's hoolio to the rescue yet again with a cool example of how to get rid of a custom port in the HTTP host header, as well as re-writing the Location info just to be sure the server isn't trying to undo our hard work.

when HTTP_REQUEST { 
    # Check if host value has a colon 
    if {[HTTP::host] contains ":"}{ 
       # Replace host header value with everything before the colon 
       HTTP::header replace Host "[getfield [HTTP::host] : 1]" 
    log local0. "[IP::client_addr]:[TCP::client_port]: Replace original host [HTTP::host] with [getfield [HTTP::host] : 1]" 
    } 

when HTTP_RESPONSE {
   # Check if response is a redirect
   if {[HTTP::is_redirect]}{
      # Replace :55555/ with / in the Location header
      HTTP::header replace Location [string map {:55555/ /} [HTTP::header value Location]]
   }
}

 

Protocol based header insert

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

This is a neat idea that I can't recall seeing recently, so I thought I'd share.  This user wanted to be able to check what protocol was in use for the transaction in question, and then based on that insert some specific information into a header. With iRules, you can.

when HTTP_REQUEST {
  if { [TCP::local_port] == 443 } {
    HTTP::header insert SSL 1
  } else {
    HTTP::header insert SSL 0}
  }
}

 

There's another 20LoL for you to sink your teeth into.  These brief examples of what iRules can do are great to keep in your back pocket. You never know when you're gonna need 'em. ;)


#Colin

Published Nov 21, 2008
Version 1.0

Was this article helpful?

No CommentsBe the first to comment