20 Lines or Less #11

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.

This week's 20LoL comes care of both the codeshare and the forums alike.  I got to deal with a couple of particularly cool forum posts this week, one of which made the list, as did an iRule from the infamous hoolio himself. Dealing with HTTP and ranging from spiders to working around a work-week, these examples are yet more ways you can leverage iRules in less than 21 lines. Here we go:

 

Rate Limiting Search Spiders

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

Spiders on the web aren't the same pests as spiders in your house, but they can certainly have adverse effects if they're making an inordinate number of requests to your web-servers, and driving the load up.  Here's a cool example of how to avoid just that scenario. We've seen something similar a long time ago on DevCentral for Network Computing, but this is a good refresher.

when RULE_INIT {
  array set ::active_crawlers { }
  set ::min_interval 1
}

when HTTP_REQUEST {
  set user_agent [string tolower [HTTP::header "User-Agent"]]
  # Logic only relevant for crawler user agents
  if { [matchclass $user_agent contains $::Crawlers] } {
    # Throttle crawlers.
    set curr_time [clock seconds]
    if { [info exists ::active_crawlers($user_agent)] } {
      if { [ $::active_crawlers($user_agent) < $curr_time ] } {
        set ::active_crawlers($user_agent) [expr {$curr_time + $::min_interval}]
      } else {
        reject
      }
    } else {
      set ::active_crawlers($user_agent) [expr {$curr_time + $::min_interval}]
    }
  }
}

 

Compression During the Work Week

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

Coming through with a great example of how to have compression enabled only from 8AM-5PM, otherwise known as the normal US Workday, citizen_elah shows of his iRules kung fooery to help a fellow community member out. This same logic could be applied to almost anything else, besides compression, making this a great iRule to keep around in your back pocket.

when CLIENT_ACCEPTED {    
set time_r [split [clock format [clock seconds] -format {%k:%M} ] " "]
set time_f [expr {[expr {[lindex $time_r 0]*100}] + [lindex $time_r 1]}]
if { not(($time_f >= 800) && ($time_f set compression "off"
}
}

when HTTP_RESPONSE {
if { $compression eq "off" } {
COMPRESS::disable
}
}

I then came through and offered some optional optimization, so I guess this could be considered your bonus-rule for the week.  It's easy when someone like elah does the legwork up front. ;) Check the link to see the extra example.

 

Fully Decode URI

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

Representing the last leg of this HTTP tri-ath-a-post is an entry from our illustrious iRules CodeShare. This example shows how to be sure you're FULLY decoding your URI before processing. It correctly points out that sometimes encoded characters can contain encoded characters can contain encoded characters can contain....well, you get the point. See how one person decided to work around such issues in a scant 11 lines of code.

when HTTP_REQUEST {
# decode original URI.
set tmpUri [HTTP::uri]
set uri [URI::decode $tmpUri]

# repeat decoding until the decoded version equals the previous value.
while { $uri ne $tmpUri } {
set tmpUri $uri
set uri [URI::decode $tmpUri]
}
HTTP::uri $uri

log local0. "Original URI: [HTTP::uri]"
log local0. "Fully decoded URI: $uri"
}
 

There you have it, three more choice examples of iRules goodness in 20 Lines or Less. Tune in again next week!

#Colin

Published Jul 17, 2008
Version 1.0

Was this article helpful?

1 Comment

  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    That's a great point, assuming people follow RFCs 100% of the time. Unfortunately, they don't.

     

     

    Whether or not you choose to use the rule as is or modify it I'll leave it up to you. It's just intended as food for thought, really, to show some of the cool things that you can do with iRules.

     

     

    Thanks for the comment!

     

     

    Colin