Forum Discussion

Matt_Breedlove_'s avatar
Matt_Breedlove_
Icon for Nimbostratus rankNimbostratus
Jul 17, 2012

switch glob tcl string match support

Hi All

 

 

When using switch -glob, I know it supports square bracket char ranges such as [a-f], but does it support either of these?

 

 

Either "a" or "d" or "f"

 

[a,d,f]

 

 

or

 

 

Either "a" or "d" or "f"

 

{a,d,f}

 

 

or am I stuck with a more broad? [a-f]

 

 

This tcl man page below references the curly brace way, but its not mentioned in the usually quoted tcl man page on these forums

 

 

http://www2.tcl.tk/3264

 

 

Thanks

 

 

  • Hi Matt,

    I am guessing that you are trying to do something like this.

    
    when HTTP_REQUEST { 
    switch -glob [string tolower [HTTP::uri]] {
    "/test[a,c,e]" { HTTP::redirect "http://www.google.com" }
    "/test[b,d,f]" { HTTP::redirect "http://www.yahoo.com" }
    }
    }
    

    The Virtual Server IP Address or the application FQDN that will get you to the Virtual Server + /test and the letter will give you the varying results.

    Hope this helps.
  • I thought I replied to this yesterday. Somehow it got lost in the ether.

    switch -glob uses the same pattern matching as string match. string match does not support the {a,d,f} syntax. I think that's for shell syntax only.

    You can use [adf] though to match any characters in the character class. For switch you could also use hyphens to take the same action for multiple matches:

    
    switch -glob $letter {
    a -
    d -
    f {
     Matched a d or f
    }
    }
    
     Or:
    
    switch -glob $letter {
    [adf] {
     Matched a d or f
    }
    }
    

    Aaron
  • Hi Aaron,

    This is the irule as it currently is and while the /vz and /spf parts are working, the redirects just hang...no redirect as expected.

    I saw your comment on not having the commas so I will remove those, but it sounds like it just means that comma's are part of the set now that would match, but commas aren't present incidentally so no harm no foul on the commas in terms of the functionality issue?

    Please let me know if anything is wrong with this. LTM version is 10.2.0

    
    when HTTP_REQUEST {
       set pool8777 "suia-p.testms1_8777"
       switch -glob [URI::decode [string tolower [HTTP::uri]]] {
          "*/vz*" { return }
          "/csp*xpf=7*" { if { [class match [string tolower [HTTP::header "User-Agent"]] contains sms1p_ua_blacklist] } {
                                            drop
                                         } else {
                                            return
                                         }
                                       }
          "/spf/*" { pool $pool8777 }
          "/csp*f[a-c][a-e]*[b-c]*o*k" -
          "/csp*kw=??aceb*k" -
          "/csp*kw=?aceb*k" -
          "/csp*f[a-b][c-j][b-e]*oo" -
          "/csp*f[a-b][l-n][b-e]*oo" -
          "/csp*kw=fb[&, ,.]" -
          "/csp*f?a?c?e" { HTTP::redirect "http://cs.testams1.com/spf/cs_sms1_facebook.asp?[HTTP::uri]" }
          "/csp*kw=goo[&, ,.]" -
          "/csp*[g,6][e,o,0][g,o,0,r][g,l,o]ul" -
          "/csp*[g,6][e.o.0][g,o,0,r]gl" -
          "/csp*[g,6][e,o,0][g,o,0][g,l,o][e,g,l,o]" { HTTP::redirect "http://cs.testams1.com/spf/cs_sms1_google.asp?[HTTP::uri]" }
          default { if { [class match [string tolower [HTTP::header "User-Agent"]] contains sms1p_ua_blacklist] } {
                            drop
                         } else {
                            return
                         }
          }
       }
    }
    
     
  • The "&" in the above code box was a literal ampersand on post, but the forum swapped them out.
  • Ah, think I figured out the issue. I've used switch's on URI's for a long time, but usually I don't double quote the URI in the match, but figured it was better to do that

     

     

    It looks like that coupled with not having the trailing asterisk prior to the last double quote was causing those matches to fail if there was any other characters present in the URI. Originally I understand the trailing asterisk to be unnecessary as the match text was a subset of the real URI, but once I realised that the only matches that worked had the trailing asterisk that seemed to be the clear issue

     

     

    so for all of these:

     

     

    "/csp*kw=??aceb*k" -

     

     

    they needed to be this:

     

     

    "/csp*kw=??aceb*k*" -