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