Skip to content

Commit

Permalink
zendframework#127 Set-Cookie: added ability to set value without URL …
Browse files Browse the repository at this point in the history
…encoding
  • Loading branch information
thomasvargiu committed Oct 13, 2018
1 parent bdfe6ca commit 926356f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Header/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,20 @@ public function __construct(array $array = [])
parent::__construct($array, ArrayObject::ARRAY_AS_PROPS);
}

/**
* @param bool $encodeValue
*
* @return $this
*/
public function setEncodeValue($encodeValue)
{
$this->encodeValue = (bool) $encodeValue;
return $this;
}

/**
* @return bool
*/
public function getEncodeValue()
{
return $this->encodeValue;
Expand Down
29 changes: 28 additions & 1 deletion src/Header/SetCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ class SetCookie implements MultipleHeaderInterface
*/
protected $httponly;

/**
* @var bool
*/
protected $encodeValue = true;

/**
* @static
* @throws Exception\InvalidArgumentException
Expand All @@ -99,6 +104,7 @@ public static function fromString($headerLine, $bypassHeaderFieldName = false)
if ($setCookieProcessor === null) {
$setCookieClass = get_called_class();
$setCookieProcessor = function ($headerLine) use ($setCookieClass) {
/** @var SetCookie $header */
$header = new $setCookieClass();
$keyValuePairs = preg_split('#;\s*#', $headerLine);

Expand All @@ -115,6 +121,11 @@ public static function fromString($headerLine, $bypassHeaderFieldName = false)
if ($header->getName() === null) {
$header->setName($headerKey);
$header->setValue(urldecode($headerValue));

// set no encode value if raw and encoded values are the same
if (urldecode($headerValue) === $headerValue) {
$header->setEncodeValue(false);
}
continue;
}

Expand Down Expand Up @@ -213,6 +224,22 @@ public function __construct(
->setHttpOnly($httponly);
}

/**
* @return bool
*/
public function getEncodeValue()
{
return $this->encodeValue;
}

/**
* @param bool $encodeValue
*/
public function setEncodeValue($encodeValue)
{
$this->encodeValue = (bool) $encodeValue;
}

/**
* @return string 'Set-Cookie'
*/
Expand All @@ -231,7 +258,7 @@ public function getFieldValue()
return '';
}

$value = urlencode($this->getValue());
$value = $this->encodeValue ? urlencode($this->getValue()) : $this->getValue();
if ($this->hasQuoteFieldValue()) {
$value = '"' . $value . '"';
}
Expand Down

0 comments on commit 926356f

Please sign in to comment.