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 );
Owen Garrett [Zeus Dev Team] 01 July 2005  Permalink 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]
The second line of the code sample above has an extra bracket, and should read as below:- if( http.getResponseHeader( "Content-Type" != "text/html" ) ) break;
Permalink 12 March 2006 @ 23:02
Comment from: Owen Garrett [Zeus Dev Team]
Thanks for that - well spotted. I've corrected the article.

Owen

Permalink 13 March 2006 @ 09:48
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.getHeader( "Content-Type" );
if( !string.startsWith( $ct, "text/html" ) ) break;


or, for more brevity:

if( !string.startsWith( http.getHeader( "Content-Type" ), "text/html" ) ) break;


[The earlier version of this comment missed the '!' - sorry for any confusion this may have caused]
Permalink 29 August 2006 @ 14:06
Comment from: slyphon [Member]
Unless I'm misinterpreting the desired effect from this code, it would seem that you'd want the *negation* of the above:


if( !string.startsWith( http.getHeader( "Content-Type" ), "text/html" ) ) break;

as you want to process "text/html" responses, and not anything else.


Or am I being dense?
Permalink 19 January 2008 @ 04:01
Comment from: Owen Garrett [Zeus Dev Team]
No - you're exactly correct! Thank you for pointing that out.

I've updated the comment above so that the code is now correct.
Permalink 21 January 2008 @ 09:22
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
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)
Download Free ZXTM Desktop Edition

Recent Articles

Other Resources



www.zeus.com