home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

HTML 4.0 Sourcebook
(Publisher: John Wiley & Sons, Inc.)
Author(s): Ian S. Graham
ISBN: 0471257249
Publication Date: 04/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next



Table 10.3 Common Gateway Environment Variables Derived from HTTP Request Header Fields

Variable Content
HTTP_ACCEPT A comma-separated list of all MIME types acceptable to the client, as indicated by the Accept headers sent to the server. An example is shown in Figure 10.8. Gateway programs can use this to determine which type of data to return to the client.
HTTP_COOKIE A semicolon-separated list of Netscape cookies. Netscape cookies are described later in this chapter.
HTTP_IF_MODIFIED_SINCE Gives the time and date, in the standard format described at the end of Chapter 9, of data held by the client. The gateway program can then decide if the server has data that are newer than this and if it should forward updated data or not.
HTTP_REFERER Contains the URL which referred the user to the current request; undefined if there is no Referer header field.
HTTP_USER_AGENT The contents of the User_Agent request header field. An example is shown at the bottom of Figure 10.8.

Some of the more common environment variables of this type are listed in Table 10.3. Note, in particular, the construction of the HTTP_ACCEPT header.

Server-Side Include Environment Variables

Several servers support a feature known as server-side includes, or SSI. SSI allows for parsable HTML documents: The documents contain special server directives that are processed by the server and are replaced by text from a second document, or by the output of a designated CGI program. SSI supports additional environment variables not mention in Tables 10.2 and 10.3. Chapter 11 contains a thorough discussion of SSI, while Tables 11.2 and 11.3 list the special environment variables provided by the SSI mechanism.


NOTE: Customized Environment Variables

Some servers permit local customization of CGI environment variables. You should check with your local server administrator to find out about any special-purpose CGI environment variables available at your site.


Example 23: HTML FORMs via a GET Request

This example examines the data passed by an HTML FORM to the program shown in Figure 10.9. The FORM used is the same one employed in Example 16 in Chapter 9, which uses the GET method to send the data to the program (the FORM document is shown in Figure 9.6 and, as rendered by a browser, in Figure 9.7). The Bourne-shell program in Figure 10.9 prints out the relevant environment variables and also reads in data from standard input (the read var command, on the fourth line from the bottom) and prints this input data to standard output.

The data sent to the server (and to the gateway program listed in Figure 10.9) by the form listed in Figure 9.6 are:

GET /cgi-bin/form1?srch=dogfish&srch_type=Exact+Match&srvr=Canada&srvr=Sweden
HTTP/1.0
Accept: text/plain
Accept: application/x-html
Accept: application/html
Accept: text/x-html
Accept: text/html
Accept: audio/*
.
.
Accept: text/x-setext
Accept: */*
User-Agent: NCSA Mosaic for the X Window System/2.4 libwww/2.12 modified

    [a blank line, containing only CRLF ]

The dots indicate Accept headers omitted to save space. You will note that these data were sent by the Mosaic for X-Windows browser.

Figure 10.10 shows the document returned by the script listed in Figure 10.9. You will note that there are no command-line arguments. In parsing the URL, the server detected “real” equals signs within the query string. This indicates a non-ISINDEX query, so the server does not create command-line arguments. The remaining quantities are obvious. The REQUEST_METHOD environment variable is set to GET, and the query string is placed in the QUERY_STRING environment variable. The CONTENT_TYPE and CONTENT_LENGTH variables are empty, since there is no data sent in a GET method, while the PATH_INFO and PATH_TRANSLATED variables are also empty, since there was no extra path information in the query.

Further processing requires more sophisticated programming tools to parse the QUERY_STRING and break it into its component parts. This is not difficult, recalling that the ampersand character divides the different segments; the equals sign relates FORM variable names to the assigned values; and spaces in the query strings are encoded as plus signs. Finally, you must decode all the special characters that may have been encoded using the URL encoding scheme discussed in Chapter 8. The perl code extract in Figure 10.11 illustrates how this decoding can be done.

Some useful collections of CGI utilities are listed in Chapter 11.


Figure 10.9 Test script form1 accessed by the HTML FORM in Figure 9.6. This script returns an HTML document listing the script command-line arguments (if there are any), the contents of all the environment variables, and any data read from standard input (if any exists).

#!/bin/sh
echo Content-TYPE:  text/html
echo
# is a FORMs test script -- it prints the environment variable
# contents generated by a FORM access to this script.
echo “<HEAD>”
echo “<TITLE>FORMs Test Page </TITLE>”
echo “</HEAD>”
echo “<P> Number of Command-line Arguments = $#. They are:”
for i in $*
do
   echo “ <code> $i </code> ”
done
echo “<h2> The Environment Variables </h2>”
echo “<pre>”
echo “SERVER_NAME = $SERVER_NAME”
echo “SERVER_PORT = $SERVER_PORT”
echo “REQUEST_METHOD = $REQUEST_METHOD”
echo “PATH_INFO = $PATH_INFO”
echo “PATH_TRANSLATED = $PATH_TRANSLATED”
echo “SCRIPT_NAME = $SCRIPT_NAME”
echo “QUERY_STRING = $QUERY_STRING”
echo “CONTENT_TYPE = $CONTENT_TYPE”
echo “CONTENT_LENGTH = $CONTENT_LENGTH”
echo
if [ -n “$CONTENT_LENGTH” ]; then # Read/print input data (if any).
      echo “<H2>data at Standard Input is:</h2>”
      echo “<PRE>”
      read “var”  # read data from standard input into “var”
      echo “$var” # print var to standard output
      echo “</PRE>”
else
      echo “<h2> No Data at standard input </h2>”
fi

echo “</BODY>


Figure 10.10  Data returned from the script shown in Figure 10.9 when accessed, using the GET method, by the FORM shown in Figure 9.6.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.