// Check if content is not already compressed
MessageBytes contentEncodingMB = responseHeaders.getValue("Content-Encoding"); if (contentEncodingMB != null) { // Content-Encoding values are ordered but order is not important // for this check so use a Set rather than a List
Set<String> tokens = new HashSet<>(); try {
TokenList.parseTokenList(responseHeaders.values("Content-Encoding"), tokens);
} catch (IOException e) { // Because we are using StringReader, any exception here is a // Tomcat bug.
log.warn(sm.getString("compressionConfig.ContentEncodingParseFail"), e); returnfalse;
} if (tokens.contains("gzip") || tokens.contains("br")) { returnfalse;
}
}
// If force mode, the length and MIME type checks are skipped if (compressionLevel != 2) { // Check if the response is of sufficient length to trigger the compression long contentLength = response.getContentLengthLong(); if (contentLength != -1 && contentLength < compressionMinSize) { returnfalse;
}
// Check for compatible MIME-TYPE
String[] compressibleMimeTypes = getCompressibleMimeTypes(); if (compressibleMimeTypes != null &&
!startsWithStringArray(compressibleMimeTypes, response.getContentType())) { returnfalse;
}
}
// Check if the resource has a strong ETag
String eTag = responseHeaders.getHeader("ETag"); if (eTag != null && !eTag.trim().startsWith("W/")) { // Has an ETag that doesn't start with "W/..." so it must be a // strong ETag returnfalse;
}
// If processing reaches this far, the response might be compressed. // Therefore, set the Vary header to keep proxies happy
ResponseUtil.addVaryFieldName(responseHeaders, "accept-encoding");
// Check if user-agent supports gzip encoding // Only interested in whether gzip encoding is supported. Other // encodings and weights can be ignored.
Enumeration<String> headerValues = request.getMimeHeaders().values("accept-encoding"); boolean foundGzip = false; while (!foundGzip && headerValues.hasMoreElements()) {
List<AcceptEncoding> acceptEncodings = null; try {
acceptEncodings = AcceptEncoding.parse(new StringReader(headerValues.nextElement()));
} catch (IOException ioe) { // If there is a problem reading the header, disable compression returnfalse;
}
for (AcceptEncoding acceptEncoding : acceptEncodings) { if ("gzip".equalsIgnoreCase(acceptEncoding.getEncoding())) {
foundGzip = true; break;
}
}
}
if (!foundGzip) { returnfalse;
}
// If force mode, the browser checks are skipped if (compressionLevel != 2) { // Check for incompatible Browser
Pattern noCompressionUserAgents = this.noCompressionUserAgents; if (noCompressionUserAgents != null) {
MessageBytes userAgentValueMB = request.getMimeHeaders().getValue("user-agent"); if (userAgentValueMB != null) {
String userAgentValue = userAgentValueMB.toString(); if (noCompressionUserAgents.matcher(userAgentValue).matches()) { returnfalse;
}
}
}
}
// All checks have passed. Compression is enabled.
// Compressed content length is unknown so mark it as such.
response.setContentLength(-1); // Configure the content encoding for compressed content
responseHeaders.setValue("Content-Encoding").setString("gzip");
returntrue;
}
/** *Checksifanyentryinthestringarraystartswiththespecifiedvalue * *@paramsArraytheStringArray *@paramvaluestring
*/ privatestaticboolean startsWithStringArray(String sArray[], String value) { if (value == null) { returnfalse;
} for (String s : sArray) { if (value.startsWith(s)) { returntrue;
}
} returnfalse;
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.9 Sekunden
(vorverarbeitet am 2026-09-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.