Forum Discussion

Dachicourt_Fabi's avatar
Dachicourt_Fabi
Icon for Nimbostratus rankNimbostratus
Jan 10, 2005

Add multiple content on mutliple match in DATA RESPONSE

Hello,

 

i want redirect all images (under image folder that call relativly in code) of my web site directly by modifying the http answer without modify the code, i write this :

 

 

when HTTP_RESPONSE {

 

set clen [HTTP::header Content-Length]

 

if {$clen > 0} {

 

HTTP::collect $clen

 

}

 

}

 

 

when HTTP_RESPONSE_DATA {

 

set found [string first "images/" [HTTP::payload $clen] $x]

 

HTTP::payload replace $found 0 [binary format "A*" "http/195.6.184/"]

 

}

 

 

and that work but only for may first image. So i write this to do on all images without succes :

 

 

when HTTP_RESPONSE {

 

set clen [HTTP::header Content-Length]

 

if {$clen > 0} {

 

HTTP::collect $clen

 

}

 

}

 

 

when HTTP_RESPONSE_DATA {

 

set last [string last "images/" [HTTP::payload $clen] ]

 

for {set x 0} {$x<$last} {}

 

{

 

incr x

 

set found [string first "images/" [HTTP::payload $clen] $x]

 

HTTP::payload replace $found 0 [binary format "A*" "http/195.6.184/"]

 

set x $found

 

}

 

}

 

 

have you any idea that i doing wrong ?

5 Replies

  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    I tried the following rule and it works fine for me using 9.0.3 (it could be something we fixed in 9.0.3):

     
     rule image_replace { 
        when HTTP_RESPONSE { 
           set clen [HTTP::header Content-Length] 
           if { $clen > 0 } { 
              HTTP::collect $clen 
           } 
        } 
        when HTTP_RESPONSE_DATA { 
           set last [string last "images/" [HTTP::payload $clen]] 
           for {set found 0} {$found < $last} {incr found 22} 
           { 
              set found [string first "images/" [HTTP::payload $clen] $found] 
              HTTP::payload replace $found 0 "http/195.6.184" 
           } 
        } 
     } 
     

    In the "incr found 22" statement above, the number 22 comes from the length of the inserted text ("http/195.6.184" - 15 bytes), plus the length of the search string ("/images" - 7 bytes). You will want to adjust this to your specific strings. It's important to skip the inserted length because you could introduce an infinite loop if not.

    Also, instead of doing this with a rule, you may want to consider using the stream profile. This profile allows the generic replacement of a string throughout the payload. You would configure it like so:

     
     profile stream image_replace { 
        source "images/" 
        target "http/195.6.184/images/" 
     } 
     virtual my_vip { 
        destination a.b.c.d:80 
        ip protocol tcp 
        profile http tcp 
        profile image_replace serverside 
        snat automap 
        ... 
     } 
     

    The stream profile is much more efficient at replacing strings throughout the content.

    I used your example strings verbatim, so I assume you will update them to whatever you need.

    Let us know how it turns out.

  • i have try both the irules and the stream profile.

     

    the irules not working perfectly, but the strema profile working good.
  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    Is using the stream profile going to work ok for you, or would you like us to continue to help with what's going on in your rule?

     

     

    Like, I said, I did test the above rule and it was working fine for me on 9.0.3. Are you running 9.0.2 or 9.0.3? Also, I don't know if you posted just an "example" of your rule, but you are welcome to send your actual rule directly to me and I will check it over.

     

     

    Thanks!

     

     

  • Hello,

     

    i am a very begginer with f5 (and especially with tcl programming) and i am supposed to make a rewrite rule for url's in a web page residing on a server

     

    The actual flow is like this:

     

    the client type https://server/resource -> the F5 terminate the SSL and send to the server the http://server/resource. Then the server answer with a page containg differrent (one or more) links like http://otherplace/resource. The F5 forward that to the client and should rewrite all the links in the page so they appear on the client browser like this: https://otherplace/resource. By doing this we know that the client will always initiate a https request instead of an http one.

     

     

     

     

    Can this be done? If yes can you show me how?

     

     

    Thank you,

     

     

    Costin

     

  • Yes, you can do this with a stream profile, no iRule required. Make sure you have chunking set to rechunk in your http profile.