Search in sources :

Example 26 with HttpMalformedHeaderException

use of org.parosproxy.paros.network.HttpMalformedHeaderException in project zaproxy by zaproxy.

the class SqlTableHistory method getHistoryCache.

/* (non-Javadoc)
	 * @see org.parosproxy.paros.db.TbleHistoryIf#getHistoryCache(org.parosproxy.paros.model.HistoryReference, org.parosproxy.paros.network.HttpMessage)
	 */
@Override
public RecordHistory getHistoryCache(HistoryReference ref, HttpMessage reqMsg) throws DatabaseException, HttpMalformedHeaderException {
    try {
        //  get the cache from provided reference.
        //  naturally, the obtained cache should be AFTER AND NEARBY to the given reference.
        //  - historyId up to historyId+200
        //  - match sessionId
        //  - history type can be MANUEL or hidden (hidden is used by images not explicitly stored in history)
        //  - match URI
        PreparedStatement psReadCache = null;
        // TODO
        if (isExistStatusCode) {
            //          psReadCache = getConnection().prepareStatement("SELECT TOP 1 * FROM HISTORY WHERE URI = ? AND METHOD = ? AND REQBODY = ? AND " + HISTORYID + " >= ? AND " + HISTORYID + " <= ? AND SESSIONID = ? AND (HISTTYPE = " + HistoryReference.TYPE_MANUAL + " OR HISTTYPE = " + HistoryReference.TYPE_HIDDEN + ") AND STATUSCODE != 304");
            psReadCache = getConnection().prepareStatement("SELECT TOP 1 * FROM HISTORY WHERE URI = ? AND METHOD = ? AND REQBODY = ? AND " + HISTORYID + " >= ? AND " + HISTORYID + " <= ? AND SESSIONID = ? AND STATUSCODE != 304");
        } else {
            //          psReadCache = getConnection().prepareStatement("SELECT * FROM HISTORY WHERE URI = ? AND METHOD = ? AND REQBODY = ? AND " + HISTORYID + " >= ? AND " + HISTORYID + " <= ? AND SESSIONID = ? AND (HISTTYPE = " + HistoryReference.TYPE_MANUAL + " OR HISTTYPE = " + HistoryReference.TYPE_HIDDEN + ")");
            psReadCache = getConnection().prepareStatement("SELECT * FROM HISTORY WHERE URI = ? AND METHOD = ? AND REQBODY = ? AND " + HISTORYID + " >= ? AND " + HISTORYID + " <= ? AND SESSIONID = ?)");
        }
        psReadCache.setString(1, reqMsg.getRequestHeader().getURI().toString());
        psReadCache.setString(2, reqMsg.getRequestHeader().getMethod());
        if (bodiesAsBytes) {
            psReadCache.setBytes(3, reqMsg.getRequestBody().getBytes());
        } else {
            psReadCache.setString(3, new String(reqMsg.getRequestBody().getBytes()));
        }
        psReadCache.setInt(4, ref.getHistoryId());
        psReadCache.setInt(5, ref.getHistoryId() + 200);
        psReadCache.setLong(6, ref.getSessionId());
        ResultSet rs = psReadCache.executeQuery();
        RecordHistory rec = null;
        try {
            do {
                rec = build(rs);
                // and the result should NOT be NOT_MODIFIED for rendering by browser
                if (rec != null && rec.getHttpMessage().equals(reqMsg) && rec.getHttpMessage().getResponseHeader().getStatusCode() != HttpStatusCode.NOT_MODIFIED) {
                    return rec;
                }
            } while (rec != null);
        } finally {
            try {
                rs.close();
                psReadCache.close();
            } catch (Exception e) {
                // ZAP: Log exceptions
                log.warn(e.getMessage(), e);
            }
        }
        // TODO
        if (isExistStatusCode) {
            psReadCache = getConnection().prepareStatement("SELECT TOP 1 * FROM HISTORY WHERE URI = ? AND METHOD = ? AND REQBODY = ? AND SESSIONID = ? AND STATUSCODE != 304");
        } else {
            psReadCache = getConnection().prepareStatement("SELECT * FROM HISTORY WHERE URI = ? AND METHOD = ? AND REQBODY = ? AND SESSIONID = ?");
        }
        psReadCache.setString(1, reqMsg.getRequestHeader().getURI().toString());
        psReadCache.setString(2, reqMsg.getRequestHeader().getMethod());
        if (bodiesAsBytes) {
            psReadCache.setBytes(3, reqMsg.getRequestBody().getBytes());
        } else {
            psReadCache.setString(3, new String(reqMsg.getRequestBody().getBytes()));
        }
        psReadCache.setLong(4, ref.getSessionId());
        rs = psReadCache.executeQuery();
        rec = null;
        try {
            do {
                rec = build(rs);
                if (rec != null && rec.getHttpMessage().equals(reqMsg) && rec.getHttpMessage().getResponseHeader().getStatusCode() != HttpStatusCode.NOT_MODIFIED) {
                    return rec;
                }
            } while (rec != null);
        } finally {
            try {
                rs.close();
                psReadCache.close();
            } catch (Exception e) {
                // ZAP: Log exceptions
                log.warn(e.getMessage(), e);
            }
        }
        return null;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) RecordHistory(org.parosproxy.paros.db.RecordHistory) DatabaseException(org.parosproxy.paros.db.DatabaseException) DatabaseException(org.parosproxy.paros.db.DatabaseException) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) SQLException(java.sql.SQLException)

