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 host/url if the current url contains say "abc"

 

while i am using http::uri contains, some times the page is failing with http 404 error or Unknown https status text.

 

  • 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.

     

  • uni's avatar
    uni
    Icon for Altostratus rankAltostratus
    They're both useful ways of testing urls. 'contains' is likely more efficient that glob testing, depending on how you implement it. Post an example of your code - if there is something wrong with it, someone here is sure to help.
  • see below the samples i'm using when HTTP_REQUEST { switch -glob [HTTP::path] { "/abc/xyz-nz" - "/abc/nz/" { virtual vip1-443 } default { pool pool1 } } } when HTTP_REQUEST { if {[HTTP::uri] contains "/abc/xyz-nz" } { virtual vip1-443 } elseif {[HTTP::uri] contains "/abc/nz/*" } { virtual vip1-443 } else { pool pool1 } }
  • 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.

     

    • Ravi9_136822's avatar
      Ravi9_136822
      Icon for Nimbostratus rankNimbostratus
      Hi Michael, Thank you for your time in explaining this. It really helped to decide what statement to use. I am going with http:path with -glob and wildcard at the end of the url
  • 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.

     

    • Ravi9_136822's avatar
      Ravi9_136822
      Icon for Nimbostratus rankNimbostratus
      Hi Michael, Thank you for your time in explaining this. It really helped to decide what statement to use. I am going with http:path with -glob and wildcard at the end of the url