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

public class httpServer
{
   String DocumentRoot = "/web/edu/topics/java/server/web";
   int            port = 81;

   public static void main(String args[])
   {
      httpServer h = new httpServer();
      h.startIt();
   }

   public void startIt()
   {
     try{
       ServerSocket sso=new ServerSocket(port);
       System.out.println("Java httpd awaiting socket connections on port "+port);
       while(true){
         Socket so=sso.accept();
         new httpHandler(so,DocumentRoot).start();
       }
     }
     catch(Exception e){
       System.out.println("ServerSocket exception: "+e);
     }
   }
}


class httpHandler extends Thread {
 Socket so;
 String PrependingPath = null;

   httpHandler(Socket so, String DocumentRoot){
     this.so=so;
     this.PrependingPath = DocumentRoot;
   }

   public void run(){
     int linecount=0;
     try
     {
          DataInputStream dis=new DataInputStream(so.getInputStream());
          boolean post=false;
          String firstline=null;
          int readlen=0;

          while(true){
                String s=dis.readLine();
                if(firstline == null){
                  if(s!=null) firstline=s;
                }
                if(s!=null && s.startsWith("POST ")){
                  post=true;
                }
                else if(s!=null && s.startsWith("Content-Length:")){
                  StringTokenizer st=new StringTokenizer(s," ");
                  st.nextToken();  // ignore "Content-Length:" 
                  readlen=Integer.parseInt(st.nextToken().trim());
				  System.out.println("Content-Length="+readlen);
                }
                if ((s==null)||(s.length()==0)) break;
                linecount++;
          }

          if (linecount>0){
               DataOutputStream dos=null;
               FileInputStream fis=null;

              if(post){
                 byte data[]=new byte[readlen];
                 dis.readFully(data);
                 String text=new String(data);
                 System.out.println("Posted data, "+readlen+" bytes: "+text);
              }
     
              StringTokenizer  st = new StringTokenizer(firstline," ");
              String      request = st.nextToken();  
              String         name = st.nextToken().trim();
              String      version = st.nextToken();
              String responseCode = " 200 OK\n";
              byte          buf[] = null;
              try{
                  File f = new File(PrependingPath + name);
                  fis    = new FileInputStream(f);
                  buf    = new byte[(int)f.length()];
                  // load file data into byte buffer
                  fis.read(buf);  
              }catch(FileNotFoundException e1){
                  responseCode = " 404 Not Found\n"; 
                  buf = new byte[responseCode.length()];
                  responseCode.getBytes(0,responseCode.length(),buf,0);
              }
     

               String header = "HTTP/1.0"+ responseCode +
                               "Allow: GET, POST\n"+
                               "MIME-Version: 1.0\n"+
                               "Content-length: " + buf.length +
                               "\n\n";

              // send output back to browser
              dos=new DataOutputStream(so.getOutputStream());
              dos.writeBytes(header);
              dos.write(buf);
              dos.flush();

              //Print to server console
              System.out.print(request+"  "+name+"  "+version+"  "+responseCode);
         } 
         else 
         {
           System.out.println("Empty request! linecount less than or equal to 0");
         }
    }catch(Exception e){
        System.out.println("HTTPHandler err: "+e);
    }
  } //end_run_method
}   //end_class

