variables in a switch statement

What are my options for variable expansion in switch cases? It seems to me that in the following code, when i visit http://example.com/bar/test, I should get “you found foo.” for my page content. In practice, this isn’t the case. What do I need to do to have the variable $foo expanded inside the switch?

Thanks.

when HTTP_REQUEST {
  set foo "bar"
  switch -glob -- [URI::decode [HTTP::uri]] {
    "/$foo/*" { HTTP::respond 200 content "you found foo." }
  }
}

[root@ve1023:Active] config  b virtual bar list
virtual bar {
   snat automap
   pool foo
   destination 172.28.19.79:80
   ip protocol 6
   rules myrule
   profiles {
      http {}
      tcp {}
   }
   vlans external enable
}
[root@ve1023:Active] config  b rule myrule list
rule myrule {
   when HTTP_REQUEST {
        set foo "bar"
        switch -glob -- [URI::decode [HTTP::uri]] "
                /$foo/* { HTTP::respond 200 content {you found foo.} }
        "
}
}

[root@ve1023:Active] config  curl -i http://172.28.19.79/bar/abc
HTTP/1.0 200 OK
Server: BigIP
Connection: Keep-Alive
Content-Length: 14

you found foo.

another one.

[root@ve1023:Active] config  b rule myrule list
rule myrule {
   when HTTP_REQUEST {
        set foo "bar"
        switch -glob -- [URI::decode [HTTP::uri]] \
                "/$foo/*" { HTTP::respond 200 content "you found foo." }
}
}

[root@ve1023:Active] config  curl -i http://172.28.19.79/bar/abc
HTTP/1.0 200 OK
Server: BigIP
Connection: Keep-Alive
Content-Length: 14

you found foo.

What the heck?!?! Now that I read the official TCL doc closely (http://www.tcl.tk/man/tcl8.4/TclCmd/switch.htm), the first example does explicitly state that variables can be used with the syntax used in your second example. But what’s up with the first example - how did you come up with that one?

how did you come up with that one?i remember brace and double quote is interchangeable (except variable substitution) but honestly i forgot where i remember the 1st example form. i might mix it up in my mind. it may not be indeed correct. sorry about that. ^ ^

Here’s one place we discussed it:

http://devcentral.f5.com/Community/GroupDetails/tabid/1082223/asg/50/aft/1177070/showtab/groupforums/Default.aspx

There’s another one but I can’t find it at the moment.

Aaron

Thanks Aaron.

Thanks, Nitass. I’m using your first example because I prefer its structure… But I’m running into a problem. I have code like this:

set foo "bar"
        switch -glob -- [URI::decode [HTTP::uri]] "
                /$foo/* { HTTP::respond 200 content {you found foo.} }
                default {
                  set x {hello}
                  log local0. {$x}
                }
        "

Executing this results in "TCL error: testirule - can’t read “x”: no such variable. Any idea why?

maybe the 2nd example is better. can you try this?

[root@ve1023:Active] config  b rule myrule list
rule myrule {
   when HTTP_REQUEST {
        set foo "bar"
        switch -glob -- [URI::decode [HTTP::uri]] \
                "/$foo/*" { HTTP::respond 200 content "you found foo." } \
                default {
                  set x "hello"
                  log local0. $x
                }
}
}

[root@ve1023:Active] config  curl -i http://172.28.19.79/bar/abc
HTTP/1.0 200 OK
Server: BigIP
Connection: Keep-Alive
Content-Length: 14

you found foo.

I added these to the DC wiki page for switch:

Aaron