20 Lines or Less #75: URIs, URIs and more URIs

What could you do with your code in 20 Lines or Less?

That's the question I like to ask for the DevCentral community, and every time I go looking to find cool new examples that show just how flexible and powerful iRules can be without getting in over your head.

One of the most common things manipulated by iRules out in the wild is the Host or URI HTTP header values. This is done for a myriad of reasons ranging from routing to obfuscating portions of an application to … you name it. Given that it’s so common amongst iRulers worldwide it’s no shock that I managed to track down three recent examples of people doing URI-ish things with iRules.

So in the first of what I hope to be several “themed” 20 Lines or Less installments, here are three ways that people are using iRules to handle their URI needs.

 

Lowercase URI

https://devcentral.f5.com/s/questions/lowercase-uri

Ensuring that comparisons are done in a single case is one of those basic but super important things that we all do when we’re writing code. As such precisely no one is surprised that it’s also extremely common, and important, in iRules. The string command has a tolower function that takes care of this for us in TCL. Making sure we’re only using it when and how we want, however, sometimes takes inspection of the URI. In this case the OP was looking for a way to only modify the URI for any incoming “.aspx” requests. The below iRule should was the solution posted by a helpful community member that should get them there. Note the handy "Only re-write if the URI contains uppercase characters" logic with string match to prevent recursion! Tasty.

 
when HTTP_REQUEST {
  if { ([string match {[A-Z]} [HTTP::uri]] and [[HTTP::path] ends_with ".aspx"])} {
    set hostname [string tolower [HTTP::host]]
    set uriname [string tolower [HTTP::uri]]
    log local0. "Request is $hostname$uriname"
    HTTP::redirect "http://$hostname$uriname" 
  } 
}

 

Append Text to a URI

https://devcentral.f5.com/s/questions/append-text-to-a-uri

Apart from inspecting the URI and modifying case, modifying the actual contents of the URI is the next most common action on my completely unofficial or scientific list of “Things User do with URIs”. As such, here’s a simple example of a user conditionally appending a slash to the inbound URI.

when HTTP_REQUEST {
  if {!([HTTP::path] ends_with "/")} { 
    HTTP::path "[HTTP::path]/"
    HTTP::redirect [HTTP::uri]
  }
}

 

Switch on HTTP::host and HTTP::uri

https://devcentral.f5.com/s/questions/how-to-use-an-irule-switch-to-match-httphost-and-httpuri

Sliding ever so slightly higher on the “How scary can URIs actually be?” scale (Hint: the answer is generally ‘not very scary…mostly’) we have a user looking to perform a switch on a Host/URI combo. Still not tough, but super handy and worth seeing in case you’re new or rusty, this switch will give you the full host/uri/path of the inbound request and let you take separate actions on any particular match.

when HTTP_REQUEST { 
  switch -glob [string tolower [getfield [HTTP::host] ":" 1][HTTP::uri]] {
    "www.liveapp.customer.co.uk/app2easypath_" { 
      HTTP::path [string map { /app2easypath /realpath } [HTTP::path]] }; 
      SSL::disable serverside; 
      pool CUST1-WEB-PROD-APP2-Pool; 
    }
    "www.testapp.customer.co.uk/app2easypath_" {  
      HTTP::path [string map { /app2easypath /realpath } [HTTP::path]] }; 
      SSL::disable serverside; 
      pool CUST1-WEB-PROD-APP2-test-Pool; 
    }
    default {
      HTTP::redirect http://www.google.com} 
    }
  }
}
Published Jun 17, 2014
Version 1.0

Was this article helpful?

5 Comments

  • string tolower 'shouldn't' be necessary on a rfc compliant request, but it's a good idea to catch all those benign and malicious exceptions. wouldn't the first irule end up in a redirect loop for uri's ending with .aspx? There isn't any actual action going on there.

     

     

  • The original iRule we were working from was this:

     

     

    when HTTP_REQUEST {

     

    if { ([string match {[A-Z]} [HTTP::uri]] and [[HTTP::path] ends_with ".aspx"])} {

     

    set hostname [string tolower [HTTP::host]]

     

    set uriname [string tolower [HTTP::uri]]

     

    log local0. "Request is $hostname$uriname"

     

    HTTP::redirect "http://$hostname$uriname"

     

    }

     

    }

     

     

    This one shouldn't encounter redirect loops.
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    Funny, I should have looked it over better before copy and pasting. I'll fix the redirect loop. ;) Thanks Jason!
  • Might want to update "Append text to a URI", its been revised to exclude files such as mypage.php