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=BrooklynCollegeThis 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).
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).
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_STRINGHere 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
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>
input
tag specifies and input component of the form
type
attribute of the input
tag specifies the
type of component
text
indicates a text box
submit
indicates a button which when pressed causes the
form to send its values to the server
name
attribute of the input
tag assigns a name to the
component. This is the name used in the name/value pair placed in the query and
eventually assigned to the QUERY_STRING environment variable.
value
attribute of the input
tag specifies an (initial)
value for the component
action
attribute in the form
tag specifies the URL that should be invoked when
the button is clicked
Here is a working version of the above form:
Your name:
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.
Part B