Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new files: build.xml in Circles and SimpleWebServer. Hello world java… #32

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added Circles/build.xml
Empty file.
9 changes: 9 additions & 0 deletions HelloWorld/src/edu/nmsu/cs/helloworld/helloworld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package edu.nmsu.cs.helloworld;

public class helloworld {

public static void main(String args[]) {
System.out.println("Hello, World");
}
}

7 changes: 7 additions & 0 deletions Questions/p1Questions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
I figure this program will take about one hour.

I'm unsure the actual time it took me for this project.
It is hard to say because I had multiple other issues with my home linux system that needed to be worked out.
Just working on the code was around one hour. It was possibly less.
However, if I add the time looking at the code there was 1 hour I spent in class while you were talking about it. And I spent another hour looking it over myself at Dions. So in total about 3 hours. I also spent about a half hour trying to figure out ant.
But just coding up the Program 1 was pretty quick. I feel like my estimate was decent.
8 changes: 8 additions & 0 deletions Questions/p2Questions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Total estimate time: 1 hour

Thought/Design:
Programming:
Debugging:
Total:

Percent Accuracy:
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Github user id: ryanschwarzkopf

# Programs
Base code for program assignments in Software Development (C S 371).

Expand Down
22 changes: 22 additions & 0 deletions SimpleWebServer/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<project name="SimpleWebServer" basedir=".">

<property name="src" location="src"/>
<property name="build" value="bin"/>

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

<target name="build">
<mkdir dir="${build}"/>
<javac srcdir="${src}" destdir="${build}"/>
</target>

<target name="run">
<java classname="edu.nmsu.cs.webserver.Webserver" classpath="${build}" fork="true" dir="${basedir}" executable="/lib/jvm/java-11-openjdk-amd64/bin/java">
<arg value="8085"/>
</java>
</target>


</project>
122 changes: 108 additions & 14 deletions SimpleWebServer/src/edu/nmsu/cs/webserver/WebWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,29 @@
* particular format).
*
**/

/**
Webworker actually serves html files. Respondes given an incorrect filename to header and 404 not found page.
Processes dynamic html tags for date and server name.
Ryan Schwarzkopf

*/

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.text.DateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class WebWorker implements Runnable
{
Expand All @@ -53,9 +67,10 @@ public void run()
{
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
readHTTPRequest(is);
writeHTTPHeader(os, "text/html");
writeContent(os);
String fileRequest = readHTTPRequest(is);
String contentType = readFileType(fileRequest);
int HTTPstatus = writeHTTPHeader(os, contentType, fileRequest);
writeContent(os, fileRequest, HTTPstatus, contentType);
os.flush();
socket.close();
}
Expand All @@ -70,9 +85,15 @@ public void run()
/**
* Read the HTTP request header.
**/
private void readHTTPRequest(InputStream is)
/**
* Search request header for any file request. Print to console if found.
* Ryan Schwarzkopf
*
*/
private String readHTTPRequest(InputStream is)
{
String line;
String fileRequest = "";
BufferedReader r = new BufferedReader(new InputStreamReader(is));
while (true)
{
Expand All @@ -82,6 +103,10 @@ private void readHTTPRequest(InputStream is)
Thread.sleep(1);
line = r.readLine();
System.err.println("Request line: (" + line + ")");
if((line.length() > 3) && (line.substring(0, 3).compareTo("GET") == 0) && (line.length() != 14)) {
fileRequest = line.substring(5, line.length()-9);
System.err.println("File name found: ("+fileRequest+")");
}
if (line.length() == 0)
break;
}
Expand All @@ -91,7 +116,7 @@ private void readHTTPRequest(InputStream is)
break;
}
}
return;
return fileRequest;
}

/**
Expand All @@ -102,23 +127,36 @@ private void readHTTPRequest(InputStream is)
* @param contentType
* is the string MIME content type (e.g. "text/html")
**/
private void writeHTTPHeader(OutputStream os, String contentType) throws Exception
/**
* Search for fileRequest in top directory. Write http header 200 or 404. Return 200 or 404
* Ryan Schwarzkopf
*
* @param fileRequest
* @return 200: file found 404: file not found
*/
private int writeHTTPHeader(OutputStream os, String contentType, String fileRequest) 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());
File f = new File(fileRequest);
if(f.exists()) {
os.write("HTTP/1.1 200 OK\n".getBytes());
} else {
os.write("HTTP/1.1 404 Not Found\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("Server: Ryan'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(!f.exists()) return 404; // Could not find the requestFile
return 200; // requestFile found
}

/**
Expand All @@ -128,11 +166,67 @@ private void writeHTTPHeader(OutputStream os, String contentType) throws Excepti
* @param os
* is the OutputStream object to write to
**/
private void writeContent(OutputStream os) throws Exception
/**
* Search for file and write its contents to output stream. Dynamic HTML for date and server name.
* Returns 200 if file is found. 404 if not found
* Ryan Schwarzkopf
*
* @param filePath
* path to file to read
*
*/
private void writeContent(OutputStream os, String filePath, int HTTPstatus, String contentType) throws Exception
{
os.write("<html><head></head><body>\n".getBytes());
os.write("<h3>My web server works!</h3>\n".getBytes());
os.write("</body></html>\n".getBytes());
if(HTTPstatus == 404) { // File was not found. Cannot write content.
os.write("".getBytes());
os.write("<!DOCTYPE html><html><head><link rel=\"icon\" type=\"image/x-icon\" href=\"https://icons8.com/icon/35747/mario-8-bit\"></head><body><p>404: Not found</p></body>".getBytes());
} else {
if(contentType.equals("text/html")) { // Case text/html
try {
// Get the date
Date date = new Date();
DateFormat formatter = new SimpleDateFormat("dd/MM/yy");
String theDate = formatter.format(date);
// Get a file reader
BufferedReader in = new BufferedReader(new FileReader(filePath));
String line;
while((line = in.readLine()) != null) {
line = line.replace("cs371date", theDate);
System.out.println(line);
line = line.replace("cs371server", "Ryan's server");
os.write(line.getBytes());
}
in.close();
return;
} catch(FileNotFoundException e) {
System.err.println("File not found.");
return;
}
} else {
if (contentType.equals("image/jpeg") || contentType.equals("image/gif") || contentType.equals("image/png")) {
try {
// Load an image from a file
File imageFile = new File("www/res/acc/image.jpg"); // Replace with your image file path
BufferedImage image = ImageIO.read(imageFile);

// Write the image to the OutputStream
ImageIO.write(image, "jpg", os);

System.out.println("Image written to OutputStream successfully.");
} catch (IOException e) {
System.out.println("Error writing image to OutputStream: " + e.getMessage());
}
}
} // end if/else
} // end file not found catch
}

private String readFileType(String fileRequest) {
if(fileRequest.contains("html")) return "text/html";
if(fileRequest.contains("jpeg")) return "image/jpeg";
if(fileRequest.contains("png")) return "image/png";
if(fileRequest.contains("gif")) return "image/gif";
return "text/html";
}

} // end class
Binary file not shown.
Binary file added SimpleWebServer/www/res/acc/apple.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions SimpleWebServer/www/res/acc/hello.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head>
</head>
<body>
<p>Hello world, the current date is: cs371date. The server is: cs371server.</p>
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions SimpleWebServer/www/res/acc/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head>
</head>
<body>
<p>This is test.html</p>
</body>
</html>