Skip to content

Commit 7d0a84d

Browse files
committed
Fix ignoring invalid double Transfer-Encoding
1 parent 924baa2 commit 7d0a84d

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

src/Response.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,10 @@ public function __construct(ReadableStreamInterface $stream, $protocol, $version
3131
$this->code = $code;
3232
$this->reasonPhrase = $reasonPhrase;
3333
$this->headers = $headers;
34-
$normalizedHeaders = array_change_key_case($headers, CASE_LOWER);
3534

36-
if (isset($normalizedHeaders['transfer-encoding']) && strtolower($normalizedHeaders['transfer-encoding']) === 'chunked') {
35+
if (strtolower($this->getHeaderLine('Transfer-Encoding')) === 'chunked') {
3736
$this->stream = new ChunkedStreamDecoder($stream);
38-
39-
foreach ($this->headers as $key => $value) {
40-
if (strcasecmp('transfer-encoding', $key) === 0) {
41-
unset($this->headers[$key]);
42-
break;
43-
}
44-
}
37+
$this->removeHeader('Transfer-Encoding');
4538
}
4639

4740
$this->stream->on('data', array($this, 'handleData'));
@@ -75,6 +68,29 @@ public function getHeaders()
7568
return $this->headers;
7669
}
7770

71+
private function removeHeader($name)
72+
{
73+
foreach ($this->headers as $key => $value) {
74+
if (strcasecmp($name, $key) === 0) {
75+
unset($this->headers[$key]);
76+
break;
77+
}
78+
}
79+
}
80+
81+
private function getHeader($name)
82+
{
83+
$name = strtolower($name);
84+
$normalized = array_change_key_case($this->headers, CASE_LOWER);
85+
86+
return isset($normalized[$name]) ? (array)$normalized[$name] : array();
87+
}
88+
89+
private function getHeaderLine($name)
90+
{
91+
return implode(', ' , $this->getHeader($name));
92+
}
93+
7894
/** @internal */
7995
public function handleData($data)
8096
{

tests/ResponseTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,36 @@ public function chunkedEncodingResponse()
129129
$response->getHeaders()
130130
);
131131
}
132+
133+
/** @test */
134+
public function doubleChunkedEncodingResponseWillBePassedAsIs()
135+
{
136+
$stream = new ThroughStream();
137+
$response = new Response(
138+
$stream,
139+
'http',
140+
'1.0',
141+
'200',
142+
'ok',
143+
array(
144+
'content-type' => 'text/plain',
145+
'transfer-encoding' => array(
146+
'chunked',
147+
'chunked'
148+
)
149+
)
150+
);
151+
152+
$this->assertSame(
153+
array(
154+
'content-type' => 'text/plain',
155+
'transfer-encoding' => array(
156+
'chunked',
157+
'chunked'
158+
)
159+
),
160+
$response->getHeaders()
161+
);
162+
}
132163
}
133164

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy