Merge pull request #4059 from eclipse/jetty-9.4.x-4057-response.containsHeader-npe

Issue #4057 - NPE in HttpFields.containsKey
This commit is contained in:
Joakim Erdfelt 2019-09-05 13:08:01 -05:00 committed by GitHub
commit 4717a8e049
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 3 deletions

View File

@ -37,7 +37,10 @@ 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 +328,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());
}

View File

@ -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,41 @@ 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);
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
{