/* Program WebServer.java  is a silly web server */

import java.net.*;
import java.io.*;

public class WebServer {
    static final byte[] EOL = {(byte)'\r', (byte)'\n' };
    static final int 	HTTP_OK = 200;	

    public static void main(String[] args) throws IOException {

			// creates a new server type socket
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(8000);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 80.");
            System.exit(1);
        }


	// Wait for the client connection. The accept() method will
	// block until a connection comes to port 2000.
	// A new socket created accept() is used for transactions.
	// The original one i only for waiting for connections.
	// For more info read on unix TCP/IP sockets.

        	Socket clientSocket = null;
        	try {
            		clientSocket = serverSocket.accept();
        	} 
		catch (IOException e) {
            		System.err.println("Accept failed.");
            		System.exit(1);
		handleClient(clientSocket);
        	}
	
    }

    static 	final int BUF_SIZE = 512;
    static	byte[] 	buf = new byte[512];
    static	boolean 	doingGet;

    static void handleClient(Socket s) throws IOException{
      
	// create input and output streams

        InputStream is = new BufferedInputStream(s.getInputStream());
        PrintStream ps = new PrintStream(s.getOutputStream());
//        s.setSoTimeout(WebServer.timeout);
        s.setTcpNoDelay(true);

        for (int i = 0; i < BUF_SIZE; i++) buf[i] = 0;
        
        try {
            int nread = 0, r = 0;

        outerloop:
            while (nread < BUF_SIZE) {
                r = is.read(buf, nread, BUF_SIZE - nread);
                if (r == -1) return;
                int i = nread;
                nread += r;
                for (; i < nread; i++) {
                    if (buf[i] == (byte)'\n' || buf[i] == (byte)'\r') {
                        /* read one line */
                        break outerloop;
                    }
                }
            }
	}
	catch (SocketException e) { }

	System.out.println(buf);

            /* are we doing a GET or just a HEAD */
            boolean doingGet;
            /* beginning of file name */
            int index;
            if (buf[0] == (byte)'G' &&
                buf[1] == (byte)'E' &&
                buf[2] == (byte)'T' &&
                buf[3] == (byte)' ') {
                doingGet = true;
                index = 4;
            } else if (buf[0] == (byte)'H' &&
                       buf[1] == (byte)'E' &&
                       buf[2] == (byte)'A' &&
                       buf[3] == (byte)'D' &&
                       buf[4] == (byte)' ') {
                doingGet = false;
                index = 5;
            } else {
                /* we don't support this method */
                ps.print("HTTP/1.0 " + 405 +
                           " unsupported method type: ");
                ps.write(buf, 0, 5);
                ps.write(EOL);
                ps.flush();
                s.close();
                return;
	}
    }


    static void sendFile(PrintStream pw) throws IOException {
        pw.write(EOL);
        FileInputStream is = 
			new FileInputStream("sample.html");

	try {
	    byte[] buf = new byte[512];
            int count;
            while ((count = is.read(buf)) > 0) {
                pw.write(buf, 0, count);
		System.out.println("Sent bytes " + count);
		}
	}
	finally { is.close(); }
    }
}

