Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public BasicListHeaderIterator(final List<? extends Header> headers, final Strin
this.lastIndex = -1;
}

BasicListHeaderIterator(final List<? extends Header> headers, final int currentIndex, final String name) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ok2c
The new BasicListHeaderIterator constructor does not initialize lastIndex, so it defaults to 0. Calling remove() before next() then removes the first header instead of throwing IllegalStateException. I confirmed it with a regression test. Initializing lastIndex to -1 fixes it.

Otherwise, the change looks good to me.

Test that prove.


@Test
void testIteratorByNameRemoveBeforeNext() {
    final HeaderGroup headerGroup = new HeaderGroup();
    final Header headerA = new BasicHeader("a", "a-one");
    final Header headerB = new BasicHeader("b", "b-one");
    headerGroup.setHeaders(headerA, headerB);

    final Iterator<Header> iterator = headerGroup.headerIterator("b");

    Assertions.assertThrows(IllegalStateException.class, iterator::remove);
    Assertions.assertArrayEquals(
            new Header[] { headerA, headerB },
            headerGroup.getHeaders());
}

super();
this.allHeaders = headers;
this.headerName = name;
this.currentIndex = currentIndex;
}

/**
* Determines the index of the next header.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,19 @@ public int countHeaders(final String name) {
*
* @return iterator over this group of headers.
*
* <p>
* IMPORTANT: please note that if the header group mutates while the
* iterator returned by this method still has pending elements
* the sequence of headers produced by such iterator is considered
* unstable and can be incorrect.
*
* @since 5.0
*/
@Override
public Iterator<Header> headerIterator() {
if (this.headers.isEmpty()) {
return NullHeaderIterator.INSTANCE;
}
return new BasicListHeaderIterator(this.headers, null);
}

Expand All @@ -360,10 +369,28 @@ public Iterator<Header> headerIterator() {
*
* @return iterator over some headers in this group.
*
* <p>
* IMPORTANT: please note that if the header group mutates while the
* iterator returned by this method still has pending elements
* the sequence of headers produced by such iterator is considered
* unstable and can be incorrect.
*
* @since 5.0
*/
@Override
public Iterator<Header> headerIterator(final String name) {
if (this.headers.isEmpty()) {
return NullHeaderIterator.INSTANCE;
}
if (name != null) {
for (int i = 0; i < this.headers.size(); i++) {
final Header h = this.headers.get(i);
if (h.getName().equalsIgnoreCase(name)) {
return new BasicListHeaderIterator(this.headers, i, name);
}
}
return NullHeaderIterator.INSTANCE;
}
return new BasicListHeaderIterator(this.headers, name);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/

package org.apache.hc.core5.http.message;

import java.util.Iterator;
import java.util.NoSuchElementException;

import org.apache.hc.core5.http.Header;

final class NullHeaderIterator implements Iterator<Header> {

final static NullHeaderIterator INSTANCE = new NullHeaderIterator();

@Override
public boolean hasNext() {
return false;
}

@Override
public Header next() throws NoSuchElementException {
throw new NoSuchElementException("Iteration already finished.");
}

@Override
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException("Removing headers is not supported.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,49 @@ void testCondensedHeader() {
}

@Test
void testIterator() {
void testEmptyListIterator() {
final HeaderGroup headergroup = new HeaderGroup();
final Iterator<Header> i = headergroup.headerIterator();
Assertions.assertNotNull(i);
Assertions.assertFalse(i.hasNext());
}

@Test
void testEmptyListIteratorByName() {
final HeaderGroup headergroup = new HeaderGroup();
final Iterator<Header> i = headergroup.headerIterator("some-header");
Assertions.assertNotNull(i);
Assertions.assertFalse(i.hasNext());
}

@Test
void testNonEmptyListIteratorByName() {
final HeaderGroup headergroup = new HeaderGroup();
headergroup.setHeaders(
new BasicHeader("a", "a-one"),
new BasicHeader("b", "b-one"),
new BasicHeader("a", "a-two"),
new BasicHeader("b", "b-two"),
new BasicHeader("b", "b-three"));
final Iterator<Header> it1 = headergroup.headerIterator("a");
Assertions.assertNotNull(it1);
Assertions.assertTrue(it1.hasNext());
Assertions.assertEquals("a-one", it1.next().getValue());
Assertions.assertTrue(it1.hasNext());
Assertions.assertEquals("a-two", it1.next().getValue());
Assertions.assertFalse(it1.hasNext());

final Iterator<Header> it2 = headergroup.headerIterator("b");
Assertions.assertNotNull(it2);
Assertions.assertTrue(it2.hasNext());
Assertions.assertEquals("b-one", it2.next().getValue());
Assertions.assertTrue(it2.hasNext());
Assertions.assertEquals("b-two", it2.next().getValue());
Assertions.assertTrue(it2.hasNext());
Assertions.assertEquals("b-three", it2.next().getValue());
Assertions.assertFalse(it2.hasNext());
}

@Test
void testHeaderRemove() {
final HeaderGroup headergroup = new HeaderGroup();
Expand Down
Loading