The manual page for the module mod_include is somewhat opaque, but on the other hand the range of possible actions is immense, so we will just give basic illustrations of each command in a number of text files in .../htdocs.
The Config file for this site (.../site.ssi) is:
User webuser Group webgroup ServerName www.butterthlies.com DocumentRoot /usr/www/site.ssi/htdocs AccessConfig /dev/null ResourceConfig /dev/null AddHandler server-parsed shtmlThe key line is:
AddHandler server-parsed shtmlshtml is our own invention and is found as the extension to the relevant files in .../htdocs. We could as well use brian or #dog_run as long as it appeared the same there, in the file with the relevant command, and in the configuration file. As usual, look in the error_log if things don't work. The error messages passed to the client are necessarily uninformative since they are probably being read three continents away where nothing useful can be done about them.
The trick is to insert special strings into our documents which then get picked up by Apache on their way through, recognized, and then replaced by dynamically written messages. As we will see, the strings have a deliberately unusual form so they won't get confused with more routine stuff. The syntax of a command is:
<!--#element attribute=value attribute=value ... -->The Apache manual tells us what the elements are.
config
errmsg
sizefmt
bytes for a count in bytes, or abbrev for a count in kilobytes or megabytes as appropriate.
timefmt
echo
include variables, defined below. If the variable is unset, it is printed as (none). Any dates printed are subject to the currently configured timefmt. The only attribute is:
var
exec
exec command executes a given shell command or CGI script. The IncludesNOEXEC option disables this command completely. The valid attribute is:
cgi
ScriptAlias or the ExecCGI option). The CGI script is given the PATH_INFO and query string (QUERY_STRING) of the original request from the client; these cannot be specified in the URL path. The include variables will be available to the script in addition to the standard CGI environment. If the script returns a Location header instead of output, this is translated into an HTML anchor. If Options NoExec is set in the Config file, this is turned off. The include virtual element should be used in preference to exec cgi.
cmd
include variables are available to the command. If Options NoExec is set in the Config file, this is turned off.
fsize
sizefmt format specification. The attributes are:
file
virtual
flastmod
timefmt format specification. The attributes are the same as for the fsize command.
include
Option IncludesNOEXEC set and including the document causes a program to be executed, it isn't included; this prevents the execution of CGI scripts. Otherwise, CGI scripts are invoked as normal using the complete URL given in the command, including any query string.
file
../, nor can it be an absolute path. The virtual attribute should always be used in preference to this one.
virtual
Options NoExec is set in the Config file. The reasoning is that clients can run the CGI anyway by using its URL as a hot link, or simply typing it into their browser, so no harm is done by using this method (unlike cmd or exec).
fsize command allows you to report the size of a file inside a document. The file size.shtml is:
<!--#config errmsg="Bungled again!"--> <!--#config sizefmt="bytes"--> The size of this file is <!--#fsize file="size.shtml"--> bytes. The size of another_file is <!--#fsize file="another_file"--> bytes.The first line provides an error message. The second line means that the size of any files is reported in bytes printed as a number, for instance: 89. Changing
bytes to abbrev gets the size in kilobytes, printed as 1k. The third line prints the size of size.shtml itself; the fourth line prints the size of another_file. You can't comment out lines with the # character since it just prints, and the following command is parsed straight away. config commands must come above commands that might want to use them.
You can replace the word file= in this script and those below, with virtual=, which gives %-encoded URL-path relative to the current document being parsed. If it does not begin with a slash, it is taken to be relative to the current document.
If you play with this stuff, you find that Apache is picky about the syntax. For instance, trailing spaces cause an error:
The size of this file is <!--#fsize file="size.shtml "--> bytes. The size of this file is Bungled again! bytesIf we had not used the
errmsg command, we would see:
...[an error occurred while processing this directive]...
flastmod. This gives the client an idea of the freshness of the data you are offering. The format of the output is controlled by the timefmt attribute of the config element. The rules for timefmt are the same as for the UNIX C function strftime().
% man strftimeshows them (we have not included it here because it may well vary from system to system). The file time.shtml gives an example:
<!--#config errmsg="Bungled again!"--> <!--#config timefmt="%A %B %C, the %jth day of the year, %S seconds since the Epoch"--> The mod time of this file is <!--#flastmod virtual="size.shtml"--> The mod time of another_file is <!--#flastmod virtual="another_file"-->This produces a response like:
The mod time of this file is Tuesday August 19, the 240th day of the year, 841162166 seconds since the Epoch The mod time of another_file is Friday August 19, the 229th day of the year, 840194838 seconds since the Epoch(The alert reader will notice a certain inconsistency about the dates. This is being investigated as a bug.)
include command:
<!--#config errmsg="Bungled again!"--> This is some text in which we want to include text from another file: << <!--#include virtual="another_file"--> >> That was it.This produces the response:
This is some text in which we want to include text from another file: << This the stuff in 'another_file'. >> That was it.
AddHandler, SetHandler, or ExecCGI. The command is exec {cmd cgi}:
<!--#config errmsg="Bungled again!"--> We're now going to execute the file 'do_this'. << <!--#exec cmd="rubbish/do_this"--> >> and now /usr/www/cgi-bin/mycgi.ok: << <!--#exec cmd="/usr/www/cgi-bin/mycgi.ok"--> >> That was it.The script do_this is simply
ls -l.
We are already familiar with mycgi.ok (see Chapter 4, Common Gateway Interface (CGI)). There are two attributes available to exec: cgi and cmd. cgi executes CGI scripts, and cmd uses the shell /bin/sh. For fine detail, see the manual, to which we cannot usefully add. This produces the response:
We're now going to execute the file 'do_this'. << >> and
now /usr/www/cgi-bin/mycgi.ok: <<
Content-type: text/html GATEWAY_INTERFACE=CGI/1.1 REMOTE_HOST=192.168.123.1
DOCUMENT_URI=/exec.shtml REMOTE_ADDR=192.168.123.1 QUERY_STRING=
HTTP_USER_AGENT=Mozilla/3.0Gold (Win95; I)
DOCUMENT_ROOT=/usr/www/site.ssi/htdocs HTTP_ACCEPT=image/gif,
image/x-xbitmap,
image/jpeg, image/pjpeg, */* SCRIPT_FILENAME=/usr/www/site.ssi/htdocs/
exec.shtml
LAST_MODIFIED=Thursday, 12-Sep-96 16:34:49 DOCUMENT_NAME=exec.shtml
HTTP_HOST=192.168.123.2 SERVER_SOFTWARE=Apache/1.2-dev
HTTP_CONNECTION=Keep-Alive HTTP_COOKIE=Apache=192380845288358461
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin HTTP_REFERER=
http://192.168.123.2/
SERVER_PROTOCOL=HTTP/1.0 DATE_GMT=Monday, 14-Oct-96 10:21:00
DOCUMENT_PATH_INFO= REQUEST_METHOD=GET SERVER_ADMIN=[no address
given] SERVER_PORT=80 USER_NAME=root SCRIPT_NAME=/exec.shtml
SERVER_NAME=www.butterthlies.com DATE_LOCAL=Monday, 14-Oct-96 10:21:00 >>
That was it.
echo a limited number of environment variables: DATE_GMT, DATE_LOCAL, DOCUMENT_NAME, DOCUMENT_URI, and LAST_MODIFIED. The file echo.shtml is:
Echoing the Document_URI <!--#echo var="DOCUMENT_URI"--> Echoing the DATE_GMT <!--#echo var="DATE_GMT"-->and produces the response:
Echoing the Document_URI /echo.shtml Echoing the DATE_GMT Saturday, 17-Aug-96 07:50:31
XSSI adds the following abilities to the standard SSI:
<tt><!--#flastmod file="$DOCUMENT_NAME" -->.
if, else, elif, and endif are used to include parts of the file based on conditional tests. For example, the $HTTP_USER_AGENT variable could be tested to see the type of browser and different HTML codes output depending on the browser capabilities.
Return to Apache: The Definitive Guide
O'Reilly Home | Catalog | Customer Service | About O'Reilly | Contact Us | Index