Forum Discussion

Moe_Jartin's avatar
Feb 08, 2010

Conditional stream rewrite based on requested uri

I want to rewrite some content based on the uri that is requested. I started with:

 
 when HTTP_REQUEST { 
   switch -glob [string tolower [HTTP::uri]] { 
   /uri1* - 
   /uri2*  { 
   snatpool SNAT_DMZ 
   pool pool_www.mysite.org_URI1 
   } 
   /testlink* { 
   snatpool SNAT_APZ 
   STREAM::expression "@http://www.mysite.org@https://www.mysite.org@" 
   STREAM::enable 
   pool pool_www.mysite.org_TESTLINK 
   } 
  } 
 } 
 

But then I realized that was only rewriting the request content and NOT the response content. So I need to catch/log the requested uri in a variable and only rewrite the content in the responses to the /testlink uri. Her is my attempt a pieceing together some examples I have found on the forum.

 
 when HTTP_REQUEST { 
   switch -glob [string tolower [HTTP::uri]] { 
   /uri1* - 
   /uri2*  { 
   snatpool SNAT_DMZ 
   pool pool_www.mysite.org_URI1 
   } 
   /testlink* { 
   snatpool SNAT_APZ 
   set requested-uri {[HTTP::uri]} 
   pool pool_www.mysite.org_TESTLINK 
   } 
  } 
 } 
  
 when HTTP_RESPONSE { 
   if {$requested-uri starts_with "/testlink"} { 
     STREAM::expression "@http://www.mysite.org@https://www.mysite.org@" 
     STREAM::enable 
   } else { 
     STREAM::disable 
   } 
 } 
 

So my question is, Is a variable set during the HTTP_REQUEST event available under the HTTP_RESPONSE event? I have not been able to test this as this is a semi-production app.

Thanks,

Joe

4 Replies

  • Hi Joe,

    That looks good. With TCL, you need to escape hyphens in variable names with curly braces: ${requested-uri}. It's easier to use an underscore instead.

    Here is an updated version you can try:

     
     when HTTP_REQUEST { 
       switch -glob [string tolower [HTTP::uri]] { 
         /uri1* - 
         /uri2* { 
           set check_response 0 
           snatpool SNAT_DMZ 
           pool pool_www.mysite.org_URI1 
         } 
         /testlink* { 
           snatpool SNAT_APZ 
           set check_response 1 
           pool pool_www.mysite.org_TESTLINK 
         } 
       } 
     } 
     when HTTP_RESPONSE { 
       if {$check_response} { 
         STREAM::expression "@http://www.mysite.org@https://www.mysite.org@" 
         STREAM::enable 
       } else { 
         STREAM::disable 
       } 
     } 
     

    Aaron
  • Aaron,

    Thanks again for your help! In your updated rule you used "set check_response 0" and "set check_response 1". This works if there are only two cases. I had actually truncated my irule a bit and there are actually several switch cases. I suppose I could just set check_response to 1 for each uri I wanted to use the stream expression for? Assuming I wanted to use the same stream expression for all match cases.

    What if I wanted to use a different stream expression for each uri? Could I set check_response to 1, 2, 3, respectively, and then do a

     
     switch $check_response { 
        1 { 
        STREAM::expression "@http://host1@https://host1@"  
        STREAM::enable 
        } 
        2 { 
        STREAM::expression "@http://host2@https://host2@"  
        STREAM::enable 
        } 
        3 { 
        STREAM::expression "@http://host3@https://host3@"  
        STREAM::enable 
        } default { 
        STREAM::disable 
        } 
     }    
     

    Also, rather than use 1, 2, 3, I could use something more intuitive that matches the path like uri1, uri2, uri3. This doesn't necessarily apply to this scenario but I have needed/wanted to do this type of thing before and just didn't know how.

    Thanks,

    Joe
  • Hi Joe,

    That looks like it would work. You could save a bit on the switch evaluation by setting the stream expression in a variable in HTTP_REQUEST for cases you want to use a stream profile for (and unsetting it by default on each request):

     
     when HTTP_REQUEST { 
      
        Unset the stream_expr variable if it exists already 
       if {[info exists stream_expr]}{ unset stream_expr } 
      
        Check the requested URI 
       switch -glob [string tolower [HTTP::uri]] { 
         /uri1* - 
         /uri2* { 
           set stream_expr "@http://www.mysite.org@https://www.mysite.org@" 
           snatpool SNAT_DMZ 
           pool pool_www.mysite.org_URI1 
         } 
         /uri3* - 
         /uri4* { 
           set stream_expr "@http://www.mysite4.org@https://www.mysite4.org@" 
           snatpool SNAT_DMZ 
           pool pool_www.mysite.org_URI4 
         } 
         /uri5* - 
         /uri6* { 
           snatpool SNAT_DMZ 
           pool pool_www.mysite.org_URI4 
         } 
         /testlink* { 
           set stream_expr "@http://www.mysite.test.org@https://www.mysite.test.org@" 
           snatpool SNAT_APZ 
           pool pool_www.mysite.org_TESTLINK 
         } 
       } 
     } 
     when HTTP_RESPONSE { 
       if {[info exists stream_expr]} { 
         STREAM::expression $stream_expr 
         STREAM::enable 
       } else { 
         STREAM::disable 
       } 
     } 
     

    Aaron
  • Beautiful!! Does that qualify as a Nerd Alert if I think an iRule is beautiful?? Thanks a ton!