import java.util.*; import java.io.*; /** * Http.java

* * * Parse and return proper HTTP protocol field values such as * Accept, Content-length, Content-type, ...

* * * @version 1.0 aug-13-2002 * @author Xiannong Meng */ class Http { /* * Returns a hash table consisting of tokens in the header portion of the * response from a server.

* * @param inBuffer a buffer that contains the header of the response. * @return a hash table that contains pairs of name and value * * @author Xiannong Meng * */ public static Hashtable ParseToken(String inBuffer) { Hashtable form_data = new Hashtable(); // // Split the name value pairs at the new line // StringTokenizer pair_tokenizer = new StringTokenizer(inBuffer,"\n"); String pair = pair_tokenizer.nextToken(); // skip command post/get while (pair_tokenizer.hasMoreTokens()) { pair = 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.toLowerCase(),value.toLowerCase()); } return form_data; } /** * * Neatly format all of the form data using HTML.

* * @param form_data The Hashtable containing the form data which was * parsed using the ReadParse method. * * @return A String containing an HTML representation of all of the * form variables and the associated values. * * @author Xiannong Meng * */ public static String Variables(Hashtable form_data) { String returnString; returnString = "

\n"; for (Enumeration e = form_data.keys() ; e.hasMoreElements() ;) { String key = (String)e.nextElement(); String value = (String)form_data.get(key); returnString += "
" + key + "
:" + value + ":
\n"; } returnString += "
\n"; return returnString; } }