Forum Discussion

d2_21508's avatar
d2_21508
Icon for Nimbostratus rankNimbostratus
Oct 06, 2015

is it possible to use regex within switch block?

Hello!

Is it possible to use regular expressions within switch block? My scenario:

  • a.domain.com
  • a.domain.net

So different TLDs. Now I want to write iRule that will handle both 'com' and 'net', but I can't figure this out and instead I have to use following syntax:

when HTTP_REQUEST { 
    switch -glob [string tolower [HTTP::host]] {
        "a.domain.com" {
            pool some_pool_a
        }
        "a.domain.net" {
            pool some_pool_a
        }
        "b.domain.com" {
            pool some_pool_b
        }
        "b.domain.net" {
            pool some_pool_b
        }
    }
}

I would like to rewrite this iRule to something like this:

when HTTP_REQUEST { 
    switch -glob [string tolower [HTTP::host]] {
        "a.domain.[com|net]" {
            pool some_pool_a
        }
        "b.domain.[com|net]" {
            pool some_pool_b
        }
    }
}

but it seems to be not working. Do you guys have any good idea how to fix it? BTW - I'm using v11.6 of BIG IP software.

  • Hi,

    you can use this syntax:

    when HTTP_REQUEST { 
        switch -glob [string tolower [HTTP::host]] {
            "a.domain.com" -
            "a.domain.net" {
                pool some_pool_a
            }
            "b.domain.com" - 
            "b.domain.net" {
                pool some_pool_b
            }
        }
    }
    
  • You can use

    switch -regexp [HTTP::host]
    instead of
    switch -glob [string tolower [HTTP::host]]

    Have a look at

    switch -regexp
    in action in this thread: https://devcentral.f5.com/questions/disable-port-translation-in-an-irule-or-node-usage

    Good luck!

    • Edit: Have removed the tip where I suggested to avoid converting HTTP Host to lower-case string. Unfortunately, not all HTTP Client or Web Service Libraries are as clean as web browsers and not all of them comply with the DNS standard.
  • @Stanislas: that worked

     

    @Hannes Rapp: will try regex, thanks. About converting strings to lowercase. "Every web browser automatically does this conversion for clients", what about shell scripts accessing URLs? (wget/curl/python/perl etc). I don't think all libraries are doing such conversion on the fly. Why using this syntax is incorrect?

     

    • Hannes_Rapp's avatar
      Hannes_Rapp
      Icon for Nimbostratus rankNimbostratus
      Syntax is good, but general logic is bad. Please find the modified answer below.
  • Hi Hannes Rapp, in the HTTP::host wiki, there is the following text:

     

    RFC2616 (section 3.2.3) states that host header evaluation MUST be case insensitive. So it is a good practice to set the Host header value to lower case before performing comparisons. This can be done using [string tolower [HTTP::host]].

     

    I did not know browsers always convert to lower case the host header, but if one does not, the irule can have a different behavior and it's hard to troubleshoot the issue... so I will continue to convert to lower case.

     

    d2, even if you can use regex in switch command, regex must be used only if you can't do something else.

     

    if you have only 2 choices like in your request, it is recommended to use the syntax I proposed to limit performance impacts:

     

    https://devcentral.f5.com/articles/so-yeah-regex-is-bad