Forum Discussion

kmtmt_51646's avatar
kmtmt_51646
Icon for Nimbostratus rankNimbostratus
Nov 19, 2012

How to extra strings inside "class match"

Hi Dev members,

 

 

Let me ask about "class match" function.

 

 

Here's my Data Group:

 

============

 

ltm data-group internal /Common/testgroup1 {

 

records {

 

/test { }

 

}

 

type string

 

}

 

ltm data-group internal /Common/testgroup2 {

 

records {

 

/test/ { }

 

}

 

type string

 

}

 

============

 

 

and my iRule

 

============

 

when HTTP_REQUEST {

 

if { [class match -- [HTTP::uri] equals testgroup1] || [class match -- [HTTP::uri] starts_with testgroup2] }{

 

pool pool-true

 

} else

 

pool pool-false

 

}

 

============

 

 

 

1)

 

I need to deted following Paths by iRules with Data Group and send to pool-true:

 

a) http://www.example.com/test

 

b) http://www.example.com/test/index.html

 

c) http://www.example.com/directory/test/index.html

 

d) http://www.example.com/testing/index.html

 

 

my iRule works fine but I need to have 2 Data Groups and tough to maintain both, so I wonder if can add "/" after DataGroup somehow inside iRule.

 

For example:

 

============

 

when HTTP_REQUEST {

 

if { [class match -- [HTTP::uri] equals testgroup1] || [class match -- [HTTP::uri] starts_with "testgroup1/"] }{

 

pool pool-true

 

============

 

 

 

Does anyone have ideas?

 

 

 

Thank you very much,

 

 

kmtmt

 

8 Replies

  • Hi kmtmt,

     

     

    You could combine the data group so that you'd have /test and /test/ in one data group. Another option would be to remove any trailing forward slashes before you do the class lookup against one data group without the trailing forward slashes:

     

     

    if { [class match -- [string trimright [HTTP::uri] "/"] equals testgroup1] }{

     

     

    Aaron
  • Hi hoolio,

     

     

    Thank you for your reply!

     

     

    And let me correct what I wrote

     

    >a) http://www.example.com/test

     

    >b) http://www.example.com/test/index.html

     

    >c) http://www.example.com/directory/test/index.html

     

    >d) http://www.example.com/testing/index.html

     

    -> a) and b) is correct URL to be sent to pool-true, but c) and d) are false, which is to be sent to pool-false.

     

     

    as you advise, I can put "/test" and "/test/" into 1 datagroup, but then I can use either "equals" or "start_with", and if I do "start_with /test || /test/", above URL d) will be false-positive...

     

     

    trimright sounds a very nice idea, but again, I cannot differentiate b) and d) .

     

    So I trimright datagroup as follows. Didn't get any error, but it seems not working to trimright datagroup...

     

    =========

     

    when HTTP_REQUEST {

     

    if { [class match -- [HTTP::uri] starts_with testgroup2] || [class match -- [HTTP::uri] equals [string trimright "testgroup2" "/"]]}{

     

    pool pool-true

     

    } else {

     

    =========

     

     

     

    I guess I should do with 2 datagroups...

     

     

     

    Thank you anyway!

     

     

    kmtmt
  • You should be using trimright with the URI, not the Data Group. Regardless, if you use 'equals' then a single Data Group will suffice no?
  • So you could explicitly list /test, /test/ and /test/index.html in the data group and use the exact client URI for the lookup:

     

     

    if { [class match -- [HTTP::path] equals testgroup2]}{

     

     

    Or you could remove the trailing slash from the data group entries and trim the client's requested URI during the class lookup:

     

     

    if { [class match -- [string trimright [HTTP::path] "/" ] starts_with testgroup2]}{

     

     

    Note that I've used [HTTP::path] to get just the requested path without the query string in the URI for the lookup.

     

     

    Aaron
  • Hi What Lies Beneath, Aaron,

     

     

    Thank you for your reply and sample code!

     

     

    /test/ can only be the first directory, and if it comes to second I'd like to ignore it like this:

     

    >c) http://www.example.com/directory/test/index.html

     

     

    so I believe I need to have "start_with" instead of having only "equals".

     

     

     

    > if { [class match -- [string trimright [HTTP::path] "/" ] starts_with testgroup2]}{

     

     

    hmmm...

     

    I thought [HTTP::path] can have file name as well, so "d) http://www.example.com/testing/index.html" will be "/testing/index.html"

     

    Please kindly tell me if I'm wrong.

     

     

     

    Thank you very much!
  • Yes, 'starts_with' would probably be best.

     

     

    And yes, HTTP::path does indeed contain the file name; actually anything up to the first ?
  • Hi Steve,

     

     

    Thank you for your reply!

     

     

    I ended up with 2 data groups with and without slash at the last, my iRule looks like :

     

     

    if {[class match -- [HTTP::path] starts_with Directory_slash] || [class match -- [HTTP::path] equals Directory_noslash]} {

     

     

     

    Thank you for everyone who gave me advice and help!

     

     

     

    Best Regards,

     

     

    kmtmt