Requests_Cookie_Jar

Cookie holder object.

Defined (1)

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

/wp-includes/Requests/Cookie/Jar.php  
  1. class Requests_Cookie_Jar implements ArrayAccess, IteratorAggregate { 
  2. /** 
  3. * Actual item data 
  4. * @var array 
  5. */ 
  6. protected $cookies = array(); 
  7.  
  8. /** 
  9. * Create a new jar 
  10. * @param array $cookies Existing cookie values 
  11. */ 
  12. public function __construct($cookies = array()) { 
  13. $this->cookies = $cookies; 
  14.  
  15. /** 
  16. * Normalise cookie data into a Requests_Cookie 
  17. * @param string|Requests_Cookie $cookie 
  18. * @return Requests_Cookie 
  19. */ 
  20. public function normalize_cookie($cookie, $key = null) { 
  21. if ($cookie instanceof Requests_Cookie) { 
  22. return $cookie; 
  23.  
  24. return Requests_Cookie::parse($cookie, $key); 
  25.  
  26. /** 
  27. * Normalise cookie data into a Requests_Cookie 
  28. * @codeCoverageIgnore 
  29. * @deprecated Use {@see Requests_Cookie_Jar::normalize_cookie} 
  30. * @return Requests_Cookie 
  31. */ 
  32. public function normalizeCookie($cookie, $key = null) { 
  33. return $this->normalize_cookie($cookie, $key); 
  34.  
  35. /** 
  36. * Check if the given item exists 
  37. * @param string $key Item key 
  38. * @return boolean Does the item exist? 
  39. */ 
  40. public function offsetExists($key) { 
  41. return isset($this->cookies[$key]); 
  42.  
  43. /** 
  44. * Get the value for the item 
  45. * @param string $key Item key 
  46. * @return string Item value 
  47. */ 
  48. public function offsetGet($key) { 
  49. if (!isset($this->cookies[$key])) { 
  50. return null; 
  51.  
  52. return $this->cookies[$key]; 
  53.  
  54. /** 
  55. * Set the given item 
  56. * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`) 
  57. * @param string $key Item name 
  58. * @param string $value Item value 
  59. */ 
  60. public function offsetSet($key, $value) { 
  61. if ($key === null) { 
  62. throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset'); 
  63.  
  64. $this->cookies[$key] = $value; 
  65.  
  66. /** 
  67. * Unset the given header 
  68. * @param string $key 
  69. */ 
  70. public function offsetUnset($key) { 
  71. unset($this->cookies[$key]); 
  72.  
  73. /** 
  74. * Get an iterator for the data 
  75. * @return ArrayIterator 
  76. */ 
  77. public function getIterator() { 
  78. return new ArrayIterator($this->cookies); 
  79.  
  80. /** 
  81. * Register the cookie handler with the request's hooking system 
  82. * @param Requests_Hooker $hooks Hooking system 
  83. */ 
  84. public function register(Requests_Hooker $hooks) { 
  85. $hooks->register('requests.before_request', array($this, 'before_request')); 
  86. $hooks->register('requests.before_redirect_check', array($this, 'before_redirect_check')); 
  87.  
  88. /** 
  89. * Add Cookie header to a request if we have any 
  90. * As per RFC 6265, cookies are separated by '; ' 
  91. * @param string $url 
  92. * @param array $headers 
  93. * @param array $data 
  94. * @param string $type 
  95. * @param array $options 
  96. */ 
  97. public function before_request($url, &$headers, &$data, &$type, &$options) { 
  98. if (!$url instanceof Requests_IRI) { 
  99. $url = new Requests_IRI($url); 
  100.  
  101. if (!empty($this->cookies)) { 
  102. $cookies = array(); 
  103. foreach ($this->cookies as $key => $cookie) { 
  104. $cookie = $this->normalize_cookie($cookie, $key); 
  105.  
  106. // Skip expired cookies 
  107. if ($cookie->is_expired()) { 
  108. continue; 
  109.  
  110. if ($cookie->domain_matches($url->host)) { 
  111. $cookies[] = $cookie->format_for_header(); 
  112.  
  113. $headers['Cookie'] = implode('; ', $cookies); 
  114.  
  115. /** 
  116. * Parse all cookies from a response and attach them to the response 
  117. * @var Requests_Response $response 
  118. */ 
  119. public function before_redirect_check(Requests_Response &$return) { 
  120. $url = $return->url; 
  121. if (!$url instanceof Requests_IRI) { 
  122. $url = new Requests_IRI($url); 
  123.  
  124. $cookies = Requests_Cookie::parse_from_headers($return->headers, $url); 
  125. $this->cookies = array_merge($this->cookies, $cookies); 
  126. $return->cookies = $this;