Forum Discussion

Rusty_Hale_8009's avatar
Rusty_Hale_8009
Icon for Nimbostratus rankNimbostratus
Apr 05, 2005

iRule conversion from 4.X to V9

I have made an attempt to convert an iRule to V9. Could someone please check my syntax? I would really appreciate it. Thanks.

 

 

if { [HTTP::host]

 

rule Image_Rule {

 

 

if (http_host == "www.mydomain.com" and (http_uri ends_with "jpg" or http_uri ends_with "gif")) {

 

 

use pool Image_Svr_80

 

 

}

 

 

else {

 

 

use pool Main_Svr_80

 

 

}

 

3 Replies

  • Here's my crack, though I don't have a box currently to test on. I'm sure the and/or logic can be reduced, I'm curious if that would make the rule more processor efficient?

     

     

    Unless jpg or gif would be anywhere else in your URL, I'd suggest using contains instead of ends_with. In the event a trailing / is appended in the uri, the rule wouldn't match. I added logging that you can use strictly while working on your rules.

     

     

    rule Image_Rule {

     

     

    when HTTP_REQUEST

     

    {

     

    if ( [HTTP::host} equals "www.mydomain.com" ) {

     

    if ( [HTTP::uri] contains "jpg" ) {

     

    use pool Image_Svr_80

     

    log "JPG file requested by [IP::client_addr]"

     

    }

     

    elseif ( [HTTP::uri] contains "gif" ) {

     

    use pool Image_Svr_80

     

    log "GIF file requested by [IP::client_addr]"

     

    }

     

    else {

     

    use pool Main_Svr_80

     

    log "Default LB Method for [IP::client_addr]"

     

    }

     

    }
  • I think I missed a bracket or fifty:

     

     

    rule Image_Rule {

     

     

    when HTTP_REQUEST

     

    {

     

    if ( [HTTP::host} equals "www.mydomain.com" ) {

     

    if ( [HTTP::uri] contains "jpg" ) {

     

    use pool Image_Svr_80

     

    log "JPG file requested by [IP::client_addr]"

     

    }

     

    elseif ( [HTTP::uri] contains "gif" ) {

     

    use pool Image_Svr_80

     

    log "GIF file requested by [IP::client_addr]"

     

    }

     

    }

     

    else {

     

    use pool Main_Svr_80

     

    log "Default LB Method for [IP::client_addr]"

     

    }

     

    }

     

    }

     

  • citizen_elah,

    There's a few syntax errors (if's use curly braces, and else,elseif should be on the same line as closing curly brace) in there but by cleaning those your solution should work. Here are 3 possible rules (assuming the uri actually "ends with" the image type - otherwise use the "contains operator").

    Here's a modified version of your solution

     when HTTP_REQUEST { 
       if { [HTTP::host] equals "www.mydomain.com" } { 
          if { [HTTP::uri] ends_with ".jpg" } { 
            use pool Image_Svr_80 
            log "JPG file requested by [IP::client_addr]"  
          } elseif { [HTTP::uri] ends_with ".gif" } { 
            use pool Image_Svr_80  
            log "GIF file requested by [IP::client_addr]"  
          } else { 
            use pool Main_Svr_80  
            log "Default LB Method for [IP::client_addr]"  
          } 
        } 
      }

    If you want to shorten this up without all the detailed logging for each image type, you could also do the following

     when HTTP_REQUEST { 
        if { [HTTP::host] equals "www.mydomain.com" and \ 
            [HTTP::uri] ends_with ".jpg" or \ 
            [HTTP::uri] ends_with ".gif" } { 
          use pool Image_Svr_80 
          log "Image file requested by [IP::client_addr]"  
        } else { 
          use pool Main_Svr_80 
          log "Default LB Method for [IP::client_addr]"  
        } 
      }

    If the group of image types increase, you might want to use a data group to store the image types to simplifly the script

     class images { 
        ".gif" 
        ".jpg" 
      } 
      
      when HTTP_REQUEST { 
        if { [HTTP::host] equals "www.mydomain.com" and \ 
            [matchclass [HTTP::uri] ends_with $::images] } { 
          use pool Image_Svr_80 
          log "Image file requested by [IP::client_addr]"  
        } else { 
          use pool Main_Svr_80 
          log "Default LB Method for [IP::client_addr]"  
        } 
      }

    *Note that the "class image{...}" is not included in the rule but where declared externally as a data group list.

    ** Also note that the log lines are optional and can be included for trouble shooting.

    -Joe