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

Commit 4434d7d

Browse files
committed
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.
1 parent 0261976 commit 4434d7d

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

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

0 commit comments

Comments
 (0)