Search in sources :

Example 1 with HarCookies

use of edu.umass.cs.benchlab.har.HarCookies in project zaproxy by zaproxy.

the class HarUtils method createHarResponse.

public static HarResponse createHarResponse(HttpMessage httpMessage) {
    HttpResponseHeader responseHeader = httpMessage.getResponseHeader();
    HarCookies harCookies = new HarCookies();
    long whenCreated = System.currentTimeMillis();
    for (HttpCookie cookie : responseHeader.getHttpCookies(httpMessage.getRequestHeader().getHostName())) {
        Date expires;
        if (cookie.getVersion() == 0) {
            expires = new Date(whenCreated + (cookie.getMaxAge() * 1000));
        } else {
            expires = new Date(httpMessage.getTimeSentMillis() + httpMessage.getTimeElapsedMillis() + (cookie.getMaxAge() * 1000));
        }
        harCookies.addCookie(new HarCookie(cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getDomain(), expires, cookie.isHttpOnly(), cookie.getSecure(), null));
    }
    String text = null;
    String encoding = null;
    String contentType = responseHeader.getHeader(HttpHeader.CONTENT_TYPE);
    if (contentType == null) {
        contentType = "";
    } else if (!contentType.isEmpty()) {
        String lcContentType = contentType.toLowerCase(Locale.ROOT);
        final int pos = lcContentType.indexOf(';');
        if (pos != -1) {
            lcContentType = lcContentType.substring(0, pos).trim();
        }
        if (!lcContentType.startsWith("text")) {
            encoding = "base64";
            text = Base64.encodeBytes(httpMessage.getResponseBody().getBytes());
        } else {
            text = httpMessage.getResponseBody().toString();
        }
    }
    HarContent harContent = new HarContent(httpMessage.getResponseBody().length(), 0, contentType, text, encoding, null);
    String redirectUrl = responseHeader.getHeader(HttpHeader.LOCATION);
    return new HarResponse(responseHeader.getStatusCode(), responseHeader.getReasonPhrase(), responseHeader.getVersion(), harCookies, createHarHeaders(responseHeader), harContent, redirectUrl == null ? "" : redirectUrl, responseHeader.toString().length(), httpMessage.getResponseBody().length(), null);
}
Also used : HarCookie(edu.umass.cs.benchlab.har.HarCookie) HttpResponseHeader(org.parosproxy.paros.network.HttpResponseHeader) HarCookies(edu.umass.cs.benchlab.har.HarCookies) HarQueryString(edu.umass.cs.benchlab.har.HarQueryString) HarContent(edu.umass.cs.benchlab.har.HarContent) HarResponse(edu.umass.cs.benchlab.har.HarResponse) HttpCookie(java.net.HttpCookie) Date(java.util.Date)

Example 2 with HarCookies

use of edu.umass.cs.benchlab.har.HarCookies 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);
}
Also used : HarCookie(edu.umass.cs.benchlab.har.HarCookie) HarQueryString(edu.umass.cs.benchlab.har.HarQueryString) HarCookies(edu.umass.cs.benchlab.har.HarCookies) HarQueryString(edu.umass.cs.benchlab.har.HarQueryString) HttpRequestHeader(org.parosproxy.paros.network.HttpRequestHeader) HarQueryParam(edu.umass.cs.benchlab.har.HarQueryParam) HarPostData(edu.umass.cs.benchlab.har.HarPostData) HttpRequestBody(org.zaproxy.zap.network.HttpRequestBody) HarRequest(edu.umass.cs.benchlab.har.HarRequest) HtmlParameter(org.parosproxy.paros.network.HtmlParameter) HarPostDataParams(edu.umass.cs.benchlab.har.HarPostDataParams) HttpCookie(java.net.HttpCookie) HarPostDataParam(edu.umass.cs.benchlab.har.HarPostDataParam)

Aggregations

HarCookie (edu.umass.cs.benchlab.har.HarCookie)2 HarCookies (edu.umass.cs.benchlab.har.HarCookies)2 HarQueryString (edu.umass.cs.benchlab.har.HarQueryString)2 HttpCookie (java.net.HttpCookie)2 HarContent (edu.umass.cs.benchlab.har.HarContent)1 HarPostData (edu.umass.cs.benchlab.har.HarPostData)1 HarPostDataParam (edu.umass.cs.benchlab.har.HarPostDataParam)1 HarPostDataParams (edu.umass.cs.benchlab.har.HarPostDataParams)1 HarQueryParam (edu.umass.cs.benchlab.har.HarQueryParam)1 HarRequest (edu.umass.cs.benchlab.har.HarRequest)1 HarResponse (edu.umass.cs.benchlab.har.HarResponse)1 Date (java.util.Date)1 HtmlParameter (org.parosproxy.paros.network.HtmlParameter)1 HttpRequestHeader (org.parosproxy.paros.network.HttpRequestHeader)1 HttpResponseHeader (org.parosproxy.paros.network.HttpResponseHeader)1 HttpRequestBody (org.zaproxy.zap.network.HttpRequestBody)1