removed Location, added 500, minor changes
This commit is contained in:
parent
7de5c549ca
commit
f74ae8c954
1 changed files with 153 additions and 120 deletions
|
@ -45,6 +45,9 @@ public final class WebServer
|
||||||
final class HttpRequest implements Runnable
|
final class HttpRequest implements Runnable
|
||||||
{
|
{
|
||||||
// Constants
|
// Constants
|
||||||
|
final static String HTTP_VERSION = "HTTP/1.0";
|
||||||
|
final static String CRLF = "\r\n";
|
||||||
|
|
||||||
// Recognized HTTP methods
|
// Recognized HTTP methods
|
||||||
final static class HTTP_METHOD
|
final static class HTTP_METHOD
|
||||||
{
|
{
|
||||||
|
@ -53,16 +56,18 @@ final class HttpRequest implements Runnable
|
||||||
final static String POST = "POST";
|
final static String POST = "POST";
|
||||||
}
|
}
|
||||||
|
|
||||||
final static String HTTPVERSION = "HTTP/1.0";
|
final static class HTTP_RESPONSE
|
||||||
final static String CRLF = "\r\n";
|
{
|
||||||
final static String RESPONSE_200 = HTTPVERSION + " 200 OK " + CRLF;
|
final static String OK = HTTP_VERSION + " 200 OK " + CRLF;
|
||||||
final static String RESPONSE_404 = HTTPVERSION + " 404 Not Found " + CRLF;
|
final static String NOT_FOUND = HTTP_VERSION + " 404 Not Found " + CRLF;
|
||||||
final static String RESPONSE_400 = HTTPVERSION + " 400 Bad Request " + CRLF;
|
final static String BAD_REQUEST = HTTP_VERSION + " 400 Bad Request " + CRLF;
|
||||||
final static String RESPONSE_501 = HTTPVERSION + " 501 Not Implemented " + CRLF;
|
final static String INTERNAL_SERVER_ERROR = HTTP_VERSION + " 500 Internal Server Error " + CRLF;
|
||||||
final static String LOCATION = " datalabb " + CRLF;
|
final static String NOT_IMPLEMENTED = HTTP_VERSION + " 501 Not Implemented " + CRLF;
|
||||||
|
}
|
||||||
|
|
||||||
Socket socket;
|
Socket socket;
|
||||||
|
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
public HttpRequest(Socket socket) throws Exception
|
public HttpRequest(Socket socket) throws Exception
|
||||||
{
|
{
|
||||||
|
@ -75,7 +80,21 @@ final class HttpRequest implements Runnable
|
||||||
try {
|
try {
|
||||||
processRequest();
|
processRequest();
|
||||||
} catch (Exception e) {
|
} 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];
|
String Request = tokens[0];
|
||||||
if(tokens.length != 3) {
|
if(tokens.length != 3) {
|
||||||
|
|
||||||
System.out.println("Wrong number of arguments in request!");
|
System.err.println("Wrong number of arguments in request!");
|
||||||
outs.writeChars(RESPONSE_400 + LOCATION + getDateString(d));
|
outs.writeChars(HTTP_RESPONSE.BAD_REQUEST + getDateHeader(d));
|
||||||
|
outs.writeChars(CRLF);
|
||||||
|
outs.writeChars("<h1>400 Bas Request</h1>");
|
||||||
|
|
||||||
} else if(tokens[1].charAt(0) != '/') {
|
} else if(tokens[1].charAt(0) != '/') {
|
||||||
|
|
||||||
System.out.println("illegal url");
|
System.err.println("illegal url");
|
||||||
outs.writeChars(RESPONSE_501 + LOCATION + getDateString(d));
|
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)) {
|
} else if(Request.equals(HTTP_METHOD.GET) || Request.equals(HTTP_METHOD.HEAD)) {
|
||||||
|
|
||||||
|
@ -127,16 +151,26 @@ final class HttpRequest implements Runnable
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (FileNotFoundException e) {
|
} 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)) {
|
} 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();
|
socket.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getDateString(Date d){
|
private String getDateHeader(Date d){
|
||||||
return "Date: " + d.toString() + CRLF;
|
return "Date: " + d.toString() + CRLF;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String createHeader(Date d, File f){
|
private String createHeader(Date d, File f){
|
||||||
String response = RESPONSE_200;
|
String response = HTTP_RESPONSE.OK;
|
||||||
response += getDateString(d);
|
response += getDateHeader(d);
|
||||||
response += "LOCATION: " + f.getName() +CRLF;
|
|
||||||
response += "Server: Labbserver" + CRLF;
|
response += "Server: Labbserver" + CRLF;
|
||||||
response += "Allow: " + HTTP_METHOD.GET + " " + HTTP_METHOD.HEAD+CRLF;
|
response += "Allow: " + HTTP_METHOD.GET + " " + HTTP_METHOD.HEAD+CRLF;
|
||||||
response += "Content-Length: " + f.length() + CRLF;
|
response += "Content-Length: " + f.length() + CRLF;
|
||||||
|
@ -175,16 +208,16 @@ final class HttpRequest implements Runnable
|
||||||
|
|
||||||
private static String contentType(String fileName)
|
private static String contentType(String fileName)
|
||||||
{
|
{
|
||||||
if (fileName.toLowerCase().endsWith(".htm") ||
|
if (fileName.toLowerCase().endsWith(".htm") || fileName.toLowerCase().endsWith(".html")) {
|
||||||
fileName.toLowerCase().endsWith(".html")) {
|
|
||||||
return "text/html";
|
return "text/html";
|
||||||
} else if (fileName.toLowerCase().endsWith(".gif")) {
|
} else if (fileName.toLowerCase().endsWith(".gif")) {
|
||||||
return "image/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";
|
return "image/jpeg";
|
||||||
} else {
|
} else {
|
||||||
return "application/octet-stream";
|
return "application/octet-stream";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue