Fix #490: Allow malformed response header to be parsed without error

This commit is contained in:
Zach Borboa
2018-01-09 01:58:51 -08:00
parent d0747c41a3
commit cbacd9a68b
2 changed files with 29 additions and 8 deletions
+10 -8
View File
@@ -1363,14 +1363,16 @@ class Curl
$raw_headers_count = count($raw_headers);
for ($i = 1; $i < $raw_headers_count; $i++) {
list($key, $value) = explode(':', $raw_headers[$i], 2);
$key = trim($key);
$value = trim($value);
// Use isset() as array_key_exists() and ArrayAccess are not compatible.
if (isset($http_headers[$key])) {
$http_headers[$key] .= ',' . $value;
} else {
$http_headers[$key] = $value;
if (strpos($raw_headers[$i], ':') !== false) {
list($key, $value) = explode(':', $raw_headers[$i], 2);
$key = trim($key);
$value = trim($value);
// Use isset() as array_key_exists() and ArrayAccess are not compatible.
if (isset($http_headers[$key])) {
$http_headers[$key] .= ',' . $value;
} else {
$http_headers[$key] = $value;
}
}
}
+19
View File
@@ -2615,6 +2615,25 @@ class CurlTest extends \PHPUnit\Framework\TestCase
$this->assertArrayHasKey('Status-Line', $response_headers);
}
public function testMalformedResponseHeaders()
{
$response =
'HTTP/1.0 403 Forbidden' . "\n" .
'Cache-Control: no-cache' . "\n" .
'Content-Type: text/html' . "\n" .
'Strict-Transport-Security: max-age=0' .
"\r\n" .
"\n";
$reflector = new \ReflectionClass('\Curl\Curl');
$reflection_method = $reflector->getMethod('parseResponseHeaders');
$reflection_method->setAccessible(true);
$curl = new Curl();
$response_headers = $reflection_method->invoke($curl, $response);
$this->assertTrue($response_headers instanceof CaseInsensitiveArray);
}
public function testArrayToStringConversion()
{
$test = new Test();