Skip to content

Commit

Permalink
Removed interference in the name of the headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Nov 13, 2010
1 parent 44629bd commit 85607a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
28 changes: 12 additions & 16 deletions cake/libs/http_socket.php
Expand Up @@ -864,6 +864,7 @@ protected function _httpSerialize($data = array()) {
* Builds the header.
*
* @param array $header Header to build
* @param string $mode
* @return string Header built from array
*/
protected function _buildHeader($header, $mode = 'standard') {
Expand All @@ -873,6 +874,17 @@ protected function _buildHeader($header, $mode = 'standard') {
return false;
}

$fieldsInHeader = array();
foreach ($header as $key => $value) {
$lowKey = strtolower($key);
if (array_key_exists($lowKey, $fieldsInHeader)) {
$header[$fieldsInHeader[$lowKey]] = $value;
unset($header[$key]);
} else {
$fieldsInHeader[$lowKey] = $key;
}
}

$returnHeader = '';
foreach ($header as $field => $contents) {
if (is_array($contents) && $mode == 'standard') {
Expand All @@ -896,16 +908,6 @@ protected function _buildHeader($header, $mode = 'standard') {
*/
protected function _parseHeader($header) {
if (is_array($header)) {
foreach ($header as $field => $value) {
unset($header[$field]);
$field = strtolower($field);
preg_match_all('/(?:^|(?<=-))[a-z]/U', $field, $offsets, PREG_OFFSET_CAPTURE);

foreach ($offsets[0] as $offset) {
$field = substr_replace($field, strtoupper($offset[0]), $offset[1], 1);
}
$header[$field] = $value;
}
return $header;
} elseif (!is_string($header)) {
return false;
Expand All @@ -922,12 +924,6 @@ protected function _parseHeader($header) {

$field = $this->_unescapeToken($field);

$field = strtolower($field);
preg_match_all('/(?:^|(?<=-))[a-z]/U', $field, $offsets, PREG_OFFSET_CAPTURE);
foreach ($offsets[0] as $offset) {
$field = substr_replace($field, strtoupper($offset[0]), $offset[1], 1);
}

if (!isset($header[$field])) {
$header[$field] = $value;
} else {
Expand Down
14 changes: 7 additions & 7 deletions cake/tests/cases/libs/http_socket.test.php
Expand Up @@ -529,7 +529,7 @@ function testRequest() {
$expectation['request']['raw'] = $expectation['request']['line'].$expectation['request']['header']."\r\n".$raw;

$r = array('config' => $this->Socket->config, 'request' => $this->Socket->request);
$v = $this->assertEquals($r, $expectation, '%s in test #'.$i.' ');
$v = $this->assertEquals($r, $expectation, 'Failed test #'.$i.' ');
$expectation['request']['raw'] = $raw;
}

Expand Down Expand Up @@ -948,13 +948,13 @@ function testDecodeChunkedBody() {
$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n0\r\nfoo-header: bar\r\ncake: PHP\r\n\r\n";
$r = $this->Socket->decodeChunkedBody($encoded);
$this->assertEquals($r['body'], $decoded);
$this->assertEquals($r['header'], array('Foo-Header' => 'bar', 'Cake' => 'PHP'));
$this->assertEquals($r['header'], array('foo-header' => 'bar', 'cake' => 'PHP'));

$this->Socket->quirksMode = true;
$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\nfoo-header: bar\r\ncake: PHP\r\n\r\n";
$r = $this->Socket->decodeChunkedBody($encoded);
$this->assertEquals($r['body'], $decoded);
$this->assertEquals($r['header'], array('Foo-Header' => 'bar', 'Cake' => 'PHP'));
$this->assertEquals($r['header'], array('foo-header' => 'bar', 'cake' => 'PHP'));

$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n";
$r = $this->Socket->decodeChunkedBody($encoded);
Expand Down Expand Up @@ -1393,7 +1393,7 @@ function testParseHeader() {
$this->Socket->reset();

$r = $this->Socket->parseHeader(array('foo' => 'Bar', 'fOO-bAr' => 'quux'));
$this->assertEquals($r, array('Foo' => 'Bar', 'Foo-Bar' => 'quux'));
$this->assertEquals($r, array('foo' => 'Bar', 'fOO-bAr' => 'quux'));

$r = $this->Socket->parseHeader(true);
$this->assertEquals($r, false);
Expand All @@ -1416,9 +1416,9 @@ function testParseHeader() {
$header = "people: Jim,John\r\nfoo-LAND: Bar\r\ncAKe-PHP: rocks\r\n";
$r = $this->Socket->parseHeader($header);
$expected = array(
'People' => 'Jim,John'
, 'Foo-Land' => 'Bar'
, 'Cake-Php' => 'rocks'
'people' => 'Jim,John'
, 'foo-LAND' => 'Bar'
, 'cAKe-PHP' => 'rocks'
);
$this->assertEquals($r, $expected);

Expand Down

0 comments on commit 85607a9

Please sign in to comment.