CIS 3120
Assignment 23
CGI Progamming -- Input From the Client


Input From the Client

There are two basic mechanisms for passing input from the client-- we will look at the simpler (though more restrictive) of the two. When the client specifes the URL of the server, it appends its input to the end of the url, following a "?". The input itself consists of name/value pairs in the form name=value pairs, each pair separated by an '&'. For example:

http://vc13.vc.panix.com/~weiss/cgi-bin/personal_hello?myname=Gerald&myaddress=BrooklynCollege
		
This form of transmission is call GET (It's in contrast to a more flexible and powerful-- but more complex-- form called POST which we will not go into.)

The portion of the url following the ? is known as the query (due to the fact that it often contains a query on the part of the client-- which is then processed by the CGI program and the answer passed back). The server, upon receiving the url, parses it into the host (which was used o get to the server's machine), the file path (which is used by the server to get to the proper cgi program, and the query. The server places the query in a environment variable QUERY_STRING (much like the PATH, CLASSPATH, or WINDOWS environment variables) which can then be accessed by the cgi program in a language-dependent fashion (which we discuss below for each of the languages).

Processing of the Input by the Client

Regardless of the number of input values coming in from the client, they are all placed into the query portion of the URL. Again, the inputs come in the form of property=value pairs. If there is more than one pair, they are separated by semicolons.

It is up to the client to parse the query string, breaking it up into individual pairs, and furthermore, spltting the name=value into the name and value components. Thus string processing is a key element of processing CGI input (which is one of the reasons Perl is so attractive, and furthermore C++/Java over C-- their string facilities are much more powerful).

Examples

As in Part 4, notice that the differences are in the languages string processing capabilities-- the basic structure is otherwise the same.

Shell

		
#!/bin/sh

echo "Content-Type: text/plain"
echo ""
echo Hello `echo "$QUERY_STRING" | sed -e 's/^.*=//'` from Unix Shell
echo Here is the full query string: $QUERY_STRING
	
Here is the link to the program on the server: http://vc13.vc.panix.com/~weiss/cgi-bin/personal?myname=Gerald

C

C uses the getenv function which accepts a string corresponding to the name of the environment variable and returns the value of that variable as a string:

		
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

main() {
        char *query_string = getenv("QUERY_STRING");

        char *name = strchr(query_string, '=')+1;

        printf("Content-Type: text/plain\n\n");
        printf("Hello %s from C\n", name);
        printf("Here is the entire query string: %s\n", query_string);
}
	
Here is the link to the program on the server:

http://vc13.vc.panix.com/~weiss/cgi-bin/personal-c?myname=Gerald

C++

C++ uses the getenv function as well:

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

main() {
        char *s = getenv("QUERY_STRING");
        string queryString = s;

        int where = queryString.find("=");
        string name = queryString.substr(where+1);

        cout << "Content-Type: text/plain" << endl << endl;
        cout << "Hello " << name << " from C++" << endl;
        cout << "here is the entire query string: " << s << endl;
}
		
Here is the link to the program on the server:

http://vc13.vc.panix.com/~weiss/cgi-bin/personal-cpp?myname=Gerald

Perl
#!/usr/bin/perl 

print "Content-Type: text/plain;charset=us-ascii\n\n";
($name, $value) = split(/=/, $ENV{'QUERY_STRING'});
print "Hello " . $value . " from Perl\n";
print "Here is the full query string: " . $ENV{'QUERY_STRING'} . "\n";
		
The split function parses the second parameter-- the input string (the QUERY_STRING environment variable in this case), using the first parameter (= here) as a delinmiter. The portions of the string before and after the = is then assigned to the left hand side elements ($name and $value in our case).

Here is the link to the program on the server:

http://vc13.vc.panix.com/~weiss/cgi-bin/personal.pl?myname=Gerald

Java

Java uses a (static) method getEnv belonging to the class System. This method is similar to C's getenv:

public class Personal {
        public static void main(String [] args) {
                String queryString = System.getenv("QUERY_STRING");
                int pos = queryString.indexOf("=");
                String name = queryString.substring(pos+1);     
                
                System.out.println("Content-Type: text/plain;charset=us-ascii\n\n");
                System.out.println("Hello " + name + " from Java");
                System.out.println("Here is the full query string: " + queryString);
        }
}
		
Here is the link to the program on the server:

http://vc13.vc.panix.com/~weiss/cgi-bin/personal-java?myname=Gerald

Again, a shell script to launch the Java interpreter is introduced:

#!/bin/sh

java Personal
		

Using HTML to Pass Input Data from the Client

We've seen how a query containing input data can be appended to the URL and sent to the server. More often though, we will use an HTML form to accept input from the user and ship it off to the client. THe basic mechanism is the same-- a query string is added to the URL, but in this situation, it is the browser that is creating and appending the query string via the information it obtains from the form.

We're going to create the most basic of HTML forms, containing nothing more than text boxes and a submit button. Here is a simple example:

<HTML>
	<form action="http://vc13.vc.panix.com/~weiss/cgi-bin/personal-cpp"/>
		Your name: <input type=text name=myname><BR>
      <input type=submit value="Submit to C++">
	</form>
</HTML>
	 

When the submit button is pressed, the data from the components used to build the query which is appended to the URL and the whole thing is shipped to the server.

Here is a working version of the above form:

Your name:

As this is an HTML file (rather than a CGI program) it should be placed in (or below) the public_html directory (and not in your cgi-bin subdirectory).

Multiple forms can appear in the same HTML file, and indeed the file personal.html in the code directory for this part has exactly that-- one form for each of the languages.

Your assignment for this Part

Part A

It's a bit more difficult to test your programs from the command line, since you have to create and initialize the QUERY_STRING environment variable.

Part B

Sample Code for Assignment 23