Forum Discussion

stucky101_88485's avatar
stucky101_88485
Icon for Nimbostratus rankNimbostratus
Aug 13, 2012

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

 

  • Hi,

    Can you try this example which retrieves the value for the matching name in the data group?

    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"
      }
    }
    

    Aaron
  • Hoolio

     

     

    I think there is a misunderstanding. My first rule (the maintenance one) works fine and I don't think it can be any shorter. This data group list doesn't contain key/values but just keys (those keys being URIs which should redirect to the maint page). The main page is always the same so its coded into the iRule itself.

     

     

    My question was regarding the second example where I wanna keep a mapping between bad and good URI's like :

     

     

    /app_old := /app_new

     

    /app2_old := /app2_new

     

     

    etc...

     

     

    so the ltm would crack open evey http header and check every URI against this list. If one is found it redirects to this URI's new value and then breaks out of the loop.

     

    As you can see I have to do a lot more work here. I have to grab each element and separate the left side from the right side using lindex.

     

     

    I was just wondering if this could be accomplished in 2-3 lines just like the maintenance example.

     

     

    Hope this clarifies.
  • It 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.

     

     

    I think the example I posted does what you're asking. If that's not the case, can you clarify further what you're trying to do?

     

     

    Thanks, Aaron
  • Aaron

     

     

    Sorry brainfart on my end. I looked at it again and I see what you did there.

     

    Nice and definitely more elegant ! I had a feeling I was complicating things :)

     

     

    Thank you !