Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit cb1ba07

Browse files
committed
Merge branch 'hotfix/140-ensure-last-header-added-by-name-is-returned' into develop
Forward port #140
2 parents c6c547b + c341ed4 commit cb1ba07

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ All notable changes to this project will be documented in this file, in reverse
4343

4444
### Fixed
4545

46+
- [#140](https://github.com/zendframework/zend-http/pull/140) fixes retrieval of headers when multiple headers of the same name
47+
are added to the `Headers` instance; it now ensures that the last header added of the same
48+
type is retrieved when it is not a multi-value type. Previous values are overwritten.
49+
4650
- [#112](https://github.com/zendframework/zend-http/pull/112) provides performance improvements when parsing large chunked messages.
4751

4852
- introduces changes to `Response::fromString()` to pull the next line of the response

src/Headers.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,27 @@ public function addHeaderLine($headerFieldNameOrLine, $fieldValue = null)
213213
*/
214214
public function addHeader(Header\HeaderInterface $header)
215215
{
216-
$this->headersKeys[] = static::createKey($header->getFieldName());
217-
$this->headers[] = $header;
216+
$key = static::createKey($header->getFieldName());
217+
$index = array_search($key, $this->headersKeys);
218+
219+
// No header by that key presently; append key and header to list.
220+
if ($index === false) {
221+
$this->headersKeys[] = $key;
222+
$this->headers[] = $header;
223+
return $this;
224+
}
225+
226+
// Header exists, and is a multi-value header; append key and header to
227+
// list (as multi-value headers are aggregated on retrieval)
228+
$class = ($this->getPluginClassLoader()->load(str_replace('-', '', $key))) ?: Header\GenericHeader::class;
229+
if (in_array(Header\MultipleHeaderInterface::class, class_implements($class, true))) {
230+
$this->headersKeys[] = $key;
231+
$this->headers[] = $header;
232+
return $this;
233+
}
234+
235+
// Otherwise, we replace the current instance.
236+
$this->headers[$index] = $header;
218237

219238
return $this;
220239
}
@@ -286,6 +305,7 @@ public function get($name)
286305
if (is_array($this->headers[$index])) {
287306
return $this->lazyLoadHeader($index);
288307
}
308+
289309
return $this->headers[$index];
290310
}
291311

test/HeadersTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ public function testHeadersHasAndGetWorkProperly()
119119
$this->assertSame($f, $headers->get('foo'));
120120
}
121121

122+
public function testHeadersGetReturnsLastAddedHeaderValue()
123+
{
124+
$headers = new Headers();
125+
$headers->addHeaders([
126+
new Header\GenericHeader('Foo', 'bar'),
127+
]);
128+
$headers->addHeader(new Header\GenericHeader('Foo', $value = 'baz'));
129+
130+
$this->assertEquals($value, $headers->get('foo')->getFieldValue());
131+
}
132+
122133
public function testHeadersAggregatesHeaderObjects()
123134
{
124135
$fakeHeader = new Header\GenericHeader('Fake', 'bar');

0 commit comments

Comments
 (0)