Stream Profile: Replace String

Hi Guys,

I had configured a virtual server for our secure site. The content is legacy code and it has hardcoded urls for http. Currently I am using stream profile to replace all the http:// to https://

The code also contains javascript which has hardcoded http%3A%2F%2F, which should be replaced with https%3A%2F%2F. Is there a way to make stream profile perform this replacement as well.

Thanks for your help as always

Srini

Are you having trouble adding multiple replace strings, or getting the second replace string to work?

You should be able to add multiple search and replace strings in a single stream profile, as of v9.2.

-Colin

HI Colin,

Thanks for the information. I do not know how to add the second string. Is there an example I can look at.

Thanks.

Here’s a doc about managing profiles in 9.2. :

Click here

I’ll see if I can dig up anything more specific, or maybe write up some brief instructions, and get back to you with what I find later today.

-Colin

Thanks Colin. This is what I had configured now

profile stream httpR_stream {

defaults from stream

source “http://”

target “https://”

}

so how do I add another search and replace for “prodSrv” to “prod2Srv”

Thanks,

Srini

HI Guys,

Is it possible to use regular expression for search string in stream profile.

Also if someone could post an example on how to add a second search and replace string, would be of much help

Yes but only on 9.2 and later. There are several enhancements worth noting here.

. We added unlimited search and replace target pairs that can be expresses as sed replacement expressions.

. We also added a new event called STREAM_REPLACED where you can query for the matching string and also override the replacement if you desire to do so.

Thanks for the information. We are using 9.2. Can you post an example to search and replace two different strings.

Expression syntax :

The expression syntax should be fairly familiar to most UNIX users, as it’s a ‘SED’ like replacement pairs of source and targets shown below. expression := /// // //. If the *last* target expression is empty, then a match event will be fired but there will be no replacement performed. The only caveat is that an empty target *must* be specified in the *last* expression. When the filter matches one of the expressions say for example ‘source1’, the matching content will be replaced by ‘target1’.

Configuration :

The new stream behavior can be enabled by applying the new expression to the ‘target’ stream profile and leaving the ‘source’ profile *empty*. If both ‘source’ and ‘target’ values are set, the stream filter will default to the original behavior of looking for string literals.

STREAM_MATCHED - Fired when a when a stream expression matches. This event can be used to skip or change the replacement target.

STREAM::disable - Disables any further processing on the stream filter for this connection. This can be called from HTTP events.

STREAM::enable - Enables the stream filter. This can be called from HTTP events.

STREAM::expression - Replace the stream expression specified in the profile with this value. The syntax is identical to the profile syntax. NOTE that this change affects just this connection and is sticky for the duration of the connection. Use this rule wisely!

STREAM::match - Returns the matching characters.

STREAM::replace [] - Can be used to change the target replacement string specified in the profile. This call is not sticky and will be applied only once during the current match. If the ‘target’ expression is missing, the replacement will be skipped.

Appreciate your quick response. Following your post, this is what I have and it’s not working

expression := /http://testbigip/https://testbigip /http.3A.2F.2Ftestbigip/https.3A.2F.2Ftestbigip /testbigip/

Could you take a minute and point out what’s wrong

Thanx

Srini

Sorry, my previous posting is confusing.

I want to replace all content which is like

http://testbigip to https://testbigip and

http%3A%2F%2Ftestbigip to https%3A%%2F%2Ftestbigip

i tried this expression and it;s not working

expression := /http(*)test/https&testbigip/ /dummy/

Any help would be appreciated

You need to either escape or use a different expression separator. The example below uses the character ‘’ as the expression separator.

/uri1/uri2//uri3/uri4/

Am I missing something? How are you setting the expression separator? (Is this set in the profile?)

-Brian

Also, what is the default context of STREAM_REPLACED?

Thanks,

Brian

STREAM::replace [] - Can be used to change the target replacement string specified in the profile. This call is not sticky and will be applied only once during the current match. If the ‘target’ expression is missing, the replacement will be skipped.

Does this mean that if I execute “STREAM::replace” without any arguements, the stream profile default action won’t take effect?

Thanks,

Brian

Thanks for the information. I tried with and this is the error I am getting when I try to update the stream profile

Bad Characters. Only the following special characters are allowed: period, asterisk, forward slash, dash, colon, underscore, question mark, equals, at sign, comma, ampersand and space (.*/-:_?=@,&

/http://testbigip/https://testbigip/

The configuration is still handled from the stream profile. Adding the expression to the ‘target’ profile value and leaving the ‘source’ empty triggers the new behavior.

Calling STREAM::replace without any arguments will skip the replacement.

The new event is stream STREAM_MATCHED and can be fired both on the client and/or server side depending on your profile configuration.

Finally, the *first* character of the expression is treated as the expression separator. If you are having trouble with the ‘’ character, give this a try @/http://testbigip@https://testbigip/@

I have found something like this seems to work for replacing multiple items within a stream

when HTTP_RESPONSE {

STREAM::enable

STREAM::expression @IE@Apache@@Windows@Linux@

}

This would replace IE with Apache and Windows with Linux

9.2 is required as well as a stream profile defined within the Virtual Server. Just a blank stream profile works.

Hmm…

Is there any way to make the replacement sting a variable (e.g. [HTTP::header host])

H

Hi Hamish,

Yes, STREAM::expression accepts variables. Just make sure to wrap the string in double quotes instead of curly braces.

Also, I think you’ll need to save the HTTP::host value in HTTP_REQUEST. And it would be good to only enable the stream filter for text responses when the client used a host header.

http://devcentral.f5.com/Wiki/default.aspx/iRules/STREAM__expression.html

when HTTP_REQUEST { 
     Save the host header value 
    set host [HTTP::host] 
 } 
 when HTTP_RESPONSE { 
  
     Disable the stream filter by default 
    STREAM::disable 
  
     Check if response type is text 
    if {[HTTP::header value Content-Type] contains "text" && $host ne ""}{ 
  
        Replace any instance of the requested with newhost.example.com 
       STREAM::expression "@$host@newhost.example.com@" 
  
        Enable the stream filter for this response only 
       STREAM::enable 
    } 
 }

Aaron