Fix NPE in PreEncodedHttpField constructor (#2488)

Which might happen when given `HttpHeader` is null. It might be null
when most generic constructor `#PreEncodedHttpField(HttpHeader, String, String)`
is invoked with a null argument or when `#PreEncodedHttpField(String, String)`
constructor is used. Later is valuable for custom headers. NPE happens
because of an attempt to call `HttpHeader#asString()` on a null argument.

This commit fixes the problem by making encoders use specified header
name, instead of trying to retrieve it from the `HttpHeader` object.

Signed-off-by: lutovich <konstantin.lutovich@neo4j.com>
This commit is contained in:
Konstantin Lutovich 2018-04-26 00:35:24 +02:00 committed by Greg Wilkins
parent ed51c4a9e6
commit 25888f0d83
2 changed files with 12 additions and 1 deletions

View File

@ -101,7 +101,7 @@ public class PreEncodedHttpField extends HttpField
{
super(header,name, value);
for (int i=0;i<__encoders.length;i++)
_encodedField[i]=__encoders[i].getEncodedField(header,header.asString(),value);
_encodedField[i]=__encoders[i].getEncodedField(header,name,value);
}
public PreEncodedHttpField(HttpHeader header,String value)

View File

@ -21,6 +21,7 @@ package org.eclipse.jetty.http;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@ -181,4 +182,14 @@ public class HttpFieldTest
assertEquals("Accept: something\r\n",s);
}
@Test
public void testCachedFieldWithHeaderName()
{
PreEncodedHttpField field = new PreEncodedHttpField("X-My-Custom-Header", "something");
assertNull(field.getHeader());
assertEquals("X-My-Custom-Header", field.getName());
assertEquals("something", field.getValue());
}
}