String method;
String ct;
String version = "";
File theFile;
try {
PrintStream os = new PrintStream(theConnection.getOutputStream());
DataInputStream is = new DataInputStream(theConnection.getInputStream());
String get = is.readLine();
StringTokenizer st = new StringTokenizer(get);
method = st.nextToken();
if (method.equals("GET")) {
String file = st.nextToken();
if (file.endsWith("/")) file += indexfile;
ct = guessContentTypeFromName(file);
if (st.hasMoreTokens()) {
version = st.nextToken();
}
// loop through the rest of the input lines
while ((get = is.readLine()) != null) {
if (get.trim().equals("")) break;
}
try {
theFile = new File(docroot, file.substring(1,file.length()));
FileInputStream fis = new FileInputStream(theFile);
byte[] theData = new byte[(int) theFile.length()];
// need to check the number of bytes read here
fis.read(theData);
fis.close();
if (version.startsWith("HTTP/")) { // send a MIME header
os.print("HTTP/1.0 200 OK\r\n");
Date now = new Date();
os.print("Date: " + now + "\r\n");
os.print("Server: jhttp 1.0\r\n");
os.print("Content-length: " + theData.length + "\r\n");
os.print("Content-type: " + ct + "\r\n\r\n");
} // end try
// send the file
os.write(theData);
os.close();
} // end try
catch (IOException e) { // can't find the file
if (version.startsWith("HTTP/")) { // send a MIME header
os.print("HTTP/1.0 404 File Not Found\r\n");
Date now = new Date();
os.print("Date: " + now + "\r\n");
os.print("Server: jhttp 1.0\r\n");
os.print("Content-type: text/html" + "\r\n\r\n");
}
os.println("<HTML><HEAD><TITLE>File Not Found</TITLE></HEAD>");
os.println("<BODY><H1>HTTP Error 404: File Not Found</H1></BODY></HTML>");
os.close();
}
}
else { // method does not equal "GET"
if (version.startsWith("HTTP/")) { // send a MIME header
os.print("HTTP/1.0 501 Not Implemented\r\n");
Date now = new Date();
os.print("Date: " + now + "\r\n");
os.print("Server: jhttp 1.0\r\n");
os.print("Content-type: text/html" + "\r\n\r\n");
}
os.println("<HTML><HEAD><TITLE>Not Implemented</TITLE></HEAD>");
os.println("<BODY><H1>HTTP Error 501: Not Implemented</H1></BODY></HTML>");
os.close();
}
}
catch (IOException e) {
}
try {
theConnection.close();
}
catch (IOException e) {
}