diff --git a/program1/SimpleWebServer/src/WebServer.java b/program1/SimpleWebServer/src/WebServer.java new file mode 100644 index 0000000..76569fa --- /dev/null +++ b/program1/SimpleWebServer/src/WebServer.java @@ -0,0 +1,97 @@ +/** +* A simple web server: it creates a new WebWorker for each new client +* connection, so all the WebServer object does is listen on the port +* for incoming client connection requests. +* +* This class contains the application "main()" (see below). At startup, +* main() creates an object of this class (WebServer) and invokes its +* start() method. Since servers run continually, the start() method +* never returns. It uses socket programming to listen for client network +* connection requests. When one happens, it creates a new object of +* the WebWorker class and hands that client connection off to the WebWorker +* object. The WebServer object then just keeps listening for new client +* connections. See the WebWorker source for more information about it. +* +**/ + +import java.net.*; + +public class WebServer +{ + +private ServerSocket socket; +private boolean running; + +/** +* Constructor +**/ +private WebServer() +{ + running = false; +} + +/** +* Web server starting point. This method does not return until +* the server is finished, so perhaps it should be named "runServer" +* or something like that. +* @param port is the TCP port number to accept connections on +**/ +private boolean start(int port) +{ + Socket workerSocket; + WebWorker worker; + try { + socket = new ServerSocket(port); + } catch (Exception e) { + System.err.println("Error binding to port "+port+": "+e); + return false; + } + while (true) { + try { + // wait and listen for new client connection + workerSocket = socket.accept(); + } catch (Exception e) { + System.err.println("No longer accepting: "+e); + break; + } + // have new client connection, so fire off a worker on it + worker = new WebWorker(workerSocket); + new Thread(worker).start(); + } + return true; +} // end start + +/** +* Does not do anything, since start() never returns. +**/ +private boolean stop() +{ + return true; +} + +/** +* Application main: process command line and start web server; default +* port number is 8080 if not given on command line. +**/ +public static void main(String args[]) +{ + int port = 8080; + if (args.length > 1) { + System.err.println("Usage: java Webserver "); + return; + } else if (args.length == 1) { + try { + port = Integer.parseInt(args[0]); + } catch (Exception e) { + System.err.println("Argument must be an int ("+e+")"); + return; + } + } + WebServer server = new WebServer(); + if (!server.start(port)) { + System.err.println("Execution failed!"); + } +} // end main + +} // end class + diff --git a/program1/SimpleWebServer/src/WebWorker.java b/program1/SimpleWebServer/src/WebWorker.java new file mode 100644 index 0000000..af65eaf --- /dev/null +++ b/program1/SimpleWebServer/src/WebWorker.java @@ -0,0 +1,124 @@ +/** +* Web worker: an object of this class executes in its own new thread +* to receive and respond to a single HTTP request. After the constructor +* the object executes on its "run" method, and leaves when it is done. +* +* One WebWorker object is only responsible for one client connection. +* This code uses Java threads to parallelize the handling of clients: +* each WebWorker runs in its own thread. This means that you can essentially +* just think about what is happening on one client at a time, ignoring +* the fact that the entirety of the webserver execution might be handling +* other clients, too. +* +* This WebWorker class (i.e., an object of this class) is where all the +* client interaction is done. The "run()" method is the beginning -- think +* of it as the "main()" for a client interaction. It does three things in +* a row, invoking three methods in this class: it reads the incoming HTTP +* request; it writes out an HTTP header to begin its response, and then it +* writes out some HTML content for the response content. HTTP requests and +* responses are just lines of text (in a very particular format). +* +**/ + +import java.net.Socket; +import java.lang.Runnable; +import java.io.*; +import java.util.Date; +import java.text.DateFormat; +import java.util.TimeZone; + +public class WebWorker implements Runnable +{ + +private Socket socket; + +/** +* Constructor: must have a valid open socket +**/ +public WebWorker(Socket s) +{ + socket = s; +} + +/** +* Worker thread starting point. Each worker handles just one HTTP +* request and then returns, which destroys the thread. This method +* assumes that whoever created the worker created it with a valid +* open socket object. +**/ +public void run() +{ + System.err.println("Handling connection..."); + try { + InputStream is = socket.getInputStream(); + OutputStream os = socket.getOutputStream(); + readHTTPRequest(is); + writeHTTPHeader(os,"text/html"); + writeContent(os); + os.flush(); + socket.close(); + } catch (Exception e) { + System.err.println("Output error: "+e); + } + System.err.println("Done handling connection."); + return; +} + +/** +* Read the HTTP request header. +**/ +private void readHTTPRequest(InputStream is) +{ + String line; + BufferedReader r = new BufferedReader(new InputStreamReader(is)); + while (true) { + try { + while (!r.ready()) Thread.sleep(1); + line = r.readLine(); + System.err.println("Request line: ("+line+")"); + if (line.length()==0) break; + } catch (Exception e) { + System.err.println("Request error: "+e); + break; + } + } + return; +} + +/** +* Write the HTTP header lines to the client network connection. +* @param os is the OutputStream object to write to +* @param contentType is the string MIME content type (e.g. "text/html") +**/ +private void writeHTTPHeader(OutputStream os, String contentType) throws Exception +{ + Date d = new Date(); + DateFormat df = DateFormat.getDateTimeInstance(); + df.setTimeZone(TimeZone.getTimeZone("GMT")); + os.write("HTTP/1.1 200 OK\n".getBytes()); + os.write("Date: ".getBytes()); + os.write((df.format(d)).getBytes()); + os.write("\n".getBytes()); + os.write("Server: Jon's very own server\n".getBytes()); + //os.write("Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\n".getBytes()); + //os.write("Content-Length: 438\n".getBytes()); + os.write("Connection: close\n".getBytes()); + os.write("Content-Type: ".getBytes()); + os.write(contentType.getBytes()); + os.write("\n\n".getBytes()); // HTTP header ends with 2 newlines + return; +} + +/** +* Write the data content to the client network connection. This MUST +* be done after the HTTP header has been written out. +* @param os is the OutputStream object to write to +**/ +private void writeContent(OutputStream os) throws Exception +{ + os.write("\n".getBytes()); + os.write("

My web server works!

\n".getBytes()); + os.write("\n".getBytes()); +} + +} // end class