From c7b2523fec0d4ff5aa3c2add0310c9538a98100d Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Wed, 20 Sep 2017 10:26:37 +0300 Subject: [PATCH] Fixed WebAPI variable parsing by replacing split symbol: & -> ;& --- .../systems/webserver/APIResponseHandler.java | 13 +------ .../plan/systems/webserver/webapi/WebAPI.java | 35 +++++++++++++------ 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIResponseHandler.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIResponseHandler.java index e4be680bc..a816563fd 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIResponseHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIResponseHandler.java @@ -17,11 +17,9 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.sql.SQLException; -import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.UUID; -import java.util.stream.Collectors; /** * Handles choosing of the correct API response to an API request. @@ -71,7 +69,7 @@ public class APIResponseHandler { return PageCache.loadPage(error, () -> new BadRequestResponse(error)); } - Map variables = readVariables(requestBody); + Map variables = WebAPI.readVariables(requestBody); String sender = variables.get("sender"); Log.debug("Received WebAPI Request" + target + " from " + sender); if (!checkKey(sender)) { @@ -128,13 +126,4 @@ public class APIResponseHandler { return false; } } - - private Map readVariables(String requestBody) { - String[] variables = requestBody.split("&"); - - return Arrays.stream(variables) - .map(variable -> variable.split("=", 2)) - .filter(splitVariables -> splitVariables.length == 2) - .collect(Collectors.toMap(splitVariables -> splitVariables[0], splitVariables -> splitVariables[1], (a, b) -> b)); - } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java index 966a9e9a9..56259faf3 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java @@ -26,9 +26,11 @@ import java.net.SocketTimeoutException; import java.net.URL; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.UUID; +import java.util.stream.Collectors; /** * @author Rsl1122 @@ -93,17 +95,11 @@ public abstract class WebAPI { connection.setRequestProperty("charset", "ISO-8859-1"); - StringBuilder parameters = new StringBuilder(); - String serverUUID = MiscUtils.getIPlan().getServerUuid().toString(); - parameters.append("sender=").append(serverUUID).append("&"); - for (Map.Entry entry : variables.entrySet()) { - parameters.append("&").append(entry.getKey()).append("=").append(entry.getValue()); - } - String params = parameters.toString(); + String parameters = parseVariables(); - connection.setRequestProperty("Content-Length", Integer.toString(params.length())); + connection.setRequestProperty("Content-Length", Integer.toString(parameters.length())); - byte[] toSend = params.getBytes(); + byte[] toSend = parameters.getBytes(); int length = toSend.length; // connection.setRequestProperty("Content-Length", Integer.toString(length)); @@ -149,7 +145,7 @@ public abstract class WebAPI { return sc.getSocketFactory(); } - static TrustManager[] trustAllCerts = new TrustManager[]{ + private static TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; @@ -176,4 +172,23 @@ public abstract class WebAPI { protected Response badRequest(String error) { return PageCache.loadPage(error, () -> new BadRequestResponse(error)); } + + private String parseVariables() { + StringBuilder parameters = new StringBuilder(); + String serverUUID = MiscUtils.getIPlan().getServerUuid().toString(); + parameters.append("sender=").append(serverUUID); + for (Map.Entry entry : variables.entrySet()) { + parameters.append(";&").append(entry.getKey()).append("=").append(entry.getValue()); + } + return parameters.toString(); + } + + public static Map readVariables(String requestBody) { + String[] variables = requestBody.split(";&"); + + return Arrays.stream(variables) + .map(variable -> variable.split("=", 2)) + .filter(splitVariables -> splitVariables.length == 2) + .collect(Collectors.toMap(splitVariables -> splitVariables[0], splitVariables -> splitVariables[1], (a, b) -> b)); + } } \ No newline at end of file