diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java index db7bce9072b..e4b1aa18953 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java @@ -37,7 +37,14 @@ public class HttpField public HttpField(HttpHeader header, String name, String value) { _header = header; - _name = name; + if (_header != null && name == null) + { + _name = _header.asString(); + } + else + { + _name = Objects.requireNonNull(name); + } _value = value; } @@ -325,8 +332,6 @@ public class HttpField return false; if (!_name.equalsIgnoreCase(field.getName())) return false; - if (_value == null && field.getValue() != null) - return false; return Objects.equals(_value, field.getValue()); } diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpFieldsTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpFieldsTest.java index bb8a71e4012..e911cbde5e1 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpFieldsTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpFieldsTest.java @@ -30,6 +30,8 @@ import java.util.NoSuchElementException; import org.eclipse.jetty.util.BufferUtil; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; @@ -666,6 +668,42 @@ public class HttpFieldsTest assertFalse(header.containsKey("n11")); } + @ParameterizedTest + @ValueSource(strings = {"Host", "host", "HOST", "HoSt", "Connection", "CONNECTION", "connection", "CoNnEcTiOn"}) + public void testContainsKeyTrue(String keyName) + { + HttpFields fields = new HttpFields(); + fields.put("Host", "localhost"); + HttpField namelessField = new HttpField(HttpHeader.CONNECTION, null, "bogus"); + fields.put(namelessField); + + assertTrue(fields.containsKey(keyName), "containsKey('" + keyName + "')"); + } + + @ParameterizedTest + @ValueSource(strings = {"Content-Type", "Content-Length", "X-Bogus", ""}) + public void testContainsKeyFalse(String keyName) + { + HttpFields fields = new HttpFields(); + fields.add("Host", "localhost"); + HttpField namelessField = new HttpField(HttpHeader.CONNECTION, null, "bogus"); + fields.put(namelessField); + System.out.println(fields); + + assertFalse(fields.containsKey(keyName), "containsKey('" + keyName + "')"); + } + + @Test + public void testPreventNullField() + { + HttpFields fields = new HttpFields(); + assertThrows(NullPointerException.class, () -> + { + HttpField nullNullField = new HttpField(null, null, "bogus"); + fields.put(nullNullField); + }); + } + @Test public void testIteration() throws Exception {