import java.util.*; import java.io.*; /** * cgi_lib.java
* *
* * Usage: This library of java functions, which I have encapsulated inside * a class called cgi_lib as class (static) member functions, * attempts to duplicate the standard PERL CGI library (cgi-lib.pl). * * You must invoke any Java program that uses this library from * within a UNIX script, Windows batch file or equivalent. As you * will see in the following example, all of the CGI environment * variables must be passed from the script into the Java application * using the -D option of the Java interpreter. This example * UNIX script uses the "main" routine of this class as a * CGI script: * *
* (testcgi.sh) * * #!/bin/sh * * java \ * -Dcgi.content_type=$CONTENT_TYPE \ * -Dcgi.content_length=$CONTENT_LENGTH \ * -Dcgi.request_method=$REQUEST_METHOD \ * -Dcgi.query_string=$QUERY_STRING \ * -Dcgi.server_name=$SERVER_NAME \ * -Dcgi.server_port=$SERVER_PORT \ * -Dcgi.script_name=$SCRIPT_NAME \ * -Dcgi.path_info=$PATH_INFO \ * cgi_lib * ** * Question and comments can be sent to pldurante@tasc.com.
* * @version 1.0 * @author Pat L. Durante * */ class cgi_lib { /** * * Parse the form data passed from the browser into * a Hashtable. The names of the input fields on the HTML form will * be used as the keys to the Hashtable returned. If you have a form * that contains an input field such as this,
* *
* <INPUT SIZE=40 TYPE="text" NAME="email" VALUE="pldurante@tasc.com"> ** * then after calling this method like this,
* *
* Hashtable form_data = cgi_lib.ReadParse(System.in); ** * you can access that email field as follows:
* *
* String email_addr = (String)form_data.get("email");
*
*
* @param inStream The input stream from which the form data can be read.
* (Only used if the form data was posted using the POST method. Usually,
* you will want to simply pass in System.in for this parameter.)
*
* @return The form data is parsed and returned in a Hashtable
* in which the keys represent the names of the input fields.
*
*/
public static Hashtable ReadParse(InputStream inStream)
{
Hashtable form_data = new Hashtable();
String inBuffer = "";
if (MethGet())
{
inBuffer = System.getProperty("cgi.query_string");
}
else
{
//
// TODO: I should probably use the cgi.content_length property when
// reading the input stream and read only that number of
// bytes. The code below does not use the content length
// passed in through the CGI API.
//
DataInput d = new DataInputStream(inStream);
String line;
try
{
while((line = d.readLine()) != null)
{
inBuffer = inBuffer + line;
}
}
catch (IOException ignored) { }
}
//
// Split the name value pairs at the ampersand (&)
//
StringTokenizer pair_tokenizer = new StringTokenizer(inBuffer,"&");
while (pair_tokenizer.hasMoreTokens())
{
String pair = urlDecode(pair_tokenizer.nextToken());
//
// Split into key and value
//
StringTokenizer keyval_tokenizer = new StringTokenizer(pair,"=");
String key = new String();
String value = new String();
if (keyval_tokenizer.hasMoreTokens())
key = keyval_tokenizer.nextToken();
else ; // ERROR - shouldn't ever occur
if (keyval_tokenizer.hasMoreTokens())
value = keyval_tokenizer.nextToken();
else ; // ERROR - shouldn't ever occur
//
// Add key and associated value into the form_data Hashtable
//
form_data.put(key,value);
}
return form_data;
}
/**
*
* URL decode a string.* * Data passed through the CGI API is URL encoded by the browser. * All spaces are turned into plus characters (+) and all "special" * characters are hex escaped into a %dd format (where dd is the hex * ASCII value that represents the original character). You probably * won't ever need to call this routine directly; it is used by the * ReadParse method to decode the form data. * * @param in The string you wish to decode. * * @return The decoded string. * */ public static String urlDecode(String in) { StringBuffer out = new StringBuffer(in.length()); int i = 0; int j = 0; while (i < in.length()) { char ch = in.charAt(i); i++; if (ch == '+') ch = ' '; else if (ch == '%') { ch = (char)Integer.parseInt(in.substring(i,i+2), 16); i+=2; } out.append(ch); j++; } return new String(out); } /** * * Generate a standard HTTP HTML header. * * @return A String containing the standard HTTP HTML header. * */ public static String Header() { return "Content-type: text/html\n\n"; } /** * * Generate some vanilla HTML that you usually * want to include at the top of any HTML page you generate. * * @param Title The title you want to put on the page. * * @return A String containing the top portion of an HTML file. * */ public static String HtmlTop(String Title) { String Top = new String(); Top = "\n"; Top+= "
\n"; Top+= "* * Please note that this routine references the member functions directly * (since they are in the same class), but you would have to * reference the member functions using the class name prefix to * use them in your own CGI application:
*
* System.out.println(cgi_lib.HtmlTop()); ** * @param args An array of Strings containing any command line * parameters supplied when this program in invoked. Any * command line parameters supplied are ignored by this routine. * */ public static void main( String args[] ) { // // This main program is simply used to test the functions in the // cgi_lib class. // // That said, you can use this main program as a test cgi script. All // it does is echo back the form inputs and enviroment information to // the browser. Use the testcgi UNIX script file to invoke it. You'll // notice that the script you use to invoke any Java application that // uses the cgi_lib functions MUST pass all the CGI enviroment variables // into it using the -D parameter. See testcgi for more details. // // // Print the required CGI header. // System.out.println(Header()); // // Create the Top of the returned HTML // page (the parameter becomes the title). // System.out.println(HtmlTop("Hello World")); System.out.println("