Rewriting HTTP responses

Suppose that you wanted to publish an internal website (http://intranet.mycorp.com/) so that it can be accessed externally (with the correct access controls and security of course!). However, the domain name internal.mycorp.com is not valid externally, and you want to refer to the site via the URL https://extranet.mycorp.com.

One key requirement is to rewrite any HTML that includes references to intranet.mycorp.com, so that they refer to extranet.mycorp.com. The following response rule will do this:

# We only want to process text/html responses (ie, not images or other types)
if( http.getResponseHeader( "Content-Type" ) != "text/html" ) break;
$response = http.getResponseBody();
# replace 'http://intranet.mycorp.com/' with 'https://extranet.mycorp.com/'
$response = string.regexsub( $response,
"http://intranet.mycorp.com/", "https://extranet.mycorp.com/", "g" );
http.setResponseBody( $response );

Here's a briefer version that works with current versions of Zeus Traffic Manager:

if( !string.startsWith( http.getResponseHeader( "Content-Type" ), "text/html" ) )
break;
$response = http.getResponseBody();
$response = string.replaceAll( $response,
"http://intranet.mycorp.com/", "https://extranet.mycorp.com/" );
http.setResponseBody( $response );
Owen Garrett [Zeus Dev Team] 01 July 2005 Bookmark with del.icio.us Post this article to Digg Post this article to reddit Post this article to Facebook Tweet this article 8 comments  

Comments:

This public messageboard is not a forum for technical support. To report technical support problems, please contact our dedicated Support team using the instructions at the bottom of this page.

Comment from: Steven Bibby [Visitor]
As per the TrafficScript reference, the result of the http.getResponseHeader( "Content-Type" ) function could be something like 'text/html; charset=ISO-8859-1'. If this is the case, then this example will break and not run the string.regexsub.

Do you have an alternative that takes this into account?
Permalink 29 August 2006 @ 14:00
Comment from: Owen Garrett [Zeus Dev Team]
Good point... try using string.startsWith() as follows:
$ct = http.getResponseHeader( "Content-Type" );
if( !string.startsWith( $ct, "text/html" ) ) break;
or, for more brevity:
if( !string.startsWith( http.getResponseHeader( "Content-Type" ), "text/html" ) )
    break;
Permalink 29 August 2006 @ 14:06
Comment from: Adam Daughterson [Visitor]
This is great for TrafficScript, but I'm not finding any way to do this with RuleBuilder and ZXTM complains that TrafficScript rules cannot be run under my current license. Any help?
Thanks in advance,
AD
Permalink 12 February 2008 @ 21:09
Comment from: Owen Garrett [Zeus Dev Team]
This type of capability is only available via TrafficScript, not RuleBuilder. You may have a ZXTM LB license (which just permits RuleBuilder), rather than a full ZXTM license.
Permalink 18 February 2008 @ 10:46
Comment from: Jenny Martin [Visitor]
I have been using this code happily for some time, but now it is crashing zxtm trying to handle a giant 2GB response body as one string. Any chance of a chunked version?
Permalink 28 April 2010 @ 16:58
Comment from: Owen Garrett [Zeus Dev Team]
Hi Jenny - take a look at the http response streaming functions we added in the Zeus Traffic Manager 6.0 release. These http.stream.* functions let you read responses in manageable chunks and modify them without having to read the entire response in in one go. There are examples in the TrafficScript manual.
Permalink 28 April 2010 @ 17:19
Comment from: Jonathan Garratt [Visitor]
So is there any way of achieving this if you only have the ZXTM LB license please?
Permalink 11 June 2010 @ 08:22
Comment from: Owen Garrett [Zeus Dev Team]
Hi Jonathan,

Zeus LB does not have TrafficScript; it just provides the simpler RuleBuilder interface.

Rulebuilder does not have the capabilites to modify HTTP responses in the way described in this article. You may be able to use the Change Site or HTTP Redirect actions to intercept incoming requests and redirect or rewrite them; in the example above, you would need to ensure that externally, the domain intranet.mycorp.com resolves to an appropriate IP that is handled by the load balancer.
Permalink 11 June 2010 @ 11:47
Leave a comment ...
Your email address will not be displayed.
Your URL will be displayed.
This public messageboard is not a forum for technical support. To report technical support problems, please contact our dedicated Support team using the instructions at the bottom of this page.
Options:
 
(Line breaks become <br />)
(Set cookies for name, email & url)

Recently...

Other Resources