Merge pull request #8345 from cakephp/issue-8330

Fix parsing empty header values.
This commit is contained in:
Mark Story 2016-02-26 23:01:59 -05:00
commit eae7a8926b
2 changed files with 29 additions and 10 deletions

View file

@ -268,18 +268,28 @@ class HttpSocketResponse implements ArrayAccess {
return false;
}
preg_match_all("/(.+):(.+)(?:(?<![\t ])\r\n|\$)/Uis", $header, $matches, PREG_SET_ORDER);
preg_match_all("/(.+):(.+)(?:\r\n|\$)/Uis", $header, $matches, PREG_SET_ORDER);
$lines = explode("\r\n", $header);
$header = array();
foreach ($matches as $match) {
list(, $field, $value) = $match;
foreach ($lines as $line) {
if (strlen($line) === 0) {
continue;
}
$continuation = false;
$first = substr($line, 0, 1);
// Multi-line header
if ($first === ' ' || $first === "\t") {
$value .= preg_replace("/\s+/", ' ', $line);
$continuation = true;
} elseif (strpos($line, ':') !== false) {
list($field, $value) = explode(':', $line, 2);
$field = $this->_unescapeToken($field);
}
$value = trim($value);
$value = preg_replace("/[\t ]\r\n/", "\r\n", $value);
$field = $this->_unescapeToken($field);
if (!isset($header[$field])) {
if (!isset($header[$field]) || $continuation) {
$header[$field] = $value;
} else {
$header[$field] = array_merge((array)$header[$field], (array)$value);

View file

@ -267,10 +267,19 @@ class HttpResponseTest extends CakeTestCase {
);
$this->assertEquals($expected, $r);
$header = "Multi-Line: I am a \r\nmulti line\t\r\nfield value.\r\nSingle-Line: I am not\r\n";
$header = "Date:Sat, 07 Apr 2007 10:10:25 GMT\r\nLink: \r\nX-Total-Count: 19\r\n";
$r = $this->HttpResponse->parseHeader($header);
$expected = array(
'Multi-Line' => "I am a\r\nmulti line\r\nfield value.",
'Date' => 'Sat, 07 Apr 2007 10:10:25 GMT',
'Link' => '',
'X-Total-Count' => '19',
);
$this->assertEquals($expected, $r);
$header = "Multi-Line: I am a\r\n multi line \r\n\tfield value.\r\nSingle-Line: I am not\r\n";
$r = $this->HttpResponse->parseHeader($header);
$expected = array(
'Multi-Line' => "I am a multi line field value.",
'Single-Line' => 'I am not'
);
$this->assertEquals($expected, $r);