Calling commands by string / reflection

Hi,

Is there any way to call iRule commands by string? For example, can I turn the string “starts_with” into a variable which calls the command ‘starts_with’?. I’m trying to make an iRule that interpret strings in a datagroup into commands efficiently, rather than using a bunch of switch statements.

To elaborate, say my iRule has just read a datagroup value, performed a split procedure on it, and returned a list with the values “HTTP::URI” , “starts_with” and “/blah” and then stored them into the variables $a , $b and $c respectively.

I want to be able to do something like

if {[$a $b $c]}

and have it be interpreted as though it was

'if {"[HTTP::URI] starts_with /blah"}

`.

in c this is called ‘reflection’, is there an iRule equivalent? http://www.dotnetperls.com/methodinfo-invoke

Thanks.

Hi,

As far as I’m aware, no. The TCL

exec

command which could otherwise be used for such purpose is excluded from the available iRule commands. Can you perhaps explain a bit what you’d like to do?

Do you want to select a specific pool or initiate a redirect, based on HTTP URI value? It also appears you want to manage all updates via an LTM data-group instead of iRule. Please give a few samples of condition/action in iRule format.

Regards,

To a degree you can use the

eval

command for this. Be very careful with this, though - it can easily lead to injection attack vulnerabilities.

Documentation: eval

Hi KingMeow,

I haven´t found a way to concat a comparison as a condition.

Using

eval

works fine with commands as demonstrated in this post.

If you find a solution to this, please let us know.

As Hannes already wrote, there are probably alternative solutions to meet your requirements from functional perspective.

Thanks & +1 for the interesting question,

Stephan

Hi Stephan,

I can confirm you’re correct for 11.4.1 at least. I haven’t built my iRule exactly but fiddled did a lot of testing. I can turn attributes like

HTTP::uri

into variables just by set

x "HTTP::uri"

. then I can get the command HTTP::uri by

$x

. Alternatively if I do

[$x]

it returns the value of the HTTP uri.

I even did something like

when HTTP_REQUEST {
    set a HTTP::header
    set b "replace"
    set c "host"
    set d "new.host.header.value"
    $a $b $c $d
}

And the last line actually replaced the host header.

I haven’t been able to turn things like “starts_with” into variables though - every approach has resulted in a run time error but I think I’ve gotten around it by using

matches_glob

for all string matching and leaving it up to the user to enter the correct wildcards into the datagroup that feeds into the iRule.

I think this is pretty much solved but I’ll build my iRule when I get a chance again and repost in a couple of days with an update.

I’m quite sure this is going to cut my iRule down to a third of its size at least. Thanks for your help again!