Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -379,33 +379,41 @@ public void setHeaderCacheCaseSensitive(boolean headerCacheCaseSensitive)

protected void checkViolation(Violation violation) throws HttpException.RuntimeException
{
boolean allowed = violation.isAllowedBy(_complianceMode);
reportComplianceViolation(violation, violation.getDescription());

if (!allowed)
if (!checkAndReportViolation(violation, violation.getDescription()))
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, violation.getDescription());
}

protected boolean checkAndReportViolation(Violation violation, String reason)
{
boolean allowed = _complianceMode.allows(violation);
reportComplianceViolation(violation, reason, allowed);
return allowed;
}

@Deprecated
protected void reportComplianceViolation(Violation violation)
{
reportComplianceViolation(violation, violation.getDescription());
checkAndReportViolation(violation, violation.getDescription());
}

@Deprecated
protected void reportComplianceViolation(Violation violation, String reason)
{
checkAndReportViolation(violation, reason);
}

protected void reportComplianceViolation(Violation violation, String reason, boolean allowed)
{
if (_requestParser)
{
boolean allowed = _complianceMode.allows(violation);
_requestHandler.onViolation(new ComplianceViolation.Event(_complianceMode, violation, reason, allowed));
}
}

protected String caseInsensitiveHeader(String orig, String normative)
{
if (CASE_SENSITIVE_FIELD_NAME.isAllowedBy(_complianceMode))
return normative;
if (!orig.equals(normative))
reportComplianceViolation(CASE_SENSITIVE_FIELD_NAME, orig);
reportComplianceViolation(CASE_SENSITIVE_FIELD_NAME, orig, false);
return orig;
}

Expand Down Expand Up @@ -795,8 +803,7 @@ private boolean parseLine(ByteBuffer buffer)
}
else
{
reportComplianceViolation(Violation.CASE_INSENSITIVE_METHOD, _methodString);
if (_complianceMode.allows(Violation.CASE_INSENSITIVE_METHOD))
if (checkAndReportViolation(Violation.CASE_INSENSITIVE_METHOD, _methodString))
{
method = HttpMethod.INSENSITIVE_CACHE.get(_methodString);
if (method != null)
Expand Down Expand Up @@ -964,9 +971,9 @@ private boolean parseLine(ByteBuffer buffer)
break;

case EOL:
{
// HTTP/0.9
reportComplianceViolation(HTTP_0_9, HTTP_0_9.getDescription());
if (Violation.HTTP_0_9.isAllowedBy(_complianceMode))
if (checkAndReportViolation(Violation.HTTP_0_9, HTTP_0_9.getDescription()))
{
_requestHandler.startRequest(_methodString, _uri.toCompleteString(), HttpVersion.HTTP_0_9);
setState(State.CONTENT);
Expand All @@ -979,6 +986,7 @@ private boolean parseLine(ByteBuffer buffer)
throw new HttpException.RuntimeException(HttpStatus.HTTP_VERSION_NOT_SUPPORTED_505, "HTTP/0.9 not supported");
}
break;
}

case ALPHA:
case DIGIT:
Expand Down Expand Up @@ -1548,11 +1556,10 @@ else if (_endOfContent == EndOfContent.UNKNOWN_CONTENT)
{
case SPACE:
case HTAB:
//Ignore trailing whitespaces ?
if (WHITESPACE_AFTER_FIELD_NAME.isAllowedBy(_complianceMode))
//Ignore trailing whitespaces?
if (checkAndReportViolation(WHITESPACE_AFTER_FIELD_NAME, _headerString))
{
_headerString = takeString();
reportComplianceViolation(WHITESPACE_AFTER_FIELD_NAME, "Space after " + _headerString);
_header = HttpHeader.CACHE.get(_headerString);
_length = -1;
setState(FieldState.WS_AFTER_NAME);
Expand All @@ -1574,8 +1581,7 @@ else if (_endOfContent == EndOfContent.UNKNOWN_CONTENT)
_valueString = "";
_length = -1;

reportComplianceViolation(NO_COLON_AFTER_FIELD_NAME, "Field " + _headerString);
if (NO_COLON_AFTER_FIELD_NAME.isAllowedBy(_complianceMode))
if (checkAndReportViolation(NO_COLON_AFTER_FIELD_NAME, _headerString))
{
setState(FieldState.FIELD);
break;
Expand Down Expand Up @@ -1606,9 +1612,8 @@ else if (_endOfContent == EndOfContent.UNKNOWN_CONTENT)
break;

case EOL:
if (NO_COLON_AFTER_FIELD_NAME.isAllowedBy(_complianceMode))
if (checkAndReportViolation(NO_COLON_AFTER_FIELD_NAME, _headerString))
{
reportComplianceViolation(NO_COLON_AFTER_FIELD_NAME, "Field " + _headerString);
setState(FieldState.FIELD);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ public void initialize()
case 1 -> listeners.get(0).initialize();
default -> new InitializedCompositeComplianceViolationListener(listeners);
};
}

if (!_connectionMetaData.getHttpConfiguration().isNotifyForbiddenComplianceViolations())
_complianceViolationListener = new AllowedOnlyComplianceListener(_complianceViolationListener);
if (_complianceViolationListener != ComplianceViolation.Listener.NOOP &&
!_connectionMetaData.getHttpConfiguration().isNotifyForbiddenComplianceViolations())
_complianceViolationListener = new AllowedOnlyComplianceListener(_complianceViolationListener);
}
}

private static class AllowedOnlyComplianceListener implements ComplianceViolation.Listener
Expand Down
Loading