Expanded internal webserver to also handle web and tiles, where web-files can be retrieved from filesystem or jar. Also some debugging changes.
This commit is contained in:
parent
b4fca31cd5
commit
6a7559f3ad
11 changed files with 247 additions and 84 deletions
|
|
@ -2,97 +2,85 @@ package org.dynmap;
|
|||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.Socket;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
import org.bukkit.*;
|
||||
import org.dynmap.debug.Debugger;
|
||||
|
||||
public class WebServerRequest extends Thread {
|
||||
protected static final Logger log = Logger.getLogger("Minecraft");
|
||||
|
||||
private Socket sock;
|
||||
private Debugger debugger;
|
||||
private Socket socket;
|
||||
private MapManager mgr;
|
||||
private DynmapPlugin etc;
|
||||
private Server server;
|
||||
|
||||
public WebServerRequest(Socket socket, MapManager mgr)
|
||||
public WebServerRequest(Socket socket, MapManager mgr, Server server, Debugger debugger)
|
||||
{
|
||||
sock = socket;
|
||||
this.debugger = debugger;
|
||||
this.socket = socket;
|
||||
this.mgr = mgr;
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
private static void sendHeader(BufferedOutputStream out, int code, String contentType, long contentLength, long lastModified) throws IOException
|
||||
{
|
||||
out.write(("HTTP/1.0 " + code + " OK\r\n" +
|
||||
"Date: " + new Date().toString() + "\r\n" +
|
||||
"Server: JibbleWebServer/1.0\r\n" +
|
||||
"Content-Type: " + contentType + "\r\n" +
|
||||
"Expires: Thu, 01 Dec 1994 16:00:00 GMT\r\n" +
|
||||
((contentLength != -1) ? "Content-Length: " + contentLength + "\r\n" : "") +
|
||||
"Last-modified: " + new Date(lastModified).toString() + "\r\n" +
|
||||
"\r\n").getBytes());
|
||||
|
||||
private static void writeHttpHeader(BufferedOutputStream out, int statusCode, String statusText) throws IOException {
|
||||
out.write("HTTP/1.0 ".getBytes());
|
||||
out.write(Integer.toString(statusCode).getBytes());
|
||||
out.write((" " + statusText + "\r\n").getBytes());
|
||||
}
|
||||
|
||||
private static void sendError(BufferedOutputStream out, int code, String message) throws IOException
|
||||
{
|
||||
message = message + "<hr>" + WebServer.VERSION;
|
||||
sendHeader(out, code, "text/html", message.length(), System.currentTimeMillis());
|
||||
out.write(message.getBytes());
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
private static void writeHeaderField(BufferedOutputStream out, String name, String value) throws IOException {
|
||||
out.write(name.getBytes());
|
||||
out.write((int)':');
|
||||
out.write((int)' ');
|
||||
out.write(value.getBytes());
|
||||
out.write(13);
|
||||
out.write(10);
|
||||
}
|
||||
|
||||
private static void writeEndOfHeaders(BufferedOutputStream out) throws IOException {
|
||||
out.write(13);
|
||||
out.write(10);
|
||||
}
|
||||
|
||||
public void run()
|
||||
{
|
||||
InputStream reader = null;
|
||||
try {
|
||||
sock.setSoTimeout(30000);
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
|
||||
BufferedOutputStream out = new BufferedOutputStream(sock.getOutputStream());
|
||||
socket.setSoTimeout(30000);
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
|
||||
|
||||
String request = in.readLine();
|
||||
if (request == null || !request.startsWith("GET ") || !(request.endsWith(" HTTP/1.0") || request.endsWith("HTTP/1.1"))) {
|
||||
// Invalid request type (no "GET")
|
||||
sendError(out, 500, "Invalid Method.");
|
||||
writeHttpHeader(out, 500, "Invalid Method.");
|
||||
writeEndOfHeaders(out);
|
||||
return;
|
||||
}
|
||||
|
||||
String path = request.substring(4, request.length() - 9);
|
||||
|
||||
int current = (int) (System.currentTimeMillis() / 1000);
|
||||
long cutoff = 0;
|
||||
|
||||
if(path.charAt(0) == '/') {
|
||||
try {
|
||||
cutoff = ((long) Integer.parseInt(path.substring(1))) * 1000;
|
||||
} catch(NumberFormatException e) {
|
||||
debugger.debug("request: " + path);
|
||||
if (path.startsWith("/up/")) {
|
||||
handleUp(out, path.substring(3));
|
||||
} else if (path.startsWith("/tiles/")) {
|
||||
handleMapToDirectory(out, path.substring(6), "/srv/http/dynmap/tiles/");
|
||||
} else if (path.startsWith("/")) {
|
||||
if(mgr.webPath.equals("[JAR]")) {
|
||||
handleMapToJar(out, path);
|
||||
} else if(mgr.webPath.length() > 0) {
|
||||
handleMapToDirectory(out, path, mgr.webPath);
|
||||
}
|
||||
}
|
||||
|
||||
sendHeader(out, 200, "text/plain", -1, System.currentTimeMillis());
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
long relativeTime = etc.getServer().getTime() % 24000;
|
||||
sb.append(current + " " + relativeTime + "\n");
|
||||
|
||||
for(Player player : etc.getServer().getOnlinePlayers()) {
|
||||
sb.append(player.getName() + " player " + player.getLocation().getX() + " " + player.getLocation().getY() + " " + player.getLocation().getZ() + "\n");
|
||||
}
|
||||
|
||||
synchronized(mgr.lock) {
|
||||
for(TileUpdate tu : mgr.tileUpdates) {
|
||||
if(tu.at >= cutoff) {
|
||||
sb.append(tu.tile.px + "_" + tu.tile.py + " " + tu.tile.zpx + "_" + tu.tile.zpy + " t\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out.write(sb.toString().getBytes());
|
||||
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
|
@ -107,4 +95,123 @@ public class WebServerRequest extends Thread {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void handleUp(BufferedOutputStream out, String path) throws IOException {
|
||||
int current = (int) (System.currentTimeMillis() / 1000);
|
||||
long cutoff = 0;
|
||||
|
||||
if(path.charAt(0) == '/') {
|
||||
try {
|
||||
cutoff = ((long) Integer.parseInt(path.substring(1))) * 1000;
|
||||
} catch(NumberFormatException e) {
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
long relativeTime = server.getTime() % 24000;
|
||||
sb.append(current + " " + relativeTime + "\n");
|
||||
|
||||
for(Player player : server.getOnlinePlayers()) {
|
||||
sb.append(player.getName() + " player " + player.getLocation().getX() + " " + player.getLocation().getY() + " " + player.getLocation().getZ() + "\n");
|
||||
}
|
||||
|
||||
synchronized(mgr.lock) {
|
||||
for(TileUpdate tu : mgr.tileUpdates) {
|
||||
if(tu.at >= cutoff) {
|
||||
sb.append(tu.tile.px + "_" + tu.tile.py + " " + tu.tile.zpx + "_" + tu.tile.zpy + " t\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
byte[] bytes = sb.toString().getBytes();
|
||||
|
||||
String dateStr = new Date().toString();
|
||||
writeHttpHeader(out, 200, "OK");
|
||||
writeHeaderField(out, "Date", dateStr);
|
||||
writeHeaderField(out, "Content-Type", "text/plain");
|
||||
writeHeaderField(out, "Expires", "Thu, 01 Dec 1994 16:00:00 GMT");
|
||||
writeHeaderField(out, "Last-modified", dateStr);
|
||||
writeHeaderField(out, "Content-Length", Integer.toString(bytes.length));
|
||||
writeEndOfHeaders(out);
|
||||
out.write(bytes);
|
||||
}
|
||||
|
||||
private byte[] readBuffer = new byte[40960];
|
||||
|
||||
public void writeFile(BufferedOutputStream out, String path, InputStream fileInput) throws IOException {
|
||||
int dotindex = path.lastIndexOf('.');
|
||||
String extension = null;
|
||||
if (dotindex > 0) extension = path.substring(dotindex);
|
||||
|
||||
writeHttpHeader(out, 200, "OK");
|
||||
writeHeaderField(out, "Content-Type", getMimeTypeFromExtension(extension));
|
||||
writeHeaderField(out, "Connection", "close");
|
||||
writeEndOfHeaders(out);
|
||||
try {
|
||||
int readBytes;
|
||||
while((readBytes = fileInput.read(readBuffer)) > 0) {
|
||||
out.write(readBuffer, 0, readBytes);
|
||||
}
|
||||
} catch(IOException e) {
|
||||
fileInput.close();
|
||||
throw e;
|
||||
}
|
||||
fileInput.close();
|
||||
}
|
||||
|
||||
public String getFilePath(String path) {
|
||||
int qmark = path.indexOf('?');
|
||||
if (qmark >= 0) path = path.substring(0, qmark);
|
||||
path = path.substring(1);
|
||||
|
||||
if (path.startsWith("/") || path.startsWith("."))
|
||||
return null;
|
||||
if (path.length() == 0) path = "index.html";
|
||||
return path;
|
||||
}
|
||||
|
||||
public void handleMapToJar(BufferedOutputStream out, String path) throws IOException {
|
||||
path = getFilePath(path);
|
||||
if (path != null) {
|
||||
InputStream s = this.getClass().getResourceAsStream("/web/" + path);
|
||||
if (s != null) {
|
||||
writeFile(out, path, s);
|
||||
return;
|
||||
}
|
||||
}
|
||||
writeHttpHeader(out, 404, "Not found");
|
||||
writeEndOfHeaders(out);
|
||||
}
|
||||
|
||||
public void handleMapToDirectory(BufferedOutputStream out, String path, String directoryPath) throws IOException {
|
||||
path = getFilePath(path);
|
||||
if (path != null) {
|
||||
File tilesDirectory = new File(directoryPath);
|
||||
File tileFile = new File(tilesDirectory, path);
|
||||
|
||||
if (tileFile.getAbsolutePath().startsWith(tilesDirectory.getAbsolutePath()) && tileFile.isFile()) {
|
||||
FileInputStream s = new FileInputStream(tileFile);
|
||||
writeFile(out, path, s);
|
||||
return;
|
||||
}
|
||||
}
|
||||
writeHttpHeader(out, 404, "Not found");
|
||||
writeEndOfHeaders(out);
|
||||
}
|
||||
|
||||
private static Map<String, String> mimes = new HashMap<String, String>();
|
||||
static {
|
||||
mimes.put(".html", "text/html");
|
||||
mimes.put(".htm", "text/html");
|
||||
mimes.put(".js", "text/javascript");
|
||||
mimes.put(".png", "image/png");
|
||||
mimes.put(".css", "text/css");
|
||||
mimes.put(".txt", "text/plain");
|
||||
}
|
||||
public static String getMimeTypeFromExtension(String extension) {
|
||||
String m = mimes.get(extension);
|
||||
if (m != null) return m;
|
||||
return "application/octet-steam";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue