removed Location, added 500, minor changes

This commit is contained in:
Jeena Paradies 2010-11-04 12:48:49 +01:00
parent 7de5c549ca
commit f74ae8c954

View file

@ -45,6 +45,9 @@ public final class WebServer
final class HttpRequest implements Runnable
{
// Constants
final static String HTTP_VERSION = "HTTP/1.0";
final static String CRLF = "\r\n";
// Recognized HTTP methods
final static class HTTP_METHOD
{
@ -53,16 +56,18 @@ final class HttpRequest implements Runnable
final static String POST = "POST";
}
final static String HTTPVERSION = "HTTP/1.0";
final static String CRLF = "\r\n";
final static String RESPONSE_200 = HTTPVERSION + " 200 OK " + CRLF;
final static String RESPONSE_404 = HTTPVERSION + " 404 Not Found " + CRLF;
final static String RESPONSE_400 = HTTPVERSION + " 400 Bad Request " + CRLF;
final static String RESPONSE_501 = HTTPVERSION + " 501 Not Implemented " + CRLF;
final static String LOCATION = " datalabb " + CRLF;
final static class HTTP_RESPONSE
{
final static String OK = HTTP_VERSION + " 200 OK " + CRLF;
final static String NOT_FOUND = HTTP_VERSION + " 404 Not Found " + CRLF;
final static String BAD_REQUEST = HTTP_VERSION + " 400 Bad Request " + CRLF;
final static String INTERNAL_SERVER_ERROR = HTTP_VERSION + " 500 Internal Server Error " + CRLF;
final static String NOT_IMPLEMENTED = HTTP_VERSION + " 501 Not Implemented " + CRLF;
}
Socket socket;
// Constructor
public HttpRequest(Socket socket) throws Exception
{
@ -75,7 +80,21 @@ final class HttpRequest implements Runnable
try {
processRequest();
} catch (Exception e) {
System.out.println(e);
System.err.println(e);
try {
DataOutputStream outs = new DataOutputStream(socket.getOutputStream());
outs.writeChars(HTTP_RESPONSE.INTERNAL_SERVER_ERROR);
outs.writeChars(CRLF);
outs.writeChars("<h1>500 Internal Server Error</h1>");
outs.close();
socket.close();
} catch (Exception e2) {
System.err.println(e2);
}
}
}
@ -104,13 +123,18 @@ final class HttpRequest implements Runnable
String Request = tokens[0];
if(tokens.length != 3) {
System.out.println("Wrong number of arguments in request!");
outs.writeChars(RESPONSE_400 + LOCATION + getDateString(d));
System.err.println("Wrong number of arguments in request!");
outs.writeChars(HTTP_RESPONSE.BAD_REQUEST + getDateHeader(d));
outs.writeChars(CRLF);
outs.writeChars("<h1>400 Bas Request</h1>");
} else if(tokens[1].charAt(0) != '/') {
System.out.println("illegal url");
outs.writeChars(RESPONSE_501 + LOCATION + getDateString(d));
System.err.println("illegal url");
outs.writeChars(HTTP_RESPONSE.NOT_IMPLEMENTED + getDateHeader(d));
outs.writeChars(CRLF);
outs.writeChars("<h1>501 Not Implemented</h1>");
} else if(Request.equals(HTTP_METHOD.GET) || Request.equals(HTTP_METHOD.HEAD)) {
@ -127,16 +151,26 @@ final class HttpRequest implements Runnable
}
} catch (FileNotFoundException e) {
outs.writeChars(RESPONSE_404 + getDateString(d));
outs.writeChars(HTTP_RESPONSE.NOT_FOUND + getDateHeader(d));
outs.writeChars(CRLF);
outs.writeChars("<h1>404 Not Found</h1>");
}
} else if(Request.equals(HTTP_METHOD.POST)) {
outs.writeChars(RESPONSE_501 + getDateString(d));
outs.writeChars(HTTP_RESPONSE.NOT_IMPLEMENTED + getDateHeader(d));
outs.writeChars(CRLF);
outs.writeChars("<h1>501 Not Implemented</h1>");
} else {
System.err.println("illegal url");
outs.writeChars(HTTP_RESPONSE.BAD_REQUEST + getDateHeader(d));
outs.writeChars(CRLF);
outs.writeChars("<h1>400 Bas Request</h1>");
}
else {
outs.writeChars(RESPONSE_400 + getDateString(d));
}
@ -146,14 +180,13 @@ final class HttpRequest implements Runnable
socket.close();
}
private String getDateString(Date d){
private String getDateHeader(Date d){
return "Date: " + d.toString() + CRLF;
}
private String createHeader(Date d, File f){
String response = RESPONSE_200;
response += getDateString(d);
response += "LOCATION: " + f.getName() +CRLF;
String response = HTTP_RESPONSE.OK;
response += getDateHeader(d);
response += "Server: Labbserver" + CRLF;
response += "Allow: " + HTTP_METHOD.GET + " " + HTTP_METHOD.HEAD+CRLF;
response += "Content-Length: " + f.length() + CRLF;
@ -175,16 +208,16 @@ final class HttpRequest implements Runnable
private static String contentType(String fileName)
{
if (fileName.toLowerCase().endsWith(".htm") ||
fileName.toLowerCase().endsWith(".html")) {
if (fileName.toLowerCase().endsWith(".htm") || fileName.toLowerCase().endsWith(".html")) {
return "text/html";
} else if (fileName.toLowerCase().endsWith(".gif")) {
return "image/gif";
} else if (fileName.toLowerCase().endsWith(".jpg")) {
} else if (fileName.toLowerCase().endsWith(".png")) {
return "image/png";
} else if (fileName.toLowerCase().endsWith(".jpg") || fileName.toLowerCase().endsWith(".jpeg")) {
return "image/jpeg";
} else {
return "application/octet-stream";
}
}
}