Forum Discussion

Cosby_17532's avatar
Icon for Nimbostratus rankNimbostratus
Feb 23, 2011

Using wildcards with classes

I'm in desperate need of help with an iRule for a customer. They are converting a working 9.x rule to 10.x. In the process we changed it from an array to a class. The class calls an external file with 8998 lines (I know it's rather large). What they are doing is redirecting on the HTTP Response. They read the name from the class and then redirect (301) with the value.



The redirection portion works perfectly. The problem is that the name portion has a lot of wildcard URIs and I can't figure out how to deal with them.



I'm posting a portion of a test data set, the current iRule, and the class commands from the config. I would really appreciate any help I can get.






"/A1/" := "",


"/A1/*" := "",


"/B1/" := "",


"/B1/*" := "",


"/C1/" := "",


"/C1/*" := "",


"/D1/" := "",


"/D1/*" := "",


"/E1/" := "",


"/E1/*" := "",


"/F1/" := "",


"/F1/*" := "",


"/g/" := "",


"/g/*" := "",


"/h/" := "


"/h/*" := "


"/i/" := "


"/i/*" := "



class testdatagroup {


type string


filename "/config/testdatagroup_file"


separator ":="








Set debug to non-zero in the events you want to debug


debug levels: 0 - warnings only, 1 - testing, 2 - verbose


WARNING: verbose logging will be enormous with production dataset





set debug 2


Set data variables


set HTTP_URI [HTTP::uri]


set HTTP_HOST [HTTP::host]


foreach element testdatagroup {


if {[string match -nocase $element $HTTP_URI]}{


log local0.notice "Element $element"








if { $debug > 0 } {log local0.notice "HTTP_URI set to [HTTP::uri] and HTTP_HOST set to [HTTP::host]."}


HTTP Redirect if correct


if { [string tolower ${HTTP_URI}] starts_with "/search?" and [URI::query ${HTTP_URI}] != ""} {


set my_query [URI::query ${HTTP_URI}]


if {$debug > 0} {log local0. "Redirecting ${HTTP_URI} to http://[HTTP::host]/Search/?${my_query}"}


HTTP::redirect "http://[HTTP::host]/Search/?${my_query}"










set dgname "testdatagroup"


if { $debug > 0 } {log local0.notice "Data Group: $dgname"}


log local0.notice "URIPATH [URI::path $HTTP_URI]"


set DG_name [class search -name testdatagroup contains $HTTP_URI ]


log local0.notice "DG_name: $DG_name"


if { [string match "\*" "${DG_name}"]} {


set name [class match -name $HTTP_URI starts_with testdatagroup ]


set value [class match -value $HTTP_URI contains testdatagroup]


if { $debug > 0 } {log local0.notice "Name Value: $name"}


} else {


set name [class match -name $HTTP_URI starts_with testdatagroup ]


set value [class match -value $HTTP_URI contains testdatagroup]


if { $debug > 0 } {log local0.notice "Name Value: $name"}




set newmap [class search -value testdatagroup equals [class match -name $HTTP_URI starts_with testdatagroup ]]


if { $debug > 0 } {log local0.notice "Redirect: $newmap"}


HTTP::respond 301 Location "$newmap"





Once again, I would appreciate any help at all.





7 Replies

  • Hi Cosby,



    I'd break up the datagroup into one that has all of the "equals" matches (/A1/, /B1/, etc) and another that has the "starts_with" matches (/A1/*, /B1/*, etc entered in the datagroup as /A1/, /B1/, etc). You can then use [class match -element [HTTP::path] equals uris_datagroup] and [class match -element [HTTP::path] starts_with uris_datagroup].



  • Aaron,



    Thank you for your reply. I'm not sure I made myself clear. The data set has wildcards in it because the users may enter almost anything in. For example, the user may enter In the data set we may have the following entries:



    "/petfriendsshop/" :=


    "/petfriendsshop/en_US*" :=



    The second entry is a wild card entry, no matter what is entered by the customer it should redirect them properly. As long as the URI starts with /petfriendsshop/en_US/.





  • Yep, so for any exact match like /petfriendshhop/, you'd add it to the exact match datagroup and use the equals operator. For a URI token that has a * on the end, you'd add it without the * to a "starts_with" datagroup and use the "starts_with" operator on class match. Similarly, if you had ends_with checks you wanted to do, you could use the same logic as starts_with. If you need to support wildcards in the middle of strings in the datagroup, you'd need to retrieve the entire datagroup as a TCL list using 'class get' and then loop through the entries one by one using 'string match'.



  • Hi Hoolio,



    Has there been any developments in the later versions of v10 or v11 that support exact and wildcard URLs in the same datagroup? We would like to use class match to do this but conscious of its limitations.
  • +1 this feature would be great! and also supporting wildcards when creating data-groups with tmsh


  • Arie's avatar
    Icon for Altostratus rankAltostratus

    You can work around the limitation by moving the match indicator (wildcard vs. exact match) to the value and using a separator.


    In other words,


    "/g/*" := ""




    "/g/" := "*;"




    "/E1/" := ""




    "/E1/" := "=;"


    This way you can build logic into your code to handle the two scenarios. I can elaborate if necessary.