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

This commit is contained in:
Fuzzlemann 2017-08-03 16:29:31 +02:00
parent 3fc0abc4f1
commit 7e1e7d80e9
4 changed files with 26 additions and 3 deletions

View File

@ -42,9 +42,12 @@ public class PlanGamemodeChangeListener implements Listener {
if (event.isCancelled()) { if (event.isCancelled()) {
return; return;
} }
Player p = event.getPlayer(); Player p = event.getPlayer();
UUID uuid = p.getUniqueId(); UUID uuid = p.getUniqueId();
String gameMode = event.getNewGameMode().name();
long time = MiscUtils.getTime(); long time = MiscUtils.getTime();
handler.addToPool(new PlaytimeDependentInfo(uuid, InfoType.GM, time, event.getNewGameMode().name(), p.getWorld().getName())); handler.addToPool(new PlaytimeDependentInfo(uuid, InfoType.GM, time, event.getNewGameMode().name(), p.getWorld().getName()));
} }
} }

View File

@ -108,6 +108,7 @@ public class PlanPlayerListener implements Listener {
Player player = event.getPlayer(); Player player = event.getPlayer();
UUID uuid = player.getUniqueId(); UUID uuid = player.getUniqueId();
handler.endSession(uuid); handler.endSession(uuid);
Log.debug(uuid + ": PlayerQuitEvent"); Log.debug(uuid + ": PlayerQuitEvent");
long time = MiscUtils.getTime(); long time = MiscUtils.getTime();
boolean banned = player.isBanned(); 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.addToPool(new LogoutInfo(uuid, time, banned, gm.name(), handler.getSession(uuid), worldName));
handler.saveCachedData(uuid); handler.saveCachedData(uuid);
Log.debug(uuid + ": PlayerQuitEvent_END"); Log.debug(uuid + ": PlayerQuitEvent_END");
} }
@ -131,10 +133,13 @@ public class PlanPlayerListener implements Listener {
if (event.isCancelled()) { if (event.isCancelled()) {
return; return;
} }
Player player = event.getPlayer(); Player player = event.getPlayer();
UUID uuid = player.getUniqueId(); UUID uuid = player.getUniqueId();
handler.endSession(uuid); handler.endSession(uuid);
Log.debug(uuid + ": PlayerKickEvent"); Log.debug(uuid + ": PlayerKickEvent");
long time = MiscUtils.getTime(); long time = MiscUtils.getTime();
boolean banned = player.isBanned(); boolean banned = player.isBanned();
Gamemode gm = Gamemode.wrap(player.getGameMode()); 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 LogoutInfo(uuid, time, banned, gm.name(), handler.getSession(uuid), worldName));
handler.addToPool(new KickInfo(uuid)); handler.addToPool(new KickInfo(uuid));
handler.saveCachedData(uuid); handler.saveCachedData(uuid);
Log.debug(uuid + ": PlayerKickEvent_END"); Log.debug(uuid + ": PlayerKickEvent_END");
} }
} }

View File

@ -23,13 +23,17 @@ public class PlanWorldChangeListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onWorldChange(PlayerChangedWorldEvent event) { public void onWorldChange(PlayerChangedWorldEvent event) {
Player p = event.getPlayer(); Player p = event.getPlayer();
String previousWorld = event.getFrom().getName(); String previousWorld = event.getFrom().getName();
String worldName = p.getWorld().getName(); String worldName = p.getWorld().getName();
if (previousWorld.equals(worldName)) { if (previousWorld.equals(worldName)) {
return; return;
} }
UUID uuid = p.getUniqueId(); UUID uuid = p.getUniqueId();
String gameMode = p.getGameMode().name();
long time = MiscUtils.getTime(); 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));
} }
} }

View File

@ -30,6 +30,7 @@ import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPOutputStream;
/** /**
* @author Rsl1122 * @author Rsl1122
@ -65,6 +66,7 @@ public class WebServer {
if (enabled) { if (enabled) {
return; return;
} }
Log.info(Phrase.WEBSERVER_INIT.toString()); Log.info(Phrase.WEBSERVER_INIT.toString());
try { try {
usingHttps = startHttpsServer(); usingHttps = startHttpsServer();
@ -82,24 +84,28 @@ public class WebServer {
try { try {
URI uri = exchange.getRequestURI(); URI uri = exchange.getRequestURI();
String target = uri.toString(); String target = uri.toString();
Headers responseHeaders = exchange.getResponseHeaders(); Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/html;"); responseHeaders.set("Content-Type", "text/html;");
WebUser user = null; WebUser user = null;
if (usingHttps) { if (usingHttps) {
user = getUser(exchange.getRequestHeaders()); user = getUser(exchange.getRequestHeaders());
// Prompt authorization // Prompt authorization
if (user == null) { if (user == null) {
responseHeaders.set("WWW-Authenticate", "Basic realm=\"/\";"); responseHeaders.set("WWW-Authenticate", "Basic realm=\"/\";");
} }
} }
responseHeaders.set("Content-Encoding", "gzip");
Response response = getResponse(target, user); Response response = getResponse(target, user);
String content = response.getContent(); String content = response.getContent();
exchange.sendResponseHeaders(response.getCode(), 0); 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())) { ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes())) {
byte[] buffer = new byte[2048]; byte[] buffer = new byte[2048];
int count; int count;
@ -115,6 +121,7 @@ public class WebServer {
} }
} }
}); });
server.setExecutor(new ThreadPoolExecutor(4, 8, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100))); server.setExecutor(new ThreadPoolExecutor(4, 8, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100)));
server.start(); server.start();
@ -133,18 +140,21 @@ public class WebServer {
if (Verify.isEmpty(authorization)) { if (Verify.isEmpty(authorization)) {
return null; return null;
} }
String auth = authorization.get(0); String auth = authorization.get(0);
if (auth.contains("Basic ")) { if (auth.contains("Basic ")) {
auth = auth.split(" ")[1]; auth = auth.split(" ")[1];
} else { } else {
throw new IllegalArgumentException("Wrong format of Auth"); throw new IllegalArgumentException("Wrong format of Auth");
} }
Base64.Decoder decoder = Base64.getDecoder(); Base64.Decoder decoder = Base64.getDecoder();
byte[] decoded = decoder.decode(auth); byte[] decoded = decoder.decode(auth);
String[] userInfo = new String(decoded).split(":"); String[] userInfo = new String(decoded).split(":");
if (userInfo.length != 2) { if (userInfo.length != 2) {
throw new IllegalArgumentException("User and Password not specified"); throw new IllegalArgumentException("User and Password not specified");
} }
String user = userInfo[0]; String user = userInfo[0];
String passwordRaw = userInfo[1]; String passwordRaw = userInfo[1];