markt-asf commented on code in PR #1154:
URL: https://github.com/apache/tomcat/pull/1154#discussion_r4589491888
##########
java/org/apache/coyote/http2/Http2UpgradeHandler.java:
##########
@@ -918,6 +921,89 @@ protected HeaderFrameBuffers doWriteHeaders(Stream stream,
MimeHeaders mimeHeade
return headerFrameBuffers;
}
+
+ /**
+ * Removes the header fields that cannot be sent in a valid HTTP/2
message, as HTTP/1.1 does. A header field with a
+ * name that is not a token, or with a value that contains a character
that is not in the range 0 to 255, would
+ * otherwise make the encoder fail after it had updated its dynamic table.
Control characters in a value are
+ * replaced with a space and leading and trailing whitespace is removed
(RFC 9113, section 8.2.1).
+ *
+ * @param stream the stream the headers are written to
+ * @param headers the headers to check. Invalid header fields are removed
and invalid values are corrected.
+ */
+ private void removeInvalidHeaders(Stream stream, MimeHeaders headers) {
+ for (int i = 0; i < headers.size(); i++) {
+ String name = headers.getName(i).toString();
+ if (name.startsWith(":")) {
+ // Pseudo header fields are always generated by Tomcat
+ continue;
+ }
+ String value = headers.getValue(i).toString();
+ String validValue = HttpParser.isToken(name) ?
sanitizeHeaderValue(value) : null;
+ if (validValue == null) {
+ log.warn(sm.getString("upgradeHandler.response.invalidHeader",
connectionId, stream.getIdAsString(),
+ name, value));
+ headers.removeHeader(i--);
+ } else if (!validValue.equals(value)) {
+ headers.getValue(i).setString(validValue);
+ }
+ }
+ }
+
+
+ /**
+ * Corrects a header value so that it is valid in an HTTP/2 message.
+ *
+ * @param value the value to correct
+ *
+ * @return the corrected value or {@code null} if the value contains a
character that cannot be sent
+ */
+ static String sanitizeHeaderValue(String value) {
+ int length = value.length();
+ boolean valid = true;
+ if (length > 0 && (isHeaderWhitespace(value.charAt(0)) ||
isHeaderWhitespace(value.charAt(length - 1)))) {
+ valid = false;
+ }
+ for (int i = 0; i < length; i++) {
+ char c = value.charAt(i);
+ if (c > 255) {
+ return null;
+ }
+ if (isHeaderControl(c)) {
+ valid = false;
+ }
+ }
+ if (valid) {
+ return value;
+ }
+
+ char[] result = value.toCharArray();
+ int first = 0;
+ int last = length;
+ for (int i = 0; i < length; i++) {
+ if (isHeaderControl(result[i])) {
+ result[i] = ' ';
+ }
+ }
+ while (first < last && isHeaderWhitespace(result[first])) {
+ first++;
+ }
+ while (last > first && isHeaderWhitespace(result[last - 1])) {
+ last--;
+ }
+ return new String(result, first, last - first);
+ }
+
+
+ private static boolean isHeaderControl(char c) {
+ return c <= 31 && c != '\t' || c == 127;
+ }
+
+
+ private static boolean isHeaderWhitespace(char c) {
+ return c == ' ' || c == '\t';
+ }
+
Review Comment:
Have a look at HttpParser. You may be able to re-use the existing methods
there. If not, you can add your own. There should be a small performance
benefit to that approach.
##########
java/org/apache/coyote/http2/Http2UpgradeHandler.java:
##########
@@ -918,6 +921,89 @@ protected HeaderFrameBuffers doWriteHeaders(Stream stream,
MimeHeaders mimeHeade
return headerFrameBuffers;
}
+
+ /**
+ * Removes the header fields that cannot be sent in a valid HTTP/2
message, as HTTP/1.1 does. A header field with a
+ * name that is not a token, or with a value that contains a character
that is not in the range 0 to 255, would
+ * otherwise make the encoder fail after it had updated its dynamic table.
Control characters in a value are
+ * replaced with a space and leading and trailing whitespace is removed
(RFC 9113, section 8.2.1).
+ *
+ * @param stream the stream the headers are written to
+ * @param headers the headers to check. Invalid header fields are removed
and invalid values are corrected.
+ */
+ private void removeInvalidHeaders(Stream stream, MimeHeaders headers) {
+ for (int i = 0; i < headers.size(); i++) {
+ String name = headers.getName(i).toString();
+ if (name.startsWith(":")) {
+ // Pseudo header fields are always generated by Tomcat
+ continue;
+ }
+ String value = headers.getValue(i).toString();
+ String validValue = HttpParser.isToken(name) ?
sanitizeHeaderValue(value) : null;
+ if (validValue == null) {
+ log.warn(sm.getString("upgradeHandler.response.invalidHeader",
connectionId, stream.getIdAsString(),
+ name, value));
+ headers.removeHeader(i--);
+ } else if (!validValue.equals(value)) {
Review Comment:
You can use != here which should be slightly faster.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]