Added guards against NPE in case HttpField.getValue() returns null.

This commit is contained in:
Simone Bordet 2016-07-15 15:45:33 +02:00
parent 389eb68c3c
commit a3b5e7ebb7
4 changed files with 39 additions and 20 deletions

View File

@ -271,7 +271,9 @@ public class MultiPartContentProvider extends AbstractTypedContentProvider imple
continue;
buffer.write(field.getName().getBytes(StandardCharsets.US_ASCII));
buffer.write(COLON_SPACE_BYTES);
buffer.write(field.getValue().getBytes(StandardCharsets.UTF_8));
String value = field.getValue();
if (value != null)
buffer.write(value.getBytes(StandardCharsets.UTF_8));
buffer.write(CR_LF_BYTES);
}
buffer.write(CR_LF_BYTES);

View File

@ -83,6 +83,9 @@ public class HttpField
public String[] getValues()
{
if (_value == null)
return null;
ArrayList<String> list = new ArrayList<>();
int state = 0;
int start=0;
@ -412,7 +415,7 @@ public class HttpField
@Override
public int hashCode()
{
int vhc = _value==null?0:_value.hashCode();
int vhc = Objects.hashCode(_value);
if (_header==null)
return vhc ^ nameHashCode();
return vhc ^ _header.hashCode();

View File

@ -410,7 +410,8 @@ public class HpackContext
public int getSize()
{
return 32+_field.getName().length()+_field.getValue().length();
String value = _field.getValue();
return 32 + _field.getName().length() + (value == null ? 0 : value.length());
}
public HttpField getHttpField()

View File

@ -67,62 +67,74 @@ public class MetaDataBuilder
}
public void emit(HttpField field)
{
int field_size = field.getName().length()+field.getValue().length();
{
HttpHeader header = field.getHeader();
String name = field.getName();
String value = field.getValue();
int field_size = name.length() + (value == null ? 0 : value.length());
_size+=field_size;
if (_size>_maxSize)
throw new BadMessageException(HttpStatus.REQUEST_ENTITY_TOO_LARGE_413,"Header size "+_size+">"+_maxSize);
if (field instanceof StaticTableHttpField)
{
StaticTableHttpField value = (StaticTableHttpField)field;
switch(field.getHeader())
StaticTableHttpField staticField = (StaticTableHttpField)field;
switch(header)
{
case C_STATUS:
_status=(Integer)value.getStaticValue();
_status=(Integer)staticField.getStaticValue();
break;
case C_METHOD:
_method=field.getValue();
_method=value;
break;
case C_SCHEME:
_scheme = (HttpScheme)value.getStaticValue();
_scheme = (HttpScheme)staticField.getStaticValue();
break;
default:
throw new IllegalArgumentException(field.getName());
throw new IllegalArgumentException(name);
}
}
else if (field.getHeader()!=null)
else if (header!=null)
{
switch(field.getHeader())
switch(header)
{
case C_STATUS:
_status=field.getIntValue();
break;
case C_METHOD:
_method=field.getValue();
_method=value;
break;
case C_SCHEME:
_scheme = HttpScheme.CACHE.get(field.getValue());
if (value != null)
_scheme = HttpScheme.CACHE.get(value);
break;
case C_AUTHORITY:
_authority=(field instanceof HostPortHttpField)?((HostPortHttpField)field):new AuthorityHttpField(field.getValue());
if (field instanceof HostPortHttpField)
_authority = (HostPortHttpField)field;
else if (value != null)
_authority = new AuthorityHttpField(value);
break;
case HOST:
// :authority fields must come first. If we have one, ignore the host header as far as authority goes.
if (_authority==null)
_authority=(field instanceof HostPortHttpField)?((HostPortHttpField)field):new AuthorityHttpField(field.getValue());
{
if (field instanceof HostPortHttpField)
_authority = (HostPortHttpField)field;
else if (value != null)
_authority = new AuthorityHttpField(value);
}
_fields.add(field);
break;
case C_PATH:
_path = field.getValue();
_path = value;
break;
case CONTENT_LENGTH:
@ -131,13 +143,14 @@ public class MetaDataBuilder
break;
default:
if (field.getName().charAt(0)!=':')
if (name.charAt(0)!=':')
_fields.add(field);
break;
}
}
else
{
if (field.getName().charAt(0)!=':')
if (name.charAt(0)!=':')
_fields.add(field);
}
}