public function addCookie($cookie, $ref_uri = null) { if (is_string($cookie)) { if ($this->_encodeCookie) { $cookie = Zend_Http_Cookie::fromString($cookie, $ref_uri); } else { $cookie = Zend_Http_Cookie::fromString($cookie, $ref_uri, false); } } if ($cookie instanceof Zend_Http_Cookie) { $domain = $cookie->getDomain(); $path = $cookie->getPath(); if (!isset($this->cookies[$domain])) { $this->cookies[$domain] = array(); } if (!isset($this->cookies[$domain][$path])) { $this->cookies[$domain][$path] = array(); } $this->cookies[$domain][$path][$cookie->getName()] = $cookie; $this->_rawCookies[] = $cookie; } else { require_once 'Zend/Http/Exception.php'; throw new Zend_Http_Exception('Supplient argument is not a valid cookie string or object'); } }
/** * Add a cookie to the request. If the client has no Cookie Jar, the cookies * will be added directly to the headers array as "Cookie" headers. * * @param Zend_Http_Cookie|string $cookie * @param string|null $value If "cookie" is a string, this is the cookie value. * @return Zend_Http_Client * @throws Zend_Http_Client_Exception */ public function setCookie($cookie, $value = null) { Zend_Loader::loadClass('Zend_Http_Cookie'); if (is_array($cookie)) { foreach ($cookie as $c => $v) { if (is_string($c)) { $this->setCookie($c, $v); } else { $this->setCookie($v); } } return $this; } if ($value !== null && $this->config['encodecookies']) { $value = urlencode($value); } if (isset($this->cookiejar)) { if ($cookie instanceof Zend_Http_Cookie) { $this->cookiejar->addCookie($cookie); } elseif (is_string($cookie) && $value !== null) { $cookie = Zend_Http_Cookie::fromString("{$cookie}={$value}", $this->uri, $this->config['encodecookies']); $this->cookiejar->addCookie($cookie); } } else { if ($cookie instanceof Zend_Http_Cookie) { $name = $cookie->getName(); $value = $cookie->getValue(); $cookie = $name; } if (preg_match("/[=,; \t\r\n\v\f]/", $cookie)) { /** @see Zend_Http_Client_Exception */ require_once 'Zend/Http/Client/Exception.php'; throw new Zend_Http_Client_Exception("Cookie name cannot contain these characters: =,; \t\r\n\v\f ({$cookie})"); } $value = addslashes($value); if (!isset($this->headers['cookie'])) { $this->headers['cookie'] = array('Cookie', ''); } $this->headers['cookie'][1] .= $cookie . '=' . $value . '; '; } return $this; }
/** * Add a cookie to the jar. Cookie should be passed either as a Zend_Http_Cookie object * or as a string - in which case an object is created from the string. * * @param Zend_Http_Cookie|string $cookie * @param Zend_Uri_Http|string $red_uri Optional reference URI (for domain, path, secure) */ public function addCookie($cookie, $ref_uri = null) { if (is_string($cookie)) { $cookie = Zend_Http_Cookie::fromString($cookie, $ref_uri); } if ($cookie instanceof Zend_Http_Cookie) { $domain = $cookie->getDomain(); $path = $cookie->getPath(); if (!isset($this->cookies[$domain])) { $this->cookies[$domain] = array(); } if (!isset($this->cookies[$domain][$path])) { $this->cookies[$domain][$path] = array(); } $this->cookies[$domain][$path][$cookie->getName()] = $cookie; } else { throw Zend::exception('Zend_Http_Exception', 'Supplient argument is not a valid cookie string or object'); } }
public function testIteratorAndCountable() { $jar = new Zend_Http_CookieJar(); $cookies = array(Zend_Http_Cookie::fromString('foo1=bar1; domain=.example.com; path=/a/b'), Zend_Http_Cookie::fromString('foo2=bar2; domain=.example.com; path=/a/b/')); foreach ($cookies as $cookie) { $jar->addCookie($cookie); } foreach ($jar as $cookie) { $this->assertTrue($cookie instanceof Zend_Http_Cookie); } $this->assertEquals(2, count($jar)); $this->assertFalse($jar->isEmpty()); $jar->reset(); $this->assertTrue($jar->isEmpty()); }
/** * Make sure we can set cookie objects with a jar * */ public function testSetCookieObjectJar() { $this->client->setUri($this->baseuri . 'testCookies.php'); $this->client->setCookieJar(); $refuri = $this->client->getUri(); $cookies = array(Zend_Http_Cookie::fromString('chocolate=chips', $refuri), Zend_Http_Cookie::fromString('crumble=apple', $refuri)); $strcookies = array(); foreach ($cookies as $c) { $this->client->setCookie($c); $strcookies[$c->getName()] = $c->getValue(); } $res = $this->client->request(); $this->assertEquals($res->getBody(), serialize($strcookies), 'Response body does not contain the expected cookies'); }
public function testFromStringFalse() { $cookie = Zend_Http_Cookie::fromString('foo; domain=www.exmaple.com'); $this->assertEquals(false, $cookie, 'fromString was expected to fail and return false'); $cookie = Zend_Http_Cookie::fromString('=bar; secure; domain=foo.nl'); $this->assertEquals(false, $cookie, 'fromString was expected to fail and return false'); $cookie = Zend_Http_Cookie::fromString('fo;o=bar; secure; domain=foo.nl'); $this->assertEquals(false, $cookie, 'fromString was expected to fail and return false'); }
/** * Test we can get all matching cookies for a request, and return as strings array / concat */ public function testGetMatchingCookiesAsStrings() { $jar = new Zend_Http_CookieJar(); $cookies = array(Zend_Http_Cookie::fromString('foo1=bar1; domain=.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)), Zend_Http_Cookie::fromString('foo2=bar2; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() + 3600)), Zend_Http_Cookie::fromString('foo3=bar3; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() - 3600)), Zend_Http_Cookie::fromString('foo4=bar4; domain=.foo.com; path=/;'), Zend_Http_Cookie::fromString('foo5=bar5; domain=.foo.com; path=/; secure; expires=' . date(DATE_COOKIE, time() + 3600)), Zend_Http_Cookie::fromString('foo6=bar6; domain=.foo.com; path=/otherpath; expires=' . date(DATE_COOKIE, time() + 3600)), Zend_Http_Cookie::fromString('foo7=bar7; domain=www.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)), Zend_Http_Cookie::fromString('foo8=bar8; domain=subdomain.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600))); foreach ($cookies as $cookie) { $jar->addCookie($cookie); } $this->assertEquals(8, count($jar->getAllCookies()), 'Cookie count is expected to be 8'); $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_STRING_ARRAY); $this->assertType('array', $cookies, '$cookies is expected to be an array, but it is not'); $this->assertType('string', $cookies[0], '$cookies[0] is expected to be a string'); $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_STRING_CONCAT); $this->assertType('string', $cookies, '$cookies is expected to be a string'); }
/** * Test that cookies with far future expiry date (beyond the 32 bit unsigned int range) are * not mistakenly marked as 'expired' * * @link http://framework.zend.com/issues/browse/ZF-5690 */ public function testZF5690OverflowingExpiryDate() { $expTime = "Sat, 29-Jan-2039 00:54:42 GMT"; $cookie = Zend_Http_Cookie::fromString("foo=bar; domain=.example.com; expires={$expTime}"); $this->assertFalse($cookie->isExpired(), 'Expiry: ' . $cookie->getExpiryTime()); }
/** * Add a cookie to the request. If the client has no Cookie Jar, the cookies * will be added directly to the headers array as "Cookie" headers. * * @param Zend_Http_Cookie|string $cookie * @param string|null $value If "cookie" is a string, this is the cookie value. * @return Zend_Http_Client */ public function setCookie($cookie, $value = null) { if (! class_exists('Zend_Http_Cookie')) require_once 'Zend/Http/Cookie.php'; if (is_array($cookie)) { foreach ($cookie as $c => $v) { if (is_string($c)) { $this->setCookie($c, $v); } else { $this->setCookie($v); } } return $this; } if ($value !== null) $value = urlencode($value); if (isset($this->cookiejar)) { if ($cookie instanceof Zend_Http_Cookie) { $this->cookiejar->addCookie($cookie); } elseif (is_string($cookie) && $value !== null) { $cookie = Zend_Http_Cookie::fromString("{$cookie}={$value}", $this->uri); $this->cookiejar->addCookie($cookie); } } else { if ($cookie instanceof Zend_Http_Cookie) { $name = $cookie->getName(); $value = $cookie->getValue(); $cookie = $name; } if (preg_match("/[=,; \t\r\n\013\014]/", $cookie)) throw new Zend_Http_Client_Exception("Cookie name cannot contain these characters: =,; \t\r\n\013\014 ({$cookie})"); $value = addslashes($value); if (! isset($this->headers['cookie'])) $this->headers['cookie'] = ''; $this->headers['cookie'] .= $cookie . '=' . $value . '; '; } return $this; }
/** * Make sure that paths with trailing slashes are matched as well as paths with no trailing slashes */ public function testMatchPathWithTrailingSlash() { $jar = new Zend_Http_CookieJar(); $cookies = array( Zend_Http_Cookie::fromString('foo1=bar1; domain=.example.com; path=/a/b'), Zend_Http_Cookie::fromString('foo2=bar2; domain=.example.com; path=/a/b/') ); foreach ($cookies as $cookie) $jar->addCookie($cookie); $cookies = $jar->getMatchingCookies('http://www.example.com/a/b/file.txt'); $this->assertType('array', $cookies); $this->assertEquals(2, count($cookies)); }