Skip to content

Commit

Permalink
[FIX] Fix http spaming logs on error
Browse files Browse the repository at this point in the history
  • Loading branch information
spthiel committed Apr 9, 2023
1 parent 7867f3a commit 7f127a9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public boolean canExecuteNow(IScriptActionProvider provider, IMacro macro, IMacr
HashMap<String,String> header = new HashMap<>();
boolean requiresExtra = false;
boolean hasEverything = true;
int timeout = -1;



Expand Down Expand Up @@ -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++) {
Expand All @@ -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();
Expand All @@ -110,17 +130,21 @@ 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<String,String> header) {
public static String executeRequest(String targetURL, String method, String output, HashMap<String,String> header, int timeout) {
HttpURLConnection connection = null;
method = method.toUpperCase();

try {
//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"));
Expand Down Expand Up @@ -161,7 +185,7 @@ public static String executeRequest(String targetURL, String method, String outp
@Override
public String getUsage() {

return "http([GET|POST|PUT|DELETE],<url>,[output stream],[headers])";
return "http([GET|POST|PUT|DELETE],<url>,[output stream],[headers|connection-timeout],[connection-timeout])";
}

@Nonnull
Expand Down

0 comments on commit 7f127a9

Please sign in to comment.