// File: ThreadedWebServer.java
// T Balls : Oct 1999
// The basic web server program, with local buffering
// is modified to allow the creation of objects to handle
// the connections.
import java.net.*;
import java.io.*;
import java.util.*;
public class ThreadedWebServer
{ public static void main( String[] args )
{ if( args.length == 1 )
{ new ThreadedWebServer( args[0] ).run();
}
else
{ new ThreadedWebServer( "44444" ).run();
}
}
private ServerSocket ss = null;
public ThreadedWebServer( String portNum )
{ int port = new Integer( portNum ).intValue();
try
{ ss = new ServerSocket( port );
}
catch( IOException ioe )
{ System.out.println( "Can't create server socket on port : " + port );
System.exit( -1 );
}
}
public void run()
{ Socket sock = null;
int handlerCount = 0;
while( true )
{ try
{ sock = ss.accept();
}
catch( IOException ioe )
{ System.out.println( "Accept failed" );
System.exit( -1 );
}
handlerCount++;
new ConnectionHandler( sock, handlerCount ).start();
}
}
}
class ConnectionHandler extends Thread
{ public ConnectionHandler( Socket s, int id )
{ this.s = s;
this.id = id;
}
private Socket s;
private int id;
public void run()
{ BufferedReader in = null;
OutputStream os = null;
try
{ in = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
os = s.getOutputStream();
}
catch( IOException ioe )
{ System.out.println( "#" + id +" Can't get streams" );
System.exit( -1 );
}
String firstLine = "";
String line = "";
try
{ firstLine = in.readLine();
System.out.println( "\n\n#" + id + " *** " + firstLine );
line = in.readLine();
while( !line.equals( "" ) )
{ System.out.println( "#" + id + " " + line );
line = in.readLine();
}
}
catch( IOException ioe )
{ System.out.println( "#" + id + " Error reading data from client" );
System.exit( -1 );
}
StringTokenizer st = new StringTokenizer( firstLine );
String command = st.nextToken();
String filename = st.nextToken();
if( command.equals( "GET" ) )
{ sendFile( os, filename );
}
else
{ sendError( os, 501, "NOT IMPLEMENTED" );
}
// This object should close the socket when it's finished with it
try
{ s.close();
}
catch( IOException ioe )
{ System.out.println( "Error closing socket" );
}
}
public void sendFile( OutputStream os, String filename )
{ filename = filename.substring( 1, filename.length() );
File f = new File( filename );
if( !f.exists() )
{ sendError( os, 404, "NOT FOUND" );
}
else
{ String extension = filename.substring( filename.lastIndexOf( "." )+1,
filename.length() );
// Send header
PrintStream ps = new PrintStream( os );
ps.println( "HTTP/1.0 200 OK" );
ps.print( "Content-Type: " );
if( extension.equals( "html" ) )
{ ps.println( "text/html" );
}
else if( extension.equals( "gif" ) )
{ ps.println( "image/gif" );
}
else if( extension.equals( "jpeg" ) || extension.equals( "jpg" ) )
{ ps.println( "image/jpeg" );
}
else
{ ps.println( "text/plain" );
}
ps.println( "Connection: close" );
ps.println();
ps.flush();
// send file body
try
{ InputStream is = new FileInputStream( filename );
// Faster read/write than in first example
// using a local buffer
byte[] buffer = new byte[1024];
int bytesRead;
while( (bytesRead = is.read( buffer )) != -1 )
{ os.write( buffer, 0, bytesRead );
System.out.print( "#" + id + " " );
}
os.flush();
}
catch( IOException ioe )
{ System.out.println( "#" + id + " Error sending file body " );
}
}
}
public void sendError( OutputStream os, int errorNo, String errorText )
{ PrintStream ps = new PrintStream( os );
ps.println( "HTTP/1.0 " + errorNo + " " + errorText );
ps.println( "Content-Type: text/html" );
ps.println( "Connection: close" );
ps.println();
ps.println( "<HTML><HEAD><TITLE>Error " + errorNo + "</TITLE>" );
ps.println( "<BODY><H2>HTTP Error " + errorNo + " " + errorText + "</H2>" );
ps.println( "</BODY></HTML>" );
ps.flush();
}
}
|