Forum Discussion

Ravi9_136822's avatar
Ravi9_136822
Icon for Nimbostratus rankNimbostratus
Oct 31, 2013

switch -glob [HTTP::path] vs HTTP::uri contains

I would like to know the difference between switch -glob [HTTP::path] and HTTP::uri contains statements while writing I-rules for URL based redirection. The requirement is to redirect to a different...
  • Michael_Yates_6's avatar
    Nov 01, 2013

    Hi Ravi9,

     

    There is a threshold where if statements become less efficient than using a switch statement. The -glob is only necessary when you wish to use Wildcards (* or ?). If I remember correctly a switch statement becomes more efficient after 4 or more nested if statements, but I could be a little off (you can search on DevCentral for the research that others have posted on the subject).

     

    My ultimate suggestion is to keep efficiency in mind, but make supportability your primary concern (using something hyper efficient can make supporting it a Nightmare).

     

    With the code you posted the switch statement is absolute:

     

    If the HTTP::path does not equal (not more and not less...exactly matches) "/abc/xyz-nz" or "/abc/nz/" then these events would not qualify for the decision to go to "virtual vip1-443".

     

    To make it equal the "contains" statement you would have to do this (use Wildcards):

     

    when HTTP_REQUEST { 
        switch -glob [string tolower [HTTP::path]] { 
           "*/abc/xyz-nz*" -
           "*/abc/nz*" {
               virtual vip1-443
            }
           default {
               pool pool1
          }
       }
    }

    I would recommend using caution when using "contains" statements as well...they will look for a match anywhere in the HTTP::path and could result in a false match under the right circumstances.

     

    You can also use starts_with or ends_with if possible.

     

    In a switch statement using Wildcards you can get the same results:

     

    contains - "/something"

     

    starts_with - "/something*"

     

    ends_with - "*/something"

     

    Hope this helps.