Forum Discussion

DB's avatar
DB
Icon for Nimbostratus rankNimbostratus
May 11, 2007

Can iRule be used to modify HTML code itself being passed?

Can iRules be used to modify the contents of an HTML page being returned to a client HTTP request? For example, if HTML page includes any link that begins with "http://images.dot.com/*" change it to "https://images.dot.com/*". I have an application that's too dumb to send the links back as HTTPS for images on a page, so my users get the "This page contains secure and unsecure items, are you sure you want to see the unsecure..." warning message.

 

 

BTW, I'm already doing a HTTP to HTTPS redirect so that if the user does try to his the HTTP://images... link they'll get it, but it's the warning about mixed content I'm trying to alter.

4 Replies

  • Of course you can modify the content in the response. You can use the HTTP::payload command to return the current payload into a variable, modify the content in that variable, and then issue the HTTP::payload command again passing in the new payload. To perform substitutions, you can use the TCL regsub or "string map" commands.

     

     

    But, if you are doing a simple conversion from one string to another, the easiest approach is with the stream profile associated with your virtual server. This thread

     

     

    http://devcentral.f5.com/Default.aspx?tabid=53&view=topic&forumid=5&postid=13504

     

    Click here

     

     

     

    goes over many options for replacing content in the response. The main thing to remember is that you need to have "Response Chunking" set to "Rechunk" in your HTTP profile if you are going to modify the length of the response payload.

     

     

    -Joe
  • DB's avatar
    DB
    Icon for Nimbostratus rankNimbostratus
    Thanks for pointing me to the Stream profile. I created a simple example and see it working.

     

     

    I'm running 9.0 and 9.1. If I understand correctly, I can do a single string replacement in these versions, but if I wanted to do regex or multiple replacements, I need 9.2, correct?
  • I do believe that you'll need 9.2 to support multiple replacements in a single stream profile. Also the stream profile does not support regular expressions (as far as I know). For that route you'll have to do it manually with the HTTP::payload command in conjuction with the TCL regsub command.

     

     

    -Joe
  • I was able to get regexes to work by wrapping the stream expression in {}'s:

    This example looks for IP addresses in response content and replaces them with 0.0.0.0:

    
    when HTTP_RESPONSE {
       STREAM::enable
       STREAM::expression {@\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}@0.0.0.0@}
    }

    Backreferences didn't seem to work though... the matched string is replaced with a literal \1.

    Aaron