diff --git a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java index 1015c73d1159..9fa6979119a1 100644 --- a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java +++ b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java @@ -379,25 +379,33 @@ 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) @@ -405,7 +413,7 @@ 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; } @@ -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) @@ -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); @@ -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: @@ -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); @@ -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; @@ -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; } diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpChannelState.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpChannelState.java index 13e376fc489f..0342e6761234 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpChannelState.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpChannelState.java @@ -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