Fixed HttpServer some more.

This commit is contained in:
FrozenCow 2011-02-13 22:30:24 +01:00
parent 835e566151
commit 9a2b9ccac0

View File

@ -81,9 +81,10 @@ public class HttpServerConnection extends Thread {
public void run() {
try {
socket.setSoTimeout(5000);
while (true) {
HttpRequest request = new HttpRequest();
if (!readRequestHeader(socket.getInputStream(), request)) {
InputStream in = socket.getInputStream();
if (!readRequestHeader(in, request)) {
socket.close();
return;
}
@ -107,7 +108,8 @@ public class HttpServerConnection extends Thread {
return;
}
HttpResponse response = new HttpResponse(socket.getOutputStream());
OutputStream out = socket.getOutputStream();
HttpResponse response = new HttpResponse(out);
try {
handler.handle(relativePath, request, response);
@ -116,28 +118,35 @@ public class HttpServerConnection extends Thread {
} catch (Exception e) {
log.log(Level.SEVERE, "HttpHandler '" + handler + "' has thown an exception", e);
if (socket != null) {
out.flush();
socket.close();
}
return;
}
if (response.fields.get("Content-Length") == null) {
response.fields.put("Content-Length", "0");
OutputStream out = response.getBody();
// The HttpHandler has already send the headers and written to the body without setting the Content-Length.
if (out == null) {
Debug.debug("Response was not cleanly handled by '" + handler + "' for path '" + request.path + "'");
socket.close();
return;
}
}
String connection = response.fields.get("Connection");
if (connection != null && connection.equals("close")) {
String contentLength = response.fields.get("Content-Length");
if (contentLength == null && connection == null) {
response.fields.put("Content-Length", "0");
OutputStream responseBody = response.getBody();
// The HttpHandler has already send the headers and written to the body without setting the Content-Length.
if (responseBody == null) {
Debug.debug("Response was given without Content-Length by '" + handler + "' for path '" + request.path + "'.");
out.flush();
socket.close();
return;
}
}
if (connection != null && connection.equals("close")) {
out.flush();
socket.close();
return;
}
out.flush();
}
} catch (IOException e) {
if (socket != null) {
try {
@ -145,6 +154,7 @@ public class HttpServerConnection extends Thread {
} catch (IOException ex) {
}
}
return;
} catch (Exception e) {
if (socket != null) {
try {
@ -154,6 +164,7 @@ public class HttpServerConnection extends Thread {
}
log.log(Level.SEVERE, "Exception while handling request: ", e);
e.printStackTrace();
return;
}
}
}