Forum Discussion
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.
- uniAltostratusThey'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.
- Ravi9_136822Nimbostratussee 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_136822NimbostratusHi 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
- Michael_YatesNimbostratus
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_136822NimbostratusHi 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
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com