public function add(Cookie $cookie) { $result = $cookie->validate(); if ($result !== true) { if ($this->strictMode) { throw new InvalidCookieException($result); } else { return false; } } foreach ($this->cookies as $i => $c) { if ($c->getPath() != $cookie->getPath() || $c->getDomain() != $cookie->getDomain() || $c->getPorts() != $cookie->getPorts() || $c->getName() != $cookie->getName()) { continue; } if (!$cookie->getDiscard() && $c->getDiscard()) { unset($this->cookies[$i]); continue; } if ($cookie->getExpires() > $c->getExpires()) { unset($this->cookies[$i]); continue; } if ($cookie->getValue() !== $c->getValue()) { unset($this->cookies[$i]); continue; } return false; } $this->cookies[] = $cookie; return true; }
public function testHoldsValues() { $t = time(); $data = array('name' => 'foo', 'value' => 'baz', 'path' => '/bar', 'domain' => 'baz.com', 'expires' => $t, 'max_age' => 100, 'comment' => 'Hi', 'comment_url' => 'foo.com', 'port' => array(1, 2), 'version' => 2, 'secure' => true, 'discard' => true, 'http_only' => true, 'data' => array('foo' => 'baz', 'bar' => 'bam')); $cookie = new Cookie($data); $this->assertEquals($data, $cookie->toArray()); $this->assertEquals('foo', $cookie->getName()); $this->assertEquals('baz', $cookie->getValue()); $this->assertEquals('baz.com', $cookie->getDomain()); $this->assertEquals('/bar', $cookie->getPath()); $this->assertEquals($t, $cookie->getExpires()); $this->assertEquals(100, $cookie->getMaxAge()); $this->assertEquals('Hi', $cookie->getComment()); $this->assertEquals('foo.com', $cookie->getCommentUrl()); $this->assertEquals(array(1, 2), $cookie->getPorts()); $this->assertEquals(2, $cookie->getVersion()); $this->assertTrue($cookie->getSecure()); $this->assertTrue($cookie->getDiscard()); $this->assertTrue($cookie->getHttpOnly()); $this->assertEquals('baz', $cookie->getAttribute('foo')); $this->assertEquals('bam', $cookie->getAttribute('bar')); $this->assertEquals(array('foo' => 'baz', 'bar' => 'bam'), $cookie->getAttributes()); $cookie->setName('a')->setValue('b')->setPath('c')->setDomain('bar.com')->setExpires(10)->setMaxAge(200)->setComment('e')->setCommentUrl('f')->setPorts(array(80))->setVersion(3)->setSecure(false)->setHttpOnly(false)->setDiscard(false)->setAttribute('snoop', 'dog'); $this->assertEquals('a', $cookie->getName()); $this->assertEquals('b', $cookie->getValue()); $this->assertEquals('c', $cookie->getPath()); $this->assertEquals('bar.com', $cookie->getDomain()); $this->assertEquals(10, $cookie->getExpires()); $this->assertEquals(200, $cookie->getMaxAge()); $this->assertEquals('e', $cookie->getComment()); $this->assertEquals('f', $cookie->getCommentUrl()); $this->assertEquals(array(80), $cookie->getPorts()); $this->assertEquals(3, $cookie->getVersion()); $this->assertFalse($cookie->getSecure()); $this->assertFalse($cookie->getDiscard()); $this->assertFalse($cookie->getHttpOnly()); $this->assertEquals('dog', $cookie->getAttribute('snoop')); }
public function add(Cookie $cookie) { // Only allow cookies with set and valid domain, name, value $result = $cookie->validate(); if ($result !== true) { if ($this->strictMode) { throw new InvalidCookieException($result); } else { return false; } } // Resolve conflicts with previously set cookies foreach ($this->cookies as $i => $c) { // Two cookies are identical, when their path, domain, port and name are identical if ($c->getPath() != $cookie->getPath() || $c->getDomain() != $cookie->getDomain() || $c->getPorts() != $cookie->getPorts() || $c->getName() != $cookie->getName()) { continue; } // The previously set cookie is a discard cookie and this one is not so allow the new cookie to be set if (!$cookie->getDiscard() && $c->getDiscard()) { unset($this->cookies[$i]); continue; } // If the new cookie's expiration is further into the future, then replace the old cookie if ($cookie->getExpires() > $c->getExpires()) { unset($this->cookies[$i]); continue; } // If the value has changed, we better change it if ($cookie->getValue() !== $c->getValue()) { unset($this->cookies[$i]); continue; } // The cookie exists, so no need to continue return false; } $this->cookies[] = $cookie; return true; }
/** * If a cookie already exists and the server asks to set it again with a null value, the * cookie must be deleted. * * @param \Guzzle\Plugin\Cookie\Cookie $cookie */ private function removeCookieIfEmpty(Cookie $cookie) { $cookieValue = $cookie->getValue(); if ($cookieValue === null || $cookieValue === '') { $this->remove($cookie->getDomain(), $cookie->getPath(), $cookie->getName()); } }