diff --git a/Questions/p1Questions.txt b/Questions/p1Questions.txt index e69de29..17eed1d 100644 --- a/Questions/p1Questions.txt +++ b/Questions/p1Questions.txt @@ -0,0 +1,11 @@ +1. Estimate the time it will take you to do the assignment, to within a quarter-hour: _____________ +A: 2 Hours + +2. Estimate the time it took you to complete the assignment, broken down as: + + Thought/Design: 1 Hour + Programming: 2 Hours + Debugging: 30 minutes + TOTAL: 3 Hours and a Half + +3. To within what percent accuracy was your original estimate: 42% error diff --git a/SimpleWebServer/SimpleWebServer.iml b/SimpleWebServer/SimpleWebServer.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/SimpleWebServer/SimpleWebServer.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/SimpleWebServer/android-chrome-192x192.png b/SimpleWebServer/android-chrome-192x192.png new file mode 100644 index 0000000..15189de Binary files /dev/null and b/SimpleWebServer/android-chrome-192x192.png differ diff --git a/SimpleWebServer/android-chrome-512x512.png b/SimpleWebServer/android-chrome-512x512.png new file mode 100644 index 0000000..d17891d Binary files /dev/null and b/SimpleWebServer/android-chrome-512x512.png differ diff --git a/SimpleWebServer/apple-touch-icon.png b/SimpleWebServer/apple-touch-icon.png new file mode 100644 index 0000000..1312202 Binary files /dev/null and b/SimpleWebServer/apple-touch-icon.png differ diff --git a/SimpleWebServer/build.xml b/SimpleWebServer/build.xml index e69de29..05ea27b 100644 --- a/SimpleWebServer/build.xml +++ b/SimpleWebServer/build.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SimpleWebServer/favicon-16x16.png b/SimpleWebServer/favicon-16x16.png new file mode 100644 index 0000000..c438dbf Binary files /dev/null and b/SimpleWebServer/favicon-16x16.png differ diff --git a/SimpleWebServer/favicon-32x32.png b/SimpleWebServer/favicon-32x32.png new file mode 100644 index 0000000..4c7d6a4 Binary files /dev/null and b/SimpleWebServer/favicon-32x32.png differ diff --git a/SimpleWebServer/favicon.ico b/SimpleWebServer/favicon.ico new file mode 100644 index 0000000..f7a9787 Binary files /dev/null and b/SimpleWebServer/favicon.ico differ diff --git a/SimpleWebServer/index.html b/SimpleWebServer/index.html new file mode 100644 index 0000000..3bc5cf1 --- /dev/null +++ b/SimpleWebServer/index.html @@ -0,0 +1,7 @@ + + + + +

Hello world, the current date is: . The server is: .

+ + \ No newline at end of file diff --git a/SimpleWebServer/site.webmanifest b/SimpleWebServer/site.webmanifest new file mode 100644 index 0000000..45dc8a2 --- /dev/null +++ b/SimpleWebServer/site.webmanifest @@ -0,0 +1 @@ +{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"} \ No newline at end of file diff --git a/SimpleWebServer/src/edu/nmsu/cs/webserver/WebWorker.java b/SimpleWebServer/src/edu/nmsu/cs/webserver/WebWorker.java index 963c5c8..9898ce6 100644 --- a/SimpleWebServer/src/edu/nmsu/cs/webserver/WebWorker.java +++ b/SimpleWebServer/src/edu/nmsu/cs/webserver/WebWorker.java @@ -4,137 +4,167 @@ package edu.nmsu.cs.webserver; * 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). - * - * @author Jon Cook, Ph.D. * + * @author Jon Cook, Ph.D. **/ -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; +import java.io.*; import java.net.Socket; import java.text.DateFormat; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.TimeZone; -public class WebWorker implements Runnable -{ +public class WebWorker implements Runnable { - private Socket socket; + private Socket socket; - /** - * Constructor: must have a valid open socket - **/ - public WebWorker(Socket s) - { - socket = s; - } + /** + * 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; - } + /** + * 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(); + String file_path = readHTTPRequest(is); + String http_code = writeHTTPHeader(os, "text/html", file_path); + writeContent(os, http_code, file_path); + 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; - } + /** + * Read the HTTP request header. + **/ + private String readHTTPRequest(InputStream is) { + String file_path = "./index.html"; + String line; + BufferedReader r = new BufferedReader(new InputStreamReader(is)); + while (true) { + try { + while (!r.ready()) + Thread.sleep(1); + line = r.readLine(); + String[] line_items = line.split(" "); - /** - * 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; - } + if (line_items[0].equals("GET")) { + System.out.println("found a get request"); + file_path = line_items[1]; + } - /** - * 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()); - } + System.err.println("Request line: (" + line + ")"); + if (line.length() == 0) + break; + } catch (Exception e) { + System.err.println("Request error: " + e); + break; + } + } + return file_path; + } + /** + * 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 String writeHTTPHeader(OutputStream os, String contentType, String file_path) throws Exception { + File file = new File("./" + file_path); + String http_code = "200 OK"; + + if (!file.isDirectory() && file.exists()) { + System.out.println("it exists"); + http_code = "200 OK"; + } else { + System.out.println("It doesn't exist."); + http_code = "404 Not Found"; + } + Date d = new Date(); + DateFormat df = DateFormat.getDateTimeInstance(); + df.setTimeZone(TimeZone.getTimeZone("GMT")); + os.write(("HTTP/1.1 " + http_code + "\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 + System.out.println(file + "|||"); + return http_code; + } + + /** + * 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, String http_code, String file_path) throws Exception { + System.out.println(file_path + "|" + http_code); + File file = new File("./" + file_path); + DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd"); + LocalDateTime now = LocalDateTime.now(); + String date = dtf.format(now); + if (http_code.equals("404 Not Found")) { + os.write("\n".getBytes()); + os.write("

404 Not Found

\n".getBytes()); + os.write(" \"John\n".getBytes()); + os.write("\n".getBytes()); + } else { + BufferedReader reader; + try { + reader = new BufferedReader(new FileReader( + "./" + file_path)); + String line = reader.readLine(); + while (line != null) { + System.out.println(line); + // read next line + line = reader.readLine(); + if (line == null){ + break; + } + line = line.replaceAll("", "This is Ziad's server"); + line = line.replaceAll("", date); + os.write(line.getBytes()); + } + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } } // end class