-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing source files and fix bugs.
- Loading branch information
1 parent
4b451c2
commit 40da0fd
Showing
9 changed files
with
319 additions
and
7 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
library/src/main/java/org/owasp/benchmarkutils/entities/JerseyTestCaseInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.owasp.benchmarkutils.entities; | ||
|
||
import org.apache.hc.client5.http.classic.methods.HttpPost; | ||
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase; | ||
import org.apache.hc.core5.http.io.entity.StringEntity; | ||
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue; | ||
|
||
@XmlDiscriminatorValue("Jersey") | ||
// @XmlType(name = "HttpPostTestCaseInput") | ||
public class JerseyTestCaseInput extends HttpTestCaseInput { | ||
|
||
@Override | ||
void buildQueryString() { | ||
setQueryString(""); | ||
} | ||
|
||
@Override | ||
void buildHeaders(HttpUriRequestBase request) { | ||
request.addHeader("Content-Type", "application/xml; charset=utf-8"); | ||
for (RequestVariable header : getHeaders()) { | ||
String name = header.getName(); | ||
String value = header.getValue(); | ||
// System.out.println("Header:" + name + "=" + value); | ||
request.addHeader(name, value); | ||
} | ||
} | ||
|
||
@Override | ||
void buildCookies(HttpUriRequestBase request) { | ||
for (RequestVariable cookie : getCookies()) { | ||
String name = cookie.getName(); | ||
String value = cookie.getValue(); | ||
// System.out.println("Cookie:" + name + "=" + value); | ||
request.addHeader("Cookie", name + "=" + value); | ||
} | ||
} | ||
|
||
@Override | ||
void buildBodyParameters(HttpUriRequestBase request) { | ||
String params = "<person>"; | ||
for (RequestVariable field : getFormParameters()) { | ||
String name = field.getName(); | ||
String value = field.getValue(); | ||
params += "<" + name + ">" + escapeXML(value) + "</" + name + ">"; | ||
} | ||
params += "</person>"; | ||
StringEntity paramsEnt = new StringEntity(params); | ||
request.setEntity(paramsEnt); | ||
} | ||
|
||
private static String escapeXML(String value) { | ||
value = value.replace("&", "&"); | ||
value = value.replace("\"", """); | ||
value = value.replace("'", "'"); | ||
value = value.replace("<", "<"); | ||
value = value.replace(">", ">"); | ||
|
||
return value; | ||
} | ||
|
||
@Override | ||
HttpUriRequestBase createRequestInstance(String url) { | ||
// Apparently all Jersey Requests are POSTS. Never any query string params per buildQuery() | ||
// above. | ||
HttpPost httpPost = new HttpPost(url); | ||
return httpPost; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
library/src/main/java/org/owasp/benchmarkutils/entities/ServletTestCaseInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.owasp.benchmarkutils.entities; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.apache.hc.client5.http.classic.methods.HttpGet; | ||
import org.apache.hc.client5.http.classic.methods.HttpPost; | ||
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase; | ||
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity; | ||
import org.apache.hc.core5.http.NameValuePair; | ||
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue; | ||
|
||
@XmlDiscriminatorValue("Servlet") | ||
public class ServletTestCaseInput extends HttpTestCaseInput { | ||
|
||
@Override | ||
void buildQueryString() { | ||
setQueryString(""); | ||
boolean first = true; | ||
for (RequestVariable field : getGetParameters()) { | ||
if (first) { | ||
setQueryString("?"); | ||
first = false; | ||
} else { | ||
setQueryString(getQueryString() + "&"); | ||
} | ||
String name = field.getName(); | ||
String value = field.getValue(); | ||
// System.out.println(query); | ||
setQueryString(getQueryString() + (name + "=" + urlEncode(value))); | ||
} | ||
} | ||
|
||
@Override | ||
void buildHeaders(HttpUriRequestBase request) { | ||
// AJAX does: text/plain;charset=UTF-8, while HTML Form: application/x-www-form-urlencoded | ||
// request.addHeader("Content-Type", ";charset=UTF-8"); --This BREAKS BenchmarkCrawling | ||
request.addHeader( | ||
"Content-Type", "application/x-www-form-urlencoded"); // Works for both though | ||
|
||
for (RequestVariable header : getHeaders()) { | ||
String name = header.getName(); | ||
String value = header.getValue(); | ||
// System.out.println("Header:" + name + "=" + value); | ||
request.addHeader(name, value); | ||
} | ||
} | ||
|
||
@Override | ||
void buildCookies(HttpUriRequestBase request) { | ||
for (RequestVariable cookie : getCookies()) { | ||
String name = cookie.getName(); | ||
String value = cookie.getValue(); | ||
// Note: URL encoding of a space becomes a +, which is OK for Java, but | ||
// not other languages. So after URLEncoding, replace all + with %20, which is the | ||
// standard URL encoding for a space char. | ||
request.addHeader("Cookie", name + "=" + urlEncode(value).replace("+", "%20")); | ||
} | ||
} | ||
|
||
@Override | ||
void buildBodyParameters(HttpUriRequestBase request) { | ||
List<NameValuePair> fields = new ArrayList<>(); | ||
for (RequestVariable formParam : getFormParameters()) { | ||
fields.add(formParam.getNameValuePair()); | ||
} | ||
|
||
// Add the body parameters to the request if there were any | ||
if (fields.size() > 0) { | ||
request.setEntity(new UrlEncodedFormEntity(fields)); | ||
} | ||
} | ||
|
||
@Override | ||
HttpUriRequestBase createRequestInstance(String url) { | ||
HttpUriRequestBase httpUriRequestBase; | ||
if (getQueryString().length() == 0) { | ||
httpUriRequestBase = new HttpPost(url); | ||
} else { | ||
httpUriRequestBase = new HttpGet(url); | ||
} | ||
return httpUriRequestBase; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
library/src/main/java/org/owasp/benchmarkutils/entities/SpringTestCaseInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.owasp.benchmarkutils.entities; | ||
|
||
import org.apache.hc.client5.http.classic.methods.HttpPost; | ||
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase; | ||
import org.apache.hc.core5.http.io.entity.StringEntity; | ||
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue; | ||
|
||
@XmlDiscriminatorValue("Spring") | ||
// @XmlType(name = "HttpPostTestCaseInput") | ||
public class SpringTestCaseInput extends HttpTestCaseInput { | ||
|
||
@Override | ||
void buildQueryString() { | ||
setQueryString(""); | ||
} | ||
|
||
@Override | ||
void buildHeaders(HttpUriRequestBase request) { | ||
request.addHeader("Content-type", "application/json"); // Should this add ;charset=utf-8? | ||
// No: "Designating the encoding is somewhat redundant for JSON, since the default encoding | ||
// for JSON is UTF-8." | ||
for (RequestVariable header : getHeaders()) { | ||
String name = header.getName(); | ||
String value = header.getValue(); | ||
System.out.println("Header:" + name + "=" + value); | ||
request.addHeader(name, value); | ||
} | ||
} | ||
|
||
@Override | ||
void buildCookies(HttpUriRequestBase request) { | ||
for (RequestVariable cookie : getCookies()) { | ||
String name = cookie.getName(); | ||
String value = cookie.getValue(); | ||
// System.out.println("Cookie:" + name + "=" + value); | ||
request.addHeader("Cookie", name + "=" + value); | ||
} | ||
} | ||
|
||
@Override | ||
void buildBodyParameters(HttpUriRequestBase request) { | ||
boolean first = true; | ||
String params = "{"; | ||
for (RequestVariable field : getFormParameters()) { | ||
String name = field.getName(); | ||
String value = field.getValue(); | ||
// System.out.println(name+"="+value); | ||
if (first) { | ||
first = false; | ||
} else { | ||
params = params + ","; | ||
} | ||
params = params + String.format("\"%s\":\"%s\"", name, value.replace("\"", "\\\"")); | ||
} | ||
params += "}"; | ||
StringEntity paramsEnt = new StringEntity(params); | ||
request.setEntity(paramsEnt); | ||
} | ||
|
||
@Override | ||
HttpUriRequestBase createRequestInstance(String url) { | ||
// Apparently all Spring Requests are POSTS. Never any query string params per buildQuery() | ||
// above. | ||
HttpPost httpPost = new HttpPost(url); | ||
return httpPost; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
plugin/src/main/java/org/owasp/benchmarkutils/tools/CliExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.owasp.benchmarkutils.tools; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.owasp.benchmarkutils.entities.CliRequest; | ||
import org.owasp.benchmarkutils.entities.RequestVariable; | ||
|
||
public class CliExecutor implements TestExecutor { | ||
CliRequest cliRequest; | ||
|
||
public CliExecutor(CliRequest cliRequest) { | ||
super(); | ||
this.cliRequest = cliRequest; | ||
} | ||
|
||
public CliRequest getCliRequest() { | ||
return cliRequest; | ||
} | ||
|
||
public void setCliRequest(CliRequest cliRequest) { | ||
this.cliRequest = cliRequest; | ||
} | ||
|
||
public String getExecutorDescription() { | ||
List<String> commandTokens = new ArrayList<>(); | ||
commandTokens.add(cliRequest.getCommand()); | ||
for (RequestVariable requestVariable : cliRequest.getArgs()) { | ||
commandTokens.add( | ||
String.format( | ||
"%s:%s%n", requestVariable.getName(), requestVariable.getValue())); | ||
} | ||
|
||
return commandTokens.toString(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
plugin/src/main/java/org/owasp/benchmarkutils/tools/HttpExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.owasp.benchmarkutils.tools; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.nio.charset.StandardCharsets; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.hc.client5.http.classic.methods.HttpPost; | ||
import org.apache.hc.client5.http.classic.methods.HttpUriRequest; | ||
import org.apache.hc.core5.http.Header; | ||
import org.apache.hc.core5.http.HttpEntity; | ||
|
||
public class HttpExecutor implements TestExecutor { | ||
HttpUriRequest httpRequest; | ||
|
||
public HttpExecutor(HttpUriRequest httpRequest) { | ||
super(); | ||
this.httpRequest = httpRequest; | ||
} | ||
|
||
public HttpUriRequest getHttpRequest() { | ||
return httpRequest; | ||
} | ||
|
||
public void setHttpRequest(HttpUriRequest httpRequest) { | ||
this.httpRequest = httpRequest; | ||
} | ||
|
||
public String getExecutorDescription() { | ||
StringWriter stringWriter = new StringWriter(); | ||
PrintWriter out = new PrintWriter(stringWriter); | ||
|
||
out.println(httpRequest.toString()); | ||
for (Header header : httpRequest.getHeaders()) { | ||
out.printf("%s:%s%n", header.getName(), header.getValue()); | ||
} | ||
if (httpRequest instanceof HttpPost) { | ||
HttpPost postHttpRequest = (HttpPost) httpRequest; | ||
out.println(); | ||
try { | ||
HttpEntity entity = postHttpRequest.getEntity(); | ||
if (entity != null) { | ||
out.println(IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8)); | ||
} | ||
} catch (IOException e) { | ||
System.out.println("ERROR: Could not parse HttpPost entities"); | ||
e.printStackTrace(); | ||
} | ||
} | ||
out.flush(); | ||
return stringWriter.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
plugin/src/main/java/org/owasp/benchmarkutils/tools/TestExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.owasp.benchmarkutils.tools; | ||
|
||
interface TestExecutor { | ||
public String getExecutorDescription(); | ||
} |