From 40ca6bea874c9a6d28e13293fd4d06118f8a13b5 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Thu, 27 Dec 2012 08:41:56 -0500 Subject: [PATCH] Make field names for headers case insensitive. --- Message.inc.php | 75 +++++++++++++++++++++++++++++++++++++++--------- Request.inc.php | 1 - Response.inc.php | 2 -- 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/Message.inc.php b/Message.inc.php index 3f91e35..8fb9001 100644 --- a/Message.inc.php +++ b/Message.inc.php @@ -24,6 +24,15 @@ abstract class Message { */ 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. * @var string @@ -114,13 +123,21 @@ abstract class Message { /** * Return the value of a given header, or false if it does not exist. * - * @param string $header + * @param string $name * @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; @@ -130,33 +147,63 @@ abstract class Message { /** * Add or update a header to a given value * - * @param string $header + * @param string name * @param $value * @param string $value */ - public function setHeader($header, $value) { - $this->headers[$header] = $value; + public function setHeader($name, $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. * - * @param $header + * @param $name * @return bool */ - public function hasHeader($header) { - return isset($this->headers[$header]); + public function hasHeader($name) { + $lowerName = strtolower($name); + return isset($this->headerLookup[$lowerName]); } /** * Remove a header. This method does nothing if the header does not exist. * - * @param string $header + * @param string $name */ - public function unsetHeader($header) { - if ($this->hasHeader($header)) { - unset($this->headers[$header]); + public function unsetHeader($name) { + + $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]); + } + } /** diff --git a/Request.inc.php b/Request.inc.php index 4ee5ff2..888052c 100644 --- a/Request.inc.php +++ b/Request.inc.php @@ -6,7 +6,6 @@ require_once(dirname(__FILE__) . '/Message.inc.php'); require_once(dirname(__FILE__) . '/Response.inc.php'); require_once(dirname(__FILE__) . '/exceptions/CurlException.inc.php'); -// TODO: Ensure header names are case insensitive // TODO: Include port in the URI /** diff --git a/Response.inc.php b/Response.inc.php index 976ac36..c9e5e32 100644 --- a/Response.inc.php +++ b/Response.inc.php @@ -4,8 +4,6 @@ namespace wellrested; require_once(dirname(__FILE__) . '/Message.inc.php'); -// TODO: Ensure header names are case insensitive - /******************************************************************************* * Response *