Search in sources :

Example 1 with HarEntry

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

the class SearchAPI method handleApiOther.

@Override
public HttpMessage handleApiOther(HttpMessage msg, String name, JSONObject params) throws ApiException {
    byte[] responseBody = {};
    ExtensionSearch.Type searchType;
    switch(name) {
        case OTHER_HAR_BY_URL_REGEX:
            searchType = ExtensionSearch.Type.URL;
            break;
        case OTHER_HAR_BY_REQUEST_REGEX:
            searchType = ExtensionSearch.Type.Request;
            break;
        case OTHER_HAR_BY_RESPONSE_REGEX:
            searchType = ExtensionSearch.Type.Response;
            break;
        case OTHER_HAR_BY_HEADER_REGEX:
            searchType = ExtensionSearch.Type.Header;
            break;
        default:
            throw new ApiException(ApiException.Type.BAD_OTHER);
    }
    validateRegex(params);
    try {
        final HarEntries entries = new HarEntries();
        search(params, searchType, rh -> {
            HarEntry entry = HarUtils.createHarEntry(rh.getHistoryId(), rh.getHistoryType(), rh.getHttpMessage());
            entries.addEntry(entry);
        });
        HarLog harLog = HarUtils.createZapHarLog();
        harLog.setEntries(entries);
        responseBody = HarUtils.harLogToByteArray(harLog);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        ApiException apiException = new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
        responseBody = apiException.toString(API.Format.JSON, incErrorDetails()).getBytes(StandardCharsets.UTF_8);
    }
    try {
        msg.setResponseHeader(API.getDefaultResponseHeader("application/json; charset=UTF-8", responseBody.length));
    } catch (HttpMalformedHeaderException e) {
        log.error("Failed to create response header: " + e.getMessage(), e);
    }
    msg.setResponseBody(responseBody);
    return msg;
}
Also used : HarEntry(edu.umass.cs.benchlab.har.HarEntry) HarEntries(edu.umass.cs.benchlab.har.HarEntries) HarLog(edu.umass.cs.benchlab.har.HarLog) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) PatternSyntaxException(java.util.regex.PatternSyntaxException) ApiException(org.zaproxy.zap.extension.api.ApiException) DatabaseException(org.parosproxy.paros.db.DatabaseException) ApiException(org.zaproxy.zap.extension.api.ApiException)

Example 2 with HarEntry

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

the class HarUtils method createHarEntry.

/**
 * Creates a {@code HarEntry} from the given message with additional custom fields for the
 * history ID/type and note.
 *
 * @param historyId the history ID of the HTTP message.
 * @param historyType the history type of the HTTP message.
 * @param httpMessage the HTTP message.
 * @return the {@code HarEntry}, never {@code null}.
 * @since 2.8.0
 * @see #createMessageHarCustomFields(int, int, String)
 */
public static HarEntry createHarEntry(int historyId, int historyType, HttpMessage httpMessage) {
    HarEntry entry = createHarEntry(httpMessage);
    entry.setCustomFields(createMessageHarCustomFields(historyId, historyType, httpMessage.getNote()));
    return entry;
}
Also used : HarEntry(edu.umass.cs.benchlab.har.HarEntry)

Aggregations

HarEntry (edu.umass.cs.benchlab.har.HarEntry)2 HarEntries (edu.umass.cs.benchlab.har.HarEntries)1 HarLog (edu.umass.cs.benchlab.har.HarLog)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 DatabaseException (org.parosproxy.paros.db.DatabaseException)1 HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)1 ApiException (org.zaproxy.zap.extension.api.ApiException)1