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

@ -44,147 +44,180 @@ public final class WebServer
final class HttpRequest implements Runnable final class HttpRequest implements Runnable
{ {
// Constants // Constants
// Recognized HTTP methods final static String HTTP_VERSION = "HTTP/1.0";
final static class HTTP_METHOD final static String CRLF = "\r\n";
{
final static String GET = "GET";
final static String HEAD = "HEAD";
final static String POST = "POST";
}
final static String HTTPVERSION = "HTTP/1.0"; // Recognized HTTP methods
final static String CRLF = "\r\n"; final static class HTTP_METHOD
final static String RESPONSE_200 = HTTPVERSION + " 200 OK " + CRLF; {
final static String RESPONSE_404 = HTTPVERSION + " 404 Not Found " + CRLF; final static String GET = "GET";
final static String RESPONSE_400 = HTTPVERSION + " 400 Bad Request " + CRLF; final static String HEAD = "HEAD";
final static String RESPONSE_501 = HTTPVERSION + " 501 Not Implemented " + CRLF; final static String POST = "POST";
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; Socket socket;
// Constructor // Constructor
public HttpRequest(Socket socket) throws Exception public HttpRequest(Socket socket) throws Exception
{ {
this.socket = socket; this.socket = socket;
} }
// Implements the run() method of the Runnable interface // Implements the run() method of the Runnable interface
public void run() public void run()
{ {
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);
}
} }
} }
// Process a HTTP request // Process a HTTP request
private void processRequest() throws Exception private void processRequest() throws Exception
{ {
// Get the input and output streams of the socket. // Get the input and output streams of the socket.
InputStream ins = socket.getInputStream(); InputStream ins = socket.getInputStream();
DataOutputStream outs = new DataOutputStream(socket.getOutputStream()); DataOutputStream outs = new DataOutputStream(socket.getOutputStream());
// Set up input stream filters // Set up input stream filters
BufferedReader br = new BufferedReader(new InputStreamReader(ins)); BufferedReader br = new BufferedReader(new InputStreamReader(ins));
Date d = new Date(); Date d = new Date();
// Get the request line of the HTTP request // Get the request line of the HTTP request
String requestLine = br.readLine(); String requestLine = br.readLine();
// Display the request line // Display the request line
System.out.println(); System.out.println();
System.out.println("Request:"); System.out.println("Request:");
System.out.println(" " + requestLine); System.out.println(" " + requestLine);
String[] tokens = requestLine.split("\\s+"); String[] tokens = requestLine.split("\\s+");
//System.out.println("-->"+tokens.length); //System.out.println("-->"+tokens.length);
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)) {
FileInputStream filein; FileInputStream filein;
try { try {
File f = new File("." +tokens[1]); File f = new File("." +tokens[1]);
filein = new FileInputStream(f); filein = new FileInputStream(f);
String response = createHeader(d,f); String response = createHeader(d,f);
outs.writeChars(response); outs.writeChars(response);
if(Request.equals(HTTP_METHOD.GET)) { if(Request.equals(HTTP_METHOD.GET)) {
sendBytes(filein, outs); sendBytes(filein, outs);
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
outs.writeChars(RESPONSE_404 + getDateString(d));
} outs.writeChars(HTTP_RESPONSE.NOT_FOUND + getDateHeader(d));
outs.writeChars(CRLF);
} else if(Request.equals(HTTP_METHOD.POST)){ outs.writeChars("<h1>404 Not Found</h1>");
outs.writeChars(RESPONSE_501 + getDateString(d));
}
else {
outs.writeChars(RESPONSE_400 + getDateString(d));
}
// Close streams and sockets
outs.close();
br.close();
socket.close();
}
private String getDateString(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;
response += "Server: Labbserver" + CRLF;
response += "Allow: " + HTTP_METHOD.GET + " " + HTTP_METHOD.HEAD+CRLF;
response += "Content-Length: " + f.length() +CRLF;
response += "Content-Type: " + contentType(f.getName()) +CRLF;
response += "Last-Modified: " + new Date(f.lastModified()).toString() +CRLF;
return response;
}
private static void sendBytes(FileInputStream fins, OutputStream outs) throws Exception
{
// Coopy buffer
byte[] buffer = new byte[1024];
int bytes = 0;
while ((bytes = fins.read(buffer)) != -1) {
outs.write(buffer, 0, bytes);
}
} }
} else if(Request.equals(HTTP_METHOD.POST)) {
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>");
}
// Close streams and sockets
outs.close();
br.close();
socket.close();
}
private static String contentType(String fileName) private String getDateHeader(Date d){
{ return "Date: " + d.toString() + CRLF;
if (fileName.toLowerCase().endsWith(".htm") || }
fileName.toLowerCase().endsWith(".html")) {
return "text/html"; private String createHeader(Date d, File f){
} else if (fileName.toLowerCase().endsWith(".gif")) { String response = HTTP_RESPONSE.OK;
return "image/gif"; response += getDateHeader(d);
} else if (fileName.toLowerCase().endsWith(".jpg")) { response += "Server: Labbserver" + CRLF;
return "image/jpeg"; response += "Allow: " + HTTP_METHOD.GET + " " + HTTP_METHOD.HEAD+CRLF;
} else { response += "Content-Length: " + f.length() + CRLF;
return "application/octet-stream"; response += "Content-Type: " + contentType(f.getName()) + CRLF;
} response += "Last-Modified: " + new Date(f.lastModified()).toString() + CRLF;
} return response;
}
private static void sendBytes(FileInputStream fins, OutputStream outs) throws Exception
{
// Coopy buffer
byte[] buffer = new byte[1024];
int bytes = 0;
while ((bytes = fins.read(buffer)) != -1) {
outs.write(buffer, 0, bytes);
}
}
private static String contentType(String fileName)
{
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(".png")) {
return "image/png";
} else if (fileName.toLowerCase().endsWith(".jpg") || fileName.toLowerCase().endsWith(".jpeg")) {
return "image/jpeg";
} else {
return "application/octet-stream";
}
}
} }