diff --git a/README.md b/README.md index e31d220..834d26a 100644 --- a/README.md +++ b/README.md @@ -9,5 +9,6 @@ Builds: https://ci.citizensnpcs.co/job/Webizen/ Not actively maintained beyond bare minimum, but should function fine. ## Simple tutorial by BlackCoyote +This is a good example of usage, however tag documentation is out of date. https://docs.google.com/document/d/11E1asJ92cpsTG1I8nUvFnfgv_duVgul8U1f2_59PBlA diff --git a/src/main/java/space/morphanone/webizen/commands/WebCommand.java b/src/main/java/space/morphanone/webizen/commands/WebCommand.java index 83159fd..204b354 100644 --- a/src/main/java/space/morphanone/webizen/commands/WebCommand.java +++ b/src/main/java/space/morphanone/webizen/commands/WebCommand.java @@ -24,21 +24,21 @@ public WebCommand() { // @Syntax web [start/stop] (port:<#>) // @Required 1 // @Maximum 2 - // @Short Manages the web server ran on your minecraft server + // @Short Manages the web server ran on your minecraft server. // @group addons // // @Description - // Lets you start or stop a web server - // Only one web server can run on the server at once - // The default port when the port argument is not specified, is 80 + // Lets you start or stop a web server. + // Only one web server can be run at a time. + // The default port when the port argument is not specified, is 80. // TODO: Document command details! // // @Usage - // Use to start the web server on port 10123 + // Use to start the web server on port 10123. // - web start port:10123 // // @Usage - // Use to stop the web server + // Use to stop the web server. // - web stop // // @Plugin Webizen diff --git a/src/main/java/space/morphanone/webizen/events/BasicRequestScriptEvent.java b/src/main/java/space/morphanone/webizen/events/BasicRequestScriptEvent.java index 70ea3b7..e1a9277 100644 --- a/src/main/java/space/morphanone/webizen/events/BasicRequestScriptEvent.java +++ b/src/main/java/space/morphanone/webizen/events/BasicRequestScriptEvent.java @@ -1,6 +1,8 @@ package space.morphanone.webizen.events; import com.denizenscript.denizen.utilities.Utilities; +import com.denizenscript.denizencore.objects.core.MapTag; +import com.denizenscript.denizencore.utilities.text.StringHolder; import com.sun.net.httpserver.HttpExchange; import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData; import com.denizenscript.denizen.tags.BukkitTagContext; @@ -18,11 +20,13 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.StandardCharsets; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -44,7 +48,7 @@ public class ResponseOptions { private String lowerRequestType = CoreUtilities.toLowerCase(requestType); private static final Pattern tagPattern = Pattern.compile("<\\{([^\\}]*)\\}>"); - private static final CharsetDecoder utfDecoder = Charset.forName("UTF-8").newDecoder(); + private static final CharsetDecoder utfDecoder = StandardCharsets.UTF_8.newDecoder(); private static final FakeScriptEntry reusableScriptEntry = FakeScriptEntry.generate(); private static final BukkitTagContext reusableTagContext = new BukkitTagContext(reusableScriptEntry); @@ -81,13 +85,7 @@ else if (scriptResponse.parseFile.asBoolean()) { String parsed = TagManager.readSingleTag(m.group(1), reusableTagContext); // If the parsed output is null, allow Denizen to handle the debugging // and return "null" - if (parsed != null) { - String cleaned = parsed; - m.appendReplacement(s, Matcher.quoteReplacement(cleaned)); - } - else { - m.appendReplacement(s, "null"); - } + m.appendReplacement(s, parsed != null ? Matcher.quoteReplacement(parsed) : "null"); } m.appendTail(s); response.write(s.toString().getBytes(StandardCharsets.UTF_8)); @@ -178,7 +176,26 @@ public ObjectTag getContext(String name) { return new ElementTag(httpExchange.getRemoteAddress().toString()); } else if (name.equals("query")) { - return new ElementTag(httpExchange.getRequestURI().getQuery()); + String query = httpExchange.getRequestURI().getQuery(); + return new ElementTag(query != null ? query : ""); + } + else if (name.equals("query_map")) { + MapTag mappedValues = new MapTag(); + String query = httpExchange.getRequestURI().getQuery(); + if (query != null) { + for (String value : CoreUtilities.split(query, '&')) { + List split = CoreUtilities.split(value, '=', 2); + try { + String split_key = java.net.URLDecoder.decode(split.get(0), "UTF-8"); + String split_value = java.net.URLDecoder.decode(split.get(1), "UTF-8"); + mappedValues.map.put(new StringHolder(split_key), new ElementTag(split_value)); + } + catch (UnsupportedEncodingException e) { + Debug.echoError(e); + } + } + } + return mappedValues; } else if (name.equals("request")) { return new ElementTag(httpExchange.getRequestURI().getPath()); diff --git a/src/main/java/space/morphanone/webizen/events/GetRequestScriptEvent.java b/src/main/java/space/morphanone/webizen/events/GetRequestScriptEvent.java index 915b185..1a1c79a 100644 --- a/src/main/java/space/morphanone/webizen/events/GetRequestScriptEvent.java +++ b/src/main/java/space/morphanone/webizen/events/GetRequestScriptEvent.java @@ -12,8 +12,9 @@ public class GetRequestScriptEvent extends BasicRequestScriptEvent { // // @Context // Returns the IP address of the device that sent the request. - // Returns the path that was requested - // Returns a ListTag of the query included with the request + // Returns the path that was requested. + // Returns an ElementTag of the raw query included with the request. + // Returns a map of the query. // Returns info about the authenticated user sending the request, if any. // // @Determine diff --git a/src/main/java/space/morphanone/webizen/events/PostRequestScriptEvent.java b/src/main/java/space/morphanone/webizen/events/PostRequestScriptEvent.java index 3b6dc76..159d123 100644 --- a/src/main/java/space/morphanone/webizen/events/PostRequestScriptEvent.java +++ b/src/main/java/space/morphanone/webizen/events/PostRequestScriptEvent.java @@ -23,8 +23,9 @@ public class PostRequestScriptEvent extends BasicRequestScriptEvent { // // @Context // Returns the IP address of the device that sent the request. - // Returns the path that was requested - // Returns a ListTag of the query included with the request + // Returns the path that was requested. + // Returns a ElementTag of the raw query included with the request. + // Returns a map of the query. // Returns info about the authenticated user sending the request, if any. // returns the name of the file posted. // returns the size of the upload in MegaBytes (where 1 MegaByte = 1 000 000 Bytes).