Example 27 with HttpMalformedHeaderException

use of org.parosproxy.paros.network.HttpMalformedHeaderException 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, new SearchResultsProcessor() {

            @Override
            public void processRecordHistory(RecordHistory recordHistory) {
                entries.addEntry(HarUtils.createHarEntry(recordHistory.getHttpMessage()));
            }
        });
        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 : HarEntries(edu.umass.cs.benchlab.har.HarEntries) HarLog(edu.umass.cs.benchlab.har.HarLog) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) RecordHistory(org.parosproxy.paros.db.RecordHistory) 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 28 with HttpMalformedHeaderException

use of org.parosproxy.paros.network.HttpMalformedHeaderException in project zaproxy by zaproxy.

the class ManualHttpRequestEditorDialog method setDefaultMessage.

@Override
public void setDefaultMessage() {
    HttpMessage msg = new HttpMessage();
    try {
        URI uri = new URI("http://www.any_domain_name.org/path", true);
        msg.setRequestHeader(new HttpRequestHeader(HttpRequestHeader.GET, uri, HttpHeader.HTTP10, Model.getSingleton().getOptionsParam().getConnectionParam()));
        setMessage(msg);
    } catch (HttpMalformedHeaderException e) {
        logger.error(e.getMessage(), e);
    } catch (URIException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : URIException(org.apache.commons.httpclient.URIException) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) HttpMessage(org.parosproxy.paros.network.HttpMessage) HttpRequestHeader(org.parosproxy.paros.network.HttpRequestHeader) URI(org.apache.commons.httpclient.URI)

Aggregations

HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)28 DatabaseException (org.parosproxy.paros.db.DatabaseException)17 HttpMessage (org.parosproxy.paros.network.HttpMessage)14 RecordHistory (org.parosproxy.paros.db.RecordHistory)7 IOException (java.io.IOException)6 HistoryReference (org.parosproxy.paros.model.HistoryReference)6 Matcher (java.util.regex.Matcher)4 URIException (org.apache.commons.httpclient.URIException)4 TableHistory (org.parosproxy.paros.db.TableHistory)4 Session (org.parosproxy.paros.model.Session)4 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 URI (org.apache.commons.httpclient.URI)3 ApiException (org.zaproxy.zap.extension.api.ApiException)3 HarEntries (edu.umass.cs.benchlab.har.HarEntries)2 HarLog (edu.umass.cs.benchlab.har.HarLog)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2