Forum Discussion
The EVAL command
I am testing the eval command and want to generate an error if my input script contains an error. My test code is as follows:
set cmd "if { \[HTTP::uri\] starts_with \"/hello\" }{ log local0.alert \"match!!!\" }"
if { [catch {
[eval $cmd]
} myresult] != 0 } {
log local0. "My rule failed: $myresult"
}
The output to this is as follows:
Rule : match!!!
Rule : My rule failed: invalid command name ""
So the "match!!!" output clearly shows the command was run successfully, but the catch command is returning an error.
Any ideas as to why?
9 Replies
- hoolio
Cirrostratus
Hi,when HTTP_REQUEST { set cmd {if { [HTTP:uri] starts_with "/hello" }{ log local0.alert "match!!!" }} if { [catch { eval $cmd } myresult] != 0 } { log local0. "My rule failed: $myresult, \$cmd: $cmd" } else { log local0. "My rule succeeded: $myresult, \$cmd: $cmd" } }
- ichalis_37981Historic F5 AccountThanks!
- brad_11480
Nimbostratus
From what I understand the EVAL command is an old, old command and continues to exist because it is used widely. Further I understand that the preferred method is to use the {*} notation to allow safe argument expansion. That is what I wanted to do.. I had the list of arguments in a string. But the use of {*}$string resulted in a syntax error. - hoolio
Cirrostratus
Hi Brad, - spark_86682Historic F5 AccountIt's more that eval and {*} do different things. Eval takes a string and executes it as a command. {*} is used to expand a Tcl list into separate arguments ("stringifying" it, essentially), typically for use in calling a command.
{ Set-Cookie "foo=bar" Set-Cookie "baz=quux" X-Server "BIG-IP iRule" }
HTTP::respond 302 Location http://new.example.com $headers
HTTP::respond 302 Location http://new.example.com {*}$headers
HTTP::respond 302 Location http://new.example.com Set-Cookie "foo=bar" Set-Cookie "baz=quux" X-Server "BIG-IP iRule"
eval "HTTP::respond 302 Location http://new.example.com $headers"
- nitass
Employee
nice explanation Spark! - Andrew_Le_12873
Nimbostratus
Yup, great explanation with examples, Spark!
- hoolio
Cirrostratus
There's more info on {*} here: http://wiki.tcl.tk/17158
- Simon_Mittelber
Nimbostratus
is there any news on this one?
using eval for this, as spark suggests, is dangerous in my eyes:
eval "HTTP::respond 302 Location http://new.example.com $headers"
what if headers contain user input? someone could inject commands...
or what if anyone would use it for a response including content:
eval "HTTP::respond 200 content $content"
what if the content contains user input?
i think introducing the {*}-command is the only way to enable safe argument expansion.
i did a
on big ip 13.0.0. tcl version is still 8.4.6.[info patchlevel]
is big ip expected to move to 8.5 in the near future?
Recent Discussions
Related Content
* 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