Skip to content

Commit

Permalink
Fixed HTML header not 404ing correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
rhollomon committed Feb 16, 2023
1 parent 8d68c65 commit f40c5be
Showing 1 changed file with 7 additions and 25 deletions.
32 changes: 7 additions & 25 deletions SimpleWebServer/src/edu/nmsu/cs/webserver/WebWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,6 @@ private String readHTTPRequest(InputStream is)



/**
* Check if file exists, else throw error
*
* @param os
* @param fName
* @throws Exception
*/
private void getFile(OutputStream os, String fName) throws Exception{

try{
File file = new File(fName);
FileReader reader = new FileReader(file);

reader.close();
} catch(Exception e){
os.write("HTTP/1.1 404 Not Found\n".getBytes());
}

} // end getFile


/**
* Read HTML file and write its contents to web page. Also, replaces
* use of tags <cs371date> and <cs371server> with specified strings.
Expand Down Expand Up @@ -197,13 +176,16 @@ private void writeHTTPHeader(OutputStream os, String contentType, String fName)
Date d = new Date();
DateFormat df = DateFormat.getDateTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("MST")); // Changed to MST

// by default, throw this
os.write("HTTP/1.1 200 OK\n".getBytes());

// if we encounter a reading error write 404 not found
fName = fName.substring(5,fName.length()-9); // getting only file name itself
getFile(os, fName);

File f = new File(fName);
if(!f.exists() || f.isDirectory()) {
os.write("HTTP/1.1 404 Not Found\n".getBytes());
} else{
os.write("HTTP/1.1 200 OK\n".getBytes());
}

os.write("Date: ".getBytes());
os.write((df.format(d)).getBytes());
Expand Down

0 comments on commit f40c5be

Please sign in to comment.