Skip to content

Commit

Permalink
1.0v
Browse files Browse the repository at this point in the history
Program 1 Completed:

simpleWebServer
CHANGELOG:
 - Added http code and page variables
 - Added getSort and searchFile methods
 - Changed readHTTPRequest to allow custom html file support
 - Changed writeHTTPHeader to support 404 errors
 - Changed writeContent to support 404 errors and custom html pages
  • Loading branch information
aacuna45 committed Feb 18, 2023
1 parent 93b275c commit aa56cd6
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Questions/p1Questions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1. 5.5 Hours
2.
3.5 Hours Thought/Research/Design
0.5 hours programming
1 hour debugging

3. Question 1 was done after the project
20 changes: 20 additions & 0 deletions SimpleWebServer/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<project name="SimpleWebServer" default="run">

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

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

<target name="run" depends="compile">
<java classname="edu.nmsu.cs.webserver.WebServer" classpath="bin/">
<arg value="8080"/>
</java>
</target>



</project>
7 changes: 7 additions & 0 deletions SimpleWebServer/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>
131 changes: 121 additions & 10 deletions SimpleWebServer/src/edu/nmsu/cs/webserver/WebWorker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package edu.nmsu.cs.webserver;

//Alexander Acuna
//2/17/2023
//CS468
//Program 1: Simple Web Server

/*
* CHANGELOG:
* - Added http code and page variables
* - Added getSort and searchFile methods
* - Changed readHTTPRequest to allow custom html file support
* - Changed writeHTTPHeader to support 404 errors
* - Changed writeContent to support 404 errors and custom html pages
*/

/**
* 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
Expand All @@ -20,17 +34,22 @@
**/

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.file.Files;
import java.text.DateFormat;
import java.util.Date;
import java.util.TimeZone;



public class WebWorker implements Runnable
{

int code; // Integer for http status code
File page; // File for html page
private Socket socket;

/**
Expand Down Expand Up @@ -67,6 +86,58 @@ public void run()
return;
}

//HTML file searcher: Checks to see if requested page exists
//PRE: HTML address string
//POST: File address set and http status code set
public void searchFile(String fileName){

//Trys to find file
try{
//File set to given address
File file = new File(fileName);

//file existance check: Sets http code to 200 if found and page set to file
//Sets code to 404 if cant be found
if (file.exists()){
System.out.println("File found");
page = file;
code = 200;}
else{System.out.println("File not found");
code = 404;
page = null;
}


}
//Catches error if file address cannot be accessed
catch(Exception e){System.out.println("File not found");}

}
//GET request handeler to search for html page request
//PRE: GET string from input string
//POST: HTML address if GET HTML request
public void getSort(String getLine){

//string array to split file extension
String temp[] = getLine.split("[.]");

//html check: icon request ignored
if(temp[1].equals("html")){

//sets address string to windows file seperators
String address = getLine.replaceAll("/", "\\\\");

//finds active directory to string
String curDir = System.getProperty("user.dir");

//sends active dir and file dir to searchFile method
searchFile(curDir+address);

}


}

/**
* Read the HTTP request header.
**/
Expand All @@ -77,10 +148,22 @@ private void readHTTPRequest(InputStream is)
while (true)
{
try
{
{

while (!r.ready())
Thread.sleep(1);
line = r.readLine();

//Creates string array to split input stream line
String[] temp = line.split(" ");

//Checks if current line is a GET request
if(temp[0].equals("GET")){

//Sends page address to getSort method
getSort(temp[1]);
}

System.err.println("Request line: (" + line + ")");
if (line.length() == 0)
break;
Expand All @@ -106,14 +189,16 @@ private void writeHTTPHeader(OutputStream os, String contentType) throws Excepti
{
Date d = new Date();
DateFormat df = DateFormat.getDateTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("GMT"));
os.write("HTTP/1.1 200 OK\n".getBytes());
df.setTimeZone(TimeZone.getDefault());

//Checks for 404 error
if(code == 404){os.write("HTTP/1.1 404 Page not found\n".getBytes());}
else {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("Server: H-S1\n".getBytes());
os.write("Connection: close\n".getBytes());
os.write("Content-Type: ".getBytes());
os.write(contentType.getBytes());
Expand All @@ -130,9 +215,35 @@ private void writeHTTPHeader(OutputStream os, String contentType) throws Excepti
**/
private void writeContent(OutputStream os) 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());
//404 HTML Message
if(code == 404){
os.write("<html><head></head><body>\n".getBytes());
os.write("<h1>Error 404</h1>\n".getBytes());
os.write("<h3>Sorry, page not found :< </h3>\n".getBytes());

}
//200 HTML Message
else{

//Grabs current date
Date date = new Date();
//Sets date format
DateFormat df = DateFormat.getDateTimeInstance();
df.setTimeZone(TimeZone.getDefault());

//Sets html page to string for parsing
String pageBody = Files.readString(page.toPath());

//Sets <cs371date> to current date
pageBody = pageBody.replaceAll("<cs371date>", df.format(date).toString() );

//Sets <cs371server> to server name
pageBody = pageBody.replaceAll("<cs371server>", "H-S1");

//Writes html page to outstream
os.write(pageBody.getBytes());

}
}

} // end class

0 comments on commit aa56cd6

Please sign in to comment.