Click Here!
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


Chapters 2 and 6 discussed how ISINDEX queries send data to the server by appending the query data to the URL of the document being viewed and then accessing the newly composed URL. An example of such a URL is:

<http://some.where.edu/cgi-bin/srch_program?string1+string2>

When this information reaches the server, and if it is an ISINDEX query, the server decodes the URL (converts plus (+) signs back into spaces, and converts URL character encodings back into the correct 8-bit characters), uses the space characters to break the query string into individual terms, and then passes these terms to the indicated gateway program as command-line arguments.

Detecting ISINDEX Queries

How does the server know if a GET method request comes from an ISINDEX query? The answer is that an ISINDEX query string never contains unencoded equals signs (=). As pointed out in Chapter 8 (which gave the details of the URL encoding mechanism) and in Example 16 of Chapter 9, FORM data are encoded as a collection of strings of the form name= value, which always contains at least one unencoded equals sign (any equals signs originally present in the name or value strings are encoded as %3d). Therefore, the presence of a “real” equals sign in the query string means that the data came from a FORM and not from an ISINDEX.

Step 1. First Access of the URL

In this example, we assume that the script srch-example is initially accessed via the URL:

<http://leonardo.utirc.utoronto.ca:8080/cgi-bin/srch-example>

Note that there is no query information attached to the URL; this is an important factor in the initial behavior of the script.

Line 1 tells the computer to interpret this script using the /bin/sh program, which is the traditional location and name for the Bourne shell. The lines 2 and 3 echo information to standard output (echo is the Bourne-shell command that prints to standard output). Standard output is sent back to the server and, from there, back to the client.

Server Directives in Gateway Programs

The second line prints an HTTP server directive, which gives the server information about the data to come. This is absolutely necessary, as the server has no other way of knowing what type of data the program will return. This line prints the header


Figure 10.2 Bourne-shell script CGI gateway program srch-example.

01 #!/bin/sh
02 echo Content-TYPE: text/html
03 echo
04
05 if [ $# = 0 ]        # is the number of arguments == 0 ?
06 then                 # do this part if there are NO arguments  48;
07    echo “<HEAD>”
08    echo “<TITLE>Local Phonebook Search</TITLE>”
09    echo “<ISINDEX>”
10    echo “</HEAD>”
11    echo “<BODY>”
12    echo “<H1>Local Phonebook Search</H1>”
13    echo “Enter your search in the search field.<P>”
14    echo “This is a case-insensitive substring search: thus”
15    echo “searching for ‘ian’ will find ‘Ian’ and Adriana’.”
16    echo “</BODY>”
17 else                     # this part if there ARE arguments
18    echo “<HEAD>”
19    echo “<TITLE>Result of search for \”$*\“.</TITLE>”
20    echo “</HEAD>”
21    echo “<BODY>”
22    echo “<H1>Result of search for \”$*\“.</H1>”
23    echo “<PRE>”
24    for i in $*
25    do
26         grep -i $i /vast/igraham/Personnel
27    done
28    echo “</PRE>”
39    echo “</BODY>”
40 fi
Content-TYPE: text/html

to tell the server that the data to follow is an HTML document. The next line prints a blank line. This denotes the end of the header—subsequent output is the actual data being returned.

Several other server directives are possible—they are summarized in Example 21.

Line 5 tests the number of command-line arguments. In this case, there was no query string, so there are no command-line arguments and the first branch of the if is executed. This branch prints, to standard output, a simple HTML document explaining the nature of the search; this is shown in Figure 10.3. This document contains an ISINDEX element, to tell the browser to prompt for search information—this gives rise to the query box in Figure 10.3. I have typed the names ian and bradley into this box (the author always likes to look for his own name), separated by a single space. These are the names that will be used in the search.

Step 2: Second Access of the URL

Submitting this ISINDEX search information accesses the same URL, but appends the names ian and bradley to the URL as query strings. Thus, in this second phase, the accessed URL is

<http://leonardo.utirc.utoronto.ca:8080/cgi-bin/srch-example?ian+bradley>

where the space between ian and bradley has been encoded as a plus sign, as required by the URL query string encoding scheme described in Chapter 8.

When the server receives this URL, it parses the query string and finds that there are no unencoded equals signs, so it knows that this is an ISINDEX query. It therefore takes the query string and breaks it into individual strings, using the plus signs to mark the string separators. This yields the two strings ian and bradley. The server next launches the gateway program srch-example, using the names ian and bradley as command-line arguments. The equivalent command, typed by hand, would be:

srch-example ian bradley

Figure 10.4 shows the results of this second access to the Bourne-shell program; by following Figure 10.2, you can see how it was generated. As before, the first two lines print the MIME content-type of the message and the blank line separating the HTTP headers from the data. At line 5, the program checks for command-line arguments. This time there are arguments, so the second branch of the script is executed, starting at line 18. This section prints a different HTML document, this time including output from the program grep. Lines 24 through 27 loop the variable i through all the command-line arguments. The content of the variable i (denoted by $i) is used as an argument to the program grep, which scans the file /vast/igraham/Personnel for names matching the pattern given by $i. Grep prints the matches to standard output. The result of the searches is shown in Figure 10.4. Note that there is no query box, as the second branch of the script in Figure 10.2 did not return an ISINDEX element.


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.