1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-24 18:13:00 +01:00

Removing header glue as it is always ",". Updating cookie header accordingly.

This commit is contained in:
Michael Dowling 2013-08-11 15:08:00 -07:00
parent f132b12195
commit 118f95c0d9
3 changed files with 20 additions and 54 deletions

View File

@ -7,11 +7,6 @@ namespace Guzzle\Http\Header;
*/
class Cookie extends DefaultHeader
{
public function __construct($name, $values = array())
{
parent::__construct($name, $values, ';');
}
/**
* Add a cookie
*
@ -22,7 +17,15 @@ class Cookie extends DefaultHeader
*/
public function addCookie($name, $value)
{
return $this->add("{$name}={$value}");
// Quote the value if it is not already and contains problematic characters
if (substr($value, 0, 1) !== '"' && substr($value, -1, 1) !== '"' && strpbrk($value, ';,')) {
$value = '"' . $value . '"';
}
$val = "{$name}={$value}";
$this->values = [isset($this->values[0]) ? "{$this->values[0]}; {$val}": $val];
return $this;
}
/**
@ -48,10 +51,11 @@ class Cookie extends DefaultHeader
{
$values = $this->getCookies();
unset($values[$name]);
$this->values = [];
foreach ($values as $k => $v) {
$this->values[] = $k . '=' . $v;
$this->values = [''];
foreach ($values as $key => $value) {
$this->values[0] .= "{$key}={$value}; ";
}
$this->values[0] = rtrim($this->values[0], '; ');
return $this;
}
@ -77,12 +81,6 @@ class Cookie extends DefaultHeader
*/
public function getCookies()
{
$values = [];
foreach ($this->toArray() as $value) {
$parts = explode('=', $value);
$values[$parts[0]] = isset($parts[1]) ? $parts[1] : null;
}
return $values;
return $this->parseParams()[0];
}
}

View File

@ -8,18 +8,15 @@ namespace Guzzle\Http\Header;
class DefaultHeader implements HeaderInterface
{
protected $values = array();
protected $glue;
private $headerName;
/**
* @param string $name Name of the header
* @param array|string $values Values of the header as an array or a scalar
* @param string $glue Glue used to combine multiple values into a string
*/
public function __construct($name, $values = array(), $glue = ',')
public function __construct($name, $values = array())
{
$this->headerName = trim($name);
$this->glue = $glue;
foreach ((array) $values as $value) {
foreach ((array) $value as $v) {
@ -30,7 +27,7 @@ class DefaultHeader implements HeaderInterface
public function __toString()
{
return implode($this->glue . ' ', $this->toArray());
return implode(', ', $this->toArray());
}
public function add($value)
@ -52,18 +49,6 @@ class DefaultHeader implements HeaderInterface
return $this;
}
public function setGlue($glue)
{
$this->glue = $glue;
return $this;
}
public function getGlue()
{
return $this->glue;
}
public function hasValue($searchValue)
{
return in_array($searchValue, $this->toArray());
@ -119,8 +104,7 @@ class DefaultHeader implements HeaderInterface
/**
* Normalize the header to be a single header with an array of values.
*
* If any values of the header contains the glue string value (e.g. ","), then the value will be exploded into
* multiple entries in the header.
* If any values of the header contains a comma, then the value will be exploded into multiple entries in the header
*
* @return self
*/
@ -129,8 +113,8 @@ class DefaultHeader implements HeaderInterface
$values = $this->toArray();
for ($i = 0, $total = count($values); $i < $total; $i++) {
if (strpos($values[$i], $this->glue) !== false) {
foreach (preg_split('/' . preg_quote($this->glue) . '(?=([^"]*"[^"]*")*[^"]*$)/', $values[$i]) as $v) {
if (strpos($values[$i], ',') !== false) {
foreach (preg_split('/,(?=([^"]*"[^"]*")*[^"]*$)/', $values[$i]) as $v) {
$values[] = trim($v);
}
unset($values[$i]);
@ -151,7 +135,7 @@ class DefaultHeader implements HeaderInterface
*/
private function trimHeader($str)
{
static $trimmed = "\"' \n\t";
static $trimmed = "\"' \n\t\r";
return trim($str, $trimmed);
}

View File

@ -38,22 +38,6 @@ interface HeaderInterface extends ToArrayInterface, \Countable, \IteratorAggrega
*/
public function setName($name);
/**
* Change the glue used to implode the values
*
* @param string $glue Glue used to implode multiple values
*
* @return self
*/
public function setGlue($glue);
/**
* Get the glue used to implode multiple values into a string
*
* @return string
*/
public function getGlue();
/**
* Remove a specific value from the header
*