Skip to content

Commit

Permalink
resized images and adjusted code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziad Arafat committed Sep 28, 2020
1 parent e941760 commit 1f6c9cd
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 37 deletions.
22 changes: 12 additions & 10 deletions SimpleWebServer/build.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
<project name="Hello World Project" default="jar">
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
<project name="Hello World Project" default="run" basedir=".">
<target name="clean">
<delete dir="bin"/>
</target>

<target name="compile" depends="clean">
<mkdir dir="bin/classes"/>
<javac srcdir="src" destdir="bin/classes"/>
</target>

<target name="jar" depends="compile">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
<mkdir dir="bin/jar"/>
<jar destfile="bin/jar/SimpleWebServer.jar" basedir="bin/classes">
<manifest>
<attribute name="Main-Class" value="edu.nmsu.cs.webserver.WebServer"/>
</manifest>
</jar>
</target>

<target name="run" depends="jar">
<java jar="build/jar/HelloWorld.jar" fork="true"/>
<java jar="bin/jar/SimpleWebServer.jar" fork="true" dir="./www/"/>
</target>

<target name="clean">
<delete dir="build"/>
</target>


</project>
9 changes: 3 additions & 6 deletions SimpleWebServer/src/edu/nmsu/cs/webserver/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,24 @@
import java.net.Socket;

public class WebServer {
private ServerSocket socket;

private boolean running;

/**
* Constructor
**/
private WebServer() {
running = false;
boolean 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
* @param port is the TCP port number to accept connections on
**/
private boolean start(int port) {
Socket workerSocket;
WebWorker worker;
ServerSocket socket;
try {
socket = new ServerSocket(port);
} catch (Exception e) {
Expand Down
33 changes: 18 additions & 15 deletions SimpleWebServer/src/edu/nmsu/cs/webserver/WebWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@
* @author Jon Cook, Ph.D.
**/

import com.sun.imageio.plugins.gif.GIFImageReader;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.Socket;
import java.nio.file.Files;
Expand All @@ -37,7 +31,7 @@

public class WebWorker implements Runnable {

private Socket socket;
private final Socket socket;

/**
* Constructor: must have a valid open socket
Expand All @@ -54,6 +48,7 @@ public WebWorker(Socket s) {
public void run() {
System.err.println("Handling connection...");
try {
System.out.println("Working Directory = " + System.getProperty("user.dir"));
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
String file_path = readHTTPRequest(is);
Expand All @@ -65,7 +60,6 @@ public void run() {
System.err.println("Output error: " + e);
}
System.err.println("Done handling connection.");
return;
}

/**
Expand All @@ -88,8 +82,6 @@ private String readHTTPRequest(InputStream is) {

// Check if it's a GET request.
if (line_items[0].equals("GET")) {
// System.out.println("found a get request");

// if it is lets get the file path its requesting to return it
file_path = line_items[1];
}
Expand All @@ -102,7 +94,7 @@ private String readHTTPRequest(InputStream is) {
break;
}
}
return "www" + file_path;
return "./" + file_path;
}

/**
Expand Down Expand Up @@ -174,6 +166,13 @@ else if (!contentType.split("/")[0].equals("text")) {

}

/**
* Used to serve binary non-text files to the browser.
*
* @param os the output stream
* @param file_path the path to the binary file
* @throws IOException
*/
private void writeBinary(OutputStream os, String file_path) throws IOException {
InputStream istream = new FileInputStream(file_path);
int byteRead;
Expand All @@ -182,6 +181,12 @@ private void writeBinary(OutputStream os, String file_path) throws IOException {
}
}

/**
* Serves a text or html file to the browser.
*
* @param os the output stream
* @param file_path the file path to the text file
*/
private void writeText(OutputStream os, String file_path) {

// Get the current date
Expand All @@ -192,8 +197,7 @@ private void writeText(OutputStream os, String file_path) {
// render the file from the path
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(
"./" + file_path));
reader = new BufferedReader(new FileReader(file_path));
String line = reader.readLine();
while (line != null) {
// read next line
Expand All @@ -212,5 +216,4 @@ private void writeText(OutputStream os, String file_path) {
e.printStackTrace();
}
}

} // end class
}
25 changes: 25 additions & 0 deletions SimpleWebServer/www/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<project name="SimpleWebServer" default="cleanbuild" basedir="..">
<description>
Buildfile for the SimpleWebServer.
</description>
<property name="src" location="src/" />
<property name="build" location="bin/" />

<target name="clean" description="delete class files">
<delete dir="${build}"/>
</target>

<target name="build" description="compiles java files to /bin">
<mkdir dir="${build}"/>
<javac srcdir="${src}" destdir="${build}"/>
</target>

<target name="cleanbuild" depends="clean,build"/>

<target name="run" description="launches SimpleWebServer on port 8080">
<java classname="edu.nmsu.cs.webserver.WebServer" classpath="${build}" fork="true" >
<arg value="8080"/>
</java>

</target>
</project>
Binary file modified SimpleWebServer/www/cheemsBonk.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified SimpleWebServer/www/cheemsHello.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified SimpleWebServer/www/dogeDance.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 45 additions & 6 deletions SimpleWebServer/www/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
<html>
<head>
</head>
<body>
<p>Hello world, the current date is: <cs371date>. The server is: <cs371server>.</p>
</body>
</html>
<head>
<title>Welcome to my server</title>
</head>

<body>
<h1>Hello world, the current date is: <cs371date>. The server is: <cs371server>.</h1>

<hr>
<p>Here are some GIFS</p>
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge">
<img src="dogeDance.gif" alt="Dancing Doge"><br>

<p>This one is a PNG</p>
<img src="cheemsBonk.png" alt="Dancing Doge">

<p>This one is a JPG</p>
<img src="cheemsHello.jpg" alt="Dancing Doge">
</body>
</html>

0 comments on commit 1f6c9cd

Please sign in to comment.