Forum Discussion
Trying to understand using square braces vs parenthesis in iRules
Hello,
I have an written the following iRule
when HTTP_REQUEST { set user_agent [string tolower [HTTP::header User-Agent]] if { not ($user_agent contains "windows nt 5.1")} { virtual virtual_server_name } }
initially, I had written the iRule by mistake with [] instead of () around the expression for the if statement, so it looked as below
when HTTP_REQUEST { set user_agent [string tolower [HTTP::header User-Agent]] if { not [$user_agent contains "windows nt 5.1"]} { virtual virtual_server_name } }
When I was using the square braces in the if statement, the iRule compiled, but the virtual server was giving me a tcp reset whenever I attempted to go it it. Once I changed the square braces to parenthesis, the iRule worked as expected and no longer gave me a tcp reset.
I am confused as to why the square braces were causing a problem, and why they were causing me to get a TCP reset. I tried to read up a bit on tcl syntax, but I am still confused. If I understand correctly, the square brackets should have caused '$user_agent contains "windows nt 5.1"' to be interpreted as a command, which returned either a 1 or 0 and was the negated by the 'not' and passed to the if command. However, this is obviously not what happened. I am also confused as to what role the parenthesis play in tcl, and why they fixed this problem.
Thanks for the help.
6 Replies
- DEJ
Nimbostratus
Hello J,
My best reference for your question would be the article below.
- jakk127_200290
Nimbostratus
Hey. I looked at that article but it did not answer my questions. It gave a use case for parenthesis but did not explain how they were used
- DEJ
Nimbostratus
The only other use I can think of is to override order of operations. The other uses I saw listed - negative comparison, regex, and arrays. - jakk127_200290
Nimbostratus
Ok, well that follows what I was finding, which is why I am confused. I would think that a statement like if { not [$user_agent contains "windows nt 5.1"]} would mean that the command '$user_agent contains "windows nt 5.1"' would be executed first, which would result in a boolean, which would be negated by the not operator, which would then be passed to 'if'. However, I must not be understanding something correctly as that was causing the tcp reset, and only when I replaced the [ with ( did the irule operate correctly. I am even more confused because the first way, with the braces, compiled and did not give me any errors
- kunjan
Nimbostratus
if { not [$user_agent contains "windows nt 5.1"]}The square bracket is considered as command which is executed when the iRule runs. Since it is an invalid command, it generates TCL error and TCP resets. /var/log/ltm logs should have the error that the iRule throws. Hope that explains.
- jakk127_200290
Nimbostratus
Yes...that makes perfect sense. That's actually what i thought was happening,but i did not realize that was an invalid command. Guess i figured contains could be used by itself
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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