From 7ec861062fdece0d3de58369d44978993eba9b67 Mon Sep 17 00:00:00 2001 From: josh Date: Sun, 3 Feb 2008 03:15:11 +0000 Subject: [PATCH] cs654/lab3 part B completed git-svn-id: svn://anubis/gvsu@5 45c1a28c-8058-47b2-ae61-ca45b979098e --- cs654/lab3/HttpRequest.class | Bin 0 -> 2853 bytes cs654/lab3/HttpRequest.java | 131 +++++++++++++++++++++++++++++++++++ cs654/lab3/WebServer.class | Bin 0 -> 790 bytes cs654/lab3/WebServer.java | 38 ++++++++++ 4 files changed, 169 insertions(+) create mode 100644 cs654/lab3/HttpRequest.class create mode 100644 cs654/lab3/HttpRequest.java create mode 100644 cs654/lab3/WebServer.class create mode 100644 cs654/lab3/WebServer.java diff --git a/cs654/lab3/HttpRequest.class b/cs654/lab3/HttpRequest.class new file mode 100644 index 0000000000000000000000000000000000000000..ac57730e0420b6a63e47e7115e868d465c89f4fa GIT binary patch literal 2853 zcmZuz-FF*T5&vCT(ymr(JC^Olf+pCsNo^^%9M^mPZmzlCzTZ;290i;yD#xlV-lI;u|Wysp4BIzOCXr zD!!}Yc@-}x_@09AD|k`Be{4Q=LLib_&z{dF@>y#&vEYoMCZJ6X$F%;qbG zK(K7*HVj9gqo!>cPGZ4jQ8_qfTBb8D(A9s7bYM|BvR7!MJ!M+PY^AVll+xMdJUQCr z#hg)eO!^XNDOD`eetDyH!O7+}X0kQ7+FLk5`Pc* z6bO|KYh~h+W6uce0@1WgV~J zhYEhA<5m1vpvTo>pvSXL)*GyXpXj)ZE7I<30=pY}6P0u4jFPc3Z)8`Dl0dW}@I1dJ z)A2gqkk@}I$(uUf!p|D|701li!H~8$49k4NC`qlib-W|B_B4a9L1v<2=3RecNXO6d z3mv~~>fKrZfxV5mWWHu?=Y(CcR(2A>B1~I3yFf7Z_4UbIysh9R9h-QU5S&VKd3l-| zn@Z25#>b{6?>|01mYz;@$|$^_ZKde z9izZzwJWsJz0*x6IXxOJ)<8Im?_molMX8i$t9N@ls}|Bx+5GKpRLu!P&Z^;f!Ox=h z*L(2)dzc7rw!Qa`Yr2hwJdHpgd+c}w9BMsHGhTrVu(K(uwUTRF@<{bnLG8qW4x+kH_8>v8`i3`<_^4^dAbT!)1n=*>st1KGTrl;lGRR&jHGNtv~KIasM`H{L80!QHE zS1F03+>i0Y>O&tX2Ow#Hy!-G)^6uv;#?{AFi)}%~;=hCM8czZfq-|&+)!|1gLOdPg z$xoU5KaNRKXrVHYj5Y$BZc2!VJN!E2ODeTDx%*WPaW2D*6OLvA-z>_$7|JVCAk z-vbKx=aVCvl%{Ya2=?_FMR>P`40DZ9~g!BV1?cTWH-v+swf#+Gn@1D>Jx- zj-)^8uOhPK(qv#8otZ6kMY^ll9Sx9Fu_qZ2$)NwRvW=chG_ZweGPnsfskR(e_=L!( ztGI*fAQzETrSVKstzz$Gw2>X<_KwXPpNM2gBsGx?cPp3CAK8}-HC%nhb@efNxkmcx zF11axM?RYjMea;$k-L)Ncr;YS{&-ZARBG*)b~>WL_{TU94OVgYCPMLOScO!;e3xGcDsW<|m#R_q~bM_`#OFRUF*B@lT0o zFaFjLgh}k>`Ae|CblGlj8hrniq0Nh~o4oN`R>%2yT9Jr85{nQMLsy{3A_;CTBN!RJ z8jBpN??<@5r=c)fXMXN#|_4>@FZ*T6b|E=dP4*vzHo>1 J`G|XQ@qf(1onHU| literal 0 HcmV?d00001 diff --git a/cs654/lab3/HttpRequest.java b/cs654/lab3/HttpRequest.java new file mode 100644 index 0000000..ba82ced --- /dev/null +++ b/cs654/lab3/HttpRequest.java @@ -0,0 +1,131 @@ +import java.io.* ; +import java.net.* ; +import java.util.* ; + +final class HttpRequest implements Runnable +{ + final static String CRLF = "\r\n"; + Socket socket; + + // Constructor + public HttpRequest(Socket socket) throws Exception + { + this.socket = socket; + } + + // Implement the run() method of the Runnable interface. + public void run() + { + try { + processRequest(); + } catch (Exception e) { + System.out.println(e); + } + } + + private void processRequest() throws Exception + { + // Set up output stream + DataOutputStream os = new DataOutputStream(socket.getOutputStream()); + + // Set up input stream filters. + BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); + + // Get the request line of the HTTP request message. + String requestLine = br.readLine(); + + // Extract the filename from the request line. + StringTokenizer tokens = new StringTokenizer(requestLine); + tokens.nextToken(); // skip over the method, which should be "GET" + String fileName = tokens.nextToken(); + + // Prepend a "." so that file request is within the current directory. + fileName = "." + fileName ; + + // Open the requested file. + FileInputStream fis = null ; + boolean fileExists = true ; + try { + fis = new FileInputStream(fileName); + } catch (FileNotFoundException e) { + fileExists = false ; + } + + // Debug info for private use + System.out.println("Incoming!!!"); + System.out.println(requestLine); + String headerLine = null; + while ((headerLine = br.readLine()).length() != 0) { + System.out.println(headerLine); + } + + // Construct the response message. + String statusLine = null; + String contentTypeLine = null; + String entityBody = null; + if (fileExists) { + statusLine = "HTTP/1.0 200 OK" + CRLF; + contentTypeLine = "Content-Type: " + + contentType(fileName) + CRLF; + } else { + statusLine = "HTTP/1.0 404 Not Found" + CRLF; + contentTypeLine = "text/html" + CRLF; + entityBody = "" + + "Not Found" + + "Not Found"; + } + // Send the status line. + os.writeBytes(statusLine); + + // Send the content type line. + os.writeBytes(contentTypeLine); + + // Send a blank line to indicate the end of the header lines. + os.writeBytes(CRLF); + + // Send the entity body. + if (fileExists) { + sendBytes(fis, os); + fis.close(); + } else { + os.writeBytes(entityBody) ; + } + + // Close streams and socket. + os.close(); + br.close(); + socket.close(); + } + + private static void sendBytes(FileInputStream fis, + OutputStream os) throws Exception + { + // Construct a 1K buffer to hold bytes on their way to the socket. + byte[] buffer = new byte[1024]; + int bytes = 0; + + // Copy requested file into the socket's output stream. + while ((bytes = fis.read(buffer)) != -1) { + os.write(buffer, 0, bytes); + } + } + + private static String contentType(String fileName) + { + if(fileName.endsWith(".htm") || fileName.endsWith(".html")) { + return "text/html"; + } + + if(fileName.endsWith(".gif")) { + return "image/gif"; + } + + if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) + { + return "image/jpeg"; + } + + return "application/octet-stream" ; + } +} + diff --git a/cs654/lab3/WebServer.class b/cs654/lab3/WebServer.class new file mode 100644 index 0000000000000000000000000000000000000000..81746ef126a84a0fe23bef4bce31c683e7109bf5 GIT binary patch literal 790 zcmZuv+fEu$6kP{q7#IhY0$OSnZ>d=vqA**LT@0`}TSGDR*LX=a>aTzAe_oZX&+{!gAw~?y| z&v6?Yk{GQB%Rbq&+I~IhWYBj$+q^9tnyO)i&Xrcjv-z$g`;ETi^$PdSxwk6&8Hgjn z5F7R`x`H>jCljj%a(K*;^s_D(xdF4%vQM~>6?udFdcrXBT8MU;f9mivAAia)K5SY2 z=yB^v);}}wT+WN!sQ9PwpnlA4!4MyEZ|*`e%>P>ucCM}U%_PGWvnZ_zS+?|n+L@UF z)4zf*euE)#-R>4BS(CS?lsm2~-@&(9rAPDx@`%#^0WjRd7{+Oj(J6pCv}hI*6qAHZ z)6QsDvpukHK4F4lXaFLDN!%rhEd2t7a9}%o^%e0x0<~;#t%qP2$|XXv^hKc$wKmm5 zXm7a-ZNHCjZKa3Ey3&VUODR2!tg9)t3*++7PwEqZoHPTCqJb0%oTq6^Bsfp93Yf(v d<_MSBVDc6Fg$UCqJ4J{>`tH-3rtbh|{sQT&q+kF5 literal 0 HcmV?d00001 diff --git a/cs654/lab3/WebServer.java b/cs654/lab3/WebServer.java new file mode 100644 index 0000000..2eb1deb --- /dev/null +++ b/cs654/lab3/WebServer.java @@ -0,0 +1,38 @@ +import java.io.* ; +import java.net.* ; +import java.util.* ; + +public final class WebServer +{ + private static final int DEFAULT_PORT = 8080; + + public static void main(String argv[]) throws Exception + { + // Get the port number from the command line. + int port = (argv.length > 0) + ? (new Integer(argv[0])).intValue() + : DEFAULT_PORT; + + // Establish the listen socket. + ServerSocket socket = new ServerSocket(port); + + // Process HTTP service requests in an infinite loop. + while (true) + { + // Listen for a TCP connection request. + Socket connection = socket.accept(); + + // Construct an object to process the HTTP request message. + HttpRequest request = new HttpRequest(connection); + + // Create a new thread to process the request. + Thread thread = new Thread(request); + + // Start the thread. + thread.start(); + } + } +} + + +