Fixed HttpServer some more.

This commit is contained in:
FrozenCow 2011-02-13 22:30:24 +01:00
parent 9031181c6f
commit 440412254b

View file

@ -81,9 +81,10 @@ public class HttpServerConnection extends Thread {
public void run() { public void run() {
try { try {
socket.setSoTimeout(5000); socket.setSoTimeout(5000);
while (true) {
HttpRequest request = new HttpRequest(); HttpRequest request = new HttpRequest();
if (!readRequestHeader(socket.getInputStream(), request)) { InputStream in = socket.getInputStream();
if (!readRequestHeader(in, request)) {
socket.close(); socket.close();
return; return;
} }
@ -107,7 +108,8 @@ public class HttpServerConnection extends Thread {
return; return;
} }
HttpResponse response = new HttpResponse(socket.getOutputStream()); OutputStream out = socket.getOutputStream();
HttpResponse response = new HttpResponse(out);
try { try {
handler.handle(relativePath, request, response); handler.handle(relativePath, request, response);
@ -116,28 +118,35 @@ public class HttpServerConnection extends Thread {
} catch (Exception e) { } catch (Exception e) {
log.log(Level.SEVERE, "HttpHandler '" + handler + "' has thown an exception", e); log.log(Level.SEVERE, "HttpHandler '" + handler + "' has thown an exception", e);
if (socket != null) { if (socket != null) {
out.flush();
socket.close(); socket.close();
} }
return; 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"); 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(); socket.close();
return; return;
} }
}
if (connection != null && connection.equals("close")) {
out.flush();
socket.close();
return;
}
out.flush();
}
} catch (IOException e) { } catch (IOException e) {
if (socket != null) { if (socket != null) {
try { try {
@ -145,6 +154,7 @@ public class HttpServerConnection extends Thread {
} catch (IOException ex) { } catch (IOException ex) {
} }
} }
return;
} catch (Exception e) { } catch (Exception e) {
if (socket != null) { if (socket != null) {
try { try {
@ -154,6 +164,7 @@ public class HttpServerConnection extends Thread {
} }
log.log(Level.SEVERE, "Exception while handling request: ", e); log.log(Level.SEVERE, "Exception while handling request: ", e);
e.printStackTrace(); e.printStackTrace();
return;
} }
} }
} }