changed tabs to spaces + little cleanup

This commit is contained in:
Jeena Paradies 2010-11-04 11:30:24 +01:00
parent d5f79cb746
commit 7de5c549ca

View file

@ -23,6 +23,7 @@ public final class WebServer
// Wait for and process HTTP service requests
while (true) {
// Wait for TCP connection
Socket requestSocket = serverSocket.accept();
requestSocket.setSoLinger(true, 5);
@ -54,11 +55,11 @@ final class HttpRequest implements Runnable
final static String HTTPVERSION = "HTTP/1.0";
final static String CRLF = "\r\n";
final static String Response200 = HTTPVERSION + " 200 OK "+CRLF;
final static String Response404 = HTTPVERSION + " 404 Not Found "+CRLF;
final static String Response400 = HTTPVERSION + " 400 Bad Request "+CRLF;
final static String Response501 = HTTPVERSION + " 501 Not Implemented "+CRLF;
final static String Location = " datalabb "+CRLF;
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;
Socket socket;
@ -99,33 +100,44 @@ final class HttpRequest implements Runnable
String[] tokens = requestLine.split("\\s+");
//System.out.println("-->"+tokens.length);
String Request = tokens[0];
if(tokens.length != 3) {
System.out.println("Wrong number of arguments in request!");
outs.writeChars(Response400+Location+getDateString(d));
}
else if(tokens[1].charAt(0) != '/'){
outs.writeChars(RESPONSE_400 + LOCATION + getDateString(d));
} else if(tokens[1].charAt(0) != '/') {
System.out.println("illegal url");
outs.writeChars(Response501+Location+getDateString(d));
}
else if(Request.equals(HTTP_METHOD.GET) || Request.equals(HTTP_METHOD.HEAD)){
outs.writeChars(RESPONSE_501 + LOCATION + getDateString(d));
} else if(Request.equals(HTTP_METHOD.GET) || Request.equals(HTTP_METHOD.HEAD)) {
FileInputStream filein;
try {
File f = new File("." +tokens[1]);
filein = new FileInputStream(f);
String response = createHeader(d,f);
outs.writeChars(response);
if(Request.equals(HTTP_METHOD.GET))
if(Request.equals(HTTP_METHOD.GET)) {
sendBytes(filein, outs);
}
catch (FileNotFoundException e){
outs.writeChars(Response404+getDateString(d));
} catch (FileNotFoundException e) {
outs.writeChars(RESPONSE_404 + getDateString(d));
}
} else if(Request.equals(HTTP_METHOD.POST)){
outs.writeChars(Response501+getDateString(d));
outs.writeChars(RESPONSE_501 + getDateString(d));
}
else {
outs.writeChars(RESPONSE_400 + getDateString(d));
}
else
outs.writeChars(Response400+getDateString(d));
// Close streams and sockets
@ -139,19 +151,18 @@ final class HttpRequest implements Runnable
}
private String createHeader(Date d, File f){
String response = Response200;
String response = RESPONSE_200;
response += getDateString(d);
response += "Location: " + f.getName() +CRLF;
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
private static void sendBytes(FileInputStream fins, OutputStream outs) throws Exception
{
// Coopy buffer
byte[] buffer = new byte[1024];