// File: WebServer.java
// T Balls : Oct 1999
// Create a basic WebServer
// can serve html, jpg and gif files
// any other file that exists is sent as plain text
import java.net.*;
import java.io.*;
import java.util.*;
public class WebServer
{ public static void main( String[] args )
{ if( args.length == 1 )
{ new WebServer( args[0] ).run();
}
else
{ new WebServer( "44444" ).run();
}
}
private ServerSocket ss = null;
public WebServer( 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;
while( true )
{ try
{ sock = ss.accept();
}
catch( IOException ioe )
{ System.out.println( "Accept failed" );
System.exit( -1 );
}
handleRequest( sock );
try
{ sock.close();
}
catch( IOException ioe )
{ System.out.println( "Error closing socket" );
}
}
}
public void handleRequest( Socket s )
{ BufferedReader in = null;
OutputStream os = null;
try
{ in = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
os = s.getOutputStream();
}
catch( IOException ioe )
{ System.out.println( "Can't get streams" );
System.exit( -1 );
}
String firstLine = "";
String line = "";
try
{ firstLine = in.readLine();
System.out.println( "*** " + firstLine );
line = in.readLine();
while( !line.equals( "" ) )
{ System.out.println( line );
line = in.readLine();
}
}
catch( IOException ioe )
{ System.out.println( "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" );
}
}
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 );
int i = is.read();
while( i != -1 )
{ os.write( i );
i = is.read();
}
os.flush();
}
catch( IOException ioe )
{ System.out.println( "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();
}
} |