GuzzleHttpCookieCookieJar

Cookie jar that stores cookies as an array.

Defined (1)

The class is defined in the following location(s).

/lib/Azure/GuzzleHttp/Cookie/CookieJar.php  
  1. class CookieJar implements CookieJarInterface 
  2. /** @var SetCookie[] Loaded cookie data */ 
  3. private $cookies = []; 
  4.  
  5. /** @var bool */ 
  6. private $strictMode; 
  7.  
  8. /** 
  9. * @param bool $strictMode Set to true to throw exceptions when invalid 
  10. * cookies are added to the cookie jar. 
  11. * @param array $cookieArray Array of SetCookie objects or a hash of 
  12. * arrays that can be used with the SetCookie 
  13. * constructor 
  14. */ 
  15. public function __construct($strictMode = false, $cookieArray = []) 
  16. $this->strictMode = $strictMode; 
  17.  
  18. foreach ($cookieArray as $cookie) { 
  19. if (!($cookie instanceof SetCookie)) { 
  20. $cookie = new SetCookie($cookie); 
  21. $this->setCookie($cookie); 
  22.  
  23. /** 
  24. * Create a new Cookie jar from an associative array and domain. 
  25. * @param array $cookies Cookies to create the jar from 
  26. * @param string $domain Domain to set the cookies to 
  27. * @return self 
  28. */ 
  29. public static function fromArray(array $cookies, $domain) 
  30. $cookieJar = new self(); 
  31. foreach ($cookies as $name => $value) { 
  32. $cookieJar->setCookie(new SetCookie([ 
  33. 'Domain' => $domain,  
  34. 'Name' => $name,  
  35. 'Value' => $value,  
  36. 'Discard' => true 
  37. ])); 
  38.  
  39. return $cookieJar; 
  40.  
  41. /** 
  42. * @deprecated 
  43. */ 
  44. public static function getCookieValue($value) 
  45. return $value; 
  46.  
  47. /** 
  48. * Evaluate if this cookie should be persisted to storage 
  49. * that survives between requests. 
  50. * @param SetCookie $cookie Being evaluated. 
  51. * @param bool $allowSessionCookies If we should persist session cookies 
  52. * @return bool 
  53. */ 
  54. public static function shouldPersist( 
  55. SetCookie $cookie,  
  56. $allowSessionCookies = false 
  57. ) { 
  58. if ($cookie->getExpires() || $allowSessionCookies) { 
  59. if (!$cookie->getDiscard()) { 
  60. return true; 
  61.  
  62. return false; 
  63.  
  64. public function toArray() 
  65. return array_map(function (SetCookie $cookie) { 
  66. return $cookie->toArray(); 
  67. }, $this->getIterator()->getArrayCopy()); 
  68.  
  69. public function clear($domain = null, $path = null, $name = null) 
  70. if (!$domain) { 
  71. $this->cookies = []; 
  72. return; 
  73. } elseif (!$path) { 
  74. $this->cookies = array_filter( 
  75. $this->cookies,  
  76. function (SetCookie $cookie) use ($path, $domain) { 
  77. return !$cookie->matchesDomain($domain); 
  78. ); 
  79. } elseif (!$name) { 
  80. $this->cookies = array_filter( 
  81. $this->cookies,  
  82. function (SetCookie $cookie) use ($path, $domain) { 
  83. return !($cookie->matchesPath($path) && 
  84. $cookie->matchesDomain($domain)); 
  85. ); 
  86. } else { 
  87. $this->cookies = array_filter( 
  88. $this->cookies,  
  89. function (SetCookie $cookie) use ($path, $domain, $name) { 
  90. return !($cookie->getName() == $name && 
  91. $cookie->matchesPath($path) && 
  92. $cookie->matchesDomain($domain)); 
  93. ); 
  94.  
  95. public function clearSessionCookies() 
  96. $this->cookies = array_filter( 
  97. $this->cookies,  
  98. function (SetCookie $cookie) { 
  99. return !$cookie->getDiscard() && $cookie->getExpires(); 
  100. ); 
  101.  
  102. public function setCookie(SetCookie $cookie) 
  103. // If the name string is empty (but not 0), ignore the set-cookie 
  104. // string entirely. 
  105. $name = $cookie->getName(); 
  106. if (!$name && $name !== '0') { 
  107. return false; 
  108.  
  109. // Only allow cookies with set and valid domain, name, value 
  110. $result = $cookie->validate(); 
  111. if ($result !== true) { 
  112. if ($this->strictMode) { 
  113. throw new \RuntimeException('Invalid cookie: ' . $result); 
  114. } else { 
  115. $this->removeCookieIfEmpty($cookie); 
  116. return false; 
  117.  
  118. // Resolve conflicts with previously set cookies 
  119. foreach ($this->cookies as $i => $c) { 
  120.  
  121. // Two cookies are identical, when their path, and domain are 
  122. // identical. 
  123. if ($c->getPath() != $cookie->getPath() || 
  124. $c->getDomain() != $cookie->getDomain() || 
  125. $c->getName() != $cookie->getName() 
  126. ) { 
  127. continue; 
  128.  
  129. // The previously set cookie is a discard cookie and this one is 
  130. // not so allow the new cookie to be set 
  131. if (!$cookie->getDiscard() && $c->getDiscard()) { 
  132. unset($this->cookies[$i]); 
  133. continue; 
  134.  
  135. // If the new cookie's expiration is further into the future, then 
  136. // replace the old cookie 
  137. if ($cookie->getExpires() > $c->getExpires()) { 
  138. unset($this->cookies[$i]); 
  139. continue; 
  140.  
  141. // If the value has changed, we better change it 
  142. if ($cookie->getValue() !== $c->getValue()) { 
  143. unset($this->cookies[$i]); 
  144. continue; 
  145.  
  146. // The cookie exists, so no need to continue 
  147. return false; 
  148.  
  149. $this->cookies[] = $cookie; 
  150.  
  151. return true; 
  152.  
  153. public function count() 
  154. return count($this->cookies); 
  155.  
  156. public function getIterator() 
  157. return new \ArrayIterator(array_values($this->cookies)); 
  158.  
  159. public function extractCookies( 
  160. RequestInterface $request,  
  161. ResponseInterface $response 
  162. ) { 
  163. if ($cookieHeader = $response->getHeader('Set-Cookie')) { 
  164. foreach ($cookieHeader as $cookie) { 
  165. $sc = SetCookie::fromString($cookie); 
  166. if (!$sc->getDomain()) { 
  167. $sc->setDomain($request->getUri()->getHost()); 
  168. $this->setCookie($sc); 
  169.  
  170. public function withCookieHeader(RequestInterface $request) 
  171. $values = []; 
  172. $uri = $request->getUri(); 
  173. $scheme = $uri->getScheme(); 
  174. $host = $uri->getHost(); 
  175. $path = $uri->getPath() ?: '/'; 
  176.  
  177. foreach ($this->cookies as $cookie) { 
  178. if ($cookie->matchesPath($path) && 
  179. $cookie->matchesDomain($host) && 
  180. !$cookie->isExpired() && 
  181. (!$cookie->getSecure() || $scheme === 'https') 
  182. ) { 
  183. $values[] = $cookie->getName() . '=' 
  184. . $cookie->getValue(); 
  185.  
  186. return $values 
  187. ? $request->withHeader('Cookie', implode('; ', $values)) 
  188. : $request; 
  189.  
  190. /** 
  191. * If a cookie already exists and the server asks to set it again with a 
  192. * null value, the cookie must be deleted. 
  193. * @param SetCookie $cookie 
  194. */ 
  195. private function removeCookieIfEmpty(SetCookie $cookie) 
  196. $cookieValue = $cookie->getValue(); 
  197. if ($cookieValue === null || $cookieValue === '') { 
  198. $this->clear( 
  199. $cookie->getDomain(),  
  200. $cookie->getPath(),  
  201. $cookie->getName() 
  202. );