Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Enable to send http headers in the curl. (#39)
Browse files Browse the repository at this point in the history
* curl with headers

* Insecure option to be selectable.
  • Loading branch information
yamamoWorks authored Apr 27, 2023
1 parent bf547fe commit a2622f2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/main/java/com/mulesoft/tool/network/NetworkUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> command = new ArrayList<String>();
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) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/mule/net-tools.xml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ output application/json
output application/java
import java!com::mulesoft::tool::network::NetworkUtils
---
NetworkUtils::curl(attributes.queryParams.url)]]></ee:set-payload>
NetworkUtils::curl(attributes.queryParams.url, attributes.queryParams.*header default [], attributes.queryParams.insecure as Boolean)]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/api/net-tools.raml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ securitySchemes:
url:
description: Target URL
type: string
header:
required: false
items:
type: string
example: |
Authorization:Basic YQxhZGRpbjpvcGVuc2VzYW1l
responses:
200:
body:
Expand Down
13 changes: 12 additions & 1 deletion src/main/resources/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@
<input name="ip" placeholder="IP Address or Host" type="text" title="IP" id="ip" />
<input name="port" placeholder="Port" type="text" title="PORT" id="port" />
<input name="url" placeholder="http://host:port/path/" type="text" title="URL" id="url" />
<input name="insecure" type="checkbox" title="insecure" id="insecure" checked="true" /><span class="helpBox" id="insecure_label">insecure</span>
<textarea name="header" placeholder="header-name:header-value" title="Header" id="header" rows="3" cols="50" style="display: block"></textarea>
<input name="dnsServer" placeholder="dns server" type="text" title="DNS SERVER" id="dnsServer" />
<button type="button" id="check">Run</button>
<button type="button" id="clean">Clean Console</button>
Expand All @@ -159,7 +161,7 @@
<div class="helpBox" id="tracerouteHelp" style="display: none;">Set an IP Address or Hostname and hit Run</div>
<div class="helpBox" id="socketHelp" style="display: none;">Set an IP Address or Hostname, the Port and hit Run</div>
<div class="helpBox" id="dnsHelp" style="display: none;">Set a Hostname and hit Run</div>
<div class="helpBox" id="curlHelp" style="display: none;">Set an IP Address or Schema (http-https://) plus Hostname, the Port, the Path and hit Run</div>
<div class="helpBox" id="curlHelp" style="display: none;">Set an IP Address or Schema (http-https://) plus Hostname, the Port, the Path, the Headers (multi-line) and hit Run</div>
</div>
<pre id="output">
</pre>
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -222,6 +226,9 @@
case "curl":
$("#url").show();
$("#curlHelp").show();
$("#header").show();
$("#insecure").show();
$("#insecure_label").show();
break;
default:

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a2622f2

Please sign in to comment.