Forum Discussion

Matt_Breedlove_'s avatar
Matt_Breedlove_
Icon for Nimbostratus rankNimbostratus
Aug 08, 2011

assistance with literal question mark in switch

Hi All,

In this switch, it allows through a few URI prefixes with a wildcard, but then there is a URI that is like this

/?g

or like this

/?w

any single character following the literal question mark

Is this the right syntax to use? Thanks in advance

M


when HTTP_REQUEST {
   switch -glob [URI::decode [string tolower [HTTP::uri]]] {
      /ab* { return }
      /cd* { return }
      /ef* { return }
      /\\?? { return }
      default {
         discard
      }
   }
   discard
}

5 Replies

  • You might have to find an alternate method that using a "?", since it is a Wildcard character.

     

     

    See this post: http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/244/Switch-Gone-Wild-Using-Wildcards-with-the-Tcl-quotswitchquot-command.aspx

     

     

    If there is something in the HTTP::query portion of the URI that you are looking for then you might have to nest a switch statement on the HTTP::query since this starts at the "?" but does not include it.

     

     

    Example:

     

    http://website.domain.com/subweb/index.html?user=test&logincheck

     

     

    HTTP::query = user=test&logincheck (Not INCLUDING the ?)
  • Michael's idea of using just the query string might work for your scenario. If not, you should be able to escape the question mark to have it interpreted literally. Here's a quick test using tclsh. I think you'd want just one backslash:

    
    % switch -glob {/z?} {
       "/ab*" -
       "/cd*" -
       "/\\??" { puts matched }
       default { puts "no match" }
    }
    no match
    
    % switch -glob {/z?} {
       "/ab*" -
       "/cd* -
       "/\??" { puts matched }
       default { puts "no match" }
    }
    matched
    

    Aaron
  • Would it be safer just to use

     

     

     

    /?? {return}

     

     

    Then it matches any two single characters? There seems to be contradicting info on the forums/wiki about needing double backslash for the literal question mark, but then in the tclsh test above it saying a single backslash works..

     

    I dont want to get caught up with syntax issues

     

     

    If its safer doing 2 questions markes for two single occurences of any character I would prefer that, but notice I am not quoting the switch match strings...is that okay when using the double question mark for this?

     

     

    Thanks

     

    M

     

  • I tinkered with this again and got these working as expected.

     
    when HTTP_REQUEST {
    switch -glob [string tolower [HTTP::uri]] {
    "/foo*" { HTTP::redirect "http://www.yahoo.com" }
    "/\\?\\?*" { HTTP::redirect "http://www.msn.com" }
    "/\\?*" { HTTP::redirect "http://www.google.com" }
    }
    }
    

    Hope this helps!
  • Michael, you were right. You need two backslashes to escape the question mark:

    
    %  switch -glob {/yz}  {
       "/ab*" -
       /cd* -
       "/\\??" { puts matched }
       default { puts "no match" }
    }
    no match
    %
    % switch -glob {/?z}  {
       "/ab*" -
       /cd* -
       "/\\??" { puts matched }
       default { puts "no match" }
    }
    matched
    

    Aaron