From 0261976df1cb0f85ac988e1c45f038d228e47b78 Mon Sep 17 00:00:00 2001 From: Stanislav Anisimov Date: Thu, 11 Jan 2018 15:15:42 +0300 Subject: [PATCH 1/3] Test if header returns last added value --- test/HeadersTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/HeadersTest.php b/test/HeadersTest.php index 603c5dc51a..7437d5ecf4 100644 --- a/test/HeadersTest.php +++ b/test/HeadersTest.php @@ -119,6 +119,17 @@ public function testHeadersHasAndGetWorkProperly() $this->assertSame($f, $headers->get('foo')); } + public function testHeadersGetReturnsLastAddedHeaderValue() + { + $headers = new Headers(); + $headers->addHeaders([ + new Header\GenericHeader('Foo', 'bar'), + ]); + $headers->addHeader(new Header\GenericHeader('Foo', $value = 'baz')); + + $this->assertEquals($value, $headers->get('foo')->getFieldValue()); + } + public function testHeadersAggregatesHeaderObjects() { $fakeHeader = new Header\GenericHeader('Fake', 'bar'); From 4434d7d99152bf0ae40b95f7f6d3ccfe977856c5 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Thu, 26 Apr 2018 15:49:30 -0500 Subject: [PATCH 2/3] Ensure that adding a header overwrites when not a multi-value header If the header is not a multi-value header, but a header of that type was previously injected, we should overwrite it. --- src/Headers.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Headers.php b/src/Headers.php index 2986959508..e3897c1a0f 100644 --- a/src/Headers.php +++ b/src/Headers.php @@ -213,8 +213,27 @@ public function addHeaderLine($headerFieldNameOrLine, $fieldValue = null) */ public function addHeader(Header\HeaderInterface $header) { - $this->headersKeys[] = static::createKey($header->getFieldName()); - $this->headers[] = $header; + $key = static::createKey($header->getFieldName()); + $index = array_search($key, $this->headersKeys); + + // No header by that key presently; append key and header to list. + if ($index === false) { + $this->headersKeys[] = $key; + $this->headers[] = $header; + return $this; + } + + // Header exists, and is a multi-value header; append key and header to + // list (as multi-value headers are aggregated on retrieval) + $class = ($this->getPluginClassLoader()->load(str_replace('-', '', $key))) ?: Header\GenericHeader::class; + if (in_array(Header\MultipleHeaderInterface::class, class_implements($class, true))) { + $this->headersKeys[] = $key; + $this->headers[] = $header; + return $this; + } + + // Otherwise, we replace the current instance. + $this->headers[$index] = $header; return $this; } @@ -286,6 +305,7 @@ public function get($name) if (is_array($this->headers[$index])) { return $this->lazyLoadHeader($index); } + return $this->headers[$index]; } From c341ed4afb43d7fca209fad9605c08904acd7802 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Thu, 26 Apr 2018 15:51:54 -0500 Subject: [PATCH 3/3] Adds CHANGELOG entry for #140 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70d9406a68..75ee1f1dd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,10 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed +- [#140](https://github.com/zendframework/zend-http/pull/140) fixes retrieval of headers when multiple headers of the same name + are added to the `Headers` instance; it now ensures that the last header added of the same + type is retrieved when it is not a multi-value type. Previous values are overwritten. + - [#112](https://github.com/zendframework/zend-http/pull/112) provides performance improvements when parsing large chunked messages. - introduces changes to `Response::fromString()` to pull the next line of the response