Some improvements to HttpServer.

This commit is contained in:
FrozenCow 2011-02-13 02:56:00 +01:00
parent 80f2942e1d
commit 9031181c6f
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.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.dynmap.debug.Debug;
public class HttpServerConnection extends Thread { public class HttpServerConnection extends Thread {
protected static final Logger log = Logger.getLogger("Minecraft"); protected static final Logger log = Logger.getLogger("Minecraft");
@ -121,7 +123,14 @@ public class HttpServerConnection extends Thread {
if (response.fields.get("Content-Length") == null) { if (response.fields.get("Content-Length") == null) {
response.fields.put("Content-Length", "0"); 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"); String connection = response.fields.get("Connection");

View file

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

View file

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

View file

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