Simulating Apache Asis Feature With Very Simple Cgi
Posted on 30 Oct 2007
by Robert Marder (robertmarder)
Rated 3.89 (Ratings: 0)
- More articles in Site Development
Apache's asis feature, provided by mod-asis, is a very simple way of sending static HTTP headers along with static content. Many installations of Apache have this disabled (the default) and those of us not on Apache are out of luck. This combination has led to this simple and useful feature being overlooked by most web developers.
Following is a simple method of simulating the behavior of asis using CGI. While CGI does tend to be slower, the only speed loss here is how long it takes the server to launch a small program since the content itself is static. For performance and simplicity, we will be using the tail program, which is included in virtually all unix-like systems. Here is the shebang line you would add to the start of your asis file:
The above command executes tail and the --lines=+2 indicates that we want the first line to be skipped. This is needed because the first line is the shebang line. In other parsed CGI languages the # would be treated as a comment, but tail does not parse the text, it just outputs it. If this wasn't an issue then we could just use the cat command instead.
Here is an example use of our simulated asis feature. Here we have this file at http://example.com/app/index.cgi
#!/usr/bin/tail --lines=+2Content-Type: text/htmlLocation: http://example.com/app/version2/
It serves as a redirect for us. Other uses for this feature include under construction pages that you do not want cached, refreshing pages that reference client side script that changes often or is somehow dynamic (like adverts or status page), and sending or clearing a cookie (and possibly redirecting or refreshing).
Please note that we are not restricted to using tail. We could use dog (an enhanced replacement for cat) like so:
#!/usr/bin/dog -l 2- -
If you are on a Windows server, then you are likely out of luck because Windows doesn't have simple command-line tools like tail included (they might be found by searching the net, however). If you were on Windows this likely would not be ideal anyway because CGI is much more expensive on Windows than on unix-like systems, and because Windows doesn't parse the shebang line.
So, we have a simple, fairly efficient way to transform dynamic pages that send headers while the rest is echo or print statements using a CGI solution that is not slow by any means.