Make field names for headers case insensitive.

This commit is contained in:
PJ Dietz 2012-12-27 08:41:56 -05:00
parent 06717be66f
commit 40ca6bea87
3 changed files with 61 additions and 17 deletions

View File

@ -24,6 +24,15 @@ abstract class Message {
*/ */
protected $headers; protected $headers;
/**
* Associative array of lowercase header field names as keys with
* corresponding case sensitive field names from the $headers array as
* values.
*
* @var array
*/
protected $headerLookup;
/** /**
* Name of the protocol to use. * Name of the protocol to use.
* @var string * @var string
@ -114,13 +123,21 @@ abstract class Message {
/** /**
* Return the value of a given header, or false if it does not exist. * Return the value of a given header, or false if it does not exist.
* *
* @param string $header * @param string $name
* @return string|bool * @return string|bool
*/ */
public function getHeader($header) { public function getHeader($name) {
$lowerName = strtolower($name);
if (isset($this->headerLookup[$lowerName])) {
$realName = $this->headerLookup[$lowerName];
if (isset($this->headers[$realName])) {
return $this->headers[$realName];
}
if ($this->hasHeader($header)) {
return $this->headers[$header];
} }
return false; return false;
@ -130,33 +147,63 @@ abstract class Message {
/** /**
* Add or update a header to a given value * Add or update a header to a given value
* *
* @param string $header * @param string name
* @param $value * @param $value
* @param string $value * @param string $value
*/ */
public function setHeader($header, $value) { public function setHeader($name, $value) {
$this->headers[$header] = $value;
$lowerName = strtolower($name);
// Check if a mapping already exists for this header.
// Remove it, if needed.
if (isset($this->headerLookup[$lowerName])
&& $this->headerLookup[$lowerName] !== $name) {
unset($this->headers[$this->headerLookup[$lowerName]]);
}
// Store the actual header.
$this->headers[$name] = $value;
// Store a mapping to the user's prefered case.
$this->headerLookup[$lowerName] = $name;
} }
/** /**
* Return if the response contains a header with the given key. * Return if the response contains a header with the given key.
* *
* @param $header * @param $name
* @return bool * @return bool
*/ */
public function hasHeader($header) { public function hasHeader($name) {
return isset($this->headers[$header]); $lowerName = strtolower($name);
return isset($this->headerLookup[$lowerName]);
} }
/** /**
* Remove a header. This method does nothing if the header does not exist. * Remove a header. This method does nothing if the header does not exist.
* *
* @param string $header * @param string $name
*/ */
public function unsetHeader($header) { public function unsetHeader($name) {
if ($this->hasHeader($header)) {
unset($this->headers[$header]); $lowerName = strtolower($name);
if (isset($this->headerLookup[$lowerName])) {
$realName = $this->headerLookup[$lowerName];
if (isset($this->headers[$realName])) {
unset($this->headers[$realName]);
}
unset($this->headerLookup[$lowerName]);
} }
} }
/** /**

View File

@ -6,7 +6,6 @@ require_once(dirname(__FILE__) . '/Message.inc.php');
require_once(dirname(__FILE__) . '/Response.inc.php'); require_once(dirname(__FILE__) . '/Response.inc.php');
require_once(dirname(__FILE__) . '/exceptions/CurlException.inc.php'); require_once(dirname(__FILE__) . '/exceptions/CurlException.inc.php');
// TODO: Ensure header names are case insensitive
// TODO: Include port in the URI // TODO: Include port in the URI
/** /**

View File

@ -4,8 +4,6 @@ namespace wellrested;
require_once(dirname(__FILE__) . '/Message.inc.php'); require_once(dirname(__FILE__) . '/Message.inc.php');
// TODO: Ensure header names are case insensitive
/******************************************************************************* /*******************************************************************************
* Response * Response
* *