From f646c1dedbf7211e4a9ae3c64ea7f407d435a896 Mon Sep 17 00:00:00 2001 From: Ryan Aird Date: Thu, 1 Oct 2020 12:14:44 -0600 Subject: [PATCH 1/4] Add python style request logging --- .../bluemap/core/webserver/HttpConnection.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/HttpConnection.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/HttpConnection.java index 298e3b3f..afce1467 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/HttpConnection.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/HttpConnection.java @@ -34,6 +34,9 @@ import java.util.concurrent.TimeUnit; import de.bluecolored.bluemap.core.logger.Logger; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; public class HttpConnection implements Runnable { @@ -66,6 +69,19 @@ public void run() { HttpRequest request = acceptRequest(); HttpResponse response = handler.handle(request); sendResponse(response); + + DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + Date date = new Date(); + Logger.global.logInfo( + connection.getInetAddress().toString() + + " [ " + + dateFormat.format(date) + + " ] \"" + + request.getMethod() + + " " + request.getPath() + + " " + request.getVersion() + + "\" " + + response.getStatusCode().toString()); } catch (InvalidRequestException e){ try { sendResponse(new HttpResponse(HttpStatusCode.BAD_REQUEST)); From 73e8da9c37174d3da9727bcf40fbbb261d79cbc4 Mon Sep 17 00:00:00 2001 From: Ryan Aird Date: Thu, 1 Oct 2020 14:38:02 -0600 Subject: [PATCH 2/4] Restrict logging of web requests to the CLI when run with --verbose or -b --- .../bluemap/common/plugin/Plugin.java | 3 +- .../core/webserver/HttpConnection.java | 32 +++++++++++-------- .../bluemap/core/webserver/WebServer.java | 6 ++-- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java index 3dda9721..45093a17 100644 --- a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java +++ b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java @@ -130,7 +130,8 @@ public void load() throws IOException, ParseResourceException { webServerConfig.getWebserverPort(), webServerConfig.getWebserverMaxConnections(), webServerConfig.getWebserverBindAdress(), - requestHandler + requestHandler, + false ); webServer.start(); } diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/HttpConnection.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/HttpConnection.java index afce1467..33805d25 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/HttpConnection.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/HttpConnection.java @@ -46,11 +46,14 @@ public class HttpConnection implements Runnable { private Socket connection; private InputStream in; private OutputStream out; + + private final boolean verbose; - public HttpConnection(ServerSocket server, Socket connection, HttpRequestHandler handler, int timeout, TimeUnit timeoutUnit) throws IOException { + public HttpConnection(ServerSocket server, Socket connection, HttpRequestHandler handler, int timeout, TimeUnit timeoutUnit, boolean verbose) throws IOException { this.server = server; this.connection = connection; this.handler = handler; + this.verbose = verbose; if (isClosed()){ throw new IOException("Socket already closed!"); @@ -69,19 +72,20 @@ public void run() { HttpRequest request = acceptRequest(); HttpResponse response = handler.handle(request); sendResponse(response); - - DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); - Date date = new Date(); - Logger.global.logInfo( - connection.getInetAddress().toString() + - " [ " + - dateFormat.format(date) + - " ] \"" + - request.getMethod() + - " " + request.getPath() + - " " + request.getVersion() + - "\" " + - response.getStatusCode().toString()); + if (verbose) { + DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + Date date = new Date(); + Logger.global.logInfo( + connection.getInetAddress().toString() + + " [ " + + dateFormat.format(date) + + " ] \"" + + request.getMethod() + + " " + request.getPath() + + " " + request.getVersion() + + "\" " + + response.getStatusCode().toString()); + } } catch (InvalidRequestException e){ try { sendResponse(new HttpResponse(HttpStatusCode.BAD_REQUEST)); diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/WebServer.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/WebServer.java index ba8a290e..1a18ae24 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/WebServer.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/WebServer.java @@ -41,6 +41,7 @@ public class WebServer extends Thread { private final int port; private final int maxConnections; private final InetAddress bindAdress; + private final boolean verbose; private HttpRequestHandler handler; @@ -48,10 +49,11 @@ public class WebServer extends Thread { private ServerSocket server; - public WebServer(int port, int maxConnections, InetAddress bindAdress, HttpRequestHandler handler) { + public WebServer(int port, int maxConnections, InetAddress bindAdress, HttpRequestHandler handler, boolean verbose) { this.port = port; this.maxConnections = maxConnections; this.bindAdress = bindAdress; + this.verbose = verbose; this.handler = handler; @@ -81,7 +83,7 @@ public void run(){ Socket connection = server.accept(); try { - connectionThreads.execute(new HttpConnection(server, connection, handler, 10, TimeUnit.SECONDS)); + connectionThreads.execute(new HttpConnection(server, connection, handler, 10, TimeUnit.SECONDS, verbose)); } catch (RejectedExecutionException e){ connection.close(); Logger.global.logWarning("Dropped an incoming HttpConnection! (Too many connections?)"); From a2579f307f4d7a0476d614ef7b4229f4c4a783db Mon Sep 17 00:00:00 2001 From: Ryan Aird Date: Thu, 1 Oct 2020 14:56:56 -0600 Subject: [PATCH 3/4] Added missing file for CLI --- .../main/java/de/bluecolored/bluemap/cli/BlueMapCLI.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/implementations/cli/src/main/java/de/bluecolored/bluemap/cli/BlueMapCLI.java b/implementations/cli/src/main/java/de/bluecolored/bluemap/cli/BlueMapCLI.java index 07024542..bf5dcabc 100644 --- a/implementations/cli/src/main/java/de/bluecolored/bluemap/cli/BlueMapCLI.java +++ b/implementations/cli/src/main/java/de/bluecolored/bluemap/cli/BlueMapCLI.java @@ -227,7 +227,7 @@ public void renderMaps(BlueMapService blueMap, boolean forceRender, boolean forc Logger.global.logInfo("Render finished!"); } - public void startWebserver(BlueMapService blueMap) throws IOException { + public void startWebserver(BlueMapService blueMap, boolean verbose) throws IOException { Logger.global.logInfo("Starting webserver ..."); WebServerConfig config = blueMap.getWebServerConfig(); @@ -237,7 +237,8 @@ public void startWebserver(BlueMapService blueMap) throws IOException { config.getWebserverPort(), config.getWebserverMaxConnections(), config.getWebserverBindAdress(), - requestHandler + requestHandler, + verbose ); webServer.start(); } @@ -283,7 +284,7 @@ public static void main(String[] args) { if (cmd.hasOption("w")) { noActions = false; - cli.startWebserver(blueMap); + cli.startWebserver(blueMap, cmd.hasOption("b")); Thread.sleep(1000); //wait a second to let the webserver start, looks nicer in the log if anything comes after that } @@ -374,6 +375,7 @@ private static Options createOptions() { ); options.addOption("w", "webserver", false, "Starts the web-server, configured in the 'webserver.conf' file"); + options.addOption("b", "verbose", false, "Causes the web-server to log requests to the console"); options.addOption("g", "generate-webapp", false, "Generates the files for the web-app to the folder, configured in the 'render.conf' file (this is done automatically when rendering if the 'index.html' file in the webroot can't be found)"); options.addOption("s", "generate-websettings", false, "Generates the settings for the web-app, using the settings from the 'render.conf' file (this is done automatically when rendering)"); From ddffdd6102676dce84793d288dbab392c701ede5 Mon Sep 17 00:00:00 2001 From: Ryan Aird Date: Sat, 3 Oct 2020 20:39:26 -0600 Subject: [PATCH 4/4] Split logging into separate function and use tabs instead of spaces' --- .../core/webserver/HttpConnection.java | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/HttpConnection.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/HttpConnection.java index 33805d25..8af9ca47 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/HttpConnection.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/webserver/HttpConnection.java @@ -72,20 +72,9 @@ public void run() { HttpRequest request = acceptRequest(); HttpResponse response = handler.handle(request); sendResponse(response); - if (verbose) { - DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); - Date date = new Date(); - Logger.global.logInfo( - connection.getInetAddress().toString() + - " [ " + - dateFormat.format(date) + - " ] \"" + - request.getMethod() + - " " + request.getPath() + - " " + request.getVersion() + - "\" " + - response.getStatusCode().toString()); - } + if (verbose) { + log(request, response); + } } catch (InvalidRequestException e){ try { sendResponse(new HttpResponse(HttpStatusCode.BAD_REQUEST)); @@ -109,6 +98,21 @@ public void run() { Logger.global.logError("Error while closing HttpConnection!", e); } } + + private void log(HttpRequest request, HttpResponse response) { + DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + Date date = new Date(); + Logger.global.logInfo( + connection.getInetAddress().toString() + + " [ " + + dateFormat.format(date) + + " ] \"" + + request.getMethod() + + " " + request.getPath() + + " " + request.getVersion() + + "\" " + + response.getStatusCode().toString()); + } private void sendResponse(HttpResponse response) throws IOException { response.write(out);