import java.net.*;
import java.io.*;
import java.util.StringTokenizer;

/*
 * ParseGET - GET line parser
 * 
 * ParseGET receives the first line of a client's request. If the line 
 * is a GET request, ParseGET reformats it for the real server and 
 * provides connection information (hostname, pathname, port number).
 *
 */

class ParseGET {
	String firstLine;		/* First line in the client's request */
	String firstWord;		/* First word in firstLine */
        boolean isGET = false;          /* Is this a GET request? */
        StringTokenizer st;             /* Used to extract tokens from the GET request */
        URL serverURL;                  /* Stores the URL (http://hostname/pathname) from the GET request */
	String hostname, filename;	/* hostname and filename from the URL */
	int portNumber;			/* Port number provided in the URL (default 80) */
	String newFirstLine;		/* GET line reformatted for the end server */

	public ParseGET(String s){
		firstLine = s;
		st = new StringTokenizer(firstLine);
	try{
		if(st.hasMoreTokens()){
			firstWord = st.nextToken();
		}
		if (firstWord.equals("GET")){
			isGET = true;
			serverURL = new URL(st.nextToken());
			hostname = serverURL.getHost();
			filename = serverURL.getFile();
			/* if (filename.equals("/")){
			*	filename = "/index.html";
			* }
			*/ 
			if (filename.equals("")){
                               filename = "/";
                        }
			System.out.println("filename = " + filename);
			portNumber = serverURL.getPort();
			if (portNumber == -1){
				portNumber = 80;
			}
			/* newFirstLine = ("GET " + filename + " " + st.nextToken() + " \r\n"); */
                        newFirstLine = ("GET " + filename + " " + "HTTP/1.0" + " \r\n");
		}
		else{
			System.out.println("Not a GET request. Ignoring request:");
			System.out.println(firstLine);
		}
	}
	catch (MalformedURLException e){}
	}
}