Platinum Solutions Corporate Website


On the fly page modification with mod_ext_filter

Recently, I had wanted to be able to add a header and footer to every web page on a site, but I didn’t want to actually change any code.

The web application in question was hosted on a Tomcat instance that was reverse proxied though Apache 2.2 (which is real easy to set up thanks to "mod_proxy_ajp" that comes with Apache 2.2).

So how did I do this? Well, Apache 2 comes with a super convenient module called "mod_ext_filter" that lets you re-write a page using any executable that takes the original page on "standard in", and writes out the new page on "standard out".

For my case, I wrote a perl script that looks for the "<body>" and "</body>" tags, and adds a header and footer in the appropriate places:


#!/usr/bin/perl
 
$header=<<end;
<table width="100%"><tr><td>
  My on the fly header
</td></tr></table>
END
;
 
$footer=<<end;
<table width="100%"><tr><td>
  My on the fly footer
</td></tr></table>
END
;
 
while (<stdin>) {
  s#(<body.*>)#\1 $header#iog;
  s#(</body>)#$footer \1#iog;
  print;
}
exit 1;

To set up the filter, I edited the httpd.conf file.

First, I define the filter. This is essentially specifying the executable and some configuration information, and giving it all a name. (note: the name must be in all lower case!)

Next, I specify the filter to use in the <Location> section where I configured the reverse proxy.


# Define our "add_header_footer" filter
ExtFilterDefine add_header_footer \
  mode=output intype=text/html outtype=text/html \
  cmd="/usr/local/tomat/scripts/addHeaderFooter.pl"
 
# This reverse-proxy mapping uses the
# "add_header_footer" filter
<Location>
  ProxyPass ajp://localhost:8009/myapp
  ProxyPassReverse ajp://localhost:8009/myapp
  SetOutputFilter add_header_footer
</Location>

The reason the filter name has to be in all lower case is because whatever value you specify on the "SetOutputFilter" line will be converted to all lower case before looking up the corresponding "ExtFilterDefine" entry.

 

Another use we found for "mod_ext_filter" was handling a reverse proxied web application that hard coded absolute URLs into its pages.

At our site, we have a "development portal" running on Apache 2.2 that serves up Bugzilla and Subversion. Rick Witter set up "cruise control" for us and made it available from our portal via reverse proxy, but a number of the links would fail because of absolute URLs that were not being modified by the proxy.

To solve this, we again turned to "mod_ext_filter". Rick wrote some perl scripts to convert all the absolute URLs to match the proxy URL.

Comments

lbjay (not verified) Wed, 1969-12-31 19:00

Why would your filter script want to return a non-zero exit status, i.e., the "exit 1;" ?

MRD (not verified) Wed, 1969-12-31 19:00

i've tried smthg similar and it worked only for the first request that apache handled, after that any other request just hungs. Here it is:

ExtFilterDefine myfilter \
mode=output intype=text/html outtype=text/html \
cmd="c:\\Progra~1\\perl\\bin\\perl.exe c:\\webpage\\perlfilter.pl"
ProxyRequests off

ProxyPass http://internal.com
ProxyPassReverse http://internal.com
SetOutputFilter myfilter
ExtFilterOptions LogStdErr DebugLevel=1
RequestHeader unset Accept-Encoding
ProxyPassReverseCookieDomanin internal.com external.com

IMHO everything looks fine. Any ideas on how to make this work?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.

More information about formatting options