From 7e1e7d80e9e367ddbacc914e7a723ff8fd33cd6c Mon Sep 17 00:00:00 2001 From: Fuzzlemann Date: Thu, 3 Aug 2017 16:29:31 +0200 Subject: [PATCH] Compresses the data before it's sent with GZIP, does not support browsers who don't support GZIP, because every browser which was downloaded in the last 10 years supports it --- .../data/listeners/PlanGamemodeChangeListener.java | 3 +++ .../plan/data/listeners/PlanPlayerListener.java | 6 ++++++ .../data/listeners/PlanWorldChangeListener.java | 6 +++++- .../djrapitops/plan/ui/webserver/WebServer.java | 14 ++++++++++++-- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanGamemodeChangeListener.java b/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanGamemodeChangeListener.java index 4a21103e5..8ff92f56e 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanGamemodeChangeListener.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanGamemodeChangeListener.java @@ -42,9 +42,12 @@ public class PlanGamemodeChangeListener implements Listener { if (event.isCancelled()) { return; } + Player p = event.getPlayer(); UUID uuid = p.getUniqueId(); + String gameMode = event.getNewGameMode().name(); long time = MiscUtils.getTime(); + handler.addToPool(new PlaytimeDependentInfo(uuid, InfoType.GM, time, event.getNewGameMode().name(), p.getWorld().getName())); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanPlayerListener.java b/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanPlayerListener.java index e57d54790..63ebebe1c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanPlayerListener.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanPlayerListener.java @@ -108,6 +108,7 @@ public class PlanPlayerListener implements Listener { Player player = event.getPlayer(); UUID uuid = player.getUniqueId(); handler.endSession(uuid); + Log.debug(uuid + ": PlayerQuitEvent"); long time = MiscUtils.getTime(); boolean banned = player.isBanned(); @@ -116,6 +117,7 @@ public class PlanPlayerListener implements Listener { handler.addToPool(new LogoutInfo(uuid, time, banned, gm.name(), handler.getSession(uuid), worldName)); handler.saveCachedData(uuid); + Log.debug(uuid + ": PlayerQuitEvent_END"); } @@ -131,10 +133,13 @@ public class PlanPlayerListener implements Listener { if (event.isCancelled()) { return; } + Player player = event.getPlayer(); UUID uuid = player.getUniqueId(); + handler.endSession(uuid); Log.debug(uuid + ": PlayerKickEvent"); + long time = MiscUtils.getTime(); boolean banned = player.isBanned(); Gamemode gm = Gamemode.wrap(player.getGameMode()); @@ -143,6 +148,7 @@ public class PlanPlayerListener implements Listener { handler.addToPool(new LogoutInfo(uuid, time, banned, gm.name(), handler.getSession(uuid), worldName)); handler.addToPool(new KickInfo(uuid)); handler.saveCachedData(uuid); + Log.debug(uuid + ": PlayerKickEvent_END"); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanWorldChangeListener.java b/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanWorldChangeListener.java index 543d0a086..6283ef264 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanWorldChangeListener.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanWorldChangeListener.java @@ -23,13 +23,17 @@ public class PlanWorldChangeListener implements Listener { @EventHandler(priority = EventPriority.MONITOR) public void onWorldChange(PlayerChangedWorldEvent event) { Player p = event.getPlayer(); + String previousWorld = event.getFrom().getName(); String worldName = p.getWorld().getName(); if (previousWorld.equals(worldName)) { return; } + UUID uuid = p.getUniqueId(); + String gameMode = p.getGameMode().name(); long time = MiscUtils.getTime(); - handler.addToPool(new PlaytimeDependentInfo(uuid, InfoType.WORLD, time, p.getGameMode().name(), p.getWorld().getName())); + + handler.addToPool(new PlaytimeDependentInfo(uuid, InfoType.WORLD, time, gameMode, worldName)); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java b/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java index 0d55fcbed..acd565f94 100644 --- a/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java +++ b/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java @@ -30,6 +30,7 @@ import java.util.UUID; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.zip.GZIPOutputStream; /** * @author Rsl1122 @@ -65,6 +66,7 @@ public class WebServer { if (enabled) { return; } + Log.info(Phrase.WEBSERVER_INIT.toString()); try { usingHttps = startHttpsServer(); @@ -82,24 +84,28 @@ public class WebServer { try { URI uri = exchange.getRequestURI(); String target = uri.toString(); + Headers responseHeaders = exchange.getResponseHeaders(); responseHeaders.set("Content-Type", "text/html;"); WebUser user = null; + if (usingHttps) { user = getUser(exchange.getRequestHeaders()); // Prompt authorization if (user == null) { - responseHeaders.set("WWW-Authenticate", "Basic realm=\"/\";"); } } + + responseHeaders.set("Content-Encoding", "gzip"); + Response response = getResponse(target, user); String content = response.getContent(); exchange.sendResponseHeaders(response.getCode(), 0); - try (BufferedOutputStream out = new BufferedOutputStream(exchange.getResponseBody()); + try (GZIPOutputStream out = new GZIPOutputStream(exchange.getResponseBody()); ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes())) { byte[] buffer = new byte[2048]; int count; @@ -115,6 +121,7 @@ public class WebServer { } } }); + server.setExecutor(new ThreadPoolExecutor(4, 8, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100))); server.start(); @@ -133,18 +140,21 @@ public class WebServer { if (Verify.isEmpty(authorization)) { return null; } + String auth = authorization.get(0); if (auth.contains("Basic ")) { auth = auth.split(" ")[1]; } else { throw new IllegalArgumentException("Wrong format of Auth"); } + Base64.Decoder decoder = Base64.getDecoder(); byte[] decoded = decoder.decode(auth); String[] userInfo = new String(decoded).split(":"); if (userInfo.length != 2) { throw new IllegalArgumentException("User and Password not specified"); } + String user = userInfo[0]; String passwordRaw = userInfo[1];