Issue #4860 NPE from HttpFields

+ Fix bug with list iterator nextIndex
 + List iterator cannot inject null fields
 + minor cleanups

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2020-05-11 14:00:04 +02:00
parent 6af6af6419
commit 1ac7fe4f9c
1 changed files with 23 additions and 9 deletions

View File

@ -303,14 +303,18 @@ public class HttpFields implements Iterable<HttpField>
*/
public List<String> getValuesList(String name)
{
final List<String> list = new ArrayList<>();
List<String> list = null;
for (int i = 0; i < _size; i++)
{
HttpField f = _fields[i];
if (f.getName().equalsIgnoreCase(name))
{
if (list == null)
list = new ArrayList<>(size() - i);
list.add(f.getValue());
}
return list;
}
return list == null ? Collections.emptyList() : list;
}
/**
@ -711,6 +715,7 @@ public class HttpFields implements Iterable<HttpField>
public void add(HttpHeader header, HttpHeaderValue value)
{
if (value != null)
add(header, value.toString());
}
@ -966,8 +971,11 @@ public class HttpFields implements Iterable<HttpField>
*
* @param fields the fields to add
*/
@Deprecated
public void add(HttpFields fields)
{
// TODO this implementation doesn't do what the javadoc says and is really the same
// as addAll, which is renamed to add anyway in 10.
if (fields == null)
return;
@ -1185,7 +1193,7 @@ public class HttpFields implements Iterable<HttpField>
@Override
public int nextIndex()
{
return _cursor + 1;
return _cursor;
}
@Override
@ -1199,11 +1207,16 @@ public class HttpFields implements Iterable<HttpField>
{
if (_current < 0)
throw new IllegalStateException();
if (field == null)
remove();
else
_fields[_current] = field;
}
@Override
public void add(HttpField field)
{
if (field != null)
{
_fields = Arrays.copyOf(_fields, _fields.length + 1);
System.arraycopy(_fields, _cursor, _fields, _cursor + 1, _size++);
@ -1212,3 +1225,4 @@ public class HttpFields implements Iterable<HttpField>
}
}
}
}