Forum Discussion

Eric_123944's avatar
Eric_123944
Icon for Nimbostratus rankNimbostratus
Jan 06, 2017

Can switch be used to compare HTTP::host against multiple datagroups?

We currently serve some pages directly from the F5 by matching

[HTTP::host]
to some datagroups using
if
/
elseif
. I'm wondering if this could be replaced using
switch
to improve legibility and make it easier to modify or add more in the future.

Example

if {[class match [string tolower [HTTP::host]] equals SiteMaintenance] } {
    HTTP::respond 200 content [ifile get "Maintenance_html"] "Content-Type" "text/html" "Edge-control" "max-age=5m"
} elseif {[class match [string tolower [HTTP::host]] equals SiteMoved] } {
    HTTP::respond 200 content [ifile get "SiteMoved_html"]
} elseif {[class match [string tolower [HTTP::host]] equals SiteDisabled] } {
    HTTP::respond 200 content [ifile get "SiteDisabled_html"]
}

1 Reply

  • I am not 100% sure but I don't think you can use "class match" within "switch".

     

    If you are looking to lower the lines of code and make it easier to make changes, you can do something like this possibly:

     

    when HTTP_REQUEST {
    set HOST [string tolower [HTTP::host]]
    set MAINT_PAGE [class match -value $HOST equals CLASS_MAINTENANCE]
    if { $MAINT_PAGE ne "" } {
    HTTP::respond 200 content [ifile get $MAINT_PAGE] "Content-Type" "text/html" "Edge-control" "max-age=5m"
    }
    }

    In this case, the CLASS_MAINTENANCE is a datagroup that contains the domain and corresponding maintenance page like this:

     

    a.domain.com := Maintenance_html
    b.domain.com := SiteMoved_html
    c.domain.com := SiteDisabled_html

    You can keep adding more domains and relevant maintenance page to this CLASS_MAINTENANCE data group as you scale and the lines of code remain the same. This hasn't been tested but it should give you an alternate idea. Let us know if it works.