Forum Discussion
how to match against a list in v11?
I'm basically trying to consolidate the endpoints and move away from data groups. That way, when people modify the iRule, everything is transparent to the editor, they dont have to go in and check other places. I'm having a hard time understanding how I can match a uri against a list, any ideas?
when HTTP_REQUEST {
set endpoints {
"/about"
"/cblock"
}
if { [class match [HTTP::uri] starts_with $endpoints] } {
pool cms
}
}
This is the error in the ltm log:
Aug 6 17:39:01 d31lb01 err tmm[12842]: 01220001:3: TCL error: /Common/dev32_irules - Could not find class "/about" "/cblock" (line 1) invoked from within "class match [HTTP::uri] starts_with $endpoints"
class match works only with datagroups. It does not work with Tcl lists. The drawbacks of not using datagroups include:
- class matching runs in O(1) time, so the size of the datagroup does not affect the search performance. This is not true for Tcl lists (though you should get something better than O(n) for large lists if you use a Tcl dictionary);
- using Tcl lists generally require "inflating" the lists before the command can operate on it, which consumes CPU and memory;
- modifying an iRule requires technicians and engineers to know code, at least to some degree, and the list elements cannot be changed via
tmshor the WebUI.
Having said that, to search a Tcl list, you can use the lsearch method:
when RULE_INIT { set static::endpoints [list /about /cblock] } when HTTP_REQUEST { if { [lsearch -glob $static::endpoint "[HTTP::uri]*"] != -1 } { pool cms } }
There are other ways, but this is likely the simplest.
6 Replies
- VernonWells
Employee
class match works only with datagroups. It does not work with Tcl lists. The drawbacks of not using datagroups include:
- class matching runs in O(1) time, so the size of the datagroup does not affect the search performance. This is not true for Tcl lists (though you should get something better than O(n) for large lists if you use a Tcl dictionary);
- using Tcl lists generally require "inflating" the lists before the command can operate on it, which consumes CPU and memory;
- modifying an iRule requires technicians and engineers to know code, at least to some degree, and the list elements cannot be changed via
tmshor the WebUI.
Having said that, to search a Tcl list, you can use the lsearch method:
when RULE_INIT { set static::endpoints [list /about /cblock] } when HTTP_REQUEST { if { [lsearch -glob $static::endpoint "[HTTP::uri]*"] != -1 } { pool cms } }
There are other ways, but this is likely the simplest.
- mgpspr_213832
Nimbostratus
Fantastic write up - thx.
- mgpspr_213832
Nimbostratus
Vernon,
Why does this match on / so if i goto my site http://my.testsite.com/, this line matches
when RULE_INIT { set static::endpoints [ list /test /guest /pest ] } when HTTP_REQUEST { if { [lsearch -glob $static::endpoints "[HTTP::uri]*"] != -1 } { log local0. "IM IN"
ltm log: Aug 6 20:24:46 d31lb01 info tmm1[12842]: Rule /Common/d_irules : IM IN
- VernonWells
Employee
Oh, you know, mostly because I'm a dummy :). I reversed the meaning (my code uses the request-uri as a glob match pattern looking to see if any list member matches, which is not what you want at all). You'll have to do this as follows (it's O(n) and probably as good as one can get with non-exact matching, unhappily):
when RULE_INIT { set static::endpoints [list /test /guest /pest] } when HTTP_REQUEST { foreach ep $static::endpoints { if { [HTTP::path] starts_with $ep } { log local0. "I'M IN" pool cms return } } }
- Mike_P__194875
Nimbostratus
I was trying to use something similar to the code Vernon posted above but I get a TCL error every time the irule triggers:
err tmm[14228]: 01220001:3: TCL error: /Common/test_selective_cac - couldn't set loop variable: " [HTTP::path] starts_with $ep " while executing "foreach ep $static::endpoints { if { [HTTP::path] starts_with $ep
Any ideas?
- VernonWells
Employee
@MikeP, can you provide your code?
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