Outbound iRule STREAM Rewrites for CDN

Hi Folks,

We have a legacy app that has a lot of static content that we want to move to a CDN. I have succesfully created a stream profile to do some of the outbound translation, however this is a lot of management overhead as each asset has to have an explicit source/dest rewrite string pair. Ideally I’d like to do it programatically.

What I am trying to to is rewrite outbound HTTP responses with string replacement of the form

search1: src=“/path/to/static/asset.[jpg,gif,png,swf]”

replace1: src=“http://my.cdn/path/to/static/asset.\[jpg,gif,png,swf]”

I know it sounds easy but I am having major grief getting an iRule to work.

I have tried messing about with STREAM::expression and STREAM::replace but I can’t seem to get my regexps to work.

Any Ideas?

Iain

Hi Iain,

You should be able to do this with a STREAM::expression based iRule. In HTTP_RESPONSE, set the STREAM::expression to:

[http://devcentral](http://devcentral).f5\.com/path/to/static/asset\.(?:jpg|gif|png|swf)

Then in the STREAM_MATCHED event, you can retrieve the full match using STREAM::match and rewrite the string using string map:

when STREAM_MATCHED {

   log local0. "[IP::client_addr]:[TCP::local_port]: matched: [STREAM::match], replaced with: [string map {http://devcentral.f5.com/ http://my.cdn/} [STREAM::match]]"  
   STREAM::replace "[string map {http://devcentral.f5.com/ http://my.cdn/} [STREAM::match]]"  
}

For details on the HTTP_RESPONSE code, check the STREAM::expression wiki page:

http://devcentral.f5.com/wiki/default.aspx/iRules/stream__expression

Aaron