For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

Forum Discussion

Andrea_Arquint's avatar
Andrea_Arquint
Icon for Nimbostratus rankNimbostratus
Jun 17, 2013

using variable in a switch statement

Hi,

I would like to test a URI which is defined as a variable ($TEST_VAR):

when HTTP_REQUEST {

   set TEST_VAR "item"

   switch -glob [string tolower [HTTP::path]] {
      "/$TEST_VAR/*" {
         log local0. "Matched pool 2 paths for [HTTP::uri]"
         pool pool2
      }
      default {
         log local0. "Hit default for [HTTP::uri]"
         pool pool_default
      }
   }
}

But this does not work.

How can this be done? Is such thing possible somehow?

Thanx

bb

5 Replies

  • spark_86682's avatar
    spark_86682
    Historic F5 Account
    This is possible. In Tcl, curly braces prevent variable substitution, and you have a set of curly braces surrounding the entire body of your switch statement, so "/$TEST_VAR/*" won't have its variable substituted. You should be able to fix this just by removing those outer braces around your switch body:

     
       switch -glob [string tolower [HTTP::path]]
          "/$TEST_VAR/*" {
             log local0. "Matched pool 2 paths for [HTTP::uri]"
             pool pool2
          }
          default {
             log local0. "Hit default for [HTTP::uri]"
             pool pool_default
          }
    
  • hi

     

    I did that. Removed the curly braces as you showed but I could not save the irule within irule editor.

     

    irule editor message: Response is not well-formed XML.

     

    So, Ithink this cannot be done at the moment with irules.

     

    greetins

     

  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    If you add a \ at the end of the first line of the switch statement it should work. Here are two forms of the syntax which allow for variables in the switch cases:

    
    when RULE_INIT {
    
    set case1 "test1"
    set case2 "test2"
    
     Syntax version 1
    switch -glob -- "test2string1" $case1 {
    log local0. "matched exact \$case1: $case1"
    } $case2 {
    log local0. "matched exact \$case2: $case2"
    } $case2* {
    log local0. "matched starts_with \$case2: $case2"
    } default {
    log local0. "matched default case"
    }
    
     Syntax version 2
    switch -glob -- "test2string2" \
    $case1 {
    log local0. "matched exact \$case1: $case1"
    } \
    $case2 {
    log local0. "matched exact \$case2: $case2"
    } \
    $case2* {
    log local0. "matched starts_with \$case2: $case2"
    } \
    default {
    log local0. "matched default case"
    }
    }
    

    Aaron
  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus

    Summarized on the switch wiki page in the last two examples... https://devcentral.f5.com/wiki/iRules.switch.ashx