Some improvements to HttpServer.

This commit is contained in:
FrozenCow 2011-02-13 02:56:00 +01:00
parent 2380325975
commit 835e566151
4 changed files with 26 additions and 6 deletions

View File

@ -13,6 +13,8 @@ import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.dynmap.debug.Debug;
public class HttpServerConnection extends Thread {
protected static final Logger log = Logger.getLogger("Minecraft");
@ -121,7 +123,14 @@ public class HttpServerConnection extends Thread {
if (response.fields.get("Content-Length") == null) {
response.fields.put("Content-Length", "0");
/* OutputStream out = */response.getBody();
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");

View File

@ -30,7 +30,7 @@ public abstract class FileHandler implements HttpHandler {
return "application/octet-steam";
}
protected abstract InputStream getFileInput(String path);
protected abstract InputStream getFileInput(String path, HttpRequest request, HttpResponse response);
protected String getExtension(String path) {
int dotindex = path.lastIndexOf('.');
@ -60,10 +60,12 @@ public abstract class FileHandler implements HttpHandler {
InputStream fileInput = null;
try {
path = formatPath(path);
fileInput = getFileInput(path);
fileInput = getFileInput(path, request, response);
if (fileInput == null) {
response.statusCode = 404;
response.statusMessage = "Not found";
response.fields.put("Content-Length", "0");
response.getBody();
return;
}

View File

@ -5,6 +5,9 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.dynmap.web.HttpRequest;
import org.dynmap.web.HttpResponse;
public class FilesystemHandler extends FileHandler {
private File root;
@ -14,14 +17,17 @@ public class FilesystemHandler extends FileHandler {
this.root = root;
}
@Override
protected InputStream getFileInput(String path) {
protected InputStream getFileInput(String path, HttpRequest request, HttpResponse response) {
File file = new File(root, path);
if (file.getAbsolutePath().startsWith(root.getAbsolutePath()) && file.isFile()) {
FileInputStream result;
try {
return new FileInputStream(file);
result = new FileInputStream(file);
} catch (FileNotFoundException e) {
return null;
}
response.fields.put("Content-Length", Long.toString(file.length()));
return result;
}
return null;
}

View File

@ -2,6 +2,9 @@ package org.dynmap.web.handlers;
import java.io.InputStream;
import org.dynmap.web.HttpRequest;
import org.dynmap.web.HttpResponse;
public class JarFileHandler extends FileHandler {
private String root;
@ -10,7 +13,7 @@ public class JarFileHandler extends FileHandler {
this.root = root;
}
@Override
protected InputStream getFileInput(String path) {
protected InputStream getFileInput(String path, HttpRequest request, HttpResponse response) {
return this.getClass().getResourceAsStream(root + "/" + path);
}
}