Forum Discussion
stucky101_88485
Aug 13, 2012Nimbostratus
class iteration for key/value
Gurus
I know you don't usually need to manually iterate over a class as it does this automatically.
Example : I have a class called "in_maintenance" containing a list of URIs. Any matching URIs
should redirect to a maintenance page :
when HTTP_REQUEST {
if { [class match [HTTP::uri] starts_with in_maintenance] } {
HTTP::redirect http://some.maintpage.com
}
}
However if my class contains key/value pairs and I want to know the value for each key I don't think this works since I don't have the actual "iterator" variable (for the lack of a better word) so I can't check for its value.
Poking around I came up with this solution :
Lets say I have class called "host_redirects" containing key/value pairs like :
/baduri := /gooduri
Then the following iRule scans for bad uris and when one is found redirects to the good uri and exits the loop.
when HTTP_REQUEST {
set id [class startsearch host_redirects]
while { [class anymore host_redirects $id] } {
set element [class nextelement host_redirects $id]
set baduri [lindex $element 0]
set gooduri [lindex $element 1]
if { [HTTP::uri] equals $baduri } {
HTTP::redirect "http://[HTTP::host]$gooduri"
break
}
}
class donesearch host_redirects $id
}
Is this much more code than needed ? If anyone has a shorter version I'd like to see it.
thx
- hooleylistCirrostratusHi,
when HTTP_REQUEST { set new_uri [class match -value [HTTP::uri] starts_with in_maintenance] if { $new_uri ne "" } { HTTP::redirect "http://some.maintpage.com$new_uri" } }
- stucky101_88485NimbostratusHoolio
- hooleylistCirrostratusIt sounds like you want to check each key in the data group and see if it matches the requested URI. If it does, you want to get the corresponding value from that element and use it in a redirect.
- stucky101_88485NimbostratusAaron
- hooleylistCirrostratusGlad to hear it's working for you.
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects