001         package com.croftsoft.core.net.http;
002    
003         import java.io.*;
004         import java.net.*;
005         import java.util.*;
006    
007         /*********************************************************************
008         * Nasty quick hack of a web server.
009         *
010         * <P>
011         *
012         * @author
013         *   <A HREF="http://www.alumni.caltech.edu/~croft">David W. Croft</A>
014         * @version
015         *   1999-12-24
016         *********************************************************************/
017    
018         public class  WebServer implements Runnable {
019         //////////////////////////////////////////////////////////////////////
020         //////////////////////////////////////////////////////////////////////
021    
022         private static final String  CRLF   = "\r\n";
023    
024         private static final String  SERVER = "FileServer/1.0";
025    
026    
027         private final String        root;
028    
029         private final int           port;
030    
031         private       boolean       running;
032    
033         private       ServerSocket  serverSocket;
034    
035         //////////////////////////////////////////////////////////////////////
036         //////////////////////////////////////////////////////////////////////
037    
038         public static void  main ( String [ ]  args )
039         //////////////////////////////////////////////////////////////////////
040         {
041           String  root = "./";
042    
043           int     port = 80;
044    
045           new Thread ( new WebServer ( root, port ) ).start ( );
046         }
047    
048         public void  shutdown ( )
049         //////////////////////////////////////////////////////////////////////
050         {
051           running = false;
052    
053           try { serverSocket.close ( ); } catch ( Exception  e ) { }
054         }
055    
056         public  WebServer ( String  root, int  port )
057         //////////////////////////////////////////////////////////////////////
058         {
059           this.root = root;
060    
061           this.port = port;
062         }
063    
064         public void  run ( )
065         //////////////////////////////////////////////////////////////////////
066         {
067           try
068           {
069             serverSocket = new ServerSocket ( port );
070    
071             running = true;
072    
073             while ( running )
074             {
075               Socket  clientSocket = serverSocket.accept ( );
076    
077               System.out.println ( clientSocket.getInetAddress ( ) );
078    
079               BufferedReader  in = new BufferedReader (
080                 new InputStreamReader ( clientSocket.getInputStream ( ) ) );
081    
082               String  line = in.readLine ( );
083    
084               StringTokenizer  tokenizer = new StringTokenizer ( line );
085    
086               tokenizer.nextToken ( );
087    
088               String  filename = tokenizer.nextToken ( );
089    
090               System.out.println ( filename );
091    
092               while ( ( line = in.readLine ( ) ) != null )
093                 System.out.println ( line );
094    
095    System.out.println ( "Done reading" );
096    
097               PrintWriter  out = new PrintWriter (
098                 new BufferedWriter ( new OutputStreamWriter (
099                 clientSocket.getOutputStream ( ) ) ) );
100    
101               File  file = new File ( root + filename );
102    
103               if ( !file.exists ( ) )
104               {
105    System.out.println ( "File does not exist." );
106                 out.print ( "HTTP/1.0 404 Not Found" + CRLF );
107                 out.print ( "Date: Mon, 09 Mar 1998 01:13:44 GMT" + CRLF );
108                 out.print ( "Server: " + SERVER + CRLF );
109                 out.print ( "Content-type: text/html" + CRLF );
110                 out.print ( CRLF );
111                 out.println ( "<HEAD><TITLE>404 Not Found</TITLE></HEAD>" );
112                 out.println ( "<BODY><H1>404 Not Found</H1>" );
113                 out.println ( "File \"" + filename + "\" was not found.<P>" );
114                 out.println ( "</BODY></HTML>" );
115               }
116               else
117               {
118                 out.print ( "HTTP/1.0 200 Document follows" + CRLF );
119                 out.print ( "Date: Mon, 09 Mar 1998 00:42:07 GMT" + CRLF );
120                 out.print ( "Server: " + SERVER + CRLF );
121                 out.print ( "Content-type: text/plain" + CRLF );
122                 out.print ( "Last-modified: Sun, 08 Mar 1998 23:41:34 GMT" + CRLF );
123    // file.lastModified ( )
124                 out.print ( "Content-length: " + file.length ( ) + CRLF );
125                 out.print ( CRLF );
126    
127                 BufferedInputStream  in_file
128                   = new BufferedInputStream ( new FileInputStream ( file ) );
129    
130                 int  c;
131    
132                 while ( ( c = in_file.read ( ) ) != -1 )
133                 {
134                   out.write ( c );
135                 }
136    
137                 in_file.close ( );
138               }
139    
140               out.flush ( );
141    
142               clientSocket.close ( );
143             }
144           }
145           catch ( Exception  ex )
146           {
147             ex.printStackTrace ( );
148           }
149    
150           try
151           {
152             serverSocket.close ( );
153           }
154           catch ( Exception  e )
155           {
156           }
157         }
158    
159         //////////////////////////////////////////////////////////////////////
160         //////////////////////////////////////////////////////////////////////
161         }