Skip to content

Commit

Permalink
[mms] Horde_Http_Base_Response#getHeader() is now case-insensitive fo…
Browse files Browse the repository at this point in the history
…r the header input (Bug #13736).
  • Loading branch information
slusarz committed Dec 4, 2014
1 parent da5b8ad commit 25a839f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
45 changes: 28 additions & 17 deletions framework/Http/lib/Horde/Http/Response/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ abstract class Horde_Http_Response_Base
*/
public $headers;

/**
* Case-insensitive list of headers.
*
* @var Horde_Support_CaseInsensitiveArray
*/
protected $_headers;

/**
* Parses an array of response headers, mindful of line continuations, etc.
*
Expand All @@ -57,7 +64,7 @@ protected function _parseHeaders($headers)
$headers = preg_split("/\r?\n/", $headers);
}

$this->headers = array();
$this->_headers = new Horde_Support_CaseInsensitiveArray();

$lastHeader = null;
foreach ($headers as $headerLine) {
Expand All @@ -69,7 +76,7 @@ protected function _parseHeaders($headers)
if (preg_match('/^HTTP\/(\d.\d) (\d{3})/', $headerLine, $httpMatches)) {
$this->httpVersion = $httpMatches[1];
$this->code = (int)$httpMatches[2];
$this->headers = array();
$this->_headers = new Horde_Support_CaseInsensitiveArray();
$lastHeader = null;
}

Expand All @@ -78,29 +85,33 @@ protected function _parseHeaders($headers)
break;
}
if (preg_match('|^([\w-]+):\s+(.+)|', $headerLine, $m)) {
unset($lastHeader);
$headerName = strtolower($m[1]);
$headerName = $m[1];
$headerValue = $m[2];

if (isset($this->headers[$headerName])) {
if (!is_array($this->headers[$headerName])) {
$this->headers[$headerName] = array($this->headers[$headerName]);
if ($tmp = $this->_headers[$headerName]) {
if (!is_array($tmp)) {
$this->_headers[$headerName] = array($tmp);
}

$this->headers[$headerName][] = $headerValue;
$this->_headers[$headerName][] = $headerValue;
} else {
$this->headers[$headerName] = $headerValue;
$this->_headers[$headerName] = $headerValue;
}
$lastHeader = $headerName;
} elseif (preg_match("|^\s+(.+)$|", $headerLine, $m) && !is_null($lastHeader)) {
if (is_array($this->headers[$lastHeader])) {
end($this->headers[$lastHeader]);
$this->headers[$lastHeader][key($this->headers[$lastHeader])] .= $m[1];
} elseif (preg_match("|^\s+(.+)$|", $headerLine, $m) &&
!is_null($lastHeader)) {
if (is_array($this->_headers[$lastHeader])) {
$tmp = $this->_headers[$lastHeader];
end($tmp);
$tmp[key($tmp)] .= $m[1];
$this->_headers[$lastHeader] = $tmp;
} else {
$this->headers[$lastHeader] .= $m[1];
$this->_headers[$lastHeader] .= $m[1];
}
}
}

$this->headers = $this->_headers->getArrayCopy();
}

/**
Expand All @@ -126,12 +137,12 @@ public function getStream()
* Returns the value of a single response header.
*
* @param string $header Header name to get ('Content-Type',
* 'Content-Length', etc.). This is case sensitive.
* 'Content-Length', etc.).
*
* @return string HTTP header value.
* @return string HTTP header value.
*/
public function getHeader($header)
{
return isset($this->headers[$header]) ? $this->headers[$header] : null;
return $this->_headers[$header];
}
}
7 changes: 4 additions & 3 deletions framework/Http/lib/Horde/Http/Response/Peclhttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public function __construct($uri, HttpMessage $message)
$this->httpVersion = $message->getHttpVersion();
$this->code = $message->getResponseCode();
$this->_message = $message;
foreach ($message->getHeaders() as $k => $v) {
$this->headers[strtolower($k)] = $v;
}
$this->_headers = new Horde_Support_CaseInsensitiveArray(
$message->getHeaders()
);
$this->headers = $this->_headers->getArrayCopy();
}

public function getBody()
Expand Down
7 changes: 4 additions & 3 deletions framework/Http/lib/Horde/Http/Response/Peclhttp2.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public function __construct($uri, \http\Client\Response $response)
$this->httpVersion = $response->getHttpVersion();
$this->code = $response->getResponseCode();
$this->_response = $response;
foreach ($response->getHeaders() as $k => $v) {
$this->headers[strtolower($k)] = $v;
}
$this->_headers = new Horde_Support_CaseInsensitiveArray(
$response->getHeaders()
);
$this->headers = $this->_headers->getArrayCopy();
}

public function getBody()
Expand Down
4 changes: 2 additions & 2 deletions framework/Http/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</stability>
<license uri="http://www.horde.org/licenses/bsd">BSD-2-Clause</license>
<notes>
*
* [mms] Horde_Http_Base_Response#getHeader() is now case-insensitive for the header input (Bug #13736).
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand Down Expand Up @@ -477,7 +477,7 @@
<date>2014-05-02</date>
<license uri="http://www.horde.org/licenses/bsd">BSD-2-Clause</license>
<notes>
*
* [mms] Horde_Http_Base_Response#getHeader() is now case-insensitive for the header input (Bug #13736).
</notes>
</release>
</changelog>
Expand Down

0 comments on commit 25a839f

Please sign in to comment.