From 4bef1a06a483c262512dc51b3732eb566f98b017 Mon Sep 17 00:00:00 2001 From: Steven Lawson Date: Tue, 3 Sep 2013 17:47:42 -0400 Subject: [PATCH] More http server tinkering. --- .../TotalFreedomMod/HTTPD/Module_help.java | 10 +- .../HTTPD/Module_schematic.java | 40 +++++--- .../HTTPD/TFM_HTTPD_Manager.java | 92 ++++++++++--------- 3 files changed, 80 insertions(+), 62 deletions(-) diff --git a/src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_help.java b/src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_help.java index 1508d410..2239148e 100644 --- a/src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_help.java +++ b/src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_help.java @@ -149,9 +149,9 @@ public class Module_help extends TFM_HTTPD_Module return ".commandName{font-weight:bold;}.commandDescription{padding-left:15px;}li{margin:.15em;padding:.15em;}"; } - @Override - public String getScript() - { - return "$(document).ready(function(){console.log(\"Ready\");});"; - } +// @Override +// public String getScript() +// { +// return "$(document).ready(function(){console.log(\"Ready\");});"; +// } } diff --git a/src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_schematic.java b/src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_schematic.java index 0e3e9354..9f5a1ab2 100644 --- a/src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_schematic.java +++ b/src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_schematic.java @@ -40,6 +40,11 @@ public class Module_schematic extends TFM_HTTPD_Module @Override public String getBody() { + if (!SCHEMATIC_FOLDER.exists()) + { + return HTMLGenerationTools.paragraph("Can't find WorldEdit schematic folder."); + } + final StringBuilder out = new StringBuilder(); final String[] args = StringUtils.split(uri, "/"); @@ -119,29 +124,36 @@ public class Module_schematic extends TFM_HTTPD_Module final File tempFile = new File(tempFileName); if (tempFile.exists()) { - if (SCHEMATIC_FILENAME.matcher(origFileName).find()) + if (tempFile.length() <= FileUtils.ONE_KB * 64L) { - final File targetFile = new File(SCHEMATIC_FOLDER.getPath(), origFileName); - if (targetFile.exists()) + if (SCHEMATIC_FILENAME.matcher(origFileName).find()) { - throw new SchematicUploadException("Schematic exists on the server already."); + final File targetFile = new File(SCHEMATIC_FOLDER.getPath(), origFileName); + if (targetFile.exists()) + { + throw new SchematicUploadException("Schematic exists on the server already."); + } + else + { + try + { + FileUtils.copyFile(tempFile, targetFile); + } + catch (IOException ex) + { + TFM_Log.severe(ex); + throw new SchematicUploadException(); + } + } } else { - try - { - FileUtils.copyFile(tempFile, targetFile); - } - catch (IOException ex) - { - TFM_Log.severe(ex); - throw new SchematicUploadException(); - } + throw new SchematicUploadException("File name must be alphanumeric with a \".schematic\" extension."); } } else { - throw new SchematicUploadException("File name must be alphanumeric with a \".schematic\" extension."); + throw new SchematicUploadException("Schematic is too big (64kb max)."); } } else diff --git a/src/me/StevenLawson/TotalFreedomMod/HTTPD/TFM_HTTPD_Manager.java b/src/me/StevenLawson/TotalFreedomMod/HTTPD/TFM_HTTPD_Manager.java index 49626e92..71027645 100644 --- a/src/me/StevenLawson/TotalFreedomMod/HTTPD/TFM_HTTPD_Manager.java +++ b/src/me/StevenLawson/TotalFreedomMod/HTTPD/TFM_HTTPD_Manager.java @@ -9,12 +9,12 @@ import java.util.concurrent.Callable; import java.util.concurrent.Future; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static me.StevenLawson.TotalFreedomMod.HTTPD.NanoHTTPD.MIME_PLAINTEXT; import me.StevenLawson.TotalFreedomMod.HTTPD.NanoHTTPD.Response; import me.StevenLawson.TotalFreedomMod.TFM_ConfigEntry; import me.StevenLawson.TotalFreedomMod.TFM_Log; import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.exception.ExceptionUtils; import org.bukkit.Bukkit; public class TFM_HTTPD_Manager @@ -129,61 +129,67 @@ public class TFM_HTTPD_Manager { Response response = null; - final String[] args = StringUtils.split(uri, "/"); - final ModuleType moduleType = args.length >= 1 ? ModuleType.getByName(args[0]) : ModuleType.FILE; - - if (moduleType.isRunOnBukkitThread()) + try { - Future responseCall = Bukkit.getScheduler().callSyncMethod(TotalFreedomMod.plugin, new Callable() - { - @Override - public Response call() throws Exception - { - switch (moduleType) - { - case HELP: - return new Module_help(uri, method, headers, params, files, socket).getResponse(); - case LIST: - return new Module_list(uri, method, headers, params, files, socket).getResponse(); - default: - return null; - } - } - }); - try + final String[] args = StringUtils.split(uri, "/"); + final ModuleType moduleType = args.length >= 1 ? ModuleType.getByName(args[0]) : ModuleType.FILE; + + if (moduleType.isRunOnBukkitThread()) { - response = responseCall.get(); + Future responseCall = Bukkit.getScheduler().callSyncMethod(TotalFreedomMod.plugin, new Callable() + { + @Override + public Response call() throws Exception + { + switch (moduleType) + { + case HELP: + return new Module_help(uri, method, headers, params, files, socket).getResponse(); + case LIST: + return new Module_list(uri, method, headers, params, files, socket).getResponse(); + default: + return null; + } + } + }); + + try + { + response = responseCall.get(); + } + catch (Exception ex) + { + TFM_Log.severe(ex); + } } - catch (Exception ex) + else { - TFM_Log.severe(ex); + switch (moduleType) + { + case DUMP: + //response = new Module_dump(uri, method, headers, params, files, socket).getResponse(); + response = new Response(Response.Status.OK, MIME_PLAINTEXT, "The DUMP module is disabled. It is intended for debugging use only."); + break; + case SCHEMATIC: + response = new Module_schematic(uri, method, headers, params, files, socket).getResponse(); + break; + default: + response = new Module_file(uri, method, headers, params, files, socket).getResponse(); + } } } - else + catch (Exception ex) { - switch (moduleType) - { - case DUMP: - //response = new Module_dump(uri, method, headers, params, files, socket).getResponse(); - response = new Response(Response.Status.OK, MIME_PLAINTEXT, "The DUMP module is disabled. It is intended for debugging use only."); - break; - case SCHEMATIC: - response = new Module_schematic(uri, method, headers, params, files, socket).getResponse(); - break; - default: - response = new Module_file(uri, method, headers, params, files, socket).getResponse(); - } + response = new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "Error 500: Internal Server Error\r\n" + ex.getMessage() + "\r\n" + ExceptionUtils.getFullStackTrace(ex)); } if (response == null) { - return new Response(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Error 404: Not Found - The requested resource was not found on this server."); - } - else - { - return response; + response = new Response(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Error 404: Not Found - The requested resource was not found on this server."); } + + return response; } }