Book HomeApache: The Definitive GuideSearch this book

4.9. Actions

A related notion to that of handlers is actions. An action passes specified files through a named CGI script before they are served up.

4.9.1. Action

Action type cgi_script
Server config, virtual host, directory, .htaccess

The cgi_script is applied to any file of MIME or handler type matching type whenever it is requested. This mechanism can be used in a number of ways. For instance, it can be handy to put certain files through a filter before they are served up on the Web. As a simple example, suppose we wanted to keep all our .html files in compressed format to save space, and to uncompress them on the fly as they are retrieved. Apache happily does this. We make site.filter a copy of site.first, except that the httpd.conf file is as follows:

User webuser
Group webgroup
ServerName localhost
DocumentRoot /usr/www/site.filter/htdocs
ScriptAlias /cgi-bin /usr/www/cgi-bin
AccessConfig /dev/null
ResourceConfig /dev/null
AddHandler peter-zipped-html zhtml
Action peter-zipped-html /cgi-bin/unziphtml
<Directory /usr/www/site.filter/htdocs>
DirectoryIndex index.zhtml
</Directory>

The points to notice are that:

Action peter-zipped-html /cgi-bin/unziphtml

The CGI script ... /cgi-bin/unziphtml contains the following:

#!/bin/sh
echo "content-type: text/html"
echo
gzip -S .zhtml -d -c $PATH_TRANSLATED

This applies gzip with the following flags:

-S

Sets the file extension as .zhtml

-d

Uncompresses the file

-c

Outputs the results to the standard output so they get sent to the client, rather than uncompressing in place

gzip is applied to the file contained in the environment variable PATH_TRANSLATED.

Finally, we have to turn our .htmls into .zhtmls. In ... /htdocs we have compressed and renamed:

It would be simpler to leave them as gzip does (with the extension .html.gz), but a file extension that maps to a MIME type cannot have a "." in it.[44]

[44]At least, not in a stock Apache. Of course, you could write a module to do it.

We also have index.html, which we want to convert, but we have to remember that it must call up the renamed catalogs with .zhtml extensions. Once that has been attended to, we can gzip it and rename it to index.zhtml.

We learned that Apache automatically serves up index.html if it is found in a directory. But this won't happen now, because we have index.zhtml. To get it to be produced as the index, we need the DirectoryIndex directive (see Chapter 7, "Indexing"), and it has to be applied to a specified directory:

<Directory /usr/www/site.filter/htdocs>
DirectoryIndex index.zhtml
</Directory>

Once all that is done, and ./go is run, the page looks just as it did before.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.