switch

Description

A built-in TCL command. Evaluates one of several scripts, depending on a given value. See the TCL wiki page for additional info:

Syntax

switch ?options? string pattern body ?pattern body …?

Matches its string argument against each of the pattern arguments in order. As soon as it finds a pattern that matches string, it evaluates the following body argument by passing it recursively to the Tcl interpreter and returns the result of that evaluation. If the last pattern argument is “default”, then it matches anything. If no pattern argument matches string and no default is given, then the command returns an empty string. Note that the match case “default” can occur more than once in the body as long as the second instance is the last match case. The first instance would match if the base string is the literal “default”; the second operates as the fallback if no other condition matches. See below for an example.
If the initial arguments start with “-“, then they are treated as options. The following options are currently supported:
-exact Use exact matching when comparing string to a pattern. This is the default.
-glob When matching string to the patterns, use glob-style matching (i.e. the same as implemented by the string match command).
-regexp When matching string to the patterns, use regular expression matching (i.e. the same as implemented by the regexp command).
Marks the end of options. The argument following this one will be treated as string even if it starts with a “-“.
Two syntaxes are provided for the pattern and body arguments. The first uses a separate argument for each of the patterns and commands; this form is convenient if substitutions are desired on some of the patterns or commands. The second form places all of the patterns and commands together into a single argument; the argument must have proper list structure, with the elements of the list being the patterns and commands. The second form makes it easy to construct multi-line commands, since the braces around the whole list make it unnecessary to include a backslash at the end of each line. Since the pattern arguments are in braces in the second form, no command or variable substitutions are performed on them; this makes the behavior of the second form different than the first form in some cases.
If a body is specified as “-” it means that the body for the next pattern should also be used as the body for this pattern (if the next pattern also has a body of “-” then the body after that is used, and so on). This feature makes it possible to share a single body among several patterns.
Often, in Tcl texts,  the switch ?options? section ends with a double-dash (–) before the match string is provided.  This is a safety mechanism.  If the string starts with a dash (-) and is not preceded by the double-dash, it will be interpreted as an option for the switch command itself.  So, unless you are certain that the match string does not and cannot start with a single dash, it is safest to precede the match string identifier with the double-dash.
Note: See the OneConnect wiki page for details on using OneConnect with a switch statement for selecting different pools in an iRule.

Examples

This example will return 2:

switch abc a - b {format 1} abc {format 2} default {format 3}

This example will return 3:

switch xyz {
      a
             -
      b
             {format 1}
      a*
             {format 2}
      default
             {format 3}
}

This example will send traffic with host header “www.domain.com” to pool www, host header “www.domain2.com” will cause header manipulation & URI rewriting to take place first, and requests with any other host header will be discarded:

switch -- [string tolower [HTTP::host]] {
    www.domain.com { pool www }
    www.domain2.com {
        HTTP::header insert Header1 domain2
        HTTP::header replace Host www.domain.com
        HTTP::uri "/domain2[HTTP::uri]"
        pool www
    }
    default { discard }
}

This example will select a pool based on the URI. Similar functionality could be gained using an HTTP class.
when HTTP_REQUEST {

   # Check the requested path (set to lowercase)
   # -glob: allow string pattern matching
   switch -glob -- [string tolower [HTTP::path]] {
      "/uri/for/pool1/*" -
      "*pool_one*" -
      "/another/uri/*" {
         log local0. "Matched pool 1 paths for [HTTP::uri]"
         pool pool1
      }
      "/uri/for/pool2/*" {
         log local0. "Matched pool 2 paths for [HTTP::uri]"
         pool pool2
      }
      default {
         log local0. "Hit default for [HTTP::uri]"
         pool pool_default
      }
   }
}

This example demonstrates the repeat use of the “default” case. If the Request-URI query parameter “layout” has a literal value of “default”, then the Request-URI is rewritten in a particular way. If no case matches, a response is generated directly by BIG-IP.
when HTTP_REQUEST {
    # direct actual request to an appropriate layout sub-area
    # the default layout is basic, but a layout type MUST be specified
    switch -- [findstr [HTTP::query] "layout=" 7 "&"] {
      "basic"     { HTTP::uri "/layout/basic[HTTP::uri]" }
      "advanced"  { HTTP::uri "/layout/advanced[HTTP::uri]" }
      "stylish"   { HTTP::uri "/layout/stylish[HTTP::uri]" }
      "default"   { HTTP::uri "/layout/basic[HTTP::uri]" }
      default {
         HTTP::respond 404 content {
            <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
            <html>
              <head><title>Layout Not Understood</title>
              <body>A layout query parameter is required.</body>
            </html>
         }
      }
    }
}
The last default case was left without quotes to visually distinguish it from the preceding case, but one, both, or neither could be quoted and the effect would be the same.
Another set of examples showing glob style wildcard usage:
when HTTP_REQUEST {

   # Check each case against the path with glob style wildcards
   # * = any character any number of times (including none)
   # ? = any character exactly once
   # [a-c] = a character range

   switch -glob [HTTP::path] {
      "/" {
         # Exact match for /
         HTTP::redirect "https://www.myapp.com/index.html"
      }
      "/custom*" {
         # URI starts with /custom
         HTTP::redirect "http://www.myapp.com/custom/"
      }
      "*.css" -
      "*.gif" -
      "*.jpg" -
      "*.js" {
         # URI ends with a static file type
         # All these cases use the same action.
         pool static_http_pool
      }
      "/app[0-9]*" {
         # URI starts with /app# where # is a digit
         HTTP::redirect "https://www.myapp.com/app"
      }
      "/app?" {
         # URI starts with /app? where ? is any single character
         #    as /app[0-9]* is before this, ? would be any non-digit character
         # Remove the /app? prefix from the path
         HTTP::path "/[string range [HTTP::path] 4 end]"
      }
      default {
         # No prior match so take some default action
         HTTP::redirect "https://www.myapp.com/default"
      }
   }
}

Here are two examples with slightly different syntax for how to use variables as switch cases. Both examples log “matched starts_with $case2: test2”.
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"
}

set case1 "test1"
set case2 "test2"

# 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"
    }