/*
  Tyba HTTP Server, v0.1  
  By Carlos Varela (cavarela@uniandes.edu.co, cvarela@isr.co.jp)
  March, 1996

  v0.2  July 8, 1996  cvarela :  Restructured server classes.
*/

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

class HTRequest {

    private static final int NONE = -1;
    private static final int HEAD = 0;
    private static final int GET = 1;
    private static final int POST = 2;
    private static final int PUT = 3;

    int method = NONE;
    String url;
    String protocolVersion;
    int noheaders = 0;
    HTHeader headers[] = new HTHeader[255];
    InputStream body;


    HTRequest(InputStream inputStream){
      try {
        DataInputStream is = new DataInputStream(
                             new BufferedInputStream(inputStream));
        String inputLine = is.readLine();

        if (inputLine == null) return;  /** Empty request **/

        StringTokenizer st = new StringTokenizer(inputLine);
        String methodString = new String(st.nextToken());
     
        if (methodString.equals("GET"))
            this.method = GET;
        else if (methodString.equals("HEAD"))
            this.method = HEAD;
        else if (methodString.equals("POST"))
            this.method = POST;
        else if (methodString.equals("PUT"))
            this.method = PUT;

        this.url = st.nextToken();
        this.protocolVersion = st.nextToken();

        while ((inputLine = is.readLine()) != null) {
             if (inputLine.length() == 0)  // No more headers
                 break;
             headers[noheaders++] = new HTHeader(inputLine);
        }

        this.body = is;
      } catch (IOException e){
        System.err.println("Tyba:  Wrong request: " + e);
      }  
    }


    HTResponse process(){

        if (method == GET) {
          try {
            StringBuffer filename = new StringBuffer(
                                    HTServer.getDocumentRoot());
            String fn;
            filename.append(url);
            if (filename.charAt(filename.length()-1) == '/') {
        	filename.append("index.html");
            }
            fn = filename.toString();
            FileInputStream fis = new FileInputStream(fn);
            int c;

            /* Better to read more bytes and catch EOF Exception... */
            /* And maybe better to give Stream as opposed to String 
               to HTResponse */
            StringBuffer theOutputBuffer = new StringBuffer();
            while ((c = fis.read()) != -1) {
		theOutputBuffer.append((char)c);
            }
            HTResponse theHTResponse = 
                new HTResponse(theOutputBuffer.toString());
            if (fn.endsWith(".html")) {
                theHTResponse.addHeader("Content-type", "text/html");
            } else if (fn.endsWith(".gif")) {
                theHTResponse.addHeader("Content-type", "image/gif");
            }
            return theHTResponse;
	  } catch (FileNotFoundException e) {
            System.err.println("Tyba: " + e);
            return new HTResponse(404, "Tyba:  File Not Found");
          } catch (IOException e) {
            System.err.println("Tyba: " + e);
            return null;
          }
        }
        else // Not implemented
          return null;
    }

}









