From a2622f29c3967d8a21a0a0e43ebc811c2e39087e Mon Sep 17 00:00:00 2001 From: yamamo Date: Fri, 28 Apr 2023 05:51:40 +0900 Subject: [PATCH] Enable to send http headers in the curl. (#39) * curl with headers * Insecure option to be selectable. --- .../com/mulesoft/tool/network/NetworkUtils.java | 17 ++++++++++++++--- src/main/mule/net-tools.xml | 2 +- src/main/resources/api/net-tools.raml | 6 ++++++ src/main/resources/web/index.html | 13 ++++++++++++- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/mulesoft/tool/network/NetworkUtils.java b/src/main/java/com/mulesoft/tool/network/NetworkUtils.java index 31d399c..97e7c4e 100644 --- a/src/main/java/com/mulesoft/tool/network/NetworkUtils.java +++ b/src/main/java/com/mulesoft/tool/network/NetworkUtils.java @@ -8,7 +8,8 @@ import java.net.InetSocketAddress; import java.net.Socket; import java.net.UnknownHostException; - +import java.util.ArrayList; +import java.util.List; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.BufferedWriter; @@ -42,12 +43,22 @@ public static String resolveIPs(String host, String dnsServer) throws UnknownHos } } - public static String curl(String url) throws IOException { + public static String curl(String url, String[] headers, Boolean insecure) throws IOException { //-i include protocol headers //-L follow redirects //-k insecure //-E cert status - return execute(new ProcessBuilder("curl","-k", "-i","-L", url)); + List command = new ArrayList(); + command.add("curl"); + if(insecure) command.add("-k"); + command.add("-i"); + command.add("-L"); + command.add(url); + for (String header : headers ) { + command.add("-H"); + command.add(header); + } + return execute(new ProcessBuilder(command)); } public static String testConnect(String host, String port) { diff --git a/src/main/mule/net-tools.xml b/src/main/mule/net-tools.xml index e395147..e5303cb 100644 --- a/src/main/mule/net-tools.xml +++ b/src/main/mule/net-tools.xml @@ -158,7 +158,7 @@ output application/json output application/java import java!com::mulesoft::tool::network::NetworkUtils --- -NetworkUtils::curl(attributes.queryParams.url)]]> +NetworkUtils::curl(attributes.queryParams.url, attributes.queryParams.*header default [], attributes.queryParams.insecure as Boolean)]]> diff --git a/src/main/resources/api/net-tools.raml b/src/main/resources/api/net-tools.raml index e2f9ef3..95dcde5 100644 --- a/src/main/resources/api/net-tools.raml +++ b/src/main/resources/api/net-tools.raml @@ -91,6 +91,12 @@ securitySchemes: url: description: Target URL type: string + header: + required: false + items: + type: string + example: | + Authorization:Basic YQxhZGRpbjpvcGVuc2VzYW1l responses: 200: body: diff --git a/src/main/resources/web/index.html b/src/main/resources/web/index.html index a1ac060..57d4fe0 100644 --- a/src/main/resources/web/index.html +++ b/src/main/resources/web/index.html @@ -149,6 +149,8 @@ + insecure + @@ -159,7 +161,7 @@ - +
             
@@ -188,6 +190,8 @@ var operation = e.target.value; var inputs = $("#theForm input").removeClass(); inputs.hide(); + var textareas = $("#theForm textarea").removeClass(); + textareas.hide(); var helps = $(".helpBox"); helps.hide(); switch (operation) { @@ -222,6 +226,9 @@ case "curl": $("#url").show(); $("#curlHelp").show(); + $("#header").show(); + $("#insecure").show(); + $("#insecure_label").show(); break; default: @@ -262,6 +269,10 @@ } if (operation === 'curl') { apiurl += '&url=' + encodeURI(url); + apiurl += '&insecure=' + $("#insecure").prop('checked'); + for (let h of $("#header").val().split('\n')) { + apiurl += '&header=' + encodeURI(h); + } } console.log(operation + " " + ip + " " + port + " " + url ); $("#check").attr("disabled", true);