Forum Discussion

smp_86112's avatar
smp_86112
Icon for Cirrostratus rankCirrostratus
Jan 03, 2014

Short iRule rewrite for efficiency/readability

I have a short iRule that is simple to understand. Logically, the iRule works by checking the host header against two known values and modifies the Host: header if neither match. This is how it's currently written:

when HTTP_REQUEST {
  if { not (( [string tolower [HTTP::host]] equals "myapp1.domain.com" ) or
            ( [string tolower [HTTP::host]] equals "myapp2.domain.com" )) } {
    HTTP::header replace "Host" [HTTP::host]:9070
  }
}

Intuitively it seems this iRule could be written more efficiently, or at least easier to read. Normally I'd do this comparison in a SWITCH statement, but I can't seem to get SWITCH to work with negation. I know that there isn't a lot of efficiency to gain here, but I was hoping someone could give me some ideas on how they would accomplish this same logic?

  • Don't know that you need to negate the match, just perform no action on matching "myapp1" or "myapp2" and use the default case to perform your replace.

     

  • Don't know that you need to negate the match, just perform no action on matching "myapp1" or "myapp2" and use the default case to perform your replace.

     

  • That never occurred to me. Were you thinking of something like this?

    when HTTP_REQUEST {
      switch -- [string tolower [HTTP::host] {
        "myapp1.example.com" -
        "myapp2.example.com" { }
        default { HTTP::header replace "Host" [HTTP::host]:9070 }
      }
    }
    
    • smp_86112's avatar
      smp_86112
      Icon for Cirrostratus rankCirrostratus
      I tried this and it worked perfectly, and it makes much more sense to read. Exactly what I was looking for. Don't understand why it didn't occur to me to write it this way, but thanks for the idea!!