/*
=begin
**
** iSimpleHTTPServer.java
** 26/DEC/2007
** ETD-Software
**  - Daniel Martin Gomez <etd[-at-]nomejortu.com>
**
** Desc:
**  Dummy HTTP server, replies with whatever is requested.
**  Based on the SingleFileHTTPServer example of Java Network Programming:
**  http://www.oreilly.com/catalog/javanp2/chapter/ch11.html#53648
**
** Version:
**  v1.0 [26/Dec/2007]: first released
**
**  This program is free software: you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation, either version 2 of the License, or
**  (at your option) any later version.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program.  If not, see <http://www.gnu.org/licenses/>.

**
=end
*/

import java.net.*;
import java.io.*;
import java.util.*;
 
public class SimpleHTTPServer extends Thread {
 
  private byte[] content;
  private byte[] header;
  private int port = 80;
 
 public SimpleHTTPServer(byte[] data, String encoding, 
   String MIMEType, int port) throws UnsupportedEncodingException {
    this.port = port;
  }
 
  
  public void run(  ) {
  
    try {
      ServerSocket server = new ServerSocket(this.port); 
      System.out.println("Accepting connections on port " 
        + server.getLocalPort(  ));
      System.out.println("Data to be sent:");
      while (true) {
        
        Socket connection = null;
        try {
          connection = server.accept(  );
          OutputStream out = new BufferedOutputStream(
                                  connection.getOutputStream(  )
                                 );
          InputStream in   = new BufferedInputStream(
                                  connection.getInputStream(  )
                                 );
          // read the first line only; that's all we need
          StringBuffer req_head = new StringBuffer(80);
          StringBuffer req_body = new StringBuffer(80);
	  StringBuffer now_reading = req_head;
	  boolean in_head = true;
	  int linecount = 0;
	  int last_c = 0;

          while (true) {
            int c = in.read(  );
            if (c == '\n' || c == -1 || c == 0) {
	      linecount += 1;
	      if (in_head) {
	        if (linecount >5) {
		  in_head = false;
		  now_reading = req_body;
		  continue;
		}
	      } else {
	        if (c == 0) {
		  now_reading.append((char) c);
		  break;
		}
	      }
	    };
            now_reading.append((char) c);
            // If this is HTTP 1.0 or later send a MIME header
          }

          StringBuffer res_header = new StringBuffer("HTTP/1.0 200 OK\r\n");
          res_header.append( "Content-length: " + req_body.length() + "\r\n" );
	  res_header.append( "Content-type: text/plain\r\n\r\n");
	  StringBuffer res_body = req_body;

          out.write(res_header.toString().getBytes());
          out.write(res_body.toString().getBytes());
          out.flush(  );
        }  // end try
        catch (IOException e) {   
        }
        finally {
          if (connection != null) connection.close(  ); 
        }
        
      } // end while
    } // end try
    catch (IOException e) {
      System.err.println("Could not start server. Port Occupied");
    }
 
  } // end run
 
 
  public static void main(String[] args) throws Exception{
 
      String contentType = "text/plain";
      
      // set the port to listen on
      int port=2000;
      
      String encoding = "ASCII";
      if (args.length >= 2) encoding = args[2]; 
       
      Thread t = new SimpleHTTPServer(null, encoding,
       contentType, port);
      t.start(  );         
 
  }
 
}


