mirror of
https://github.com/webbukkit/dynmap.git
synced 2024-12-25 10:07:37 +01:00
Some improvements to HttpServer.
This commit is contained in:
parent
2380325975
commit
835e566151
@ -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");
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user