For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

Forum Discussion

Ashish_Ram_Tak1's avatar
Ashish_Ram_Tak1
Icon for Nimbostratus rankNimbostratus
Jul 25, 2014

need to simplify the below iRule which contain multipal if else statements, is it possible to replace with switch

Dear DevCentral team,

Thank you for being there to help us always. I have below iRule which contains multipal if else statements so because of this there is lots of resources are using, along with that I am using F5 virtual edition. is it possible to simplify the below iRule with switch or any other statement.

when RULE_INIT { set doSSL 0 } when HTTP_REQUEST {

         HTTP::header remove "Accept-Encoding"

             switch [string tolower [HTTP::host]] {

                           "abc.xyz.com" {

                                           if {([HTTP::path] eq "/")} {

                                                           HTTP::header replace Host "def.pqr.com"

                                                           HTTP::uri "/ssearch/mail/ssoForm.jsp"

                                                           }

                                           pool pqr 


                                           }



                           "abcadmin.xyz.com" {

                                           if {([HTTP::path] eq "/")} {

                                                           HTTP::header replace Host "def.pqr.com"

                                                           HTTP::uri "/abc/pqr/login.jsp"             

                                                           }

                                           pool pqr
                                           set doSSL 1

                                           }



                           "test.xyz.com" {

                                           if {([HTTP::path] eq "/")} {

                                                            HTTP::header replace Host "any.any.com"

                                                           HTTP::uri "/company/"

                                           }

                                           pool any
                                           set doSSL 1

                           }

}

when SERVER_CONNECTED {

log local0. "pool [LB::server pool]"
if {[string tolower [LB::server pool]] contains "pqr" } {

     SSL::enable serverside

log local0. "z" } elseif {[string tolower [LB::server pool]] contains "any"} {

       SSL::enable serverside

log local0. "z" }

} 

1 Reply

  • With regards to the HTTP_REQUEST event, I would think that the way you're doing it is more efficient the 'prettier' alternative (which could involve setting a variable and updating the pool selection after your switch completes), since it's not using variables, which add a little bit of overhead. Though this looks like it has more redundant code, only one of the if statements would be executed in any one request, so would wind up being a little bit more performant. In the SERVER_CONNECTED event, you could simplify your code with a switch -glob statement, though I don't know if it would give you any better performance. Here's an update to your code that may be helpful (not much really changed though)

     

    when RULE_INIT { 
        set doSSL 0 
    } 
    
    when HTTP_REQUEST {
    
        HTTP::header remove "Accept-Encoding"
    
        switch [string tolower [HTTP::host]] {
            "abc.xyz.com" {
                if {([HTTP::path] eq "/")} {
                    HTTP::header replace Host "def.pqr.com"
                    HTTP::uri "/ssearch/mail/ssoForm.jsp"
                }
    
                pool pqr 
            }
            "abcadmin.xyz.com" {
                if {([HTTP::path] eq "/")} {
                    HTTP::header replace Host "def.pqr.com"
                    HTTP::uri "/abc/pqr/login.jsp"
                }
    
                pool pqr
                set doSSL 1
            }
            "test.xyz.com" {
                if {([HTTP::path] eq "/")} {
                     HTTP::header replace Host "any.any.com"
                    HTTP::uri "/company/"
                }
    
                pool any
                set doSSL 1
            }
        }
    }
    
    when SERVER_CONNECTED {
        log local0. "pool [LB::server pool]"
    
        switch -glob [string tolower [LB:server pool]] {
            "*pqr*" -
            "*any*" {
                SSL::enable serverside
                log local0. "z" 
            }
        }
    } 

    And I noticed (maybe this isn't the entire iRule, you set doSSL in the HTTP_REQUEST, but don't use it anywhere else. You could modify your SERVER_CONNECTED event to use that variable instead of checking the pool if you wanted. Like this:

     

    when SERVER_CONNECTED {
        log local0. "pool [LB::server pool]"
    
        if { $doSSL } {
            SSL::enable serverside
            log local0. "z" 
        }
    }