From 1ac7fe4f9c0726c70102e83fe4e3a93901d9872c Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 11 May 2020 14:00:04 +0200 Subject: [PATCH] Issue #4860 NPE from HttpFields + Fix bug with list iterator nextIndex + List iterator cannot inject null fields + minor cleanups Signed-off-by: Greg Wilkins --- .../org/eclipse/jetty/http/HttpFields.java | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java index 316ea07245a..3787fc627f8 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java @@ -303,14 +303,18 @@ public class HttpFields implements Iterable */ public List getValuesList(String name) { - final List list = new ArrayList<>(); + List 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,7 +715,8 @@ public class HttpFields implements Iterable public void add(HttpHeader header, HttpHeaderValue value) { - add(header, value.toString()); + if (value != null) + add(header, value.toString()); } /** @@ -966,8 +971,11 @@ public class HttpFields implements Iterable * * @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 @Override public int nextIndex() { - return _cursor + 1; + return _cursor; } @Override @@ -1199,16 +1207,22 @@ public class HttpFields implements Iterable { if (_current < 0) throw new IllegalStateException(); - _fields[_current] = field; + if (field == null) + remove(); + else + _fields[_current] = field; } @Override public void add(HttpField field) { - _fields = Arrays.copyOf(_fields, _fields.length + 1); - System.arraycopy(_fields, _cursor, _fields, _cursor + 1, _size++); - _fields[_cursor++] = field; - _current = -1; + if (field != null) + { + _fields = Arrays.copyOf(_fields, _fields.length + 1); + System.arraycopy(_fields, _cursor, _fields, _cursor + 1, _size++); + _fields[_cursor++] = field; + _current = -1; + } } } }