contentLengthValues = getHeaderValues(HttpHeader.CONTENT_LENGTH, headers);
if (Lists.isEmpty(contentLengthValues)) {
return;
diff --git a/apiphany-core/src/main/java/org/apiphany/header/HeaderValues.java b/apiphany-core/src/main/java/org/apiphany/header/HeaderValues.java
index 5dc807d1..be129307 100644
--- a/apiphany-core/src/main/java/org/apiphany/header/HeaderValues.java
+++ b/apiphany-core/src/main/java/org/apiphany/header/HeaderValues.java
@@ -8,11 +8,14 @@
import org.apiphany.security.Sensitive;
/**
- * Represents a node in a chain of responsibility for resolving HTTP header values. Each instance can either handle a
- * header request directly or delegate it to the next {@link HeaderValues} in the chain.
+ * Represents a node in a chain of responsibility for resolving header values. Each instance can either handle a header
+ * request directly or delegate it to the next {@link HeaderValues} in the chain.
*
* By default, this implementation returns an empty list for all header requests, serving as a base class that can be
- * extended for custom behavior.
+ * extended for custom behavior but also allowing for delegation to subsequent handlers in the chain.
+ *
+ * This class is also intended to be used as the last element in the chain, where it can provide default behavior
+ * (returning an empty list) when no other handler can resolve the header.
*
* @author Radu Sebastian LAZIN
*/
@@ -21,7 +24,12 @@ public class HeaderValues {
/**
* The string used to redact sensitive information.
*/
- public static final String REDACTED = Sensitive.Value.REDACTED;
+ public static final List REDACTED = Collections.singletonList(Sensitive.Value.REDACTED);
+
+ /**
+ * An immutable list of dangerous characters that will be replaced whenever we add a header.
+ */
+ public static final List DANGEROUS_CHARACTERS = List.of('\0', '\r', '\n');
/**
* The next {@link HeaderValues} in the chain of responsibility. If {@code null}, this represents the end of the chain.
@@ -124,4 +132,20 @@ public static String value(final L lvalue, final R rvalue) {
public static String value(final L lvalue, final R rvalue, final String separator) {
return String.join(separator, Strings.safeToString(lvalue), Strings.safeToString(rvalue));
}
+
+ /**
+ * Normalizes dangerous characters in a header field value before it is added to a headers map.
+ *
+ * Replace {@code CR}, {@code LF}, and {@code NUL} characters before processing or forwarding a header value.
+ *
+ * @param value the header field value
+ * @return the normalized header field value
+ */
+ public static String normalize(final String value) {
+ String normalizedValue = value;
+ for (char dangerousCharacter : DANGEROUS_CHARACTERS) {
+ normalizedValue = normalizedValue.replace(dangerousCharacter, ' ');
+ }
+ return normalizedValue;
+ }
}
diff --git a/apiphany-core/src/main/java/org/apiphany/header/Headers.java b/apiphany-core/src/main/java/org/apiphany/header/Headers.java
index 6c6a2b07..d760e2f8 100644
--- a/apiphany-core/src/main/java/org/apiphany/header/Headers.java
+++ b/apiphany-core/src/main/java/org/apiphany/header/Headers.java
@@ -3,13 +3,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.function.Function;
+import java.util.function.UnaryOperator;
import org.apiphany.lang.Strings;
+import org.morphix.lang.Nullables;
import org.morphix.lang.collections.Lists;
import org.morphix.lang.collections.Maps;
@@ -37,7 +39,8 @@ static Map> of(final HeaderFunction... headerFunctions) {
if (null == headerFunctions || 0 == headerFunctions.length) {
return Collections.emptyMap();
}
- var map = new HashMap>();
+ // TODO: implement a case insensitive linked hash map
+ var map = new LinkedHashMap>();
for (HeaderFunction headerFunction : headerFunctions) {
headerFunction.addTo(map);
}
@@ -54,10 +57,24 @@ static Map> of(final HeaderFunction... headerFunctions) {
* @param headers headers map
*/
static void addTo(final Map> existingHeaders, final Map headers) {
+ addTo(existingHeaders, headers, UnaryOperator.identity());
+ }
+
+ /**
+ * Adds headers to existing headers using the given value normalizer.
+ *
+ * @param header name type
+ * @param header value type
+ *
+ * @param existingHeaders existing headers
+ * @param headers headers map
+ * @param valueNormalizer function applied to each non-empty header value
+ */
+ static void addTo(final Map> existingHeaders, final Map headers, final UnaryOperator valueNormalizer) {
for (Map.Entry header : Maps.safe(headers).entrySet()) {
N headerName = header.getKey();
H headerValue = header.getValue();
- addTo(existingHeaders, headerName, headerValue);
+ addTo(existingHeaders, headerName, headerValue, valueNormalizer);
}
}
@@ -73,6 +90,22 @@ static void addTo(final Map> existingHeaders, final
* @param headerValue header value
*/
static void addTo(final Map> existingHeaders, final N headerName, final H headerValue) {
+ addTo(existingHeaders, headerName, headerValue, UnaryOperator.identity());
+ }
+
+ /**
+ * Adds a header to existing headers using the given value normalizer.
+ *
+ * @param header name type
+ * @param header value type
+ *
+ * @param existingHeaders existing headers
+ * @param headerName header name
+ * @param headerValue header value
+ * @param valueNormalizer function applied to each non-empty header value
+ */
+ static void addTo(final Map> existingHeaders, final N headerName, final H headerValue,
+ final UnaryOperator valueNormalizer) {
if (null == headerName) {
return;
}
@@ -83,15 +116,41 @@ static void addTo(final Map> existingHeaders, final
case Object[] array -> Arrays.asList(array);
default -> Collections.singletonList(headerValue);
};
- List existing = existingHeaders.computeIfAbsent(headerName.toString(), k -> new ArrayList<>());
+ String headerKey = resolveName(existingHeaders, headerName);
+ List existing = existingHeaders.computeIfAbsent(headerKey, k -> new ArrayList<>(1));
headerValues.forEach(hv -> {
String stringValue = Strings.safeToString(hv);
if (Strings.isNotEmpty(stringValue)) {
- existing.add(stringValue);
+ String normalizedValue = valueNormalizer.apply(stringValue);
+ if (Strings.isNotEmpty(normalizedValue)) {
+ existing.add(normalizedValue);
+ }
}
});
}
+ /**
+ * Resolves the header name as {@link String} via case insensitive comparison.
+ *
+ * @param header name type
+ *
+ * @param headers existing headers
+ * @param headerName header name
+ * @return the resolved header name as {@link String}
+ */
+ static String resolveName(final Map> headers, final N headerName) {
+ String headerKey = Strings.safeToString(headerName);
+ if (headers.containsKey(headerKey)) {
+ return headerKey;
+ }
+ for (String key : headers.keySet()) {
+ if (Strings.equalsIgnoreCase(key, headerKey)) {
+ return key;
+ }
+ }
+ return headerKey;
+ }
+
/**
* Adds a header to existing headers with null value. The method preserves all header values, including duplicates so
* that multiple values for the same header name are allowed and not overwritten.
@@ -149,7 +208,7 @@ static boolean contains(final N headerName, final V headerValue, final Fu
* @return true if the given headers contain the given header with the given value, false otherwise
*/
static boolean contains(final N headerName, final V headerValue, final Function> getValuesFunction) {
- return contains(headerName, headerValue, getValuesFunction, String::equalsIgnoreCase);
+ return contains(headerName, headerValue, getValuesFunction, String::equals);
}
/**
@@ -180,16 +239,8 @@ static boolean contains(final N headerName, final Map>
if (Maps.isEmpty(headers)) {
return false;
}
- String headerKey = Strings.safeToString(headerName);
- if (headers.containsKey(headerKey)) {
- return true;
- }
- for (Map.Entry> entry : headers.entrySet()) {
- if (entry.getKey().equalsIgnoreCase(headerKey)) {
- return true;
- }
- }
- return false;
+ String headerKey = resolveName(headers, headerName);
+ return headers.containsKey(headerKey);
}
/**
@@ -197,24 +248,15 @@ static boolean contains(final N headerName, final Map>
*
* @param header name type
*
- * @param header the name of the header whose values are to be retrieved.
+ * @param headerName the name of the header whose values are to be retrieved.
* @param headers the map of headers, where each key is a header name and the value is a list of header values.
* @return a list of values for the specified header. If the header is not found, an empty list is returned.
*/
- static List get(final N header, final Map> headers) {
+ static List get(final N headerName, final Map> headers) {
if (Maps.isEmpty(headers)) {
return Collections.emptyList();
}
- String headerKey = Strings.safeToString(header);
- List values = headers.get(headerKey);
- if (null != values) {
- return values;
- }
- for (Map.Entry> entry : headers.entrySet()) {
- if (entry.getKey().equalsIgnoreCase(headerKey)) {
- return entry.getValue();
- }
- }
- return Collections.emptyList();
+ String headerKey = resolveName(headers, headerName);
+ return Nullables.nonNullOrDefault(headers.get(headerKey), Collections::emptyList);
}
}
diff --git a/apiphany-core/src/main/java/org/apiphany/header/MapHeaderValues.java b/apiphany-core/src/main/java/org/apiphany/header/MapHeaderValues.java
index 97b7fe72..bfa4441f 100644
--- a/apiphany-core/src/main/java/org/apiphany/header/MapHeaderValues.java
+++ b/apiphany-core/src/main/java/org/apiphany/header/MapHeaderValues.java
@@ -1,5 +1,6 @@
package org.apiphany.header;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -42,6 +43,7 @@ public MapHeaderValues() {
@Override
public List get(final N header, final Object headers) {
return switch (headers) {
+ case null -> Collections.emptyList();
case Map, ?> map -> Headers.get(header, JavaObjects.cast(map));
default -> getNext().get(header, headers);
};
@@ -68,6 +70,7 @@ public List get(final N header, final Object headers) {
@Override
public boolean contains(final N headerName, final V headerValue, final Object headers) {
return switch (headers) {
+ case null -> false;
case Map, ?> map -> Headers.contains(headerName, headerValue, JavaObjects.