package client_server;

/**
 * The Request class uses the StringTokenizer created in the SimpleWebServer class to
 * create a Properties list of the parameters encoded in the URL string.  
*/
import java.util.*;
import java.awt.*;

public class Request
{
	private Vector parameters;
	private TextArea area;
	
	/* Use the tokenizer to get the name and value parameters and 
	 * store them in the Properties list.  
	*/
	public Request (StringTokenizer tokenizer, TextArea area)
	{
		String name, value;
		this.area = area;
		int size = 0;
		parameters = new Vector ();
		while (tokenizer.hasMoreTokens())
		{
			name = tokenizer.nextToken ();
			value = tokenizer.nextToken ();
			area.append (name + " " + value + "\n");
			if (!name.equals ("HTTP"))
			{
				value = replaceHexCode (value);
				Param param = new Param (name, value);
				parameters.addElement (param);
				size++;
			}
		} 
	} // constructor 
	
	/* Some characters are sent in hex by the URL string.  They are preceded by a '%' sign.
	 * The following method replaces them with the corresponding characters.  It also replaces
	 * the '+' sign in the string with a space.
	*/
	private String replaceHexCode (String value)
	{
		value = value.replace ('+', ' ');	
		int index = value.indexOf ('%');
		while (index >= 0)
		{	try 
			{
				// Get the hex code and covert it to decimal.
				String hex = value.substring (index+1, index+3);
				int decimal = Integer.parseInt (hex, 16);
			
				/* Get the character with the decimal code, change the characters '<' to &lt,
				and '>' to &gt.  Include the resulting string in the value parameter. */
				char ch = (char) decimal;
				String code;
				if (ch == '<') code = "&lt";
				else if (ch == '>') code = "&gt";
				else code = String.valueOf (ch);
				value = value.substring (0, index) + code + value.substring (index+3);
			} catch (NumberFormatException e) {}
			index = value.indexOf ('%', index+1);
		}
		return value;
	} // replaceHexCode
	
	// Given a name, return the corresponding value.
	public String getParameter (String name)
	{
		int index = 0;
		boolean found = false;
		Param param = null;
		while (index < parameters.size () && !found)
		{
			param = (Param) parameters.elementAt (index);
			if (param.getName ().equals (name)) found = true;
			else index ++;
		}
		if (found) return param.getValue ();
		else return null;
		
	} // getParameter
	
	
	// Return an array containing just the parameter values, not the names.
	public String [] getParameterValues (String name)
	{
		String [] values = new String [10];
		int index = 0;
		Param param;
		for (int count = 0; count < parameters.size (); count ++)
		{
			param = (Param) parameters.elementAt (count);
			if (param.getName ().equals (name))
				values [index] = param.getValue ();
			index++;
		}
		return values;
	} // getParameterValues

} // class Request
			
			
class Param
{
	private String name, value;
	
	Param (String n, String v){name = n; value = v;} //constructor
	
	protected String getName(){return name;}
	protected String getValue(){return value;}
}//Param
