Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize header validation loop #9326

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 12 additions & 14 deletions http/http/src/main/java/io/helidon/http/Header.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -86,14 +86,12 @@ default List<String> allValues(boolean split) {
String value = values.get(0);
if (value.contains(", ")) {
return List.of(value.split(", "));
} else {
return List.of(value);
}
return List.of(value);
}
return values;
} else {
return allValues();
}
return allValues();
}

/**
Expand Down Expand Up @@ -160,17 +158,17 @@ default void validate() throws IllegalArgumentException {
private static void validateValue(String name, String value) throws IllegalArgumentException {
char[] vChars = value.toCharArray();
int vLength = vChars.length;
for (int i = 0; i < vLength; i++) {
char vChar = vChars[i];
if (i == 0) {
if (vChar < '!' || vChar == '\u007f') {
throw new IllegalArgumentException("First character of the header value is invalid"
+ " for header '" + name + "'");
}
} else {
if (vLength >= 1) {
char vChar = vChars[0];
if (vChar < '!' || vChar == '\u007f') {
throw new IllegalArgumentException("First character of the header value is invalid"
+ " for header '" + name + "'");
}
for (int i = 1; i < vLength; i++) {
vChar = vChars[i];
if (vChar < ' ' && vChar != '\t' || vChar == '\u007f') {
throw new IllegalArgumentException("Character at position " + (i + 1) + " of the header value is invalid"
+ " for header '" + name + "'");
+ " for header '" + name + "'");
}
}
}
Expand Down