From 7f127a9cad61a35bd2c7c827d8e61023f7380649 Mon Sep 17 00:00:00 2001 From: spthiel Date: Sun, 9 Apr 2023 10:57:29 +0200 Subject: [PATCH] [FIX] Fix http spaming logs on error --- gradle.properties | 2 +- .../actions/information/external/Http.java | 34 ++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/gradle.properties b/gradle.properties index 7c9e707..32e7762 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,7 +15,7 @@ group=me.spthiel.klacaiba archivesBaseName=Klacaiba #Version of your project -version=3.0.0-beta.8 +version=3.0.0-beta.9 ################################################################################ # Module settings diff --git a/src/main/java/me/spthiel/klacaiba/module/actions/information/external/Http.java b/src/main/java/me/spthiel/klacaiba/module/actions/information/external/Http.java index 1b52a90..757c55c 100644 --- a/src/main/java/me/spthiel/klacaiba/module/actions/information/external/Http.java +++ b/src/main/java/me/spthiel/klacaiba/module/actions/information/external/Http.java @@ -39,6 +39,7 @@ public boolean canExecuteNow(IScriptActionProvider provider, IMacro macro, IMacr HashMap header = new HashMap<>(); boolean requiresExtra = false; boolean hasEverything = true; + int timeout = -1; @@ -67,6 +68,16 @@ public boolean canExecuteNow(IScriptActionProvider provider, IMacro macro, IMacr index++; } else if(requiresExtra){ hasEverything = false; + } else if (params.length > index){ + try { + timeout = Integer.parseInt(provider.expand(macro, params[index], false)); + } catch (NumberFormatException ignored) {} + } + + if (requiresExtra && params.length > index) { + try { + timeout = Integer.parseInt(provider.expand(macro, params[index], false)); + } catch (NumberFormatException ignored) {} } for(; index < params.length; index++) { @@ -84,10 +95,19 @@ public boolean canExecuteNow(IScriptActionProvider provider, IMacro macro, IMacr String finalUrl = url; String finalAction = action; String finalOutput = output; + int finalTimeout = timeout; instance.setState(true); Thread request = new Thread(() -> { - - instance.setState(executeRequest(finalUrl, finalAction, finalOutput, header)); + String response = executeRequest(finalUrl, finalAction, finalOutput, header, finalTimeout); + if (response == null) { + provider.setVariable(macro, "&httpdebug1", "Last Request >> " + finalAction + ": " + finalUrl); + provider.setVariable(macro, "&httpdebug2", "Last Request >> " + finalOutput); + provider.setVariable(macro, "&httpdebug3", "Last Request >> " + header); + + instance.setState("Error"); + } else { + instance.setState(response); + } }); request.start(); @@ -110,7 +130,7 @@ public IReturnValue execute(IScriptActionProvider provider, IMacro macro, IMacro } /* Partially from http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139 */ - public static String executeRequest(String targetURL, String method, String output, HashMap header) { + public static String executeRequest(String targetURL, String method, String output, HashMap header, int timeout) { HttpURLConnection connection = null; method = method.toUpperCase(); @@ -118,9 +138,13 @@ public static String executeRequest(String targetURL, String method, String outp //Create connection URL url = new URL(targetURL); connection = (HttpURLConnection) url.openConnection(); + if (timeout != -1) { + connection.setConnectTimeout(timeout); + connection.setReadTimeout(timeout); + } connection.setRequestMethod(method); - connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"); + connection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"); connection.setUseCaches(false); connection.setDoOutput(method.matches("POST|PUT")); @@ -161,7 +185,7 @@ public static String executeRequest(String targetURL, String method, String outp @Override public String getUsage() { - return "http([GET|POST|PUT|DELETE],,[output stream],[headers])"; + return "http([GET|POST|PUT|DELETE],,[output stream],[headers|connection-timeout],[connection-timeout])"; } @Nonnull