use of org.parosproxy.paros.db.DatabaseException in project zaproxy by zaproxy.
the class ParosTableHistory method deleteTemporary.
/**
* Deletes all records whose history type was marked as temporary (by calling {@code setHistoryTypeTemporary(int)}).
* <p>
* By default the only temporary history types are {@code HistoryReference#TYPE_TEMPORARY} and
* {@code HistoryReference#TYPE_SCANNER_TEMPORARY}.
* </p>
*
* @throws DatabaseException if an error occurred while deleting the temporary history records
* @see HistoryReference#getTemporaryTypes()
*/
@Override
public void deleteTemporary() throws DatabaseException {
try {
for (Integer type : HistoryReference.getTemporaryTypes()) {
while (true) {
psDeleteTemp.setInt(1, type);
int result = psDeleteTemp.executeUpdate();
if (result == 0) {
break;
}
}
}
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
use of org.parosproxy.paros.db.DatabaseException in project zaproxy by zaproxy.
the class ParosTableHistory method getHistoryList.
@Override
public List<Integer> getHistoryList(long sessionId, int histType, String filter, boolean isRequest) throws DatabaseException {
try {
PreparedStatement psReadSearch = getConnection().prepareStatement("SELECT * FROM HISTORY WHERE " + SESSIONID + " = ? AND " + HISTTYPE + " = ? ORDER BY " + HISTORYID);
ResultSet rs = null;
Vector<Integer> v = new Vector<>();
try {
Pattern pattern = Pattern.compile(filter, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
Matcher matcher = null;
psReadSearch.setLong(1, sessionId);
psReadSearch.setInt(2, histType);
rs = psReadSearch.executeQuery();
while (rs.next()) {
if (isRequest) {
matcher = pattern.matcher(rs.getString(REQHEADER));
if (matcher.find()) {
// ZAP: Changed to use the method Integer.valueOf.
v.add(Integer.valueOf(rs.getInt(HISTORYID)));
continue;
}
matcher = pattern.matcher(rs.getString(REQBODY));
if (matcher.find()) {
// ZAP: Changed to use the method Integer.valueOf.
v.add(Integer.valueOf(rs.getInt(HISTORYID)));
continue;
}
} else {
matcher = pattern.matcher(rs.getString(RESHEADER));
if (matcher.find()) {
// ZAP: Changed to use the method Integer.valueOf.
v.add(Integer.valueOf(rs.getInt(HISTORYID)));
continue;
}
matcher = pattern.matcher(rs.getString(RESBODY));
if (matcher.find()) {
// ZAP: Changed to use the method Integer.valueOf.
v.add(Integer.valueOf(rs.getInt(HISTORYID)));
continue;
}
}
}
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
// Ignore
}
}
psReadSearch.close();
}
return v;
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
use of org.parosproxy.paros.db.DatabaseException in project zaproxy by zaproxy.
the class ParosTableHistory method containsURI.
@Override
public synchronized boolean containsURI(long sessionId, int historyType, String method, String uri, byte[] body) throws DatabaseException {
try {
psContainsURI.setString(1, uri);
psContainsURI.setString(2, method);
if (bodiesAsBytes) {
psContainsURI.setBytes(3, body);
} else {
psContainsURI.setString(3, new String(body));
}
psContainsURI.setLong(4, sessionId);
psContainsURI.setInt(5, historyType);
try (ResultSet rs = psContainsURI.executeQuery()) {
if (rs.next()) {
return true;
}
}
return false;
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
use of org.parosproxy.paros.db.DatabaseException in project zaproxy by zaproxy.
the class HostProcess method traverse.
private void traverse(StructuralNode node, boolean incRelatedSiblings, TraverseAction action) {
if (node == null || isStop()) {
return;
}
Set<StructuralNode> parentNodes = new HashSet<>();
parentNodes.add(node);
action.apply(node);
if (!action.isStopTraversing() && parentScanner.scanChildren()) {
if (incRelatedSiblings) {
// Note that this is only done for the top level
try {
Iterator<StructuralNode> iter = node.getParent().getChildIterator();
String nodeName = SessionStructure.getCleanRelativeName(node, false);
while (iter.hasNext()) {
StructuralNode sibling = iter.next();
if (!node.isSameAs(sibling) && nodeName.equals(SessionStructure.getCleanRelativeName(sibling, false))) {
log.debug("traverse: including related sibling " + sibling.getName());
parentNodes.add(sibling);
}
}
} catch (DatabaseException e) {
// Ignore - if we cant connect to the db there will be plenty of other errors logged ;)
}
}
for (StructuralNode pNode : parentNodes) {
Iterator<StructuralNode> iter = pNode.getChildIterator();
while (iter.hasNext() && !isStop() && !action.isStopTraversing()) {
StructuralNode child = iter.next();
// ZAP: Implement pause and resume
while (parentScanner.isPaused() && !isStop()) {
Util.sleep(500);
}
try {
traverse(child, action);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
}
}
use of org.parosproxy.paros.db.DatabaseException in project zaproxy by zaproxy.
the class Analyser method isFileExist.
public boolean isFileExist(HttpMessage msg) {
if (msg.getResponseHeader().isEmpty()) {
return false;
}
// RFC
if (msg.getResponseHeader().getStatusCode() == HttpStatusCode.NOT_FOUND) {
return false;
}
// ZAP: catch CloneNotSupportedException as introduced with version 3.1 of HttpClient
URI uri = null;
String sUri = null;
try {
uri = (URI) msg.getRequestHeader().getURI().clone();
// strip off last part of path - use folder only
uri.setQuery(null);
String path = uri.getPath();
path = path.replaceAll("/[^/]*$", "");
uri.setPath(path);
} catch (Exception e) {
} finally {
if (uri != null) {
sUri = uri.toString();
}
}
// get sample with same relative path position when possible.
// if not exist, use the host only
// ZAP: Removed unnecessary cast.
SampleResponse sample = mapVisited.get(sUri);
if (sample == null) {
try {
uri.setPath(null);
} catch (URIException e2) {
}
String sHostOnly = uri.toString();
// ZAP: Removed unnecessary cast.
sample = mapVisited.get(sHostOnly);
}
// check if any analysed result.
if (sample == null) {
if (msg.getResponseHeader().getStatusCode() == HttpStatusCode.OK) {
// no anlaysed result to confirm, assume file exist and return
return true;
} else {
return false;
}
}
// check for redirect response. If redirect to same location, then file does not exist
if (HttpStatusCode.isRedirection(msg.getResponseHeader().getStatusCode())) {
try {
if (sample.getMessage().getResponseHeader().getStatusCode() == msg.getResponseHeader().getStatusCode()) {
String location = msg.getResponseHeader().getHeader(HttpHeader.LOCATION);
if (location != null && location.equals(sample.getMessage().getResponseHeader().getHeader(HttpHeader.LOCATION))) {
return false;
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return true;
}
// Not success code
if (msg.getResponseHeader().getStatusCode() != HttpStatusCode.OK) {
return false;
}
// remain only OK response here
// nothing more to determine. Check for possible not found page pattern.
Matcher matcher = patternNotFound.matcher(msg.getResponseBody().toString());
if (matcher.find()) {
return false;
}
// static response
String body = msg.getResponseBody().toString().replaceAll(p_REMOVE_HEADER, "");
if (sample.getErrorPageType() == SampleResponse.ERROR_PAGE_STATIC) {
try {
if (sample.getMessage().getResponseBody().toString().equals(body)) {
return false;
}
} catch (HttpMalformedHeaderException | DatabaseException e) {
logger.error("Failed to read the message: " + e.getMessage(), e);
}
return true;
}
uri = msg.getRequestHeader().getURI();
try {
if (sample.getErrorPageType() == SampleResponse.ERROR_PAGE_DYNAMIC_BUT_DETERMINISTIC) {
body = msg.getResponseBody().toString().replaceAll(getPathRegex(uri), "").replaceAll("\\s[012]\\d:[0-5]\\d:[0-5]\\d\\s", "");
// ZAP: FindBugs fix - added call to HttpBody.toString()
if (sample.getMessage().getResponseBody().toString().equals(body)) {
return false;
}
return true;
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return true;
}
Aggregations