Commit fb2d11ec authored by 9731301's avatar 9731301

fix content-type and and support multupart form data

parent 87963623
This diff is collapsed.
......@@ -11,11 +11,12 @@ import java.awt.image.BufferedImage;
import java.io.DataInput;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class RequestPanel extends JPanel {
private VerticalFlowLayout vfLayout = new VerticalFlowLayout();
private String[] ss = {"Form Data","JSON","Binary Data"};
private String[] ss = {"Form","Multipart Form Data","JSON","Binary Data"};
private JComboBox Json_FormData = new JComboBox(ss);
private JPanel body_pane = new JPanel(){
@Override
......@@ -176,6 +177,22 @@ public class RequestPanel extends JPanel {
return headers;
}
public void addContentType(String type){
for(Component c : mainCenterHeader.getComponents()){
if (c instanceof JPanel){
JPanel nh = (JPanel) c;
JPanel l = (JPanel) nh.getComponent(1);
JTextField h = (JTextField) l.getComponent(0);
JTextField v = (JTextField) l.getComponent(1);
if (h.getText().toLowerCase().trim().equals("content-type")){
v.setText(type);
return;
}
}
}
addHeader("Content-Type",type);
}
public HashMap<String,String> getFormDataBody(){
HashMap<String,String> bodydata = new HashMap<>();
for(Component c : ((JPanel)body_pane.getComponent(0)).getComponents()){
......@@ -197,11 +214,31 @@ public class RequestPanel extends JPanel {
if(b.size()==0) return "";
String f = "";
for (String key : b.keySet()){
if (key=="-file") continue;
f = f+key+"="+b.get(key)+"&";
}
return f.substring(0,f.length()-1);
}
public String findUploadPath(){
HashMap<String,String> b = getFormDataBody();
if(b.size()==0) return "";
String f = "";
for (String key : b.keySet()){
if (key=="-file") f = b.get(key);
}
return f;
}
public String getHeadersAsString(HashMap<String,String> b){
if(b.size()==0) return "";
String f = "";
for (String key : b.keySet()){
f = f+key+":"+b.get(key)+";";
}
return f.substring(0,f.length()-1);
}
int selectedItem = -2;
MouseAdapter body_insert = null;
void initBody(String... args){
......@@ -225,6 +262,7 @@ public class RequestPanel extends JPanel {
textArea.setBorder(border);
if (args.length>0) textArea.setText(args[0]);
body_pane.add(textArea);
addContentType("application/json");
} else if(Json_FormData.getSelectedItem().equals("Binary Data")){
JTextField path = new JTextField("File Path"){
@Override
......@@ -247,10 +285,11 @@ public class RequestPanel extends JPanel {
fc.setVisible(true);
String f = fc.getFile();
if (f!=null && !f.isEmpty()) {
path.setText(new File(fc.getDirectory(),fc.getName()).getAbsolutePath());
path.setText(new File(fc.getDirectory(),new File(f).getName()).getAbsolutePath());
}
}
});
addContentType("application/octet-stream");
}else {
final JPanel vl = new JPanel(vfLayout) {
......@@ -272,10 +311,18 @@ public class RequestPanel extends JPanel {
};
}
if (args.length==0) body_insert.mouseClicked(null);
if (Json_FormData.getSelectedItem().equals("Multipart Form Data")){
addContentType("multipart/form-data");
}else{
addContentType("application/x-www-form-urlencoded");
}
}
body_pane.revalidate();
body_pane.repaint();
}
private void addBody(String... args){
......@@ -331,30 +378,49 @@ public class RequestPanel extends JPanel {
sendButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
RequestData requestData = new RequestData();
requestData.setUrl(URLTextField.getText());
requestData.setMethod(Parser.findMethod((String)centerMenu.getSelectedItem()));
ArrayList<String> argsList = new ArrayList<>();
argsList.add("--url");
argsList.add(URLTextField.getText());
argsList.add("--Method");
argsList.add(Parser.findMethod((String)centerMenu.getSelectedItem()));
//set if it is Json or not
if(Json_FormData.getSelectedItem().equals("JSON")) {
requestData.setJson(true);
argsList.add("--json");
argsList.add("--data");
JTextArea textArea = (JTextArea) body_pane.getComponent(0);
requestData.setBody(textArea.getText().trim());
argsList.add(textArea.getText().trim());
}
else if(Json_FormData.getSelectedItem().equals("Binary Data")){
JTextField p = (JTextField) body_pane.getComponent(0);
if (!p.getText().isEmpty() && !p.getText().equals("File Path"))
requestData.setUploadPath(p.getText());
if (!p.getText().isEmpty() && !p.getText().equals("File Path")) {
argsList.add("--upload");
argsList.add(p.getText());
}
}else {
requestData.setBody(getFormDataBodyAsString());
argsList.add("--data");
argsList.add(getFormDataBodyAsString());
String upload = findUploadPath();
if (!upload.isEmpty() && !upload.equals("File Path")) {
argsList.add("--upload");
argsList.add(upload);
}
}
requestData.setHeaders(getHeaders());
requestData.setFollowRedirects(true);
requestData.setShowResponseHeaders(true);
requestData.setSaveRequest(false);
HashMap<String,String> map = getHeaders();
if (Json_FormData.getSelectedItem().equals("Multipart Form Data")){
map.put("Content-Type","multipart/form-data");
}
if (map!=null && map.size()>0){
argsList.add("--headers");
argsList.add(getHeadersAsString(map));
}
ResponseData data = RequestLoader.run(requestData);
argsList.add("-f");
String[] args = argsList.toArray(new String[argsList.size()]);
ResponseData data = RequestLoader.run(Parser.parse(args));
main_class.load(data);
}
});
......@@ -388,13 +454,13 @@ public class RequestPanel extends JPanel {
URLTextField.setText(r.getUrl());
if (r.isJson()){
Json_FormData.setSelectedIndex(1);
Json_FormData.setSelectedIndex(2);
initBody(r.getBody());
}else if(r.getUploadPath().length()>1){
Json_FormData.setSelectedIndex(2);
Json_FormData.setSelectedIndex(3);
initBody(r.getUploadPath());
}else{
Json_FormData.setSelectedIndex(0);
Json_FormData.setSelectedIndex(r.isMultipartFormData()?1:0);
initBody("r");
if (body_insert!=null &&!r.getBody().isEmpty()) {
if (!r.getBody().contains("&")) {
......
......@@ -25,6 +25,12 @@ public class RequestData implements Serializable {
private String body = "";
private String uploadPath = "";
boolean isMultipart = false;
public boolean isMultipartFormData() {
return isMultipart;
}
public String getName() {
return name;
}
......
package com.insomnia.parser;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
......@@ -20,11 +21,6 @@ public class RequestLoader {
return responseData.finish();
}
if (data.isJson() ){
responseData.setSyntaxError(true);
responseData.setSyntaxErrorLog("enter a valid json data");
return responseData.finish();
}
File file = null;
......@@ -51,7 +47,7 @@ public class RequestLoader {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setUseCaches(false);
if (data.getMethod().equals(RequestData.POST) && (!data.getBody().isEmpty()||!data.getUploadPath().isEmpty()) ){
if (!data.getMethod().equals(RequestData.GET) && (!data.getBody().isEmpty()||!data.getUploadPath().isEmpty()) ){
con.setDoOutput(true);
}
......@@ -59,25 +55,34 @@ public class RequestLoader {
con.setInstanceFollowRedirects(data.isFollowRedirects());
con.setRequestMethod(data.getMethod());
String content_type = "";
if (data.getHeaders()!=null){
for (String key : data.getHeaders().keySet()){
if (key.toLowerCase().equals("content-type")) {
content_type = data.getHeaders().get(key);
continue;
}
con.setRequestProperty(key,data.getHeaders().get(key));
}
}
if (data.isJson()){
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
content_type = "application/json; charset=UTF-8";
}
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Cache-Control", "no-cache");
if (!data.getUploadPath().isEmpty()){
if (con.getDoOutput()){
if (content_type.toLowerCase().equals("multipart/form-data")) {
con.setRequestProperty(
"Content-Type", "multipart/form-data;boundary=" + boundary);
responseData.getRequestData().isMultipart = true;
}else{
con.setRequestProperty("Content-Type",content_type);
}
if (content_type.toLowerCase().equals("multipart/form-data") || !data.getUploadPath().isEmpty()) {
if (con.getDoOutput()){
if (!data.getUploadPath().isEmpty()){
DataOutputStream request = new DataOutputStream(con.getOutputStream());
String twoHyphens = "--";
String crlf = "\r\n";
......@@ -96,14 +101,15 @@ public class RequestLoader {
for (String key : fields.keySet()) {
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" + key + "\""+ crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + crlf);
request.writeBytes("Content-Type: text/plain; charset=UTF-8" + crlf);
request.writeBytes(crlf);
request.writeBytes(fields.get(key)+ crlf);
request.writeBytes(fields.get(key) + crlf);
request.flush();
}
}
if (!data.getUploadPath().isEmpty()) {
String fieldName = "file";
String fileName = file.getName();
......@@ -130,31 +136,36 @@ public class RequestLoader {
}
request.writeBytes(crlf);
}
request.writeBytes(twoHyphens + boundary +
twoHyphens + crlf);
request.flush();
request.close();
}else {
request.close();
}else{
OutputStream os = con.getOutputStream();
os.write(data.getBody().getBytes("UTF-8"));
os.close();
}
} else{
con.setRequestProperty("Content-Type",content_type);
}
int status = con.getResponseCode();
responseData.setStatus(status);
if (status == HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_ACCEPTED) {
InputStream responseStream = new
BufferedInputStream(con.getInputStream());
responseData.setResponseHeaders(con.getHeaderFields());
responseData.setSuccessful(true);
//Save files
if (responseData.getRequestData().getOutput().equals("-null")){
File f = new File(System.getProperty("java.io.tmpdir"),"insomnia");
try{
if (responseData.getRequestData().getOutput().equals("-null")) {
File f = new File(System.getProperty("java.io.tmpdir"), "insomnia");
try {
FileOutputStream outputStream = new FileOutputStream(f);
int read;
byte[] bytes = new byte[1024];
......@@ -162,18 +173,23 @@ public class RequestLoader {
while ((read = responseStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
}catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
responseData.setSize(f.length());
responseData.res = new String(Files.readAllBytes(Paths.get(f.getAbsolutePath())), Charset.forName("utf-8"));
f.delete();
}else {
Saver.saveResponse(responseData,responseStream);
} else {
Saver.saveResponse(responseData, responseStream);
}
if (data.isSaveRequest()) Saver.saveRequest(data);
con.disconnect();
}else if (status == HttpsURLConnection.HTTP_NO_CONTENT){
responseData.setSuccessful(true);
responseData.res = "No body returned for response";
con.disconnect();
} else {
responseData.setSuccessful(false);
......
......@@ -87,6 +87,7 @@ public class ResponseData implements Serializable {
String res = "";
public String getResponseString(){
return res;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment