Forum Discussion

CSA's avatar
CSA
Icon for Nimbostratus rankNimbostratus
Oct 29, 2012

Smartest way to do a class lookup case insensitive

Hi,

 

 

Everything is in the title.

 

 

I'd like to use class lookup (or a class match -value as it's the same) in a case insenstive way. There isn't any "-nocase" option like in some other class commands.

 

 

Basically, I have a datagroup for URL redirections (many "http://www.a.com/b" := "http://www.c.com/d") and I want the URL redirections to be case insensitive.

 

 

Thanks!

 

8 Replies

  • You'll want to use the stringtolower command, more info here: https://devcentral.f5.com/tutorials/tech-tips/irules-101-13-tcl-string-commands-part-1
  • e.g.

    [root@ve10:Active] config  b virtual bar list
    virtual bar {
       snat automap
       pool foo
       destination 172.28.19.79:80
       ip protocol 6
       rules myrule
       profiles {
          http {}
          tcp {}
       }
    }
    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
       set host [HTTP::host]
       set uri [HTTP::uri]
       if { [class match -- "http://[string tolower $host$uri]" equals redirection_class] } {
          HTTP::redirect [class match -value "http://[string tolower $host$uri]" equals redirection_class]
       }
    }
    }
    [root@ve10:Active] config  b class redirection_class list
    class redirection_class {
       "http://www.a.com/b" { "http://www.c.com/d" }
    }
    
    [root@ve10:Active] config  curl -I http://172.28.19.79/b -H "Host: www.a.com"
    HTTP/1.0 302 Found
    Location: http://www.c.com/d
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    [root@ve10:Active] config  curl -I http://172.28.19.79/B -H "Host: www.A.com"
    HTTP/1.0 302 Found
    Location: http://www.c.com/d
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    
  • i think this could be a bit better.

    when HTTP_REQUEST {
       set host [string tolower [HTTP::host]]
       set uri [string tolower [HTTP::uri]]
       if { [class match -- "http://$host$uri" equals redirection_class] } {
          HTTP::redirect [class match -value "http://$host$uri" equals redirection_class]
       }
    }
    
  • CSA's avatar
    CSA
    Icon for Nimbostratus rankNimbostratus
    Thanks all for your answers.

     

     

    I know the "string tolower" function, but I think your solutions have the same issue as mine: you must write the lowcase URL in the data group.

     

     

    My idea would be to compare the values whatever the case is in my data group. I have regularly requests to implement redirections for long URLs, I'd like to avoid switching them to lowercase before putting them in the data group.

     

     

  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    If you can't/don't want to guarantee that the data group entries are in lower case, you could loop through each data group element name and set it to lowercase for comparison or use string match -nocase. However, the overhead for the looping should be higher than using a native class command with string tolower on the input string.

     

     

    Aaron
  • e.g.

    [root@ve10:Active] config  b virtual bar list
    virtual bar {
       snat automap
       pool foo
       destination 172.28.19.79:80
       ip protocol 6
       rules myrule
       profiles {
          http {}
          tcp {}
       }
    }
    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
       set result [lindex [class get -nocase redirection_class "http://[HTTP::host][HTTP::uri]"] 1]
       if { $result ne "" } {
          HTTP::redirect $result
       }
    }
    }
    [root@ve10:Active] config  b class redirection_class list
    class redirection_class {
       "http://www.A.com/B" { "http://www.c.com/d" }
    }
    
    [root@ve10:Active] config  curl -I http://172.28.19.79/b -H "Host: WWW.a.com"
    HTTP/1.0 302 Found
    Location: http://www.c.com/d
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    [root@ve10:Active] config  curl -I http://172.28.19.79/somethingelse -H "Host: www.domain.com"
    HTTP/1.1 404 Not Found
    Date: Tue, 30 Oct 2012 01:55:58 GMT
    Server: Apache/2.2.3 (CentOS)
    Content-Type: text/html; charset=iso-8859-1
    
    
  • CSA's avatar
    CSA
    Icon for Nimbostratus rankNimbostratus
    Interesting solution, I'm going to give it a try!

     

     

    Thanks nitass!

     

  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    That's novel Nitass. Nice solution.

     

     

    However, you're going to use up a lot more memory, especially if it's a large datagroup, by retrieving all of the contents. I think it would be better overall to just ensure the data group contents are in lower case and then use class search/lookup/match.

     

     

    I think we'll have improvements on case sensitivity in an upcoming release next year.

     

     

    Aaron