Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
Meta & Tag Updates. (#5)
Browse files Browse the repository at this point in the history
* Meta & Tag Updates.

Updated Meta for <context.query> as it never returned a ListTag. it returns an element.
Added <context.query_map> to return a MapTag of the query. (Much more user friendly).
Minor meta grammatical updates.

Marked BlackCoyote's tutorial as out of date as the content was invalid, however left there for the examples.

* Further simplifification.

* Added URLDecoding to the query_map tag

* Moved the decoder to the correct place

* Had some sleep. Finally realised I'm an idiot.

* `, 2`
  • Loading branch information
Fortifier42 committed Jun 25, 2020
1 parent bef8b08 commit f83c607
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions src/main/java/space/morphanone/webizen/commands/WebCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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);

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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<String> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public class GetRequestScriptEvent extends BasicRequestScriptEvent {
//
// @Context
// <context.address> Returns the IP address of the device that sent the request.
// <context.request> Returns the path that was requested
// <context.query> Returns a ListTag of the query included with the request
// <context.request> Returns the path that was requested.
// <context.query> Returns an ElementTag of the raw query included with the request.
// <context.query_map> Returns a map of the query.
// <context.user_info> Returns info about the authenticated user sending the request, if any.
//
// @Determine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ public class PostRequestScriptEvent extends BasicRequestScriptEvent {
//
// @Context
// <context.address> Returns the IP address of the device that sent the request.
// <context.request> Returns the path that was requested
// <context.query> Returns a ListTag of the query included with the request
// <context.request> Returns the path that was requested.
// <context.query> Returns a ElementTag of the raw query included with the request.
// <context.query_map> Returns a map of the query.
// <context.user_info> Returns info about the authenticated user sending the request, if any.
// <context.upload_name> returns the name of the file posted.
// <context.upload_size_mb> returns the size of the upload in MegaBytes (where 1 MegaByte = 1 000 000 Bytes).
Expand Down

0 comments on commit f83c607

Please sign in to comment.