Skip to content

Commit

Permalink
Prefer code-duplication for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
franz1981 committed Nov 20, 2024
1 parent e4f4342 commit 95a8246
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/main/java/io/vertx/core/http/impl/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -880,10 +880,30 @@ private static void validateAsciiHeaderValue(AsciiString value) {
}
byte[] asciiChars = value.array();
int off = value.arrayOffset();
int end = off + length;
if (off == 0 && length == asciiChars.length) {
for (int index = 0; index < asciiChars.length; index++) {
int latinChar = asciiChars[index] & 0xFF;
if (latinChar == 0x7F) {
throw new IllegalArgumentException("a header value contains a prohibited character '127': " + value);
}
// non-printable chars are rare so let's make it a fall-back method, whilst still accepting HTAB
if (latinChar < 32 && latinChar != 0x09) {
validateSequenceHeaderValue(value, index - off);
break;
}
}
} else {
validateAsciiRangeHeaderValue(value, off, length, asciiChars);
}
}

// DON'T REMOVE THE OPTIMIZATION off == 0? ..: it's meant to reduce computation to perform bound checks
for (int index = off == 0? 0 : off; index < end; index++) {
/**
* This method is the slow-path generic version of {@link #validateAsciiHeaderValue(AsciiString)} which
* is optimized for {@link AsciiString} instances which are backed by a 0-offset full-blown byte array.
*/
private static void validateAsciiRangeHeaderValue(AsciiString value, int off, int length, byte[] asciiChars) {
int end = off + length;
for (int index = off; index < end; index++) {
int latinChar = asciiChars[index] & 0xFF;
if (latinChar == 0x7F) {
throw new IllegalArgumentException("a header value contains a prohibited character '127': " + value);
Expand Down

0 comments on commit 95a8246

Please sign in to comment.