use of edu.umass.cs.benchlab.har.HarPostData in project zaproxy by zaproxy.
the class HarUtils method createHttpMessage.
public static HttpMessage createHttpMessage(HarRequest harRequest) throws HttpMalformedHeaderException {
StringBuilder strBuilderReqHeader = new StringBuilder();
strBuilderReqHeader.append(harRequest.getMethod()).append(' ').append(harRequest.getUrl()).append(' ').append(harRequest.getHttpVersion()).append("\r\n");
for (HarHeader harHeader : harRequest.getHeaders().getHeaders()) {
strBuilderReqHeader.append(harHeader.getName()).append(": ").append(harHeader.getValue()).append("\r\n");
}
strBuilderReqHeader.append("\r\n");
StringBuilder strBuilderReqBody = new StringBuilder();
final HarPostData harPostData = harRequest.getPostData();
if (harPostData != null) {
final String text = harPostData.getText();
if (text != null && !text.isEmpty()) {
strBuilderReqBody.append(harRequest.getPostData().getText());
} else if (harPostData.getParams() != null && !harPostData.getParams().getPostDataParams().isEmpty()) {
for (HarPostDataParam param : harRequest.getPostData().getParams().getPostDataParams()) {
if (strBuilderReqBody.length() > 0) {
strBuilderReqBody.append('&');
}
strBuilderReqBody.append(param.getName()).append('=').append(param.getValue());
}
}
}
return new HttpMessage(new HttpRequestHeader(strBuilderReqHeader.toString()), new HttpRequestBody(strBuilderReqBody.toString()));
}
use of edu.umass.cs.benchlab.har.HarPostData in project zaproxy by zaproxy.
the class HarUtils method createHarRequest.
public static HarRequest createHarRequest(HttpMessage httpMessage) {
HttpRequestHeader requestHeader = httpMessage.getRequestHeader();
HarCookies harCookies = new HarCookies();
try {
for (HttpCookie cookie : requestHeader.getHttpCookies()) {
harCookies.addCookie(new HarCookie(cookie.getName(), cookie.getValue()));
}
} catch (IllegalArgumentException e) {
LOGGER.warn("Ignoring cookies for HAR (\"request\") \"cookies\" list. Request contains invalid cookie: " + e.getMessage());
}
HarQueryString harQueryString = new HarQueryString();
for (HtmlParameter param : httpMessage.getUrlParams()) {
harQueryString.addQueryParam(new HarQueryParam(param.getName(), param.getValue()));
}
HarPostData harPostData = null;
HttpRequestBody requestBody = httpMessage.getRequestBody();
if (requestBody.length() >= 0) {
HarPostDataParams params = new HarPostDataParams();
String text = "";
String contentType = requestHeader.getHeader(HttpHeader.CONTENT_TYPE);
if (contentType == null) {
contentType = "";
text = requestBody.toString();
} else {
if (StringUtils.startsWithIgnoreCase(contentType.trim(), HttpHeader.FORM_URLENCODED_CONTENT_TYPE)) {
for (HtmlParameter param : httpMessage.getFormParams()) {
params.addPostDataParam(new HarPostDataParam(param.getName(), param.getValue()));
}
} else {
text = requestBody.toString();
}
}
harPostData = new HarPostData(contentType, params, text, null);
}
return new HarRequest(requestHeader.getMethod(), requestHeader.getURI().toString(), requestHeader.getVersion(), harCookies, createHarHeaders(requestHeader), harQueryString, harPostData, requestHeader.toString().length(), httpMessage.getRequestBody().length(), null);
}
Aggregations