Skip to content

Commit

Permalink
Restructuring internal header structure, adding granular headers, mov…
Browse files Browse the repository at this point in the history
…e HeaderComparision because it is only for testing, and using a header collection
  • Loading branch information
mtdowling committed May 30, 2013
1 parent 922675c commit 598567d
Show file tree
Hide file tree
Showing 27 changed files with 678 additions and 503 deletions.
176 changes: 55 additions & 121 deletions src/Guzzle/Http/Message/AbstractMessage.php
Expand Up @@ -4,32 +4,42 @@

use Guzzle\Common\Collection;
use Guzzle\Common\Exception\InvalidArgumentException;
use Guzzle\Http\Message\Header\HeaderCollection;
use Guzzle\Http\Message\Header\HeaderFactory;
use Guzzle\Http\Message\Header\HeaderInterface;

/**
* Abstract HTTP request/response message
*/
abstract class AbstractMessage implements MessageInterface
{
/**
* @var array HTTP headers
* @var array HTTP header collection
*/
protected $headers = array();
protected $headers;

/**
* @var Collection Custom message parameters that are extendable by plugins
* @var HeaderFactoryInterface $headerFactory
*/
protected $params;
protected $headerFactory;

/**
* @var array Cache-Control directive information
* @var Collection Custom message parameters that are extendable by plugins
*/
private $cacheControl = array();
protected $params;

/*
/**
* @var string HTTP protocol version of the message
*/
protected $protocolVersion = '1.1';

public function __construct()
{
$this->params = new Collection();
$this->headerFactory = new HeaderFactory();
$this->headers = new HeaderCollection();
}

/**
* {@inheritdoc}
*/
Expand All @@ -43,13 +53,13 @@ public function getParams()
*/
public function addHeader($header, $value)
{
$key = strtolower($header);
if (!isset($this->headers[$key])) {
$this->headers[$key] = new Header($header, $value);
if (isset($this->headers[$header])) {
$this->headers[$header]->add($value);
} elseif ($value instanceof HeaderInterface) {
$this->headers[$header] = $value;
} else {
$this->headers[$key]->add($value, $header);
$this->headers[$header] = $this->headerFactory->createHeader($header, $value);
}
$this->changedHeader($key);

return $this;
}
Expand All @@ -71,32 +81,23 @@ public function addHeaders(array $headers)
*/
public function getHeader($header, $string = false)
{
$key = strtolower($header);
if (!isset($this->headers[$key])) {
$value = $this->headers[$header];

if ($value === null) {
return null;
} elseif ($string) {
return (string) $value;
} else {
return $value;
}

return $string ? (string) $this->headers[$key] : $this->headers[$key];
}

/**
* {@inheritdoc}
*/
public function getHeaders($asObjects = false)
public function getHeaders()
{
if ($asObjects) {
$result = $this->headers;
} else {
$result = array();
// Convert all of the headers into a collection
foreach ($this->headers as $header) {
foreach ($header->raw() as $key => $value) {
$result[$key] = $value;
}
}
}

return new Collection($result);
return $this->headers;
}

/**
Expand All @@ -106,10 +107,7 @@ public function getHeaderLines()
{
$headers = array();
foreach ($this->headers as $value) {
$glue = $value->getGlue();
foreach ($value->raw() as $key => $v) {
$headers[] = rtrim($key . ': ' . implode($glue, $v));
}
$headers[] = $value->getName() . ': ' . $value;
}

return $headers;
Expand All @@ -120,20 +118,8 @@ public function getHeaderLines()
*/
public function setHeader($header, $value)
{
// Remove any existing header
$key = strtolower($header);
unset($this->headers[$key]);

if ($value instanceof Header) {
$this->headers[$key] = $value;
} else {
// Allow for 0, '', and NULL to be set
if (!$value) {
$value = array($value);
}
$this->headers[$key] = new Header($header, $value);
}
$this->changedHeader($key);
unset($this->headers[$header]);
$this->addHeader($header, $value);

return $this;
}
Expand All @@ -143,21 +129,11 @@ public function setHeader($header, $value)
*/
public function setHeaders(array $headers)
{
// Get the keys that are changing
$changed = array_keys($this->headers);
// Erase the old headers
$this->headers = array();
// Add the new headers
$this->headers->clear();
foreach ($headers as $key => $value) {
$changed[] = $key;
$this->addHeader($key, $value);
}

// Notify of the changed headers
foreach (array_unique($changed) as $header) {
$this->changedHeader(strtolower($header));
}

return $this;
}

Expand All @@ -166,17 +142,15 @@ public function setHeaders(array $headers)
*/
public function hasHeader($header)
{
return array_key_exists(strtolower($header), $this->headers);
return isset($this->headers[$header]);
}

/**
* {@inheritdoc}
*/
public function removeHeader($header)
{
$header = strtolower($header);
unset($this->headers[$header]);
$this->changedHeader($header);

return $this;
}
Expand All @@ -198,93 +172,53 @@ public function setTokenizedHeader($header, $data, $token = ';')
}

/**
* {@inheritdoc}
* @deprecated
*/
public function getCacheControlDirective($directive)
{
if (!isset($this->cacheControl[$directive])) {
if (!($header = $this->getHeader('Cache-Control'))) {
return null;
}

$directive = $this->cacheControl[$directive];

if (is_array($directive) && !empty($directive)) {
return $directive[0];
}

return $directive;
return $header->getDirective($directive);
}

/**
* {@inheritdoc}
* @deprecated
*/
public function hasCacheControlDirective($directive)
{
return isset($this->cacheControl[$directive]);
if ($header = $this->getHeader('Cache-Control')) {
return $header->hasDirective($directive);
} else {
return false;
}
}

/**
* {@inheritdoc}
* @deprecated
*/
public function addCacheControlDirective($directive, $value = true)
{
$this->cacheControl[$directive] = $value;
$this->rebuildCacheControlDirective();

return $this;
}

/**
* {@inheritdoc}
*/
public function removeCacheControlDirective($directive)
{
if (array_key_exists($directive, $this->cacheControl)) {
unset($this->cacheControl[$directive]);
$this->rebuildCacheControlDirective();
if (!($header = $this->getHeader('Cache-Control'))) {
$this->addHeader('Cache-Control', '');
$header = $this->getHeader('Cache-Control');
}

return $this;
}
$header->addDirective($directive, $value);

/**
* Check to see if the modified headers need to reset any of the managed
* headers like cache-control
*
* @param string $header Header that changed
*/
protected function changedHeader($header)
{
if ($header == 'cache-control') {
$this->parseCacheControlDirective();
}
return $this;
}

/**
* Parse the Cache-Control HTTP header into an array
* @deprecated
*/
private function parseCacheControlDirective()
public function removeCacheControlDirective($directive)
{
$this->cacheControl = array();
if ($header = $this->getHeader('Cache-Control')) {
foreach ($header->parseParams() as $collection) {
foreach ($collection as $key => $value) {
$value = $value === '' ? true : $value;
$this->cacheControl[$key] = $value;
}
}
$header->removeDirective($directive);
}
}

/**
* Rebuild the Cache-Control HTTP header using the user-specified values
*/
private function rebuildCacheControlDirective()
{
$cacheControl = array();
foreach ($this->cacheControl as $key => $value) {
$cacheControl[] = ($value === true) ? $key : ($key . '=' . $value);
}
$this->headers['cache-control'] = new Header('Cache-Control', $cacheControl, ', ');
return $this;
}
}

0 comments on commit 598567d

Please sign in to comment.