Forum Discussion

Robert_47833's avatar
Robert_47833
Icon for Altostratus rankAltostratus
May 10, 2011

confused by match_regx

Hi,can someone help?

 

when HTTP_REQUEST

 

{ default vars set in most iRules written by StubHub

 

set host [HTTP::host]

 

set uri [HTTP::path]

 

set subdomain1 "static"

 

set subdomain2 "s"

 

if { $host matches_regex "^$subdomain1.|$subdomain2." and ($uri matches_regex "(jpg|bmp|gif|png|img|ico|js|css|swf|swz|vbs|html|htm|csv|txt|crossdomain.xml)$")}

 

{ if { $uri matches_regex "^/(cswebtool|cobrands|jsrs|overflow|promotions|data|resources)" }

 

{ persist none

 

pool SRWD25-STATIC return

 

}

 

}

 

 

 

else { xxxxx

 

 

 

 

Now I visit

 

 

 

http://sjj-cache28.dddstatic.com/resources/flex/ticketFilter-1.10.2.swf

 

 

Will this match pool ddd25-STATIC

 

 

Because in my opinion,

 

$host matches_regex "^$subdomain1.|$subdomain2."

 

so it should be static.|s because "|" is contained by ""

 

the operator "|" means "or" or only a string?

 

 

confused again
  • Yes, it should match as a pipe is considered a logical OR in regex. Here's a simple test to show the match:

    when RULE_INIT {
    
        http://sjj-cache28.dddstatic.com/resources/flex/ticketFilter-1.10.2.swf 
       set host sjj-cache28.dddstatic.com
       set uri "/resources/flex/ticketFilter-1.10.2.swf"
       set subdomain1 "static" 
       set subdomain2 "s"
       if { $host matches_regex "^$subdomain1.|$subdomain2." and ($uri matches_regex "(jpg|bmp|gif|png|img|ico|js|css|swf|swz|vbs|html|htm|csv|txt|crossdomain.xml)$")} {
       if { $uri matches_regex "^/(cswebtool|cobrands|jsrs|overflow|promotions|data|resources)" }{
             log local0. "matched"
             return
          }
       }
       log local0. "no match"
    }
    

    < RULE_INIT >: matched

    However, it would be a lot more efficient to replace the regexes with string commands:

    
    when HTTP_REQUEST {
    
       switch -glob [string tolower [HTTP::host]] {
          "s.*" -
          "static.*" {
             switch -glob [HTTP::path] {
                "*.jpg" -
                "*.bmp" -
                "*.gif" -
                "*.png" -
                "*.img" -
                "*.ico" -
                "*.js" -
                "*.css" -
                "*.swf" -
                "*.swz" -
                "*.vbs" -
                "*.html" -
                "*.htm" -
                "*.csv" -
                "*.txt" -
                "*.crossdomain.xml" {
                   switch -glob [HTTP::path] {
                      "/cswebtool*" -
                      "/cobrands*" -
                      "/jsrs*" -
                      "/overflow*" -
                      "/promotions*" -
                      "/data*" -
                      "/resources*" {
                         persist none
                         pool SRWD25-STATIC
                         return
                      }
           }
                }
             }
          }
       }
    }
    

    Aaron
  • oh,Thanks ,it seems match_regx think ^should apply to $subdomain1. and $subdomain2.
  • If you want the regex to match on a string starting with $subdomain1 or $subdomain2 I think you'd need to use parentheses around them or expliclity put the caret on both:

     

     

    "^(?:$subdomain1|$subdomain2)\."

     

     

    Aaron
  • Hi,Aaron

     

    matches_regex "^$subdomain1|$subdomain2" also can match string start with "s"

     

     

    Am I right?

     

     

    I also don't like matches_regex,.....but this irule is wrote by another network huy,I need to maintain this ..................

     

     

  • Actually, the issue seems to be that the backslash needs to be escaped. This is necessary as the regex is wrapped in double quotes. If you didn't have any variables that needed to be interpreted, you could wrap the regex in curly braces.

     

     

    So this code correctly logs no match:

     

     

    set host "sjj-cache28.dddstatic.com"
    set subdomain1 "static" 
    set subdomain2 "s"
    if { $host matches_regex "^(?:$subdomain1|$subdomain2)\\." }{
    log local0. "matched host check for $subdomain2 on $host"
    } else {
    log local0. "no match on host check for $subdomain2 on $host"
    } 

     

     

    Aaron